blob: 44118b1375d1d5876c3c95d33273cd7ce25b1581 [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
Bram Moolenaara26b9702020-04-18 19:53:28 +0200733 hi = hash_find(&func_hashtab,
734 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200735 if (!HASHITEM_EMPTY(hi))
736 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100737
738 return NULL;
739}
740
741/*
742 * Find a function by name, return pointer to it in ufuncs.
743 * "cctx" is passed in a :def function to find imported functions.
744 * Return NULL for unknown or dead function.
745 */
746 ufunc_T *
747find_func(char_u *name, cctx_T *cctx)
748{
749 ufunc_T *fp = find_func_even_dead(name, cctx);
750
751 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
752 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200753 return NULL;
754}
755
756/*
757 * Copy the function name of "fp" to buffer "buf".
758 * "buf" must be able to hold the function name plus three bytes.
759 * Takes care of script-local function names.
760 */
761 static void
762cat_func_name(char_u *buf, ufunc_T *fp)
763{
764 if (fp->uf_name[0] == K_SPECIAL)
765 {
766 STRCPY(buf, "<SNR>");
767 STRCAT(buf, fp->uf_name + 3);
768 }
769 else
770 STRCPY(buf, fp->uf_name);
771}
772
773/*
774 * Add a number variable "name" to dict "dp" with value "nr".
775 */
776 static void
777add_nr_var(
778 dict_T *dp,
779 dictitem_T *v,
780 char *name,
781 varnumber_T nr)
782{
783 STRCPY(v->di_key, name);
784 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
785 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
786 v->di_tv.v_type = VAR_NUMBER;
787 v->di_tv.v_lock = VAR_FIXED;
788 v->di_tv.vval.v_number = nr;
789}
790
791/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100792 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200793 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100794 static void
795free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200796{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100797 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200798
799 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
800 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100801 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200802
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100803 // When garbage collecting a funccall_T may be freed before the
804 // function that references it, clear its uf_scoped field.
805 // The function may have been redefined and point to another
806 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200807 if (fp != NULL && fp->uf_scoped == fc)
808 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200809 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200810 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200811
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200812 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200813 vim_free(fc);
814}
815
816/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100817 * Free "fc" and what it contains.
818 * Can be called only when "fc" is kept beyond the period of it called,
819 * i.e. after cleanup_function_call(fc).
820 */
821 static void
822free_funccal_contents(funccall_T *fc)
823{
824 listitem_T *li;
825
826 // Free all l: variables.
827 vars_clear(&fc->l_vars.dv_hashtab);
828
829 // Free all a: variables.
830 vars_clear(&fc->l_avars.dv_hashtab);
831
832 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200833 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100834 clear_tv(&li->li_tv);
835
836 free_funccal(fc);
837}
838
839/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200840 * Handle the last part of returning from a function: free the local hashtable.
841 * Unless it is still in use by a closure.
842 */
843 static void
844cleanup_function_call(funccall_T *fc)
845{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100846 int may_free_fc = fc->fc_refcount <= 0;
847 int free_fc = TRUE;
848
Bram Moolenaar6914c642017-04-01 21:21:30 +0200849 current_funccal = fc->caller;
850
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100851 // Free all l: variables if not referred.
852 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
853 vars_clear(&fc->l_vars.dv_hashtab);
854 else
855 free_fc = FALSE;
856
857 // If the a:000 list and the l: and a: dicts are not referenced and
858 // there is no closure using it, we can free the funccall_T and what's
859 // in it.
860 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
861 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200862 else
863 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100864 int todo;
865 hashitem_T *hi;
866 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200867
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100868 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200869
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100870 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200871 todo = (int)fc->l_avars.dv_hashtab.ht_used;
872 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
873 {
874 if (!HASHITEM_EMPTY(hi))
875 {
876 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100877 di = HI2DI(hi);
878 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200879 }
880 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100881 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200882
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100883 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
884 fc->l_varlist.lv_first = NULL;
885 else
886 {
887 listitem_T *li;
888
889 free_fc = FALSE;
890
891 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200892 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +0200893 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100894 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100895
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100896 if (free_fc)
897 free_funccal(fc);
898 else
899 {
900 static int made_copy = 0;
901
902 // "fc" is still in use. This can happen when returning "a:000",
903 // assigning "l:" to a global variable or defining a closure.
904 // Link "fc" in the list for garbage collection later.
905 fc->caller = previous_funccal;
906 previous_funccal = fc;
907
908 if (want_garbage_collect)
909 // If garbage collector is ready, clear count.
910 made_copy = 0;
911 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100912 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100913 // We have made a lot of copies, worth 4 Mbyte. This can happen
914 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100915 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100916 // too much memory.
917 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100918 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100919 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200920 }
921}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100922/*
923 * Unreference "fc": decrement the reference count and free it when it
924 * becomes zero. "fp" is detached from "fc".
925 * When "force" is TRUE we are exiting.
926 */
927 static void
928funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
929{
930 funccall_T **pfc;
931 int i;
932
933 if (fc == NULL)
934 return;
935
936 if (--fc->fc_refcount <= 0 && (force || (
937 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
938 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
939 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
940 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
941 {
942 if (fc == *pfc)
943 {
944 *pfc = fc->caller;
945 free_funccal_contents(fc);
946 return;
947 }
948 }
949 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
950 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
951 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
952}
953
954/*
955 * Remove the function from the function hashtable. If the function was
956 * deleted while it still has references this was already done.
957 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
958 */
959 static int
960func_remove(ufunc_T *fp)
961{
962 hashitem_T *hi;
963
964 // Return if it was already virtually deleted.
965 if (fp->uf_flags & FC_DEAD)
966 return FALSE;
967
968 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
969 if (!HASHITEM_EMPTY(hi))
970 {
971 // When there is a def-function index do not actually remove the
972 // function, so we can find the index when defining the function again.
973 if (fp->uf_dfunc_idx >= 0)
974 fp->uf_flags |= FC_DEAD;
975 else
976 hash_remove(&func_hashtab, hi);
977 return TRUE;
978 }
979 return FALSE;
980}
981
982 static void
983func_clear_items(ufunc_T *fp)
984{
985 ga_clear_strings(&(fp->uf_args));
986 ga_clear_strings(&(fp->uf_def_args));
987 ga_clear_strings(&(fp->uf_lines));
988 VIM_CLEAR(fp->uf_name_exp);
989 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100990 VIM_CLEAR(fp->uf_def_arg_idx);
991 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200992 while (fp->uf_type_list.ga_len > 0)
993 vim_free(((type_T **)fp->uf_type_list.ga_data)
994 [--fp->uf_type_list.ga_len]);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995 ga_clear(&fp->uf_type_list);
996#ifdef FEAT_PROFILE
997 VIM_CLEAR(fp->uf_tml_count);
998 VIM_CLEAR(fp->uf_tml_total);
999 VIM_CLEAR(fp->uf_tml_self);
1000#endif
1001}
1002
1003/*
1004 * Free all things that a function contains. Does not free the function
1005 * itself, use func_free() for that.
1006 * When "force" is TRUE we are exiting.
1007 */
1008 static void
1009func_clear(ufunc_T *fp, int force)
1010{
1011 if (fp->uf_cleared)
1012 return;
1013 fp->uf_cleared = TRUE;
1014
1015 // clear this function
1016 func_clear_items(fp);
1017 funccal_unref(fp->uf_scoped, fp, force);
1018 delete_def_function(fp);
1019}
1020
1021/*
1022 * Free a function and remove it from the list of functions. Does not free
1023 * what a function contains, call func_clear() first.
1024 */
1025 static void
1026func_free(ufunc_T *fp)
1027{
1028 // Only remove it when not done already, otherwise we would remove a newer
1029 // version of the function with the same name.
1030 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1031 func_remove(fp);
1032
1033 if ((fp->uf_flags & FC_DEAD) == 0)
1034 vim_free(fp);
1035}
1036
1037/*
1038 * Free all things that a function contains and free the function itself.
1039 * When "force" is TRUE we are exiting.
1040 */
1041 static void
1042func_clear_free(ufunc_T *fp, int force)
1043{
1044 func_clear(fp, force);
1045 func_free(fp);
1046}
1047
Bram Moolenaar6914c642017-04-01 21:21:30 +02001048
1049/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001050 * Call a user function.
1051 */
1052 static void
1053call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001054 ufunc_T *fp, // pointer to function
1055 int argcount, // nr of args
1056 typval_T *argvars, // arguments
1057 typval_T *rettv, // return value
1058 linenr_T firstline, // first line of range
1059 linenr_T lastline, // last line of range
1060 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001061{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001062 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001063 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001064 funccall_T *fc;
1065 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001066 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001067 static int depth = 0;
1068 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001069 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001070 int i;
1071 int ai;
1072 int islambda = FALSE;
1073 char_u numbuf[NUMBUFLEN];
1074 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001075#ifdef FEAT_PROFILE
1076 proftime_T wait_start;
1077 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001078 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001079#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001080 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001081
Bram Moolenaare38eab22019-12-05 21:50:01 +01001082 // If depth of calling is getting too high, don't execute the function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001083 if (depth >= p_mfd)
1084 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001085 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001086 rettv->v_type = VAR_NUMBER;
1087 rettv->vval.v_number = -1;
1088 return;
1089 }
1090 ++depth;
1091
Bram Moolenaare38eab22019-12-05 21:50:01 +01001092 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001093
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001094 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001095 if (fc == NULL)
1096 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001097 fc->caller = current_funccal;
1098 current_funccal = fc;
1099 fc->func = fp;
1100 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001101 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001102 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001103 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1104 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001105 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001106 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001107 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 if (fp->uf_dfunc_idx >= 0)
1110 {
1111 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001112 save_current_sctx = current_sctx;
1113 current_sctx = fp->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001114
1115 // Execute the compiled function.
1116 call_def_function(fp, argcount, argvars, rettv);
1117 --depth;
1118 current_funccal = fc->caller;
1119
1120 estack_pop();
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001121 current_sctx = save_current_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122 free_funccal(fc);
1123 return;
1124 }
1125
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001126 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1127 islambda = TRUE;
1128
1129 /*
1130 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1131 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1132 * each argument variable and saves a lot of time.
1133 */
1134 /*
1135 * Init l: variables.
1136 */
1137 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1138 if (selfdict != NULL)
1139 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001140 // Set l:self to "selfdict". Use "name" to avoid a warning from
1141 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001142 v = &fc->fixvar[fixvar_idx++].var;
1143 name = v->di_key;
1144 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001145 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001146 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1147 v->di_tv.v_type = VAR_DICT;
1148 v->di_tv.v_lock = 0;
1149 v->di_tv.vval.v_dict = selfdict;
1150 ++selfdict->dv_refcount;
1151 }
1152
1153 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001154 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001155 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001156 * Set a:000 to a list with room for the "..." arguments.
1157 */
1158 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001159 if ((fp->uf_flags & FC_NOARGS) == 0)
1160 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001161 (varnumber_T)(argcount >= fp->uf_args.ga_len
1162 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001163 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001164 if ((fp->uf_flags & FC_NOARGS) == 0)
1165 {
1166 // Use "name" to avoid a warning from some compiler that checks the
1167 // destination size.
1168 v = &fc->fixvar[fixvar_idx++].var;
1169 name = v->di_key;
1170 STRCPY(name, "000");
1171 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1172 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1173 v->di_tv.v_type = VAR_LIST;
1174 v->di_tv.v_lock = VAR_FIXED;
1175 v->di_tv.vval.v_list = &fc->l_varlist;
1176 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001177 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001178 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1179 fc->l_varlist.lv_lock = VAR_FIXED;
1180
1181 /*
1182 * Set a:firstline to "firstline" and a:lastline to "lastline".
1183 * Set a:name to named arguments.
1184 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001185 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001186 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001187 if ((fp->uf_flags & FC_NOARGS) == 0)
1188 {
1189 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001190 (varnumber_T)firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001191 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001192 (varnumber_T)lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001193 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001194 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001195 {
1196 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001197 typval_T def_rettv;
1198 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001199
1200 ai = i - fp->uf_args.ga_len;
1201 if (ai < 0)
1202 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001203 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001204 name = FUNCARG(fp, i);
1205 if (islambda)
1206 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001207
1208 // evaluate named argument default expression
1209 isdefault = ai + fp->uf_def_args.ga_len >= 0
1210 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1211 && argvars[i].vval.v_number == VVAL_NONE));
1212 if (isdefault)
1213 {
1214 char_u *default_expr = NULL;
1215 def_rettv.v_type = VAR_NUMBER;
1216 def_rettv.vval.v_number = -1;
1217
1218 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1219 [ai + fp->uf_def_args.ga_len];
1220 if (eval1(&default_expr, &def_rettv, TRUE) == FAIL)
1221 {
1222 default_arg_err = 1;
1223 break;
1224 }
1225 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001226 }
1227 else
1228 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001229 if ((fp->uf_flags & FC_NOARGS) != 0)
1230 // Bail out if no a: arguments used (in lambda).
1231 break;
1232
Bram Moolenaare38eab22019-12-05 21:50:01 +01001233 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001234 sprintf((char *)numbuf, "%d", ai + 1);
1235 name = numbuf;
1236 }
1237 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1238 {
1239 v = &fc->fixvar[fixvar_idx++].var;
1240 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001241 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001242 }
1243 else
1244 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001245 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001246 if (v == NULL)
1247 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001248 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001249 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001250
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001251 // Note: the values are copied directly to avoid alloc/free.
1252 // "argvars" must have VAR_FIXED for v_lock.
1253 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001254 v->di_tv.v_lock = VAR_FIXED;
1255
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001256 if (addlocal)
1257 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001258 // Named arguments should be accessed without the "a:" prefix in
1259 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001260 copy_tv(&v->di_tv, &v->di_tv);
1261 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001262 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001263 else
1264 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001265
1266 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1267 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001268 listitem_T *li = &fc->l_listitems[ai];
1269
1270 li->li_tv = argvars[i];
1271 li->li_tv.v_lock = VAR_FIXED;
1272 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001273 }
1274 }
1275
Bram Moolenaare38eab22019-12-05 21:50:01 +01001276 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001277 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001278
1279 if (fp->uf_flags & FC_SANDBOX)
1280 {
1281 using_sandbox = TRUE;
1282 ++sandbox;
1283 }
1284
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001285 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001286 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001287 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001288 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001289 ++no_wait_return;
1290 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001291
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001292 smsg(_("calling %s"), SOURCING_NAME);
1293 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001294 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001295 char_u buf[MSG_BUF_LEN];
1296 char_u numbuf2[NUMBUFLEN];
1297 char_u *tofree;
1298 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001299
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001300 msg_puts("(");
1301 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001302 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001303 if (i > 0)
1304 msg_puts(", ");
1305 if (argvars[i].v_type == VAR_NUMBER)
1306 msg_outnum((long)argvars[i].vval.v_number);
1307 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001308 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001309 // Do not want errors such as E724 here.
1310 ++emsg_off;
1311 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1312 --emsg_off;
1313 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001314 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001315 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001316 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001317 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1318 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001319 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001320 msg_puts((char *)s);
1321 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001322 }
1323 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001324 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001325 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001326 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001327 msg_puts("\n"); // don't overwrite this either
1328
1329 verbose_leave_scroll();
1330 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001331 }
1332#ifdef FEAT_PROFILE
1333 if (do_profiling == PROF_YES)
1334 {
1335 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001336 {
1337 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001338 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001339 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001340 if (fp->uf_profiling
1341 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1342 {
1343 ++fp->uf_tm_count;
1344 profile_start(&call_start);
1345 profile_zero(&fp->uf_tm_children);
1346 }
1347 script_prof_save(&wait_start);
1348 }
1349#endif
1350
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001351 save_current_sctx = current_sctx;
1352 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001353 save_did_emsg = did_emsg;
1354 did_emsg = FALSE;
1355
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001356 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1357 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001358 else if (islambda)
1359 {
1360 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1361
1362 // A Lambda always has the command "return {expr}". It is much faster
1363 // to evaluate {expr} directly.
1364 ++ex_nesting_level;
Bram Moolenaard1e9dc22020-04-03 18:13:57 +02001365 (void)eval1(&p, rettv, TRUE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001366 --ex_nesting_level;
1367 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001368 else
1369 // call do_cmdline() to execute the lines
1370 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001371 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1372
1373 --RedrawingDisabled;
1374
Bram Moolenaare38eab22019-12-05 21:50:01 +01001375 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001376 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1377 {
1378 clear_tv(rettv);
1379 rettv->v_type = VAR_NUMBER;
1380 rettv->vval.v_number = -1;
1381 }
1382
1383#ifdef FEAT_PROFILE
1384 if (do_profiling == PROF_YES && (fp->uf_profiling
1385 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1386 {
1387 profile_end(&call_start);
1388 profile_sub_wait(&wait_start, &call_start);
1389 profile_add(&fp->uf_tm_total, &call_start);
1390 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1391 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1392 {
1393 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1394 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1395 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001396 if (started_profiling)
1397 // make a ":profdel func" stop profiling the function
1398 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001399 }
1400#endif
1401
Bram Moolenaare38eab22019-12-05 21:50:01 +01001402 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001403 if (p_verbose >= 12)
1404 {
1405 ++no_wait_return;
1406 verbose_enter_scroll();
1407
1408 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001409 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001410 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001411 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001412 (long)fc->rettv->vval.v_number);
1413 else
1414 {
1415 char_u buf[MSG_BUF_LEN];
1416 char_u numbuf2[NUMBUFLEN];
1417 char_u *tofree;
1418 char_u *s;
1419
Bram Moolenaare38eab22019-12-05 21:50:01 +01001420 // The value may be very long. Skip the middle part, so that we
1421 // have some idea how it starts and ends. smsg() would always
1422 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001423 ++emsg_off;
1424 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1425 --emsg_off;
1426 if (s != NULL)
1427 {
1428 if (vim_strsize(s) > MSG_BUF_CLEN)
1429 {
1430 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1431 s = buf;
1432 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001433 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001434 vim_free(tofree);
1435 }
1436 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001437 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001438
1439 verbose_leave_scroll();
1440 --no_wait_return;
1441 }
1442
Bram Moolenaare31ee862020-01-07 20:59:34 +01001443 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001444 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001445 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001446#ifdef FEAT_PROFILE
1447 if (do_profiling == PROF_YES)
1448 script_prof_restore(&wait_start);
1449#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001450 if (using_sandbox)
1451 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001452
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001453 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001454 {
1455 ++no_wait_return;
1456 verbose_enter_scroll();
1457
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001458 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001459 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001460
1461 verbose_leave_scroll();
1462 --no_wait_return;
1463 }
1464
1465 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001466 --depth;
1467
Bram Moolenaar6914c642017-04-01 21:21:30 +02001468 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001469}
1470
1471/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001473 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474 int
1475call_user_func_check(
1476 ufunc_T *fp,
1477 int argcount,
1478 typval_T *argvars,
1479 typval_T *rettv,
1480 funcexe_T *funcexe,
1481 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001482{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 int error;
1484 int regular_args = fp->uf_args.ga_len;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1487 *funcexe->doesrange = TRUE;
1488 if (argcount < regular_args - fp->uf_def_args.ga_len)
1489 error = FCERR_TOOFEW;
1490 else if (!has_varargs(fp) && argcount > regular_args)
1491 error = FCERR_TOOMANY;
1492 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1493 error = FCERR_DICT;
1494 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001495 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 int did_save_redo = FALSE;
1497 save_redo_T save_redo;
1498
1499 /*
1500 * Call the user function.
1501 * Save and restore search patterns, script variables and
1502 * redo buffer.
1503 */
1504 save_search_patterns();
1505 if (!ins_compl_active())
1506 {
1507 saveRedobuff(&save_redo);
1508 did_save_redo = TRUE;
1509 }
1510 ++fp->uf_calls;
1511 call_user_func(fp, argcount, argvars, rettv,
1512 funcexe->firstline, funcexe->lastline,
1513 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
1514 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1515 // Function was unreferenced while being used, free it now.
1516 func_clear_free(fp, FALSE);
1517 if (did_save_redo)
1518 restoreRedobuff(&save_redo);
1519 restore_search_patterns();
1520 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001521 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001523}
1524
1525/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001526 * There are two kinds of function names:
1527 * 1. ordinary names, function defined with :function
1528 * 2. numbered functions and lambdas
1529 * For the first we only count the name stored in func_hashtab as a reference,
1530 * using function() does not count as a reference, because the function is
1531 * looked up by name.
1532 */
1533 static int
1534func_name_refcount(char_u *name)
1535{
1536 return isdigit(*name) || *name == '<';
1537}
1538
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001539static funccal_entry_T *funccal_stack = NULL;
1540
1541/*
1542 * Save the current function call pointer, and set it to NULL.
1543 * Used when executing autocommands and for ":source".
1544 */
1545 void
1546save_funccal(funccal_entry_T *entry)
1547{
1548 entry->top_funccal = current_funccal;
1549 entry->next = funccal_stack;
1550 funccal_stack = entry;
1551 current_funccal = NULL;
1552}
1553
1554 void
1555restore_funccal(void)
1556{
1557 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001558 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001559 else
1560 {
1561 current_funccal = funccal_stack->top_funccal;
1562 funccal_stack = funccal_stack->next;
1563 }
1564}
1565
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001566 funccall_T *
1567get_current_funccal(void)
1568{
1569 return current_funccal;
1570}
1571
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001572#if defined(EXITFREE) || defined(PROTO)
1573 void
1574free_all_functions(void)
1575{
1576 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001577 ufunc_T *fp;
1578 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001579 long_u todo = 1;
1580 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001581
Bram Moolenaare38eab22019-12-05 21:50:01 +01001582 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001583 while (current_funccal != NULL)
1584 {
1585 clear_tv(current_funccal->rettv);
1586 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001587 if (current_funccal == NULL && funccal_stack != NULL)
1588 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001589 }
1590
Bram Moolenaare38eab22019-12-05 21:50:01 +01001591 // First clear what the functions contain. Since this may lower the
1592 // reference count of a function, it may also free a function and change
1593 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001594 while (todo > 0)
1595 {
1596 todo = func_hashtab.ht_used;
1597 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1598 if (!HASHITEM_EMPTY(hi))
1599 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 // clear the def function index now
1601 fp = HI2UF(hi);
1602 fp->uf_flags &= ~FC_DEAD;
1603 fp->uf_dfunc_idx = -1;
1604
Bram Moolenaare38eab22019-12-05 21:50:01 +01001605 // Only free functions that are not refcounted, those are
1606 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001607 if (func_name_refcount(fp->uf_name))
1608 ++skipped;
1609 else
1610 {
1611 used = func_hashtab.ht_used;
1612 func_clear(fp, TRUE);
1613 if (used != func_hashtab.ht_used)
1614 {
1615 skipped = 0;
1616 break;
1617 }
1618 }
1619 --todo;
1620 }
1621 }
1622
Bram Moolenaare38eab22019-12-05 21:50:01 +01001623 // Now actually free the functions. Need to start all over every time,
1624 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001625 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001626 while (func_hashtab.ht_used > skipped)
1627 {
1628 todo = func_hashtab.ht_used;
1629 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001630 if (!HASHITEM_EMPTY(hi))
1631 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001632 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001633 // Only free functions that are not refcounted, those are
1634 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02001635 fp = HI2UF(hi);
1636 if (func_name_refcount(fp->uf_name))
1637 ++skipped;
1638 else
1639 {
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001640 func_free(fp);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001641 skipped = 0;
1642 break;
1643 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001644 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001645 }
1646 if (skipped == 0)
1647 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648
1649 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001650}
1651#endif
1652
1653/*
1654 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02001655 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001656 * "len" is the length of "name", or -1 for NUL terminated.
1657 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001659builtin_function(char_u *name, int len)
1660{
1661 char_u *p;
1662
Bram Moolenaara26b9702020-04-18 19:53:28 +02001663 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001664 return FALSE;
1665 p = vim_strchr(name, AUTOLOAD_CHAR);
1666 return p == NULL || (len > 0 && p > name + len);
1667}
1668
1669 int
1670func_call(
1671 char_u *name,
1672 typval_T *args,
1673 partial_T *partial,
1674 dict_T *selfdict,
1675 typval_T *rettv)
1676{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001677 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001678 listitem_T *item;
1679 typval_T argv[MAX_FUNC_ARGS + 1];
1680 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001681 int r = 0;
1682
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001683 range_list_materialize(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001684 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001685 {
1686 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1687 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001688 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001689 break;
1690 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001691 // Make a copy of each argument. This is needed to be able to set
1692 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001693 copy_tv(&item->li_tv, &argv[argc++]);
1694 }
1695
1696 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001697 {
1698 funcexe_T funcexe;
1699
Bram Moolenaara80faa82020-04-12 19:37:17 +02001700 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001701 funcexe.firstline = curwin->w_cursor.lnum;
1702 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001703 funcexe.evaluate = TRUE;
1704 funcexe.partial = partial;
1705 funcexe.selfdict = selfdict;
1706 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1707 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001708
Bram Moolenaare38eab22019-12-05 21:50:01 +01001709 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001710 while (argc > 0)
1711 clear_tv(&argv[--argc]);
1712
1713 return r;
1714}
1715
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001716static int callback_depth = 0;
1717
1718 int
1719get_callback_depth(void)
1720{
1721 return callback_depth;
1722}
1723
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001724/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001725 * Invoke call_func() with a callback.
1726 */
1727 int
1728call_callback(
1729 callback_T *callback,
1730 int len, // length of "name" or -1 to use strlen()
1731 typval_T *rettv, // return value goes here
1732 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001733 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001734 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001735{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001736 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001737 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001738
Bram Moolenaara80faa82020-04-12 19:37:17 +02001739 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001740 funcexe.evaluate = TRUE;
1741 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001742 ++callback_depth;
1743 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1744 --callback_depth;
1745 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001746}
1747
1748/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001749 * Give an error message for the result of a function.
1750 * Nothing if "error" is FCERR_NONE.
1751 */
1752 void
1753user_func_error(int error, char_u *name)
1754{
1755 switch (error)
1756 {
1757 case FCERR_UNKNOWN:
1758 emsg_funcname(e_unknownfunc, name);
1759 break;
1760 case FCERR_NOTMETHOD:
1761 emsg_funcname(
1762 N_("E276: Cannot use function as a method: %s"), name);
1763 break;
1764 case FCERR_DELETED:
1765 emsg_funcname(N_(e_func_deleted), name);
1766 break;
1767 case FCERR_TOOMANY:
1768 emsg_funcname((char *)e_toomanyarg, name);
1769 break;
1770 case FCERR_TOOFEW:
1771 emsg_funcname((char *)e_toofewarg, name);
1772 break;
1773 case FCERR_SCRIPT:
1774 emsg_funcname(
1775 N_("E120: Using <SID> not in a script context: %s"), name);
1776 break;
1777 case FCERR_DICT:
1778 emsg_funcname(
1779 N_("E725: Calling dict function without Dictionary: %s"),
1780 name);
1781 break;
1782 }
1783}
1784
1785/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001786 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001787 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001788 * Return FAIL when the function can't be called, OK otherwise.
1789 * Also returns OK when an error was encountered while executing the function.
1790 */
1791 int
1792call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001793 char_u *funcname, // name of the function
1794 int len, // length of "name" or -1 to use strlen()
1795 typval_T *rettv, // return value goes here
1796 int argcount_in, // number of "argvars"
1797 typval_T *argvars_in, // vars for arguments, must have "argcount"
1798 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001799 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001800{
1801 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01001802 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001803 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001804 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805 char_u fname_buf[FLEN_FIXED + 1];
1806 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001807 char_u *fname = NULL;
1808 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001809 int argcount = argcount_in;
1810 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001811 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001812 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1813 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001814 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001815 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001816 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001817
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001818 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1819 // even when call_func() returns FAIL.
1820 rettv->v_type = VAR_UNKNOWN;
1821
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001822 if (partial != NULL)
1823 fp = partial->pt_func;
1824 if (fp == NULL)
1825 {
1826 // Make a copy of the name, if it comes from a funcref variable it
1827 // could be changed or deleted in the called function.
1828 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
1829 if (name == NULL)
1830 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001831
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001832 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1833 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001834
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001835 if (funcexe->doesrange != NULL)
1836 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001837
1838 if (partial != NULL)
1839 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001840 // When the function has a partial with a dict and there is a dict
1841 // argument, use the dict argument. That is backwards compatible.
1842 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001843 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001844 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01001845 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001846 {
1847 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001848 {
1849 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
1850 {
Bram Moolenaaref140542019-12-31 21:27:13 +01001851 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001852 goto theend;
1853 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001854 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001855 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001856 for (i = 0; i < argcount_in; ++i)
1857 argv[i + argv_clear] = argvars_in[i];
1858 argvars = argv;
1859 argcount = partial->pt_argc + argcount_in;
1860 }
1861 }
1862
Bram Moolenaaref140542019-12-31 21:27:13 +01001863 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001864 {
1865 char_u *rfname = fname;
1866
Bram Moolenaare38eab22019-12-05 21:50:01 +01001867 // Ignore "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001868 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001869 rfname = fname + 2;
1870
Bram Moolenaare38eab22019-12-05 21:50:01 +01001871 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001872 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01001873 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001874
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001875 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001876 {
1877 /*
1878 * User defined function.
1879 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001880 if (fp == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001882
Bram Moolenaare38eab22019-12-05 21:50:01 +01001883 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001884 if (fp == NULL
1885 && apply_autocmds(EVENT_FUNCUNDEFINED,
1886 rfname, rfname, TRUE, NULL)
1887 && !aborting())
1888 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001889 // executed an autocommand, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001891 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001892 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001893 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1894 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001895 // loaded a package, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001896 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001897 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02001898 if (fp == NULL)
1899 {
1900 char_u *p = untrans_function_name(rfname);
1901
1902 // If using Vim9 script try not local to the script.
1903 // TODO: should not do this if the name started with "s:".
1904 if (p != NULL)
1905 fp = find_func(p, NULL);
1906 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001907
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001908 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01001909 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001910 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001911 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001912 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01001913 // postponed filling in the arguments, do it now
1914 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001915 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001916
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001917 if (funcexe->basetv != NULL)
1918 {
1919 // Method call: base->Method()
1920 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
1921 argv[0] = *funcexe->basetv;
1922 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001923 argvars = argv;
1924 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001925 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927 error = call_user_func_check(fp, argcount, argvars, rettv,
1928 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001929 }
1930 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001931 else if (funcexe->basetv != NULL)
1932 {
1933 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001934 * expr->method(): Find the method name in the table, call its
1935 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02001936 */
1937 error = call_internal_method(fname, argcount, argvars, rettv,
1938 funcexe->basetv);
1939 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001940 else
1941 {
1942 /*
1943 * Find the function name in the table, call its implementation.
1944 */
1945 error = call_internal_func(fname, argcount, argvars, rettv);
1946 }
1947 /*
1948 * The function call (or "FuncUndefined" autocommand sequence) might
1949 * have been aborted by an error, an interrupt, or an explicitly thrown
1950 * exception that has not been caught so far. This situation can be
1951 * tested for by calling aborting(). For an error in an internal
1952 * function or for the "E132" error in call_user_func(), however, the
1953 * throw point at which the "force_abort" flag (temporarily reset by
1954 * emsg()) is normally updated has not been reached yet. We need to
1955 * update that flag first to make aborting() reliable.
1956 */
1957 update_force_abort();
1958 }
Bram Moolenaaref140542019-12-31 21:27:13 +01001959 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001960 ret = OK;
1961
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001962theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001963 /*
1964 * Report an error unless the argument evaluation or function call has been
1965 * cancelled due to an aborting error, an interrupt, or an exception.
1966 */
1967 if (!aborting())
1968 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001969 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001970 }
1971
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001972 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001973 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001974 clear_tv(&argv[--argv_clear + argv_base]);
1975
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001976 vim_free(tofree);
1977 vim_free(name);
1978
1979 return ret;
1980}
1981
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 static char_u *
1983printable_func_name(ufunc_T *fp)
1984{
1985 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
1986}
1987
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001988/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001989 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001990 */
1991 static void
1992list_func_head(ufunc_T *fp, int indent)
1993{
1994 int j;
1995
1996 msg_start();
1997 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001998 msg_puts(" ");
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001999 if (fp->uf_dfunc_idx >= 0)
2000 msg_puts("def ");
2001 else
2002 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002004 msg_putchar('(');
2005 for (j = 0; j < fp->uf_args.ga_len; ++j)
2006 {
2007 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002008 msg_puts(", ");
2009 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 if (fp->uf_arg_types != NULL)
2011 {
2012 char *tofree;
2013
2014 msg_puts(": ");
2015 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
2016 vim_free(tofree);
2017 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002018 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
2019 {
2020 msg_puts(" = ");
2021 msg_puts(((char **)(fp->uf_def_args.ga_data))
2022 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
2023 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002024 }
2025 if (fp->uf_varargs)
2026 {
2027 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002028 msg_puts(", ");
2029 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002030 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 if (fp->uf_va_name != NULL)
2032 {
2033 if (j)
2034 msg_puts(", ");
2035 msg_puts("...");
2036 msg_puts((char *)fp->uf_va_name);
2037 if (fp->uf_va_type)
2038 {
2039 char *tofree;
2040
2041 msg_puts(": ");
2042 msg_puts(type_name(fp->uf_va_type, &tofree));
2043 vim_free(tofree);
2044 }
2045 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002046 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002047
2048 if (fp->uf_dfunc_idx >= 0)
2049 {
2050 if (fp->uf_ret_type != &t_void)
2051 {
2052 char *tofree;
2053
2054 msg_puts(": ");
2055 msg_puts(type_name(fp->uf_ret_type, &tofree));
2056 vim_free(tofree);
2057 }
2058 }
2059 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002060 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002061 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002062 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002063 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002064 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002065 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002066 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002067 msg_clr_eos();
2068 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002069 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002070}
2071
2072/*
2073 * Get a function name, translating "<SID>" and "<SNR>".
2074 * Also handles a Funcref in a List or Dictionary.
2075 * Returns the function name in allocated memory, or NULL for failure.
2076 * flags:
2077 * TFN_INT: internal function name OK
2078 * TFN_QUIET: be quiet
2079 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002080 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002081 * Advances "pp" to just after the function name (if no error).
2082 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002083 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002084trans_function_name(
2085 char_u **pp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002086 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002087 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002088 funcdict_T *fdp, // return: info about dictionary used
2089 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002090{
2091 char_u *name = NULL;
2092 char_u *start;
2093 char_u *end;
2094 int lead;
2095 char_u sid_buf[20];
2096 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002098 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002100
2101 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002102 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002103 start = *pp;
2104
Bram Moolenaare38eab22019-12-05 21:50:01 +01002105 // Check for hard coded <SNR>: already translated function ID (from a user
2106 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002107 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2108 && (*pp)[2] == (int)KE_SNR)
2109 {
2110 *pp += 3;
2111 len = get_id_len(pp) + 3;
2112 return vim_strnsave(start, len);
2113 }
2114
Bram Moolenaare38eab22019-12-05 21:50:01 +01002115 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2116 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002117 lead = eval_fname_script(start);
2118 if (lead > 2)
2119 start += lead;
2120
Bram Moolenaare38eab22019-12-05 21:50:01 +01002121 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002122 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002123 lead > 2 ? 0 : FNE_CHECK_START);
2124 if (end == start)
2125 {
2126 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002127 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002128 goto theend;
2129 }
2130 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2131 {
2132 /*
2133 * Report an invalid expression in braces, unless the expression
2134 * evaluation has been cancelled due to an aborting error, an
2135 * interrupt, or an exception.
2136 */
2137 if (!aborting())
2138 {
2139 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002140 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002141 }
2142 else
2143 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2144 goto theend;
2145 }
2146
2147 if (lv.ll_tv != NULL)
2148 {
2149 if (fdp != NULL)
2150 {
2151 fdp->fd_dict = lv.ll_dict;
2152 fdp->fd_newkey = lv.ll_newkey;
2153 lv.ll_newkey = NULL;
2154 fdp->fd_di = lv.ll_di;
2155 }
2156 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2157 {
2158 name = vim_strsave(lv.ll_tv->vval.v_string);
2159 *pp = end;
2160 }
2161 else if (lv.ll_tv->v_type == VAR_PARTIAL
2162 && lv.ll_tv->vval.v_partial != NULL)
2163 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002164 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002165 *pp = end;
2166 if (partial != NULL)
2167 *partial = lv.ll_tv->vval.v_partial;
2168 }
2169 else
2170 {
2171 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2172 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002173 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002174 else
2175 *pp = end;
2176 name = NULL;
2177 }
2178 goto theend;
2179 }
2180
2181 if (lv.ll_name == NULL)
2182 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002183 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002184 *pp = end;
2185 goto theend;
2186 }
2187
Bram Moolenaare38eab22019-12-05 21:50:01 +01002188 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002189 if (lv.ll_exp_name != NULL)
2190 {
2191 len = (int)STRLEN(lv.ll_exp_name);
2192 name = deref_func_name(lv.ll_exp_name, &len, partial,
2193 flags & TFN_NO_AUTOLOAD);
2194 if (name == lv.ll_exp_name)
2195 name = NULL;
2196 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002197 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002198 {
2199 len = (int)(end - *pp);
2200 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2201 if (name == *pp)
2202 name = NULL;
2203 }
2204 if (name != NULL)
2205 {
2206 name = vim_strsave(name);
2207 *pp = end;
2208 if (STRNCMP(name, "<SNR>", 5) == 0)
2209 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002210 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002211 name[0] = K_SPECIAL;
2212 name[1] = KS_EXTRA;
2213 name[2] = (int)KE_SNR;
2214 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2215 }
2216 goto theend;
2217 }
2218
2219 if (lv.ll_exp_name != NULL)
2220 {
2221 len = (int)STRLEN(lv.ll_exp_name);
2222 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2223 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2224 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002225 // When there was "s:" already or the name expanded to get a
2226 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002227 lv.ll_name += 2;
2228 len -= 2;
2229 lead = 2;
2230 }
2231 }
2232 else
2233 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002234 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002235 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
2236 lv.ll_name += 2;
2237 len = (int)(end - lv.ll_name);
2238 }
2239
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002240 // In Vim9 script a user function is script-local by default.
2241 vim9script = ASCII_ISUPPER(*start)
2242 && current_sctx.sc_version == SCRIPT_VERSION_VIM9;
2243
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002244 /*
2245 * Copy the function name to allocated memory.
2246 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2247 * Accept <SNR>123_name() outside a script.
2248 */
2249 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002250 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002252 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 if (!vim9script)
2254 lead = 3;
2255 if (vim9script || (lv.ll_exp_name != NULL
2256 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002257 || eval_fname_sid(*pp))
2258 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002260 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002262 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002263 goto theend;
2264 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002265 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 if (vim9script)
2267 extra = 3 + (int)STRLEN(sid_buf);
2268 else
2269 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002270 }
2271 }
2272 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2273 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002274 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002275 start);
2276 goto theend;
2277 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002278 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002279 {
2280 char_u *cp = vim_strchr(lv.ll_name, ':');
2281
2282 if (cp != NULL && cp < end)
2283 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002284 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002285 goto theend;
2286 }
2287 }
2288
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002290 if (name != NULL)
2291 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002292 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002293 {
2294 name[0] = K_SPECIAL;
2295 name[1] = KS_EXTRA;
2296 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002298 STRCPY(name + 3, sid_buf);
2299 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2301 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002302 }
2303 *pp = end;
2304
2305theend:
2306 clear_lval(&lv);
2307 return name;
2308}
2309
2310/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02002311 * Assuming "name" is the result of trans_function_name() and it was prefixed
2312 * to use the script-local name, return the unmodified name (points into
2313 * "name"). Otherwise return NULL.
2314 * This can be used to first search for a script-local function and fall back
2315 * to the global function if not found.
2316 */
2317 char_u *
2318untrans_function_name(char_u *name)
2319{
2320 char_u *p;
2321
2322 if (*name == K_SPECIAL && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2323 {
2324 p = vim_strchr(name, '_');
2325 if (p != NULL)
2326 return p + 1;
2327 }
2328 return NULL;
2329}
2330
2331/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002332 * ":function"
2333 */
2334 void
2335ex_function(exarg_T *eap)
2336{
2337 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002338 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002339 int j;
2340 int c;
2341 int saved_did_emsg;
2342 int saved_wait_return = need_wait_return;
2343 char_u *name = NULL;
2344 char_u *p;
2345 char_u *arg;
2346 char_u *line_arg = NULL;
2347 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002349 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002350 garray_T newlines;
2351 int varargs = FALSE;
2352 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002354 ufunc_T *fp;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002355 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002356 int indent;
2357 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002358#define MAX_FUNC_NESTING 50
2359 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002360 dictitem_T *v;
2361 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002362 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002363 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002364 int todo;
2365 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002366 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002367 linenr_T sourcing_lnum_off;
2368 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002369 int is_heredoc = FALSE;
2370 char_u *skip_until = NULL;
2371 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002372
2373 /*
2374 * ":function" without argument: list functions.
2375 */
2376 if (ends_excmd(*eap->arg))
2377 {
2378 if (!eap->skip)
2379 {
2380 todo = (int)func_hashtab.ht_used;
2381 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2382 {
2383 if (!HASHITEM_EMPTY(hi))
2384 {
2385 --todo;
2386 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002387 if ((fp->uf_flags & FC_DEAD)
2388 || message_filtered(fp->uf_name))
Bram Moolenaarf86db782018-10-25 13:31:37 +02002389 continue;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002390 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002391 list_func_head(fp, FALSE);
2392 }
2393 }
2394 }
2395 eap->nextcmd = check_nextcmd(eap->arg);
2396 return;
2397 }
2398
2399 /*
2400 * ":function /pat": list functions matching pattern.
2401 */
2402 if (*eap->arg == '/')
2403 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002404 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002405 if (!eap->skip)
2406 {
2407 regmatch_T regmatch;
2408
2409 c = *p;
2410 *p = NUL;
2411 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2412 *p = c;
2413 if (regmatch.regprog != NULL)
2414 {
2415 regmatch.rm_ic = p_ic;
2416
2417 todo = (int)func_hashtab.ht_used;
2418 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2419 {
2420 if (!HASHITEM_EMPTY(hi))
2421 {
2422 --todo;
2423 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424 if ((fp->uf_flags & FC_DEAD) == 0
2425 && !isdigit(*fp->uf_name)
2426 && vim_regexec(&regmatch, fp->uf_name, 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002427 list_func_head(fp, FALSE);
2428 }
2429 }
2430 vim_regfree(regmatch.regprog);
2431 }
2432 }
2433 if (*p == '/')
2434 ++p;
2435 eap->nextcmd = check_nextcmd(p);
2436 return;
2437 }
2438
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439 ga_init(&newargs);
2440 ga_init(&argtypes);
2441 ga_init(&default_args);
2442
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002443 /*
2444 * Get the function name. There are these situations:
2445 * func normal function name
2446 * "name" == func, "fudi.fd_dict" == NULL
2447 * dict.func new dictionary entry
2448 * "name" == NULL, "fudi.fd_dict" set,
2449 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2450 * dict.func existing dict entry with a Funcref
2451 * "name" == func, "fudi.fd_dict" set,
2452 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2453 * dict.func existing dict entry that's not a Funcref
2454 * "name" == NULL, "fudi.fd_dict" set,
2455 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2456 * s:func script-local function name
2457 * g:func global function name, same as "func"
2458 */
2459 p = eap->arg;
Bram Moolenaar3388d332017-12-07 22:23:04 +01002460 name = trans_function_name(&p, eap->skip, TFN_NO_AUTOLOAD, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002461 paren = (vim_strchr(p, '(') != NULL);
2462 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
2463 {
2464 /*
2465 * Return on an invalid expression in braces, unless the expression
2466 * evaluation has been cancelled due to an aborting error, an
2467 * interrupt, or an exception.
2468 */
2469 if (!aborting())
2470 {
2471 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002472 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002473 vim_free(fudi.fd_newkey);
2474 return;
2475 }
2476 else
2477 eap->skip = TRUE;
2478 }
2479
Bram Moolenaare38eab22019-12-05 21:50:01 +01002480 // An error in a function call during evaluation of an expression in magic
2481 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002482 saved_did_emsg = did_emsg;
2483 did_emsg = FALSE;
2484
2485 /*
2486 * ":function func" with only function name: list function.
2487 */
2488 if (!paren)
2489 {
2490 if (!ends_excmd(*skipwhite(p)))
2491 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002492 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002493 goto ret_free;
2494 }
2495 eap->nextcmd = check_nextcmd(p);
2496 if (eap->nextcmd != NULL)
2497 *p = NUL;
2498 if (!eap->skip && !got_int)
2499 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500 fp = find_func(name, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002501 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
2502 {
2503 char_u *up = untrans_function_name(name);
2504
2505 // With Vim9 script the name was made script-local, if not
2506 // found try again with the original name.
2507 if (p != NULL)
2508 fp = find_func(up, NULL);
2509 }
2510
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002511 if (fp != NULL)
2512 {
2513 list_func_head(fp, TRUE);
2514 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2515 {
2516 if (FUNCLINE(fp, j) == NULL)
2517 continue;
2518 msg_putchar('\n');
2519 msg_outnum((long)(j + 1));
2520 if (j < 9)
2521 msg_putchar(' ');
2522 if (j < 99)
2523 msg_putchar(' ');
2524 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002525 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002526 ui_breakcheck();
2527 }
2528 if (!got_int)
2529 {
2530 msg_putchar('\n');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 if (fp->uf_dfunc_idx >= 0)
2532 msg_puts(" enddef");
2533 else
2534 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002535 }
2536 }
2537 else
Bram Moolenaara26b9702020-04-18 19:53:28 +02002538 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002539 }
2540 goto ret_free;
2541 }
2542
2543 /*
2544 * ":function name(arg1, arg2)" Define function.
2545 */
2546 p = skipwhite(p);
2547 if (*p != '(')
2548 {
2549 if (!eap->skip)
2550 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002551 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002552 goto ret_free;
2553 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002554 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002555 if (vim_strchr(p, '(') != NULL)
2556 p = vim_strchr(p, '(');
2557 }
2558 p = skipwhite(p + 1);
2559
2560 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2561
2562 if (!eap->skip)
2563 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002564 // Check the name of the function. Unless it's a dictionary function
2565 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002566 if (name != NULL)
2567 arg = name;
2568 else
2569 arg = fudi.fd_newkey;
2570 if (arg != NULL && (fudi.fd_di == NULL
2571 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2572 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2573 {
2574 if (*arg == K_SPECIAL)
2575 j = 3;
2576 else
2577 j = 0;
2578 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2579 : eval_isnamec(arg[j])))
2580 ++j;
2581 if (arg[j] != NUL)
2582 emsg_funcname((char *)e_invarg2, arg);
2583 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002584 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002585 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002586 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002587 }
2588
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002589 // This may get more lines and make the pointers into the first line
2590 // invalid.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 if (get_function_args(&p, ')', &newargs,
2592 eap->cmdidx == CMD_def ? &argtypes : NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002593 &varargs, &default_args, eap->skip,
2594 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002595 goto errret_2;
2596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002598 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 // find the return type: :def Func(): type
2600 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002601 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 ret_type = skipwhite(p + 1);
2603 p = skip_type(ret_type);
2604 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002605 {
2606 ret_type = vim_strnsave(ret_type, (int)(p - ret_type));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002608 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002610 {
2611 ret_type = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612 semsg(_("E1056: expected a type: %s"), ret_type);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002613 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002614 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002615 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 else
2617 // find extra arguments "range", "dict", "abort" and "closure"
2618 for (;;)
2619 {
2620 p = skipwhite(p);
2621 if (STRNCMP(p, "range", 5) == 0)
2622 {
2623 flags |= FC_RANGE;
2624 p += 5;
2625 }
2626 else if (STRNCMP(p, "dict", 4) == 0)
2627 {
2628 flags |= FC_DICT;
2629 p += 4;
2630 }
2631 else if (STRNCMP(p, "abort", 5) == 0)
2632 {
2633 flags |= FC_ABORT;
2634 p += 5;
2635 }
2636 else if (STRNCMP(p, "closure", 7) == 0)
2637 {
2638 flags |= FC_CLOSURE;
2639 p += 7;
2640 if (current_funccal == NULL)
2641 {
2642 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
2643 name == NULL ? (char_u *)"" : name);
2644 goto erret;
2645 }
2646 }
2647 else
2648 break;
2649 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002650
Bram Moolenaare38eab22019-12-05 21:50:01 +01002651 // When there is a line break use what follows for the function body.
2652 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002653 if (*p == '\n')
2654 line_arg = p + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002655 else if (*p != NUL && *p != '"' && !(eap->cmdidx == CMD_def && *p == '#')
2656 && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002657 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002658
2659 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
2661 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002662 */
2663 if (KeyTyped)
2664 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002665 // Check if the function already exists, don't let the user type the
2666 // whole function before telling him it doesn't work! For a script we
2667 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002668 if (!eap->skip && !eap->forceit)
2669 {
2670 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002671 emsg(_(e_funcdict));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672 else if (name != NULL && find_func(name, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002673 emsg_funcname(e_funcexts, name);
2674 }
2675
2676 if (!eap->skip && did_emsg)
2677 goto erret;
2678
Bram Moolenaare38eab22019-12-05 21:50:01 +01002679 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002680 cmdline_row = msg_row;
2681 }
2682
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002683 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002684 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002685
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002686 indent = 2;
2687 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002689 for (;;)
2690 {
2691 if (KeyTyped)
2692 {
2693 msg_scroll = TRUE;
2694 saved_wait_return = FALSE;
2695 }
2696 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002697
2698 if (line_arg != NULL)
2699 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002700 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002701 theline = line_arg;
2702 p = vim_strchr(theline, '\n');
2703 if (p == NULL)
2704 line_arg += STRLEN(line_arg);
2705 else
2706 {
2707 *p = NUL;
2708 line_arg = p + 1;
2709 }
2710 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002711 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002712 {
2713 vim_free(line_to_free);
2714 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002715 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002716 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002717 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002718 line_to_free = theline;
2719 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002720 if (KeyTyped)
2721 lines_left = Rows - 1;
2722 if (theline == NULL)
2723 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 if (eap->cmdidx == CMD_def)
2725 emsg(_("E1057: Missing :enddef"));
2726 else
2727 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002728 goto erret;
2729 }
2730
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002731 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002732 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002733 if (SOURCING_LNUM < sourcing_lnum_off)
2734 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002735 else
2736 sourcing_lnum_off = 0;
2737
2738 if (skip_until != NULL)
2739 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002741 // * ":append" and "."
2742 // * ":python <<EOF" and "EOF"
2743 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2744 if (heredoc_trimmed == NULL
2745 || (is_heredoc && skipwhite(theline) == theline)
2746 || STRNCMP(theline, heredoc_trimmed,
2747 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002748 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002749 if (heredoc_trimmed == NULL)
2750 p = theline;
2751 else if (is_heredoc)
2752 p = skipwhite(theline) == theline
2753 ? theline : theline + STRLEN(heredoc_trimmed);
2754 else
2755 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002756 if (STRCMP(p, skip_until) == 0)
2757 {
2758 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002759 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002760 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002761 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002762 }
2763 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002764 }
2765 else
2766 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002767 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01002768 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002769 ;
2770
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 // Check for "endfunction" or "enddef".
2772 if (checkforcmd(&p, nesting_def[nesting]
2773 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002774 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002775 char_u *nextcmd = NULL;
2776
Bram Moolenaar663bb232017-06-22 19:12:10 +02002777 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002778 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002779 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002780 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002781 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 give_warning2(eap->cmdidx == CMD_def
2783 ? (char_u *)_("W1001: Text found after :enddef: %s")
2784 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002785 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002786 if (nextcmd != NULL)
2787 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002788 // Another command follows. If the line came from "eap" we
2789 // can simply point into it, otherwise we need to change
2790 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02002791 eap->nextcmd = nextcmd;
2792 if (line_to_free != NULL)
2793 {
2794 vim_free(*eap->cmdlinep);
2795 *eap->cmdlinep = line_to_free;
2796 line_to_free = NULL;
2797 }
2798 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002799 break;
2800 }
2801
Bram Moolenaare38eab22019-12-05 21:50:01 +01002802 // Increase indent inside "if", "while", "for" and "try", decrease
2803 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002805 indent -= 2;
2806 else if (STRNCMP(p, "if", 2) == 0
2807 || STRNCMP(p, "wh", 2) == 0
2808 || STRNCMP(p, "for", 3) == 0
2809 || STRNCMP(p, "try", 3) == 0)
2810 indent += 2;
2811
Bram Moolenaare38eab22019-12-05 21:50:01 +01002812 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01002813 // Only recognize "def" inside "def", not inside "function",
2814 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01002816 if (checkforcmd(&p, "function", 2)
2817 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002818 {
2819 if (*p == '!')
2820 p = skipwhite(p + 1);
2821 p += eval_fname_script(p);
2822 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
2823 if (*skipwhite(p) == '(')
2824 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 if (nesting == MAX_FUNC_NESTING - 1)
2826 emsg(_("E1058: function nesting too deep"));
2827 else
2828 {
2829 ++nesting;
2830 nesting_def[nesting] = (c == 'd');
2831 indent += 2;
2832 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002833 }
2834 }
2835
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002836 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002837 p = skip_range(p, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002838 if (eap->cmdidx != CMD_def
2839 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002840 || (p[0] == 'c'
2841 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2842 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2843 && (STRNCMP(&p[3], "nge", 3) != 0
2844 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002845 || (p[0] == 'i'
2846 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002847 && (!ASCII_ISALPHA(p[2])
2848 || (p[2] == 's'
2849 && (!ASCII_ISALPHA(p[3])
2850 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002851 skip_until = vim_strsave((char_u *)".");
2852
Bram Moolenaare38eab22019-12-05 21:50:01 +01002853 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002854 arg = skipwhite(skiptowhite(p));
2855 if (arg[0] == '<' && arg[1] =='<'
2856 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002857 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2858 || ((p[2] == '3' || p[2] == 'x')
2859 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002860 || (p[0] == 'p' && p[1] == 'e'
2861 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2862 || (p[0] == 't' && p[1] == 'c'
2863 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2864 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2865 && !ASCII_ISALPHA(p[3]))
2866 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2867 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2868 || (p[0] == 'm' && p[1] == 'z'
2869 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2870 ))
2871 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002872 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002873 p = skipwhite(arg + 2);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02002874 if (STRNCMP(p, "trim", 4) == 0)
2875 {
2876 // Ignore leading white space.
2877 p = skipwhite(p + 4);
2878 heredoc_trimmed = vim_strnsave(theline,
2879 (int)(skipwhite(theline) - theline));
2880 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002881 if (*p == NUL)
2882 skip_until = vim_strsave((char_u *)".");
2883 else
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02002884 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2885 do_concat = FALSE;
2886 is_heredoc = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002887 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002888
2889 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002890 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02002891 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002892 if (*arg == '[')
2893 arg = vim_strchr(arg, ']');
2894 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002895 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002896 arg = skipwhite(skiptowhite(arg));
2897 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
2898 && ((p[0] == 'l'
2899 && p[1] == 'e'
2900 && (!ASCII_ISALNUM(p[2])
2901 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02002902 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002903 p = skipwhite(arg + 3);
2904 if (STRNCMP(p, "trim", 4) == 0)
2905 {
2906 // Ignore leading white space.
2907 p = skipwhite(p + 4);
2908 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar8471e572019-05-19 21:37:18 +02002909 (int)(skipwhite(theline) - theline));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002910 }
2911 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2912 do_concat = FALSE;
2913 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002914 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002915 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002916 }
2917
Bram Moolenaare38eab22019-12-05 21:50:01 +01002918 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002919 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002920 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002921
Bram Moolenaare38eab22019-12-05 21:50:01 +01002922 // Copy the line to newly allocated memory. get_one_sourceline()
2923 // allocates 250 bytes per line, this saves 80% on average. The cost
2924 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002925 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002926 if (p == NULL)
2927 goto erret;
2928 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002929
Bram Moolenaare38eab22019-12-05 21:50:01 +01002930 // Add NULL lines for continuation lines, so that the line count is
2931 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002932 while (sourcing_lnum_off-- > 0)
2933 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2934
Bram Moolenaare38eab22019-12-05 21:50:01 +01002935 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002936 if (line_arg != NULL && *line_arg == NUL)
2937 line_arg = NULL;
2938 }
2939
Bram Moolenaare38eab22019-12-05 21:50:01 +01002940 // Don't define the function when skipping commands or when an error was
2941 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002942 if (eap->skip || did_emsg)
2943 goto erret;
2944
2945 /*
2946 * If there are no errors, add the function
2947 */
2948 if (fudi.fd_dict == NULL)
2949 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 hashtab_T *ht;
2951
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002952 v = find_var(name, &ht, FALSE);
2953 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
2954 {
2955 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
2956 name);
2957 goto erret;
2958 }
2959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 fp = find_func_even_dead(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002961 if (fp != NULL)
2962 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 int dead = fp->uf_flags & FC_DEAD;
2964
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002965 // Function can be replaced with "function!" and when sourcing the
2966 // same script again, but only once.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 if (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002968 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
2969 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002970 {
2971 emsg_funcname(e_funcexts, name);
2972 goto erret;
2973 }
2974 if (fp->uf_calls > 0)
2975 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002976 emsg_funcname(
2977 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002978 name);
2979 goto erret;
2980 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002981 if (fp->uf_refcount > 1)
2982 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002983 // This function is referenced somewhere, don't redefine it but
2984 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002985 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002986 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002987 fp = NULL;
2988 overwrite = TRUE;
2989 }
2990 else
2991 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002992 char_u *exp_name = fp->uf_name_exp;
2993
2994 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01002995 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002996 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002997 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002998 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003000#ifdef FEAT_PROFILE
3001 fp->uf_profiling = FALSE;
3002 fp->uf_prof_initialized = FALSE;
3003#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003004 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003005 }
3006 }
3007 else
3008 {
3009 char numbuf[20];
3010
3011 fp = NULL;
3012 if (fudi.fd_newkey == NULL && !eap->forceit)
3013 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003014 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003015 goto erret;
3016 }
3017 if (fudi.fd_di == NULL)
3018 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003019 // Can't add a function to a locked dictionary
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003020 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003021 goto erret;
3022 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003023 // Can't change an existing function if it is locked
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003024 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003025 goto erret;
3026
Bram Moolenaare38eab22019-12-05 21:50:01 +01003027 // Give the function a sequential number. Can only be used with a
3028 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003029 vim_free(name);
3030 sprintf(numbuf, "%d", ++func_nr);
3031 name = vim_strsave((char_u *)numbuf);
3032 if (name == NULL)
3033 goto erret;
3034 }
3035
3036 if (fp == NULL)
3037 {
3038 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3039 {
3040 int slen, plen;
3041 char_u *scriptname;
3042
Bram Moolenaare38eab22019-12-05 21:50:01 +01003043 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003044 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003045 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003046 {
3047 scriptname = autoload_name(name);
3048 if (scriptname != NULL)
3049 {
3050 p = vim_strchr(scriptname, '/');
3051 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003052 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003053 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003054 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003055 j = OK;
3056 vim_free(scriptname);
3057 }
3058 }
3059 if (j == FAIL)
3060 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003061 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003062 goto erret;
3063 }
3064 }
3065
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003066 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003067 if (fp == NULL)
3068 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 fp->uf_dfunc_idx = -1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003070
3071 if (fudi.fd_dict != NULL)
3072 {
3073 if (fudi.fd_di == NULL)
3074 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003075 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003076 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3077 if (fudi.fd_di == NULL)
3078 {
3079 vim_free(fp);
3080 goto erret;
3081 }
3082 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3083 {
3084 vim_free(fudi.fd_di);
3085 vim_free(fp);
3086 goto erret;
3087 }
3088 }
3089 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003090 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003091 clear_tv(&fudi.fd_di->di_tv);
3092 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003093 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003094
Bram Moolenaare38eab22019-12-05 21:50:01 +01003095 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003096 flags |= FC_DICT;
3097 }
3098
Bram Moolenaare38eab22019-12-05 21:50:01 +01003099 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003100 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003101 if (overwrite)
3102 {
3103 hi = hash_find(&func_hashtab, name);
3104 hi->hi_key = UF2HIKEY(fp);
3105 }
3106 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003107 {
3108 vim_free(fp);
3109 goto erret;
3110 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003111 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003112 }
3113 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003114 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003116 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117
3118 if (eap->cmdidx == CMD_def)
3119 {
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003120 int lnum_save = SOURCING_LNUM;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003121
3122 // error messages are for the first function line
3123 SOURCING_LNUM = sourcing_lnum_top;
3124
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 // parse the argument types
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003126 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127
3128 if (argtypes.ga_len > 0)
3129 {
3130 // When "varargs" is set the last name/type goes into uf_va_name
3131 // and uf_va_type.
3132 int len = argtypes.ga_len - (varargs ? 1 : 0);
3133
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003134 if (len > 0)
3135 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 if (fp->uf_arg_types != NULL)
3137 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003138 int i;
3139 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140
3141 for (i = 0; i < len; ++ i)
3142 {
3143 p = ((char_u **)argtypes.ga_data)[i];
3144 if (p == NULL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003145 // will get the type from the default value
3146 type = &t_unknown;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 else
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003148 type = parse_type(&p, &fp->uf_type_list);
3149 if (type == NULL)
3150 {
3151 SOURCING_LNUM = lnum_save;
3152 goto errret_2;
3153 }
3154 fp->uf_arg_types[i] = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 }
3156 }
3157 if (varargs)
3158 {
3159 // Move the last argument "...name: type" to uf_va_name and
3160 // uf_va_type.
3161 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
3162 [fp->uf_args.ga_len - 1];
3163 --fp->uf_args.ga_len;
3164 p = ((char_u **)argtypes.ga_data)[len];
3165 if (p == NULL)
3166 // todo: get type from default value
3167 fp->uf_va_type = &t_any;
3168 else
3169 fp->uf_va_type = parse_type(&p, &fp->uf_type_list);
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003170 if (fp->uf_va_type == NULL)
3171 {
3172 SOURCING_LNUM = lnum_save;
3173 goto errret_2;
3174 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 }
3176 varargs = FALSE;
3177 }
3178
3179 // parse the return type, if any
3180 if (ret_type == NULL)
3181 fp->uf_ret_type = &t_void;
3182 else
3183 {
3184 p = ret_type;
3185 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list);
3186 }
3187 }
3188
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003189 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003190 if ((flags & FC_CLOSURE) != 0)
3191 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003192 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003193 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003194 }
3195 else
3196 fp->uf_scoped = NULL;
3197
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003198#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003199 if (prof_def_func())
3200 func_do_profile(fp);
3201#endif
3202 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003203 if (sandbox)
3204 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003205 fp->uf_flags = flags;
3206 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003207 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003208 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003209 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 if (is_export)
3211 {
3212 fp->uf_flags |= FC_EXPORT;
3213 // let ex_export() know the export worked.
3214 is_export = FALSE;
3215 }
3216
3217 // ":def Func()" needs to be compiled
3218 if (eap->cmdidx == CMD_def)
3219 compile_def_function(fp, FALSE);
3220
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003221 goto ret_free;
3222
3223erret:
3224 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003225 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003226errret_2:
3227 ga_clear_strings(&newlines);
3228ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003229 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003230 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003231 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003232 vim_free(fudi.fd_newkey);
3233 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003234 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003235 did_emsg |= saved_did_emsg;
3236 need_wait_return |= saved_wait_return;
3237}
3238
3239/*
3240 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3241 * Return 2 if "p" starts with "s:".
3242 * Return 0 otherwise.
3243 */
3244 int
3245eval_fname_script(char_u *p)
3246{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003247 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3248 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003249 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3250 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3251 return 5;
3252 if (p[0] == 's' && p[1] == ':')
3253 return 2;
3254 return 0;
3255}
3256
3257 int
3258translated_function_exists(char_u *name)
3259{
3260 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003261 return has_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 return find_func(name, NULL) != NULL;
3263}
3264
3265/*
3266 * Return TRUE when "ufunc" has old-style "..." varargs
3267 * or named varargs "...name: type".
3268 */
3269 int
3270has_varargs(ufunc_T *ufunc)
3271{
3272 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003273}
3274
3275/*
3276 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003277 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003278 */
3279 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003280function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003281{
3282 char_u *nm = name;
3283 char_u *p;
3284 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003285 int flag;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003286
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003287 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3288 if (no_deref)
3289 flag |= TFN_NO_DEREF;
3290 p = trans_function_name(&nm, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003291 nm = skipwhite(nm);
3292
Bram Moolenaare38eab22019-12-05 21:50:01 +01003293 // Only accept "funcname", "funcname ", "funcname (..." and
3294 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003295 if (p != NULL && (*nm == NUL || *nm == '('))
3296 n = translated_function_exists(p);
3297 vim_free(p);
3298 return n;
3299}
3300
Bram Moolenaar113e1072019-01-20 15:30:40 +01003301#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003302 char_u *
3303get_expanded_name(char_u *name, int check)
3304{
3305 char_u *nm = name;
3306 char_u *p;
3307
3308 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
3309
3310 if (p != NULL && *nm == NUL)
3311 if (!check || translated_function_exists(p))
3312 return p;
3313
3314 vim_free(p);
3315 return NULL;
3316}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003317#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003318
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003319/*
3320 * Function given to ExpandGeneric() to obtain the list of user defined
3321 * function names.
3322 */
3323 char_u *
3324get_user_func_name(expand_T *xp, int idx)
3325{
3326 static long_u done;
3327 static hashitem_T *hi;
3328 ufunc_T *fp;
3329
3330 if (idx == 0)
3331 {
3332 done = 0;
3333 hi = func_hashtab.ht_array;
3334 }
3335 if (done < func_hashtab.ht_used)
3336 {
3337 if (done++ > 0)
3338 ++hi;
3339 while (HASHITEM_EMPTY(hi))
3340 ++hi;
3341 fp = HI2UF(hi);
3342
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 // don't show dead, dict and lambda functions
3344 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02003345 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003347
3348 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003349 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003350
3351 cat_func_name(IObuff, fp);
3352 if (xp->xp_context != EXPAND_USER_FUNC)
3353 {
3354 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003356 STRCAT(IObuff, ")");
3357 }
3358 return IObuff;
3359 }
3360 return NULL;
3361}
3362
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003363/*
3364 * ":delfunction {name}"
3365 */
3366 void
3367ex_delfunction(exarg_T *eap)
3368{
3369 ufunc_T *fp = NULL;
3370 char_u *p;
3371 char_u *name;
3372 funcdict_T fudi;
3373
3374 p = eap->arg;
3375 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
3376 vim_free(fudi.fd_newkey);
3377 if (name == NULL)
3378 {
3379 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003380 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003381 return;
3382 }
3383 if (!ends_excmd(*skipwhite(p)))
3384 {
3385 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003386 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003387 return;
3388 }
3389 eap->nextcmd = check_nextcmd(p);
3390 if (eap->nextcmd != NULL)
3391 *p = NUL;
3392
3393 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 fp = find_func(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003395 vim_free(name);
3396
3397 if (!eap->skip)
3398 {
3399 if (fp == NULL)
3400 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02003401 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003402 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003403 return;
3404 }
3405 if (fp->uf_calls > 0)
3406 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003407 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003408 return;
3409 }
3410
3411 if (fudi.fd_dict != NULL)
3412 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003413 // Delete the dict item that refers to the function, it will
3414 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003415 dictitem_remove(fudi.fd_dict, fudi.fd_di);
3416 }
3417 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003418 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003419 // A normal function (not a numbered function or lambda) has a
3420 // refcount of 1 for the entry in the hashtable. When deleting
3421 // it and the refcount is more than one, it should be kept.
3422 // A numbered function and lambda should be kept if the refcount is
3423 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003424 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003425 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003426 // Function is still referenced somewhere. Don't free it but
3427 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003428 if (func_remove(fp))
3429 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003430 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003431 }
3432 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003433 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003434 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003435 }
3436}
3437
3438/*
3439 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003440 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003441 */
3442 void
3443func_unref(char_u *name)
3444{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003445 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003446
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003447 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003448 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003450 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003451 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003452#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003453 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003454#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01003455 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003456 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003457 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003458 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003459 // Only delete it when it's not being used. Otherwise it's done
3460 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003461 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003462 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02003463 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003464}
3465
3466/*
3467 * Unreference a Function: decrement the reference count and free it when it
3468 * becomes zero.
3469 */
3470 void
3471func_ptr_unref(ufunc_T *fp)
3472{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003473 if (fp != NULL && --fp->uf_refcount <= 0)
3474 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003475 // Only delete it when it's not being used. Otherwise it's done
3476 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02003477 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003478 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003479 }
3480}
3481
3482/*
3483 * Count a reference to a Function.
3484 */
3485 void
3486func_ref(char_u *name)
3487{
3488 ufunc_T *fp;
3489
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003490 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003491 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003493 if (fp != NULL)
3494 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003495 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01003496 // Only give an error for a numbered function.
3497 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01003498 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003499}
3500
3501/*
3502 * Count a reference to a Function.
3503 */
3504 void
3505func_ptr_ref(ufunc_T *fp)
3506{
3507 if (fp != NULL)
3508 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003509}
3510
3511/*
3512 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3513 * referenced from anywhere that is in use.
3514 */
3515 static int
3516can_free_funccal(funccall_T *fc, int copyID)
3517{
3518 return (fc->l_varlist.lv_copyID != copyID
3519 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003520 && fc->l_avars.dv_copyID != copyID
3521 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003522}
3523
3524/*
3525 * ":return [expr]"
3526 */
3527 void
3528ex_return(exarg_T *eap)
3529{
3530 char_u *arg = eap->arg;
3531 typval_T rettv;
3532 int returning = FALSE;
3533
3534 if (current_funccal == NULL)
3535 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003536 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003537 return;
3538 }
3539
3540 if (eap->skip)
3541 ++emsg_skip;
3542
3543 eap->nextcmd = NULL;
3544 if ((*arg != NUL && *arg != '|' && *arg != '\n')
3545 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
3546 {
3547 if (!eap->skip)
3548 returning = do_return(eap, FALSE, TRUE, &rettv);
3549 else
3550 clear_tv(&rettv);
3551 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003552 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003553 else if (!eap->skip)
3554 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003555 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003556 update_force_abort();
3557
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003558 /*
3559 * Return unless the expression evaluation has been cancelled due to an
3560 * aborting error, an interrupt, or an exception.
3561 */
3562 if (!aborting())
3563 returning = do_return(eap, FALSE, TRUE, NULL);
3564 }
3565
Bram Moolenaare38eab22019-12-05 21:50:01 +01003566 // When skipping or the return gets pending, advance to the next command
3567 // in this line (!returning). Otherwise, ignore the rest of the line.
3568 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003569 if (returning)
3570 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003571 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003572 eap->nextcmd = check_nextcmd(arg);
3573
3574 if (eap->skip)
3575 --emsg_skip;
3576}
3577
3578/*
3579 * ":1,25call func(arg1, arg2)" function call.
3580 */
3581 void
3582ex_call(exarg_T *eap)
3583{
3584 char_u *arg = eap->arg;
3585 char_u *startarg;
3586 char_u *name;
3587 char_u *tofree;
3588 int len;
3589 typval_T rettv;
3590 linenr_T lnum;
3591 int doesrange;
3592 int failed = FALSE;
3593 funcdict_T fudi;
3594 partial_T *partial = NULL;
3595
3596 if (eap->skip)
3597 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003598 // trans_function_name() doesn't work well when skipping, use eval0()
3599 // instead to skip to any following command, e.g. for:
3600 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003601 ++emsg_skip;
3602 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3603 clear_tv(&rettv);
3604 --emsg_skip;
3605 return;
3606 }
3607
3608 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
3609 if (fudi.fd_newkey != NULL)
3610 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003611 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003612 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003613 vim_free(fudi.fd_newkey);
3614 }
3615 if (tofree == NULL)
3616 return;
3617
Bram Moolenaare38eab22019-12-05 21:50:01 +01003618 // Increase refcount on dictionary, it could get deleted when evaluating
3619 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003620 if (fudi.fd_dict != NULL)
3621 ++fudi.fd_dict->dv_refcount;
3622
Bram Moolenaare38eab22019-12-05 21:50:01 +01003623 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3624 // contents. For VAR_PARTIAL get its partial, unless we already have one
3625 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003626 len = (int)STRLEN(tofree);
3627 name = deref_func_name(tofree, &len,
3628 partial != NULL ? NULL : &partial, FALSE);
3629
Bram Moolenaare38eab22019-12-05 21:50:01 +01003630 // Skip white space to allow ":call func ()". Not good, but required for
3631 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003632 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003633 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003634
3635 if (*startarg != '(')
3636 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003638 goto end;
3639 }
3640
3641 /*
3642 * When skipping, evaluate the function once, to find the end of the
3643 * arguments.
3644 * When the function takes a range, this is discovered after the first
3645 * call, and the loop is broken.
3646 */
3647 if (eap->skip)
3648 {
3649 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003650 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003651 }
3652 else
3653 lnum = eap->line1;
3654 for ( ; lnum <= eap->line2; ++lnum)
3655 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003656 funcexe_T funcexe;
3657
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003658 if (!eap->skip && eap->addr_count > 0)
3659 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003660 if (lnum > curbuf->b_ml.ml_line_count)
3661 {
3662 // If the function deleted lines or switched to another buffer
3663 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003664 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003665 break;
3666 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003667 curwin->w_cursor.lnum = lnum;
3668 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003669 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003670 }
3671 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003672
Bram Moolenaara80faa82020-04-12 19:37:17 +02003673 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003674 funcexe.firstline = eap->line1;
3675 funcexe.lastline = eap->line2;
3676 funcexe.doesrange = &doesrange;
3677 funcexe.evaluate = !eap->skip;
3678 funcexe.partial = partial;
3679 funcexe.selfdict = fudi.fd_dict;
3680 if (get_func_tv(name, -1, &rettv, &arg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003681 {
3682 failed = TRUE;
3683 break;
3684 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003685 if (has_watchexpr())
3686 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003687
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003688 // Handle a function returning a Funcref, Dictionary or List.
3689 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE,
3690 name, &name) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003691 {
3692 failed = TRUE;
3693 break;
3694 }
3695
3696 clear_tv(&rettv);
3697 if (doesrange || eap->skip)
3698 break;
3699
Bram Moolenaare38eab22019-12-05 21:50:01 +01003700 // Stop when immediately aborting on error, or when an interrupt
3701 // occurred or an exception was thrown but not caught.
3702 // get_func_tv() returned OK, so that the check for trailing
3703 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003704 if (aborting())
3705 break;
3706 }
3707 if (eap->skip)
3708 --emsg_skip;
3709
Bram Moolenaare51bb172020-02-16 19:42:23 +01003710 // When inside :try we need to check for following "| catch".
3711 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003712 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003713 // Check for trailing illegal characters and a following command.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003714 if (!ends_excmd(*arg))
3715 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01003716 if (!failed)
3717 {
3718 emsg_severe = TRUE;
3719 emsg(_(e_trailing));
3720 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003721 }
3722 else
3723 eap->nextcmd = check_nextcmd(arg);
3724 }
3725
3726end:
3727 dict_unref(fudi.fd_dict);
3728 vim_free(tofree);
3729}
3730
3731/*
3732 * Return from a function. Possibly makes the return pending. Also called
3733 * for a pending return at the ":endtry" or after returning from an extra
3734 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3735 * when called due to a ":return" command. "rettv" may point to a typval_T
3736 * with the return rettv. Returns TRUE when the return can be carried out,
3737 * FALSE when the return gets pending.
3738 */
3739 int
3740do_return(
3741 exarg_T *eap,
3742 int reanimate,
3743 int is_cmd,
3744 void *rettv)
3745{
3746 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01003747 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003748
3749 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003750 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003751 current_funccal->returned = FALSE;
3752
3753 /*
3754 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3755 * not in its finally clause (which then is to be executed next) is found.
3756 * In this case, make the ":return" pending for execution at the ":endtry".
3757 * Otherwise, return normally.
3758 */
3759 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3760 if (idx >= 0)
3761 {
3762 cstack->cs_pending[idx] = CSTP_RETURN;
3763
3764 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003765 // A pending return again gets pending. "rettv" points to an
3766 // allocated variable with the rettv of the original ":return"'s
3767 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003768 cstack->cs_rettv[idx] = rettv;
3769 else
3770 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003771 // When undoing a return in order to make it pending, get the stored
3772 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003773 if (reanimate)
3774 rettv = current_funccal->rettv;
3775
3776 if (rettv != NULL)
3777 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003778 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003779 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3780 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3781 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003782 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003783 }
3784 else
3785 cstack->cs_rettv[idx] = NULL;
3786
3787 if (reanimate)
3788 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003789 // The pending return value could be overwritten by a ":return"
3790 // without argument in a finally clause; reset the default
3791 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003792 current_funccal->rettv->v_type = VAR_NUMBER;
3793 current_funccal->rettv->vval.v_number = 0;
3794 }
3795 }
3796 report_make_pending(CSTP_RETURN, rettv);
3797 }
3798 else
3799 {
3800 current_funccal->returned = TRUE;
3801
Bram Moolenaare38eab22019-12-05 21:50:01 +01003802 // If the return is carried out now, store the return value. For
3803 // a return immediately after reanimation, the value is already
3804 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003805 if (!reanimate && rettv != NULL)
3806 {
3807 clear_tv(current_funccal->rettv);
3808 *current_funccal->rettv = *(typval_T *)rettv;
3809 if (!is_cmd)
3810 vim_free(rettv);
3811 }
3812 }
3813
3814 return idx < 0;
3815}
3816
3817/*
3818 * Free the variable with a pending return value.
3819 */
3820 void
3821discard_pending_return(void *rettv)
3822{
3823 free_tv((typval_T *)rettv);
3824}
3825
3826/*
3827 * Generate a return command for producing the value of "rettv". The result
3828 * is an allocated string. Used by report_pending() for verbose messages.
3829 */
3830 char_u *
3831get_return_cmd(void *rettv)
3832{
3833 char_u *s = NULL;
3834 char_u *tofree = NULL;
3835 char_u numbuf[NUMBUFLEN];
3836
3837 if (rettv != NULL)
3838 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3839 if (s == NULL)
3840 s = (char_u *)"";
3841
3842 STRCPY(IObuff, ":return ");
3843 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3844 if (STRLEN(s) + 8 >= IOSIZE)
3845 STRCPY(IObuff + IOSIZE - 4, "...");
3846 vim_free(tofree);
3847 return vim_strsave(IObuff);
3848}
3849
3850/*
3851 * Get next function line.
3852 * Called by do_cmdline() to get the next line.
3853 * Returns allocated string, or NULL for end of function.
3854 */
3855 char_u *
3856get_func_line(
3857 int c UNUSED,
3858 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003859 int indent UNUSED,
3860 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003861{
3862 funccall_T *fcp = (funccall_T *)cookie;
3863 ufunc_T *fp = fcp->func;
3864 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003865 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003866
Bram Moolenaare38eab22019-12-05 21:50:01 +01003867 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003868 if (fcp->dbg_tick != debug_tick)
3869 {
3870 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003871 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003872 fcp->dbg_tick = debug_tick;
3873 }
3874#ifdef FEAT_PROFILE
3875 if (do_profiling == PROF_YES)
3876 func_line_end(cookie);
3877#endif
3878
3879 gap = &fp->uf_lines;
3880 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3881 || fcp->returned)
3882 retval = NULL;
3883 else
3884 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003885 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003886 while (fcp->linenr < gap->ga_len
3887 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3888 ++fcp->linenr;
3889 if (fcp->linenr >= gap->ga_len)
3890 retval = NULL;
3891 else
3892 {
3893 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003894 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003895#ifdef FEAT_PROFILE
3896 if (do_profiling == PROF_YES)
3897 func_line_start(cookie);
3898#endif
3899 }
3900 }
3901
Bram Moolenaare38eab22019-12-05 21:50:01 +01003902 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003903 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003904 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003905 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003906 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003907 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003908 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003909 fcp->dbg_tick = debug_tick;
3910 }
3911
3912 return retval;
3913}
3914
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003915/*
3916 * Return TRUE if the currently active function should be ended, because a
3917 * return was encountered or an error occurred. Used inside a ":while".
3918 */
3919 int
3920func_has_ended(void *cookie)
3921{
3922 funccall_T *fcp = (funccall_T *)cookie;
3923
Bram Moolenaare38eab22019-12-05 21:50:01 +01003924 // Ignore the "abort" flag if the abortion behavior has been changed due to
3925 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003926 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3927 || fcp->returned);
3928}
3929
3930/*
3931 * return TRUE if cookie indicates a function which "abort"s on errors.
3932 */
3933 int
3934func_has_abort(
3935 void *cookie)
3936{
3937 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
3938}
3939
3940
3941/*
3942 * Turn "dict.Func" into a partial for "Func" bound to "dict".
3943 * Don't do this when "Func" is already a partial that was bound
3944 * explicitly (pt_auto is FALSE).
3945 * Changes "rettv" in-place.
3946 * Returns the updated "selfdict_in".
3947 */
3948 dict_T *
3949make_partial(dict_T *selfdict_in, typval_T *rettv)
3950{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003951 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003952 char_u *tofree = NULL;
3953 ufunc_T *fp;
3954 char_u fname_buf[FLEN_FIXED + 1];
3955 int error;
3956 dict_T *selfdict = selfdict_in;
3957
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003958 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
3959 fp = rettv->vval.v_partial->pt_func;
3960 else
3961 {
3962 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
3963 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003964 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003965 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003967 vim_free(tofree);
3968 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003969
3970 if (fp != NULL && (fp->uf_flags & FC_DICT))
3971 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003972 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003973
3974 if (pt != NULL)
3975 {
3976 pt->pt_refcount = 1;
3977 pt->pt_dict = selfdict;
3978 pt->pt_auto = TRUE;
3979 selfdict = NULL;
3980 if (rettv->v_type == VAR_FUNC)
3981 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003982 // Just a function: Take over the function name and use
3983 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003984 pt->pt_name = rettv->vval.v_string;
3985 }
3986 else
3987 {
3988 partial_T *ret_pt = rettv->vval.v_partial;
3989 int i;
3990
Bram Moolenaare38eab22019-12-05 21:50:01 +01003991 // Partial: copy the function name, use selfdict and copy
3992 // args. Can't take over name or args, the partial might
3993 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003994 if (ret_pt->pt_name != NULL)
3995 {
3996 pt->pt_name = vim_strsave(ret_pt->pt_name);
3997 func_ref(pt->pt_name);
3998 }
3999 else
4000 {
4001 pt->pt_func = ret_pt->pt_func;
4002 func_ptr_ref(pt->pt_func);
4003 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004004 if (ret_pt->pt_argc > 0)
4005 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004006 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004007 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004008 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004009 pt->pt_argc = 0;
4010 else
4011 {
4012 pt->pt_argc = ret_pt->pt_argc;
4013 for (i = 0; i < pt->pt_argc; i++)
4014 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
4015 }
4016 }
4017 partial_unref(ret_pt);
4018 }
4019 rettv->v_type = VAR_PARTIAL;
4020 rettv->vval.v_partial = pt;
4021 }
4022 }
4023 return selfdict;
4024}
4025
4026/*
4027 * Return the name of the executed function.
4028 */
4029 char_u *
4030func_name(void *cookie)
4031{
4032 return ((funccall_T *)cookie)->func->uf_name;
4033}
4034
4035/*
4036 * Return the address holding the next breakpoint line for a funccall cookie.
4037 */
4038 linenr_T *
4039func_breakpoint(void *cookie)
4040{
4041 return &((funccall_T *)cookie)->breakpoint;
4042}
4043
4044/*
4045 * Return the address holding the debug tick for a funccall cookie.
4046 */
4047 int *
4048func_dbg_tick(void *cookie)
4049{
4050 return &((funccall_T *)cookie)->dbg_tick;
4051}
4052
4053/*
4054 * Return the nesting level for a funccall cookie.
4055 */
4056 int
4057func_level(void *cookie)
4058{
4059 return ((funccall_T *)cookie)->level;
4060}
4061
4062/*
4063 * Return TRUE when a function was ended by a ":return" command.
4064 */
4065 int
4066current_func_returned(void)
4067{
4068 return current_funccal->returned;
4069}
4070
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004071 int
4072free_unref_funccal(int copyID, int testing)
4073{
4074 int did_free = FALSE;
4075 int did_free_funccal = FALSE;
4076 funccall_T *fc, **pfc;
4077
4078 for (pfc = &previous_funccal; *pfc != NULL; )
4079 {
4080 if (can_free_funccal(*pfc, copyID))
4081 {
4082 fc = *pfc;
4083 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004084 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004085 did_free = TRUE;
4086 did_free_funccal = TRUE;
4087 }
4088 else
4089 pfc = &(*pfc)->caller;
4090 }
4091 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004092 // When a funccal was freed some more items might be garbage
4093 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004094 (void)garbage_collect(testing);
4095
4096 return did_free;
4097}
4098
4099/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004100 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004101 */
4102 static funccall_T *
4103get_funccal(void)
4104{
4105 int i;
4106 funccall_T *funccal;
4107 funccall_T *temp_funccal;
4108
4109 funccal = current_funccal;
4110 if (debug_backtrace_level > 0)
4111 {
4112 for (i = 0; i < debug_backtrace_level; i++)
4113 {
4114 temp_funccal = funccal->caller;
4115 if (temp_funccal)
4116 funccal = temp_funccal;
4117 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004118 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004119 debug_backtrace_level = i;
4120 }
4121 }
4122 return funccal;
4123}
4124
4125/*
4126 * Return the hashtable used for local variables in the current funccal.
4127 * Return NULL if there is no current funccal.
4128 */
4129 hashtab_T *
4130get_funccal_local_ht()
4131{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004133 return NULL;
4134 return &get_funccal()->l_vars.dv_hashtab;
4135}
4136
4137/*
4138 * Return the l: scope variable.
4139 * Return NULL if there is no current funccal.
4140 */
4141 dictitem_T *
4142get_funccal_local_var()
4143{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004145 return NULL;
4146 return &get_funccal()->l_vars_var;
4147}
4148
4149/*
4150 * Return the hashtable used for argument in the current funccal.
4151 * Return NULL if there is no current funccal.
4152 */
4153 hashtab_T *
4154get_funccal_args_ht()
4155{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004157 return NULL;
4158 return &get_funccal()->l_avars.dv_hashtab;
4159}
4160
4161/*
4162 * Return the a: scope variable.
4163 * Return NULL if there is no current funccal.
4164 */
4165 dictitem_T *
4166get_funccal_args_var()
4167{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004169 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004170 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004171}
4172
4173/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004174 * List function variables, if there is a function.
4175 */
4176 void
4177list_func_vars(int *first)
4178{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004180 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004181 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004182}
4183
4184/*
4185 * If "ht" is the hashtable for local variables in the current funccal, return
4186 * the dict that contains it.
4187 * Otherwise return NULL.
4188 */
4189 dict_T *
4190get_current_funccal_dict(hashtab_T *ht)
4191{
4192 if (current_funccal != NULL
4193 && ht == &current_funccal->l_vars.dv_hashtab)
4194 return &current_funccal->l_vars;
4195 return NULL;
4196}
4197
4198/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004199 * Search hashitem in parent scope.
4200 */
4201 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004202find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004203{
4204 funccall_T *old_current_funccal = current_funccal;
4205 hashtab_T *ht;
4206 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004207 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004208
4209 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4210 return NULL;
4211
Bram Moolenaare38eab22019-12-05 21:50:01 +01004212 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004213 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004214 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004215 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004216 ht = find_var_ht(name, &varname);
4217 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004218 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004219 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004220 if (!HASHITEM_EMPTY(hi))
4221 {
4222 *pht = ht;
4223 break;
4224 }
4225 }
4226 if (current_funccal == current_funccal->func->uf_scoped)
4227 break;
4228 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004229 }
4230 current_funccal = old_current_funccal;
4231
4232 return hi;
4233}
4234
4235/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004236 * Search variable in parent scope.
4237 */
4238 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004239find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004240{
4241 dictitem_T *v = NULL;
4242 funccall_T *old_current_funccal = current_funccal;
4243 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004244 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004245
4246 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4247 return NULL;
4248
Bram Moolenaare38eab22019-12-05 21:50:01 +01004249 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004250 current_funccal = current_funccal->func->uf_scoped;
4251 while (current_funccal)
4252 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004253 ht = find_var_ht(name, &varname);
4254 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004255 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004256 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004257 if (v != NULL)
4258 break;
4259 }
4260 if (current_funccal == current_funccal->func->uf_scoped)
4261 break;
4262 current_funccal = current_funccal->func->uf_scoped;
4263 }
4264 current_funccal = old_current_funccal;
4265
4266 return v;
4267}
4268
4269/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004270 * Set "copyID + 1" in previous_funccal and callers.
4271 */
4272 int
4273set_ref_in_previous_funccal(int copyID)
4274{
4275 int abort = FALSE;
4276 funccall_T *fc;
4277
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004278 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004279 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004280 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004281 abort = abort
4282 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4283 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004284 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004285 }
4286 return abort;
4287}
4288
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004289 static int
4290set_ref_in_funccal(funccall_T *fc, int copyID)
4291{
4292 int abort = FALSE;
4293
4294 if (fc->fc_copyID != copyID)
4295 {
4296 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004297 abort = abort
4298 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4299 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004300 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004301 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004302 }
4303 return abort;
4304}
4305
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004306/*
4307 * Set "copyID" in all local vars and arguments in the call stack.
4308 */
4309 int
4310set_ref_in_call_stack(int copyID)
4311{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004312 int abort = FALSE;
4313 funccall_T *fc;
4314 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004315
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004316 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004317 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004318
4319 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004320 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
4321 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004322 abort = abort || set_ref_in_funccal(fc, copyID);
4323
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004324 return abort;
4325}
4326
4327/*
4328 * Set "copyID" in all functions available by name.
4329 */
4330 int
4331set_ref_in_functions(int copyID)
4332{
4333 int todo;
4334 hashitem_T *hi = NULL;
4335 int abort = FALSE;
4336 ufunc_T *fp;
4337
4338 todo = (int)func_hashtab.ht_used;
4339 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004340 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004341 if (!HASHITEM_EMPTY(hi))
4342 {
4343 --todo;
4344 fp = HI2UF(hi);
4345 if (!func_name_refcount(fp->uf_name))
4346 abort = abort || set_ref_in_func(NULL, fp, copyID);
4347 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004348 }
4349 return abort;
4350}
4351
4352/*
4353 * Set "copyID" in all function arguments.
4354 */
4355 int
4356set_ref_in_func_args(int copyID)
4357{
4358 int i;
4359 int abort = FALSE;
4360
4361 for (i = 0; i < funcargs.ga_len; ++i)
4362 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
4363 copyID, NULL, NULL);
4364 return abort;
4365}
4366
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004367/*
4368 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004369 * Returns TRUE if setting references failed somehow.
4370 */
4371 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004372set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004373{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004374 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004375 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01004376 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004377 char_u fname_buf[FLEN_FIXED + 1];
4378 char_u *tofree = NULL;
4379 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004380 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004381
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004382 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004383 return FALSE;
4384
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004385 if (fp_in == NULL)
4386 {
4387 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004389 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004390 if (fp != NULL)
4391 {
4392 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004393 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004394 }
4395 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004396 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004397}
4398
Bram Moolenaare38eab22019-12-05 21:50:01 +01004399#endif // FEAT_EVAL