blob: 98e1ec5f2e37520c23c39e0907adab9759d59db9 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar93343722018-07-10 19:39:18 +020017// flags used in uf_flags
18#define FC_ABORT 0x01 // abort function on error
19#define FC_RANGE 0x02 // function accepts range
20#define FC_DICT 0x04 // Dict function, uses "self"
21#define FC_CLOSURE 0x08 // closure, uses outer scope variables
22#define FC_DELETED 0x10 // :delfunction used while uf_refcount > 0
23#define FC_REMOVED 0x20 // function redefined while uf_refcount > 0
24#define FC_SANDBOX 0x40 // function defined in the sandbox
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010025#define FC_DEAD 0x80 // function kept only for reference to dfunc
26#define FC_EXPORT 0x100 // "export def Func()"
Bram Moolenaarf10806b2020-04-02 18:34:35 +020027#define FC_NOARGS 0x200 // no a: variables in lambda
Bram Moolenaara9b579f2016-07-17 18:29:19 +020028
Bram Moolenaara9b579f2016-07-17 18:29:19 +020029/*
30 * All user-defined functions are found in this hashtable.
31 */
32static hashtab_T func_hashtab;
33
Bram Moolenaare38eab22019-12-05 21:50:01 +010034// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020035static garray_T funcargs = GA_EMPTY;
36
Bram Moolenaar209b8e32019-03-14 13:43:24 +010037// pointer to funccal for currently active function
38static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020039
Bram Moolenaar209b8e32019-03-14 13:43:24 +010040// Pointer to list of previously used funccal, still around because some
41// item in it is still being used.
42static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020043
44static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
45static char *e_funcdict = N_("E717: Dictionary entry already exists");
46static char *e_funcref = N_("E718: Funcref required");
47static char *e_nofunc = N_("E130: Unknown function: %s");
48
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020049static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020050
51 void
52func_init()
53{
54 hash_init(&func_hashtab);
55}
56
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020057/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020058 * Return the function hash table
59 */
60 hashtab_T *
61func_tbl_get(void)
62{
63 return &func_hashtab;
64}
65
66/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010067 * Get one function argument and an optional type: "arg: type".
68 * Return a pointer to after the type.
69 * When something is wrong return "arg".
70 */
71 static char_u *
72one_function_arg(char_u *arg, garray_T *newargs, garray_T *argtypes, int skip)
73{
74 char_u *p = arg;
75
76 while (ASCII_ISALNUM(*p) || *p == '_')
77 ++p;
78 if (arg == p || isdigit(*arg)
79 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
80 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
81 {
82 if (!skip)
83 semsg(_("E125: Illegal argument: %s"), arg);
84 return arg;
85 }
86 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
87 return arg;
88 if (newargs != NULL)
89 {
90 char_u *arg_copy;
91 int c;
92 int i;
93
94 c = *p;
95 *p = NUL;
96 arg_copy = vim_strsave(arg);
97 if (arg_copy == NULL)
98 {
99 *p = c;
100 return arg;
101 }
102
103 // Check for duplicate argument name.
104 for (i = 0; i < newargs->ga_len; ++i)
105 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
106 {
107 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
108 vim_free(arg_copy);
109 return arg;
110 }
111 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
112 newargs->ga_len++;
113
114 *p = c;
115 }
116
117 // get any type from "arg: type"
118 if (argtypes != NULL && ga_grow(argtypes, 1) == OK)
119 {
120 char_u *type = NULL;
121
122 if (*p == ':')
123 {
124 type = skipwhite(p + 1);
125 p = skip_type(type);
126 type = vim_strnsave(type, p - type);
127 }
128 else if (*skipwhite(p) == ':')
129 emsg(_("E1059: No white space allowed before :"));
130 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
131 }
132
133 return p;
134}
135
136/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200137 * Get function arguments.
138 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100139 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200140get_function_args(
141 char_u **argp,
142 char_u endchar,
143 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144 garray_T *argtypes, // NULL unless using :def
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200145 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200146 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200147 int skip,
148 exarg_T *eap,
149 char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200150{
151 int mustend = FALSE;
152 char_u *arg = *argp;
153 char_u *p = arg;
154 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200155 int any_default = FALSE;
156 char_u *expr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200157
158 if (newargs != NULL)
159 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100160 if (argtypes != NULL)
161 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200162 if (default_args != NULL)
163 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200164
165 if (varargs != NULL)
166 *varargs = FALSE;
167
168 /*
169 * Isolate the arguments: "arg1, arg2, ...)"
170 */
171 while (*p != endchar)
172 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200173 if (eap != NULL && *p == NUL && eap->getline != NULL)
174 {
175 char_u *theline;
176
177 // End of the line, get the next one.
178 theline = eap->getline(':', eap->cookie, 0, TRUE);
179 if (theline == NULL)
180 break;
181 vim_free(*line_to_free);
182 *line_to_free = theline;
183 p = skipwhite(theline);
184 }
185
186 if (mustend && *p != endchar)
187 {
188 if (!skip)
189 semsg(_(e_invarg2), *argp);
190 break;
191 }
192 if (*p == endchar)
193 break;
194
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200195 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
196 {
197 if (varargs != NULL)
198 *varargs = TRUE;
199 p += 3;
200 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100201
202 if (argtypes != NULL)
203 {
204 // ...name: list<type>
205 if (!ASCII_ISALPHA(*p))
206 {
207 emsg(_("E1055: Missing name after ..."));
208 break;
209 }
210
211 arg = p;
212 p = one_function_arg(p, newargs, argtypes, skip);
213 if (p == arg)
214 break;
215 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200216 }
217 else
218 {
219 arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100220 p = one_function_arg(p, newargs, argtypes, skip);
221 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200222 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200223
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200224 if (*skipwhite(p) == '=' && default_args != NULL)
225 {
226 typval_T rettv;
227
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100228 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200229 any_default = TRUE;
230 p = skipwhite(p) + 1;
231 p = skipwhite(p);
232 expr = p;
233 if (eval1(&p, &rettv, FALSE) != FAIL)
234 {
235 if (ga_grow(default_args, 1) == FAIL)
236 goto err_ret;
237
238 // trim trailing whitespace
239 while (p > expr && VIM_ISWHITE(p[-1]))
240 p--;
241 c = *p;
242 *p = NUL;
243 expr = vim_strsave(expr);
244 if (expr == NULL)
245 {
246 *p = c;
247 goto err_ret;
248 }
249 ((char_u **)(default_args->ga_data))
250 [default_args->ga_len] = expr;
251 default_args->ga_len++;
252 *p = c;
253 }
254 else
255 mustend = TRUE;
256 }
257 else if (any_default)
258 {
259 emsg(_("E989: Non-default argument follows default argument"));
260 mustend = TRUE;
261 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200262 if (*p == ',')
263 ++p;
264 else
265 mustend = TRUE;
266 }
267 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200268 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200269
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200270 if (*p != endchar)
271 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100272 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200273
274 *argp = p;
275 return OK;
276
277err_ret:
278 if (newargs != NULL)
279 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200280 if (default_args != NULL)
281 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200282 return FAIL;
283}
284
285/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200286 * Register function "fp" as using "current_funccal" as its scope.
287 */
288 static int
289register_closure(ufunc_T *fp)
290{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200291 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100292 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200293 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200294 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200295 fp->uf_scoped = current_funccal;
296 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200297
Bram Moolenaar58016442016-07-31 18:30:22 +0200298 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
299 return FAIL;
300 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
301 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200302 return OK;
303}
304
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100305 static void
306set_ufunc_name(ufunc_T *fp, char_u *name)
307{
308 STRCPY(fp->uf_name, name);
309
310 if (name[0] == K_SPECIAL)
311 {
312 fp->uf_name_exp = alloc(STRLEN(name) + 3);
313 if (fp->uf_name_exp != NULL)
314 {
315 STRCPY(fp->uf_name_exp, "<SNR>");
316 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
317 }
318 }
319}
320
Bram Moolenaar58016442016-07-31 18:30:22 +0200321/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200322 * Parse a lambda expression and get a Funcref from "*arg".
323 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
324 */
325 int
326get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
327{
328 garray_T newargs;
329 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200330 garray_T *pnewargs;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200331 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100332 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200333 int varargs;
334 int ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200335 char_u *start = skipwhite(*arg + 1);
336 char_u *s, *e;
337 static int lambda_no = 0;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200338 int *old_eval_lavars = eval_lavars_used;
339 int eval_lavars = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200340
341 ga_init(&newargs);
342 ga_init(&newlines);
343
Bram Moolenaare38eab22019-12-05 21:50:01 +0100344 // First, check if this is a lambda expression. "->" must exist.
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200345 ret = get_function_args(&start, '-', NULL, NULL, NULL, NULL, TRUE,
346 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200347 if (ret == FAIL || *start != '>')
348 return NOTDONE;
349
Bram Moolenaare38eab22019-12-05 21:50:01 +0100350 // Parse the arguments again.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200351 if (evaluate)
352 pnewargs = &newargs;
353 else
354 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200355 *arg = skipwhite(*arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100356 // TODO: argument types
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200357 ret = get_function_args(arg, '-', pnewargs, NULL, &varargs, NULL, FALSE,
358 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200359 if (ret == FAIL || **arg != '>')
360 goto errret;
361
Bram Moolenaare38eab22019-12-05 21:50:01 +0100362 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200363 if (evaluate)
364 eval_lavars_used = &eval_lavars;
365
Bram Moolenaare38eab22019-12-05 21:50:01 +0100366 // Get the start and the end of the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200367 *arg = skipwhite(*arg + 1);
368 s = *arg;
369 ret = skip_expr(arg);
370 if (ret == FAIL)
371 goto errret;
372 e = *arg;
373 *arg = skipwhite(*arg);
374 if (**arg != '}')
Bram Moolenaaree619e52020-03-28 21:38:06 +0100375 {
376 semsg(_("E451: Expected }: %s"), *arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200377 goto errret;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100378 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200379 ++*arg;
380
381 if (evaluate)
382 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200383 int len, flags = 0;
384 char_u *p;
385 char_u name[20];
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200386
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200387 sprintf((char*)name, "<lambda>%d", ++lambda_no);
388
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200389 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200390 if (fp == NULL)
391 goto errret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100392 fp->uf_dfunc_idx = -1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200393 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200394 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200395 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200396
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200397 ga_init2(&newlines, (int)sizeof(char_u *), 1);
398 if (ga_grow(&newlines, 1) == FAIL)
399 goto errret;
400
Bram Moolenaare38eab22019-12-05 21:50:01 +0100401 // Add "return " before the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200402 len = 7 + e - s + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200403 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200404 if (p == NULL)
405 goto errret;
406 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
407 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200408 vim_strncpy(p + 7, s, e - s);
Bram Moolenaarf10806b2020-04-02 18:34:35 +0200409 if (strstr((char *)p + 7, "a:") == NULL)
410 // No a: variables are used for sure.
411 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200412
413 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100414 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200415 hash_add(&func_hashtab, UF2HIKEY(fp));
416 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200417 ga_init(&fp->uf_def_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200418 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200419 if (current_funccal != NULL && eval_lavars)
420 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200421 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200422 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200423 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200424 }
425 else
426 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200427
428#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200429 if (prof_def_func())
430 func_do_profile(fp);
431#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200432 if (sandbox)
433 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100434 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200435 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200436 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200437 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200438 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100439 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200440
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200441 pt->pt_func = fp;
442 pt->pt_refcount = 1;
443 rettv->vval.v_partial = pt;
444 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200445 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200446
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200447 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200448 return OK;
449
450errret:
451 ga_clear_strings(&newargs);
452 ga_clear_strings(&newlines);
453 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100454 vim_free(pt);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200455 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200456 return FAIL;
457}
458
459/*
460 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
461 * name it contains, otherwise return "name".
462 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
463 * "partialp".
464 */
465 char_u *
466deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
467{
468 dictitem_T *v;
469 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200470 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200471
472 if (partialp != NULL)
473 *partialp = NULL;
474
475 cc = name[*lenp];
476 name[*lenp] = NUL;
477 v = find_var(name, NULL, no_autoload);
478 name[*lenp] = cc;
479 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
480 {
481 if (v->di_tv.vval.v_string == NULL)
482 {
483 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100484 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200485 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200486 s = v->di_tv.vval.v_string;
487 *lenp = (int)STRLEN(s);
488 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200489 }
490
491 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
492 {
493 partial_T *pt = v->di_tv.vval.v_partial;
494
495 if (pt == NULL)
496 {
497 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100498 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200499 }
500 if (partialp != NULL)
501 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200502 s = partial_name(pt);
503 *lenp = (int)STRLEN(s);
504 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200505 }
506
507 return name;
508}
509
510/*
511 * Give an error message with a function name. Handle <SNR> things.
512 * "ermsg" is to be passed without translation, use N_() instead of _().
513 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100514 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200515emsg_funcname(char *ermsg, char_u *name)
516{
517 char_u *p;
518
519 if (*name == K_SPECIAL)
520 p = concat_str((char_u *)"<SNR>", name + 3);
521 else
522 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100523 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200524 if (p != name)
525 vim_free(p);
526}
527
528/*
529 * Allocate a variable for the result of a function.
530 * Return OK or FAIL.
531 */
532 int
533get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200534 char_u *name, // name of the function
535 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200536 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200537 char_u **arg, // argument, pointing to the '('
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200538 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200539{
540 char_u *argp;
541 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100542 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
543 int argcount = 0; // number of arguments found
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200544
545 /*
546 * Get the arguments.
547 */
548 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200549 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
550 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200551 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100552 argp = skipwhite(argp + 1); // skip the '(' or ','
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200553 if (*argp == ')' || *argp == ',' || *argp == NUL)
554 break;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200555 if (eval1(&argp, &argvars[argcount], funcexe->evaluate) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200556 {
557 ret = FAIL;
558 break;
559 }
560 ++argcount;
561 if (*argp != ',')
562 break;
563 }
564 if (*argp == ')')
565 ++argp;
566 else
567 ret = FAIL;
568
569 if (ret == OK)
570 {
571 int i = 0;
572
573 if (get_vim_var_nr(VV_TESTING))
574 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100575 // Prepare for calling test_garbagecollect_now(), need to know
576 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200577 if (funcargs.ga_itemsize == 0)
578 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
579 for (i = 0; i < argcount; ++i)
580 if (ga_grow(&funcargs, 1) == OK)
581 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
582 &argvars[i];
583 }
584
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200585 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200586
587 funcargs.ga_len -= i;
588 }
589 else if (!aborting())
590 {
591 if (argcount == MAX_FUNC_ARGS)
592 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
593 else
594 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
595 }
596
597 while (--argcount >= 0)
598 clear_tv(&argvars[argcount]);
599
600 *arg = skipwhite(argp);
601 return ret;
602}
603
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200604/*
605 * Return TRUE if "p" starts with "<SID>" or "s:".
606 * Only works if eval_fname_script() returned non-zero for "p"!
607 */
608 static int
609eval_fname_sid(char_u *p)
610{
611 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
612}
613
614/*
615 * In a script change <SID>name() and s:name() to K_SNR 123_name().
616 * Change <SNR>123_name() to K_SNR 123_name().
617 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
618 * (slow).
619 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100620 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200621fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
622{
623 int llen;
624 char_u *fname;
625 int i;
626
627 llen = eval_fname_script(name);
628 if (llen > 0)
629 {
630 fname_buf[0] = K_SPECIAL;
631 fname_buf[1] = KS_EXTRA;
632 fname_buf[2] = (int)KE_SNR;
633 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100634 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200635 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200636 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100637 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200638 else
639 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200640 sprintf((char *)fname_buf + 3, "%ld_",
641 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200642 i = (int)STRLEN(fname_buf);
643 }
644 }
645 if (i + STRLEN(name + llen) < FLEN_FIXED)
646 {
647 STRCPY(fname_buf + i, name + llen);
648 fname = fname_buf;
649 }
650 else
651 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200652 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200653 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100654 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200655 else
656 {
657 *tofree = fname;
658 mch_memmove(fname, fname_buf, (size_t)i);
659 STRCPY(fname + i, name + llen);
660 }
661 }
662 }
663 else
664 fname = name;
665 return fname;
666}
667
668/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669 * Find a function "name" in script "sid".
670 */
671 static ufunc_T *
672find_func_with_sid(char_u *name, int sid)
673{
674 hashitem_T *hi;
675 char_u buffer[200];
676
677 buffer[0] = K_SPECIAL;
678 buffer[1] = KS_EXTRA;
679 buffer[2] = (int)KE_SNR;
680 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
681 (long)sid, name);
682 hi = hash_find(&func_hashtab, buffer);
683 if (!HASHITEM_EMPTY(hi))
684 return HI2UF(hi);
685
686 return NULL;
687}
688
689/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200690 * Find a function by name, return pointer to it in ufuncs.
691 * Return NULL for unknown function.
692 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100693 static ufunc_T *
694find_func_even_dead(char_u *name, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200695{
696 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697 ufunc_T *func;
698 imported_T *imported;
699
700 if (in_vim9script())
701 {
702 // Find script-local function before global one.
703 func = find_func_with_sid(name, current_sctx.sc_sid);
704 if (func != NULL)
705 return func;
706
707 // Find imported funcion before global one.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +0100708 imported = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 if (imported != NULL && imported->imp_funcname != NULL)
710 {
711 hi = hash_find(&func_hashtab, imported->imp_funcname);
712 if (!HASHITEM_EMPTY(hi))
713 return HI2UF(hi);
714 }
715 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200716
717 hi = hash_find(&func_hashtab, name);
718 if (!HASHITEM_EMPTY(hi))
719 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720
721 return NULL;
722}
723
724/*
725 * Find a function by name, return pointer to it in ufuncs.
726 * "cctx" is passed in a :def function to find imported functions.
727 * Return NULL for unknown or dead function.
728 */
729 ufunc_T *
730find_func(char_u *name, cctx_T *cctx)
731{
732 ufunc_T *fp = find_func_even_dead(name, cctx);
733
734 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
735 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200736 return NULL;
737}
738
739/*
740 * Copy the function name of "fp" to buffer "buf".
741 * "buf" must be able to hold the function name plus three bytes.
742 * Takes care of script-local function names.
743 */
744 static void
745cat_func_name(char_u *buf, ufunc_T *fp)
746{
747 if (fp->uf_name[0] == K_SPECIAL)
748 {
749 STRCPY(buf, "<SNR>");
750 STRCAT(buf, fp->uf_name + 3);
751 }
752 else
753 STRCPY(buf, fp->uf_name);
754}
755
756/*
757 * Add a number variable "name" to dict "dp" with value "nr".
758 */
759 static void
760add_nr_var(
761 dict_T *dp,
762 dictitem_T *v,
763 char *name,
764 varnumber_T nr)
765{
766 STRCPY(v->di_key, name);
767 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
768 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
769 v->di_tv.v_type = VAR_NUMBER;
770 v->di_tv.v_lock = VAR_FIXED;
771 v->di_tv.vval.v_number = nr;
772}
773
774/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100775 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200776 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100777 static void
778free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200779{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100780 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200781
782 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
783 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100784 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200785
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100786 // When garbage collecting a funccall_T may be freed before the
787 // function that references it, clear its uf_scoped field.
788 // The function may have been redefined and point to another
789 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200790 if (fp != NULL && fp->uf_scoped == fc)
791 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200792 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200793 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200794
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200795 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200796 vim_free(fc);
797}
798
799/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100800 * Free "fc" and what it contains.
801 * Can be called only when "fc" is kept beyond the period of it called,
802 * i.e. after cleanup_function_call(fc).
803 */
804 static void
805free_funccal_contents(funccall_T *fc)
806{
807 listitem_T *li;
808
809 // Free all l: variables.
810 vars_clear(&fc->l_vars.dv_hashtab);
811
812 // Free all a: variables.
813 vars_clear(&fc->l_avars.dv_hashtab);
814
815 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200816 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100817 clear_tv(&li->li_tv);
818
819 free_funccal(fc);
820}
821
822/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200823 * Handle the last part of returning from a function: free the local hashtable.
824 * Unless it is still in use by a closure.
825 */
826 static void
827cleanup_function_call(funccall_T *fc)
828{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100829 int may_free_fc = fc->fc_refcount <= 0;
830 int free_fc = TRUE;
831
Bram Moolenaar6914c642017-04-01 21:21:30 +0200832 current_funccal = fc->caller;
833
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100834 // Free all l: variables if not referred.
835 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
836 vars_clear(&fc->l_vars.dv_hashtab);
837 else
838 free_fc = FALSE;
839
840 // If the a:000 list and the l: and a: dicts are not referenced and
841 // there is no closure using it, we can free the funccall_T and what's
842 // in it.
843 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
844 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200845 else
846 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100847 int todo;
848 hashitem_T *hi;
849 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200850
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100851 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200852
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100853 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200854 todo = (int)fc->l_avars.dv_hashtab.ht_used;
855 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
856 {
857 if (!HASHITEM_EMPTY(hi))
858 {
859 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100860 di = HI2DI(hi);
861 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200862 }
863 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100864 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200865
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100866 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
867 fc->l_varlist.lv_first = NULL;
868 else
869 {
870 listitem_T *li;
871
872 free_fc = FALSE;
873
874 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200875 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +0200876 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100877 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100878
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100879 if (free_fc)
880 free_funccal(fc);
881 else
882 {
883 static int made_copy = 0;
884
885 // "fc" is still in use. This can happen when returning "a:000",
886 // assigning "l:" to a global variable or defining a closure.
887 // Link "fc" in the list for garbage collection later.
888 fc->caller = previous_funccal;
889 previous_funccal = fc;
890
891 if (want_garbage_collect)
892 // If garbage collector is ready, clear count.
893 made_copy = 0;
894 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100895 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100896 // We have made a lot of copies, worth 4 Mbyte. This can happen
897 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100898 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100899 // too much memory.
900 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100901 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100902 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200903 }
904}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905/*
906 * Unreference "fc": decrement the reference count and free it when it
907 * becomes zero. "fp" is detached from "fc".
908 * When "force" is TRUE we are exiting.
909 */
910 static void
911funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
912{
913 funccall_T **pfc;
914 int i;
915
916 if (fc == NULL)
917 return;
918
919 if (--fc->fc_refcount <= 0 && (force || (
920 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
921 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
922 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
923 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
924 {
925 if (fc == *pfc)
926 {
927 *pfc = fc->caller;
928 free_funccal_contents(fc);
929 return;
930 }
931 }
932 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
933 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
934 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
935}
936
937/*
938 * Remove the function from the function hashtable. If the function was
939 * deleted while it still has references this was already done.
940 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
941 */
942 static int
943func_remove(ufunc_T *fp)
944{
945 hashitem_T *hi;
946
947 // Return if it was already virtually deleted.
948 if (fp->uf_flags & FC_DEAD)
949 return FALSE;
950
951 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
952 if (!HASHITEM_EMPTY(hi))
953 {
954 // When there is a def-function index do not actually remove the
955 // function, so we can find the index when defining the function again.
956 if (fp->uf_dfunc_idx >= 0)
957 fp->uf_flags |= FC_DEAD;
958 else
959 hash_remove(&func_hashtab, hi);
960 return TRUE;
961 }
962 return FALSE;
963}
964
965 static void
966func_clear_items(ufunc_T *fp)
967{
968 ga_clear_strings(&(fp->uf_args));
969 ga_clear_strings(&(fp->uf_def_args));
970 ga_clear_strings(&(fp->uf_lines));
971 VIM_CLEAR(fp->uf_name_exp);
972 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100973 VIM_CLEAR(fp->uf_def_arg_idx);
974 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200975 while (fp->uf_type_list.ga_len > 0)
976 vim_free(((type_T **)fp->uf_type_list.ga_data)
977 [--fp->uf_type_list.ga_len]);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100978 ga_clear(&fp->uf_type_list);
979#ifdef FEAT_PROFILE
980 VIM_CLEAR(fp->uf_tml_count);
981 VIM_CLEAR(fp->uf_tml_total);
982 VIM_CLEAR(fp->uf_tml_self);
983#endif
984}
985
986/*
987 * Free all things that a function contains. Does not free the function
988 * itself, use func_free() for that.
989 * When "force" is TRUE we are exiting.
990 */
991 static void
992func_clear(ufunc_T *fp, int force)
993{
994 if (fp->uf_cleared)
995 return;
996 fp->uf_cleared = TRUE;
997
998 // clear this function
999 func_clear_items(fp);
1000 funccal_unref(fp->uf_scoped, fp, force);
1001 delete_def_function(fp);
1002}
1003
1004/*
1005 * Free a function and remove it from the list of functions. Does not free
1006 * what a function contains, call func_clear() first.
1007 */
1008 static void
1009func_free(ufunc_T *fp)
1010{
1011 // Only remove it when not done already, otherwise we would remove a newer
1012 // version of the function with the same name.
1013 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1014 func_remove(fp);
1015
1016 if ((fp->uf_flags & FC_DEAD) == 0)
1017 vim_free(fp);
1018}
1019
1020/*
1021 * Free all things that a function contains and free the function itself.
1022 * When "force" is TRUE we are exiting.
1023 */
1024 static void
1025func_clear_free(ufunc_T *fp, int force)
1026{
1027 func_clear(fp, force);
1028 func_free(fp);
1029}
1030
Bram Moolenaar6914c642017-04-01 21:21:30 +02001031
1032/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001033 * Call a user function.
1034 */
1035 static void
1036call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001037 ufunc_T *fp, // pointer to function
1038 int argcount, // nr of args
1039 typval_T *argvars, // arguments
1040 typval_T *rettv, // return value
1041 linenr_T firstline, // first line of range
1042 linenr_T lastline, // last line of range
1043 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001044{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001045 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001046 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001047 funccall_T *fc;
1048 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001049 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001050 static int depth = 0;
1051 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001052 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001053 int i;
1054 int ai;
1055 int islambda = FALSE;
1056 char_u numbuf[NUMBUFLEN];
1057 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001058#ifdef FEAT_PROFILE
1059 proftime_T wait_start;
1060 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001061 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001062#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001063 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001064
Bram Moolenaare38eab22019-12-05 21:50:01 +01001065 // If depth of calling is getting too high, don't execute the function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001066 if (depth >= p_mfd)
1067 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001068 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001069 rettv->v_type = VAR_NUMBER;
1070 rettv->vval.v_number = -1;
1071 return;
1072 }
1073 ++depth;
1074
Bram Moolenaare38eab22019-12-05 21:50:01 +01001075 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001076
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001077 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001078 if (fc == NULL)
1079 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001080 fc->caller = current_funccal;
1081 current_funccal = fc;
1082 fc->func = fp;
1083 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001084 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001085 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001086 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1087 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001088 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001089 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001090 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001091
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001092 if (fp->uf_dfunc_idx >= 0)
1093 {
1094 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001095 save_current_sctx = current_sctx;
1096 current_sctx = fp->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097
1098 // Execute the compiled function.
1099 call_def_function(fp, argcount, argvars, rettv);
1100 --depth;
1101 current_funccal = fc->caller;
1102
1103 estack_pop();
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001104 current_sctx = save_current_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105 free_funccal(fc);
1106 return;
1107 }
1108
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001109 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1110 islambda = TRUE;
1111
1112 /*
1113 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1114 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1115 * each argument variable and saves a lot of time.
1116 */
1117 /*
1118 * Init l: variables.
1119 */
1120 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1121 if (selfdict != NULL)
1122 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001123 // Set l:self to "selfdict". Use "name" to avoid a warning from
1124 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001125 v = &fc->fixvar[fixvar_idx++].var;
1126 name = v->di_key;
1127 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001128 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001129 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1130 v->di_tv.v_type = VAR_DICT;
1131 v->di_tv.v_lock = 0;
1132 v->di_tv.vval.v_dict = selfdict;
1133 ++selfdict->dv_refcount;
1134 }
1135
1136 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001137 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001138 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001139 * Set a:000 to a list with room for the "..." arguments.
1140 */
1141 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001142 if ((fp->uf_flags & FC_NOARGS) == 0)
1143 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001144 (varnumber_T)(argcount >= fp->uf_args.ga_len
1145 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001146 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001147 if ((fp->uf_flags & FC_NOARGS) == 0)
1148 {
1149 // Use "name" to avoid a warning from some compiler that checks the
1150 // destination size.
1151 v = &fc->fixvar[fixvar_idx++].var;
1152 name = v->di_key;
1153 STRCPY(name, "000");
1154 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1155 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1156 v->di_tv.v_type = VAR_LIST;
1157 v->di_tv.v_lock = VAR_FIXED;
1158 v->di_tv.vval.v_list = &fc->l_varlist;
1159 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001160 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001161 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1162 fc->l_varlist.lv_lock = VAR_FIXED;
1163
1164 /*
1165 * Set a:firstline to "firstline" and a:lastline to "lastline".
1166 * Set a:name to named arguments.
1167 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001168 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001169 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001170 if ((fp->uf_flags & FC_NOARGS) == 0)
1171 {
1172 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001173 (varnumber_T)firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001174 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001175 (varnumber_T)lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001176 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001177 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001178 {
1179 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001180 typval_T def_rettv;
1181 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001182
1183 ai = i - fp->uf_args.ga_len;
1184 if (ai < 0)
1185 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001186 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001187 name = FUNCARG(fp, i);
1188 if (islambda)
1189 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001190
1191 // evaluate named argument default expression
1192 isdefault = ai + fp->uf_def_args.ga_len >= 0
1193 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1194 && argvars[i].vval.v_number == VVAL_NONE));
1195 if (isdefault)
1196 {
1197 char_u *default_expr = NULL;
1198 def_rettv.v_type = VAR_NUMBER;
1199 def_rettv.vval.v_number = -1;
1200
1201 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1202 [ai + fp->uf_def_args.ga_len];
1203 if (eval1(&default_expr, &def_rettv, TRUE) == FAIL)
1204 {
1205 default_arg_err = 1;
1206 break;
1207 }
1208 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001209 }
1210 else
1211 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001212 if ((fp->uf_flags & FC_NOARGS) != 0)
1213 // Bail out if no a: arguments used (in lambda).
1214 break;
1215
Bram Moolenaare38eab22019-12-05 21:50:01 +01001216 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001217 sprintf((char *)numbuf, "%d", ai + 1);
1218 name = numbuf;
1219 }
1220 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1221 {
1222 v = &fc->fixvar[fixvar_idx++].var;
1223 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001224 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001225 }
1226 else
1227 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001228 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001229 if (v == NULL)
1230 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001231 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001232 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001233
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001234 // Note: the values are copied directly to avoid alloc/free.
1235 // "argvars" must have VAR_FIXED for v_lock.
1236 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001237 v->di_tv.v_lock = VAR_FIXED;
1238
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001239 if (addlocal)
1240 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001241 // Named arguments should be accessed without the "a:" prefix in
1242 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001243 copy_tv(&v->di_tv, &v->di_tv);
1244 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001245 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001246 else
1247 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001248
1249 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1250 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001251 listitem_T *li = &fc->l_listitems[ai];
1252
1253 li->li_tv = argvars[i];
1254 li->li_tv.v_lock = VAR_FIXED;
1255 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001256 }
1257 }
1258
Bram Moolenaare38eab22019-12-05 21:50:01 +01001259 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001260 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001261
1262 if (fp->uf_flags & FC_SANDBOX)
1263 {
1264 using_sandbox = TRUE;
1265 ++sandbox;
1266 }
1267
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001268 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001269 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001270 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001271 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001272 ++no_wait_return;
1273 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001274
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001275 smsg(_("calling %s"), SOURCING_NAME);
1276 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001277 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001278 char_u buf[MSG_BUF_LEN];
1279 char_u numbuf2[NUMBUFLEN];
1280 char_u *tofree;
1281 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001282
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001283 msg_puts("(");
1284 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001285 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001286 if (i > 0)
1287 msg_puts(", ");
1288 if (argvars[i].v_type == VAR_NUMBER)
1289 msg_outnum((long)argvars[i].vval.v_number);
1290 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001291 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001292 // Do not want errors such as E724 here.
1293 ++emsg_off;
1294 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1295 --emsg_off;
1296 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001297 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001298 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001299 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001300 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1301 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001302 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001303 msg_puts((char *)s);
1304 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001305 }
1306 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001307 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001308 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001309 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001310 msg_puts("\n"); // don't overwrite this either
1311
1312 verbose_leave_scroll();
1313 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001314 }
1315#ifdef FEAT_PROFILE
1316 if (do_profiling == PROF_YES)
1317 {
1318 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001319 {
1320 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001321 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001322 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001323 if (fp->uf_profiling
1324 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1325 {
1326 ++fp->uf_tm_count;
1327 profile_start(&call_start);
1328 profile_zero(&fp->uf_tm_children);
1329 }
1330 script_prof_save(&wait_start);
1331 }
1332#endif
1333
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001334 save_current_sctx = current_sctx;
1335 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001336 save_did_emsg = did_emsg;
1337 did_emsg = FALSE;
1338
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001339 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1340 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001341 else if (islambda)
1342 {
1343 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1344
1345 // A Lambda always has the command "return {expr}". It is much faster
1346 // to evaluate {expr} directly.
1347 ++ex_nesting_level;
Bram Moolenaard1e9dc22020-04-03 18:13:57 +02001348 (void)eval1(&p, rettv, TRUE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001349 --ex_nesting_level;
1350 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001351 else
1352 // call do_cmdline() to execute the lines
1353 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001354 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1355
1356 --RedrawingDisabled;
1357
Bram Moolenaare38eab22019-12-05 21:50:01 +01001358 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001359 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1360 {
1361 clear_tv(rettv);
1362 rettv->v_type = VAR_NUMBER;
1363 rettv->vval.v_number = -1;
1364 }
1365
1366#ifdef FEAT_PROFILE
1367 if (do_profiling == PROF_YES && (fp->uf_profiling
1368 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1369 {
1370 profile_end(&call_start);
1371 profile_sub_wait(&wait_start, &call_start);
1372 profile_add(&fp->uf_tm_total, &call_start);
1373 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1374 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1375 {
1376 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1377 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1378 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001379 if (started_profiling)
1380 // make a ":profdel func" stop profiling the function
1381 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001382 }
1383#endif
1384
Bram Moolenaare38eab22019-12-05 21:50:01 +01001385 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001386 if (p_verbose >= 12)
1387 {
1388 ++no_wait_return;
1389 verbose_enter_scroll();
1390
1391 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001392 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001393 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001394 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001395 (long)fc->rettv->vval.v_number);
1396 else
1397 {
1398 char_u buf[MSG_BUF_LEN];
1399 char_u numbuf2[NUMBUFLEN];
1400 char_u *tofree;
1401 char_u *s;
1402
Bram Moolenaare38eab22019-12-05 21:50:01 +01001403 // The value may be very long. Skip the middle part, so that we
1404 // have some idea how it starts and ends. smsg() would always
1405 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001406 ++emsg_off;
1407 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1408 --emsg_off;
1409 if (s != NULL)
1410 {
1411 if (vim_strsize(s) > MSG_BUF_CLEN)
1412 {
1413 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1414 s = buf;
1415 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001416 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001417 vim_free(tofree);
1418 }
1419 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001420 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001421
1422 verbose_leave_scroll();
1423 --no_wait_return;
1424 }
1425
Bram Moolenaare31ee862020-01-07 20:59:34 +01001426 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001427 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001428 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001429#ifdef FEAT_PROFILE
1430 if (do_profiling == PROF_YES)
1431 script_prof_restore(&wait_start);
1432#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001433 if (using_sandbox)
1434 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001435
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001436 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001437 {
1438 ++no_wait_return;
1439 verbose_enter_scroll();
1440
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001441 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001442 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001443
1444 verbose_leave_scroll();
1445 --no_wait_return;
1446 }
1447
1448 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001449 --depth;
1450
Bram Moolenaar6914c642017-04-01 21:21:30 +02001451 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001452}
1453
1454/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001456 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 int
1458call_user_func_check(
1459 ufunc_T *fp,
1460 int argcount,
1461 typval_T *argvars,
1462 typval_T *rettv,
1463 funcexe_T *funcexe,
1464 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001465{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 int error;
1467 int regular_args = fp->uf_args.ga_len;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001468
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1470 *funcexe->doesrange = TRUE;
1471 if (argcount < regular_args - fp->uf_def_args.ga_len)
1472 error = FCERR_TOOFEW;
1473 else if (!has_varargs(fp) && argcount > regular_args)
1474 error = FCERR_TOOMANY;
1475 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1476 error = FCERR_DICT;
1477 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001478 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 int did_save_redo = FALSE;
1480 save_redo_T save_redo;
1481
1482 /*
1483 * Call the user function.
1484 * Save and restore search patterns, script variables and
1485 * redo buffer.
1486 */
1487 save_search_patterns();
1488 if (!ins_compl_active())
1489 {
1490 saveRedobuff(&save_redo);
1491 did_save_redo = TRUE;
1492 }
1493 ++fp->uf_calls;
1494 call_user_func(fp, argcount, argvars, rettv,
1495 funcexe->firstline, funcexe->lastline,
1496 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
1497 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1498 // Function was unreferenced while being used, free it now.
1499 func_clear_free(fp, FALSE);
1500 if (did_save_redo)
1501 restoreRedobuff(&save_redo);
1502 restore_search_patterns();
1503 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001504 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001506}
1507
1508/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001509 * There are two kinds of function names:
1510 * 1. ordinary names, function defined with :function
1511 * 2. numbered functions and lambdas
1512 * For the first we only count the name stored in func_hashtab as a reference,
1513 * using function() does not count as a reference, because the function is
1514 * looked up by name.
1515 */
1516 static int
1517func_name_refcount(char_u *name)
1518{
1519 return isdigit(*name) || *name == '<';
1520}
1521
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001522static funccal_entry_T *funccal_stack = NULL;
1523
1524/*
1525 * Save the current function call pointer, and set it to NULL.
1526 * Used when executing autocommands and for ":source".
1527 */
1528 void
1529save_funccal(funccal_entry_T *entry)
1530{
1531 entry->top_funccal = current_funccal;
1532 entry->next = funccal_stack;
1533 funccal_stack = entry;
1534 current_funccal = NULL;
1535}
1536
1537 void
1538restore_funccal(void)
1539{
1540 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001541 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001542 else
1543 {
1544 current_funccal = funccal_stack->top_funccal;
1545 funccal_stack = funccal_stack->next;
1546 }
1547}
1548
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001549 funccall_T *
1550get_current_funccal(void)
1551{
1552 return current_funccal;
1553}
1554
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001555#if defined(EXITFREE) || defined(PROTO)
1556 void
1557free_all_functions(void)
1558{
1559 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001560 ufunc_T *fp;
1561 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001562 long_u todo = 1;
1563 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001564
Bram Moolenaare38eab22019-12-05 21:50:01 +01001565 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001566 while (current_funccal != NULL)
1567 {
1568 clear_tv(current_funccal->rettv);
1569 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001570 if (current_funccal == NULL && funccal_stack != NULL)
1571 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001572 }
1573
Bram Moolenaare38eab22019-12-05 21:50:01 +01001574 // First clear what the functions contain. Since this may lower the
1575 // reference count of a function, it may also free a function and change
1576 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001577 while (todo > 0)
1578 {
1579 todo = func_hashtab.ht_used;
1580 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1581 if (!HASHITEM_EMPTY(hi))
1582 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583 // clear the def function index now
1584 fp = HI2UF(hi);
1585 fp->uf_flags &= ~FC_DEAD;
1586 fp->uf_dfunc_idx = -1;
1587
Bram Moolenaare38eab22019-12-05 21:50:01 +01001588 // Only free functions that are not refcounted, those are
1589 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001590 if (func_name_refcount(fp->uf_name))
1591 ++skipped;
1592 else
1593 {
1594 used = func_hashtab.ht_used;
1595 func_clear(fp, TRUE);
1596 if (used != func_hashtab.ht_used)
1597 {
1598 skipped = 0;
1599 break;
1600 }
1601 }
1602 --todo;
1603 }
1604 }
1605
Bram Moolenaare38eab22019-12-05 21:50:01 +01001606 // Now actually free the functions. Need to start all over every time,
1607 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001608 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001609 while (func_hashtab.ht_used > skipped)
1610 {
1611 todo = func_hashtab.ht_used;
1612 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001613 if (!HASHITEM_EMPTY(hi))
1614 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001615 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001616 // Only free functions that are not refcounted, those are
1617 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02001618 fp = HI2UF(hi);
1619 if (func_name_refcount(fp->uf_name))
1620 ++skipped;
1621 else
1622 {
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001623 func_free(fp);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001624 skipped = 0;
1625 break;
1626 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001627 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001628 }
1629 if (skipped == 0)
1630 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631
1632 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001633}
1634#endif
1635
1636/*
1637 * Return TRUE if "name" looks like a builtin function name: starts with a
1638 * lower case letter and doesn't contain AUTOLOAD_CHAR.
1639 * "len" is the length of "name", or -1 for NUL terminated.
1640 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001642builtin_function(char_u *name, int len)
1643{
1644 char_u *p;
1645
1646 if (!ASCII_ISLOWER(name[0]))
1647 return FALSE;
1648 p = vim_strchr(name, AUTOLOAD_CHAR);
1649 return p == NULL || (len > 0 && p > name + len);
1650}
1651
1652 int
1653func_call(
1654 char_u *name,
1655 typval_T *args,
1656 partial_T *partial,
1657 dict_T *selfdict,
1658 typval_T *rettv)
1659{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001660 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001661 listitem_T *item;
1662 typval_T argv[MAX_FUNC_ARGS + 1];
1663 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001664 int r = 0;
1665
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001666 range_list_materialize(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001667 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001668 {
1669 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001671 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001672 break;
1673 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001674 // Make a copy of each argument. This is needed to be able to set
1675 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001676 copy_tv(&item->li_tv, &argv[argc++]);
1677 }
1678
1679 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001680 {
1681 funcexe_T funcexe;
1682
Bram Moolenaara80faa82020-04-12 19:37:17 +02001683 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001684 funcexe.firstline = curwin->w_cursor.lnum;
1685 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001686 funcexe.evaluate = TRUE;
1687 funcexe.partial = partial;
1688 funcexe.selfdict = selfdict;
1689 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1690 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001691
Bram Moolenaare38eab22019-12-05 21:50:01 +01001692 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001693 while (argc > 0)
1694 clear_tv(&argv[--argc]);
1695
1696 return r;
1697}
1698
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001699static int callback_depth = 0;
1700
1701 int
1702get_callback_depth(void)
1703{
1704 return callback_depth;
1705}
1706
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001707/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001708 * Invoke call_func() with a callback.
1709 */
1710 int
1711call_callback(
1712 callback_T *callback,
1713 int len, // length of "name" or -1 to use strlen()
1714 typval_T *rettv, // return value goes here
1715 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001716 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001717 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001718{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001719 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001720 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001721
Bram Moolenaara80faa82020-04-12 19:37:17 +02001722 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001723 funcexe.evaluate = TRUE;
1724 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001725 ++callback_depth;
1726 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1727 --callback_depth;
1728 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001729}
1730
1731/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732 * Give an error message for the result of a function.
1733 * Nothing if "error" is FCERR_NONE.
1734 */
1735 void
1736user_func_error(int error, char_u *name)
1737{
1738 switch (error)
1739 {
1740 case FCERR_UNKNOWN:
1741 emsg_funcname(e_unknownfunc, name);
1742 break;
1743 case FCERR_NOTMETHOD:
1744 emsg_funcname(
1745 N_("E276: Cannot use function as a method: %s"), name);
1746 break;
1747 case FCERR_DELETED:
1748 emsg_funcname(N_(e_func_deleted), name);
1749 break;
1750 case FCERR_TOOMANY:
1751 emsg_funcname((char *)e_toomanyarg, name);
1752 break;
1753 case FCERR_TOOFEW:
1754 emsg_funcname((char *)e_toofewarg, name);
1755 break;
1756 case FCERR_SCRIPT:
1757 emsg_funcname(
1758 N_("E120: Using <SID> not in a script context: %s"), name);
1759 break;
1760 case FCERR_DICT:
1761 emsg_funcname(
1762 N_("E725: Calling dict function without Dictionary: %s"),
1763 name);
1764 break;
1765 }
1766}
1767
1768/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001769 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001770 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001771 * Return FAIL when the function can't be called, OK otherwise.
1772 * Also returns OK when an error was encountered while executing the function.
1773 */
1774 int
1775call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001776 char_u *funcname, // name of the function
1777 int len, // length of "name" or -1 to use strlen()
1778 typval_T *rettv, // return value goes here
1779 int argcount_in, // number of "argvars"
1780 typval_T *argvars_in, // vars for arguments, must have "argcount"
1781 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001782 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001783{
1784 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01001785 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001786 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001787 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001788 char_u fname_buf[FLEN_FIXED + 1];
1789 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001790 char_u *fname = NULL;
1791 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001792 int argcount = argcount_in;
1793 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001794 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001795 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1796 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001797 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001798 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001799 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001800
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001801 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1802 // even when call_func() returns FAIL.
1803 rettv->v_type = VAR_UNKNOWN;
1804
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001805 if (partial != NULL)
1806 fp = partial->pt_func;
1807 if (fp == NULL)
1808 {
1809 // Make a copy of the name, if it comes from a funcref variable it
1810 // could be changed or deleted in the called function.
1811 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
1812 if (name == NULL)
1813 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001814
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001815 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1816 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001817
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001818 if (funcexe->doesrange != NULL)
1819 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001820
1821 if (partial != NULL)
1822 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001823 // When the function has a partial with a dict and there is a dict
1824 // argument, use the dict argument. That is backwards compatible.
1825 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001826 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001827 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01001828 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001829 {
1830 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001831 {
1832 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
1833 {
Bram Moolenaaref140542019-12-31 21:27:13 +01001834 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001835 goto theend;
1836 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001837 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001838 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001839 for (i = 0; i < argcount_in; ++i)
1840 argv[i + argv_clear] = argvars_in[i];
1841 argvars = argv;
1842 argcount = partial->pt_argc + argcount_in;
1843 }
1844 }
1845
Bram Moolenaaref140542019-12-31 21:27:13 +01001846 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001847 {
1848 char_u *rfname = fname;
1849
Bram Moolenaare38eab22019-12-05 21:50:01 +01001850 // Ignore "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001851 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001852 rfname = fname + 2;
1853
Bram Moolenaare38eab22019-12-05 21:50:01 +01001854 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001855 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01001856 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001857
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001858 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001859 {
1860 /*
1861 * User defined function.
1862 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001863 if (fp == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001865
Bram Moolenaare38eab22019-12-05 21:50:01 +01001866 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001867 if (fp == NULL
1868 && apply_autocmds(EVENT_FUNCUNDEFINED,
1869 rfname, rfname, TRUE, NULL)
1870 && !aborting())
1871 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001872 // executed an autocommand, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001874 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001875 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001876 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1877 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001878 // loaded a package, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001879 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001880 }
1881
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001882 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01001883 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001884 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001885 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001886 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01001887 // postponed filling in the arguments, do it now
1888 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001889 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001890
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001891 if (funcexe->basetv != NULL)
1892 {
1893 // Method call: base->Method()
1894 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
1895 argv[0] = *funcexe->basetv;
1896 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001897 argvars = argv;
1898 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001899 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001900
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001901 error = call_user_func_check(fp, argcount, argvars, rettv,
1902 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001903 }
1904 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001905 else if (funcexe->basetv != NULL)
1906 {
1907 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001908 * expr->method(): Find the method name in the table, call its
1909 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02001910 */
1911 error = call_internal_method(fname, argcount, argvars, rettv,
1912 funcexe->basetv);
1913 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001914 else
1915 {
1916 /*
1917 * Find the function name in the table, call its implementation.
1918 */
1919 error = call_internal_func(fname, argcount, argvars, rettv);
1920 }
1921 /*
1922 * The function call (or "FuncUndefined" autocommand sequence) might
1923 * have been aborted by an error, an interrupt, or an explicitly thrown
1924 * exception that has not been caught so far. This situation can be
1925 * tested for by calling aborting(). For an error in an internal
1926 * function or for the "E132" error in call_user_func(), however, the
1927 * throw point at which the "force_abort" flag (temporarily reset by
1928 * emsg()) is normally updated has not been reached yet. We need to
1929 * update that flag first to make aborting() reliable.
1930 */
1931 update_force_abort();
1932 }
Bram Moolenaaref140542019-12-31 21:27:13 +01001933 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001934 ret = OK;
1935
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001936theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001937 /*
1938 * Report an error unless the argument evaluation or function call has been
1939 * cancelled due to an aborting error, an interrupt, or an exception.
1940 */
1941 if (!aborting())
1942 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001943 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001944 }
1945
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001946 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001947 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001948 clear_tv(&argv[--argv_clear + argv_base]);
1949
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001950 vim_free(tofree);
1951 vim_free(name);
1952
1953 return ret;
1954}
1955
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 static char_u *
1957printable_func_name(ufunc_T *fp)
1958{
1959 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
1960}
1961
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001962/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001963 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001964 */
1965 static void
1966list_func_head(ufunc_T *fp, int indent)
1967{
1968 int j;
1969
1970 msg_start();
1971 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001972 msg_puts(" ");
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001973 if (fp->uf_dfunc_idx >= 0)
1974 msg_puts("def ");
1975 else
1976 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001978 msg_putchar('(');
1979 for (j = 0; j < fp->uf_args.ga_len; ++j)
1980 {
1981 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001982 msg_puts(", ");
1983 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 if (fp->uf_arg_types != NULL)
1985 {
1986 char *tofree;
1987
1988 msg_puts(": ");
1989 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
1990 vim_free(tofree);
1991 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001992 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
1993 {
1994 msg_puts(" = ");
1995 msg_puts(((char **)(fp->uf_def_args.ga_data))
1996 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
1997 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001998 }
1999 if (fp->uf_varargs)
2000 {
2001 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002002 msg_puts(", ");
2003 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002004 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005 if (fp->uf_va_name != NULL)
2006 {
2007 if (j)
2008 msg_puts(", ");
2009 msg_puts("...");
2010 msg_puts((char *)fp->uf_va_name);
2011 if (fp->uf_va_type)
2012 {
2013 char *tofree;
2014
2015 msg_puts(": ");
2016 msg_puts(type_name(fp->uf_va_type, &tofree));
2017 vim_free(tofree);
2018 }
2019 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002020 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002021
2022 if (fp->uf_dfunc_idx >= 0)
2023 {
2024 if (fp->uf_ret_type != &t_void)
2025 {
2026 char *tofree;
2027
2028 msg_puts(": ");
2029 msg_puts(type_name(fp->uf_ret_type, &tofree));
2030 vim_free(tofree);
2031 }
2032 }
2033 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002034 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002035 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002036 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002037 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002038 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002039 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002040 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002041 msg_clr_eos();
2042 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002043 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002044}
2045
2046/*
2047 * Get a function name, translating "<SID>" and "<SNR>".
2048 * Also handles a Funcref in a List or Dictionary.
2049 * Returns the function name in allocated memory, or NULL for failure.
2050 * flags:
2051 * TFN_INT: internal function name OK
2052 * TFN_QUIET: be quiet
2053 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002054 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002055 * Advances "pp" to just after the function name (if no error).
2056 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002057 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002058trans_function_name(
2059 char_u **pp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002060 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002061 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002062 funcdict_T *fdp, // return: info about dictionary used
2063 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002064{
2065 char_u *name = NULL;
2066 char_u *start;
2067 char_u *end;
2068 int lead;
2069 char_u sid_buf[20];
2070 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002072 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002074
2075 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002076 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002077 start = *pp;
2078
Bram Moolenaare38eab22019-12-05 21:50:01 +01002079 // Check for hard coded <SNR>: already translated function ID (from a user
2080 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002081 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2082 && (*pp)[2] == (int)KE_SNR)
2083 {
2084 *pp += 3;
2085 len = get_id_len(pp) + 3;
2086 return vim_strnsave(start, len);
2087 }
2088
Bram Moolenaare38eab22019-12-05 21:50:01 +01002089 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2090 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002091 lead = eval_fname_script(start);
2092 if (lead > 2)
2093 start += lead;
2094
Bram Moolenaare38eab22019-12-05 21:50:01 +01002095 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002096 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002097 lead > 2 ? 0 : FNE_CHECK_START);
2098 if (end == start)
2099 {
2100 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002101 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002102 goto theend;
2103 }
2104 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2105 {
2106 /*
2107 * Report an invalid expression in braces, unless the expression
2108 * evaluation has been cancelled due to an aborting error, an
2109 * interrupt, or an exception.
2110 */
2111 if (!aborting())
2112 {
2113 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002114 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002115 }
2116 else
2117 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2118 goto theend;
2119 }
2120
2121 if (lv.ll_tv != NULL)
2122 {
2123 if (fdp != NULL)
2124 {
2125 fdp->fd_dict = lv.ll_dict;
2126 fdp->fd_newkey = lv.ll_newkey;
2127 lv.ll_newkey = NULL;
2128 fdp->fd_di = lv.ll_di;
2129 }
2130 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2131 {
2132 name = vim_strsave(lv.ll_tv->vval.v_string);
2133 *pp = end;
2134 }
2135 else if (lv.ll_tv->v_type == VAR_PARTIAL
2136 && lv.ll_tv->vval.v_partial != NULL)
2137 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002138 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002139 *pp = end;
2140 if (partial != NULL)
2141 *partial = lv.ll_tv->vval.v_partial;
2142 }
2143 else
2144 {
2145 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2146 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002147 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002148 else
2149 *pp = end;
2150 name = NULL;
2151 }
2152 goto theend;
2153 }
2154
2155 if (lv.ll_name == NULL)
2156 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002157 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002158 *pp = end;
2159 goto theend;
2160 }
2161
Bram Moolenaare38eab22019-12-05 21:50:01 +01002162 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002163 if (lv.ll_exp_name != NULL)
2164 {
2165 len = (int)STRLEN(lv.ll_exp_name);
2166 name = deref_func_name(lv.ll_exp_name, &len, partial,
2167 flags & TFN_NO_AUTOLOAD);
2168 if (name == lv.ll_exp_name)
2169 name = NULL;
2170 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002171 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002172 {
2173 len = (int)(end - *pp);
2174 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2175 if (name == *pp)
2176 name = NULL;
2177 }
2178 if (name != NULL)
2179 {
2180 name = vim_strsave(name);
2181 *pp = end;
2182 if (STRNCMP(name, "<SNR>", 5) == 0)
2183 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002184 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002185 name[0] = K_SPECIAL;
2186 name[1] = KS_EXTRA;
2187 name[2] = (int)KE_SNR;
2188 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2189 }
2190 goto theend;
2191 }
2192
2193 if (lv.ll_exp_name != NULL)
2194 {
2195 len = (int)STRLEN(lv.ll_exp_name);
2196 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2197 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2198 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002199 // When there was "s:" already or the name expanded to get a
2200 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002201 lv.ll_name += 2;
2202 len -= 2;
2203 lead = 2;
2204 }
2205 }
2206 else
2207 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002208 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002209 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
2210 lv.ll_name += 2;
2211 len = (int)(end - lv.ll_name);
2212 }
2213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 // In Vim9 script a user function is script-local by default.
2215 vim9script = ASCII_ISUPPER(*start)
2216 && current_sctx.sc_version == SCRIPT_VERSION_VIM9;
2217
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002218 /*
2219 * Copy the function name to allocated memory.
2220 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2221 * Accept <SNR>123_name() outside a script.
2222 */
2223 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002224 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002226 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227 if (!vim9script)
2228 lead = 3;
2229 if (vim9script || (lv.ll_exp_name != NULL
2230 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002231 || eval_fname_sid(*pp))
2232 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002234 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002235 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002236 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002237 goto theend;
2238 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002239 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002240 if (vim9script)
2241 extra = 3 + (int)STRLEN(sid_buf);
2242 else
2243 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002244 }
2245 }
2246 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2247 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002248 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002249 start);
2250 goto theend;
2251 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002252 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002253 {
2254 char_u *cp = vim_strchr(lv.ll_name, ':');
2255
2256 if (cp != NULL && cp < end)
2257 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002258 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002259 goto theend;
2260 }
2261 }
2262
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002264 if (name != NULL)
2265 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002266 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002267 {
2268 name[0] = K_SPECIAL;
2269 name[1] = KS_EXTRA;
2270 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002272 STRCPY(name + 3, sid_buf);
2273 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2275 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002276 }
2277 *pp = end;
2278
2279theend:
2280 clear_lval(&lv);
2281 return name;
2282}
2283
2284/*
2285 * ":function"
2286 */
2287 void
2288ex_function(exarg_T *eap)
2289{
2290 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002291 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002292 int j;
2293 int c;
2294 int saved_did_emsg;
2295 int saved_wait_return = need_wait_return;
2296 char_u *name = NULL;
2297 char_u *p;
2298 char_u *arg;
2299 char_u *line_arg = NULL;
2300 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002302 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002303 garray_T newlines;
2304 int varargs = FALSE;
2305 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002307 ufunc_T *fp;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002308 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002309 int indent;
2310 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311#define MAX_FUNC_NESTING 50
2312 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002313 dictitem_T *v;
2314 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002315 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002316 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002317 int todo;
2318 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002319 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002320 linenr_T sourcing_lnum_off;
2321 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002322 int is_heredoc = FALSE;
2323 char_u *skip_until = NULL;
2324 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002325
2326 /*
2327 * ":function" without argument: list functions.
2328 */
2329 if (ends_excmd(*eap->arg))
2330 {
2331 if (!eap->skip)
2332 {
2333 todo = (int)func_hashtab.ht_used;
2334 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2335 {
2336 if (!HASHITEM_EMPTY(hi))
2337 {
2338 --todo;
2339 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340 if ((fp->uf_flags & FC_DEAD)
2341 || message_filtered(fp->uf_name))
Bram Moolenaarf86db782018-10-25 13:31:37 +02002342 continue;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002343 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002344 list_func_head(fp, FALSE);
2345 }
2346 }
2347 }
2348 eap->nextcmd = check_nextcmd(eap->arg);
2349 return;
2350 }
2351
2352 /*
2353 * ":function /pat": list functions matching pattern.
2354 */
2355 if (*eap->arg == '/')
2356 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002357 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002358 if (!eap->skip)
2359 {
2360 regmatch_T regmatch;
2361
2362 c = *p;
2363 *p = NUL;
2364 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2365 *p = c;
2366 if (regmatch.regprog != NULL)
2367 {
2368 regmatch.rm_ic = p_ic;
2369
2370 todo = (int)func_hashtab.ht_used;
2371 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2372 {
2373 if (!HASHITEM_EMPTY(hi))
2374 {
2375 --todo;
2376 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377 if ((fp->uf_flags & FC_DEAD) == 0
2378 && !isdigit(*fp->uf_name)
2379 && vim_regexec(&regmatch, fp->uf_name, 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002380 list_func_head(fp, FALSE);
2381 }
2382 }
2383 vim_regfree(regmatch.regprog);
2384 }
2385 }
2386 if (*p == '/')
2387 ++p;
2388 eap->nextcmd = check_nextcmd(p);
2389 return;
2390 }
2391
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392 ga_init(&newargs);
2393 ga_init(&argtypes);
2394 ga_init(&default_args);
2395
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002396 /*
2397 * Get the function name. There are these situations:
2398 * func normal function name
2399 * "name" == func, "fudi.fd_dict" == NULL
2400 * dict.func new dictionary entry
2401 * "name" == NULL, "fudi.fd_dict" set,
2402 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2403 * dict.func existing dict entry with a Funcref
2404 * "name" == func, "fudi.fd_dict" set,
2405 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2406 * dict.func existing dict entry that's not a Funcref
2407 * "name" == NULL, "fudi.fd_dict" set,
2408 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2409 * s:func script-local function name
2410 * g:func global function name, same as "func"
2411 */
2412 p = eap->arg;
Bram Moolenaar3388d332017-12-07 22:23:04 +01002413 name = trans_function_name(&p, eap->skip, TFN_NO_AUTOLOAD, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002414 paren = (vim_strchr(p, '(') != NULL);
2415 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
2416 {
2417 /*
2418 * Return on an invalid expression in braces, unless the expression
2419 * evaluation has been cancelled due to an aborting error, an
2420 * interrupt, or an exception.
2421 */
2422 if (!aborting())
2423 {
2424 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002425 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002426 vim_free(fudi.fd_newkey);
2427 return;
2428 }
2429 else
2430 eap->skip = TRUE;
2431 }
2432
Bram Moolenaare38eab22019-12-05 21:50:01 +01002433 // An error in a function call during evaluation of an expression in magic
2434 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002435 saved_did_emsg = did_emsg;
2436 did_emsg = FALSE;
2437
2438 /*
2439 * ":function func" with only function name: list function.
2440 */
2441 if (!paren)
2442 {
2443 if (!ends_excmd(*skipwhite(p)))
2444 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002445 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002446 goto ret_free;
2447 }
2448 eap->nextcmd = check_nextcmd(p);
2449 if (eap->nextcmd != NULL)
2450 *p = NUL;
2451 if (!eap->skip && !got_int)
2452 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453 fp = find_func(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002454 if (fp != NULL)
2455 {
2456 list_func_head(fp, TRUE);
2457 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2458 {
2459 if (FUNCLINE(fp, j) == NULL)
2460 continue;
2461 msg_putchar('\n');
2462 msg_outnum((long)(j + 1));
2463 if (j < 9)
2464 msg_putchar(' ');
2465 if (j < 99)
2466 msg_putchar(' ');
2467 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002468 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002469 ui_breakcheck();
2470 }
2471 if (!got_int)
2472 {
2473 msg_putchar('\n');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 if (fp->uf_dfunc_idx >= 0)
2475 msg_puts(" enddef");
2476 else
2477 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002478 }
2479 }
2480 else
2481 emsg_funcname(N_("E123: Undefined function: %s"), name);
2482 }
2483 goto ret_free;
2484 }
2485
2486 /*
2487 * ":function name(arg1, arg2)" Define function.
2488 */
2489 p = skipwhite(p);
2490 if (*p != '(')
2491 {
2492 if (!eap->skip)
2493 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002494 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002495 goto ret_free;
2496 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002497 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002498 if (vim_strchr(p, '(') != NULL)
2499 p = vim_strchr(p, '(');
2500 }
2501 p = skipwhite(p + 1);
2502
2503 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2504
2505 if (!eap->skip)
2506 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002507 // Check the name of the function. Unless it's a dictionary function
2508 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002509 if (name != NULL)
2510 arg = name;
2511 else
2512 arg = fudi.fd_newkey;
2513 if (arg != NULL && (fudi.fd_di == NULL
2514 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2515 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2516 {
2517 if (*arg == K_SPECIAL)
2518 j = 3;
2519 else
2520 j = 0;
2521 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2522 : eval_isnamec(arg[j])))
2523 ++j;
2524 if (arg[j] != NUL)
2525 emsg_funcname((char *)e_invarg2, arg);
2526 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002527 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002528 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002529 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002530 }
2531
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002532 // This may get more lines and make the pointers into the first line
2533 // invalid.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 if (get_function_args(&p, ')', &newargs,
2535 eap->cmdidx == CMD_def ? &argtypes : NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002536 &varargs, &default_args, eap->skip,
2537 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002538 goto errret_2;
2539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002541 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 // find the return type: :def Func(): type
2543 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002544 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 ret_type = skipwhite(p + 1);
2546 p = skip_type(ret_type);
2547 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002548 {
2549 ret_type = vim_strnsave(ret_type, (int)(p - ret_type));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002551 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002553 {
2554 ret_type = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 semsg(_("E1056: expected a type: %s"), ret_type);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002556 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002557 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002558 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 else
2560 // find extra arguments "range", "dict", "abort" and "closure"
2561 for (;;)
2562 {
2563 p = skipwhite(p);
2564 if (STRNCMP(p, "range", 5) == 0)
2565 {
2566 flags |= FC_RANGE;
2567 p += 5;
2568 }
2569 else if (STRNCMP(p, "dict", 4) == 0)
2570 {
2571 flags |= FC_DICT;
2572 p += 4;
2573 }
2574 else if (STRNCMP(p, "abort", 5) == 0)
2575 {
2576 flags |= FC_ABORT;
2577 p += 5;
2578 }
2579 else if (STRNCMP(p, "closure", 7) == 0)
2580 {
2581 flags |= FC_CLOSURE;
2582 p += 7;
2583 if (current_funccal == NULL)
2584 {
2585 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
2586 name == NULL ? (char_u *)"" : name);
2587 goto erret;
2588 }
2589 }
2590 else
2591 break;
2592 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002593
Bram Moolenaare38eab22019-12-05 21:50:01 +01002594 // When there is a line break use what follows for the function body.
2595 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002596 if (*p == '\n')
2597 line_arg = p + 1;
2598 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002599 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002600
2601 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
2603 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002604 */
2605 if (KeyTyped)
2606 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002607 // Check if the function already exists, don't let the user type the
2608 // whole function before telling him it doesn't work! For a script we
2609 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002610 if (!eap->skip && !eap->forceit)
2611 {
2612 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002613 emsg(_(e_funcdict));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 else if (name != NULL && find_func(name, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002615 emsg_funcname(e_funcexts, name);
2616 }
2617
2618 if (!eap->skip && did_emsg)
2619 goto erret;
2620
Bram Moolenaare38eab22019-12-05 21:50:01 +01002621 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002622 cmdline_row = msg_row;
2623 }
2624
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002625 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002626 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002627
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002628 indent = 2;
2629 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002631 for (;;)
2632 {
2633 if (KeyTyped)
2634 {
2635 msg_scroll = TRUE;
2636 saved_wait_return = FALSE;
2637 }
2638 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002639
2640 if (line_arg != NULL)
2641 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002642 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002643 theline = line_arg;
2644 p = vim_strchr(theline, '\n');
2645 if (p == NULL)
2646 line_arg += STRLEN(line_arg);
2647 else
2648 {
2649 *p = NUL;
2650 line_arg = p + 1;
2651 }
2652 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002653 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002654 {
2655 vim_free(line_to_free);
2656 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002657 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002658 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002659 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002660 line_to_free = theline;
2661 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002662 if (KeyTyped)
2663 lines_left = Rows - 1;
2664 if (theline == NULL)
2665 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 if (eap->cmdidx == CMD_def)
2667 emsg(_("E1057: Missing :enddef"));
2668 else
2669 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002670 goto erret;
2671 }
2672
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002673 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002674 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002675 if (SOURCING_LNUM < sourcing_lnum_off)
2676 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002677 else
2678 sourcing_lnum_off = 0;
2679
2680 if (skip_until != NULL)
2681 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002683 // * ":append" and "."
2684 // * ":python <<EOF" and "EOF"
2685 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2686 if (heredoc_trimmed == NULL
2687 || (is_heredoc && skipwhite(theline) == theline)
2688 || STRNCMP(theline, heredoc_trimmed,
2689 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002690 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002691 if (heredoc_trimmed == NULL)
2692 p = theline;
2693 else if (is_heredoc)
2694 p = skipwhite(theline) == theline
2695 ? theline : theline + STRLEN(heredoc_trimmed);
2696 else
2697 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002698 if (STRCMP(p, skip_until) == 0)
2699 {
2700 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002701 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002702 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002703 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002704 }
2705 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002706 }
2707 else
2708 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002709 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01002710 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002711 ;
2712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 // Check for "endfunction" or "enddef".
2714 if (checkforcmd(&p, nesting_def[nesting]
2715 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002716 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002717 char_u *nextcmd = NULL;
2718
Bram Moolenaar663bb232017-06-22 19:12:10 +02002719 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002720 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002721 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002722 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002723 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 give_warning2(eap->cmdidx == CMD_def
2725 ? (char_u *)_("W1001: Text found after :enddef: %s")
2726 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002727 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002728 if (nextcmd != NULL)
2729 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002730 // Another command follows. If the line came from "eap" we
2731 // can simply point into it, otherwise we need to change
2732 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02002733 eap->nextcmd = nextcmd;
2734 if (line_to_free != NULL)
2735 {
2736 vim_free(*eap->cmdlinep);
2737 *eap->cmdlinep = line_to_free;
2738 line_to_free = NULL;
2739 }
2740 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002741 break;
2742 }
2743
Bram Moolenaare38eab22019-12-05 21:50:01 +01002744 // Increase indent inside "if", "while", "for" and "try", decrease
2745 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002747 indent -= 2;
2748 else if (STRNCMP(p, "if", 2) == 0
2749 || STRNCMP(p, "wh", 2) == 0
2750 || STRNCMP(p, "for", 3) == 0
2751 || STRNCMP(p, "try", 3) == 0)
2752 indent += 2;
2753
Bram Moolenaare38eab22019-12-05 21:50:01 +01002754 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01002755 // Only recognize "def" inside "def", not inside "function",
2756 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01002758 if (checkforcmd(&p, "function", 2)
2759 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002760 {
2761 if (*p == '!')
2762 p = skipwhite(p + 1);
2763 p += eval_fname_script(p);
2764 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
2765 if (*skipwhite(p) == '(')
2766 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 if (nesting == MAX_FUNC_NESTING - 1)
2768 emsg(_("E1058: function nesting too deep"));
2769 else
2770 {
2771 ++nesting;
2772 nesting_def[nesting] = (c == 'd');
2773 indent += 2;
2774 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002775 }
2776 }
2777
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002778 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002779 p = skip_range(p, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002780 if (eap->cmdidx != CMD_def
2781 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002782 || (p[0] == 'c'
2783 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2784 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2785 && (STRNCMP(&p[3], "nge", 3) != 0
2786 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002787 || (p[0] == 'i'
2788 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002789 && (!ASCII_ISALPHA(p[2])
2790 || (p[2] == 's'
2791 && (!ASCII_ISALPHA(p[3])
2792 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002793 skip_until = vim_strsave((char_u *)".");
2794
Bram Moolenaare38eab22019-12-05 21:50:01 +01002795 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002796 arg = skipwhite(skiptowhite(p));
2797 if (arg[0] == '<' && arg[1] =='<'
2798 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002799 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2800 || ((p[2] == '3' || p[2] == 'x')
2801 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002802 || (p[0] == 'p' && p[1] == 'e'
2803 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2804 || (p[0] == 't' && p[1] == 'c'
2805 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2806 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2807 && !ASCII_ISALPHA(p[3]))
2808 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2809 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2810 || (p[0] == 'm' && p[1] == 'z'
2811 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2812 ))
2813 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002814 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002815 p = skipwhite(arg + 2);
2816 if (*p == NUL)
2817 skip_until = vim_strsave((char_u *)".");
2818 else
2819 skip_until = vim_strsave(p);
2820 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002821
2822 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002823 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02002824 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002825 if (*arg == '[')
2826 arg = vim_strchr(arg, ']');
2827 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002828 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002829 arg = skipwhite(skiptowhite(arg));
2830 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
2831 && ((p[0] == 'l'
2832 && p[1] == 'e'
2833 && (!ASCII_ISALNUM(p[2])
2834 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02002835 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002836 p = skipwhite(arg + 3);
2837 if (STRNCMP(p, "trim", 4) == 0)
2838 {
2839 // Ignore leading white space.
2840 p = skipwhite(p + 4);
2841 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar8471e572019-05-19 21:37:18 +02002842 (int)(skipwhite(theline) - theline));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002843 }
2844 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2845 do_concat = FALSE;
2846 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002847 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002848 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002849 }
2850
Bram Moolenaare38eab22019-12-05 21:50:01 +01002851 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002852 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002853 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002854
Bram Moolenaare38eab22019-12-05 21:50:01 +01002855 // Copy the line to newly allocated memory. get_one_sourceline()
2856 // allocates 250 bytes per line, this saves 80% on average. The cost
2857 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002858 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002859 if (p == NULL)
2860 goto erret;
2861 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002862
Bram Moolenaare38eab22019-12-05 21:50:01 +01002863 // Add NULL lines for continuation lines, so that the line count is
2864 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002865 while (sourcing_lnum_off-- > 0)
2866 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2867
Bram Moolenaare38eab22019-12-05 21:50:01 +01002868 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002869 if (line_arg != NULL && *line_arg == NUL)
2870 line_arg = NULL;
2871 }
2872
Bram Moolenaare38eab22019-12-05 21:50:01 +01002873 // Don't define the function when skipping commands or when an error was
2874 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002875 if (eap->skip || did_emsg)
2876 goto erret;
2877
2878 /*
2879 * If there are no errors, add the function
2880 */
2881 if (fudi.fd_dict == NULL)
2882 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 hashtab_T *ht;
2884
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002885 v = find_var(name, &ht, FALSE);
2886 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
2887 {
2888 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
2889 name);
2890 goto erret;
2891 }
2892
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 fp = find_func_even_dead(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002894 if (fp != NULL)
2895 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 int dead = fp->uf_flags & FC_DEAD;
2897
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002898 // Function can be replaced with "function!" and when sourcing the
2899 // same script again, but only once.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 if (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002901 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
2902 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002903 {
2904 emsg_funcname(e_funcexts, name);
2905 goto erret;
2906 }
2907 if (fp->uf_calls > 0)
2908 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002909 emsg_funcname(
2910 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002911 name);
2912 goto erret;
2913 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002914 if (fp->uf_refcount > 1)
2915 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002916 // This function is referenced somewhere, don't redefine it but
2917 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002918 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002919 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002920 fp = NULL;
2921 overwrite = TRUE;
2922 }
2923 else
2924 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002925 char_u *exp_name = fp->uf_name_exp;
2926
2927 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01002928 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002929 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002930 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002931 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002933#ifdef FEAT_PROFILE
2934 fp->uf_profiling = FALSE;
2935 fp->uf_prof_initialized = FALSE;
2936#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002937 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002938 }
2939 }
2940 else
2941 {
2942 char numbuf[20];
2943
2944 fp = NULL;
2945 if (fudi.fd_newkey == NULL && !eap->forceit)
2946 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002947 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002948 goto erret;
2949 }
2950 if (fudi.fd_di == NULL)
2951 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002952 // Can't add a function to a locked dictionary
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002953 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002954 goto erret;
2955 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002956 // Can't change an existing function if it is locked
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002957 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002958 goto erret;
2959
Bram Moolenaare38eab22019-12-05 21:50:01 +01002960 // Give the function a sequential number. Can only be used with a
2961 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002962 vim_free(name);
2963 sprintf(numbuf, "%d", ++func_nr);
2964 name = vim_strsave((char_u *)numbuf);
2965 if (name == NULL)
2966 goto erret;
2967 }
2968
2969 if (fp == NULL)
2970 {
2971 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
2972 {
2973 int slen, plen;
2974 char_u *scriptname;
2975
Bram Moolenaare38eab22019-12-05 21:50:01 +01002976 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002977 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002978 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002979 {
2980 scriptname = autoload_name(name);
2981 if (scriptname != NULL)
2982 {
2983 p = vim_strchr(scriptname, '/');
2984 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002985 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002986 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002987 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002988 j = OK;
2989 vim_free(scriptname);
2990 }
2991 }
2992 if (j == FAIL)
2993 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002994 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002995 goto erret;
2996 }
2997 }
2998
Bram Moolenaar47ed5532019-08-08 20:49:14 +02002999 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003000 if (fp == NULL)
3001 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 fp->uf_dfunc_idx = -1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003003
3004 if (fudi.fd_dict != NULL)
3005 {
3006 if (fudi.fd_di == NULL)
3007 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003008 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003009 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3010 if (fudi.fd_di == NULL)
3011 {
3012 vim_free(fp);
3013 goto erret;
3014 }
3015 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3016 {
3017 vim_free(fudi.fd_di);
3018 vim_free(fp);
3019 goto erret;
3020 }
3021 }
3022 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003023 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003024 clear_tv(&fudi.fd_di->di_tv);
3025 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003026 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003027
Bram Moolenaare38eab22019-12-05 21:50:01 +01003028 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003029 flags |= FC_DICT;
3030 }
3031
Bram Moolenaare38eab22019-12-05 21:50:01 +01003032 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003033 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003034 if (overwrite)
3035 {
3036 hi = hash_find(&func_hashtab, name);
3037 hi->hi_key = UF2HIKEY(fp);
3038 }
3039 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003040 {
3041 vim_free(fp);
3042 goto erret;
3043 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003044 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003045 }
3046 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003047 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003049 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050
3051 if (eap->cmdidx == CMD_def)
3052 {
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003053 int lnum_save = SOURCING_LNUM;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003054
3055 // error messages are for the first function line
3056 SOURCING_LNUM = sourcing_lnum_top;
3057
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 // parse the argument types
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003059 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060
3061 if (argtypes.ga_len > 0)
3062 {
3063 // When "varargs" is set the last name/type goes into uf_va_name
3064 // and uf_va_type.
3065 int len = argtypes.ga_len - (varargs ? 1 : 0);
3066
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003067 if (len > 0)
3068 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 if (fp->uf_arg_types != NULL)
3070 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003071 int i;
3072 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073
3074 for (i = 0; i < len; ++ i)
3075 {
3076 p = ((char_u **)argtypes.ga_data)[i];
3077 if (p == NULL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003078 // will get the type from the default value
3079 type = &t_unknown;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 else
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003081 type = parse_type(&p, &fp->uf_type_list);
3082 if (type == NULL)
3083 {
3084 SOURCING_LNUM = lnum_save;
3085 goto errret_2;
3086 }
3087 fp->uf_arg_types[i] = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 }
3089 }
3090 if (varargs)
3091 {
3092 // Move the last argument "...name: type" to uf_va_name and
3093 // uf_va_type.
3094 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
3095 [fp->uf_args.ga_len - 1];
3096 --fp->uf_args.ga_len;
3097 p = ((char_u **)argtypes.ga_data)[len];
3098 if (p == NULL)
3099 // todo: get type from default value
3100 fp->uf_va_type = &t_any;
3101 else
3102 fp->uf_va_type = parse_type(&p, &fp->uf_type_list);
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003103 if (fp->uf_va_type == NULL)
3104 {
3105 SOURCING_LNUM = lnum_save;
3106 goto errret_2;
3107 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 }
3109 varargs = FALSE;
3110 }
3111
3112 // parse the return type, if any
3113 if (ret_type == NULL)
3114 fp->uf_ret_type = &t_void;
3115 else
3116 {
3117 p = ret_type;
3118 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list);
3119 }
3120 }
3121
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003122 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003123 if ((flags & FC_CLOSURE) != 0)
3124 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003125 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003126 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003127 }
3128 else
3129 fp->uf_scoped = NULL;
3130
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003131#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003132 if (prof_def_func())
3133 func_do_profile(fp);
3134#endif
3135 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003136 if (sandbox)
3137 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003138 fp->uf_flags = flags;
3139 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003140 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003141 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003142 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 if (is_export)
3144 {
3145 fp->uf_flags |= FC_EXPORT;
3146 // let ex_export() know the export worked.
3147 is_export = FALSE;
3148 }
3149
3150 // ":def Func()" needs to be compiled
3151 if (eap->cmdidx == CMD_def)
3152 compile_def_function(fp, FALSE);
3153
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003154 goto ret_free;
3155
3156erret:
3157 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003158 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003159errret_2:
3160 ga_clear_strings(&newlines);
3161ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003162 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003163 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003164 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003165 vim_free(fudi.fd_newkey);
3166 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003167 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003168 did_emsg |= saved_did_emsg;
3169 need_wait_return |= saved_wait_return;
3170}
3171
3172/*
3173 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3174 * Return 2 if "p" starts with "s:".
3175 * Return 0 otherwise.
3176 */
3177 int
3178eval_fname_script(char_u *p)
3179{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003180 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3181 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003182 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3183 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3184 return 5;
3185 if (p[0] == 's' && p[1] == ':')
3186 return 2;
3187 return 0;
3188}
3189
3190 int
3191translated_function_exists(char_u *name)
3192{
3193 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003194 return has_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 return find_func(name, NULL) != NULL;
3196}
3197
3198/*
3199 * Return TRUE when "ufunc" has old-style "..." varargs
3200 * or named varargs "...name: type".
3201 */
3202 int
3203has_varargs(ufunc_T *ufunc)
3204{
3205 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003206}
3207
3208/*
3209 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003210 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003211 */
3212 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003213function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003214{
3215 char_u *nm = name;
3216 char_u *p;
3217 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003218 int flag;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003219
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003220 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3221 if (no_deref)
3222 flag |= TFN_NO_DEREF;
3223 p = trans_function_name(&nm, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003224 nm = skipwhite(nm);
3225
Bram Moolenaare38eab22019-12-05 21:50:01 +01003226 // Only accept "funcname", "funcname ", "funcname (..." and
3227 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003228 if (p != NULL && (*nm == NUL || *nm == '('))
3229 n = translated_function_exists(p);
3230 vim_free(p);
3231 return n;
3232}
3233
Bram Moolenaar113e1072019-01-20 15:30:40 +01003234#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003235 char_u *
3236get_expanded_name(char_u *name, int check)
3237{
3238 char_u *nm = name;
3239 char_u *p;
3240
3241 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
3242
3243 if (p != NULL && *nm == NUL)
3244 if (!check || translated_function_exists(p))
3245 return p;
3246
3247 vim_free(p);
3248 return NULL;
3249}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003250#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003251
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003252/*
3253 * Function given to ExpandGeneric() to obtain the list of user defined
3254 * function names.
3255 */
3256 char_u *
3257get_user_func_name(expand_T *xp, int idx)
3258{
3259 static long_u done;
3260 static hashitem_T *hi;
3261 ufunc_T *fp;
3262
3263 if (idx == 0)
3264 {
3265 done = 0;
3266 hi = func_hashtab.ht_array;
3267 }
3268 if (done < func_hashtab.ht_used)
3269 {
3270 if (done++ > 0)
3271 ++hi;
3272 while (HASHITEM_EMPTY(hi))
3273 ++hi;
3274 fp = HI2UF(hi);
3275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 // don't show dead, dict and lambda functions
3277 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02003278 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003280
3281 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003282 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003283
3284 cat_func_name(IObuff, fp);
3285 if (xp->xp_context != EXPAND_USER_FUNC)
3286 {
3287 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003289 STRCAT(IObuff, ")");
3290 }
3291 return IObuff;
3292 }
3293 return NULL;
3294}
3295
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003296/*
3297 * ":delfunction {name}"
3298 */
3299 void
3300ex_delfunction(exarg_T *eap)
3301{
3302 ufunc_T *fp = NULL;
3303 char_u *p;
3304 char_u *name;
3305 funcdict_T fudi;
3306
3307 p = eap->arg;
3308 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
3309 vim_free(fudi.fd_newkey);
3310 if (name == NULL)
3311 {
3312 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003313 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003314 return;
3315 }
3316 if (!ends_excmd(*skipwhite(p)))
3317 {
3318 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003319 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003320 return;
3321 }
3322 eap->nextcmd = check_nextcmd(p);
3323 if (eap->nextcmd != NULL)
3324 *p = NUL;
3325
3326 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 fp = find_func(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003328 vim_free(name);
3329
3330 if (!eap->skip)
3331 {
3332 if (fp == NULL)
3333 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02003334 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003335 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003336 return;
3337 }
3338 if (fp->uf_calls > 0)
3339 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003340 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003341 return;
3342 }
3343
3344 if (fudi.fd_dict != NULL)
3345 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003346 // Delete the dict item that refers to the function, it will
3347 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003348 dictitem_remove(fudi.fd_dict, fudi.fd_di);
3349 }
3350 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003351 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003352 // A normal function (not a numbered function or lambda) has a
3353 // refcount of 1 for the entry in the hashtable. When deleting
3354 // it and the refcount is more than one, it should be kept.
3355 // A numbered function and lambda should be kept if the refcount is
3356 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003357 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003358 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003359 // Function is still referenced somewhere. Don't free it but
3360 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003361 if (func_remove(fp))
3362 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003363 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003364 }
3365 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003366 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003367 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003368 }
3369}
3370
3371/*
3372 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003373 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003374 */
3375 void
3376func_unref(char_u *name)
3377{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003378 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003379
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003380 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003381 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003383 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003384 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003385#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003386 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003387#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01003388 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003389 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003390 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003391 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003392 // Only delete it when it's not being used. Otherwise it's done
3393 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003394 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003395 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02003396 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003397}
3398
3399/*
3400 * Unreference a Function: decrement the reference count and free it when it
3401 * becomes zero.
3402 */
3403 void
3404func_ptr_unref(ufunc_T *fp)
3405{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003406 if (fp != NULL && --fp->uf_refcount <= 0)
3407 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003408 // Only delete it when it's not being used. Otherwise it's done
3409 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02003410 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003411 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003412 }
3413}
3414
3415/*
3416 * Count a reference to a Function.
3417 */
3418 void
3419func_ref(char_u *name)
3420{
3421 ufunc_T *fp;
3422
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003423 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003424 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003426 if (fp != NULL)
3427 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003428 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01003429 // Only give an error for a numbered function.
3430 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01003431 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003432}
3433
3434/*
3435 * Count a reference to a Function.
3436 */
3437 void
3438func_ptr_ref(ufunc_T *fp)
3439{
3440 if (fp != NULL)
3441 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003442}
3443
3444/*
3445 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3446 * referenced from anywhere that is in use.
3447 */
3448 static int
3449can_free_funccal(funccall_T *fc, int copyID)
3450{
3451 return (fc->l_varlist.lv_copyID != copyID
3452 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003453 && fc->l_avars.dv_copyID != copyID
3454 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003455}
3456
3457/*
3458 * ":return [expr]"
3459 */
3460 void
3461ex_return(exarg_T *eap)
3462{
3463 char_u *arg = eap->arg;
3464 typval_T rettv;
3465 int returning = FALSE;
3466
3467 if (current_funccal == NULL)
3468 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003469 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003470 return;
3471 }
3472
3473 if (eap->skip)
3474 ++emsg_skip;
3475
3476 eap->nextcmd = NULL;
3477 if ((*arg != NUL && *arg != '|' && *arg != '\n')
3478 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
3479 {
3480 if (!eap->skip)
3481 returning = do_return(eap, FALSE, TRUE, &rettv);
3482 else
3483 clear_tv(&rettv);
3484 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003485 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003486 else if (!eap->skip)
3487 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003488 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003489 update_force_abort();
3490
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003491 /*
3492 * Return unless the expression evaluation has been cancelled due to an
3493 * aborting error, an interrupt, or an exception.
3494 */
3495 if (!aborting())
3496 returning = do_return(eap, FALSE, TRUE, NULL);
3497 }
3498
Bram Moolenaare38eab22019-12-05 21:50:01 +01003499 // When skipping or the return gets pending, advance to the next command
3500 // in this line (!returning). Otherwise, ignore the rest of the line.
3501 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003502 if (returning)
3503 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003504 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003505 eap->nextcmd = check_nextcmd(arg);
3506
3507 if (eap->skip)
3508 --emsg_skip;
3509}
3510
3511/*
3512 * ":1,25call func(arg1, arg2)" function call.
3513 */
3514 void
3515ex_call(exarg_T *eap)
3516{
3517 char_u *arg = eap->arg;
3518 char_u *startarg;
3519 char_u *name;
3520 char_u *tofree;
3521 int len;
3522 typval_T rettv;
3523 linenr_T lnum;
3524 int doesrange;
3525 int failed = FALSE;
3526 funcdict_T fudi;
3527 partial_T *partial = NULL;
3528
3529 if (eap->skip)
3530 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003531 // trans_function_name() doesn't work well when skipping, use eval0()
3532 // instead to skip to any following command, e.g. for:
3533 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003534 ++emsg_skip;
3535 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3536 clear_tv(&rettv);
3537 --emsg_skip;
3538 return;
3539 }
3540
3541 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
3542 if (fudi.fd_newkey != NULL)
3543 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003544 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003545 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003546 vim_free(fudi.fd_newkey);
3547 }
3548 if (tofree == NULL)
3549 return;
3550
Bram Moolenaare38eab22019-12-05 21:50:01 +01003551 // Increase refcount on dictionary, it could get deleted when evaluating
3552 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003553 if (fudi.fd_dict != NULL)
3554 ++fudi.fd_dict->dv_refcount;
3555
Bram Moolenaare38eab22019-12-05 21:50:01 +01003556 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3557 // contents. For VAR_PARTIAL get its partial, unless we already have one
3558 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003559 len = (int)STRLEN(tofree);
3560 name = deref_func_name(tofree, &len,
3561 partial != NULL ? NULL : &partial, FALSE);
3562
Bram Moolenaare38eab22019-12-05 21:50:01 +01003563 // Skip white space to allow ":call func ()". Not good, but required for
3564 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003565 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003566 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003567
3568 if (*startarg != '(')
3569 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003571 goto end;
3572 }
3573
3574 /*
3575 * When skipping, evaluate the function once, to find the end of the
3576 * arguments.
3577 * When the function takes a range, this is discovered after the first
3578 * call, and the loop is broken.
3579 */
3580 if (eap->skip)
3581 {
3582 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003583 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003584 }
3585 else
3586 lnum = eap->line1;
3587 for ( ; lnum <= eap->line2; ++lnum)
3588 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003589 funcexe_T funcexe;
3590
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003591 if (!eap->skip && eap->addr_count > 0)
3592 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003593 if (lnum > curbuf->b_ml.ml_line_count)
3594 {
3595 // If the function deleted lines or switched to another buffer
3596 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003597 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003598 break;
3599 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003600 curwin->w_cursor.lnum = lnum;
3601 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003602 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003603 }
3604 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003605
Bram Moolenaara80faa82020-04-12 19:37:17 +02003606 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003607 funcexe.firstline = eap->line1;
3608 funcexe.lastline = eap->line2;
3609 funcexe.doesrange = &doesrange;
3610 funcexe.evaluate = !eap->skip;
3611 funcexe.partial = partial;
3612 funcexe.selfdict = fudi.fd_dict;
3613 if (get_func_tv(name, -1, &rettv, &arg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003614 {
3615 failed = TRUE;
3616 break;
3617 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003618 if (has_watchexpr())
3619 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003620
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003621 // Handle a function returning a Funcref, Dictionary or List.
3622 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE,
3623 name, &name) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003624 {
3625 failed = TRUE;
3626 break;
3627 }
3628
3629 clear_tv(&rettv);
3630 if (doesrange || eap->skip)
3631 break;
3632
Bram Moolenaare38eab22019-12-05 21:50:01 +01003633 // Stop when immediately aborting on error, or when an interrupt
3634 // occurred or an exception was thrown but not caught.
3635 // get_func_tv() returned OK, so that the check for trailing
3636 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003637 if (aborting())
3638 break;
3639 }
3640 if (eap->skip)
3641 --emsg_skip;
3642
Bram Moolenaare51bb172020-02-16 19:42:23 +01003643 // When inside :try we need to check for following "| catch".
3644 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003645 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003646 // Check for trailing illegal characters and a following command.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003647 if (!ends_excmd(*arg))
3648 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01003649 if (!failed)
3650 {
3651 emsg_severe = TRUE;
3652 emsg(_(e_trailing));
3653 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003654 }
3655 else
3656 eap->nextcmd = check_nextcmd(arg);
3657 }
3658
3659end:
3660 dict_unref(fudi.fd_dict);
3661 vim_free(tofree);
3662}
3663
3664/*
3665 * Return from a function. Possibly makes the return pending. Also called
3666 * for a pending return at the ":endtry" or after returning from an extra
3667 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3668 * when called due to a ":return" command. "rettv" may point to a typval_T
3669 * with the return rettv. Returns TRUE when the return can be carried out,
3670 * FALSE when the return gets pending.
3671 */
3672 int
3673do_return(
3674 exarg_T *eap,
3675 int reanimate,
3676 int is_cmd,
3677 void *rettv)
3678{
3679 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01003680 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003681
3682 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003683 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003684 current_funccal->returned = FALSE;
3685
3686 /*
3687 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3688 * not in its finally clause (which then is to be executed next) is found.
3689 * In this case, make the ":return" pending for execution at the ":endtry".
3690 * Otherwise, return normally.
3691 */
3692 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3693 if (idx >= 0)
3694 {
3695 cstack->cs_pending[idx] = CSTP_RETURN;
3696
3697 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003698 // A pending return again gets pending. "rettv" points to an
3699 // allocated variable with the rettv of the original ":return"'s
3700 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003701 cstack->cs_rettv[idx] = rettv;
3702 else
3703 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003704 // When undoing a return in order to make it pending, get the stored
3705 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003706 if (reanimate)
3707 rettv = current_funccal->rettv;
3708
3709 if (rettv != NULL)
3710 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003711 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003712 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3713 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3714 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003715 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003716 }
3717 else
3718 cstack->cs_rettv[idx] = NULL;
3719
3720 if (reanimate)
3721 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003722 // The pending return value could be overwritten by a ":return"
3723 // without argument in a finally clause; reset the default
3724 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003725 current_funccal->rettv->v_type = VAR_NUMBER;
3726 current_funccal->rettv->vval.v_number = 0;
3727 }
3728 }
3729 report_make_pending(CSTP_RETURN, rettv);
3730 }
3731 else
3732 {
3733 current_funccal->returned = TRUE;
3734
Bram Moolenaare38eab22019-12-05 21:50:01 +01003735 // If the return is carried out now, store the return value. For
3736 // a return immediately after reanimation, the value is already
3737 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003738 if (!reanimate && rettv != NULL)
3739 {
3740 clear_tv(current_funccal->rettv);
3741 *current_funccal->rettv = *(typval_T *)rettv;
3742 if (!is_cmd)
3743 vim_free(rettv);
3744 }
3745 }
3746
3747 return idx < 0;
3748}
3749
3750/*
3751 * Free the variable with a pending return value.
3752 */
3753 void
3754discard_pending_return(void *rettv)
3755{
3756 free_tv((typval_T *)rettv);
3757}
3758
3759/*
3760 * Generate a return command for producing the value of "rettv". The result
3761 * is an allocated string. Used by report_pending() for verbose messages.
3762 */
3763 char_u *
3764get_return_cmd(void *rettv)
3765{
3766 char_u *s = NULL;
3767 char_u *tofree = NULL;
3768 char_u numbuf[NUMBUFLEN];
3769
3770 if (rettv != NULL)
3771 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3772 if (s == NULL)
3773 s = (char_u *)"";
3774
3775 STRCPY(IObuff, ":return ");
3776 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3777 if (STRLEN(s) + 8 >= IOSIZE)
3778 STRCPY(IObuff + IOSIZE - 4, "...");
3779 vim_free(tofree);
3780 return vim_strsave(IObuff);
3781}
3782
3783/*
3784 * Get next function line.
3785 * Called by do_cmdline() to get the next line.
3786 * Returns allocated string, or NULL for end of function.
3787 */
3788 char_u *
3789get_func_line(
3790 int c UNUSED,
3791 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003792 int indent UNUSED,
3793 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003794{
3795 funccall_T *fcp = (funccall_T *)cookie;
3796 ufunc_T *fp = fcp->func;
3797 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003798 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003799
Bram Moolenaare38eab22019-12-05 21:50:01 +01003800 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003801 if (fcp->dbg_tick != debug_tick)
3802 {
3803 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003804 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003805 fcp->dbg_tick = debug_tick;
3806 }
3807#ifdef FEAT_PROFILE
3808 if (do_profiling == PROF_YES)
3809 func_line_end(cookie);
3810#endif
3811
3812 gap = &fp->uf_lines;
3813 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3814 || fcp->returned)
3815 retval = NULL;
3816 else
3817 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003818 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003819 while (fcp->linenr < gap->ga_len
3820 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3821 ++fcp->linenr;
3822 if (fcp->linenr >= gap->ga_len)
3823 retval = NULL;
3824 else
3825 {
3826 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003827 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003828#ifdef FEAT_PROFILE
3829 if (do_profiling == PROF_YES)
3830 func_line_start(cookie);
3831#endif
3832 }
3833 }
3834
Bram Moolenaare38eab22019-12-05 21:50:01 +01003835 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003836 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003837 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003838 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003839 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003840 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003841 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003842 fcp->dbg_tick = debug_tick;
3843 }
3844
3845 return retval;
3846}
3847
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003848/*
3849 * Return TRUE if the currently active function should be ended, because a
3850 * return was encountered or an error occurred. Used inside a ":while".
3851 */
3852 int
3853func_has_ended(void *cookie)
3854{
3855 funccall_T *fcp = (funccall_T *)cookie;
3856
Bram Moolenaare38eab22019-12-05 21:50:01 +01003857 // Ignore the "abort" flag if the abortion behavior has been changed due to
3858 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003859 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3860 || fcp->returned);
3861}
3862
3863/*
3864 * return TRUE if cookie indicates a function which "abort"s on errors.
3865 */
3866 int
3867func_has_abort(
3868 void *cookie)
3869{
3870 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
3871}
3872
3873
3874/*
3875 * Turn "dict.Func" into a partial for "Func" bound to "dict".
3876 * Don't do this when "Func" is already a partial that was bound
3877 * explicitly (pt_auto is FALSE).
3878 * Changes "rettv" in-place.
3879 * Returns the updated "selfdict_in".
3880 */
3881 dict_T *
3882make_partial(dict_T *selfdict_in, typval_T *rettv)
3883{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003884 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003885 char_u *tofree = NULL;
3886 ufunc_T *fp;
3887 char_u fname_buf[FLEN_FIXED + 1];
3888 int error;
3889 dict_T *selfdict = selfdict_in;
3890
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003891 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
3892 fp = rettv->vval.v_partial->pt_func;
3893 else
3894 {
3895 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
3896 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003897 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003898 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003900 vim_free(tofree);
3901 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003902
3903 if (fp != NULL && (fp->uf_flags & FC_DICT))
3904 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003905 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003906
3907 if (pt != NULL)
3908 {
3909 pt->pt_refcount = 1;
3910 pt->pt_dict = selfdict;
3911 pt->pt_auto = TRUE;
3912 selfdict = NULL;
3913 if (rettv->v_type == VAR_FUNC)
3914 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003915 // Just a function: Take over the function name and use
3916 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003917 pt->pt_name = rettv->vval.v_string;
3918 }
3919 else
3920 {
3921 partial_T *ret_pt = rettv->vval.v_partial;
3922 int i;
3923
Bram Moolenaare38eab22019-12-05 21:50:01 +01003924 // Partial: copy the function name, use selfdict and copy
3925 // args. Can't take over name or args, the partial might
3926 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003927 if (ret_pt->pt_name != NULL)
3928 {
3929 pt->pt_name = vim_strsave(ret_pt->pt_name);
3930 func_ref(pt->pt_name);
3931 }
3932 else
3933 {
3934 pt->pt_func = ret_pt->pt_func;
3935 func_ptr_ref(pt->pt_func);
3936 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003937 if (ret_pt->pt_argc > 0)
3938 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003939 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003940 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003941 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003942 pt->pt_argc = 0;
3943 else
3944 {
3945 pt->pt_argc = ret_pt->pt_argc;
3946 for (i = 0; i < pt->pt_argc; i++)
3947 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
3948 }
3949 }
3950 partial_unref(ret_pt);
3951 }
3952 rettv->v_type = VAR_PARTIAL;
3953 rettv->vval.v_partial = pt;
3954 }
3955 }
3956 return selfdict;
3957}
3958
3959/*
3960 * Return the name of the executed function.
3961 */
3962 char_u *
3963func_name(void *cookie)
3964{
3965 return ((funccall_T *)cookie)->func->uf_name;
3966}
3967
3968/*
3969 * Return the address holding the next breakpoint line for a funccall cookie.
3970 */
3971 linenr_T *
3972func_breakpoint(void *cookie)
3973{
3974 return &((funccall_T *)cookie)->breakpoint;
3975}
3976
3977/*
3978 * Return the address holding the debug tick for a funccall cookie.
3979 */
3980 int *
3981func_dbg_tick(void *cookie)
3982{
3983 return &((funccall_T *)cookie)->dbg_tick;
3984}
3985
3986/*
3987 * Return the nesting level for a funccall cookie.
3988 */
3989 int
3990func_level(void *cookie)
3991{
3992 return ((funccall_T *)cookie)->level;
3993}
3994
3995/*
3996 * Return TRUE when a function was ended by a ":return" command.
3997 */
3998 int
3999current_func_returned(void)
4000{
4001 return current_funccal->returned;
4002}
4003
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004004 int
4005free_unref_funccal(int copyID, int testing)
4006{
4007 int did_free = FALSE;
4008 int did_free_funccal = FALSE;
4009 funccall_T *fc, **pfc;
4010
4011 for (pfc = &previous_funccal; *pfc != NULL; )
4012 {
4013 if (can_free_funccal(*pfc, copyID))
4014 {
4015 fc = *pfc;
4016 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004017 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004018 did_free = TRUE;
4019 did_free_funccal = TRUE;
4020 }
4021 else
4022 pfc = &(*pfc)->caller;
4023 }
4024 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004025 // When a funccal was freed some more items might be garbage
4026 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004027 (void)garbage_collect(testing);
4028
4029 return did_free;
4030}
4031
4032/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004033 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004034 */
4035 static funccall_T *
4036get_funccal(void)
4037{
4038 int i;
4039 funccall_T *funccal;
4040 funccall_T *temp_funccal;
4041
4042 funccal = current_funccal;
4043 if (debug_backtrace_level > 0)
4044 {
4045 for (i = 0; i < debug_backtrace_level; i++)
4046 {
4047 temp_funccal = funccal->caller;
4048 if (temp_funccal)
4049 funccal = temp_funccal;
4050 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004051 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004052 debug_backtrace_level = i;
4053 }
4054 }
4055 return funccal;
4056}
4057
4058/*
4059 * Return the hashtable used for local variables in the current funccal.
4060 * Return NULL if there is no current funccal.
4061 */
4062 hashtab_T *
4063get_funccal_local_ht()
4064{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004066 return NULL;
4067 return &get_funccal()->l_vars.dv_hashtab;
4068}
4069
4070/*
4071 * Return the l: scope variable.
4072 * Return NULL if there is no current funccal.
4073 */
4074 dictitem_T *
4075get_funccal_local_var()
4076{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004078 return NULL;
4079 return &get_funccal()->l_vars_var;
4080}
4081
4082/*
4083 * Return the hashtable used for argument in the current funccal.
4084 * Return NULL if there is no current funccal.
4085 */
4086 hashtab_T *
4087get_funccal_args_ht()
4088{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004090 return NULL;
4091 return &get_funccal()->l_avars.dv_hashtab;
4092}
4093
4094/*
4095 * Return the a: scope variable.
4096 * Return NULL if there is no current funccal.
4097 */
4098 dictitem_T *
4099get_funccal_args_var()
4100{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004102 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004103 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004104}
4105
4106/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004107 * List function variables, if there is a function.
4108 */
4109 void
4110list_func_vars(int *first)
4111{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004113 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004114 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004115}
4116
4117/*
4118 * If "ht" is the hashtable for local variables in the current funccal, return
4119 * the dict that contains it.
4120 * Otherwise return NULL.
4121 */
4122 dict_T *
4123get_current_funccal_dict(hashtab_T *ht)
4124{
4125 if (current_funccal != NULL
4126 && ht == &current_funccal->l_vars.dv_hashtab)
4127 return &current_funccal->l_vars;
4128 return NULL;
4129}
4130
4131/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004132 * Search hashitem in parent scope.
4133 */
4134 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004135find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004136{
4137 funccall_T *old_current_funccal = current_funccal;
4138 hashtab_T *ht;
4139 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004140 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004141
4142 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4143 return NULL;
4144
Bram Moolenaare38eab22019-12-05 21:50:01 +01004145 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004146 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004147 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004148 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004149 ht = find_var_ht(name, &varname);
4150 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004151 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004152 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004153 if (!HASHITEM_EMPTY(hi))
4154 {
4155 *pht = ht;
4156 break;
4157 }
4158 }
4159 if (current_funccal == current_funccal->func->uf_scoped)
4160 break;
4161 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004162 }
4163 current_funccal = old_current_funccal;
4164
4165 return hi;
4166}
4167
4168/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004169 * Search variable in parent scope.
4170 */
4171 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004172find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004173{
4174 dictitem_T *v = NULL;
4175 funccall_T *old_current_funccal = current_funccal;
4176 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004177 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004178
4179 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4180 return NULL;
4181
Bram Moolenaare38eab22019-12-05 21:50:01 +01004182 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004183 current_funccal = current_funccal->func->uf_scoped;
4184 while (current_funccal)
4185 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004186 ht = find_var_ht(name, &varname);
4187 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004188 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004189 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004190 if (v != NULL)
4191 break;
4192 }
4193 if (current_funccal == current_funccal->func->uf_scoped)
4194 break;
4195 current_funccal = current_funccal->func->uf_scoped;
4196 }
4197 current_funccal = old_current_funccal;
4198
4199 return v;
4200}
4201
4202/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004203 * Set "copyID + 1" in previous_funccal and callers.
4204 */
4205 int
4206set_ref_in_previous_funccal(int copyID)
4207{
4208 int abort = FALSE;
4209 funccall_T *fc;
4210
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004211 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004212 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004213 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004214 abort = abort
4215 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4216 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004217 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004218 }
4219 return abort;
4220}
4221
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004222 static int
4223set_ref_in_funccal(funccall_T *fc, int copyID)
4224{
4225 int abort = FALSE;
4226
4227 if (fc->fc_copyID != copyID)
4228 {
4229 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004230 abort = abort
4231 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4232 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004233 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004234 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004235 }
4236 return abort;
4237}
4238
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004239/*
4240 * Set "copyID" in all local vars and arguments in the call stack.
4241 */
4242 int
4243set_ref_in_call_stack(int copyID)
4244{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004245 int abort = FALSE;
4246 funccall_T *fc;
4247 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004248
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004249 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004250 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004251
4252 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004253 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
4254 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004255 abort = abort || set_ref_in_funccal(fc, copyID);
4256
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004257 return abort;
4258}
4259
4260/*
4261 * Set "copyID" in all functions available by name.
4262 */
4263 int
4264set_ref_in_functions(int copyID)
4265{
4266 int todo;
4267 hashitem_T *hi = NULL;
4268 int abort = FALSE;
4269 ufunc_T *fp;
4270
4271 todo = (int)func_hashtab.ht_used;
4272 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004273 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004274 if (!HASHITEM_EMPTY(hi))
4275 {
4276 --todo;
4277 fp = HI2UF(hi);
4278 if (!func_name_refcount(fp->uf_name))
4279 abort = abort || set_ref_in_func(NULL, fp, copyID);
4280 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004281 }
4282 return abort;
4283}
4284
4285/*
4286 * Set "copyID" in all function arguments.
4287 */
4288 int
4289set_ref_in_func_args(int copyID)
4290{
4291 int i;
4292 int abort = FALSE;
4293
4294 for (i = 0; i < funcargs.ga_len; ++i)
4295 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
4296 copyID, NULL, NULL);
4297 return abort;
4298}
4299
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004300/*
4301 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004302 * Returns TRUE if setting references failed somehow.
4303 */
4304 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004305set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004306{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004307 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004308 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01004309 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004310 char_u fname_buf[FLEN_FIXED + 1];
4311 char_u *tofree = NULL;
4312 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004313 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004314
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004315 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004316 return FALSE;
4317
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004318 if (fp_in == NULL)
4319 {
4320 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004322 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004323 if (fp != NULL)
4324 {
4325 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004326 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004327 }
4328 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004329 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004330}
4331
Bram Moolenaare38eab22019-12-05 21:50:01 +01004332#endif // FEAT_EVAL