blob: c6a8a8cd3d336f09512eec65c905d6dfa1d706ca [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 Moolenaara9b579f2016-07-17 18:29:19 +020017/*
18 * All user-defined functions are found in this hashtable.
19 */
20static hashtab_T func_hashtab;
21
Bram Moolenaare38eab22019-12-05 21:50:01 +010022// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020023static garray_T funcargs = GA_EMPTY;
24
Bram Moolenaar209b8e32019-03-14 13:43:24 +010025// pointer to funccal for currently active function
26static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaar209b8e32019-03-14 13:43:24 +010028// Pointer to list of previously used funccal, still around because some
29// item in it is still being used.
30static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020031
32static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
33static char *e_funcdict = N_("E717: Dictionary entry already exists");
34static char *e_funcref = N_("E718: Funcref required");
35static char *e_nofunc = N_("E130: Unknown function: %s");
36
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020037static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020038
39 void
40func_init()
41{
42 hash_init(&func_hashtab);
43}
44
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020045/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020046 * Return the function hash table
47 */
48 hashtab_T *
49func_tbl_get(void)
50{
51 return &func_hashtab;
52}
53
54/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020055 * Get one function argument.
56 * If "argtypes" is not NULL also get the type: "arg: type".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010057 * Return a pointer to after the type.
58 * When something is wrong return "arg".
59 */
60 static char_u *
61one_function_arg(char_u *arg, garray_T *newargs, garray_T *argtypes, int skip)
62{
Bram Moolenaar6e949782020-04-13 17:21:00 +020063 char_u *p = arg;
64 char_u *arg_copy = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010065
66 while (ASCII_ISALNUM(*p) || *p == '_')
67 ++p;
68 if (arg == p || isdigit(*arg)
69 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
70 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
71 {
72 if (!skip)
73 semsg(_("E125: Illegal argument: %s"), arg);
74 return arg;
75 }
76 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
77 return arg;
78 if (newargs != NULL)
79 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010080 int c;
81 int i;
82
83 c = *p;
84 *p = NUL;
85 arg_copy = vim_strsave(arg);
86 if (arg_copy == NULL)
87 {
88 *p = c;
89 return arg;
90 }
91
92 // Check for duplicate argument name.
93 for (i = 0; i < newargs->ga_len; ++i)
94 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
95 {
96 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
97 vim_free(arg_copy);
98 return arg;
99 }
100 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
101 newargs->ga_len++;
102
103 *p = c;
104 }
105
106 // get any type from "arg: type"
107 if (argtypes != NULL && ga_grow(argtypes, 1) == OK)
108 {
109 char_u *type = NULL;
110
Bram Moolenaar6e949782020-04-13 17:21:00 +0200111 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
112 {
113 semsg(_("E1059: No white space allowed before colon: %s"),
114 arg_copy == NULL ? arg : arg_copy);
115 p = skipwhite(p);
116 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100117 if (*p == ':')
118 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200119 ++p;
120 if (!VIM_ISWHITE(*p))
121 {
122 semsg(_(e_white_after), ":");
123 return arg;
124 }
125 type = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100126 p = skip_type(type);
127 type = vim_strnsave(type, p - type);
128 }
Bram Moolenaar6e949782020-04-13 17:21:00 +0200129 else if (*skipwhite(p) != '=')
130 {
131 semsg(_("E1077: Missing argument type for %s"),
132 arg_copy == NULL ? arg : arg_copy);
133 return arg;
134 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
136 }
137
138 return p;
139}
140
141/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200142 * Get function arguments.
143 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200145get_function_args(
146 char_u **argp,
147 char_u endchar,
148 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149 garray_T *argtypes, // NULL unless using :def
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200150 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200151 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200152 int skip,
153 exarg_T *eap,
154 char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200155{
156 int mustend = FALSE;
157 char_u *arg = *argp;
158 char_u *p = arg;
159 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200160 int any_default = FALSE;
161 char_u *expr;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200162 char_u *whitep = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200163
164 if (newargs != NULL)
165 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 if (argtypes != NULL)
167 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200168 if (default_args != NULL)
169 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200170
171 if (varargs != NULL)
172 *varargs = FALSE;
173
174 /*
175 * Isolate the arguments: "arg1, arg2, ...)"
176 */
177 while (*p != endchar)
178 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200179 while (eap != NULL && eap->getline != NULL
180 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200181 {
182 char_u *theline;
183
184 // End of the line, get the next one.
185 theline = eap->getline(':', eap->cookie, 0, TRUE);
186 if (theline == NULL)
187 break;
188 vim_free(*line_to_free);
189 *line_to_free = theline;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200190 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200191 p = skipwhite(theline);
192 }
193
194 if (mustend && *p != endchar)
195 {
196 if (!skip)
197 semsg(_(e_invarg2), *argp);
198 break;
199 }
200 if (*p == endchar)
201 break;
202
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200203 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
204 {
205 if (varargs != NULL)
206 *varargs = TRUE;
207 p += 3;
208 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100209
210 if (argtypes != NULL)
211 {
212 // ...name: list<type>
213 if (!ASCII_ISALPHA(*p))
214 {
215 emsg(_("E1055: Missing name after ..."));
216 break;
217 }
218
219 arg = p;
220 p = one_function_arg(p, newargs, argtypes, skip);
221 if (p == arg)
222 break;
223 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200224 }
225 else
226 {
227 arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 p = one_function_arg(p, newargs, argtypes, skip);
229 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200230 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200231
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200232 if (*skipwhite(p) == '=' && default_args != NULL)
233 {
234 typval_T rettv;
235
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100236 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200237 any_default = TRUE;
238 p = skipwhite(p) + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200239 whitep = p;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200240 p = skipwhite(p);
241 expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200242 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200243 {
244 if (ga_grow(default_args, 1) == FAIL)
245 goto err_ret;
246
247 // trim trailing whitespace
248 while (p > expr && VIM_ISWHITE(p[-1]))
249 p--;
250 c = *p;
251 *p = NUL;
252 expr = vim_strsave(expr);
253 if (expr == NULL)
254 {
255 *p = c;
256 goto err_ret;
257 }
258 ((char_u **)(default_args->ga_data))
259 [default_args->ga_len] = expr;
260 default_args->ga_len++;
261 *p = c;
262 }
263 else
264 mustend = TRUE;
265 }
266 else if (any_default)
267 {
268 emsg(_("E989: Non-default argument follows default argument"));
269 mustend = TRUE;
270 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200271 if (*p == ',')
272 ++p;
273 else
274 mustend = TRUE;
275 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200276 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200277 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200278 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200279
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200280 if (*p != endchar)
281 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100282 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200283
284 *argp = p;
285 return OK;
286
287err_ret:
288 if (newargs != NULL)
289 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200290 if (default_args != NULL)
291 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200292 return FAIL;
293}
294
295/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200296 * Register function "fp" as using "current_funccal" as its scope.
297 */
298 static int
299register_closure(ufunc_T *fp)
300{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200301 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100302 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200303 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200304 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200305 fp->uf_scoped = current_funccal;
306 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200307
Bram Moolenaar58016442016-07-31 18:30:22 +0200308 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
309 return FAIL;
310 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
311 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200312 return OK;
313}
314
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100315 static void
316set_ufunc_name(ufunc_T *fp, char_u *name)
317{
318 STRCPY(fp->uf_name, name);
319
320 if (name[0] == K_SPECIAL)
321 {
322 fp->uf_name_exp = alloc(STRLEN(name) + 3);
323 if (fp->uf_name_exp != NULL)
324 {
325 STRCPY(fp->uf_name_exp, "<SNR>");
326 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
327 }
328 }
329}
330
Bram Moolenaar58016442016-07-31 18:30:22 +0200331/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200332 * Get a name for a lambda. Returned in static memory.
333 */
334 char_u *
335get_lambda_name(void)
336{
337 static char_u name[30];
338 static int lambda_no = 0;
339
340 sprintf((char*)name, "<lambda>%d", ++lambda_no);
341 return name;
342}
343
344/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200345 * Parse a lambda expression and get a Funcref from "*arg".
346 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
347 */
348 int
349get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
350{
351 garray_T newargs;
352 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200353 garray_T *pnewargs;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200354 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100355 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200356 int varargs;
357 int ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200358 char_u *start = skipwhite(*arg + 1);
359 char_u *s, *e;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200360 int *old_eval_lavars = eval_lavars_used;
361 int eval_lavars = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200362
363 ga_init(&newargs);
364 ga_init(&newlines);
365
Bram Moolenaare38eab22019-12-05 21:50:01 +0100366 // First, check if this is a lambda expression. "->" must exist.
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200367 ret = get_function_args(&start, '-', NULL, NULL, NULL, NULL, TRUE,
368 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200369 if (ret == FAIL || *start != '>')
370 return NOTDONE;
371
Bram Moolenaare38eab22019-12-05 21:50:01 +0100372 // Parse the arguments again.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200373 if (evaluate)
374 pnewargs = &newargs;
375 else
376 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200377 *arg = skipwhite(*arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100378 // TODO: argument types
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200379 ret = get_function_args(arg, '-', pnewargs, NULL, &varargs, NULL, FALSE,
380 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200381 if (ret == FAIL || **arg != '>')
382 goto errret;
383
Bram Moolenaare38eab22019-12-05 21:50:01 +0100384 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200385 if (evaluate)
386 eval_lavars_used = &eval_lavars;
387
Bram Moolenaare38eab22019-12-05 21:50:01 +0100388 // Get the start and the end of the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200389 *arg = skipwhite(*arg + 1);
390 s = *arg;
391 ret = skip_expr(arg);
392 if (ret == FAIL)
393 goto errret;
394 e = *arg;
395 *arg = skipwhite(*arg);
396 if (**arg != '}')
Bram Moolenaaree619e52020-03-28 21:38:06 +0100397 {
398 semsg(_("E451: Expected }: %s"), *arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200399 goto errret;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100400 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200401 ++*arg;
402
403 if (evaluate)
404 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200405 int len, flags = 0;
406 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200407 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200408
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200409 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200410 if (fp == NULL)
411 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200412 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200413 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200414 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200415 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200416
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200417 ga_init2(&newlines, (int)sizeof(char_u *), 1);
418 if (ga_grow(&newlines, 1) == FAIL)
419 goto errret;
420
Bram Moolenaare38eab22019-12-05 21:50:01 +0100421 // Add "return " before the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200422 len = 7 + e - s + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200423 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200424 if (p == NULL)
425 goto errret;
426 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
427 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200428 vim_strncpy(p + 7, s, e - s);
Bram Moolenaarf10806b2020-04-02 18:34:35 +0200429 if (strstr((char *)p + 7, "a:") == NULL)
430 // No a: variables are used for sure.
431 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200432
433 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100434 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200435 hash_add(&func_hashtab, UF2HIKEY(fp));
436 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200437 ga_init(&fp->uf_def_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200438 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200439 if (current_funccal != NULL && eval_lavars)
440 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200441 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200442 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200443 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200444 }
445 else
446 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200447
448#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200449 if (prof_def_func())
450 func_do_profile(fp);
451#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200452 if (sandbox)
453 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100454 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200455 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200456 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200457 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200458 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100459 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200460
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200461 pt->pt_func = fp;
462 pt->pt_refcount = 1;
463 rettv->vval.v_partial = pt;
464 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200465 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200466
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200467 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200468 return OK;
469
470errret:
471 ga_clear_strings(&newargs);
472 ga_clear_strings(&newlines);
473 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100474 vim_free(pt);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200475 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200476 return FAIL;
477}
478
479/*
480 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
481 * name it contains, otherwise return "name".
482 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
483 * "partialp".
484 */
485 char_u *
486deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
487{
488 dictitem_T *v;
489 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200490 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200491
492 if (partialp != NULL)
493 *partialp = NULL;
494
495 cc = name[*lenp];
496 name[*lenp] = NUL;
497 v = find_var(name, NULL, no_autoload);
498 name[*lenp] = cc;
499 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
500 {
501 if (v->di_tv.vval.v_string == NULL)
502 {
503 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100504 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200505 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200506 s = v->di_tv.vval.v_string;
507 *lenp = (int)STRLEN(s);
508 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200509 }
510
511 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
512 {
513 partial_T *pt = v->di_tv.vval.v_partial;
514
515 if (pt == NULL)
516 {
517 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100518 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200519 }
520 if (partialp != NULL)
521 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200522 s = partial_name(pt);
523 *lenp = (int)STRLEN(s);
524 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200525 }
526
527 return name;
528}
529
530/*
531 * Give an error message with a function name. Handle <SNR> things.
532 * "ermsg" is to be passed without translation, use N_() instead of _().
533 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100534 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200535emsg_funcname(char *ermsg, char_u *name)
536{
537 char_u *p;
538
539 if (*name == K_SPECIAL)
540 p = concat_str((char_u *)"<SNR>", name + 3);
541 else
542 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100543 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200544 if (p != name)
545 vim_free(p);
546}
547
548/*
549 * Allocate a variable for the result of a function.
550 * Return OK or FAIL.
551 */
552 int
553get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200554 char_u *name, // name of the function
555 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200556 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200557 char_u **arg, // argument, pointing to the '('
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200558 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200559{
560 char_u *argp;
561 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100562 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
563 int argcount = 0; // number of arguments found
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200564 evalarg_T evalarg;
565
566 CLEAR_FIELD(evalarg);
567 evalarg.eval_flags = funcexe->evaluate ? EVAL_EVALUATE : 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200568
569 /*
570 * Get the arguments.
571 */
572 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200573 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
574 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200575 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100576 argp = skipwhite(argp + 1); // skip the '(' or ','
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200577 if (*argp == ')' || *argp == ',' || *argp == NUL)
578 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200579 if (eval1(&argp, &argvars[argcount], &evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200580 {
581 ret = FAIL;
582 break;
583 }
584 ++argcount;
585 if (*argp != ',')
586 break;
587 }
588 if (*argp == ')')
589 ++argp;
590 else
591 ret = FAIL;
592
593 if (ret == OK)
594 {
595 int i = 0;
596
597 if (get_vim_var_nr(VV_TESTING))
598 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100599 // Prepare for calling test_garbagecollect_now(), need to know
600 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200601 if (funcargs.ga_itemsize == 0)
602 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
603 for (i = 0; i < argcount; ++i)
604 if (ga_grow(&funcargs, 1) == OK)
605 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
606 &argvars[i];
607 }
608
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200609 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200610
611 funcargs.ga_len -= i;
612 }
613 else if (!aborting())
614 {
615 if (argcount == MAX_FUNC_ARGS)
616 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
617 else
618 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
619 }
620
621 while (--argcount >= 0)
622 clear_tv(&argvars[argcount]);
623
624 *arg = skipwhite(argp);
625 return ret;
626}
627
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200628/*
629 * Return TRUE if "p" starts with "<SID>" or "s:".
630 * Only works if eval_fname_script() returned non-zero for "p"!
631 */
632 static int
633eval_fname_sid(char_u *p)
634{
635 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
636}
637
638/*
639 * In a script change <SID>name() and s:name() to K_SNR 123_name().
640 * Change <SNR>123_name() to K_SNR 123_name().
641 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
642 * (slow).
643 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100644 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200645fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
646{
647 int llen;
648 char_u *fname;
649 int i;
650
651 llen = eval_fname_script(name);
652 if (llen > 0)
653 {
654 fname_buf[0] = K_SPECIAL;
655 fname_buf[1] = KS_EXTRA;
656 fname_buf[2] = (int)KE_SNR;
657 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100658 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200659 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200660 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100661 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200662 else
663 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200664 sprintf((char *)fname_buf + 3, "%ld_",
665 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200666 i = (int)STRLEN(fname_buf);
667 }
668 }
669 if (i + STRLEN(name + llen) < FLEN_FIXED)
670 {
671 STRCPY(fname_buf + i, name + llen);
672 fname = fname_buf;
673 }
674 else
675 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200676 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200677 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100678 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200679 else
680 {
681 *tofree = fname;
682 mch_memmove(fname, fname_buf, (size_t)i);
683 STRCPY(fname + i, name + llen);
684 }
685 }
686 }
687 else
688 fname = name;
689 return fname;
690}
691
692/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100693 * Find a function "name" in script "sid".
694 */
695 static ufunc_T *
696find_func_with_sid(char_u *name, int sid)
697{
698 hashitem_T *hi;
699 char_u buffer[200];
700
701 buffer[0] = K_SPECIAL;
702 buffer[1] = KS_EXTRA;
703 buffer[2] = (int)KE_SNR;
704 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
705 (long)sid, name);
706 hi = hash_find(&func_hashtab, buffer);
707 if (!HASHITEM_EMPTY(hi))
708 return HI2UF(hi);
709
710 return NULL;
711}
712
713/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200714 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200715 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200716 * Return NULL for unknown function.
717 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718 static ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200719find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200720{
721 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 ufunc_T *func;
723 imported_T *imported;
724
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200725 if (!is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100726 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200727 char_u *after_script = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200729 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100730 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200731 // Find script-local function before global one.
732 func = find_func_with_sid(name, current_sctx.sc_sid);
733 if (func != NULL)
734 return func;
735 }
736
737 if (!in_vim9script()
738 && name[0] == K_SPECIAL
739 && name[1] == KS_EXTRA
740 && name[2] == KE_SNR)
741 {
742 long sid;
743
744 // Caller changes s: to <SNR>99_name.
745
746 after_script = name + 3;
747 sid = getdigits(&after_script);
748 if (sid == current_sctx.sc_sid && *after_script == '_')
749 ++after_script;
750 else
751 after_script = NULL;
752 }
753 if (in_vim9script() || after_script != NULL)
754 {
755 // Find imported function before global one.
756 imported = find_imported(
757 after_script == NULL ? name : after_script, 0, cctx);
758 if (imported != NULL && imported->imp_funcname != NULL)
759 {
760 hi = hash_find(&func_hashtab, imported->imp_funcname);
761 if (!HASHITEM_EMPTY(hi))
762 return HI2UF(hi);
763 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 }
765 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200766
Bram Moolenaara26b9702020-04-18 19:53:28 +0200767 hi = hash_find(&func_hashtab,
768 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200769 if (!HASHITEM_EMPTY(hi))
770 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100771
772 return NULL;
773}
774
775/*
776 * Find a function by name, return pointer to it in ufuncs.
777 * "cctx" is passed in a :def function to find imported functions.
778 * Return NULL for unknown or dead function.
779 */
780 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200781find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100782{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200783 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784
785 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
786 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200787 return NULL;
788}
789
790/*
791 * Copy the function name of "fp" to buffer "buf".
792 * "buf" must be able to hold the function name plus three bytes.
793 * Takes care of script-local function names.
794 */
795 static void
796cat_func_name(char_u *buf, ufunc_T *fp)
797{
798 if (fp->uf_name[0] == K_SPECIAL)
799 {
800 STRCPY(buf, "<SNR>");
801 STRCAT(buf, fp->uf_name + 3);
802 }
803 else
804 STRCPY(buf, fp->uf_name);
805}
806
807/*
808 * Add a number variable "name" to dict "dp" with value "nr".
809 */
810 static void
811add_nr_var(
812 dict_T *dp,
813 dictitem_T *v,
814 char *name,
815 varnumber_T nr)
816{
817 STRCPY(v->di_key, name);
818 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
819 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
820 v->di_tv.v_type = VAR_NUMBER;
821 v->di_tv.v_lock = VAR_FIXED;
822 v->di_tv.vval.v_number = nr;
823}
824
825/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100826 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200827 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100828 static void
829free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200830{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100831 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200832
833 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
834 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100835 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200836
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100837 // When garbage collecting a funccall_T may be freed before the
838 // function that references it, clear its uf_scoped field.
839 // The function may have been redefined and point to another
840 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200841 if (fp != NULL && fp->uf_scoped == fc)
842 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200843 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200844 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200845
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200846 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200847 vim_free(fc);
848}
849
850/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100851 * Free "fc" and what it contains.
852 * Can be called only when "fc" is kept beyond the period of it called,
853 * i.e. after cleanup_function_call(fc).
854 */
855 static void
856free_funccal_contents(funccall_T *fc)
857{
858 listitem_T *li;
859
860 // Free all l: variables.
861 vars_clear(&fc->l_vars.dv_hashtab);
862
863 // Free all a: variables.
864 vars_clear(&fc->l_avars.dv_hashtab);
865
866 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200867 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100868 clear_tv(&li->li_tv);
869
870 free_funccal(fc);
871}
872
873/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200874 * Handle the last part of returning from a function: free the local hashtable.
875 * Unless it is still in use by a closure.
876 */
877 static void
878cleanup_function_call(funccall_T *fc)
879{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100880 int may_free_fc = fc->fc_refcount <= 0;
881 int free_fc = TRUE;
882
Bram Moolenaar6914c642017-04-01 21:21:30 +0200883 current_funccal = fc->caller;
884
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100885 // Free all l: variables if not referred.
886 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
887 vars_clear(&fc->l_vars.dv_hashtab);
888 else
889 free_fc = FALSE;
890
891 // If the a:000 list and the l: and a: dicts are not referenced and
892 // there is no closure using it, we can free the funccall_T and what's
893 // in it.
894 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
895 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200896 else
897 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100898 int todo;
899 hashitem_T *hi;
900 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200901
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100902 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200903
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100904 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200905 todo = (int)fc->l_avars.dv_hashtab.ht_used;
906 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
907 {
908 if (!HASHITEM_EMPTY(hi))
909 {
910 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100911 di = HI2DI(hi);
912 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200913 }
914 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100915 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200916
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100917 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
918 fc->l_varlist.lv_first = NULL;
919 else
920 {
921 listitem_T *li;
922
923 free_fc = FALSE;
924
925 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200926 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +0200927 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100928 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100929
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100930 if (free_fc)
931 free_funccal(fc);
932 else
933 {
934 static int made_copy = 0;
935
936 // "fc" is still in use. This can happen when returning "a:000",
937 // assigning "l:" to a global variable or defining a closure.
938 // Link "fc" in the list for garbage collection later.
939 fc->caller = previous_funccal;
940 previous_funccal = fc;
941
942 if (want_garbage_collect)
943 // If garbage collector is ready, clear count.
944 made_copy = 0;
945 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100946 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100947 // We have made a lot of copies, worth 4 Mbyte. This can happen
948 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100949 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100950 // too much memory.
951 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100952 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100953 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200954 }
955}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956/*
957 * Unreference "fc": decrement the reference count and free it when it
958 * becomes zero. "fp" is detached from "fc".
959 * When "force" is TRUE we are exiting.
960 */
961 static void
962funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
963{
964 funccall_T **pfc;
965 int i;
966
967 if (fc == NULL)
968 return;
969
970 if (--fc->fc_refcount <= 0 && (force || (
971 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
972 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
973 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
974 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
975 {
976 if (fc == *pfc)
977 {
978 *pfc = fc->caller;
979 free_funccal_contents(fc);
980 return;
981 }
982 }
983 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
984 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
985 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
986}
987
988/*
989 * Remove the function from the function hashtable. If the function was
990 * deleted while it still has references this was already done.
991 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
992 */
993 static int
994func_remove(ufunc_T *fp)
995{
996 hashitem_T *hi;
997
998 // Return if it was already virtually deleted.
999 if (fp->uf_flags & FC_DEAD)
1000 return FALSE;
1001
1002 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1003 if (!HASHITEM_EMPTY(hi))
1004 {
1005 // When there is a def-function index do not actually remove the
1006 // function, so we can find the index when defining the function again.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001007 if (fp->uf_def_status == UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008 fp->uf_flags |= FC_DEAD;
1009 else
1010 hash_remove(&func_hashtab, hi);
1011 return TRUE;
1012 }
1013 return FALSE;
1014}
1015
1016 static void
1017func_clear_items(ufunc_T *fp)
1018{
1019 ga_clear_strings(&(fp->uf_args));
1020 ga_clear_strings(&(fp->uf_def_args));
1021 ga_clear_strings(&(fp->uf_lines));
1022 VIM_CLEAR(fp->uf_name_exp);
1023 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001024 VIM_CLEAR(fp->uf_def_arg_idx);
1025 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001026 while (fp->uf_type_list.ga_len > 0)
1027 vim_free(((type_T **)fp->uf_type_list.ga_data)
1028 [--fp->uf_type_list.ga_len]);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029 ga_clear(&fp->uf_type_list);
1030#ifdef FEAT_PROFILE
1031 VIM_CLEAR(fp->uf_tml_count);
1032 VIM_CLEAR(fp->uf_tml_total);
1033 VIM_CLEAR(fp->uf_tml_self);
1034#endif
1035}
1036
1037/*
1038 * Free all things that a function contains. Does not free the function
1039 * itself, use func_free() for that.
1040 * When "force" is TRUE we are exiting.
1041 */
1042 static void
1043func_clear(ufunc_T *fp, int force)
1044{
1045 if (fp->uf_cleared)
1046 return;
1047 fp->uf_cleared = TRUE;
1048
1049 // clear this function
1050 func_clear_items(fp);
1051 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001052 clear_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053}
1054
1055/*
1056 * Free a function and remove it from the list of functions. Does not free
1057 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001058 * When "force" is TRUE we are exiting.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 */
1060 static void
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001061func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001062{
1063 // Only remove it when not done already, otherwise we would remove a newer
1064 // version of the function with the same name.
1065 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1066 func_remove(fp);
1067
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001068 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001069 vim_free(fp);
1070}
1071
1072/*
1073 * Free all things that a function contains and free the function itself.
1074 * When "force" is TRUE we are exiting.
1075 */
1076 static void
1077func_clear_free(ufunc_T *fp, int force)
1078{
1079 func_clear(fp, force);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001080 if (force || fp->uf_dfunc_idx == 0)
1081 func_free(fp, force);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001082}
1083
Bram Moolenaar6914c642017-04-01 21:21:30 +02001084
1085/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001086 * Call a user function.
1087 */
1088 static void
1089call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001090 ufunc_T *fp, // pointer to function
1091 int argcount, // nr of args
1092 typval_T *argvars, // arguments
1093 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001094 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01001095 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001096{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001097 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001098 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001099 funccall_T *fc;
1100 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001101 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001102 static int depth = 0;
1103 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001104 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001105 int i;
1106 int ai;
1107 int islambda = FALSE;
1108 char_u numbuf[NUMBUFLEN];
1109 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001110#ifdef FEAT_PROFILE
1111 proftime_T wait_start;
1112 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001113 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001114#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001115 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001116
Bram Moolenaare38eab22019-12-05 21:50:01 +01001117 // If depth of calling is getting too high, don't execute the function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001118 if (depth >= p_mfd)
1119 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001120 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001121 rettv->v_type = VAR_NUMBER;
1122 rettv->vval.v_number = -1;
1123 return;
1124 }
1125 ++depth;
1126
Bram Moolenaare38eab22019-12-05 21:50:01 +01001127 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001128
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001129 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001130 if (fc == NULL)
1131 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001132 fc->caller = current_funccal;
1133 current_funccal = fc;
1134 fc->func = fp;
1135 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001136 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001137 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001138 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1139 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001140 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001141 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001142 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001143
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001144 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145 {
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001146 estack_push_ufunc(fp, 1);
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001147 save_current_sctx = current_sctx;
1148 current_sctx = fp->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001150 // Execute the function, possibly compiling it first.
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001151 call_def_function(fp, argcount, argvars, funcexe->partial, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152 --depth;
1153 current_funccal = fc->caller;
1154
1155 estack_pop();
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001156 current_sctx = save_current_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157 free_funccal(fc);
1158 return;
1159 }
1160
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001161 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1162 islambda = TRUE;
1163
1164 /*
1165 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1166 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1167 * each argument variable and saves a lot of time.
1168 */
1169 /*
1170 * Init l: variables.
1171 */
1172 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1173 if (selfdict != NULL)
1174 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001175 // Set l:self to "selfdict". Use "name" to avoid a warning from
1176 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001177 v = &fc->fixvar[fixvar_idx++].var;
1178 name = v->di_key;
1179 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001180 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001181 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1182 v->di_tv.v_type = VAR_DICT;
1183 v->di_tv.v_lock = 0;
1184 v->di_tv.vval.v_dict = selfdict;
1185 ++selfdict->dv_refcount;
1186 }
1187
1188 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001189 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001190 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001191 * Set a:000 to a list with room for the "..." arguments.
1192 */
1193 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001194 if ((fp->uf_flags & FC_NOARGS) == 0)
1195 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001196 (varnumber_T)(argcount >= fp->uf_args.ga_len
1197 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001198 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001199 if ((fp->uf_flags & FC_NOARGS) == 0)
1200 {
1201 // Use "name" to avoid a warning from some compiler that checks the
1202 // destination size.
1203 v = &fc->fixvar[fixvar_idx++].var;
1204 name = v->di_key;
1205 STRCPY(name, "000");
1206 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1207 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1208 v->di_tv.v_type = VAR_LIST;
1209 v->di_tv.v_lock = VAR_FIXED;
1210 v->di_tv.vval.v_list = &fc->l_varlist;
1211 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001212 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001213 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1214 fc->l_varlist.lv_lock = VAR_FIXED;
1215
1216 /*
1217 * Set a:firstline to "firstline" and a:lastline to "lastline".
1218 * Set a:name to named arguments.
1219 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001220 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001221 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001222 if ((fp->uf_flags & FC_NOARGS) == 0)
1223 {
1224 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001225 (varnumber_T)funcexe->firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001226 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001227 (varnumber_T)funcexe->lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001228 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001229 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001230 {
1231 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001232 typval_T def_rettv;
1233 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001234
1235 ai = i - fp->uf_args.ga_len;
1236 if (ai < 0)
1237 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001238 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001239 name = FUNCARG(fp, i);
1240 if (islambda)
1241 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001242
1243 // evaluate named argument default expression
1244 isdefault = ai + fp->uf_def_args.ga_len >= 0
1245 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1246 && argvars[i].vval.v_number == VVAL_NONE));
1247 if (isdefault)
1248 {
1249 char_u *default_expr = NULL;
1250 def_rettv.v_type = VAR_NUMBER;
1251 def_rettv.vval.v_number = -1;
1252
1253 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1254 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001255 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001256 {
1257 default_arg_err = 1;
1258 break;
1259 }
1260 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001261 }
1262 else
1263 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001264 if ((fp->uf_flags & FC_NOARGS) != 0)
1265 // Bail out if no a: arguments used (in lambda).
1266 break;
1267
Bram Moolenaare38eab22019-12-05 21:50:01 +01001268 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001269 sprintf((char *)numbuf, "%d", ai + 1);
1270 name = numbuf;
1271 }
1272 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1273 {
1274 v = &fc->fixvar[fixvar_idx++].var;
1275 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001276 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001277 }
1278 else
1279 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001280 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001281 if (v == NULL)
1282 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001283 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001284 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001285
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001286 // Note: the values are copied directly to avoid alloc/free.
1287 // "argvars" must have VAR_FIXED for v_lock.
1288 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001289 v->di_tv.v_lock = VAR_FIXED;
1290
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001291 if (addlocal)
1292 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001293 // Named arguments should be accessed without the "a:" prefix in
1294 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001295 copy_tv(&v->di_tv, &v->di_tv);
1296 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001297 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001298 else
1299 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001300
1301 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1302 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001303 listitem_T *li = &fc->l_listitems[ai];
1304
1305 li->li_tv = argvars[i];
1306 li->li_tv.v_lock = VAR_FIXED;
1307 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001308 }
1309 }
1310
Bram Moolenaare38eab22019-12-05 21:50:01 +01001311 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001312 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001313
1314 if (fp->uf_flags & FC_SANDBOX)
1315 {
1316 using_sandbox = TRUE;
1317 ++sandbox;
1318 }
1319
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001320 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001321 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001322 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001323 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001324 ++no_wait_return;
1325 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001326
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001327 smsg(_("calling %s"), SOURCING_NAME);
1328 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001329 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001330 char_u buf[MSG_BUF_LEN];
1331 char_u numbuf2[NUMBUFLEN];
1332 char_u *tofree;
1333 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001334
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001335 msg_puts("(");
1336 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001337 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001338 if (i > 0)
1339 msg_puts(", ");
1340 if (argvars[i].v_type == VAR_NUMBER)
1341 msg_outnum((long)argvars[i].vval.v_number);
1342 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001343 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001344 // Do not want errors such as E724 here.
1345 ++emsg_off;
1346 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1347 --emsg_off;
1348 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001349 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001350 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001351 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001352 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1353 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001354 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001355 msg_puts((char *)s);
1356 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001357 }
1358 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001359 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001360 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001361 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001362 msg_puts("\n"); // don't overwrite this either
1363
1364 verbose_leave_scroll();
1365 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001366 }
1367#ifdef FEAT_PROFILE
1368 if (do_profiling == PROF_YES)
1369 {
1370 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001371 {
1372 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001373 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001374 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001375 if (fp->uf_profiling
1376 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1377 {
1378 ++fp->uf_tm_count;
1379 profile_start(&call_start);
1380 profile_zero(&fp->uf_tm_children);
1381 }
1382 script_prof_save(&wait_start);
1383 }
1384#endif
1385
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001386 save_current_sctx = current_sctx;
1387 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001388 save_did_emsg = did_emsg;
1389 did_emsg = FALSE;
1390
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001391 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1392 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001393 else if (islambda)
1394 {
1395 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1396
1397 // A Lambda always has the command "return {expr}". It is much faster
1398 // to evaluate {expr} directly.
1399 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001400 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001401 --ex_nesting_level;
1402 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001403 else
1404 // call do_cmdline() to execute the lines
1405 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001406 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1407
1408 --RedrawingDisabled;
1409
Bram Moolenaare38eab22019-12-05 21:50:01 +01001410 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001411 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1412 {
1413 clear_tv(rettv);
1414 rettv->v_type = VAR_NUMBER;
1415 rettv->vval.v_number = -1;
1416 }
1417
1418#ifdef FEAT_PROFILE
1419 if (do_profiling == PROF_YES && (fp->uf_profiling
1420 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1421 {
1422 profile_end(&call_start);
1423 profile_sub_wait(&wait_start, &call_start);
1424 profile_add(&fp->uf_tm_total, &call_start);
1425 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1426 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1427 {
1428 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1429 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1430 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001431 if (started_profiling)
1432 // make a ":profdel func" stop profiling the function
1433 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001434 }
1435#endif
1436
Bram Moolenaare38eab22019-12-05 21:50:01 +01001437 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001438 if (p_verbose >= 12)
1439 {
1440 ++no_wait_return;
1441 verbose_enter_scroll();
1442
1443 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001444 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001445 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001446 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001447 (long)fc->rettv->vval.v_number);
1448 else
1449 {
1450 char_u buf[MSG_BUF_LEN];
1451 char_u numbuf2[NUMBUFLEN];
1452 char_u *tofree;
1453 char_u *s;
1454
Bram Moolenaare38eab22019-12-05 21:50:01 +01001455 // The value may be very long. Skip the middle part, so that we
1456 // have some idea how it starts and ends. smsg() would always
1457 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001458 ++emsg_off;
1459 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1460 --emsg_off;
1461 if (s != NULL)
1462 {
1463 if (vim_strsize(s) > MSG_BUF_CLEN)
1464 {
1465 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1466 s = buf;
1467 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001468 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001469 vim_free(tofree);
1470 }
1471 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001472 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001473
1474 verbose_leave_scroll();
1475 --no_wait_return;
1476 }
1477
Bram Moolenaare31ee862020-01-07 20:59:34 +01001478 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001479 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001480 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001481#ifdef FEAT_PROFILE
1482 if (do_profiling == PROF_YES)
1483 script_prof_restore(&wait_start);
1484#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001485 if (using_sandbox)
1486 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001487
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001488 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001489 {
1490 ++no_wait_return;
1491 verbose_enter_scroll();
1492
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001493 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001494 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001495
1496 verbose_leave_scroll();
1497 --no_wait_return;
1498 }
1499
1500 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001501 --depth;
1502
Bram Moolenaar6914c642017-04-01 21:21:30 +02001503 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001504}
1505
1506/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001508 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509 int
1510call_user_func_check(
1511 ufunc_T *fp,
1512 int argcount,
1513 typval_T *argvars,
1514 typval_T *rettv,
1515 funcexe_T *funcexe,
1516 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001517{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 int error;
1519 int regular_args = fp->uf_args.ga_len;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001520
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1522 *funcexe->doesrange = TRUE;
1523 if (argcount < regular_args - fp->uf_def_args.ga_len)
1524 error = FCERR_TOOFEW;
1525 else if (!has_varargs(fp) && argcount > regular_args)
1526 error = FCERR_TOOMANY;
1527 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1528 error = FCERR_DICT;
1529 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001530 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001531 int did_save_redo = FALSE;
1532 save_redo_T save_redo;
1533
1534 /*
1535 * Call the user function.
1536 * Save and restore search patterns, script variables and
1537 * redo buffer.
1538 */
1539 save_search_patterns();
1540 if (!ins_compl_active())
1541 {
1542 saveRedobuff(&save_redo);
1543 did_save_redo = TRUE;
1544 }
1545 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001546 call_user_func(fp, argcount, argvars, rettv, funcexe,
1547 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1549 // Function was unreferenced while being used, free it now.
1550 func_clear_free(fp, FALSE);
1551 if (did_save_redo)
1552 restoreRedobuff(&save_redo);
1553 restore_search_patterns();
1554 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001555 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001557}
1558
1559/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001560 * There are two kinds of function names:
1561 * 1. ordinary names, function defined with :function
1562 * 2. numbered functions and lambdas
1563 * For the first we only count the name stored in func_hashtab as a reference,
1564 * using function() does not count as a reference, because the function is
1565 * looked up by name.
1566 */
1567 static int
1568func_name_refcount(char_u *name)
1569{
1570 return isdigit(*name) || *name == '<';
1571}
1572
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001573static funccal_entry_T *funccal_stack = NULL;
1574
1575/*
1576 * Save the current function call pointer, and set it to NULL.
1577 * Used when executing autocommands and for ":source".
1578 */
1579 void
1580save_funccal(funccal_entry_T *entry)
1581{
1582 entry->top_funccal = current_funccal;
1583 entry->next = funccal_stack;
1584 funccal_stack = entry;
1585 current_funccal = NULL;
1586}
1587
1588 void
1589restore_funccal(void)
1590{
1591 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001592 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001593 else
1594 {
1595 current_funccal = funccal_stack->top_funccal;
1596 funccal_stack = funccal_stack->next;
1597 }
1598}
1599
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001600 funccall_T *
1601get_current_funccal(void)
1602{
1603 return current_funccal;
1604}
1605
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001606/*
1607 * Mark all functions of script "sid" as deleted.
1608 */
1609 void
1610delete_script_functions(int sid)
1611{
1612 hashitem_T *hi;
1613 ufunc_T *fp;
1614 long_u todo;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02001615 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001616 size_t len;
1617
1618 buf[0] = K_SPECIAL;
1619 buf[1] = KS_EXTRA;
1620 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02001621 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001622 len = STRLEN(buf);
1623
1624 todo = func_hashtab.ht_used;
1625 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1626 if (!HASHITEM_EMPTY(hi))
1627 {
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001628 fp = HI2UF(hi);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001629 if (STRNCMP(fp->uf_name, buf, len) == 0)
1630 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001631 fp->uf_flags |= FC_DEAD;
1632 func_clear(fp, TRUE);
1633 }
1634 --todo;
1635 }
1636}
1637
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001638#if defined(EXITFREE) || defined(PROTO)
1639 void
1640free_all_functions(void)
1641{
1642 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001643 ufunc_T *fp;
1644 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001645 long_u todo = 1;
1646 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001647
Bram Moolenaare38eab22019-12-05 21:50:01 +01001648 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001649 while (current_funccal != NULL)
1650 {
1651 clear_tv(current_funccal->rettv);
1652 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001653 if (current_funccal == NULL && funccal_stack != NULL)
1654 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001655 }
1656
Bram Moolenaare38eab22019-12-05 21:50:01 +01001657 // First clear what the functions contain. Since this may lower the
1658 // reference count of a function, it may also free a function and change
1659 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001660 while (todo > 0)
1661 {
1662 todo = func_hashtab.ht_used;
1663 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1664 if (!HASHITEM_EMPTY(hi))
1665 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666 // clear the def function index now
1667 fp = HI2UF(hi);
1668 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001669 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670
Bram Moolenaare38eab22019-12-05 21:50:01 +01001671 // Only free functions that are not refcounted, those are
1672 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001673 if (func_name_refcount(fp->uf_name))
1674 ++skipped;
1675 else
1676 {
1677 used = func_hashtab.ht_used;
1678 func_clear(fp, TRUE);
1679 if (used != func_hashtab.ht_used)
1680 {
1681 skipped = 0;
1682 break;
1683 }
1684 }
1685 --todo;
1686 }
1687 }
1688
Bram Moolenaare38eab22019-12-05 21:50:01 +01001689 // Now actually free the functions. Need to start all over every time,
1690 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001691 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001692 while (func_hashtab.ht_used > skipped)
1693 {
1694 todo = func_hashtab.ht_used;
1695 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001696 if (!HASHITEM_EMPTY(hi))
1697 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001698 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001699 // Only free functions that are not refcounted, those are
1700 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02001701 fp = HI2UF(hi);
1702 if (func_name_refcount(fp->uf_name))
1703 ++skipped;
1704 else
1705 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001706 func_free(fp, FALSE);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001707 skipped = 0;
1708 break;
1709 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001710 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001711 }
1712 if (skipped == 0)
1713 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714
1715 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001716}
1717#endif
1718
1719/*
1720 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02001721 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001722 * "len" is the length of "name", or -1 for NUL terminated.
1723 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001725builtin_function(char_u *name, int len)
1726{
1727 char_u *p;
1728
Bram Moolenaara26b9702020-04-18 19:53:28 +02001729 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001730 return FALSE;
1731 p = vim_strchr(name, AUTOLOAD_CHAR);
1732 return p == NULL || (len > 0 && p > name + len);
1733}
1734
1735 int
1736func_call(
1737 char_u *name,
1738 typval_T *args,
1739 partial_T *partial,
1740 dict_T *selfdict,
1741 typval_T *rettv)
1742{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001743 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001744 listitem_T *item;
1745 typval_T argv[MAX_FUNC_ARGS + 1];
1746 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001747 int r = 0;
1748
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001749 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001750 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001751 {
1752 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1753 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001754 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001755 break;
1756 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001757 // Make a copy of each argument. This is needed to be able to set
1758 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001759 copy_tv(&item->li_tv, &argv[argc++]);
1760 }
1761
1762 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001763 {
1764 funcexe_T funcexe;
1765
Bram Moolenaara80faa82020-04-12 19:37:17 +02001766 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001767 funcexe.firstline = curwin->w_cursor.lnum;
1768 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001769 funcexe.evaluate = TRUE;
1770 funcexe.partial = partial;
1771 funcexe.selfdict = selfdict;
1772 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1773 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001774
Bram Moolenaare38eab22019-12-05 21:50:01 +01001775 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001776 while (argc > 0)
1777 clear_tv(&argv[--argc]);
1778
1779 return r;
1780}
1781
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001782static int callback_depth = 0;
1783
1784 int
1785get_callback_depth(void)
1786{
1787 return callback_depth;
1788}
1789
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001790/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001791 * Invoke call_func() with a callback.
1792 */
1793 int
1794call_callback(
1795 callback_T *callback,
1796 int len, // length of "name" or -1 to use strlen()
1797 typval_T *rettv, // return value goes here
1798 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001799 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001800 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001801{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001802 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001803 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001804
Bram Moolenaara80faa82020-04-12 19:37:17 +02001805 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001806 funcexe.evaluate = TRUE;
1807 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001808 ++callback_depth;
1809 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1810 --callback_depth;
1811 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001812}
1813
1814/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 * Give an error message for the result of a function.
1816 * Nothing if "error" is FCERR_NONE.
1817 */
1818 void
1819user_func_error(int error, char_u *name)
1820{
1821 switch (error)
1822 {
1823 case FCERR_UNKNOWN:
1824 emsg_funcname(e_unknownfunc, name);
1825 break;
1826 case FCERR_NOTMETHOD:
1827 emsg_funcname(
1828 N_("E276: Cannot use function as a method: %s"), name);
1829 break;
1830 case FCERR_DELETED:
1831 emsg_funcname(N_(e_func_deleted), name);
1832 break;
1833 case FCERR_TOOMANY:
1834 emsg_funcname((char *)e_toomanyarg, name);
1835 break;
1836 case FCERR_TOOFEW:
1837 emsg_funcname((char *)e_toofewarg, name);
1838 break;
1839 case FCERR_SCRIPT:
1840 emsg_funcname(
1841 N_("E120: Using <SID> not in a script context: %s"), name);
1842 break;
1843 case FCERR_DICT:
1844 emsg_funcname(
1845 N_("E725: Calling dict function without Dictionary: %s"),
1846 name);
1847 break;
1848 }
1849}
1850
1851/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001852 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001853 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001854 * Return FAIL when the function can't be called, OK otherwise.
1855 * Also returns OK when an error was encountered while executing the function.
1856 */
1857 int
1858call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001859 char_u *funcname, // name of the function
1860 int len, // length of "name" or -1 to use strlen()
1861 typval_T *rettv, // return value goes here
1862 int argcount_in, // number of "argvars"
1863 typval_T *argvars_in, // vars for arguments, must have "argcount"
1864 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001865 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001866{
1867 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01001868 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001869 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001870 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001871 char_u fname_buf[FLEN_FIXED + 1];
1872 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001873 char_u *fname = NULL;
1874 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001875 int argcount = argcount_in;
1876 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001877 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001878 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1879 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001880 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001881 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001882 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001883
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001884 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1885 // even when call_func() returns FAIL.
1886 rettv->v_type = VAR_UNKNOWN;
1887
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001888 if (partial != NULL)
1889 fp = partial->pt_func;
1890 if (fp == NULL)
1891 {
1892 // Make a copy of the name, if it comes from a funcref variable it
1893 // could be changed or deleted in the called function.
1894 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
1895 if (name == NULL)
1896 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001897
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001898 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1899 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001900
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001901 if (funcexe->doesrange != NULL)
1902 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001903
1904 if (partial != NULL)
1905 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001906 // When the function has a partial with a dict and there is a dict
1907 // argument, use the dict argument. That is backwards compatible.
1908 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001909 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001910 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01001911 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001912 {
1913 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001914 {
1915 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
1916 {
Bram Moolenaaref140542019-12-31 21:27:13 +01001917 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001918 goto theend;
1919 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001920 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001921 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001922 for (i = 0; i < argcount_in; ++i)
1923 argv[i + argv_clear] = argvars_in[i];
1924 argvars = argv;
1925 argcount = partial->pt_argc + argcount_in;
1926 }
1927 }
1928
Bram Moolenaaref140542019-12-31 21:27:13 +01001929 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001930 {
1931 char_u *rfname = fname;
1932
Bram Moolenaare38eab22019-12-05 21:50:01 +01001933 // Ignore "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001934 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001935 rfname = fname + 2;
1936
Bram Moolenaare38eab22019-12-05 21:50:01 +01001937 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001938 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01001939 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001940
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001941 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001942 {
1943 /*
1944 * User defined function.
1945 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001946 if (fp == NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001947 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001948
Bram Moolenaare38eab22019-12-05 21:50:01 +01001949 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001950 if (fp == NULL
1951 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001952 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001953 && !aborting())
1954 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001955 // executed an autocommand, search for the function again
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001956 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001957 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001958 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001959 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1960 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001961 // loaded a package, search for the function again
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001962 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001963 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02001964 if (fp == NULL)
1965 {
1966 char_u *p = untrans_function_name(rfname);
1967
1968 // If using Vim9 script try not local to the script.
1969 // TODO: should not do this if the name started with "s:".
1970 if (p != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001971 fp = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02001972 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001973
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001974 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01001975 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001976 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001977 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001978 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01001979 // postponed filling in the arguments, do it now
1980 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001981 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001982
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001983 if (funcexe->basetv != NULL)
1984 {
1985 // Method call: base->Method()
1986 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
1987 argv[0] = *funcexe->basetv;
1988 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001989 argvars = argv;
1990 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001991 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 error = call_user_func_check(fp, argcount, argvars, rettv,
1994 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001995 }
1996 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001997 else if (funcexe->basetv != NULL)
1998 {
1999 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002000 * expr->method(): Find the method name in the table, call its
2001 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02002002 */
2003 error = call_internal_method(fname, argcount, argvars, rettv,
2004 funcexe->basetv);
2005 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002006 else
2007 {
2008 /*
2009 * Find the function name in the table, call its implementation.
2010 */
2011 error = call_internal_func(fname, argcount, argvars, rettv);
2012 }
2013 /*
2014 * The function call (or "FuncUndefined" autocommand sequence) might
2015 * have been aborted by an error, an interrupt, or an explicitly thrown
2016 * exception that has not been caught so far. This situation can be
2017 * tested for by calling aborting(). For an error in an internal
2018 * function or for the "E132" error in call_user_func(), however, the
2019 * throw point at which the "force_abort" flag (temporarily reset by
2020 * emsg()) is normally updated has not been reached yet. We need to
2021 * update that flag first to make aborting() reliable.
2022 */
2023 update_force_abort();
2024 }
Bram Moolenaaref140542019-12-31 21:27:13 +01002025 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002026 ret = OK;
2027
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002028theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002029 /*
2030 * Report an error unless the argument evaluation or function call has been
2031 * cancelled due to an aborting error, an interrupt, or an exception.
2032 */
2033 if (!aborting())
2034 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002035 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002036 }
2037
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002038 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002039 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002040 clear_tv(&argv[--argv_clear + argv_base]);
2041
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002042 vim_free(tofree);
2043 vim_free(name);
2044
2045 return ret;
2046}
2047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 static char_u *
2049printable_func_name(ufunc_T *fp)
2050{
2051 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
2052}
2053
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002054/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002055 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002056 */
2057 static void
2058list_func_head(ufunc_T *fp, int indent)
2059{
2060 int j;
2061
2062 msg_start();
2063 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002064 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002065 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002066 msg_puts("def ");
2067 else
2068 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002070 msg_putchar('(');
2071 for (j = 0; j < fp->uf_args.ga_len; ++j)
2072 {
2073 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002074 msg_puts(", ");
2075 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076 if (fp->uf_arg_types != NULL)
2077 {
2078 char *tofree;
2079
2080 msg_puts(": ");
2081 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
2082 vim_free(tofree);
2083 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002084 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
2085 {
2086 msg_puts(" = ");
2087 msg_puts(((char **)(fp->uf_def_args.ga_data))
2088 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
2089 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002090 }
2091 if (fp->uf_varargs)
2092 {
2093 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002094 msg_puts(", ");
2095 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002096 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097 if (fp->uf_va_name != NULL)
2098 {
2099 if (j)
2100 msg_puts(", ");
2101 msg_puts("...");
2102 msg_puts((char *)fp->uf_va_name);
2103 if (fp->uf_va_type)
2104 {
2105 char *tofree;
2106
2107 msg_puts(": ");
2108 msg_puts(type_name(fp->uf_va_type, &tofree));
2109 vim_free(tofree);
2110 }
2111 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002112 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002113
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002114 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002115 {
2116 if (fp->uf_ret_type != &t_void)
2117 {
2118 char *tofree;
2119
2120 msg_puts(": ");
2121 msg_puts(type_name(fp->uf_ret_type, &tofree));
2122 vim_free(tofree);
2123 }
2124 }
2125 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002126 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002127 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002128 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002129 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002130 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002131 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002132 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002133 msg_clr_eos();
2134 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002135 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002136}
2137
2138/*
2139 * Get a function name, translating "<SID>" and "<SNR>".
2140 * Also handles a Funcref in a List or Dictionary.
2141 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002142 * Set "*is_global" to TRUE when the function must be global, unless
2143 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002144 * flags:
2145 * TFN_INT: internal function name OK
2146 * TFN_QUIET: be quiet
2147 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002148 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002149 * Advances "pp" to just after the function name (if no error).
2150 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002151 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002152trans_function_name(
2153 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002154 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002155 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002156 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002157 funcdict_T *fdp, // return: info about dictionary used
2158 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002159{
2160 char_u *name = NULL;
2161 char_u *start;
2162 char_u *end;
2163 int lead;
2164 char_u sid_buf[20];
2165 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002167 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002169
2170 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002171 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002172 start = *pp;
2173
Bram Moolenaare38eab22019-12-05 21:50:01 +01002174 // Check for hard coded <SNR>: already translated function ID (from a user
2175 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002176 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2177 && (*pp)[2] == (int)KE_SNR)
2178 {
2179 *pp += 3;
2180 len = get_id_len(pp) + 3;
2181 return vim_strnsave(start, len);
2182 }
2183
Bram Moolenaare38eab22019-12-05 21:50:01 +01002184 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2185 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002186 lead = eval_fname_script(start);
2187 if (lead > 2)
2188 start += lead;
2189
Bram Moolenaare38eab22019-12-05 21:50:01 +01002190 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002191 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002192 lead > 2 ? 0 : FNE_CHECK_START);
2193 if (end == start)
2194 {
2195 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002196 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002197 goto theend;
2198 }
2199 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2200 {
2201 /*
2202 * Report an invalid expression in braces, unless the expression
2203 * evaluation has been cancelled due to an aborting error, an
2204 * interrupt, or an exception.
2205 */
2206 if (!aborting())
2207 {
2208 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002209 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002210 }
2211 else
2212 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2213 goto theend;
2214 }
2215
2216 if (lv.ll_tv != NULL)
2217 {
2218 if (fdp != NULL)
2219 {
2220 fdp->fd_dict = lv.ll_dict;
2221 fdp->fd_newkey = lv.ll_newkey;
2222 lv.ll_newkey = NULL;
2223 fdp->fd_di = lv.ll_di;
2224 }
2225 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2226 {
2227 name = vim_strsave(lv.ll_tv->vval.v_string);
2228 *pp = end;
2229 }
2230 else if (lv.ll_tv->v_type == VAR_PARTIAL
2231 && lv.ll_tv->vval.v_partial != NULL)
2232 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002233 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002234 *pp = end;
2235 if (partial != NULL)
2236 *partial = lv.ll_tv->vval.v_partial;
2237 }
2238 else
2239 {
2240 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2241 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002242 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002243 else
2244 *pp = end;
2245 name = NULL;
2246 }
2247 goto theend;
2248 }
2249
2250 if (lv.ll_name == NULL)
2251 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002252 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002253 *pp = end;
2254 goto theend;
2255 }
2256
Bram Moolenaare38eab22019-12-05 21:50:01 +01002257 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002258 if (lv.ll_exp_name != NULL)
2259 {
2260 len = (int)STRLEN(lv.ll_exp_name);
2261 name = deref_func_name(lv.ll_exp_name, &len, partial,
2262 flags & TFN_NO_AUTOLOAD);
2263 if (name == lv.ll_exp_name)
2264 name = NULL;
2265 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002266 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002267 {
2268 len = (int)(end - *pp);
2269 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2270 if (name == *pp)
2271 name = NULL;
2272 }
2273 if (name != NULL)
2274 {
2275 name = vim_strsave(name);
2276 *pp = end;
2277 if (STRNCMP(name, "<SNR>", 5) == 0)
2278 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002279 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002280 name[0] = K_SPECIAL;
2281 name[1] = KS_EXTRA;
2282 name[2] = (int)KE_SNR;
2283 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2284 }
2285 goto theend;
2286 }
2287
2288 if (lv.ll_exp_name != NULL)
2289 {
2290 len = (int)STRLEN(lv.ll_exp_name);
2291 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2292 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2293 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002294 // When there was "s:" already or the name expanded to get a
2295 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002296 lv.ll_name += 2;
2297 len -= 2;
2298 lead = 2;
2299 }
2300 }
2301 else
2302 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002303 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002304 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002305 {
2306 if (is_global != NULL && lv.ll_name[0] == 'g')
2307 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002308 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002309 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002310 len = (int)(end - lv.ll_name);
2311 }
2312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 // In Vim9 script a user function is script-local by default.
2314 vim9script = ASCII_ISUPPER(*start)
2315 && current_sctx.sc_version == SCRIPT_VERSION_VIM9;
2316
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002317 /*
2318 * Copy the function name to allocated memory.
2319 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2320 * Accept <SNR>123_name() outside a script.
2321 */
2322 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002323 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002325 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326 if (!vim9script)
2327 lead = 3;
2328 if (vim9script || (lv.ll_exp_name != NULL
2329 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002330 || eval_fname_sid(*pp))
2331 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002333 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002334 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002335 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002336 goto theend;
2337 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002338 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339 if (vim9script)
2340 extra = 3 + (int)STRLEN(sid_buf);
2341 else
2342 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002343 }
2344 }
2345 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2346 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002347 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002348 start);
2349 goto theend;
2350 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002351 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002352 {
2353 char_u *cp = vim_strchr(lv.ll_name, ':');
2354
2355 if (cp != NULL && cp < end)
2356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002357 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002358 goto theend;
2359 }
2360 }
2361
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002363 if (name != NULL)
2364 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002365 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002366 {
2367 name[0] = K_SPECIAL;
2368 name[1] = KS_EXTRA;
2369 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002371 STRCPY(name + 3, sid_buf);
2372 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2374 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002375 }
2376 *pp = end;
2377
2378theend:
2379 clear_lval(&lv);
2380 return name;
2381}
2382
2383/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02002384 * Assuming "name" is the result of trans_function_name() and it was prefixed
2385 * to use the script-local name, return the unmodified name (points into
2386 * "name"). Otherwise return NULL.
2387 * This can be used to first search for a script-local function and fall back
2388 * to the global function if not found.
2389 */
2390 char_u *
2391untrans_function_name(char_u *name)
2392{
2393 char_u *p;
2394
2395 if (*name == K_SPECIAL && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2396 {
2397 p = vim_strchr(name, '_');
2398 if (p != NULL)
2399 return p + 1;
2400 }
2401 return NULL;
2402}
2403
2404/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002405 * List functions. When "regmatch" is NULL all of then.
2406 * Otherwise functions matching "regmatch".
2407 */
2408 static void
2409list_functions(regmatch_T *regmatch)
2410{
2411 long_u used = func_hashtab.ht_used;
2412 long_u todo = used;
2413 hashitem_T *ht_array = func_hashtab.ht_array;
2414 hashitem_T *hi;
2415
2416 for (hi = ht_array; todo > 0 && !got_int; ++hi)
2417 {
2418 if (!HASHITEM_EMPTY(hi))
2419 {
2420 ufunc_T *fp = HI2UF(hi);
2421
2422 --todo;
2423 if ((fp->uf_flags & FC_DEAD) == 0
2424 && (regmatch == NULL
2425 ? !message_filtered(fp->uf_name)
2426 && !func_name_refcount(fp->uf_name)
2427 : !isdigit(*fp->uf_name)
2428 && vim_regexec(regmatch, fp->uf_name, 0)))
2429 {
2430 list_func_head(fp, FALSE);
2431 if (used != func_hashtab.ht_used
2432 || ht_array != func_hashtab.ht_array)
2433 {
2434 emsg(_("E454: function list was modified"));
2435 return;
2436 }
2437 }
2438 }
2439 }
2440}
2441
2442/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02002443 * ":function" also supporting nested ":def".
2444 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002445 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02002446 ufunc_T *
Bram Moolenaar822ba242020-05-24 23:00:18 +02002447def_function(exarg_T *eap, char_u *name_arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002448{
2449 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002450 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002451 int j;
2452 int c;
2453 int saved_did_emsg;
2454 int saved_wait_return = need_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002455 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002456 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002457 char_u *p;
2458 char_u *arg;
2459 char_u *line_arg = NULL;
2460 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002462 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002463 garray_T newlines;
2464 int varargs = FALSE;
2465 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002467 ufunc_T *fp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002468 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002469 int indent;
2470 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471#define MAX_FUNC_NESTING 50
2472 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002473 dictitem_T *v;
2474 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002475 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002476 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002477 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002478 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002479 linenr_T sourcing_lnum_off;
2480 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002481 int is_heredoc = FALSE;
2482 char_u *skip_until = NULL;
2483 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002484
Bram Moolenaar822ba242020-05-24 23:00:18 +02002485 if (in_vim9script() && eap->forceit)
2486 {
2487 emsg(_(e_nobang));
2488 return NULL;
2489 }
2490
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002491 /*
2492 * ":function" without argument: list functions.
2493 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002494 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002495 {
2496 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002497 list_functions(NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002498 eap->nextcmd = check_nextcmd(eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02002499 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002500 }
2501
2502 /*
2503 * ":function /pat": list functions matching pattern.
2504 */
2505 if (*eap->arg == '/')
2506 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002507 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002508 if (!eap->skip)
2509 {
2510 regmatch_T regmatch;
2511
2512 c = *p;
2513 *p = NUL;
2514 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2515 *p = c;
2516 if (regmatch.regprog != NULL)
2517 {
2518 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002519 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002520 vim_regfree(regmatch.regprog);
2521 }
2522 }
2523 if (*p == '/')
2524 ++p;
2525 eap->nextcmd = check_nextcmd(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02002526 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002527 }
2528
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 ga_init(&newargs);
2530 ga_init(&argtypes);
2531 ga_init(&default_args);
2532
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002533 /*
2534 * Get the function name. There are these situations:
2535 * func normal function name
2536 * "name" == func, "fudi.fd_dict" == NULL
2537 * dict.func new dictionary entry
2538 * "name" == NULL, "fudi.fd_dict" set,
2539 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2540 * dict.func existing dict entry with a Funcref
2541 * "name" == func, "fudi.fd_dict" set,
2542 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2543 * dict.func existing dict entry that's not a Funcref
2544 * "name" == NULL, "fudi.fd_dict" set,
2545 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2546 * s:func script-local function name
2547 * g:func global function name, same as "func"
2548 */
2549 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002550 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002551 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02002552 // nested function, argument is (args).
2553 paren = TRUE;
2554 CLEAR_FIELD(fudi);
2555 }
2556 else
2557 {
2558 name = trans_function_name(&p, &is_global, eap->skip,
2559 TFN_NO_AUTOLOAD, &fudi, NULL);
2560 paren = (vim_strchr(p, '(') != NULL);
2561 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002562 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02002563 /*
2564 * Return on an invalid expression in braces, unless the expression
2565 * evaluation has been cancelled due to an aborting error, an
2566 * interrupt, or an exception.
2567 */
2568 if (!aborting())
2569 {
2570 if (!eap->skip && fudi.fd_newkey != NULL)
2571 semsg(_(e_dictkey), fudi.fd_newkey);
2572 vim_free(fudi.fd_newkey);
2573 return NULL;
2574 }
2575 else
2576 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002577 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002578 }
2579
Bram Moolenaare38eab22019-12-05 21:50:01 +01002580 // An error in a function call during evaluation of an expression in magic
2581 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002582 saved_did_emsg = did_emsg;
2583 did_emsg = FALSE;
2584
2585 /*
2586 * ":function func" with only function name: list function.
2587 */
2588 if (!paren)
2589 {
2590 if (!ends_excmd(*skipwhite(p)))
2591 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002592 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002593 goto ret_free;
2594 }
2595 eap->nextcmd = check_nextcmd(p);
2596 if (eap->nextcmd != NULL)
2597 *p = NUL;
2598 if (!eap->skip && !got_int)
2599 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002600 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002601 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
2602 {
2603 char_u *up = untrans_function_name(name);
2604
2605 // With Vim9 script the name was made script-local, if not
2606 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02002607 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002608 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002609 }
2610
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002611 if (fp != NULL)
2612 {
2613 list_func_head(fp, TRUE);
2614 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2615 {
2616 if (FUNCLINE(fp, j) == NULL)
2617 continue;
2618 msg_putchar('\n');
2619 msg_outnum((long)(j + 1));
2620 if (j < 9)
2621 msg_putchar(' ');
2622 if (j < 99)
2623 msg_putchar(' ');
2624 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002625 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002626 ui_breakcheck();
2627 }
2628 if (!got_int)
2629 {
2630 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002631 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 msg_puts(" enddef");
2633 else
2634 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002635 }
2636 }
2637 else
Bram Moolenaara26b9702020-04-18 19:53:28 +02002638 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002639 }
2640 goto ret_free;
2641 }
2642
2643 /*
2644 * ":function name(arg1, arg2)" Define function.
2645 */
2646 p = skipwhite(p);
2647 if (*p != '(')
2648 {
2649 if (!eap->skip)
2650 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002651 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002652 goto ret_free;
2653 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002654 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002655 if (vim_strchr(p, '(') != NULL)
2656 p = vim_strchr(p, '(');
2657 }
2658 p = skipwhite(p + 1);
2659
2660 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2661
Bram Moolenaar04b12692020-05-04 23:24:44 +02002662 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002663 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002664 // Check the name of the function. Unless it's a dictionary function
2665 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002666 if (name != NULL)
2667 arg = name;
2668 else
2669 arg = fudi.fd_newkey;
2670 if (arg != NULL && (fudi.fd_di == NULL
2671 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2672 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2673 {
2674 if (*arg == K_SPECIAL)
2675 j = 3;
2676 else
2677 j = 0;
2678 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2679 : eval_isnamec(arg[j])))
2680 ++j;
2681 if (arg[j] != NUL)
2682 emsg_funcname((char *)e_invarg2, arg);
2683 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002684 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002685 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002686 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002687 }
2688
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002689 // This may get more lines and make the pointers into the first line
2690 // invalid.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691 if (get_function_args(&p, ')', &newargs,
2692 eap->cmdidx == CMD_def ? &argtypes : NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002693 &varargs, &default_args, eap->skip,
2694 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002695 goto errret_2;
2696
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002698 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 // find the return type: :def Func(): type
2700 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002701 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 ret_type = skipwhite(p + 1);
2703 p = skip_type(ret_type);
2704 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002705 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002706 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002708 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002710 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 semsg(_("E1056: expected a type: %s"), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02002712 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002713 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002714 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002715 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716 else
2717 // find extra arguments "range", "dict", "abort" and "closure"
2718 for (;;)
2719 {
2720 p = skipwhite(p);
2721 if (STRNCMP(p, "range", 5) == 0)
2722 {
2723 flags |= FC_RANGE;
2724 p += 5;
2725 }
2726 else if (STRNCMP(p, "dict", 4) == 0)
2727 {
2728 flags |= FC_DICT;
2729 p += 4;
2730 }
2731 else if (STRNCMP(p, "abort", 5) == 0)
2732 {
2733 flags |= FC_ABORT;
2734 p += 5;
2735 }
2736 else if (STRNCMP(p, "closure", 7) == 0)
2737 {
2738 flags |= FC_CLOSURE;
2739 p += 7;
2740 if (current_funccal == NULL)
2741 {
2742 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
2743 name == NULL ? (char_u *)"" : name);
2744 goto erret;
2745 }
2746 }
2747 else
2748 break;
2749 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002750
Bram Moolenaare38eab22019-12-05 21:50:01 +01002751 // When there is a line break use what follows for the function body.
2752 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002753 if (*p == '\n')
2754 line_arg = p + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002755 else if (*p != NUL && *p != '"' && !(eap->cmdidx == CMD_def && *p == '#')
2756 && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002757 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002758
2759 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
2761 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002762 */
2763 if (KeyTyped)
2764 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002765 // Check if the function already exists, don't let the user type the
2766 // whole function before telling him it doesn't work! For a script we
2767 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002768 if (!eap->skip && !eap->forceit)
2769 {
2770 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002771 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002772 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002773 emsg_funcname(e_funcexts, name);
2774 }
2775
2776 if (!eap->skip && did_emsg)
2777 goto erret;
2778
Bram Moolenaare38eab22019-12-05 21:50:01 +01002779 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002780 cmdline_row = msg_row;
2781 }
2782
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002783 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002784 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002785
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002786 indent = 2;
2787 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002789 for (;;)
2790 {
2791 if (KeyTyped)
2792 {
2793 msg_scroll = TRUE;
2794 saved_wait_return = FALSE;
2795 }
2796 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002797
2798 if (line_arg != NULL)
2799 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002800 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002801 theline = line_arg;
2802 p = vim_strchr(theline, '\n');
2803 if (p == NULL)
2804 line_arg += STRLEN(line_arg);
2805 else
2806 {
2807 *p = NUL;
2808 line_arg = p + 1;
2809 }
2810 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002811 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002812 {
2813 vim_free(line_to_free);
2814 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002815 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002816 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002817 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002818 line_to_free = theline;
2819 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002820 if (KeyTyped)
2821 lines_left = Rows - 1;
2822 if (theline == NULL)
2823 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 if (eap->cmdidx == CMD_def)
2825 emsg(_("E1057: Missing :enddef"));
2826 else
2827 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002828 goto erret;
2829 }
2830
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002831 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002832 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002833 if (SOURCING_LNUM < sourcing_lnum_off)
2834 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002835 else
2836 sourcing_lnum_off = 0;
2837
2838 if (skip_until != NULL)
2839 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002841 // * ":append" and "."
2842 // * ":python <<EOF" and "EOF"
2843 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2844 if (heredoc_trimmed == NULL
2845 || (is_heredoc && skipwhite(theline) == theline)
2846 || STRNCMP(theline, heredoc_trimmed,
2847 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002848 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002849 if (heredoc_trimmed == NULL)
2850 p = theline;
2851 else if (is_heredoc)
2852 p = skipwhite(theline) == theline
2853 ? theline : theline + STRLEN(heredoc_trimmed);
2854 else
2855 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002856 if (STRCMP(p, skip_until) == 0)
2857 {
2858 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002859 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002860 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002861 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002862 }
2863 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002864 }
2865 else
2866 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002867 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01002868 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002869 ;
2870
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 // Check for "endfunction" or "enddef".
2872 if (checkforcmd(&p, nesting_def[nesting]
2873 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002874 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002875 char_u *nextcmd = NULL;
2876
Bram Moolenaar663bb232017-06-22 19:12:10 +02002877 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002878 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002879 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002880 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002881 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 give_warning2(eap->cmdidx == CMD_def
2883 ? (char_u *)_("W1001: Text found after :enddef: %s")
2884 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002885 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002886 if (nextcmd != NULL)
2887 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002888 // Another command follows. If the line came from "eap" we
2889 // can simply point into it, otherwise we need to change
2890 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02002891 eap->nextcmd = nextcmd;
2892 if (line_to_free != NULL)
2893 {
2894 vim_free(*eap->cmdlinep);
2895 *eap->cmdlinep = line_to_free;
2896 line_to_free = NULL;
2897 }
2898 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002899 break;
2900 }
2901
Bram Moolenaare38eab22019-12-05 21:50:01 +01002902 // Increase indent inside "if", "while", "for" and "try", decrease
2903 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002905 indent -= 2;
2906 else if (STRNCMP(p, "if", 2) == 0
2907 || STRNCMP(p, "wh", 2) == 0
2908 || STRNCMP(p, "for", 3) == 0
2909 || STRNCMP(p, "try", 3) == 0)
2910 indent += 2;
2911
Bram Moolenaare38eab22019-12-05 21:50:01 +01002912 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01002913 // Only recognize "def" inside "def", not inside "function",
2914 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01002916 if (checkforcmd(&p, "function", 2)
2917 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002918 {
2919 if (*p == '!')
2920 p = skipwhite(p + 1);
2921 p += eval_fname_script(p);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002922 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL, NULL));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002923 if (*skipwhite(p) == '(')
2924 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 if (nesting == MAX_FUNC_NESTING - 1)
2926 emsg(_("E1058: function nesting too deep"));
2927 else
2928 {
2929 ++nesting;
2930 nesting_def[nesting] = (c == 'd');
2931 indent += 2;
2932 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002933 }
2934 }
2935
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002936 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002937 p = skip_range(p, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002938 if (eap->cmdidx != CMD_def
2939 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002940 || (p[0] == 'c'
2941 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2942 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2943 && (STRNCMP(&p[3], "nge", 3) != 0
2944 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002945 || (p[0] == 'i'
2946 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002947 && (!ASCII_ISALPHA(p[2])
2948 || (p[2] == 's'
2949 && (!ASCII_ISALPHA(p[3])
2950 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002951 skip_until = vim_strsave((char_u *)".");
2952
Bram Moolenaare38eab22019-12-05 21:50:01 +01002953 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002954 arg = skipwhite(skiptowhite(p));
2955 if (arg[0] == '<' && arg[1] =='<'
2956 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002957 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2958 || ((p[2] == '3' || p[2] == 'x')
2959 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002960 || (p[0] == 'p' && p[1] == 'e'
2961 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2962 || (p[0] == 't' && p[1] == 'c'
2963 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2964 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2965 && !ASCII_ISALPHA(p[3]))
2966 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2967 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2968 || (p[0] == 'm' && p[1] == 'z'
2969 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2970 ))
2971 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002972 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002973 p = skipwhite(arg + 2);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02002974 if (STRNCMP(p, "trim", 4) == 0)
2975 {
2976 // Ignore leading white space.
2977 p = skipwhite(p + 4);
2978 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002979 skipwhite(theline) - theline);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02002980 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002981 if (*p == NUL)
2982 skip_until = vim_strsave((char_u *)".");
2983 else
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002984 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02002985 do_concat = FALSE;
2986 is_heredoc = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002987 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002988
2989 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002990 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02002991 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002992 if (*arg == '[')
2993 arg = vim_strchr(arg, ']');
2994 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002995 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002996 arg = skipwhite(skiptowhite(arg));
2997 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
2998 && ((p[0] == 'l'
2999 && p[1] == 'e'
3000 && (!ASCII_ISALNUM(p[2])
3001 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02003002 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003003 p = skipwhite(arg + 3);
3004 if (STRNCMP(p, "trim", 4) == 0)
3005 {
3006 // Ignore leading white space.
3007 p = skipwhite(p + 4);
3008 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003009 skipwhite(theline) - theline);
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003010 }
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003011 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003012 do_concat = FALSE;
3013 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003014 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003015 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003016 }
3017
Bram Moolenaare38eab22019-12-05 21:50:01 +01003018 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003019 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003020 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003021
Bram Moolenaare38eab22019-12-05 21:50:01 +01003022 // Copy the line to newly allocated memory. get_one_sourceline()
3023 // allocates 250 bytes per line, this saves 80% on average. The cost
3024 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003025 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003026 if (p == NULL)
3027 goto erret;
3028 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003029
Bram Moolenaare38eab22019-12-05 21:50:01 +01003030 // Add NULL lines for continuation lines, so that the line count is
3031 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003032 while (sourcing_lnum_off-- > 0)
3033 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3034
Bram Moolenaare38eab22019-12-05 21:50:01 +01003035 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003036 if (line_arg != NULL && *line_arg == NUL)
3037 line_arg = NULL;
3038 }
3039
Bram Moolenaare38eab22019-12-05 21:50:01 +01003040 // Don't define the function when skipping commands or when an error was
3041 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003042 if (eap->skip || did_emsg)
3043 goto erret;
3044
3045 /*
3046 * If there are no errors, add the function
3047 */
3048 if (fudi.fd_dict == NULL)
3049 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 hashtab_T *ht;
3051
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003052 v = find_var(name, &ht, FALSE);
3053 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
3054 {
3055 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
3056 name);
3057 goto erret;
3058 }
3059
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003060 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003061 if (fp != NULL)
3062 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 int dead = fp->uf_flags & FC_DEAD;
3064
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003065 // Function can be replaced with "function!" and when sourcing the
3066 // same script again, but only once.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 if (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003068 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
3069 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003070 {
3071 emsg_funcname(e_funcexts, name);
3072 goto erret;
3073 }
3074 if (fp->uf_calls > 0)
3075 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003076 emsg_funcname(
3077 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003078 name);
3079 goto erret;
3080 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003081 if (fp->uf_refcount > 1)
3082 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003083 // This function is referenced somewhere, don't redefine it but
3084 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003085 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003086 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003087 fp = NULL;
3088 overwrite = TRUE;
3089 }
3090 else
3091 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003092 char_u *exp_name = fp->uf_name_exp;
3093
3094 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01003095 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003096 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003097 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003098 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003100#ifdef FEAT_PROFILE
3101 fp->uf_profiling = FALSE;
3102 fp->uf_prof_initialized = FALSE;
3103#endif
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003104 clear_def_function(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003105 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003106 }
3107 }
3108 else
3109 {
3110 char numbuf[20];
3111
3112 fp = NULL;
3113 if (fudi.fd_newkey == NULL && !eap->forceit)
3114 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003115 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003116 goto erret;
3117 }
3118 if (fudi.fd_di == NULL)
3119 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003120 // Can't add a function to a locked dictionary
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003121 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003122 goto erret;
3123 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003124 // Can't change an existing function if it is locked
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003125 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003126 goto erret;
3127
Bram Moolenaare38eab22019-12-05 21:50:01 +01003128 // Give the function a sequential number. Can only be used with a
3129 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003130 vim_free(name);
3131 sprintf(numbuf, "%d", ++func_nr);
3132 name = vim_strsave((char_u *)numbuf);
3133 if (name == NULL)
3134 goto erret;
3135 }
3136
3137 if (fp == NULL)
3138 {
3139 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3140 {
3141 int slen, plen;
3142 char_u *scriptname;
3143
Bram Moolenaare38eab22019-12-05 21:50:01 +01003144 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003145 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003146 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003147 {
3148 scriptname = autoload_name(name);
3149 if (scriptname != NULL)
3150 {
3151 p = vim_strchr(scriptname, '/');
3152 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003153 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003154 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003155 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003156 j = OK;
3157 vim_free(scriptname);
3158 }
3159 }
3160 if (j == FAIL)
3161 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003162 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003163 goto erret;
3164 }
3165 }
3166
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003167 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003168 if (fp == NULL)
3169 goto erret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003170 fp->uf_def_status = eap->cmdidx == CMD_def ? UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003171 : UF_NOT_COMPILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003172
3173 if (fudi.fd_dict != NULL)
3174 {
3175 if (fudi.fd_di == NULL)
3176 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003177 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003178 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3179 if (fudi.fd_di == NULL)
3180 {
3181 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003182 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003183 goto erret;
3184 }
3185 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3186 {
3187 vim_free(fudi.fd_di);
3188 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003189 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003190 goto erret;
3191 }
3192 }
3193 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003194 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003195 clear_tv(&fudi.fd_di->di_tv);
3196 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003197 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003198
Bram Moolenaare38eab22019-12-05 21:50:01 +01003199 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003200 flags |= FC_DICT;
3201 }
3202
Bram Moolenaare38eab22019-12-05 21:50:01 +01003203 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003204 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003205 if (overwrite)
3206 {
3207 hi = hash_find(&func_hashtab, name);
3208 hi->hi_key = UF2HIKEY(fp);
3209 }
3210 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003211 {
3212 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003213 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003214 goto erret;
3215 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003216 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003217 }
3218 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003219 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003221 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222
3223 if (eap->cmdidx == CMD_def)
3224 {
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003225 int lnum_save = SOURCING_LNUM;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003226
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003227 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003228
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003229 // error messages are for the first function line
3230 SOURCING_LNUM = sourcing_lnum_top;
3231
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 // parse the argument types
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003233 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234
3235 if (argtypes.ga_len > 0)
3236 {
3237 // When "varargs" is set the last name/type goes into uf_va_name
3238 // and uf_va_type.
3239 int len = argtypes.ga_len - (varargs ? 1 : 0);
3240
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003241 if (len > 0)
3242 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 if (fp->uf_arg_types != NULL)
3244 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003245 int i;
3246 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247
3248 for (i = 0; i < len; ++ i)
3249 {
3250 p = ((char_u **)argtypes.ga_data)[i];
3251 if (p == NULL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003252 // will get the type from the default value
3253 type = &t_unknown;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 else
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003255 type = parse_type(&p, &fp->uf_type_list);
3256 if (type == NULL)
3257 {
3258 SOURCING_LNUM = lnum_save;
3259 goto errret_2;
3260 }
3261 fp->uf_arg_types[i] = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 }
3263 }
3264 if (varargs)
3265 {
3266 // Move the last argument "...name: type" to uf_va_name and
3267 // uf_va_type.
3268 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
3269 [fp->uf_args.ga_len - 1];
3270 --fp->uf_args.ga_len;
3271 p = ((char_u **)argtypes.ga_data)[len];
3272 if (p == NULL)
3273 // todo: get type from default value
3274 fp->uf_va_type = &t_any;
3275 else
3276 fp->uf_va_type = parse_type(&p, &fp->uf_type_list);
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003277 if (fp->uf_va_type == NULL)
3278 {
3279 SOURCING_LNUM = lnum_save;
3280 goto errret_2;
3281 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 }
3283 varargs = FALSE;
3284 }
3285
3286 // parse the return type, if any
3287 if (ret_type == NULL)
3288 fp->uf_ret_type = &t_void;
3289 else
3290 {
3291 p = ret_type;
3292 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list);
3293 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02003294 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003296 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003297 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003299 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003300 if ((flags & FC_CLOSURE) != 0)
3301 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003302 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003303 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003304 }
3305 else
3306 fp->uf_scoped = NULL;
3307
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003308#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003309 if (prof_def_func())
3310 func_do_profile(fp);
3311#endif
3312 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003313 if (sandbox)
3314 flags |= FC_SANDBOX;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003315 if (in_vim9script() && !ASCII_ISUPPER(*fp->uf_name))
3316 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003317 fp->uf_flags = flags;
3318 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003319 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003320 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003321 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322 if (is_export)
3323 {
3324 fp->uf_flags |= FC_EXPORT;
3325 // let ex_export() know the export worked.
3326 is_export = FALSE;
3327 }
3328
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003329 if (eap->cmdidx == CMD_def)
3330 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02003331 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
3332 // :func does not use Vim9 script syntax, even in a Vim9 script file
3333 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003334
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003335 goto ret_free;
3336
3337erret:
3338 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003339 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003340errret_2:
3341 ga_clear_strings(&newlines);
3342ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003343 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003344 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003345 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003346 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003347 if (name != name_arg)
3348 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003349 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003350 did_emsg |= saved_did_emsg;
3351 need_wait_return |= saved_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003352
3353 return fp;
3354}
3355
3356/*
3357 * ":function"
3358 */
3359 void
3360ex_function(exarg_T *eap)
3361{
Bram Moolenaar822ba242020-05-24 23:00:18 +02003362 (void)def_function(eap, NULL);
3363}
3364
3365/*
3366 * :defcompile - compile all :def functions in the current script.
3367 */
3368 void
3369ex_defcompile(exarg_T *eap UNUSED)
3370{
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003371 long_u ht_used = func_hashtab.ht_used;
3372 int todo = (int)ht_used;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003373 hashitem_T *hi;
3374 ufunc_T *ufunc;
3375
3376 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
3377 {
3378 if (!HASHITEM_EMPTY(hi))
3379 {
3380 --todo;
3381 ufunc = HI2UF(hi);
3382 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003383 && ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003384 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02003385 compile_def_function(ufunc, FALSE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003386
3387 if (func_hashtab.ht_used != ht_used)
3388 {
3389 // another function has been defined, need to start over
3390 hi = func_hashtab.ht_array;
3391 ht_used = func_hashtab.ht_used;
3392 todo = (int)ht_used;
Bram Moolenaar285b1892020-05-26 11:37:26 +02003393 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003394 }
3395 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003396 }
3397 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003398}
3399
3400/*
3401 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3402 * Return 2 if "p" starts with "s:".
3403 * Return 0 otherwise.
3404 */
3405 int
3406eval_fname_script(char_u *p)
3407{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003408 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3409 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003410 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3411 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3412 return 5;
3413 if (p[0] == 's' && p[1] == ':')
3414 return 2;
3415 return 0;
3416}
3417
3418 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003419translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003420{
3421 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003422 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003423 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424}
3425
3426/*
3427 * Return TRUE when "ufunc" has old-style "..." varargs
3428 * or named varargs "...name: type".
3429 */
3430 int
3431has_varargs(ufunc_T *ufunc)
3432{
3433 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003434}
3435
3436/*
3437 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003438 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003439 */
3440 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003441function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003442{
3443 char_u *nm = name;
3444 char_u *p;
3445 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003446 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003447 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003448
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003449 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3450 if (no_deref)
3451 flag |= TFN_NO_DEREF;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003452 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003453 nm = skipwhite(nm);
3454
Bram Moolenaare38eab22019-12-05 21:50:01 +01003455 // Only accept "funcname", "funcname ", "funcname (..." and
3456 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003457 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003458 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003459 vim_free(p);
3460 return n;
3461}
3462
Bram Moolenaar113e1072019-01-20 15:30:40 +01003463#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003464 char_u *
3465get_expanded_name(char_u *name, int check)
3466{
3467 char_u *nm = name;
3468 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003469 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003470
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003471 p = trans_function_name(&nm, &is_global, FALSE,
3472 TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003473
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003474 if (p != NULL && *nm == NUL
3475 && (!check || translated_function_exists(p, is_global)))
3476 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003477
3478 vim_free(p);
3479 return NULL;
3480}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003481#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003482
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003483/*
3484 * Function given to ExpandGeneric() to obtain the list of user defined
3485 * function names.
3486 */
3487 char_u *
3488get_user_func_name(expand_T *xp, int idx)
3489{
3490 static long_u done;
3491 static hashitem_T *hi;
3492 ufunc_T *fp;
3493
3494 if (idx == 0)
3495 {
3496 done = 0;
3497 hi = func_hashtab.ht_array;
3498 }
3499 if (done < func_hashtab.ht_used)
3500 {
3501 if (done++ > 0)
3502 ++hi;
3503 while (HASHITEM_EMPTY(hi))
3504 ++hi;
3505 fp = HI2UF(hi);
3506
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 // don't show dead, dict and lambda functions
3508 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02003509 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003511
3512 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003513 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003514
3515 cat_func_name(IObuff, fp);
3516 if (xp->xp_context != EXPAND_USER_FUNC)
3517 {
3518 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003520 STRCAT(IObuff, ")");
3521 }
3522 return IObuff;
3523 }
3524 return NULL;
3525}
3526
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003527/*
3528 * ":delfunction {name}"
3529 */
3530 void
3531ex_delfunction(exarg_T *eap)
3532{
3533 ufunc_T *fp = NULL;
3534 char_u *p;
3535 char_u *name;
3536 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003537 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003538
3539 p = eap->arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003540 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003541 vim_free(fudi.fd_newkey);
3542 if (name == NULL)
3543 {
3544 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003545 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003546 return;
3547 }
3548 if (!ends_excmd(*skipwhite(p)))
3549 {
3550 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003551 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003552 return;
3553 }
3554 eap->nextcmd = check_nextcmd(p);
3555 if (eap->nextcmd != NULL)
3556 *p = NUL;
3557
3558 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003559 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003560 vim_free(name);
3561
3562 if (!eap->skip)
3563 {
3564 if (fp == NULL)
3565 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02003566 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003567 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003568 return;
3569 }
3570 if (fp->uf_calls > 0)
3571 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003572 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003573 return;
3574 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003575 if (fp->uf_flags & FC_VIM9)
3576 {
3577 semsg(_("E1084: Cannot delete Vim9 script function %s"), eap->arg);
3578 return;
3579 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003580
3581 if (fudi.fd_dict != NULL)
3582 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003583 // Delete the dict item that refers to the function, it will
3584 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003585 dictitem_remove(fudi.fd_dict, fudi.fd_di);
3586 }
3587 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003588 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003589 // A normal function (not a numbered function or lambda) has a
3590 // refcount of 1 for the entry in the hashtable. When deleting
3591 // it and the refcount is more than one, it should be kept.
3592 // A numbered function and lambda should be kept if the refcount is
3593 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003594 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003595 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003596 // Function is still referenced somewhere. Don't free it but
3597 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003598 if (func_remove(fp))
3599 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003600 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003601 }
3602 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003603 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003604 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003605 }
3606}
3607
3608/*
3609 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003610 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003611 */
3612 void
3613func_unref(char_u *name)
3614{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003615 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003616
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003617 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003618 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003619 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003620 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003621 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003622#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003623 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003624#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01003625 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003626 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003627 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003628 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003629 // Only delete it when it's not being used. Otherwise it's done
3630 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003631 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003632 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02003633 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003634}
3635
3636/*
3637 * Unreference a Function: decrement the reference count and free it when it
3638 * becomes zero.
3639 */
3640 void
3641func_ptr_unref(ufunc_T *fp)
3642{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003643 if (fp != NULL && --fp->uf_refcount <= 0)
3644 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003645 // Only delete it when it's not being used. Otherwise it's done
3646 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02003647 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003648 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003649 }
3650}
3651
3652/*
3653 * Count a reference to a Function.
3654 */
3655 void
3656func_ref(char_u *name)
3657{
3658 ufunc_T *fp;
3659
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003660 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003661 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003662 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003663 if (fp != NULL)
3664 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003665 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01003666 // Only give an error for a numbered function.
3667 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01003668 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003669}
3670
3671/*
3672 * Count a reference to a Function.
3673 */
3674 void
3675func_ptr_ref(ufunc_T *fp)
3676{
3677 if (fp != NULL)
3678 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003679}
3680
3681/*
3682 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3683 * referenced from anywhere that is in use.
3684 */
3685 static int
3686can_free_funccal(funccall_T *fc, int copyID)
3687{
3688 return (fc->l_varlist.lv_copyID != copyID
3689 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003690 && fc->l_avars.dv_copyID != copyID
3691 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003692}
3693
3694/*
3695 * ":return [expr]"
3696 */
3697 void
3698ex_return(exarg_T *eap)
3699{
3700 char_u *arg = eap->arg;
3701 typval_T rettv;
3702 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003703 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003704
3705 if (current_funccal == NULL)
3706 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003707 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003708 return;
3709 }
3710
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003711 CLEAR_FIELD(evalarg);
3712 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
3713
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003714 if (eap->skip)
3715 ++emsg_skip;
3716
3717 eap->nextcmd = NULL;
3718 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003719 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003720 {
3721 if (!eap->skip)
3722 returning = do_return(eap, FALSE, TRUE, &rettv);
3723 else
3724 clear_tv(&rettv);
3725 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003726 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003727 else if (!eap->skip)
3728 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003729 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003730 update_force_abort();
3731
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003732 /*
3733 * Return unless the expression evaluation has been cancelled due to an
3734 * aborting error, an interrupt, or an exception.
3735 */
3736 if (!aborting())
3737 returning = do_return(eap, FALSE, TRUE, NULL);
3738 }
3739
Bram Moolenaare38eab22019-12-05 21:50:01 +01003740 // When skipping or the return gets pending, advance to the next command
3741 // in this line (!returning). Otherwise, ignore the rest of the line.
3742 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003743 if (returning)
3744 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003745 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003746 eap->nextcmd = check_nextcmd(arg);
3747
3748 if (eap->skip)
3749 --emsg_skip;
3750}
3751
3752/*
3753 * ":1,25call func(arg1, arg2)" function call.
3754 */
3755 void
3756ex_call(exarg_T *eap)
3757{
3758 char_u *arg = eap->arg;
3759 char_u *startarg;
3760 char_u *name;
3761 char_u *tofree;
3762 int len;
3763 typval_T rettv;
3764 linenr_T lnum;
3765 int doesrange;
3766 int failed = FALSE;
3767 funcdict_T fudi;
3768 partial_T *partial = NULL;
3769
3770 if (eap->skip)
3771 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003772 // trans_function_name() doesn't work well when skipping, use eval0()
3773 // instead to skip to any following command, e.g. for:
3774 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003775 ++emsg_skip;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003776 if (eval0(eap->arg, &rettv, eap, NULL) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003777 clear_tv(&rettv);
3778 --emsg_skip;
3779 return;
3780 }
3781
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003782 tofree = trans_function_name(&arg, NULL, eap->skip,
3783 TFN_INT, &fudi, &partial);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003784 if (fudi.fd_newkey != NULL)
3785 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003786 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003787 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003788 vim_free(fudi.fd_newkey);
3789 }
3790 if (tofree == NULL)
3791 return;
3792
Bram Moolenaare38eab22019-12-05 21:50:01 +01003793 // Increase refcount on dictionary, it could get deleted when evaluating
3794 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003795 if (fudi.fd_dict != NULL)
3796 ++fudi.fd_dict->dv_refcount;
3797
Bram Moolenaare38eab22019-12-05 21:50:01 +01003798 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3799 // contents. For VAR_PARTIAL get its partial, unless we already have one
3800 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003801 len = (int)STRLEN(tofree);
3802 name = deref_func_name(tofree, &len,
3803 partial != NULL ? NULL : &partial, FALSE);
3804
Bram Moolenaare38eab22019-12-05 21:50:01 +01003805 // Skip white space to allow ":call func ()". Not good, but required for
3806 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003807 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003808 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003809
3810 if (*startarg != '(')
3811 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003813 goto end;
3814 }
3815
3816 /*
3817 * When skipping, evaluate the function once, to find the end of the
3818 * arguments.
3819 * When the function takes a range, this is discovered after the first
3820 * call, and the loop is broken.
3821 */
3822 if (eap->skip)
3823 {
3824 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003825 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003826 }
3827 else
3828 lnum = eap->line1;
3829 for ( ; lnum <= eap->line2; ++lnum)
3830 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003831 funcexe_T funcexe;
3832
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003833 if (!eap->skip && eap->addr_count > 0)
3834 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003835 if (lnum > curbuf->b_ml.ml_line_count)
3836 {
3837 // If the function deleted lines or switched to another buffer
3838 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003839 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003840 break;
3841 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003842 curwin->w_cursor.lnum = lnum;
3843 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003844 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003845 }
3846 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003847
Bram Moolenaara80faa82020-04-12 19:37:17 +02003848 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003849 funcexe.firstline = eap->line1;
3850 funcexe.lastline = eap->line2;
3851 funcexe.doesrange = &doesrange;
3852 funcexe.evaluate = !eap->skip;
3853 funcexe.partial = partial;
3854 funcexe.selfdict = fudi.fd_dict;
3855 if (get_func_tv(name, -1, &rettv, &arg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003856 {
3857 failed = TRUE;
3858 break;
3859 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003860 if (has_watchexpr())
3861 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003862
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003863 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaar32e35112020-05-14 22:41:15 +02003864 if (handle_subscript(&arg, &rettv, eap->skip ? 0 : EVAL_EVALUATE,
3865 TRUE, name, &name) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003866 {
3867 failed = TRUE;
3868 break;
3869 }
3870
3871 clear_tv(&rettv);
3872 if (doesrange || eap->skip)
3873 break;
3874
Bram Moolenaare38eab22019-12-05 21:50:01 +01003875 // Stop when immediately aborting on error, or when an interrupt
3876 // occurred or an exception was thrown but not caught.
3877 // get_func_tv() returned OK, so that the check for trailing
3878 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003879 if (aborting())
3880 break;
3881 }
3882 if (eap->skip)
3883 --emsg_skip;
3884
Bram Moolenaare51bb172020-02-16 19:42:23 +01003885 // When inside :try we need to check for following "| catch".
3886 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003887 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003888 // Check for trailing illegal characters and a following command.
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003889 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003890 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01003891 if (!failed)
3892 {
3893 emsg_severe = TRUE;
3894 emsg(_(e_trailing));
3895 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003896 }
3897 else
3898 eap->nextcmd = check_nextcmd(arg);
3899 }
3900
3901end:
3902 dict_unref(fudi.fd_dict);
3903 vim_free(tofree);
3904}
3905
3906/*
3907 * Return from a function. Possibly makes the return pending. Also called
3908 * for a pending return at the ":endtry" or after returning from an extra
3909 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3910 * when called due to a ":return" command. "rettv" may point to a typval_T
3911 * with the return rettv. Returns TRUE when the return can be carried out,
3912 * FALSE when the return gets pending.
3913 */
3914 int
3915do_return(
3916 exarg_T *eap,
3917 int reanimate,
3918 int is_cmd,
3919 void *rettv)
3920{
3921 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01003922 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003923
3924 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003925 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003926 current_funccal->returned = FALSE;
3927
3928 /*
3929 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3930 * not in its finally clause (which then is to be executed next) is found.
3931 * In this case, make the ":return" pending for execution at the ":endtry".
3932 * Otherwise, return normally.
3933 */
3934 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3935 if (idx >= 0)
3936 {
3937 cstack->cs_pending[idx] = CSTP_RETURN;
3938
3939 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003940 // A pending return again gets pending. "rettv" points to an
3941 // allocated variable with the rettv of the original ":return"'s
3942 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003943 cstack->cs_rettv[idx] = rettv;
3944 else
3945 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003946 // When undoing a return in order to make it pending, get the stored
3947 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003948 if (reanimate)
3949 rettv = current_funccal->rettv;
3950
3951 if (rettv != NULL)
3952 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003953 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003954 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3955 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3956 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003957 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003958 }
3959 else
3960 cstack->cs_rettv[idx] = NULL;
3961
3962 if (reanimate)
3963 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003964 // The pending return value could be overwritten by a ":return"
3965 // without argument in a finally clause; reset the default
3966 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003967 current_funccal->rettv->v_type = VAR_NUMBER;
3968 current_funccal->rettv->vval.v_number = 0;
3969 }
3970 }
3971 report_make_pending(CSTP_RETURN, rettv);
3972 }
3973 else
3974 {
3975 current_funccal->returned = TRUE;
3976
Bram Moolenaare38eab22019-12-05 21:50:01 +01003977 // If the return is carried out now, store the return value. For
3978 // a return immediately after reanimation, the value is already
3979 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003980 if (!reanimate && rettv != NULL)
3981 {
3982 clear_tv(current_funccal->rettv);
3983 *current_funccal->rettv = *(typval_T *)rettv;
3984 if (!is_cmd)
3985 vim_free(rettv);
3986 }
3987 }
3988
3989 return idx < 0;
3990}
3991
3992/*
3993 * Free the variable with a pending return value.
3994 */
3995 void
3996discard_pending_return(void *rettv)
3997{
3998 free_tv((typval_T *)rettv);
3999}
4000
4001/*
4002 * Generate a return command for producing the value of "rettv". The result
4003 * is an allocated string. Used by report_pending() for verbose messages.
4004 */
4005 char_u *
4006get_return_cmd(void *rettv)
4007{
4008 char_u *s = NULL;
4009 char_u *tofree = NULL;
4010 char_u numbuf[NUMBUFLEN];
4011
4012 if (rettv != NULL)
4013 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
4014 if (s == NULL)
4015 s = (char_u *)"";
4016
4017 STRCPY(IObuff, ":return ");
4018 STRNCPY(IObuff + 8, s, IOSIZE - 8);
4019 if (STRLEN(s) + 8 >= IOSIZE)
4020 STRCPY(IObuff + IOSIZE - 4, "...");
4021 vim_free(tofree);
4022 return vim_strsave(IObuff);
4023}
4024
4025/*
4026 * Get next function line.
4027 * Called by do_cmdline() to get the next line.
4028 * Returns allocated string, or NULL for end of function.
4029 */
4030 char_u *
4031get_func_line(
4032 int c UNUSED,
4033 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02004034 int indent UNUSED,
4035 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004036{
4037 funccall_T *fcp = (funccall_T *)cookie;
4038 ufunc_T *fp = fcp->func;
4039 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004040 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004041
Bram Moolenaare38eab22019-12-05 21:50:01 +01004042 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004043 if (fcp->dbg_tick != debug_tick)
4044 {
4045 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004046 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004047 fcp->dbg_tick = debug_tick;
4048 }
4049#ifdef FEAT_PROFILE
4050 if (do_profiling == PROF_YES)
4051 func_line_end(cookie);
4052#endif
4053
4054 gap = &fp->uf_lines;
4055 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4056 || fcp->returned)
4057 retval = NULL;
4058 else
4059 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004060 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004061 while (fcp->linenr < gap->ga_len
4062 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
4063 ++fcp->linenr;
4064 if (fcp->linenr >= gap->ga_len)
4065 retval = NULL;
4066 else
4067 {
4068 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004069 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004070#ifdef FEAT_PROFILE
4071 if (do_profiling == PROF_YES)
4072 func_line_start(cookie);
4073#endif
4074 }
4075 }
4076
Bram Moolenaare38eab22019-12-05 21:50:01 +01004077 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004078 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004079 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004080 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004081 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004082 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004083 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004084 fcp->dbg_tick = debug_tick;
4085 }
4086
4087 return retval;
4088}
4089
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004090/*
4091 * Return TRUE if the currently active function should be ended, because a
4092 * return was encountered or an error occurred. Used inside a ":while".
4093 */
4094 int
4095func_has_ended(void *cookie)
4096{
4097 funccall_T *fcp = (funccall_T *)cookie;
4098
Bram Moolenaare38eab22019-12-05 21:50:01 +01004099 // Ignore the "abort" flag if the abortion behavior has been changed due to
4100 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004101 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4102 || fcp->returned);
4103}
4104
4105/*
4106 * return TRUE if cookie indicates a function which "abort"s on errors.
4107 */
4108 int
4109func_has_abort(
4110 void *cookie)
4111{
4112 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
4113}
4114
4115
4116/*
4117 * Turn "dict.Func" into a partial for "Func" bound to "dict".
4118 * Don't do this when "Func" is already a partial that was bound
4119 * explicitly (pt_auto is FALSE).
4120 * Changes "rettv" in-place.
4121 * Returns the updated "selfdict_in".
4122 */
4123 dict_T *
4124make_partial(dict_T *selfdict_in, typval_T *rettv)
4125{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004126 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004127 char_u *tofree = NULL;
4128 ufunc_T *fp;
4129 char_u fname_buf[FLEN_FIXED + 1];
4130 int error;
4131 dict_T *selfdict = selfdict_in;
4132
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004133 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
4134 fp = rettv->vval.v_partial->pt_func;
4135 else
4136 {
4137 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
4138 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004139 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004140 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004141 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004142 vim_free(tofree);
4143 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004144
4145 if (fp != NULL && (fp->uf_flags & FC_DICT))
4146 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004147 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004148
4149 if (pt != NULL)
4150 {
4151 pt->pt_refcount = 1;
4152 pt->pt_dict = selfdict;
4153 pt->pt_auto = TRUE;
4154 selfdict = NULL;
4155 if (rettv->v_type == VAR_FUNC)
4156 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004157 // Just a function: Take over the function name and use
4158 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004159 pt->pt_name = rettv->vval.v_string;
4160 }
4161 else
4162 {
4163 partial_T *ret_pt = rettv->vval.v_partial;
4164 int i;
4165
Bram Moolenaare38eab22019-12-05 21:50:01 +01004166 // Partial: copy the function name, use selfdict and copy
4167 // args. Can't take over name or args, the partial might
4168 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004169 if (ret_pt->pt_name != NULL)
4170 {
4171 pt->pt_name = vim_strsave(ret_pt->pt_name);
4172 func_ref(pt->pt_name);
4173 }
4174 else
4175 {
4176 pt->pt_func = ret_pt->pt_func;
4177 func_ptr_ref(pt->pt_func);
4178 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004179 if (ret_pt->pt_argc > 0)
4180 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004181 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004182 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004183 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004184 pt->pt_argc = 0;
4185 else
4186 {
4187 pt->pt_argc = ret_pt->pt_argc;
4188 for (i = 0; i < pt->pt_argc; i++)
4189 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
4190 }
4191 }
4192 partial_unref(ret_pt);
4193 }
4194 rettv->v_type = VAR_PARTIAL;
4195 rettv->vval.v_partial = pt;
4196 }
4197 }
4198 return selfdict;
4199}
4200
4201/*
4202 * Return the name of the executed function.
4203 */
4204 char_u *
4205func_name(void *cookie)
4206{
4207 return ((funccall_T *)cookie)->func->uf_name;
4208}
4209
4210/*
4211 * Return the address holding the next breakpoint line for a funccall cookie.
4212 */
4213 linenr_T *
4214func_breakpoint(void *cookie)
4215{
4216 return &((funccall_T *)cookie)->breakpoint;
4217}
4218
4219/*
4220 * Return the address holding the debug tick for a funccall cookie.
4221 */
4222 int *
4223func_dbg_tick(void *cookie)
4224{
4225 return &((funccall_T *)cookie)->dbg_tick;
4226}
4227
4228/*
4229 * Return the nesting level for a funccall cookie.
4230 */
4231 int
4232func_level(void *cookie)
4233{
4234 return ((funccall_T *)cookie)->level;
4235}
4236
4237/*
4238 * Return TRUE when a function was ended by a ":return" command.
4239 */
4240 int
4241current_func_returned(void)
4242{
4243 return current_funccal->returned;
4244}
4245
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004246 int
4247free_unref_funccal(int copyID, int testing)
4248{
4249 int did_free = FALSE;
4250 int did_free_funccal = FALSE;
4251 funccall_T *fc, **pfc;
4252
4253 for (pfc = &previous_funccal; *pfc != NULL; )
4254 {
4255 if (can_free_funccal(*pfc, copyID))
4256 {
4257 fc = *pfc;
4258 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004259 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004260 did_free = TRUE;
4261 did_free_funccal = TRUE;
4262 }
4263 else
4264 pfc = &(*pfc)->caller;
4265 }
4266 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004267 // When a funccal was freed some more items might be garbage
4268 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004269 (void)garbage_collect(testing);
4270
4271 return did_free;
4272}
4273
4274/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004275 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004276 */
4277 static funccall_T *
4278get_funccal(void)
4279{
4280 int i;
4281 funccall_T *funccal;
4282 funccall_T *temp_funccal;
4283
4284 funccal = current_funccal;
4285 if (debug_backtrace_level > 0)
4286 {
4287 for (i = 0; i < debug_backtrace_level; i++)
4288 {
4289 temp_funccal = funccal->caller;
4290 if (temp_funccal)
4291 funccal = temp_funccal;
4292 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004293 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004294 debug_backtrace_level = i;
4295 }
4296 }
4297 return funccal;
4298}
4299
4300/*
4301 * Return the hashtable used for local variables in the current funccal.
4302 * Return NULL if there is no current funccal.
4303 */
4304 hashtab_T *
4305get_funccal_local_ht()
4306{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004308 return NULL;
4309 return &get_funccal()->l_vars.dv_hashtab;
4310}
4311
4312/*
4313 * Return the l: scope variable.
4314 * Return NULL if there is no current funccal.
4315 */
4316 dictitem_T *
4317get_funccal_local_var()
4318{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004320 return NULL;
4321 return &get_funccal()->l_vars_var;
4322}
4323
4324/*
4325 * Return the hashtable used for argument in the current funccal.
4326 * Return NULL if there is no current funccal.
4327 */
4328 hashtab_T *
4329get_funccal_args_ht()
4330{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004332 return NULL;
4333 return &get_funccal()->l_avars.dv_hashtab;
4334}
4335
4336/*
4337 * Return the a: scope variable.
4338 * Return NULL if there is no current funccal.
4339 */
4340 dictitem_T *
4341get_funccal_args_var()
4342{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004344 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004345 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004346}
4347
4348/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004349 * List function variables, if there is a function.
4350 */
4351 void
4352list_func_vars(int *first)
4353{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004355 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004356 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004357}
4358
4359/*
4360 * If "ht" is the hashtable for local variables in the current funccal, return
4361 * the dict that contains it.
4362 * Otherwise return NULL.
4363 */
4364 dict_T *
4365get_current_funccal_dict(hashtab_T *ht)
4366{
4367 if (current_funccal != NULL
4368 && ht == &current_funccal->l_vars.dv_hashtab)
4369 return &current_funccal->l_vars;
4370 return NULL;
4371}
4372
4373/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004374 * Search hashitem in parent scope.
4375 */
4376 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004377find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004378{
4379 funccall_T *old_current_funccal = current_funccal;
4380 hashtab_T *ht;
4381 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004382 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004383
4384 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4385 return NULL;
4386
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004387 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004388 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004389 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004390 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004391 ht = find_var_ht(name, &varname);
4392 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004393 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004394 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004395 if (!HASHITEM_EMPTY(hi))
4396 {
4397 *pht = ht;
4398 break;
4399 }
4400 }
4401 if (current_funccal == current_funccal->func->uf_scoped)
4402 break;
4403 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004404 }
4405 current_funccal = old_current_funccal;
4406
4407 return hi;
4408}
4409
4410/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004411 * Search variable in parent scope.
4412 */
4413 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004414find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004415{
4416 dictitem_T *v = NULL;
4417 funccall_T *old_current_funccal = current_funccal;
4418 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004419 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004420
4421 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4422 return NULL;
4423
Bram Moolenaare38eab22019-12-05 21:50:01 +01004424 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004425 current_funccal = current_funccal->func->uf_scoped;
4426 while (current_funccal)
4427 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004428 ht = find_var_ht(name, &varname);
4429 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004430 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004431 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004432 if (v != NULL)
4433 break;
4434 }
4435 if (current_funccal == current_funccal->func->uf_scoped)
4436 break;
4437 current_funccal = current_funccal->func->uf_scoped;
4438 }
4439 current_funccal = old_current_funccal;
4440
4441 return v;
4442}
4443
4444/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004445 * Set "copyID + 1" in previous_funccal and callers.
4446 */
4447 int
4448set_ref_in_previous_funccal(int copyID)
4449{
4450 int abort = FALSE;
4451 funccall_T *fc;
4452
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004453 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004454 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004455 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004456 abort = abort
4457 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4458 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004459 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004460 }
4461 return abort;
4462}
4463
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004464 static int
4465set_ref_in_funccal(funccall_T *fc, int copyID)
4466{
4467 int abort = FALSE;
4468
4469 if (fc->fc_copyID != copyID)
4470 {
4471 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004472 abort = abort
4473 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4474 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004475 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004476 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004477 }
4478 return abort;
4479}
4480
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004481/*
4482 * Set "copyID" in all local vars and arguments in the call stack.
4483 */
4484 int
4485set_ref_in_call_stack(int copyID)
4486{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004487 int abort = FALSE;
4488 funccall_T *fc;
4489 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004490
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004491 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004492 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004493
4494 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004495 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
4496 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004497 abort = abort || set_ref_in_funccal(fc, copyID);
4498
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004499 return abort;
4500}
4501
4502/*
4503 * Set "copyID" in all functions available by name.
4504 */
4505 int
4506set_ref_in_functions(int copyID)
4507{
4508 int todo;
4509 hashitem_T *hi = NULL;
4510 int abort = FALSE;
4511 ufunc_T *fp;
4512
4513 todo = (int)func_hashtab.ht_used;
4514 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004515 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004516 if (!HASHITEM_EMPTY(hi))
4517 {
4518 --todo;
4519 fp = HI2UF(hi);
4520 if (!func_name_refcount(fp->uf_name))
4521 abort = abort || set_ref_in_func(NULL, fp, copyID);
4522 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004523 }
4524 return abort;
4525}
4526
4527/*
4528 * Set "copyID" in all function arguments.
4529 */
4530 int
4531set_ref_in_func_args(int copyID)
4532{
4533 int i;
4534 int abort = FALSE;
4535
4536 for (i = 0; i < funcargs.ga_len; ++i)
4537 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
4538 copyID, NULL, NULL);
4539 return abort;
4540}
4541
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004542/*
4543 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004544 * Returns TRUE if setting references failed somehow.
4545 */
4546 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004547set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004548{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004549 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004550 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01004551 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004552 char_u fname_buf[FLEN_FIXED + 1];
4553 char_u *tofree = NULL;
4554 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004555 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004556
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004557 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004558 return FALSE;
4559
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004560 if (fp_in == NULL)
4561 {
4562 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004563 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004564 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004565 if (fp != NULL)
4566 {
4567 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004568 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004569 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004570
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004571 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004572 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004573}
4574
Bram Moolenaare38eab22019-12-05 21:50:01 +01004575#endif // FEAT_EVAL