blob: 51c437f208132b69810d582e2d4e906d1c5f6869 [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
Bram Moolenaar801ab062020-06-25 19:27:56 +0200344#if defined(FEAT_LUA) || defined(PROTO)
345/*
346 * Registers a native C callback which can be called from Vim script.
347 * Returns the name of the Vim script function.
348 */
349 char_u *
350register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
351{
352 char_u *name = get_lambda_name();
353 ufunc_T *fp = NULL;
354 garray_T newargs;
355 garray_T newlines;
356
357 ga_init(&newargs);
358 ga_init(&newlines);
359
360 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
361 if (fp == NULL)
362 goto errret;
363
364 fp->uf_dfunc_idx = UF_NOT_COMPILED;
365 fp->uf_refcount = 1;
366 fp->uf_varargs = TRUE;
367 fp->uf_flags = FC_CFUNC;
368 fp->uf_calls = 0;
369 fp->uf_script_ctx = current_sctx;
370 fp->uf_lines = newlines;
371 fp->uf_args = newargs;
372 fp->uf_cb = cb;
373 fp->uf_cb_free = cb_free;
374 fp->uf_cb_state = state;
375
376 set_ufunc_name(fp, name);
377 hash_add(&func_hashtab, UF2HIKEY(fp));
378
379 return name;
380
381errret:
382 ga_clear_strings(&newargs);
383 ga_clear_strings(&newlines);
384 vim_free(fp);
385 return NULL;
386}
387#endif
388
Bram Moolenaar04b12692020-05-04 23:24:44 +0200389/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200390 * Parse a lambda expression and get a Funcref from "*arg".
391 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
392 */
393 int
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200394get_lambda_tv(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200395{
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200396 int evaluate = evalarg != NULL
397 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200398 garray_T newargs;
399 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200400 garray_T *pnewargs;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200401 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100402 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200403 int varargs;
404 int ret;
Bram Moolenaar9215f012020-06-27 21:18:00 +0200405 char_u *start;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200406 char_u *s, *e;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200407 int *old_eval_lavars = eval_lavars_used;
408 int eval_lavars = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200409 char_u *tofree = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200410
411 ga_init(&newargs);
412 ga_init(&newlines);
413
Bram Moolenaare38eab22019-12-05 21:50:01 +0100414 // First, check if this is a lambda expression. "->" must exist.
Bram Moolenaar9215f012020-06-27 21:18:00 +0200415 start = skipwhite(*arg + 1);
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200416 ret = get_function_args(&start, '-', NULL, NULL, NULL, NULL, TRUE,
417 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200418 if (ret == FAIL || *start != '>')
419 return NOTDONE;
420
Bram Moolenaare38eab22019-12-05 21:50:01 +0100421 // Parse the arguments again.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200422 if (evaluate)
423 pnewargs = &newargs;
424 else
425 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200426 *arg = skipwhite(*arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100427 // TODO: argument types
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200428 ret = get_function_args(arg, '-', pnewargs, NULL, &varargs, NULL, FALSE,
429 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200430 if (ret == FAIL || **arg != '>')
431 goto errret;
432
Bram Moolenaare38eab22019-12-05 21:50:01 +0100433 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200434 if (evaluate)
435 eval_lavars_used = &eval_lavars;
436
Bram Moolenaare38eab22019-12-05 21:50:01 +0100437 // Get the start and the end of the expression.
Bram Moolenaar9215f012020-06-27 21:18:00 +0200438 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200439 s = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200440 ret = skip_expr_concatenate(&s, arg, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200441 if (ret == FAIL)
442 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200443 if (evalarg != NULL)
444 {
445 // avoid that the expression gets freed when another line break follows
446 tofree = evalarg->eval_tofree;
447 evalarg->eval_tofree = NULL;
448 }
449
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200450 e = *arg;
Bram Moolenaar9215f012020-06-27 21:18:00 +0200451 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200452 if (**arg != '}')
Bram Moolenaaree619e52020-03-28 21:38:06 +0100453 {
454 semsg(_("E451: Expected }: %s"), *arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200455 goto errret;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100456 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200457 ++*arg;
458
459 if (evaluate)
460 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200461 int len;
462 int flags = 0;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200463 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200464 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200465
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200466 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200467 if (fp == NULL)
468 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200469 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200470 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200471 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200472 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200473
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200474 ga_init2(&newlines, (int)sizeof(char_u *), 1);
475 if (ga_grow(&newlines, 1) == FAIL)
476 goto errret;
477
Bram Moolenaare38eab22019-12-05 21:50:01 +0100478 // Add "return " before the expression.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200479 len = 7 + (int)(e - s) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200480 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200481 if (p == NULL)
482 goto errret;
483 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
484 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200485 vim_strncpy(p + 7, s, e - s);
Bram Moolenaarf10806b2020-04-02 18:34:35 +0200486 if (strstr((char *)p + 7, "a:") == NULL)
487 // No a: variables are used for sure.
488 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200489
490 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100491 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200492 hash_add(&func_hashtab, UF2HIKEY(fp));
493 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200494 ga_init(&fp->uf_def_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200495 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200496 if (current_funccal != NULL && eval_lavars)
497 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200498 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200499 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200500 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200501 }
502 else
503 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200504
505#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200506 if (prof_def_func())
507 func_do_profile(fp);
508#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200509 if (sandbox)
510 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100511 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200512 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200513 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200514 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200515 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100516 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200517
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200518 pt->pt_func = fp;
519 pt->pt_refcount = 1;
520 rettv->vval.v_partial = pt;
521 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200522 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200523
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200524 eval_lavars_used = old_eval_lavars;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200525 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200526 return OK;
527
528errret:
529 ga_clear_strings(&newargs);
530 ga_clear_strings(&newlines);
531 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100532 vim_free(pt);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200533 vim_free(tofree);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200534 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200535 return FAIL;
536}
537
538/*
539 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
540 * name it contains, otherwise return "name".
541 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
542 * "partialp".
543 */
544 char_u *
545deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
546{
547 dictitem_T *v;
548 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200549 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200550
551 if (partialp != NULL)
552 *partialp = NULL;
553
554 cc = name[*lenp];
555 name[*lenp] = NUL;
556 v = find_var(name, NULL, no_autoload);
557 name[*lenp] = cc;
558 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
559 {
560 if (v->di_tv.vval.v_string == NULL)
561 {
562 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100563 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200564 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200565 s = v->di_tv.vval.v_string;
566 *lenp = (int)STRLEN(s);
567 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200568 }
569
570 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
571 {
572 partial_T *pt = v->di_tv.vval.v_partial;
573
574 if (pt == NULL)
575 {
576 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100577 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200578 }
579 if (partialp != NULL)
580 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200581 s = partial_name(pt);
582 *lenp = (int)STRLEN(s);
583 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200584 }
585
586 return name;
587}
588
589/*
590 * Give an error message with a function name. Handle <SNR> things.
591 * "ermsg" is to be passed without translation, use N_() instead of _().
592 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100593 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200594emsg_funcname(char *ermsg, char_u *name)
595{
596 char_u *p;
597
598 if (*name == K_SPECIAL)
599 p = concat_str((char_u *)"<SNR>", name + 3);
600 else
601 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100602 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200603 if (p != name)
604 vim_free(p);
605}
606
607/*
608 * Allocate a variable for the result of a function.
609 * Return OK or FAIL.
610 */
611 int
612get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200613 char_u *name, // name of the function
614 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200615 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200616 char_u **arg, // argument, pointing to the '('
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200617 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200618{
619 char_u *argp;
620 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100621 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
622 int argcount = 0; // number of arguments found
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200623 evalarg_T evalarg;
624
625 CLEAR_FIELD(evalarg);
626 evalarg.eval_flags = funcexe->evaluate ? EVAL_EVALUATE : 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200627
628 /*
629 * Get the arguments.
630 */
631 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200632 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
633 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200634 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100635 argp = skipwhite(argp + 1); // skip the '(' or ','
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200636 if (*argp == ')' || *argp == ',' || *argp == NUL)
637 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200638 if (eval1(&argp, &argvars[argcount], &evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200639 {
640 ret = FAIL;
641 break;
642 }
643 ++argcount;
644 if (*argp != ',')
645 break;
646 }
647 if (*argp == ')')
648 ++argp;
649 else
650 ret = FAIL;
651
652 if (ret == OK)
653 {
654 int i = 0;
655
656 if (get_vim_var_nr(VV_TESTING))
657 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100658 // Prepare for calling test_garbagecollect_now(), need to know
659 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200660 if (funcargs.ga_itemsize == 0)
661 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
662 for (i = 0; i < argcount; ++i)
663 if (ga_grow(&funcargs, 1) == OK)
664 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
665 &argvars[i];
666 }
667
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200668 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200669
670 funcargs.ga_len -= i;
671 }
672 else if (!aborting())
673 {
674 if (argcount == MAX_FUNC_ARGS)
675 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
676 else
677 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
678 }
679
680 while (--argcount >= 0)
681 clear_tv(&argvars[argcount]);
682
683 *arg = skipwhite(argp);
684 return ret;
685}
686
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200687/*
688 * Return TRUE if "p" starts with "<SID>" or "s:".
689 * Only works if eval_fname_script() returned non-zero for "p"!
690 */
691 static int
692eval_fname_sid(char_u *p)
693{
694 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
695}
696
697/*
698 * In a script change <SID>name() and s:name() to K_SNR 123_name().
699 * Change <SNR>123_name() to K_SNR 123_name().
700 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
701 * (slow).
702 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100703 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200704fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
705{
706 int llen;
707 char_u *fname;
708 int i;
709
710 llen = eval_fname_script(name);
711 if (llen > 0)
712 {
713 fname_buf[0] = K_SPECIAL;
714 fname_buf[1] = KS_EXTRA;
715 fname_buf[2] = (int)KE_SNR;
716 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100717 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200718 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200719 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100720 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200721 else
722 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200723 sprintf((char *)fname_buf + 3, "%ld_",
724 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200725 i = (int)STRLEN(fname_buf);
726 }
727 }
728 if (i + STRLEN(name + llen) < FLEN_FIXED)
729 {
730 STRCPY(fname_buf + i, name + llen);
731 fname = fname_buf;
732 }
733 else
734 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200735 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200736 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100737 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200738 else
739 {
740 *tofree = fname;
741 mch_memmove(fname, fname_buf, (size_t)i);
742 STRCPY(fname + i, name + llen);
743 }
744 }
745 }
746 else
747 fname = name;
748 return fname;
749}
750
751/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752 * Find a function "name" in script "sid".
753 */
754 static ufunc_T *
755find_func_with_sid(char_u *name, int sid)
756{
757 hashitem_T *hi;
758 char_u buffer[200];
759
760 buffer[0] = K_SPECIAL;
761 buffer[1] = KS_EXTRA;
762 buffer[2] = (int)KE_SNR;
763 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
764 (long)sid, name);
765 hi = hash_find(&func_hashtab, buffer);
766 if (!HASHITEM_EMPTY(hi))
767 return HI2UF(hi);
768
769 return NULL;
770}
771
772/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200773 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200774 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200775 * Return NULL for unknown function.
776 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 static ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200778find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200779{
780 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781 ufunc_T *func;
782 imported_T *imported;
783
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200784 if (!is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200786 char_u *after_script = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100787
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200788 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100789 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200790 // Find script-local function before global one.
791 func = find_func_with_sid(name, current_sctx.sc_sid);
792 if (func != NULL)
793 return func;
794 }
795
796 if (!in_vim9script()
797 && name[0] == K_SPECIAL
798 && name[1] == KS_EXTRA
799 && name[2] == KE_SNR)
800 {
801 long sid;
802
803 // Caller changes s: to <SNR>99_name.
804
805 after_script = name + 3;
806 sid = getdigits(&after_script);
807 if (sid == current_sctx.sc_sid && *after_script == '_')
808 ++after_script;
809 else
810 after_script = NULL;
811 }
812 if (in_vim9script() || after_script != NULL)
813 {
814 // Find imported function before global one.
815 imported = find_imported(
816 after_script == NULL ? name : after_script, 0, cctx);
817 if (imported != NULL && imported->imp_funcname != NULL)
818 {
819 hi = hash_find(&func_hashtab, imported->imp_funcname);
820 if (!HASHITEM_EMPTY(hi))
821 return HI2UF(hi);
822 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 }
824 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200825
Bram Moolenaara26b9702020-04-18 19:53:28 +0200826 hi = hash_find(&func_hashtab,
827 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200828 if (!HASHITEM_EMPTY(hi))
829 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830
831 return NULL;
832}
833
834/*
835 * Find a function by name, return pointer to it in ufuncs.
836 * "cctx" is passed in a :def function to find imported functions.
837 * Return NULL for unknown or dead function.
838 */
839 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200840find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100841{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200842 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843
844 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
845 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200846 return NULL;
847}
848
849/*
850 * Copy the function name of "fp" to buffer "buf".
851 * "buf" must be able to hold the function name plus three bytes.
852 * Takes care of script-local function names.
853 */
854 static void
855cat_func_name(char_u *buf, ufunc_T *fp)
856{
857 if (fp->uf_name[0] == K_SPECIAL)
858 {
859 STRCPY(buf, "<SNR>");
860 STRCAT(buf, fp->uf_name + 3);
861 }
862 else
863 STRCPY(buf, fp->uf_name);
864}
865
866/*
867 * Add a number variable "name" to dict "dp" with value "nr".
868 */
869 static void
870add_nr_var(
871 dict_T *dp,
872 dictitem_T *v,
873 char *name,
874 varnumber_T nr)
875{
876 STRCPY(v->di_key, name);
877 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
878 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
879 v->di_tv.v_type = VAR_NUMBER;
880 v->di_tv.v_lock = VAR_FIXED;
881 v->di_tv.vval.v_number = nr;
882}
883
884/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100885 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200886 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100887 static void
888free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200889{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100890 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200891
892 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
893 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100894 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200895
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100896 // When garbage collecting a funccall_T may be freed before the
897 // function that references it, clear its uf_scoped field.
898 // The function may have been redefined and point to another
899 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200900 if (fp != NULL && fp->uf_scoped == fc)
901 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200902 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200903 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200904
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200905 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200906 vim_free(fc);
907}
908
909/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100910 * Free "fc" and what it contains.
911 * Can be called only when "fc" is kept beyond the period of it called,
912 * i.e. after cleanup_function_call(fc).
913 */
914 static void
915free_funccal_contents(funccall_T *fc)
916{
917 listitem_T *li;
918
919 // Free all l: variables.
920 vars_clear(&fc->l_vars.dv_hashtab);
921
922 // Free all a: variables.
923 vars_clear(&fc->l_avars.dv_hashtab);
924
925 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200926 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100927 clear_tv(&li->li_tv);
928
929 free_funccal(fc);
930}
931
932/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200933 * Handle the last part of returning from a function: free the local hashtable.
934 * Unless it is still in use by a closure.
935 */
936 static void
937cleanup_function_call(funccall_T *fc)
938{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100939 int may_free_fc = fc->fc_refcount <= 0;
940 int free_fc = TRUE;
941
Bram Moolenaar6914c642017-04-01 21:21:30 +0200942 current_funccal = fc->caller;
943
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100944 // Free all l: variables if not referred.
945 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
946 vars_clear(&fc->l_vars.dv_hashtab);
947 else
948 free_fc = FALSE;
949
950 // If the a:000 list and the l: and a: dicts are not referenced and
951 // there is no closure using it, we can free the funccall_T and what's
952 // in it.
953 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
954 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200955 else
956 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100957 int todo;
958 hashitem_T *hi;
959 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200960
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100961 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200962
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100963 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200964 todo = (int)fc->l_avars.dv_hashtab.ht_used;
965 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
966 {
967 if (!HASHITEM_EMPTY(hi))
968 {
969 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100970 di = HI2DI(hi);
971 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200972 }
973 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100974 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200975
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100976 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
977 fc->l_varlist.lv_first = NULL;
978 else
979 {
980 listitem_T *li;
981
982 free_fc = FALSE;
983
984 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200985 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +0200986 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100987 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100988
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100989 if (free_fc)
990 free_funccal(fc);
991 else
992 {
993 static int made_copy = 0;
994
995 // "fc" is still in use. This can happen when returning "a:000",
996 // assigning "l:" to a global variable or defining a closure.
997 // Link "fc" in the list for garbage collection later.
998 fc->caller = previous_funccal;
999 previous_funccal = fc;
1000
1001 if (want_garbage_collect)
1002 // If garbage collector is ready, clear count.
1003 made_copy = 0;
1004 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001005 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001006 // We have made a lot of copies, worth 4 Mbyte. This can happen
1007 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001008 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001009 // too much memory.
1010 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001011 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001012 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001013 }
1014}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001015/*
1016 * Unreference "fc": decrement the reference count and free it when it
1017 * becomes zero. "fp" is detached from "fc".
1018 * When "force" is TRUE we are exiting.
1019 */
1020 static void
1021funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
1022{
1023 funccall_T **pfc;
1024 int i;
1025
1026 if (fc == NULL)
1027 return;
1028
1029 if (--fc->fc_refcount <= 0 && (force || (
1030 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
1031 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
1032 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
1033 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
1034 {
1035 if (fc == *pfc)
1036 {
1037 *pfc = fc->caller;
1038 free_funccal_contents(fc);
1039 return;
1040 }
1041 }
1042 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1043 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
1044 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
1045}
1046
1047/*
1048 * Remove the function from the function hashtable. If the function was
1049 * deleted while it still has references this was already done.
1050 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
1051 */
1052 static int
1053func_remove(ufunc_T *fp)
1054{
1055 hashitem_T *hi;
1056
1057 // Return if it was already virtually deleted.
1058 if (fp->uf_flags & FC_DEAD)
1059 return FALSE;
1060
1061 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1062 if (!HASHITEM_EMPTY(hi))
1063 {
1064 // When there is a def-function index do not actually remove the
1065 // function, so we can find the index when defining the function again.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001066 if (fp->uf_def_status == UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001067 fp->uf_flags |= FC_DEAD;
1068 else
1069 hash_remove(&func_hashtab, hi);
1070 return TRUE;
1071 }
1072 return FALSE;
1073}
1074
1075 static void
1076func_clear_items(ufunc_T *fp)
1077{
1078 ga_clear_strings(&(fp->uf_args));
1079 ga_clear_strings(&(fp->uf_def_args));
1080 ga_clear_strings(&(fp->uf_lines));
1081 VIM_CLEAR(fp->uf_name_exp);
1082 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001083 VIM_CLEAR(fp->uf_def_arg_idx);
1084 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001085 while (fp->uf_type_list.ga_len > 0)
1086 vim_free(((type_T **)fp->uf_type_list.ga_data)
1087 [--fp->uf_type_list.ga_len]);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 ga_clear(&fp->uf_type_list);
Bram Moolenaar801ab062020-06-25 19:27:56 +02001089
1090#ifdef FEAT_LUA
1091 if (fp->uf_cb_free != NULL)
1092 {
1093 fp->uf_cb_free(fp->uf_cb_state);
1094 fp->uf_cb_free = NULL;
1095 }
1096
1097 fp->uf_cb_state = NULL;
1098 fp->uf_cb = NULL;
1099#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001100#ifdef FEAT_PROFILE
1101 VIM_CLEAR(fp->uf_tml_count);
1102 VIM_CLEAR(fp->uf_tml_total);
1103 VIM_CLEAR(fp->uf_tml_self);
1104#endif
1105}
1106
1107/*
1108 * Free all things that a function contains. Does not free the function
1109 * itself, use func_free() for that.
1110 * When "force" is TRUE we are exiting.
1111 */
1112 static void
1113func_clear(ufunc_T *fp, int force)
1114{
1115 if (fp->uf_cleared)
1116 return;
1117 fp->uf_cleared = TRUE;
1118
1119 // clear this function
1120 func_clear_items(fp);
1121 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001122 clear_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123}
1124
1125/*
1126 * Free a function and remove it from the list of functions. Does not free
1127 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001128 * When "force" is TRUE we are exiting.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129 */
1130 static void
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001131func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132{
1133 // Only remove it when not done already, otherwise we would remove a newer
1134 // version of the function with the same name.
1135 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1136 func_remove(fp);
1137
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001138 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 vim_free(fp);
1140}
1141
1142/*
1143 * Free all things that a function contains and free the function itself.
1144 * When "force" is TRUE we are exiting.
1145 */
1146 static void
1147func_clear_free(ufunc_T *fp, int force)
1148{
1149 func_clear(fp, force);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001150 if (force || fp->uf_dfunc_idx == 0)
1151 func_free(fp, force);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152}
1153
Bram Moolenaar6914c642017-04-01 21:21:30 +02001154
1155/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001156 * Call a user function.
1157 */
1158 static void
1159call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001160 ufunc_T *fp, // pointer to function
1161 int argcount, // nr of args
1162 typval_T *argvars, // arguments
1163 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001164 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01001165 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001166{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001167 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001168 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001169 funccall_T *fc;
1170 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001171 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001172 static int depth = 0;
1173 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001174 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001175 int i;
1176 int ai;
1177 int islambda = FALSE;
1178 char_u numbuf[NUMBUFLEN];
1179 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001180#ifdef FEAT_PROFILE
1181 proftime_T wait_start;
1182 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001183 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001184#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001185 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001186
Bram Moolenaare38eab22019-12-05 21:50:01 +01001187 // If depth of calling is getting too high, don't execute the function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001188 if (depth >= p_mfd)
1189 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001190 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001191 rettv->v_type = VAR_NUMBER;
1192 rettv->vval.v_number = -1;
1193 return;
1194 }
1195 ++depth;
1196
Bram Moolenaare38eab22019-12-05 21:50:01 +01001197 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001198
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001199 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001200 if (fc == NULL)
1201 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001202 fc->caller = current_funccal;
1203 current_funccal = fc;
1204 fc->func = fp;
1205 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001206 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001207 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001208 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1209 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001210 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001211 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001212 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001213
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001214 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 {
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001216 estack_push_ufunc(fp, 1);
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001217 save_current_sctx = current_sctx;
1218 current_sctx = fp->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001220 // Execute the function, possibly compiling it first.
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001221 call_def_function(fp, argcount, argvars, funcexe->partial, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 --depth;
1223 current_funccal = fc->caller;
1224
1225 estack_pop();
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001226 current_sctx = save_current_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001227 free_funccal(fc);
1228 return;
1229 }
1230
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001231 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1232 islambda = TRUE;
1233
1234 /*
1235 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1236 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1237 * each argument variable and saves a lot of time.
1238 */
1239 /*
1240 * Init l: variables.
1241 */
1242 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1243 if (selfdict != NULL)
1244 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001245 // Set l:self to "selfdict". Use "name" to avoid a warning from
1246 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001247 v = &fc->fixvar[fixvar_idx++].var;
1248 name = v->di_key;
1249 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001250 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001251 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1252 v->di_tv.v_type = VAR_DICT;
1253 v->di_tv.v_lock = 0;
1254 v->di_tv.vval.v_dict = selfdict;
1255 ++selfdict->dv_refcount;
1256 }
1257
1258 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001259 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001260 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001261 * Set a:000 to a list with room for the "..." arguments.
1262 */
1263 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001264 if ((fp->uf_flags & FC_NOARGS) == 0)
1265 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001266 (varnumber_T)(argcount >= fp->uf_args.ga_len
1267 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001268 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001269 if ((fp->uf_flags & FC_NOARGS) == 0)
1270 {
1271 // Use "name" to avoid a warning from some compiler that checks the
1272 // destination size.
1273 v = &fc->fixvar[fixvar_idx++].var;
1274 name = v->di_key;
1275 STRCPY(name, "000");
1276 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1277 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1278 v->di_tv.v_type = VAR_LIST;
1279 v->di_tv.v_lock = VAR_FIXED;
1280 v->di_tv.vval.v_list = &fc->l_varlist;
1281 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001282 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001283 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1284 fc->l_varlist.lv_lock = VAR_FIXED;
1285
1286 /*
1287 * Set a:firstline to "firstline" and a:lastline to "lastline".
1288 * Set a:name to named arguments.
1289 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001290 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001291 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001292 if ((fp->uf_flags & FC_NOARGS) == 0)
1293 {
1294 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001295 (varnumber_T)funcexe->firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001296 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001297 (varnumber_T)funcexe->lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001298 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001299 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001300 {
1301 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001302 typval_T def_rettv;
1303 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001304
1305 ai = i - fp->uf_args.ga_len;
1306 if (ai < 0)
1307 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001308 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001309 name = FUNCARG(fp, i);
1310 if (islambda)
1311 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001312
1313 // evaluate named argument default expression
1314 isdefault = ai + fp->uf_def_args.ga_len >= 0
1315 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1316 && argvars[i].vval.v_number == VVAL_NONE));
1317 if (isdefault)
1318 {
1319 char_u *default_expr = NULL;
1320 def_rettv.v_type = VAR_NUMBER;
1321 def_rettv.vval.v_number = -1;
1322
1323 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1324 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001325 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001326 {
1327 default_arg_err = 1;
1328 break;
1329 }
1330 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001331 }
1332 else
1333 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001334 if ((fp->uf_flags & FC_NOARGS) != 0)
1335 // Bail out if no a: arguments used (in lambda).
1336 break;
1337
Bram Moolenaare38eab22019-12-05 21:50:01 +01001338 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001339 sprintf((char *)numbuf, "%d", ai + 1);
1340 name = numbuf;
1341 }
1342 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1343 {
1344 v = &fc->fixvar[fixvar_idx++].var;
1345 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001346 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001347 }
1348 else
1349 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001350 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001351 if (v == NULL)
1352 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001353 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001354 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001355
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001356 // Note: the values are copied directly to avoid alloc/free.
1357 // "argvars" must have VAR_FIXED for v_lock.
1358 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001359 v->di_tv.v_lock = VAR_FIXED;
1360
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001361 if (addlocal)
1362 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001363 // Named arguments should be accessed without the "a:" prefix in
1364 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001365 copy_tv(&v->di_tv, &v->di_tv);
1366 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001367 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001368 else
1369 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001370
1371 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1372 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001373 listitem_T *li = &fc->l_listitems[ai];
1374
1375 li->li_tv = argvars[i];
1376 li->li_tv.v_lock = VAR_FIXED;
1377 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001378 }
1379 }
1380
Bram Moolenaare38eab22019-12-05 21:50:01 +01001381 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001382 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001383
1384 if (fp->uf_flags & FC_SANDBOX)
1385 {
1386 using_sandbox = TRUE;
1387 ++sandbox;
1388 }
1389
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001390 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001391 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001392 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001393 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001394 ++no_wait_return;
1395 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001396
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001397 smsg(_("calling %s"), SOURCING_NAME);
1398 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001399 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001400 char_u buf[MSG_BUF_LEN];
1401 char_u numbuf2[NUMBUFLEN];
1402 char_u *tofree;
1403 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001404
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001405 msg_puts("(");
1406 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001407 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001408 if (i > 0)
1409 msg_puts(", ");
1410 if (argvars[i].v_type == VAR_NUMBER)
1411 msg_outnum((long)argvars[i].vval.v_number);
1412 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001413 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001414 // Do not want errors such as E724 here.
1415 ++emsg_off;
1416 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1417 --emsg_off;
1418 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001419 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001420 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001421 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001422 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1423 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001424 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001425 msg_puts((char *)s);
1426 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001427 }
1428 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001429 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001430 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001431 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001432 msg_puts("\n"); // don't overwrite this either
1433
1434 verbose_leave_scroll();
1435 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001436 }
1437#ifdef FEAT_PROFILE
1438 if (do_profiling == PROF_YES)
1439 {
1440 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001441 {
1442 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001443 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001444 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001445 if (fp->uf_profiling
1446 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1447 {
1448 ++fp->uf_tm_count;
1449 profile_start(&call_start);
1450 profile_zero(&fp->uf_tm_children);
1451 }
1452 script_prof_save(&wait_start);
1453 }
1454#endif
1455
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001456 save_current_sctx = current_sctx;
1457 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001458 save_did_emsg = did_emsg;
1459 did_emsg = FALSE;
1460
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001461 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1462 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001463 else if (islambda)
1464 {
1465 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1466
1467 // A Lambda always has the command "return {expr}". It is much faster
1468 // to evaluate {expr} directly.
1469 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001470 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001471 --ex_nesting_level;
1472 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001473 else
1474 // call do_cmdline() to execute the lines
1475 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001476 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1477
1478 --RedrawingDisabled;
1479
Bram Moolenaare38eab22019-12-05 21:50:01 +01001480 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001481 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1482 {
1483 clear_tv(rettv);
1484 rettv->v_type = VAR_NUMBER;
1485 rettv->vval.v_number = -1;
1486 }
1487
1488#ifdef FEAT_PROFILE
1489 if (do_profiling == PROF_YES && (fp->uf_profiling
1490 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1491 {
1492 profile_end(&call_start);
1493 profile_sub_wait(&wait_start, &call_start);
1494 profile_add(&fp->uf_tm_total, &call_start);
1495 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1496 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1497 {
1498 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1499 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1500 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001501 if (started_profiling)
1502 // make a ":profdel func" stop profiling the function
1503 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001504 }
1505#endif
1506
Bram Moolenaare38eab22019-12-05 21:50:01 +01001507 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001508 if (p_verbose >= 12)
1509 {
1510 ++no_wait_return;
1511 verbose_enter_scroll();
1512
1513 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001514 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001515 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001516 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001517 (long)fc->rettv->vval.v_number);
1518 else
1519 {
1520 char_u buf[MSG_BUF_LEN];
1521 char_u numbuf2[NUMBUFLEN];
1522 char_u *tofree;
1523 char_u *s;
1524
Bram Moolenaare38eab22019-12-05 21:50:01 +01001525 // The value may be very long. Skip the middle part, so that we
1526 // have some idea how it starts and ends. smsg() would always
1527 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001528 ++emsg_off;
1529 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1530 --emsg_off;
1531 if (s != NULL)
1532 {
1533 if (vim_strsize(s) > MSG_BUF_CLEN)
1534 {
1535 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1536 s = buf;
1537 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001538 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001539 vim_free(tofree);
1540 }
1541 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001542 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001543
1544 verbose_leave_scroll();
1545 --no_wait_return;
1546 }
1547
Bram Moolenaare31ee862020-01-07 20:59:34 +01001548 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001549 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001550 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001551#ifdef FEAT_PROFILE
1552 if (do_profiling == PROF_YES)
1553 script_prof_restore(&wait_start);
1554#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001555 if (using_sandbox)
1556 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001557
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001558 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001559 {
1560 ++no_wait_return;
1561 verbose_enter_scroll();
1562
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001563 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001564 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001565
1566 verbose_leave_scroll();
1567 --no_wait_return;
1568 }
1569
1570 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001571 --depth;
1572
Bram Moolenaar6914c642017-04-01 21:21:30 +02001573 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001574}
1575
1576/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001578 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579 int
1580call_user_func_check(
1581 ufunc_T *fp,
1582 int argcount,
1583 typval_T *argvars,
1584 typval_T *rettv,
1585 funcexe_T *funcexe,
1586 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001587{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 int error;
1589 int regular_args = fp->uf_args.ga_len;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1592 *funcexe->doesrange = TRUE;
1593 if (argcount < regular_args - fp->uf_def_args.ga_len)
1594 error = FCERR_TOOFEW;
1595 else if (!has_varargs(fp) && argcount > regular_args)
1596 error = FCERR_TOOMANY;
1597 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1598 error = FCERR_DICT;
1599 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001600 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 int did_save_redo = FALSE;
1602 save_redo_T save_redo;
1603
1604 /*
1605 * Call the user function.
1606 * Save and restore search patterns, script variables and
1607 * redo buffer.
1608 */
1609 save_search_patterns();
1610 if (!ins_compl_active())
1611 {
1612 saveRedobuff(&save_redo);
1613 did_save_redo = TRUE;
1614 }
1615 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001616 call_user_func(fp, argcount, argvars, rettv, funcexe,
1617 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1619 // Function was unreferenced while being used, free it now.
1620 func_clear_free(fp, FALSE);
1621 if (did_save_redo)
1622 restoreRedobuff(&save_redo);
1623 restore_search_patterns();
1624 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001625 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001627}
1628
1629/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001630 * There are two kinds of function names:
1631 * 1. ordinary names, function defined with :function
1632 * 2. numbered functions and lambdas
1633 * For the first we only count the name stored in func_hashtab as a reference,
1634 * using function() does not count as a reference, because the function is
1635 * looked up by name.
1636 */
1637 static int
1638func_name_refcount(char_u *name)
1639{
1640 return isdigit(*name) || *name == '<';
1641}
1642
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001643static funccal_entry_T *funccal_stack = NULL;
1644
1645/*
1646 * Save the current function call pointer, and set it to NULL.
1647 * Used when executing autocommands and for ":source".
1648 */
1649 void
1650save_funccal(funccal_entry_T *entry)
1651{
1652 entry->top_funccal = current_funccal;
1653 entry->next = funccal_stack;
1654 funccal_stack = entry;
1655 current_funccal = NULL;
1656}
1657
1658 void
1659restore_funccal(void)
1660{
1661 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001662 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001663 else
1664 {
1665 current_funccal = funccal_stack->top_funccal;
1666 funccal_stack = funccal_stack->next;
1667 }
1668}
1669
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001670 funccall_T *
1671get_current_funccal(void)
1672{
1673 return current_funccal;
1674}
1675
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001676/*
1677 * Mark all functions of script "sid" as deleted.
1678 */
1679 void
1680delete_script_functions(int sid)
1681{
1682 hashitem_T *hi;
1683 ufunc_T *fp;
1684 long_u todo;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02001685 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001686 size_t len;
1687
1688 buf[0] = K_SPECIAL;
1689 buf[1] = KS_EXTRA;
1690 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02001691 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001692 len = STRLEN(buf);
1693
1694 todo = func_hashtab.ht_used;
1695 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1696 if (!HASHITEM_EMPTY(hi))
1697 {
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001698 fp = HI2UF(hi);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001699 if (STRNCMP(fp->uf_name, buf, len) == 0)
1700 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001701 fp->uf_flags |= FC_DEAD;
1702 func_clear(fp, TRUE);
1703 }
1704 --todo;
1705 }
1706}
1707
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001708#if defined(EXITFREE) || defined(PROTO)
1709 void
1710free_all_functions(void)
1711{
1712 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001713 ufunc_T *fp;
1714 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001715 long_u todo = 1;
1716 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001717
Bram Moolenaare38eab22019-12-05 21:50:01 +01001718 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001719 while (current_funccal != NULL)
1720 {
1721 clear_tv(current_funccal->rettv);
1722 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001723 if (current_funccal == NULL && funccal_stack != NULL)
1724 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001725 }
1726
Bram Moolenaare38eab22019-12-05 21:50:01 +01001727 // First clear what the functions contain. Since this may lower the
1728 // reference count of a function, it may also free a function and change
1729 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001730 while (todo > 0)
1731 {
1732 todo = func_hashtab.ht_used;
1733 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1734 if (!HASHITEM_EMPTY(hi))
1735 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736 // clear the def function index now
1737 fp = HI2UF(hi);
1738 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001739 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740
Bram Moolenaare38eab22019-12-05 21:50:01 +01001741 // Only free functions that are not refcounted, those are
1742 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001743 if (func_name_refcount(fp->uf_name))
1744 ++skipped;
1745 else
1746 {
1747 used = func_hashtab.ht_used;
1748 func_clear(fp, TRUE);
1749 if (used != func_hashtab.ht_used)
1750 {
1751 skipped = 0;
1752 break;
1753 }
1754 }
1755 --todo;
1756 }
1757 }
1758
Bram Moolenaare38eab22019-12-05 21:50:01 +01001759 // Now actually free the functions. Need to start all over every time,
1760 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001761 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001762 while (func_hashtab.ht_used > skipped)
1763 {
1764 todo = func_hashtab.ht_used;
1765 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001766 if (!HASHITEM_EMPTY(hi))
1767 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001768 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001769 // Only free functions that are not refcounted, those are
1770 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02001771 fp = HI2UF(hi);
1772 if (func_name_refcount(fp->uf_name))
1773 ++skipped;
1774 else
1775 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001776 func_free(fp, FALSE);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001777 skipped = 0;
1778 break;
1779 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001780 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001781 }
1782 if (skipped == 0)
1783 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784
1785 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001786}
1787#endif
1788
1789/*
1790 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02001791 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001792 * "len" is the length of "name", or -1 for NUL terminated.
1793 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001795builtin_function(char_u *name, int len)
1796{
1797 char_u *p;
1798
Bram Moolenaara26b9702020-04-18 19:53:28 +02001799 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001800 return FALSE;
1801 p = vim_strchr(name, AUTOLOAD_CHAR);
1802 return p == NULL || (len > 0 && p > name + len);
1803}
1804
1805 int
1806func_call(
1807 char_u *name,
1808 typval_T *args,
1809 partial_T *partial,
1810 dict_T *selfdict,
1811 typval_T *rettv)
1812{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001813 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001814 listitem_T *item;
1815 typval_T argv[MAX_FUNC_ARGS + 1];
1816 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001817 int r = 0;
1818
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001819 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001820 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001821 {
1822 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1823 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001824 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001825 break;
1826 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001827 // Make a copy of each argument. This is needed to be able to set
1828 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001829 copy_tv(&item->li_tv, &argv[argc++]);
1830 }
1831
1832 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001833 {
1834 funcexe_T funcexe;
1835
Bram Moolenaara80faa82020-04-12 19:37:17 +02001836 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001837 funcexe.firstline = curwin->w_cursor.lnum;
1838 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001839 funcexe.evaluate = TRUE;
1840 funcexe.partial = partial;
1841 funcexe.selfdict = selfdict;
1842 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1843 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001844
Bram Moolenaare38eab22019-12-05 21:50:01 +01001845 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001846 while (argc > 0)
1847 clear_tv(&argv[--argc]);
1848
1849 return r;
1850}
1851
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001852static int callback_depth = 0;
1853
1854 int
1855get_callback_depth(void)
1856{
1857 return callback_depth;
1858}
1859
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001860/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001861 * Invoke call_func() with a callback.
1862 */
1863 int
1864call_callback(
1865 callback_T *callback,
1866 int len, // length of "name" or -1 to use strlen()
1867 typval_T *rettv, // return value goes here
1868 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001869 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001870 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001871{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001872 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001873 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001874
Bram Moolenaara80faa82020-04-12 19:37:17 +02001875 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001876 funcexe.evaluate = TRUE;
1877 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001878 ++callback_depth;
1879 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1880 --callback_depth;
1881 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001882}
1883
1884/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001885 * Give an error message for the result of a function.
1886 * Nothing if "error" is FCERR_NONE.
1887 */
1888 void
1889user_func_error(int error, char_u *name)
1890{
1891 switch (error)
1892 {
1893 case FCERR_UNKNOWN:
1894 emsg_funcname(e_unknownfunc, name);
1895 break;
1896 case FCERR_NOTMETHOD:
1897 emsg_funcname(
1898 N_("E276: Cannot use function as a method: %s"), name);
1899 break;
1900 case FCERR_DELETED:
1901 emsg_funcname(N_(e_func_deleted), name);
1902 break;
1903 case FCERR_TOOMANY:
1904 emsg_funcname((char *)e_toomanyarg, name);
1905 break;
1906 case FCERR_TOOFEW:
1907 emsg_funcname((char *)e_toofewarg, name);
1908 break;
1909 case FCERR_SCRIPT:
1910 emsg_funcname(
1911 N_("E120: Using <SID> not in a script context: %s"), name);
1912 break;
1913 case FCERR_DICT:
1914 emsg_funcname(
1915 N_("E725: Calling dict function without Dictionary: %s"),
1916 name);
1917 break;
1918 }
1919}
1920
1921/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001922 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001923 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001924 * Return FAIL when the function can't be called, OK otherwise.
1925 * Also returns OK when an error was encountered while executing the function.
1926 */
1927 int
1928call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001929 char_u *funcname, // name of the function
1930 int len, // length of "name" or -1 to use strlen()
1931 typval_T *rettv, // return value goes here
1932 int argcount_in, // number of "argvars"
1933 typval_T *argvars_in, // vars for arguments, must have "argcount"
1934 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001935 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001936{
1937 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01001938 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001939 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001940 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001941 char_u fname_buf[FLEN_FIXED + 1];
1942 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001943 char_u *fname = NULL;
1944 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001945 int argcount = argcount_in;
1946 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001947 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001948 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1949 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001950 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001951 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001952 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001953
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001954 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1955 // even when call_func() returns FAIL.
1956 rettv->v_type = VAR_UNKNOWN;
1957
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001958 if (partial != NULL)
1959 fp = partial->pt_func;
1960 if (fp == NULL)
1961 {
1962 // Make a copy of the name, if it comes from a funcref variable it
1963 // could be changed or deleted in the called function.
1964 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
1965 if (name == NULL)
1966 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001967
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001968 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1969 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001970
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001971 if (funcexe->doesrange != NULL)
1972 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001973
1974 if (partial != NULL)
1975 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001976 // When the function has a partial with a dict and there is a dict
1977 // argument, use the dict argument. That is backwards compatible.
1978 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001979 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001980 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01001981 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001982 {
1983 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001984 {
1985 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
1986 {
Bram Moolenaaref140542019-12-31 21:27:13 +01001987 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001988 goto theend;
1989 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001990 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001991 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001992 for (i = 0; i < argcount_in; ++i)
1993 argv[i + argv_clear] = argvars_in[i];
1994 argvars = argv;
1995 argcount = partial->pt_argc + argcount_in;
1996 }
1997 }
1998
Bram Moolenaaref140542019-12-31 21:27:13 +01001999 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002000 {
2001 char_u *rfname = fname;
2002
Bram Moolenaare38eab22019-12-05 21:50:01 +01002003 // Ignore "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002004 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002005 rfname = fname + 2;
2006
Bram Moolenaare38eab22019-12-05 21:50:01 +01002007 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002008 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01002009 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002010
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002011 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002012 {
2013 /*
2014 * User defined function.
2015 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002016 if (fp == NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002017 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002018
Bram Moolenaare38eab22019-12-05 21:50:01 +01002019 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002020 if (fp == NULL
2021 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002022 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002023 && !aborting())
2024 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002025 // executed an autocommand, search for the function again
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002026 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002027 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002028 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002029 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
2030 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002031 // loaded a package, search for the function again
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002032 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002033 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02002034 if (fp == NULL)
2035 {
2036 char_u *p = untrans_function_name(rfname);
2037
2038 // If using Vim9 script try not local to the script.
2039 // TODO: should not do this if the name started with "s:".
2040 if (p != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002041 fp = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002042 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002043
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002044 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01002045 error = FCERR_DELETED;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002046#ifdef FEAT_LUA
2047 else if (fp != NULL && (fp->uf_flags & FC_CFUNC))
2048 {
2049 cfunc_T cb = fp->uf_cb;
2050
2051 error = (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
2052 }
2053#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002054 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002055 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002056 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01002057 // postponed filling in the arguments, do it now
2058 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002059 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002060
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002061 if (funcexe->basetv != NULL)
2062 {
2063 // Method call: base->Method()
2064 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
2065 argv[0] = *funcexe->basetv;
2066 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002067 argvars = argv;
2068 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002069 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002070
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 error = call_user_func_check(fp, argcount, argvars, rettv,
2072 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002073 }
2074 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002075 else if (funcexe->basetv != NULL)
2076 {
2077 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002078 * expr->method(): Find the method name in the table, call its
2079 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02002080 */
2081 error = call_internal_method(fname, argcount, argvars, rettv,
2082 funcexe->basetv);
2083 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002084 else
2085 {
2086 /*
2087 * Find the function name in the table, call its implementation.
2088 */
2089 error = call_internal_func(fname, argcount, argvars, rettv);
2090 }
2091 /*
2092 * The function call (or "FuncUndefined" autocommand sequence) might
2093 * have been aborted by an error, an interrupt, or an explicitly thrown
2094 * exception that has not been caught so far. This situation can be
2095 * tested for by calling aborting(). For an error in an internal
2096 * function or for the "E132" error in call_user_func(), however, the
2097 * throw point at which the "force_abort" flag (temporarily reset by
2098 * emsg()) is normally updated has not been reached yet. We need to
2099 * update that flag first to make aborting() reliable.
2100 */
2101 update_force_abort();
2102 }
Bram Moolenaaref140542019-12-31 21:27:13 +01002103 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002104 ret = OK;
2105
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002106theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002107 /*
2108 * Report an error unless the argument evaluation or function call has been
2109 * cancelled due to an aborting error, an interrupt, or an exception.
2110 */
2111 if (!aborting())
2112 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002113 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002114 }
2115
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002116 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002117 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002118 clear_tv(&argv[--argv_clear + argv_base]);
2119
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002120 vim_free(tofree);
2121 vim_free(name);
2122
2123 return ret;
2124}
2125
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126 static char_u *
2127printable_func_name(ufunc_T *fp)
2128{
2129 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
2130}
2131
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002132/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002133 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002134 */
2135 static void
2136list_func_head(ufunc_T *fp, int indent)
2137{
2138 int j;
2139
2140 msg_start();
2141 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002142 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002143 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002144 msg_puts("def ");
2145 else
2146 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002147 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002148 msg_putchar('(');
2149 for (j = 0; j < fp->uf_args.ga_len; ++j)
2150 {
2151 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002152 msg_puts(", ");
2153 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154 if (fp->uf_arg_types != NULL)
2155 {
2156 char *tofree;
2157
2158 msg_puts(": ");
2159 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
2160 vim_free(tofree);
2161 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002162 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
2163 {
2164 msg_puts(" = ");
2165 msg_puts(((char **)(fp->uf_def_args.ga_data))
2166 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
2167 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002168 }
2169 if (fp->uf_varargs)
2170 {
2171 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002172 msg_puts(", ");
2173 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002174 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 if (fp->uf_va_name != NULL)
2176 {
2177 if (j)
2178 msg_puts(", ");
2179 msg_puts("...");
2180 msg_puts((char *)fp->uf_va_name);
2181 if (fp->uf_va_type)
2182 {
2183 char *tofree;
2184
2185 msg_puts(": ");
2186 msg_puts(type_name(fp->uf_va_type, &tofree));
2187 vim_free(tofree);
2188 }
2189 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002190 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002191
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002192 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002193 {
2194 if (fp->uf_ret_type != &t_void)
2195 {
2196 char *tofree;
2197
2198 msg_puts(": ");
2199 msg_puts(type_name(fp->uf_ret_type, &tofree));
2200 vim_free(tofree);
2201 }
2202 }
2203 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002204 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002205 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002206 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002207 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002208 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002209 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002210 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002211 msg_clr_eos();
2212 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002213 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002214}
2215
2216/*
2217 * Get a function name, translating "<SID>" and "<SNR>".
2218 * Also handles a Funcref in a List or Dictionary.
2219 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002220 * Set "*is_global" to TRUE when the function must be global, unless
2221 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002222 * flags:
2223 * TFN_INT: internal function name OK
2224 * TFN_QUIET: be quiet
2225 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002226 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002227 * Advances "pp" to just after the function name (if no error).
2228 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002229 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002230trans_function_name(
2231 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002232 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002233 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002234 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002235 funcdict_T *fdp, // return: info about dictionary used
2236 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002237{
2238 char_u *name = NULL;
2239 char_u *start;
2240 char_u *end;
2241 int lead;
2242 char_u sid_buf[20];
2243 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002244 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002245 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002247
2248 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002249 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002250 start = *pp;
2251
Bram Moolenaare38eab22019-12-05 21:50:01 +01002252 // Check for hard coded <SNR>: already translated function ID (from a user
2253 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002254 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2255 && (*pp)[2] == (int)KE_SNR)
2256 {
2257 *pp += 3;
2258 len = get_id_len(pp) + 3;
2259 return vim_strnsave(start, len);
2260 }
2261
Bram Moolenaare38eab22019-12-05 21:50:01 +01002262 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2263 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002264 lead = eval_fname_script(start);
2265 if (lead > 2)
2266 start += lead;
2267
Bram Moolenaare38eab22019-12-05 21:50:01 +01002268 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002269 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002270 lead > 2 ? 0 : FNE_CHECK_START);
2271 if (end == start)
2272 {
2273 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002274 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002275 goto theend;
2276 }
2277 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2278 {
2279 /*
2280 * Report an invalid expression in braces, unless the expression
2281 * evaluation has been cancelled due to an aborting error, an
2282 * interrupt, or an exception.
2283 */
2284 if (!aborting())
2285 {
2286 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002287 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002288 }
2289 else
2290 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2291 goto theend;
2292 }
2293
2294 if (lv.ll_tv != NULL)
2295 {
2296 if (fdp != NULL)
2297 {
2298 fdp->fd_dict = lv.ll_dict;
2299 fdp->fd_newkey = lv.ll_newkey;
2300 lv.ll_newkey = NULL;
2301 fdp->fd_di = lv.ll_di;
2302 }
2303 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2304 {
2305 name = vim_strsave(lv.ll_tv->vval.v_string);
2306 *pp = end;
2307 }
2308 else if (lv.ll_tv->v_type == VAR_PARTIAL
2309 && lv.ll_tv->vval.v_partial != NULL)
2310 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002311 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002312 *pp = end;
2313 if (partial != NULL)
2314 *partial = lv.ll_tv->vval.v_partial;
2315 }
2316 else
2317 {
2318 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2319 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002320 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002321 else
2322 *pp = end;
2323 name = NULL;
2324 }
2325 goto theend;
2326 }
2327
2328 if (lv.ll_name == NULL)
2329 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002330 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002331 *pp = end;
2332 goto theend;
2333 }
2334
Bram Moolenaare38eab22019-12-05 21:50:01 +01002335 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002336 if (lv.ll_exp_name != NULL)
2337 {
2338 len = (int)STRLEN(lv.ll_exp_name);
2339 name = deref_func_name(lv.ll_exp_name, &len, partial,
2340 flags & TFN_NO_AUTOLOAD);
2341 if (name == lv.ll_exp_name)
2342 name = NULL;
2343 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002344 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002345 {
2346 len = (int)(end - *pp);
2347 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2348 if (name == *pp)
2349 name = NULL;
2350 }
2351 if (name != NULL)
2352 {
2353 name = vim_strsave(name);
2354 *pp = end;
2355 if (STRNCMP(name, "<SNR>", 5) == 0)
2356 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002357 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002358 name[0] = K_SPECIAL;
2359 name[1] = KS_EXTRA;
2360 name[2] = (int)KE_SNR;
2361 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2362 }
2363 goto theend;
2364 }
2365
2366 if (lv.ll_exp_name != NULL)
2367 {
2368 len = (int)STRLEN(lv.ll_exp_name);
2369 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2370 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2371 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002372 // When there was "s:" already or the name expanded to get a
2373 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002374 lv.ll_name += 2;
2375 len -= 2;
2376 lead = 2;
2377 }
2378 }
2379 else
2380 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002381 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002382 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002383 {
2384 if (is_global != NULL && lv.ll_name[0] == 'g')
2385 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002386 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002387 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002388 len = (int)(end - lv.ll_name);
2389 }
2390
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 // In Vim9 script a user function is script-local by default.
2392 vim9script = ASCII_ISUPPER(*start)
2393 && current_sctx.sc_version == SCRIPT_VERSION_VIM9;
2394
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002395 /*
2396 * Copy the function name to allocated memory.
2397 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2398 * Accept <SNR>123_name() outside a script.
2399 */
2400 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002401 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002403 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404 if (!vim9script)
2405 lead = 3;
2406 if (vim9script || (lv.ll_exp_name != NULL
2407 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002408 || eval_fname_sid(*pp))
2409 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002411 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002412 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002413 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002414 goto theend;
2415 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002416 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 if (vim9script)
2418 extra = 3 + (int)STRLEN(sid_buf);
2419 else
2420 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002421 }
2422 }
2423 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2424 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002425 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002426 start);
2427 goto theend;
2428 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002429 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002430 {
2431 char_u *cp = vim_strchr(lv.ll_name, ':');
2432
2433 if (cp != NULL && cp < end)
2434 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002435 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002436 goto theend;
2437 }
2438 }
2439
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002441 if (name != NULL)
2442 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002443 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002444 {
2445 name[0] = K_SPECIAL;
2446 name[1] = KS_EXTRA;
2447 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002449 STRCPY(name + 3, sid_buf);
2450 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2452 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002453 }
2454 *pp = end;
2455
2456theend:
2457 clear_lval(&lv);
2458 return name;
2459}
2460
2461/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02002462 * Assuming "name" is the result of trans_function_name() and it was prefixed
2463 * to use the script-local name, return the unmodified name (points into
2464 * "name"). Otherwise return NULL.
2465 * This can be used to first search for a script-local function and fall back
2466 * to the global function if not found.
2467 */
2468 char_u *
2469untrans_function_name(char_u *name)
2470{
2471 char_u *p;
2472
2473 if (*name == K_SPECIAL && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2474 {
2475 p = vim_strchr(name, '_');
2476 if (p != NULL)
2477 return p + 1;
2478 }
2479 return NULL;
2480}
2481
2482/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002483 * List functions. When "regmatch" is NULL all of then.
2484 * Otherwise functions matching "regmatch".
2485 */
2486 static void
2487list_functions(regmatch_T *regmatch)
2488{
2489 long_u used = func_hashtab.ht_used;
2490 long_u todo = used;
2491 hashitem_T *ht_array = func_hashtab.ht_array;
2492 hashitem_T *hi;
2493
2494 for (hi = ht_array; todo > 0 && !got_int; ++hi)
2495 {
2496 if (!HASHITEM_EMPTY(hi))
2497 {
2498 ufunc_T *fp = HI2UF(hi);
2499
2500 --todo;
2501 if ((fp->uf_flags & FC_DEAD) == 0
2502 && (regmatch == NULL
2503 ? !message_filtered(fp->uf_name)
2504 && !func_name_refcount(fp->uf_name)
2505 : !isdigit(*fp->uf_name)
2506 && vim_regexec(regmatch, fp->uf_name, 0)))
2507 {
2508 list_func_head(fp, FALSE);
2509 if (used != func_hashtab.ht_used
2510 || ht_array != func_hashtab.ht_array)
2511 {
2512 emsg(_("E454: function list was modified"));
2513 return;
2514 }
2515 }
2516 }
2517 }
2518}
2519
2520/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02002521 * ":function" also supporting nested ":def".
2522 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002523 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02002524 ufunc_T *
Bram Moolenaar822ba242020-05-24 23:00:18 +02002525def_function(exarg_T *eap, char_u *name_arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002526{
2527 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002528 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002529 int j;
2530 int c;
2531 int saved_did_emsg;
2532 int saved_wait_return = need_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002533 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002534 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002535 char_u *p;
2536 char_u *arg;
2537 char_u *line_arg = NULL;
2538 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002540 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002541 garray_T newlines;
2542 int varargs = FALSE;
2543 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002545 ufunc_T *fp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002546 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002547 int indent;
2548 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549#define MAX_FUNC_NESTING 50
2550 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002551 dictitem_T *v;
2552 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002553 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002554 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002555 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002556 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002557 linenr_T sourcing_lnum_off;
2558 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002559 int is_heredoc = FALSE;
2560 char_u *skip_until = NULL;
2561 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002562
Bram Moolenaar822ba242020-05-24 23:00:18 +02002563 if (in_vim9script() && eap->forceit)
2564 {
2565 emsg(_(e_nobang));
2566 return NULL;
2567 }
2568
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002569 /*
2570 * ":function" without argument: list functions.
2571 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002572 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002573 {
2574 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002575 list_functions(NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002576 eap->nextcmd = check_nextcmd(eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02002577 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002578 }
2579
2580 /*
2581 * ":function /pat": list functions matching pattern.
2582 */
2583 if (*eap->arg == '/')
2584 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002585 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002586 if (!eap->skip)
2587 {
2588 regmatch_T regmatch;
2589
2590 c = *p;
2591 *p = NUL;
2592 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2593 *p = c;
2594 if (regmatch.regprog != NULL)
2595 {
2596 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002597 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002598 vim_regfree(regmatch.regprog);
2599 }
2600 }
2601 if (*p == '/')
2602 ++p;
2603 eap->nextcmd = check_nextcmd(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02002604 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002605 }
2606
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607 ga_init(&newargs);
2608 ga_init(&argtypes);
2609 ga_init(&default_args);
2610
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002611 /*
2612 * Get the function name. There are these situations:
2613 * func normal function name
2614 * "name" == func, "fudi.fd_dict" == NULL
2615 * dict.func new dictionary entry
2616 * "name" == NULL, "fudi.fd_dict" set,
2617 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2618 * dict.func existing dict entry with a Funcref
2619 * "name" == func, "fudi.fd_dict" set,
2620 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2621 * dict.func existing dict entry that's not a Funcref
2622 * "name" == NULL, "fudi.fd_dict" set,
2623 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2624 * s:func script-local function name
2625 * g:func global function name, same as "func"
2626 */
2627 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002628 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002629 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02002630 // nested function, argument is (args).
2631 paren = TRUE;
2632 CLEAR_FIELD(fudi);
2633 }
2634 else
2635 {
2636 name = trans_function_name(&p, &is_global, eap->skip,
2637 TFN_NO_AUTOLOAD, &fudi, NULL);
2638 paren = (vim_strchr(p, '(') != NULL);
2639 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002640 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02002641 /*
2642 * Return on an invalid expression in braces, unless the expression
2643 * evaluation has been cancelled due to an aborting error, an
2644 * interrupt, or an exception.
2645 */
2646 if (!aborting())
2647 {
2648 if (!eap->skip && fudi.fd_newkey != NULL)
2649 semsg(_(e_dictkey), fudi.fd_newkey);
2650 vim_free(fudi.fd_newkey);
2651 return NULL;
2652 }
2653 else
2654 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002655 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002656 }
2657
Bram Moolenaare38eab22019-12-05 21:50:01 +01002658 // An error in a function call during evaluation of an expression in magic
2659 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002660 saved_did_emsg = did_emsg;
2661 did_emsg = FALSE;
2662
2663 /*
2664 * ":function func" with only function name: list function.
2665 */
2666 if (!paren)
2667 {
2668 if (!ends_excmd(*skipwhite(p)))
2669 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002670 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002671 goto ret_free;
2672 }
2673 eap->nextcmd = check_nextcmd(p);
2674 if (eap->nextcmd != NULL)
2675 *p = NUL;
2676 if (!eap->skip && !got_int)
2677 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002678 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002679 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
2680 {
2681 char_u *up = untrans_function_name(name);
2682
2683 // With Vim9 script the name was made script-local, if not
2684 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02002685 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002686 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002687 }
2688
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002689 if (fp != NULL)
2690 {
2691 list_func_head(fp, TRUE);
2692 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2693 {
2694 if (FUNCLINE(fp, j) == NULL)
2695 continue;
2696 msg_putchar('\n');
2697 msg_outnum((long)(j + 1));
2698 if (j < 9)
2699 msg_putchar(' ');
2700 if (j < 99)
2701 msg_putchar(' ');
2702 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002703 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002704 ui_breakcheck();
2705 }
2706 if (!got_int)
2707 {
2708 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002709 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 msg_puts(" enddef");
2711 else
2712 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002713 }
2714 }
2715 else
Bram Moolenaara26b9702020-04-18 19:53:28 +02002716 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002717 }
2718 goto ret_free;
2719 }
2720
2721 /*
2722 * ":function name(arg1, arg2)" Define function.
2723 */
2724 p = skipwhite(p);
2725 if (*p != '(')
2726 {
2727 if (!eap->skip)
2728 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002729 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002730 goto ret_free;
2731 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002732 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002733 if (vim_strchr(p, '(') != NULL)
2734 p = vim_strchr(p, '(');
2735 }
2736 p = skipwhite(p + 1);
2737
2738 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2739
Bram Moolenaar04b12692020-05-04 23:24:44 +02002740 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002741 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002742 // Check the name of the function. Unless it's a dictionary function
2743 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002744 if (name != NULL)
2745 arg = name;
2746 else
2747 arg = fudi.fd_newkey;
2748 if (arg != NULL && (fudi.fd_di == NULL
2749 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2750 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2751 {
2752 if (*arg == K_SPECIAL)
2753 j = 3;
2754 else
2755 j = 0;
2756 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2757 : eval_isnamec(arg[j])))
2758 ++j;
2759 if (arg[j] != NUL)
2760 emsg_funcname((char *)e_invarg2, arg);
2761 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002762 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002763 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002764 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002765 }
2766
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002767 // This may get more lines and make the pointers into the first line
2768 // invalid.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002769 if (get_function_args(&p, ')', &newargs,
2770 eap->cmdidx == CMD_def ? &argtypes : NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002771 &varargs, &default_args, eap->skip,
2772 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002773 goto errret_2;
2774
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002776 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 // find the return type: :def Func(): type
2778 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002779 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 ret_type = skipwhite(p + 1);
2781 p = skip_type(ret_type);
2782 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002783 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002784 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002786 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002788 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 semsg(_("E1056: expected a type: %s"), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02002790 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002791 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002792 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002793 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 else
2795 // find extra arguments "range", "dict", "abort" and "closure"
2796 for (;;)
2797 {
2798 p = skipwhite(p);
2799 if (STRNCMP(p, "range", 5) == 0)
2800 {
2801 flags |= FC_RANGE;
2802 p += 5;
2803 }
2804 else if (STRNCMP(p, "dict", 4) == 0)
2805 {
2806 flags |= FC_DICT;
2807 p += 4;
2808 }
2809 else if (STRNCMP(p, "abort", 5) == 0)
2810 {
2811 flags |= FC_ABORT;
2812 p += 5;
2813 }
2814 else if (STRNCMP(p, "closure", 7) == 0)
2815 {
2816 flags |= FC_CLOSURE;
2817 p += 7;
2818 if (current_funccal == NULL)
2819 {
2820 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
2821 name == NULL ? (char_u *)"" : name);
2822 goto erret;
2823 }
2824 }
2825 else
2826 break;
2827 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002828
Bram Moolenaare38eab22019-12-05 21:50:01 +01002829 // When there is a line break use what follows for the function body.
2830 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002831 if (*p == '\n')
2832 line_arg = p + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002833 else if (*p != NUL && *p != '"' && !(eap->cmdidx == CMD_def && *p == '#')
2834 && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002835 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002836
2837 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
2839 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002840 */
2841 if (KeyTyped)
2842 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002843 // Check if the function already exists, don't let the user type the
2844 // whole function before telling him it doesn't work! For a script we
2845 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002846 if (!eap->skip && !eap->forceit)
2847 {
2848 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002849 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002850 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002851 emsg_funcname(e_funcexts, name);
2852 }
2853
2854 if (!eap->skip && did_emsg)
2855 goto erret;
2856
Bram Moolenaare38eab22019-12-05 21:50:01 +01002857 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002858 cmdline_row = msg_row;
2859 }
2860
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002861 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002862 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002863
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002864 indent = 2;
2865 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002867 for (;;)
2868 {
2869 if (KeyTyped)
2870 {
2871 msg_scroll = TRUE;
2872 saved_wait_return = FALSE;
2873 }
2874 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002875
2876 if (line_arg != NULL)
2877 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002878 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002879 theline = line_arg;
2880 p = vim_strchr(theline, '\n');
2881 if (p == NULL)
2882 line_arg += STRLEN(line_arg);
2883 else
2884 {
2885 *p = NUL;
2886 line_arg = p + 1;
2887 }
2888 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002889 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002890 {
2891 vim_free(line_to_free);
2892 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002893 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002894 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002895 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002896 line_to_free = theline;
2897 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002898 if (KeyTyped)
2899 lines_left = Rows - 1;
2900 if (theline == NULL)
2901 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 if (eap->cmdidx == CMD_def)
2903 emsg(_("E1057: Missing :enddef"));
2904 else
2905 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002906 goto erret;
2907 }
2908
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002909 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002910 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002911 if (SOURCING_LNUM < sourcing_lnum_off)
2912 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002913 else
2914 sourcing_lnum_off = 0;
2915
2916 if (skip_until != NULL)
2917 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002919 // * ":append" and "."
2920 // * ":python <<EOF" and "EOF"
2921 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2922 if (heredoc_trimmed == NULL
2923 || (is_heredoc && skipwhite(theline) == theline)
2924 || STRNCMP(theline, heredoc_trimmed,
2925 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002926 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002927 if (heredoc_trimmed == NULL)
2928 p = theline;
2929 else if (is_heredoc)
2930 p = skipwhite(theline) == theline
2931 ? theline : theline + STRLEN(heredoc_trimmed);
2932 else
2933 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002934 if (STRCMP(p, skip_until) == 0)
2935 {
2936 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002937 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002938 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002939 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002940 }
2941 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002942 }
2943 else
2944 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002945 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01002946 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002947 ;
2948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 // Check for "endfunction" or "enddef".
2950 if (checkforcmd(&p, nesting_def[nesting]
2951 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002952 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002953 char_u *nextcmd = NULL;
2954
Bram Moolenaar663bb232017-06-22 19:12:10 +02002955 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002956 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002957 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002958 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002959 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 give_warning2(eap->cmdidx == CMD_def
2961 ? (char_u *)_("W1001: Text found after :enddef: %s")
2962 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002963 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002964 if (nextcmd != NULL)
2965 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002966 // Another command follows. If the line came from "eap" we
2967 // can simply point into it, otherwise we need to change
2968 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02002969 eap->nextcmd = nextcmd;
2970 if (line_to_free != NULL)
2971 {
2972 vim_free(*eap->cmdlinep);
2973 *eap->cmdlinep = line_to_free;
2974 line_to_free = NULL;
2975 }
2976 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002977 break;
2978 }
2979
Bram Moolenaare38eab22019-12-05 21:50:01 +01002980 // Increase indent inside "if", "while", "for" and "try", decrease
2981 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002983 indent -= 2;
2984 else if (STRNCMP(p, "if", 2) == 0
2985 || STRNCMP(p, "wh", 2) == 0
2986 || STRNCMP(p, "for", 3) == 0
2987 || STRNCMP(p, "try", 3) == 0)
2988 indent += 2;
2989
Bram Moolenaare38eab22019-12-05 21:50:01 +01002990 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01002991 // Only recognize "def" inside "def", not inside "function",
2992 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01002994 if (checkforcmd(&p, "function", 2)
2995 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002996 {
2997 if (*p == '!')
2998 p = skipwhite(p + 1);
2999 p += eval_fname_script(p);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003000 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL, NULL));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003001 if (*skipwhite(p) == '(')
3002 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 if (nesting == MAX_FUNC_NESTING - 1)
3004 emsg(_("E1058: function nesting too deep"));
3005 else
3006 {
3007 ++nesting;
3008 nesting_def[nesting] = (c == 'd');
3009 indent += 2;
3010 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003011 }
3012 }
3013
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003014 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003015 p = skip_range(p, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003016 if (eap->cmdidx != CMD_def
3017 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01003018 || (p[0] == 'c'
3019 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
3020 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
3021 && (STRNCMP(&p[3], "nge", 3) != 0
3022 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003023 || (p[0] == 'i'
3024 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003025 && (!ASCII_ISALPHA(p[2])
3026 || (p[2] == 's'
3027 && (!ASCII_ISALPHA(p[3])
3028 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003029 skip_until = vim_strsave((char_u *)".");
3030
Bram Moolenaare38eab22019-12-05 21:50:01 +01003031 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003032 arg = skipwhite(skiptowhite(p));
3033 if (arg[0] == '<' && arg[1] =='<'
3034 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003035 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
3036 || ((p[2] == '3' || p[2] == 'x')
3037 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003038 || (p[0] == 'p' && p[1] == 'e'
3039 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
3040 || (p[0] == 't' && p[1] == 'c'
3041 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
3042 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
3043 && !ASCII_ISALPHA(p[3]))
3044 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
3045 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
3046 || (p[0] == 'm' && p[1] == 'z'
3047 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
3048 ))
3049 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003050 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003051 p = skipwhite(arg + 2);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003052 if (STRNCMP(p, "trim", 4) == 0)
3053 {
3054 // Ignore leading white space.
3055 p = skipwhite(p + 4);
3056 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003057 skipwhite(theline) - theline);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003058 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003059 if (*p == NUL)
3060 skip_until = vim_strsave((char_u *)".");
3061 else
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003062 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003063 do_concat = FALSE;
3064 is_heredoc = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003065 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003066
3067 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003068 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02003069 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003070 if (*arg == '[')
3071 arg = vim_strchr(arg, ']');
3072 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003073 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003074 arg = skipwhite(skiptowhite(arg));
3075 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
3076 && ((p[0] == 'l'
3077 && p[1] == 'e'
3078 && (!ASCII_ISALNUM(p[2])
3079 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02003080 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003081 p = skipwhite(arg + 3);
3082 if (STRNCMP(p, "trim", 4) == 0)
3083 {
3084 // Ignore leading white space.
3085 p = skipwhite(p + 4);
3086 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003087 skipwhite(theline) - theline);
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003088 }
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003089 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003090 do_concat = FALSE;
3091 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003092 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003093 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003094 }
3095
Bram Moolenaare38eab22019-12-05 21:50:01 +01003096 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003097 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003098 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003099
Bram Moolenaare38eab22019-12-05 21:50:01 +01003100 // Copy the line to newly allocated memory. get_one_sourceline()
3101 // allocates 250 bytes per line, this saves 80% on average. The cost
3102 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003103 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003104 if (p == NULL)
3105 goto erret;
3106 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003107
Bram Moolenaare38eab22019-12-05 21:50:01 +01003108 // Add NULL lines for continuation lines, so that the line count is
3109 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003110 while (sourcing_lnum_off-- > 0)
3111 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3112
Bram Moolenaare38eab22019-12-05 21:50:01 +01003113 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003114 if (line_arg != NULL && *line_arg == NUL)
3115 line_arg = NULL;
3116 }
3117
Bram Moolenaare38eab22019-12-05 21:50:01 +01003118 // Don't define the function when skipping commands or when an error was
3119 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003120 if (eap->skip || did_emsg)
3121 goto erret;
3122
3123 /*
3124 * If there are no errors, add the function
3125 */
3126 if (fudi.fd_dict == NULL)
3127 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 hashtab_T *ht;
3129
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003130 v = find_var(name, &ht, FALSE);
3131 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
3132 {
3133 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
3134 name);
3135 goto erret;
3136 }
3137
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003138 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003139 if (fp != NULL)
3140 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 int dead = fp->uf_flags & FC_DEAD;
3142
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003143 // Function can be replaced with "function!" and when sourcing the
3144 // same script again, but only once.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 if (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003146 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
3147 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003148 {
3149 emsg_funcname(e_funcexts, name);
3150 goto erret;
3151 }
3152 if (fp->uf_calls > 0)
3153 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003154 emsg_funcname(
3155 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003156 name);
3157 goto erret;
3158 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003159 if (fp->uf_refcount > 1)
3160 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003161 // This function is referenced somewhere, don't redefine it but
3162 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003163 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003164 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003165 fp = NULL;
3166 overwrite = TRUE;
3167 }
3168 else
3169 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003170 char_u *exp_name = fp->uf_name_exp;
3171
3172 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01003173 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003174 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003175 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003176 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003178#ifdef FEAT_PROFILE
3179 fp->uf_profiling = FALSE;
3180 fp->uf_prof_initialized = FALSE;
3181#endif
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003182 clear_def_function(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003183 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003184 }
3185 }
3186 else
3187 {
3188 char numbuf[20];
3189
3190 fp = NULL;
3191 if (fudi.fd_newkey == NULL && !eap->forceit)
3192 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003193 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003194 goto erret;
3195 }
3196 if (fudi.fd_di == NULL)
3197 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003198 // Can't add a function to a locked dictionary
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003199 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003200 goto erret;
3201 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003202 // Can't change an existing function if it is locked
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003203 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003204 goto erret;
3205
Bram Moolenaare38eab22019-12-05 21:50:01 +01003206 // Give the function a sequential number. Can only be used with a
3207 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003208 vim_free(name);
3209 sprintf(numbuf, "%d", ++func_nr);
3210 name = vim_strsave((char_u *)numbuf);
3211 if (name == NULL)
3212 goto erret;
3213 }
3214
3215 if (fp == NULL)
3216 {
3217 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3218 {
3219 int slen, plen;
3220 char_u *scriptname;
3221
Bram Moolenaare38eab22019-12-05 21:50:01 +01003222 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003223 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003224 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003225 {
3226 scriptname = autoload_name(name);
3227 if (scriptname != NULL)
3228 {
3229 p = vim_strchr(scriptname, '/');
3230 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003231 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003232 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003233 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003234 j = OK;
3235 vim_free(scriptname);
3236 }
3237 }
3238 if (j == FAIL)
3239 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003240 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003241 goto erret;
3242 }
3243 }
3244
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003245 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003246 if (fp == NULL)
3247 goto erret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003248 fp->uf_def_status = eap->cmdidx == CMD_def ? UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003249 : UF_NOT_COMPILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003250
3251 if (fudi.fd_dict != NULL)
3252 {
3253 if (fudi.fd_di == NULL)
3254 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003255 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003256 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3257 if (fudi.fd_di == NULL)
3258 {
3259 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003260 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003261 goto erret;
3262 }
3263 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3264 {
3265 vim_free(fudi.fd_di);
3266 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003267 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003268 goto erret;
3269 }
3270 }
3271 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003272 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003273 clear_tv(&fudi.fd_di->di_tv);
3274 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003275 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003276
Bram Moolenaare38eab22019-12-05 21:50:01 +01003277 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003278 flags |= FC_DICT;
3279 }
3280
Bram Moolenaare38eab22019-12-05 21:50:01 +01003281 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003282 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003283 if (overwrite)
3284 {
3285 hi = hash_find(&func_hashtab, name);
3286 hi->hi_key = UF2HIKEY(fp);
3287 }
3288 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003289 {
3290 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003291 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003292 goto erret;
3293 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003294 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003295 }
3296 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003297 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003299 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300
3301 if (eap->cmdidx == CMD_def)
3302 {
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003303 int lnum_save = SOURCING_LNUM;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003304
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003305 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003306
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003307 // error messages are for the first function line
3308 SOURCING_LNUM = sourcing_lnum_top;
3309
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 // parse the argument types
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003311 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312
3313 if (argtypes.ga_len > 0)
3314 {
3315 // When "varargs" is set the last name/type goes into uf_va_name
3316 // and uf_va_type.
3317 int len = argtypes.ga_len - (varargs ? 1 : 0);
3318
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003319 if (len > 0)
3320 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 if (fp->uf_arg_types != NULL)
3322 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003323 int i;
3324 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325
3326 for (i = 0; i < len; ++ i)
3327 {
3328 p = ((char_u **)argtypes.ga_data)[i];
3329 if (p == NULL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003330 // will get the type from the default value
3331 type = &t_unknown;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 else
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003333 type = parse_type(&p, &fp->uf_type_list);
3334 if (type == NULL)
3335 {
3336 SOURCING_LNUM = lnum_save;
3337 goto errret_2;
3338 }
3339 fp->uf_arg_types[i] = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 }
3341 }
3342 if (varargs)
3343 {
3344 // Move the last argument "...name: type" to uf_va_name and
3345 // uf_va_type.
3346 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
3347 [fp->uf_args.ga_len - 1];
3348 --fp->uf_args.ga_len;
3349 p = ((char_u **)argtypes.ga_data)[len];
3350 if (p == NULL)
3351 // todo: get type from default value
3352 fp->uf_va_type = &t_any;
3353 else
3354 fp->uf_va_type = parse_type(&p, &fp->uf_type_list);
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003355 if (fp->uf_va_type == NULL)
3356 {
3357 SOURCING_LNUM = lnum_save;
3358 goto errret_2;
3359 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 }
3361 varargs = FALSE;
3362 }
3363
3364 // parse the return type, if any
3365 if (ret_type == NULL)
3366 fp->uf_ret_type = &t_void;
3367 else
3368 {
3369 p = ret_type;
3370 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list);
3371 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02003372 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003374 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003375 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003377 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003378 if ((flags & FC_CLOSURE) != 0)
3379 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003380 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003381 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003382 }
3383 else
3384 fp->uf_scoped = NULL;
3385
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003386#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003387 if (prof_def_func())
3388 func_do_profile(fp);
3389#endif
3390 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003391 if (sandbox)
3392 flags |= FC_SANDBOX;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003393 if (in_vim9script() && !ASCII_ISUPPER(*fp->uf_name))
3394 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003395 fp->uf_flags = flags;
3396 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003397 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003398 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003399 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 if (is_export)
3401 {
3402 fp->uf_flags |= FC_EXPORT;
3403 // let ex_export() know the export worked.
3404 is_export = FALSE;
3405 }
3406
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003407 if (eap->cmdidx == CMD_def)
3408 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02003409 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
3410 // :func does not use Vim9 script syntax, even in a Vim9 script file
3411 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003412
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003413 goto ret_free;
3414
3415erret:
3416 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003417 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003418errret_2:
3419 ga_clear_strings(&newlines);
3420ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003421 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003422 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003423 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003424 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003425 if (name != name_arg)
3426 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003427 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003428 did_emsg |= saved_did_emsg;
3429 need_wait_return |= saved_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003430
3431 return fp;
3432}
3433
3434/*
3435 * ":function"
3436 */
3437 void
3438ex_function(exarg_T *eap)
3439{
Bram Moolenaar822ba242020-05-24 23:00:18 +02003440 (void)def_function(eap, NULL);
3441}
3442
3443/*
3444 * :defcompile - compile all :def functions in the current script.
3445 */
3446 void
3447ex_defcompile(exarg_T *eap UNUSED)
3448{
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003449 long_u ht_used = func_hashtab.ht_used;
3450 int todo = (int)ht_used;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003451 hashitem_T *hi;
3452 ufunc_T *ufunc;
3453
3454 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
3455 {
3456 if (!HASHITEM_EMPTY(hi))
3457 {
3458 --todo;
3459 ufunc = HI2UF(hi);
3460 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003461 && ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003462 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02003463 compile_def_function(ufunc, FALSE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003464
3465 if (func_hashtab.ht_used != ht_used)
3466 {
3467 // another function has been defined, need to start over
3468 hi = func_hashtab.ht_array;
3469 ht_used = func_hashtab.ht_used;
3470 todo = (int)ht_used;
Bram Moolenaar285b1892020-05-26 11:37:26 +02003471 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003472 }
3473 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003474 }
3475 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003476}
3477
3478/*
3479 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3480 * Return 2 if "p" starts with "s:".
3481 * Return 0 otherwise.
3482 */
3483 int
3484eval_fname_script(char_u *p)
3485{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003486 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3487 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003488 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3489 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3490 return 5;
3491 if (p[0] == 's' && p[1] == ':')
3492 return 2;
3493 return 0;
3494}
3495
3496 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003497translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003498{
3499 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003500 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003501 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502}
3503
3504/*
3505 * Return TRUE when "ufunc" has old-style "..." varargs
3506 * or named varargs "...name: type".
3507 */
3508 int
3509has_varargs(ufunc_T *ufunc)
3510{
3511 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003512}
3513
3514/*
3515 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003516 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003517 */
3518 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003519function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003520{
3521 char_u *nm = name;
3522 char_u *p;
3523 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003524 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003525 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003526
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003527 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3528 if (no_deref)
3529 flag |= TFN_NO_DEREF;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003530 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003531 nm = skipwhite(nm);
3532
Bram Moolenaare38eab22019-12-05 21:50:01 +01003533 // Only accept "funcname", "funcname ", "funcname (..." and
3534 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003535 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003536 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003537 vim_free(p);
3538 return n;
3539}
3540
Bram Moolenaar113e1072019-01-20 15:30:40 +01003541#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003542 char_u *
3543get_expanded_name(char_u *name, int check)
3544{
3545 char_u *nm = name;
3546 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003547 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003548
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003549 p = trans_function_name(&nm, &is_global, FALSE,
3550 TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003551
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003552 if (p != NULL && *nm == NUL
3553 && (!check || translated_function_exists(p, is_global)))
3554 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003555
3556 vim_free(p);
3557 return NULL;
3558}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003559#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003560
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003561/*
3562 * Function given to ExpandGeneric() to obtain the list of user defined
3563 * function names.
3564 */
3565 char_u *
3566get_user_func_name(expand_T *xp, int idx)
3567{
3568 static long_u done;
3569 static hashitem_T *hi;
3570 ufunc_T *fp;
3571
3572 if (idx == 0)
3573 {
3574 done = 0;
3575 hi = func_hashtab.ht_array;
3576 }
3577 if (done < func_hashtab.ht_used)
3578 {
3579 if (done++ > 0)
3580 ++hi;
3581 while (HASHITEM_EMPTY(hi))
3582 ++hi;
3583 fp = HI2UF(hi);
3584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 // don't show dead, dict and lambda functions
3586 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02003587 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003588 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003589
3590 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003591 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003592
3593 cat_func_name(IObuff, fp);
3594 if (xp->xp_context != EXPAND_USER_FUNC)
3595 {
3596 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003598 STRCAT(IObuff, ")");
3599 }
3600 return IObuff;
3601 }
3602 return NULL;
3603}
3604
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003605/*
3606 * ":delfunction {name}"
3607 */
3608 void
3609ex_delfunction(exarg_T *eap)
3610{
3611 ufunc_T *fp = NULL;
3612 char_u *p;
3613 char_u *name;
3614 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003615 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003616
3617 p = eap->arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003618 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003619 vim_free(fudi.fd_newkey);
3620 if (name == NULL)
3621 {
3622 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003623 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003624 return;
3625 }
3626 if (!ends_excmd(*skipwhite(p)))
3627 {
3628 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003629 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003630 return;
3631 }
3632 eap->nextcmd = check_nextcmd(p);
3633 if (eap->nextcmd != NULL)
3634 *p = NUL;
3635
3636 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003637 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003638 vim_free(name);
3639
3640 if (!eap->skip)
3641 {
3642 if (fp == NULL)
3643 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02003644 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003645 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003646 return;
3647 }
3648 if (fp->uf_calls > 0)
3649 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003650 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003651 return;
3652 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003653 if (fp->uf_flags & FC_VIM9)
3654 {
3655 semsg(_("E1084: Cannot delete Vim9 script function %s"), eap->arg);
3656 return;
3657 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003658
3659 if (fudi.fd_dict != NULL)
3660 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003661 // Delete the dict item that refers to the function, it will
3662 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003663 dictitem_remove(fudi.fd_dict, fudi.fd_di);
3664 }
3665 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003666 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003667 // A normal function (not a numbered function or lambda) has a
3668 // refcount of 1 for the entry in the hashtable. When deleting
3669 // it and the refcount is more than one, it should be kept.
3670 // A numbered function and lambda should be kept if the refcount is
3671 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003672 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003673 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003674 // Function is still referenced somewhere. Don't free it but
3675 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003676 if (func_remove(fp))
3677 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003678 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003679 }
3680 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003681 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003682 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003683 }
3684}
3685
3686/*
3687 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003688 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003689 */
3690 void
3691func_unref(char_u *name)
3692{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003693 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003694
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003695 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003696 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003697 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003698 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003699 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003700#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003701 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003702#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01003703 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003704 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003705 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003706 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003707 // Only delete it when it's not being used. Otherwise it's done
3708 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003709 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003710 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02003711 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003712}
3713
3714/*
3715 * Unreference a Function: decrement the reference count and free it when it
3716 * becomes zero.
3717 */
3718 void
3719func_ptr_unref(ufunc_T *fp)
3720{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003721 if (fp != NULL && --fp->uf_refcount <= 0)
3722 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003723 // Only delete it when it's not being used. Otherwise it's done
3724 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02003725 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003726 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003727 }
3728}
3729
3730/*
3731 * Count a reference to a Function.
3732 */
3733 void
3734func_ref(char_u *name)
3735{
3736 ufunc_T *fp;
3737
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003738 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003739 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003740 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003741 if (fp != NULL)
3742 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003743 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01003744 // Only give an error for a numbered function.
3745 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01003746 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003747}
3748
3749/*
3750 * Count a reference to a Function.
3751 */
3752 void
3753func_ptr_ref(ufunc_T *fp)
3754{
3755 if (fp != NULL)
3756 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003757}
3758
3759/*
3760 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3761 * referenced from anywhere that is in use.
3762 */
3763 static int
3764can_free_funccal(funccall_T *fc, int copyID)
3765{
3766 return (fc->l_varlist.lv_copyID != copyID
3767 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003768 && fc->l_avars.dv_copyID != copyID
3769 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003770}
3771
3772/*
3773 * ":return [expr]"
3774 */
3775 void
3776ex_return(exarg_T *eap)
3777{
3778 char_u *arg = eap->arg;
3779 typval_T rettv;
3780 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003781 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003782
3783 if (current_funccal == NULL)
3784 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003785 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003786 return;
3787 }
3788
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003789 CLEAR_FIELD(evalarg);
3790 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
3791
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003792 if (eap->skip)
3793 ++emsg_skip;
3794
3795 eap->nextcmd = NULL;
3796 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003797 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003798 {
3799 if (!eap->skip)
3800 returning = do_return(eap, FALSE, TRUE, &rettv);
3801 else
3802 clear_tv(&rettv);
3803 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003804 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003805 else if (!eap->skip)
3806 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003807 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003808 update_force_abort();
3809
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003810 /*
3811 * Return unless the expression evaluation has been cancelled due to an
3812 * aborting error, an interrupt, or an exception.
3813 */
3814 if (!aborting())
3815 returning = do_return(eap, FALSE, TRUE, NULL);
3816 }
3817
Bram Moolenaare38eab22019-12-05 21:50:01 +01003818 // When skipping or the return gets pending, advance to the next command
3819 // in this line (!returning). Otherwise, ignore the rest of the line.
3820 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003821 if (returning)
3822 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003823 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003824 eap->nextcmd = check_nextcmd(arg);
3825
3826 if (eap->skip)
3827 --emsg_skip;
3828}
3829
3830/*
3831 * ":1,25call func(arg1, arg2)" function call.
3832 */
3833 void
3834ex_call(exarg_T *eap)
3835{
3836 char_u *arg = eap->arg;
3837 char_u *startarg;
3838 char_u *name;
3839 char_u *tofree;
3840 int len;
3841 typval_T rettv;
3842 linenr_T lnum;
3843 int doesrange;
3844 int failed = FALSE;
3845 funcdict_T fudi;
3846 partial_T *partial = NULL;
3847
3848 if (eap->skip)
3849 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003850 // trans_function_name() doesn't work well when skipping, use eval0()
3851 // instead to skip to any following command, e.g. for:
3852 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003853 ++emsg_skip;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003854 if (eval0(eap->arg, &rettv, eap, NULL) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003855 clear_tv(&rettv);
3856 --emsg_skip;
3857 return;
3858 }
3859
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003860 tofree = trans_function_name(&arg, NULL, eap->skip,
3861 TFN_INT, &fudi, &partial);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003862 if (fudi.fd_newkey != NULL)
3863 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003864 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003865 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003866 vim_free(fudi.fd_newkey);
3867 }
3868 if (tofree == NULL)
3869 return;
3870
Bram Moolenaare38eab22019-12-05 21:50:01 +01003871 // Increase refcount on dictionary, it could get deleted when evaluating
3872 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003873 if (fudi.fd_dict != NULL)
3874 ++fudi.fd_dict->dv_refcount;
3875
Bram Moolenaare38eab22019-12-05 21:50:01 +01003876 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3877 // contents. For VAR_PARTIAL get its partial, unless we already have one
3878 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003879 len = (int)STRLEN(tofree);
3880 name = deref_func_name(tofree, &len,
3881 partial != NULL ? NULL : &partial, FALSE);
3882
Bram Moolenaare38eab22019-12-05 21:50:01 +01003883 // Skip white space to allow ":call func ()". Not good, but required for
3884 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003885 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003886 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003887
3888 if (*startarg != '(')
3889 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003891 goto end;
3892 }
3893
3894 /*
3895 * When skipping, evaluate the function once, to find the end of the
3896 * arguments.
3897 * When the function takes a range, this is discovered after the first
3898 * call, and the loop is broken.
3899 */
3900 if (eap->skip)
3901 {
3902 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003903 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003904 }
3905 else
3906 lnum = eap->line1;
3907 for ( ; lnum <= eap->line2; ++lnum)
3908 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003909 funcexe_T funcexe;
3910
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003911 if (!eap->skip && eap->addr_count > 0)
3912 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003913 if (lnum > curbuf->b_ml.ml_line_count)
3914 {
3915 // If the function deleted lines or switched to another buffer
3916 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003917 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003918 break;
3919 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003920 curwin->w_cursor.lnum = lnum;
3921 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003922 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003923 }
3924 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003925
Bram Moolenaara80faa82020-04-12 19:37:17 +02003926 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003927 funcexe.firstline = eap->line1;
3928 funcexe.lastline = eap->line2;
3929 funcexe.doesrange = &doesrange;
3930 funcexe.evaluate = !eap->skip;
3931 funcexe.partial = partial;
3932 funcexe.selfdict = fudi.fd_dict;
3933 if (get_func_tv(name, -1, &rettv, &arg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003934 {
3935 failed = TRUE;
3936 break;
3937 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003938 if (has_watchexpr())
3939 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003940
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003941 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003942 if (handle_subscript(&arg, &rettv,
3943 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003944 {
3945 failed = TRUE;
3946 break;
3947 }
3948
3949 clear_tv(&rettv);
3950 if (doesrange || eap->skip)
3951 break;
3952
Bram Moolenaare38eab22019-12-05 21:50:01 +01003953 // Stop when immediately aborting on error, or when an interrupt
3954 // occurred or an exception was thrown but not caught.
3955 // get_func_tv() returned OK, so that the check for trailing
3956 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003957 if (aborting())
3958 break;
3959 }
3960 if (eap->skip)
3961 --emsg_skip;
3962
Bram Moolenaare51bb172020-02-16 19:42:23 +01003963 // When inside :try we need to check for following "| catch".
3964 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003965 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003966 // Check for trailing illegal characters and a following command.
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003967 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003968 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01003969 if (!failed)
3970 {
3971 emsg_severe = TRUE;
3972 emsg(_(e_trailing));
3973 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003974 }
3975 else
3976 eap->nextcmd = check_nextcmd(arg);
3977 }
3978
3979end:
3980 dict_unref(fudi.fd_dict);
3981 vim_free(tofree);
3982}
3983
3984/*
3985 * Return from a function. Possibly makes the return pending. Also called
3986 * for a pending return at the ":endtry" or after returning from an extra
3987 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3988 * when called due to a ":return" command. "rettv" may point to a typval_T
3989 * with the return rettv. Returns TRUE when the return can be carried out,
3990 * FALSE when the return gets pending.
3991 */
3992 int
3993do_return(
3994 exarg_T *eap,
3995 int reanimate,
3996 int is_cmd,
3997 void *rettv)
3998{
3999 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01004000 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004001
4002 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004003 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004004 current_funccal->returned = FALSE;
4005
4006 /*
4007 * Cleanup (and inactivate) conditionals, but stop when a try conditional
4008 * not in its finally clause (which then is to be executed next) is found.
4009 * In this case, make the ":return" pending for execution at the ":endtry".
4010 * Otherwise, return normally.
4011 */
4012 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
4013 if (idx >= 0)
4014 {
4015 cstack->cs_pending[idx] = CSTP_RETURN;
4016
4017 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004018 // A pending return again gets pending. "rettv" points to an
4019 // allocated variable with the rettv of the original ":return"'s
4020 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004021 cstack->cs_rettv[idx] = rettv;
4022 else
4023 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004024 // When undoing a return in order to make it pending, get the stored
4025 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004026 if (reanimate)
4027 rettv = current_funccal->rettv;
4028
4029 if (rettv != NULL)
4030 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004031 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004032 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
4033 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
4034 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004035 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004036 }
4037 else
4038 cstack->cs_rettv[idx] = NULL;
4039
4040 if (reanimate)
4041 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004042 // The pending return value could be overwritten by a ":return"
4043 // without argument in a finally clause; reset the default
4044 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004045 current_funccal->rettv->v_type = VAR_NUMBER;
4046 current_funccal->rettv->vval.v_number = 0;
4047 }
4048 }
4049 report_make_pending(CSTP_RETURN, rettv);
4050 }
4051 else
4052 {
4053 current_funccal->returned = TRUE;
4054
Bram Moolenaare38eab22019-12-05 21:50:01 +01004055 // If the return is carried out now, store the return value. For
4056 // a return immediately after reanimation, the value is already
4057 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004058 if (!reanimate && rettv != NULL)
4059 {
4060 clear_tv(current_funccal->rettv);
4061 *current_funccal->rettv = *(typval_T *)rettv;
4062 if (!is_cmd)
4063 vim_free(rettv);
4064 }
4065 }
4066
4067 return idx < 0;
4068}
4069
4070/*
4071 * Free the variable with a pending return value.
4072 */
4073 void
4074discard_pending_return(void *rettv)
4075{
4076 free_tv((typval_T *)rettv);
4077}
4078
4079/*
4080 * Generate a return command for producing the value of "rettv". The result
4081 * is an allocated string. Used by report_pending() for verbose messages.
4082 */
4083 char_u *
4084get_return_cmd(void *rettv)
4085{
4086 char_u *s = NULL;
4087 char_u *tofree = NULL;
4088 char_u numbuf[NUMBUFLEN];
4089
4090 if (rettv != NULL)
4091 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
4092 if (s == NULL)
4093 s = (char_u *)"";
4094
4095 STRCPY(IObuff, ":return ");
4096 STRNCPY(IObuff + 8, s, IOSIZE - 8);
4097 if (STRLEN(s) + 8 >= IOSIZE)
4098 STRCPY(IObuff + IOSIZE - 4, "...");
4099 vim_free(tofree);
4100 return vim_strsave(IObuff);
4101}
4102
4103/*
4104 * Get next function line.
4105 * Called by do_cmdline() to get the next line.
4106 * Returns allocated string, or NULL for end of function.
4107 */
4108 char_u *
4109get_func_line(
4110 int c UNUSED,
4111 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02004112 int indent UNUSED,
4113 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004114{
4115 funccall_T *fcp = (funccall_T *)cookie;
4116 ufunc_T *fp = fcp->func;
4117 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004118 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004119
Bram Moolenaare38eab22019-12-05 21:50:01 +01004120 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004121 if (fcp->dbg_tick != debug_tick)
4122 {
4123 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004124 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004125 fcp->dbg_tick = debug_tick;
4126 }
4127#ifdef FEAT_PROFILE
4128 if (do_profiling == PROF_YES)
4129 func_line_end(cookie);
4130#endif
4131
4132 gap = &fp->uf_lines;
4133 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4134 || fcp->returned)
4135 retval = NULL;
4136 else
4137 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004138 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004139 while (fcp->linenr < gap->ga_len
4140 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
4141 ++fcp->linenr;
4142 if (fcp->linenr >= gap->ga_len)
4143 retval = NULL;
4144 else
4145 {
4146 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004147 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004148#ifdef FEAT_PROFILE
4149 if (do_profiling == PROF_YES)
4150 func_line_start(cookie);
4151#endif
4152 }
4153 }
4154
Bram Moolenaare38eab22019-12-05 21:50:01 +01004155 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004156 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004157 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004158 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004159 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004160 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004161 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004162 fcp->dbg_tick = debug_tick;
4163 }
4164
4165 return retval;
4166}
4167
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004168/*
4169 * Return TRUE if the currently active function should be ended, because a
4170 * return was encountered or an error occurred. Used inside a ":while".
4171 */
4172 int
4173func_has_ended(void *cookie)
4174{
4175 funccall_T *fcp = (funccall_T *)cookie;
4176
Bram Moolenaare38eab22019-12-05 21:50:01 +01004177 // Ignore the "abort" flag if the abortion behavior has been changed due to
4178 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004179 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4180 || fcp->returned);
4181}
4182
4183/*
4184 * return TRUE if cookie indicates a function which "abort"s on errors.
4185 */
4186 int
4187func_has_abort(
4188 void *cookie)
4189{
4190 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
4191}
4192
4193
4194/*
4195 * Turn "dict.Func" into a partial for "Func" bound to "dict".
4196 * Don't do this when "Func" is already a partial that was bound
4197 * explicitly (pt_auto is FALSE).
4198 * Changes "rettv" in-place.
4199 * Returns the updated "selfdict_in".
4200 */
4201 dict_T *
4202make_partial(dict_T *selfdict_in, typval_T *rettv)
4203{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004204 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004205 char_u *tofree = NULL;
4206 ufunc_T *fp;
4207 char_u fname_buf[FLEN_FIXED + 1];
4208 int error;
4209 dict_T *selfdict = selfdict_in;
4210
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004211 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
4212 fp = rettv->vval.v_partial->pt_func;
4213 else
4214 {
4215 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
4216 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004217 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004218 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004219 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004220 vim_free(tofree);
4221 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004222
4223 if (fp != NULL && (fp->uf_flags & FC_DICT))
4224 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004225 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004226
4227 if (pt != NULL)
4228 {
4229 pt->pt_refcount = 1;
4230 pt->pt_dict = selfdict;
4231 pt->pt_auto = TRUE;
4232 selfdict = NULL;
4233 if (rettv->v_type == VAR_FUNC)
4234 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004235 // Just a function: Take over the function name and use
4236 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004237 pt->pt_name = rettv->vval.v_string;
4238 }
4239 else
4240 {
4241 partial_T *ret_pt = rettv->vval.v_partial;
4242 int i;
4243
Bram Moolenaare38eab22019-12-05 21:50:01 +01004244 // Partial: copy the function name, use selfdict and copy
4245 // args. Can't take over name or args, the partial might
4246 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004247 if (ret_pt->pt_name != NULL)
4248 {
4249 pt->pt_name = vim_strsave(ret_pt->pt_name);
4250 func_ref(pt->pt_name);
4251 }
4252 else
4253 {
4254 pt->pt_func = ret_pt->pt_func;
4255 func_ptr_ref(pt->pt_func);
4256 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004257 if (ret_pt->pt_argc > 0)
4258 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004259 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004260 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004261 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004262 pt->pt_argc = 0;
4263 else
4264 {
4265 pt->pt_argc = ret_pt->pt_argc;
4266 for (i = 0; i < pt->pt_argc; i++)
4267 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
4268 }
4269 }
4270 partial_unref(ret_pt);
4271 }
4272 rettv->v_type = VAR_PARTIAL;
4273 rettv->vval.v_partial = pt;
4274 }
4275 }
4276 return selfdict;
4277}
4278
4279/*
4280 * Return the name of the executed function.
4281 */
4282 char_u *
4283func_name(void *cookie)
4284{
4285 return ((funccall_T *)cookie)->func->uf_name;
4286}
4287
4288/*
4289 * Return the address holding the next breakpoint line for a funccall cookie.
4290 */
4291 linenr_T *
4292func_breakpoint(void *cookie)
4293{
4294 return &((funccall_T *)cookie)->breakpoint;
4295}
4296
4297/*
4298 * Return the address holding the debug tick for a funccall cookie.
4299 */
4300 int *
4301func_dbg_tick(void *cookie)
4302{
4303 return &((funccall_T *)cookie)->dbg_tick;
4304}
4305
4306/*
4307 * Return the nesting level for a funccall cookie.
4308 */
4309 int
4310func_level(void *cookie)
4311{
4312 return ((funccall_T *)cookie)->level;
4313}
4314
4315/*
4316 * Return TRUE when a function was ended by a ":return" command.
4317 */
4318 int
4319current_func_returned(void)
4320{
4321 return current_funccal->returned;
4322}
4323
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004324 int
4325free_unref_funccal(int copyID, int testing)
4326{
4327 int did_free = FALSE;
4328 int did_free_funccal = FALSE;
4329 funccall_T *fc, **pfc;
4330
4331 for (pfc = &previous_funccal; *pfc != NULL; )
4332 {
4333 if (can_free_funccal(*pfc, copyID))
4334 {
4335 fc = *pfc;
4336 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004337 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004338 did_free = TRUE;
4339 did_free_funccal = TRUE;
4340 }
4341 else
4342 pfc = &(*pfc)->caller;
4343 }
4344 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004345 // When a funccal was freed some more items might be garbage
4346 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004347 (void)garbage_collect(testing);
4348
4349 return did_free;
4350}
4351
4352/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004353 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004354 */
4355 static funccall_T *
4356get_funccal(void)
4357{
4358 int i;
4359 funccall_T *funccal;
4360 funccall_T *temp_funccal;
4361
4362 funccal = current_funccal;
4363 if (debug_backtrace_level > 0)
4364 {
4365 for (i = 0; i < debug_backtrace_level; i++)
4366 {
4367 temp_funccal = funccal->caller;
4368 if (temp_funccal)
4369 funccal = temp_funccal;
4370 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004371 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004372 debug_backtrace_level = i;
4373 }
4374 }
4375 return funccal;
4376}
4377
4378/*
4379 * Return the hashtable used for local variables in the current funccal.
4380 * Return NULL if there is no current funccal.
4381 */
4382 hashtab_T *
4383get_funccal_local_ht()
4384{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004386 return NULL;
4387 return &get_funccal()->l_vars.dv_hashtab;
4388}
4389
4390/*
4391 * Return the l: scope variable.
4392 * Return NULL if there is no current funccal.
4393 */
4394 dictitem_T *
4395get_funccal_local_var()
4396{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004398 return NULL;
4399 return &get_funccal()->l_vars_var;
4400}
4401
4402/*
4403 * Return the hashtable used for argument in the current funccal.
4404 * Return NULL if there is no current funccal.
4405 */
4406 hashtab_T *
4407get_funccal_args_ht()
4408{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004410 return NULL;
4411 return &get_funccal()->l_avars.dv_hashtab;
4412}
4413
4414/*
4415 * Return the a: scope variable.
4416 * Return NULL if there is no current funccal.
4417 */
4418 dictitem_T *
4419get_funccal_args_var()
4420{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004422 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004423 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004424}
4425
4426/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004427 * List function variables, if there is a function.
4428 */
4429 void
4430list_func_vars(int *first)
4431{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004433 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004434 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004435}
4436
4437/*
4438 * If "ht" is the hashtable for local variables in the current funccal, return
4439 * the dict that contains it.
4440 * Otherwise return NULL.
4441 */
4442 dict_T *
4443get_current_funccal_dict(hashtab_T *ht)
4444{
4445 if (current_funccal != NULL
4446 && ht == &current_funccal->l_vars.dv_hashtab)
4447 return &current_funccal->l_vars;
4448 return NULL;
4449}
4450
4451/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004452 * Search hashitem in parent scope.
4453 */
4454 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004455find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004456{
4457 funccall_T *old_current_funccal = current_funccal;
4458 hashtab_T *ht;
4459 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004460 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004461
4462 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4463 return NULL;
4464
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004465 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004466 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004467 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004468 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004469 ht = find_var_ht(name, &varname);
4470 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004471 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004472 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004473 if (!HASHITEM_EMPTY(hi))
4474 {
4475 *pht = ht;
4476 break;
4477 }
4478 }
4479 if (current_funccal == current_funccal->func->uf_scoped)
4480 break;
4481 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004482 }
4483 current_funccal = old_current_funccal;
4484
4485 return hi;
4486}
4487
4488/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004489 * Search variable in parent scope.
4490 */
4491 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004492find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004493{
4494 dictitem_T *v = NULL;
4495 funccall_T *old_current_funccal = current_funccal;
4496 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004497 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004498
4499 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4500 return NULL;
4501
Bram Moolenaare38eab22019-12-05 21:50:01 +01004502 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004503 current_funccal = current_funccal->func->uf_scoped;
4504 while (current_funccal)
4505 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004506 ht = find_var_ht(name, &varname);
4507 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004508 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004509 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004510 if (v != NULL)
4511 break;
4512 }
4513 if (current_funccal == current_funccal->func->uf_scoped)
4514 break;
4515 current_funccal = current_funccal->func->uf_scoped;
4516 }
4517 current_funccal = old_current_funccal;
4518
4519 return v;
4520}
4521
4522/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004523 * Set "copyID + 1" in previous_funccal and callers.
4524 */
4525 int
4526set_ref_in_previous_funccal(int copyID)
4527{
4528 int abort = FALSE;
4529 funccall_T *fc;
4530
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004531 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004532 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004533 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004534 abort = abort
4535 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4536 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004537 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004538 }
4539 return abort;
4540}
4541
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004542 static int
4543set_ref_in_funccal(funccall_T *fc, int copyID)
4544{
4545 int abort = FALSE;
4546
4547 if (fc->fc_copyID != copyID)
4548 {
4549 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004550 abort = abort
4551 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4552 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004553 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004554 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004555 }
4556 return abort;
4557}
4558
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004559/*
4560 * Set "copyID" in all local vars and arguments in the call stack.
4561 */
4562 int
4563set_ref_in_call_stack(int copyID)
4564{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004565 int abort = FALSE;
4566 funccall_T *fc;
4567 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004568
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004569 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004570 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004571
4572 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004573 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
4574 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004575 abort = abort || set_ref_in_funccal(fc, copyID);
4576
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004577 return abort;
4578}
4579
4580/*
4581 * Set "copyID" in all functions available by name.
4582 */
4583 int
4584set_ref_in_functions(int copyID)
4585{
4586 int todo;
4587 hashitem_T *hi = NULL;
4588 int abort = FALSE;
4589 ufunc_T *fp;
4590
4591 todo = (int)func_hashtab.ht_used;
4592 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004593 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004594 if (!HASHITEM_EMPTY(hi))
4595 {
4596 --todo;
4597 fp = HI2UF(hi);
4598 if (!func_name_refcount(fp->uf_name))
4599 abort = abort || set_ref_in_func(NULL, fp, copyID);
4600 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004601 }
4602 return abort;
4603}
4604
4605/*
4606 * Set "copyID" in all function arguments.
4607 */
4608 int
4609set_ref_in_func_args(int copyID)
4610{
4611 int i;
4612 int abort = FALSE;
4613
4614 for (i = 0; i < funcargs.ga_len; ++i)
4615 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
4616 copyID, NULL, NULL);
4617 return abort;
4618}
4619
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004620/*
4621 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004622 * Returns TRUE if setting references failed somehow.
4623 */
4624 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004625set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004626{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004627 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004628 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01004629 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004630 char_u fname_buf[FLEN_FIXED + 1];
4631 char_u *tofree = NULL;
4632 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004633 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004634
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004635 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004636 return FALSE;
4637
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004638 if (fp_in == NULL)
4639 {
4640 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004641 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004642 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004643 if (fp != NULL)
4644 {
4645 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004646 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004647 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004648
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004649 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004650 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004651}
4652
Bram Moolenaare38eab22019-12-05 21:50:01 +01004653#endif // FEAT_EVAL