blob: 316eac6aa0837252dd96651eae4ef65715a17a2c [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 Moolenaara9b579f2016-07-17 18:29:19 +0200147 int skip)
148{
149 int mustend = FALSE;
150 char_u *arg = *argp;
151 char_u *p = arg;
152 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200153 int any_default = FALSE;
154 char_u *expr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200155
156 if (newargs != NULL)
157 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 if (argtypes != NULL)
159 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200160 if (default_args != NULL)
161 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200162
163 if (varargs != NULL)
164 *varargs = FALSE;
165
166 /*
167 * Isolate the arguments: "arg1, arg2, ...)"
168 */
169 while (*p != endchar)
170 {
171 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
172 {
173 if (varargs != NULL)
174 *varargs = TRUE;
175 p += 3;
176 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100177
178 if (argtypes != NULL)
179 {
180 // ...name: list<type>
181 if (!ASCII_ISALPHA(*p))
182 {
183 emsg(_("E1055: Missing name after ..."));
184 break;
185 }
186
187 arg = p;
188 p = one_function_arg(p, newargs, argtypes, skip);
189 if (p == arg)
190 break;
191 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200192 }
193 else
194 {
195 arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196 p = one_function_arg(p, newargs, argtypes, skip);
197 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200198 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200199
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200200 if (*skipwhite(p) == '=' && default_args != NULL)
201 {
202 typval_T rettv;
203
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100204 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200205 any_default = TRUE;
206 p = skipwhite(p) + 1;
207 p = skipwhite(p);
208 expr = p;
209 if (eval1(&p, &rettv, FALSE) != FAIL)
210 {
211 if (ga_grow(default_args, 1) == FAIL)
212 goto err_ret;
213
214 // trim trailing whitespace
215 while (p > expr && VIM_ISWHITE(p[-1]))
216 p--;
217 c = *p;
218 *p = NUL;
219 expr = vim_strsave(expr);
220 if (expr == NULL)
221 {
222 *p = c;
223 goto err_ret;
224 }
225 ((char_u **)(default_args->ga_data))
226 [default_args->ga_len] = expr;
227 default_args->ga_len++;
228 *p = c;
229 }
230 else
231 mustend = TRUE;
232 }
233 else if (any_default)
234 {
235 emsg(_("E989: Non-default argument follows default argument"));
236 mustend = TRUE;
237 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200238 if (*p == ',')
239 ++p;
240 else
241 mustend = TRUE;
242 }
243 p = skipwhite(p);
244 if (mustend && *p != endchar)
245 {
246 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100247 semsg(_(e_invarg2), *argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200248 break;
249 }
250 }
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200251 if (*p != endchar)
252 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100253 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200254
255 *argp = p;
256 return OK;
257
258err_ret:
259 if (newargs != NULL)
260 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200261 if (default_args != NULL)
262 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200263 return FAIL;
264}
265
266/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200267 * Register function "fp" as using "current_funccal" as its scope.
268 */
269 static int
270register_closure(ufunc_T *fp)
271{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200272 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100273 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200274 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200275 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200276 fp->uf_scoped = current_funccal;
277 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200278
Bram Moolenaar58016442016-07-31 18:30:22 +0200279 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
280 return FAIL;
281 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
282 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200283 return OK;
284}
285
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100286 static void
287set_ufunc_name(ufunc_T *fp, char_u *name)
288{
289 STRCPY(fp->uf_name, name);
290
291 if (name[0] == K_SPECIAL)
292 {
293 fp->uf_name_exp = alloc(STRLEN(name) + 3);
294 if (fp->uf_name_exp != NULL)
295 {
296 STRCPY(fp->uf_name_exp, "<SNR>");
297 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
298 }
299 }
300}
301
Bram Moolenaar58016442016-07-31 18:30:22 +0200302/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200303 * Parse a lambda expression and get a Funcref from "*arg".
304 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
305 */
306 int
307get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
308{
309 garray_T newargs;
310 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200311 garray_T *pnewargs;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200312 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100313 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200314 int varargs;
315 int ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200316 char_u *start = skipwhite(*arg + 1);
317 char_u *s, *e;
318 static int lambda_no = 0;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200319 int *old_eval_lavars = eval_lavars_used;
320 int eval_lavars = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200321
322 ga_init(&newargs);
323 ga_init(&newlines);
324
Bram Moolenaare38eab22019-12-05 21:50:01 +0100325 // First, check if this is a lambda expression. "->" must exist.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326 ret = get_function_args(&start, '-', NULL, NULL, NULL, NULL, TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200327 if (ret == FAIL || *start != '>')
328 return NOTDONE;
329
Bram Moolenaare38eab22019-12-05 21:50:01 +0100330 // Parse the arguments again.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200331 if (evaluate)
332 pnewargs = &newargs;
333 else
334 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200335 *arg = skipwhite(*arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 // TODO: argument types
337 ret = get_function_args(arg, '-', pnewargs, NULL, &varargs, NULL, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200338 if (ret == FAIL || **arg != '>')
339 goto errret;
340
Bram Moolenaare38eab22019-12-05 21:50:01 +0100341 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200342 if (evaluate)
343 eval_lavars_used = &eval_lavars;
344
Bram Moolenaare38eab22019-12-05 21:50:01 +0100345 // Get the start and the end of the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200346 *arg = skipwhite(*arg + 1);
347 s = *arg;
348 ret = skip_expr(arg);
349 if (ret == FAIL)
350 goto errret;
351 e = *arg;
352 *arg = skipwhite(*arg);
353 if (**arg != '}')
Bram Moolenaaree619e52020-03-28 21:38:06 +0100354 {
355 semsg(_("E451: Expected }: %s"), *arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200356 goto errret;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100357 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200358 ++*arg;
359
360 if (evaluate)
361 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200362 int len, flags = 0;
363 char_u *p;
364 char_u name[20];
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200365
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200366 sprintf((char*)name, "<lambda>%d", ++lambda_no);
367
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200368 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200369 if (fp == NULL)
370 goto errret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100371 fp->uf_dfunc_idx = -1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200372 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200373 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200374 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200375
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200376 ga_init2(&newlines, (int)sizeof(char_u *), 1);
377 if (ga_grow(&newlines, 1) == FAIL)
378 goto errret;
379
Bram Moolenaare38eab22019-12-05 21:50:01 +0100380 // Add "return " before the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200381 len = 7 + e - s + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200382 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200383 if (p == NULL)
384 goto errret;
385 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
386 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200387 vim_strncpy(p + 7, s, e - s);
Bram Moolenaarf10806b2020-04-02 18:34:35 +0200388 if (strstr((char *)p + 7, "a:") == NULL)
389 // No a: variables are used for sure.
390 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200391
392 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100393 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200394 hash_add(&func_hashtab, UF2HIKEY(fp));
395 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200396 ga_init(&fp->uf_def_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200397 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200398 if (current_funccal != NULL && eval_lavars)
399 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200400 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200401 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200402 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200403 }
404 else
405 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200406
407#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200408 if (prof_def_func())
409 func_do_profile(fp);
410#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200411 if (sandbox)
412 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200414 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200415 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200416 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200417 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100418 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200419
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200420 pt->pt_func = fp;
421 pt->pt_refcount = 1;
422 rettv->vval.v_partial = pt;
423 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200424 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200425
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200426 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200427 return OK;
428
429errret:
430 ga_clear_strings(&newargs);
431 ga_clear_strings(&newlines);
432 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100433 vim_free(pt);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200434 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200435 return FAIL;
436}
437
438/*
439 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
440 * name it contains, otherwise return "name".
441 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
442 * "partialp".
443 */
444 char_u *
445deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
446{
447 dictitem_T *v;
448 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200449 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200450
451 if (partialp != NULL)
452 *partialp = NULL;
453
454 cc = name[*lenp];
455 name[*lenp] = NUL;
456 v = find_var(name, NULL, no_autoload);
457 name[*lenp] = cc;
458 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
459 {
460 if (v->di_tv.vval.v_string == NULL)
461 {
462 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100463 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200464 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200465 s = v->di_tv.vval.v_string;
466 *lenp = (int)STRLEN(s);
467 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200468 }
469
470 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
471 {
472 partial_T *pt = v->di_tv.vval.v_partial;
473
474 if (pt == NULL)
475 {
476 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100477 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200478 }
479 if (partialp != NULL)
480 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200481 s = partial_name(pt);
482 *lenp = (int)STRLEN(s);
483 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200484 }
485
486 return name;
487}
488
489/*
490 * Give an error message with a function name. Handle <SNR> things.
491 * "ermsg" is to be passed without translation, use N_() instead of _().
492 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100493 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200494emsg_funcname(char *ermsg, char_u *name)
495{
496 char_u *p;
497
498 if (*name == K_SPECIAL)
499 p = concat_str((char_u *)"<SNR>", name + 3);
500 else
501 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100502 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200503 if (p != name)
504 vim_free(p);
505}
506
507/*
508 * Allocate a variable for the result of a function.
509 * Return OK or FAIL.
510 */
511 int
512get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200513 char_u *name, // name of the function
514 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200515 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200516 char_u **arg, // argument, pointing to the '('
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200517 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200518{
519 char_u *argp;
520 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100521 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
522 int argcount = 0; // number of arguments found
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200523
524 /*
525 * Get the arguments.
526 */
527 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200528 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
529 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200530 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100531 argp = skipwhite(argp + 1); // skip the '(' or ','
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200532 if (*argp == ')' || *argp == ',' || *argp == NUL)
533 break;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200534 if (eval1(&argp, &argvars[argcount], funcexe->evaluate) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200535 {
536 ret = FAIL;
537 break;
538 }
539 ++argcount;
540 if (*argp != ',')
541 break;
542 }
543 if (*argp == ')')
544 ++argp;
545 else
546 ret = FAIL;
547
548 if (ret == OK)
549 {
550 int i = 0;
551
552 if (get_vim_var_nr(VV_TESTING))
553 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100554 // Prepare for calling test_garbagecollect_now(), need to know
555 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200556 if (funcargs.ga_itemsize == 0)
557 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
558 for (i = 0; i < argcount; ++i)
559 if (ga_grow(&funcargs, 1) == OK)
560 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
561 &argvars[i];
562 }
563
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200564 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200565
566 funcargs.ga_len -= i;
567 }
568 else if (!aborting())
569 {
570 if (argcount == MAX_FUNC_ARGS)
571 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
572 else
573 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
574 }
575
576 while (--argcount >= 0)
577 clear_tv(&argvars[argcount]);
578
579 *arg = skipwhite(argp);
580 return ret;
581}
582
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200583/*
584 * Return TRUE if "p" starts with "<SID>" or "s:".
585 * Only works if eval_fname_script() returned non-zero for "p"!
586 */
587 static int
588eval_fname_sid(char_u *p)
589{
590 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
591}
592
593/*
594 * In a script change <SID>name() and s:name() to K_SNR 123_name().
595 * Change <SNR>123_name() to K_SNR 123_name().
596 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
597 * (slow).
598 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100599 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200600fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
601{
602 int llen;
603 char_u *fname;
604 int i;
605
606 llen = eval_fname_script(name);
607 if (llen > 0)
608 {
609 fname_buf[0] = K_SPECIAL;
610 fname_buf[1] = KS_EXTRA;
611 fname_buf[2] = (int)KE_SNR;
612 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100613 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200614 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200615 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100616 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200617 else
618 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200619 sprintf((char *)fname_buf + 3, "%ld_",
620 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200621 i = (int)STRLEN(fname_buf);
622 }
623 }
624 if (i + STRLEN(name + llen) < FLEN_FIXED)
625 {
626 STRCPY(fname_buf + i, name + llen);
627 fname = fname_buf;
628 }
629 else
630 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200631 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200632 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100633 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200634 else
635 {
636 *tofree = fname;
637 mch_memmove(fname, fname_buf, (size_t)i);
638 STRCPY(fname + i, name + llen);
639 }
640 }
641 }
642 else
643 fname = name;
644 return fname;
645}
646
647/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100648 * Find a function "name" in script "sid".
649 */
650 static ufunc_T *
651find_func_with_sid(char_u *name, int sid)
652{
653 hashitem_T *hi;
654 char_u buffer[200];
655
656 buffer[0] = K_SPECIAL;
657 buffer[1] = KS_EXTRA;
658 buffer[2] = (int)KE_SNR;
659 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
660 (long)sid, name);
661 hi = hash_find(&func_hashtab, buffer);
662 if (!HASHITEM_EMPTY(hi))
663 return HI2UF(hi);
664
665 return NULL;
666}
667
668/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200669 * Find a function by name, return pointer to it in ufuncs.
670 * Return NULL for unknown function.
671 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 static ufunc_T *
673find_func_even_dead(char_u *name, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200674{
675 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100676 ufunc_T *func;
677 imported_T *imported;
678
679 if (in_vim9script())
680 {
681 // Find script-local function before global one.
682 func = find_func_with_sid(name, current_sctx.sc_sid);
683 if (func != NULL)
684 return func;
685
686 // Find imported funcion before global one.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +0100687 imported = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688 if (imported != NULL && imported->imp_funcname != NULL)
689 {
690 hi = hash_find(&func_hashtab, imported->imp_funcname);
691 if (!HASHITEM_EMPTY(hi))
692 return HI2UF(hi);
693 }
694 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200695
696 hi = hash_find(&func_hashtab, name);
697 if (!HASHITEM_EMPTY(hi))
698 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100699
700 return NULL;
701}
702
703/*
704 * Find a function by name, return pointer to it in ufuncs.
705 * "cctx" is passed in a :def function to find imported functions.
706 * Return NULL for unknown or dead function.
707 */
708 ufunc_T *
709find_func(char_u *name, cctx_T *cctx)
710{
711 ufunc_T *fp = find_func_even_dead(name, cctx);
712
713 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
714 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200715 return NULL;
716}
717
718/*
719 * Copy the function name of "fp" to buffer "buf".
720 * "buf" must be able to hold the function name plus three bytes.
721 * Takes care of script-local function names.
722 */
723 static void
724cat_func_name(char_u *buf, ufunc_T *fp)
725{
726 if (fp->uf_name[0] == K_SPECIAL)
727 {
728 STRCPY(buf, "<SNR>");
729 STRCAT(buf, fp->uf_name + 3);
730 }
731 else
732 STRCPY(buf, fp->uf_name);
733}
734
735/*
736 * Add a number variable "name" to dict "dp" with value "nr".
737 */
738 static void
739add_nr_var(
740 dict_T *dp,
741 dictitem_T *v,
742 char *name,
743 varnumber_T nr)
744{
745 STRCPY(v->di_key, name);
746 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
747 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
748 v->di_tv.v_type = VAR_NUMBER;
749 v->di_tv.v_lock = VAR_FIXED;
750 v->di_tv.vval.v_number = nr;
751}
752
753/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100754 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200755 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100756 static void
757free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200758{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100759 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200760
761 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
762 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100763 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200764
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100765 // When garbage collecting a funccall_T may be freed before the
766 // function that references it, clear its uf_scoped field.
767 // The function may have been redefined and point to another
768 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200769 if (fp != NULL && fp->uf_scoped == fc)
770 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200771 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200772 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200773
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200774 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200775 vim_free(fc);
776}
777
778/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100779 * Free "fc" and what it contains.
780 * Can be called only when "fc" is kept beyond the period of it called,
781 * i.e. after cleanup_function_call(fc).
782 */
783 static void
784free_funccal_contents(funccall_T *fc)
785{
786 listitem_T *li;
787
788 // Free all l: variables.
789 vars_clear(&fc->l_vars.dv_hashtab);
790
791 // Free all a: variables.
792 vars_clear(&fc->l_avars.dv_hashtab);
793
794 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200795 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100796 clear_tv(&li->li_tv);
797
798 free_funccal(fc);
799}
800
801/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200802 * Handle the last part of returning from a function: free the local hashtable.
803 * Unless it is still in use by a closure.
804 */
805 static void
806cleanup_function_call(funccall_T *fc)
807{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100808 int may_free_fc = fc->fc_refcount <= 0;
809 int free_fc = TRUE;
810
Bram Moolenaar6914c642017-04-01 21:21:30 +0200811 current_funccal = fc->caller;
812
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100813 // Free all l: variables if not referred.
814 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
815 vars_clear(&fc->l_vars.dv_hashtab);
816 else
817 free_fc = FALSE;
818
819 // If the a:000 list and the l: and a: dicts are not referenced and
820 // there is no closure using it, we can free the funccall_T and what's
821 // in it.
822 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
823 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200824 else
825 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100826 int todo;
827 hashitem_T *hi;
828 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200829
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100830 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200831
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100832 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200833 todo = (int)fc->l_avars.dv_hashtab.ht_used;
834 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
835 {
836 if (!HASHITEM_EMPTY(hi))
837 {
838 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100839 di = HI2DI(hi);
840 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200841 }
842 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100843 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200844
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100845 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
846 fc->l_varlist.lv_first = NULL;
847 else
848 {
849 listitem_T *li;
850
851 free_fc = FALSE;
852
853 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200854 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +0200855 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100856 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100857
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100858 if (free_fc)
859 free_funccal(fc);
860 else
861 {
862 static int made_copy = 0;
863
864 // "fc" is still in use. This can happen when returning "a:000",
865 // assigning "l:" to a global variable or defining a closure.
866 // Link "fc" in the list for garbage collection later.
867 fc->caller = previous_funccal;
868 previous_funccal = fc;
869
870 if (want_garbage_collect)
871 // If garbage collector is ready, clear count.
872 made_copy = 0;
873 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100874 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100875 // We have made a lot of copies, worth 4 Mbyte. This can happen
876 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100877 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100878 // too much memory.
879 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100880 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100881 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200882 }
883}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100884/*
885 * Unreference "fc": decrement the reference count and free it when it
886 * becomes zero. "fp" is detached from "fc".
887 * When "force" is TRUE we are exiting.
888 */
889 static void
890funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
891{
892 funccall_T **pfc;
893 int i;
894
895 if (fc == NULL)
896 return;
897
898 if (--fc->fc_refcount <= 0 && (force || (
899 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
900 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
901 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
902 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
903 {
904 if (fc == *pfc)
905 {
906 *pfc = fc->caller;
907 free_funccal_contents(fc);
908 return;
909 }
910 }
911 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
912 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
913 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
914}
915
916/*
917 * Remove the function from the function hashtable. If the function was
918 * deleted while it still has references this was already done.
919 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
920 */
921 static int
922func_remove(ufunc_T *fp)
923{
924 hashitem_T *hi;
925
926 // Return if it was already virtually deleted.
927 if (fp->uf_flags & FC_DEAD)
928 return FALSE;
929
930 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
931 if (!HASHITEM_EMPTY(hi))
932 {
933 // When there is a def-function index do not actually remove the
934 // function, so we can find the index when defining the function again.
935 if (fp->uf_dfunc_idx >= 0)
936 fp->uf_flags |= FC_DEAD;
937 else
938 hash_remove(&func_hashtab, hi);
939 return TRUE;
940 }
941 return FALSE;
942}
943
944 static void
945func_clear_items(ufunc_T *fp)
946{
947 ga_clear_strings(&(fp->uf_args));
948 ga_clear_strings(&(fp->uf_def_args));
949 ga_clear_strings(&(fp->uf_lines));
950 VIM_CLEAR(fp->uf_name_exp);
951 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100952 VIM_CLEAR(fp->uf_def_arg_idx);
953 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200954 while (fp->uf_type_list.ga_len > 0)
955 vim_free(((type_T **)fp->uf_type_list.ga_data)
956 [--fp->uf_type_list.ga_len]);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957 ga_clear(&fp->uf_type_list);
958#ifdef FEAT_PROFILE
959 VIM_CLEAR(fp->uf_tml_count);
960 VIM_CLEAR(fp->uf_tml_total);
961 VIM_CLEAR(fp->uf_tml_self);
962#endif
963}
964
965/*
966 * Free all things that a function contains. Does not free the function
967 * itself, use func_free() for that.
968 * When "force" is TRUE we are exiting.
969 */
970 static void
971func_clear(ufunc_T *fp, int force)
972{
973 if (fp->uf_cleared)
974 return;
975 fp->uf_cleared = TRUE;
976
977 // clear this function
978 func_clear_items(fp);
979 funccal_unref(fp->uf_scoped, fp, force);
980 delete_def_function(fp);
981}
982
983/*
984 * Free a function and remove it from the list of functions. Does not free
985 * what a function contains, call func_clear() first.
986 */
987 static void
988func_free(ufunc_T *fp)
989{
990 // Only remove it when not done already, otherwise we would remove a newer
991 // version of the function with the same name.
992 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
993 func_remove(fp);
994
995 if ((fp->uf_flags & FC_DEAD) == 0)
996 vim_free(fp);
997}
998
999/*
1000 * Free all things that a function contains and free the function itself.
1001 * When "force" is TRUE we are exiting.
1002 */
1003 static void
1004func_clear_free(ufunc_T *fp, int force)
1005{
1006 func_clear(fp, force);
1007 func_free(fp);
1008}
1009
Bram Moolenaar6914c642017-04-01 21:21:30 +02001010
1011/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001012 * Call a user function.
1013 */
1014 static void
1015call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001016 ufunc_T *fp, // pointer to function
1017 int argcount, // nr of args
1018 typval_T *argvars, // arguments
1019 typval_T *rettv, // return value
1020 linenr_T firstline, // first line of range
1021 linenr_T lastline, // last line of range
1022 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001023{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001024 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001025 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001026 funccall_T *fc;
1027 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001028 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001029 static int depth = 0;
1030 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001031 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001032 int i;
1033 int ai;
1034 int islambda = FALSE;
1035 char_u numbuf[NUMBUFLEN];
1036 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001037#ifdef FEAT_PROFILE
1038 proftime_T wait_start;
1039 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001040 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001041#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001042 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001043
Bram Moolenaare38eab22019-12-05 21:50:01 +01001044 // If depth of calling is getting too high, don't execute the function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001045 if (depth >= p_mfd)
1046 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001047 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001048 rettv->v_type = VAR_NUMBER;
1049 rettv->vval.v_number = -1;
1050 return;
1051 }
1052 ++depth;
1053
Bram Moolenaare38eab22019-12-05 21:50:01 +01001054 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001055
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001056 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001057 if (fc == NULL)
1058 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001059 fc->caller = current_funccal;
1060 current_funccal = fc;
1061 fc->func = fp;
1062 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001063 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001064 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001065 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1066 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001067 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001068 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001069 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001070
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001071 if (fp->uf_dfunc_idx >= 0)
1072 {
1073 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001074 save_current_sctx = current_sctx;
1075 current_sctx = fp->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076
1077 // Execute the compiled function.
1078 call_def_function(fp, argcount, argvars, rettv);
1079 --depth;
1080 current_funccal = fc->caller;
1081
1082 estack_pop();
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001083 current_sctx = save_current_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 free_funccal(fc);
1085 return;
1086 }
1087
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001088 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1089 islambda = TRUE;
1090
1091 /*
1092 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1093 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1094 * each argument variable and saves a lot of time.
1095 */
1096 /*
1097 * Init l: variables.
1098 */
1099 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1100 if (selfdict != NULL)
1101 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001102 // Set l:self to "selfdict". Use "name" to avoid a warning from
1103 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001104 v = &fc->fixvar[fixvar_idx++].var;
1105 name = v->di_key;
1106 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001107 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001108 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1109 v->di_tv.v_type = VAR_DICT;
1110 v->di_tv.v_lock = 0;
1111 v->di_tv.vval.v_dict = selfdict;
1112 ++selfdict->dv_refcount;
1113 }
1114
1115 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001116 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001117 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001118 * Set a:000 to a list with room for the "..." arguments.
1119 */
1120 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001121 if ((fp->uf_flags & FC_NOARGS) == 0)
1122 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001123 (varnumber_T)(argcount >= fp->uf_args.ga_len
1124 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001125 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001126 if ((fp->uf_flags & FC_NOARGS) == 0)
1127 {
1128 // Use "name" to avoid a warning from some compiler that checks the
1129 // destination size.
1130 v = &fc->fixvar[fixvar_idx++].var;
1131 name = v->di_key;
1132 STRCPY(name, "000");
1133 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1134 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1135 v->di_tv.v_type = VAR_LIST;
1136 v->di_tv.v_lock = VAR_FIXED;
1137 v->di_tv.vval.v_list = &fc->l_varlist;
1138 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001139 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
1140 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1141 fc->l_varlist.lv_lock = VAR_FIXED;
1142
1143 /*
1144 * Set a:firstline to "firstline" and a:lastline to "lastline".
1145 * Set a:name to named arguments.
1146 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001147 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001148 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001149 if ((fp->uf_flags & FC_NOARGS) == 0)
1150 {
1151 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001152 (varnumber_T)firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001153 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001154 (varnumber_T)lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001155 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001156 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001157 {
1158 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001159 typval_T def_rettv;
1160 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001161
1162 ai = i - fp->uf_args.ga_len;
1163 if (ai < 0)
1164 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001165 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001166 name = FUNCARG(fp, i);
1167 if (islambda)
1168 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001169
1170 // evaluate named argument default expression
1171 isdefault = ai + fp->uf_def_args.ga_len >= 0
1172 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1173 && argvars[i].vval.v_number == VVAL_NONE));
1174 if (isdefault)
1175 {
1176 char_u *default_expr = NULL;
1177 def_rettv.v_type = VAR_NUMBER;
1178 def_rettv.vval.v_number = -1;
1179
1180 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1181 [ai + fp->uf_def_args.ga_len];
1182 if (eval1(&default_expr, &def_rettv, TRUE) == FAIL)
1183 {
1184 default_arg_err = 1;
1185 break;
1186 }
1187 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001188 }
1189 else
1190 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001191 if ((fp->uf_flags & FC_NOARGS) != 0)
1192 // Bail out if no a: arguments used (in lambda).
1193 break;
1194
Bram Moolenaare38eab22019-12-05 21:50:01 +01001195 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001196 sprintf((char *)numbuf, "%d", ai + 1);
1197 name = numbuf;
1198 }
1199 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1200 {
1201 v = &fc->fixvar[fixvar_idx++].var;
1202 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001203 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001204 }
1205 else
1206 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001207 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001208 if (v == NULL)
1209 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001210 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001211 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001212
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001213 // Note: the values are copied directly to avoid alloc/free.
1214 // "argvars" must have VAR_FIXED for v_lock.
1215 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001216 v->di_tv.v_lock = VAR_FIXED;
1217
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001218 if (addlocal)
1219 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001220 // Named arguments should be accessed without the "a:" prefix in
1221 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001222 copy_tv(&v->di_tv, &v->di_tv);
1223 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001224 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001225 else
1226 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001227
1228 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1229 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001230 listitem_T *li = &fc->l_listitems[ai];
1231
1232 li->li_tv = argvars[i];
1233 li->li_tv.v_lock = VAR_FIXED;
1234 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001235 }
1236 }
1237
Bram Moolenaare38eab22019-12-05 21:50:01 +01001238 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001239 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001240
1241 if (fp->uf_flags & FC_SANDBOX)
1242 {
1243 using_sandbox = TRUE;
1244 ++sandbox;
1245 }
1246
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001247 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001248 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001249 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001250 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001251 ++no_wait_return;
1252 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001253
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001254 smsg(_("calling %s"), SOURCING_NAME);
1255 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001256 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001257 char_u buf[MSG_BUF_LEN];
1258 char_u numbuf2[NUMBUFLEN];
1259 char_u *tofree;
1260 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001261
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001262 msg_puts("(");
1263 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001264 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001265 if (i > 0)
1266 msg_puts(", ");
1267 if (argvars[i].v_type == VAR_NUMBER)
1268 msg_outnum((long)argvars[i].vval.v_number);
1269 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001270 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001271 // Do not want errors such as E724 here.
1272 ++emsg_off;
1273 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1274 --emsg_off;
1275 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001276 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001277 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001278 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001279 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1280 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001281 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001282 msg_puts((char *)s);
1283 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001284 }
1285 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001286 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001287 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001288 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001289 msg_puts("\n"); // don't overwrite this either
1290
1291 verbose_leave_scroll();
1292 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001293 }
1294#ifdef FEAT_PROFILE
1295 if (do_profiling == PROF_YES)
1296 {
1297 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001298 {
1299 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001300 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001301 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001302 if (fp->uf_profiling
1303 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1304 {
1305 ++fp->uf_tm_count;
1306 profile_start(&call_start);
1307 profile_zero(&fp->uf_tm_children);
1308 }
1309 script_prof_save(&wait_start);
1310 }
1311#endif
1312
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001313 save_current_sctx = current_sctx;
1314 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001315 save_did_emsg = did_emsg;
1316 did_emsg = FALSE;
1317
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001318 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1319 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001320 else if (islambda)
1321 {
1322 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1323
1324 // A Lambda always has the command "return {expr}". It is much faster
1325 // to evaluate {expr} directly.
1326 ++ex_nesting_level;
Bram Moolenaard1e9dc22020-04-03 18:13:57 +02001327 (void)eval1(&p, rettv, TRUE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001328 --ex_nesting_level;
1329 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001330 else
1331 // call do_cmdline() to execute the lines
1332 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001333 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1334
1335 --RedrawingDisabled;
1336
Bram Moolenaare38eab22019-12-05 21:50:01 +01001337 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001338 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1339 {
1340 clear_tv(rettv);
1341 rettv->v_type = VAR_NUMBER;
1342 rettv->vval.v_number = -1;
1343 }
1344
1345#ifdef FEAT_PROFILE
1346 if (do_profiling == PROF_YES && (fp->uf_profiling
1347 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1348 {
1349 profile_end(&call_start);
1350 profile_sub_wait(&wait_start, &call_start);
1351 profile_add(&fp->uf_tm_total, &call_start);
1352 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1353 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1354 {
1355 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1356 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1357 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001358 if (started_profiling)
1359 // make a ":profdel func" stop profiling the function
1360 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001361 }
1362#endif
1363
Bram Moolenaare38eab22019-12-05 21:50:01 +01001364 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001365 if (p_verbose >= 12)
1366 {
1367 ++no_wait_return;
1368 verbose_enter_scroll();
1369
1370 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001371 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001372 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001373 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001374 (long)fc->rettv->vval.v_number);
1375 else
1376 {
1377 char_u buf[MSG_BUF_LEN];
1378 char_u numbuf2[NUMBUFLEN];
1379 char_u *tofree;
1380 char_u *s;
1381
Bram Moolenaare38eab22019-12-05 21:50:01 +01001382 // The value may be very long. Skip the middle part, so that we
1383 // have some idea how it starts and ends. smsg() would always
1384 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001385 ++emsg_off;
1386 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1387 --emsg_off;
1388 if (s != NULL)
1389 {
1390 if (vim_strsize(s) > MSG_BUF_CLEN)
1391 {
1392 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1393 s = buf;
1394 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001395 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001396 vim_free(tofree);
1397 }
1398 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001399 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001400
1401 verbose_leave_scroll();
1402 --no_wait_return;
1403 }
1404
Bram Moolenaare31ee862020-01-07 20:59:34 +01001405 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001406 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001407 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001408#ifdef FEAT_PROFILE
1409 if (do_profiling == PROF_YES)
1410 script_prof_restore(&wait_start);
1411#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001412 if (using_sandbox)
1413 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001414
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001415 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001416 {
1417 ++no_wait_return;
1418 verbose_enter_scroll();
1419
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001420 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001421 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001422
1423 verbose_leave_scroll();
1424 --no_wait_return;
1425 }
1426
1427 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001428 --depth;
1429
Bram Moolenaar6914c642017-04-01 21:21:30 +02001430 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001431}
1432
1433/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001435 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001436 int
1437call_user_func_check(
1438 ufunc_T *fp,
1439 int argcount,
1440 typval_T *argvars,
1441 typval_T *rettv,
1442 funcexe_T *funcexe,
1443 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001444{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 int error;
1446 int regular_args = fp->uf_args.ga_len;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001447
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001448 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1449 *funcexe->doesrange = TRUE;
1450 if (argcount < regular_args - fp->uf_def_args.ga_len)
1451 error = FCERR_TOOFEW;
1452 else if (!has_varargs(fp) && argcount > regular_args)
1453 error = FCERR_TOOMANY;
1454 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1455 error = FCERR_DICT;
1456 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001457 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 int did_save_redo = FALSE;
1459 save_redo_T save_redo;
1460
1461 /*
1462 * Call the user function.
1463 * Save and restore search patterns, script variables and
1464 * redo buffer.
1465 */
1466 save_search_patterns();
1467 if (!ins_compl_active())
1468 {
1469 saveRedobuff(&save_redo);
1470 did_save_redo = TRUE;
1471 }
1472 ++fp->uf_calls;
1473 call_user_func(fp, argcount, argvars, rettv,
1474 funcexe->firstline, funcexe->lastline,
1475 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
1476 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1477 // Function was unreferenced while being used, free it now.
1478 func_clear_free(fp, FALSE);
1479 if (did_save_redo)
1480 restoreRedobuff(&save_redo);
1481 restore_search_patterns();
1482 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001483 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001484 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001485}
1486
1487/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001488 * There are two kinds of function names:
1489 * 1. ordinary names, function defined with :function
1490 * 2. numbered functions and lambdas
1491 * For the first we only count the name stored in func_hashtab as a reference,
1492 * using function() does not count as a reference, because the function is
1493 * looked up by name.
1494 */
1495 static int
1496func_name_refcount(char_u *name)
1497{
1498 return isdigit(*name) || *name == '<';
1499}
1500
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001501static funccal_entry_T *funccal_stack = NULL;
1502
1503/*
1504 * Save the current function call pointer, and set it to NULL.
1505 * Used when executing autocommands and for ":source".
1506 */
1507 void
1508save_funccal(funccal_entry_T *entry)
1509{
1510 entry->top_funccal = current_funccal;
1511 entry->next = funccal_stack;
1512 funccal_stack = entry;
1513 current_funccal = NULL;
1514}
1515
1516 void
1517restore_funccal(void)
1518{
1519 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001520 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001521 else
1522 {
1523 current_funccal = funccal_stack->top_funccal;
1524 funccal_stack = funccal_stack->next;
1525 }
1526}
1527
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001528 funccall_T *
1529get_current_funccal(void)
1530{
1531 return current_funccal;
1532}
1533
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001534#if defined(EXITFREE) || defined(PROTO)
1535 void
1536free_all_functions(void)
1537{
1538 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001539 ufunc_T *fp;
1540 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001541 long_u todo = 1;
1542 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001543
Bram Moolenaare38eab22019-12-05 21:50:01 +01001544 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001545 while (current_funccal != NULL)
1546 {
1547 clear_tv(current_funccal->rettv);
1548 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001549 if (current_funccal == NULL && funccal_stack != NULL)
1550 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001551 }
1552
Bram Moolenaare38eab22019-12-05 21:50:01 +01001553 // First clear what the functions contain. Since this may lower the
1554 // reference count of a function, it may also free a function and change
1555 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001556 while (todo > 0)
1557 {
1558 todo = func_hashtab.ht_used;
1559 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1560 if (!HASHITEM_EMPTY(hi))
1561 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 // clear the def function index now
1563 fp = HI2UF(hi);
1564 fp->uf_flags &= ~FC_DEAD;
1565 fp->uf_dfunc_idx = -1;
1566
Bram Moolenaare38eab22019-12-05 21:50:01 +01001567 // Only free functions that are not refcounted, those are
1568 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001569 if (func_name_refcount(fp->uf_name))
1570 ++skipped;
1571 else
1572 {
1573 used = func_hashtab.ht_used;
1574 func_clear(fp, TRUE);
1575 if (used != func_hashtab.ht_used)
1576 {
1577 skipped = 0;
1578 break;
1579 }
1580 }
1581 --todo;
1582 }
1583 }
1584
Bram Moolenaare38eab22019-12-05 21:50:01 +01001585 // Now actually free the functions. Need to start all over every time,
1586 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001587 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001588 while (func_hashtab.ht_used > skipped)
1589 {
1590 todo = func_hashtab.ht_used;
1591 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001592 if (!HASHITEM_EMPTY(hi))
1593 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001594 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001595 // Only free functions that are not refcounted, those are
1596 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02001597 fp = HI2UF(hi);
1598 if (func_name_refcount(fp->uf_name))
1599 ++skipped;
1600 else
1601 {
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001602 func_free(fp);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001603 skipped = 0;
1604 break;
1605 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001606 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001607 }
1608 if (skipped == 0)
1609 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610
1611 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001612}
1613#endif
1614
1615/*
1616 * Return TRUE if "name" looks like a builtin function name: starts with a
1617 * lower case letter and doesn't contain AUTOLOAD_CHAR.
1618 * "len" is the length of "name", or -1 for NUL terminated.
1619 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001620 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001621builtin_function(char_u *name, int len)
1622{
1623 char_u *p;
1624
1625 if (!ASCII_ISLOWER(name[0]))
1626 return FALSE;
1627 p = vim_strchr(name, AUTOLOAD_CHAR);
1628 return p == NULL || (len > 0 && p > name + len);
1629}
1630
1631 int
1632func_call(
1633 char_u *name,
1634 typval_T *args,
1635 partial_T *partial,
1636 dict_T *selfdict,
1637 typval_T *rettv)
1638{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001639 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001640 listitem_T *item;
1641 typval_T argv[MAX_FUNC_ARGS + 1];
1642 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001643 int r = 0;
1644
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001645 range_list_materialize(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001646 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001647 {
1648 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1649 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001650 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001651 break;
1652 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001653 // Make a copy of each argument. This is needed to be able to set
1654 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001655 copy_tv(&item->li_tv, &argv[argc++]);
1656 }
1657
1658 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001659 {
1660 funcexe_T funcexe;
1661
Bram Moolenaarac92e252019-08-03 21:58:38 +02001662 vim_memset(&funcexe, 0, sizeof(funcexe));
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001663 funcexe.firstline = curwin->w_cursor.lnum;
1664 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001665 funcexe.evaluate = TRUE;
1666 funcexe.partial = partial;
1667 funcexe.selfdict = selfdict;
1668 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1669 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001670
Bram Moolenaare38eab22019-12-05 21:50:01 +01001671 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001672 while (argc > 0)
1673 clear_tv(&argv[--argc]);
1674
1675 return r;
1676}
1677
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001678static int callback_depth = 0;
1679
1680 int
1681get_callback_depth(void)
1682{
1683 return callback_depth;
1684}
1685
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001686/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001687 * Invoke call_func() with a callback.
1688 */
1689 int
1690call_callback(
1691 callback_T *callback,
1692 int len, // length of "name" or -1 to use strlen()
1693 typval_T *rettv, // return value goes here
1694 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001695 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001696 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001697{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001698 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001699 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001700
1701 vim_memset(&funcexe, 0, sizeof(funcexe));
1702 funcexe.evaluate = TRUE;
1703 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001704 ++callback_depth;
1705 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1706 --callback_depth;
1707 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001708}
1709
1710/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 * Give an error message for the result of a function.
1712 * Nothing if "error" is FCERR_NONE.
1713 */
1714 void
1715user_func_error(int error, char_u *name)
1716{
1717 switch (error)
1718 {
1719 case FCERR_UNKNOWN:
1720 emsg_funcname(e_unknownfunc, name);
1721 break;
1722 case FCERR_NOTMETHOD:
1723 emsg_funcname(
1724 N_("E276: Cannot use function as a method: %s"), name);
1725 break;
1726 case FCERR_DELETED:
1727 emsg_funcname(N_(e_func_deleted), name);
1728 break;
1729 case FCERR_TOOMANY:
1730 emsg_funcname((char *)e_toomanyarg, name);
1731 break;
1732 case FCERR_TOOFEW:
1733 emsg_funcname((char *)e_toofewarg, name);
1734 break;
1735 case FCERR_SCRIPT:
1736 emsg_funcname(
1737 N_("E120: Using <SID> not in a script context: %s"), name);
1738 break;
1739 case FCERR_DICT:
1740 emsg_funcname(
1741 N_("E725: Calling dict function without Dictionary: %s"),
1742 name);
1743 break;
1744 }
1745}
1746
1747/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001748 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001749 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001750 * Return FAIL when the function can't be called, OK otherwise.
1751 * Also returns OK when an error was encountered while executing the function.
1752 */
1753 int
1754call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001755 char_u *funcname, // name of the function
1756 int len, // length of "name" or -1 to use strlen()
1757 typval_T *rettv, // return value goes here
1758 int argcount_in, // number of "argvars"
1759 typval_T *argvars_in, // vars for arguments, must have "argcount"
1760 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001761 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001762{
1763 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01001764 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001765 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001766 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001767 char_u fname_buf[FLEN_FIXED + 1];
1768 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001769 char_u *fname = NULL;
1770 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001771 int argcount = argcount_in;
1772 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001773 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001774 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1775 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001776 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001777 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001778 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001779
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001780 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1781 // even when call_func() returns FAIL.
1782 rettv->v_type = VAR_UNKNOWN;
1783
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001784 if (partial != NULL)
1785 fp = partial->pt_func;
1786 if (fp == NULL)
1787 {
1788 // Make a copy of the name, if it comes from a funcref variable it
1789 // could be changed or deleted in the called function.
1790 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
1791 if (name == NULL)
1792 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001793
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001794 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1795 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001796
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001797 if (funcexe->doesrange != NULL)
1798 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001799
1800 if (partial != NULL)
1801 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001802 // When the function has a partial with a dict and there is a dict
1803 // argument, use the dict argument. That is backwards compatible.
1804 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001805 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001806 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01001807 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001808 {
1809 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001810 {
1811 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
1812 {
Bram Moolenaaref140542019-12-31 21:27:13 +01001813 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001814 goto theend;
1815 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001816 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001817 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001818 for (i = 0; i < argcount_in; ++i)
1819 argv[i + argv_clear] = argvars_in[i];
1820 argvars = argv;
1821 argcount = partial->pt_argc + argcount_in;
1822 }
1823 }
1824
Bram Moolenaaref140542019-12-31 21:27:13 +01001825 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001826 {
1827 char_u *rfname = fname;
1828
Bram Moolenaare38eab22019-12-05 21:50:01 +01001829 // Ignore "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001830 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001831 rfname = fname + 2;
1832
Bram Moolenaare38eab22019-12-05 21:50:01 +01001833 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001834 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01001835 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001836
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001837 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001838 {
1839 /*
1840 * User defined function.
1841 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001842 if (fp == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001844
Bram Moolenaare38eab22019-12-05 21:50:01 +01001845 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001846 if (fp == NULL
1847 && apply_autocmds(EVENT_FUNCUNDEFINED,
1848 rfname, rfname, TRUE, NULL)
1849 && !aborting())
1850 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001851 // executed an autocommand, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001853 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001854 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001855 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1856 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001857 // loaded a package, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001859 }
1860
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001861 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01001862 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001863 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001864 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001865 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01001866 // postponed filling in the arguments, do it now
1867 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001868 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001869
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001870 if (funcexe->basetv != NULL)
1871 {
1872 // Method call: base->Method()
1873 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
1874 argv[0] = *funcexe->basetv;
1875 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001876 argvars = argv;
1877 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001878 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001879
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 error = call_user_func_check(fp, argcount, argvars, rettv,
1881 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001882 }
1883 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001884 else if (funcexe->basetv != NULL)
1885 {
1886 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001887 * expr->method(): Find the method name in the table, call its
1888 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02001889 */
1890 error = call_internal_method(fname, argcount, argvars, rettv,
1891 funcexe->basetv);
1892 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001893 else
1894 {
1895 /*
1896 * Find the function name in the table, call its implementation.
1897 */
1898 error = call_internal_func(fname, argcount, argvars, rettv);
1899 }
1900 /*
1901 * The function call (or "FuncUndefined" autocommand sequence) might
1902 * have been aborted by an error, an interrupt, or an explicitly thrown
1903 * exception that has not been caught so far. This situation can be
1904 * tested for by calling aborting(). For an error in an internal
1905 * function or for the "E132" error in call_user_func(), however, the
1906 * throw point at which the "force_abort" flag (temporarily reset by
1907 * emsg()) is normally updated has not been reached yet. We need to
1908 * update that flag first to make aborting() reliable.
1909 */
1910 update_force_abort();
1911 }
Bram Moolenaaref140542019-12-31 21:27:13 +01001912 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001913 ret = OK;
1914
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001915theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001916 /*
1917 * Report an error unless the argument evaluation or function call has been
1918 * cancelled due to an aborting error, an interrupt, or an exception.
1919 */
1920 if (!aborting())
1921 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001922 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001923 }
1924
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001925 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001926 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001927 clear_tv(&argv[--argv_clear + argv_base]);
1928
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001929 vim_free(tofree);
1930 vim_free(name);
1931
1932 return ret;
1933}
1934
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935 static char_u *
1936printable_func_name(ufunc_T *fp)
1937{
1938 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
1939}
1940
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001941/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001942 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001943 */
1944 static void
1945list_func_head(ufunc_T *fp, int indent)
1946{
1947 int j;
1948
1949 msg_start();
1950 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001951 msg_puts(" ");
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001952 if (fp->uf_dfunc_idx >= 0)
1953 msg_puts("def ");
1954 else
1955 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001957 msg_putchar('(');
1958 for (j = 0; j < fp->uf_args.ga_len; ++j)
1959 {
1960 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001961 msg_puts(", ");
1962 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963 if (fp->uf_arg_types != NULL)
1964 {
1965 char *tofree;
1966
1967 msg_puts(": ");
1968 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
1969 vim_free(tofree);
1970 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001971 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
1972 {
1973 msg_puts(" = ");
1974 msg_puts(((char **)(fp->uf_def_args.ga_data))
1975 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
1976 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001977 }
1978 if (fp->uf_varargs)
1979 {
1980 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001981 msg_puts(", ");
1982 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001983 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 if (fp->uf_va_name != NULL)
1985 {
1986 if (j)
1987 msg_puts(", ");
1988 msg_puts("...");
1989 msg_puts((char *)fp->uf_va_name);
1990 if (fp->uf_va_type)
1991 {
1992 char *tofree;
1993
1994 msg_puts(": ");
1995 msg_puts(type_name(fp->uf_va_type, &tofree));
1996 vim_free(tofree);
1997 }
1998 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001999 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002000
2001 if (fp->uf_dfunc_idx >= 0)
2002 {
2003 if (fp->uf_ret_type != &t_void)
2004 {
2005 char *tofree;
2006
2007 msg_puts(": ");
2008 msg_puts(type_name(fp->uf_ret_type, &tofree));
2009 vim_free(tofree);
2010 }
2011 }
2012 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002013 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002014 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002015 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002016 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002017 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002018 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002019 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002020 msg_clr_eos();
2021 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002022 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002023}
2024
2025/*
2026 * Get a function name, translating "<SID>" and "<SNR>".
2027 * Also handles a Funcref in a List or Dictionary.
2028 * Returns the function name in allocated memory, or NULL for failure.
2029 * flags:
2030 * TFN_INT: internal function name OK
2031 * TFN_QUIET: be quiet
2032 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002033 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002034 * Advances "pp" to just after the function name (if no error).
2035 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002036 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002037trans_function_name(
2038 char_u **pp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002039 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002040 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002041 funcdict_T *fdp, // return: info about dictionary used
2042 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002043{
2044 char_u *name = NULL;
2045 char_u *start;
2046 char_u *end;
2047 int lead;
2048 char_u sid_buf[20];
2049 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002051 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002053
2054 if (fdp != NULL)
2055 vim_memset(fdp, 0, sizeof(funcdict_T));
2056 start = *pp;
2057
Bram Moolenaare38eab22019-12-05 21:50:01 +01002058 // Check for hard coded <SNR>: already translated function ID (from a user
2059 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002060 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2061 && (*pp)[2] == (int)KE_SNR)
2062 {
2063 *pp += 3;
2064 len = get_id_len(pp) + 3;
2065 return vim_strnsave(start, len);
2066 }
2067
Bram Moolenaare38eab22019-12-05 21:50:01 +01002068 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2069 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002070 lead = eval_fname_script(start);
2071 if (lead > 2)
2072 start += lead;
2073
Bram Moolenaare38eab22019-12-05 21:50:01 +01002074 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002075 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002076 lead > 2 ? 0 : FNE_CHECK_START);
2077 if (end == start)
2078 {
2079 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002080 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002081 goto theend;
2082 }
2083 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2084 {
2085 /*
2086 * Report an invalid expression in braces, unless the expression
2087 * evaluation has been cancelled due to an aborting error, an
2088 * interrupt, or an exception.
2089 */
2090 if (!aborting())
2091 {
2092 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002093 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002094 }
2095 else
2096 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2097 goto theend;
2098 }
2099
2100 if (lv.ll_tv != NULL)
2101 {
2102 if (fdp != NULL)
2103 {
2104 fdp->fd_dict = lv.ll_dict;
2105 fdp->fd_newkey = lv.ll_newkey;
2106 lv.ll_newkey = NULL;
2107 fdp->fd_di = lv.ll_di;
2108 }
2109 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2110 {
2111 name = vim_strsave(lv.ll_tv->vval.v_string);
2112 *pp = end;
2113 }
2114 else if (lv.ll_tv->v_type == VAR_PARTIAL
2115 && lv.ll_tv->vval.v_partial != NULL)
2116 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002117 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002118 *pp = end;
2119 if (partial != NULL)
2120 *partial = lv.ll_tv->vval.v_partial;
2121 }
2122 else
2123 {
2124 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2125 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002126 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002127 else
2128 *pp = end;
2129 name = NULL;
2130 }
2131 goto theend;
2132 }
2133
2134 if (lv.ll_name == NULL)
2135 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002136 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002137 *pp = end;
2138 goto theend;
2139 }
2140
Bram Moolenaare38eab22019-12-05 21:50:01 +01002141 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002142 if (lv.ll_exp_name != NULL)
2143 {
2144 len = (int)STRLEN(lv.ll_exp_name);
2145 name = deref_func_name(lv.ll_exp_name, &len, partial,
2146 flags & TFN_NO_AUTOLOAD);
2147 if (name == lv.ll_exp_name)
2148 name = NULL;
2149 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002150 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002151 {
2152 len = (int)(end - *pp);
2153 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2154 if (name == *pp)
2155 name = NULL;
2156 }
2157 if (name != NULL)
2158 {
2159 name = vim_strsave(name);
2160 *pp = end;
2161 if (STRNCMP(name, "<SNR>", 5) == 0)
2162 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002163 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002164 name[0] = K_SPECIAL;
2165 name[1] = KS_EXTRA;
2166 name[2] = (int)KE_SNR;
2167 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2168 }
2169 goto theend;
2170 }
2171
2172 if (lv.ll_exp_name != NULL)
2173 {
2174 len = (int)STRLEN(lv.ll_exp_name);
2175 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2176 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2177 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002178 // When there was "s:" already or the name expanded to get a
2179 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002180 lv.ll_name += 2;
2181 len -= 2;
2182 lead = 2;
2183 }
2184 }
2185 else
2186 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002187 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002188 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
2189 lv.ll_name += 2;
2190 len = (int)(end - lv.ll_name);
2191 }
2192
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 // In Vim9 script a user function is script-local by default.
2194 vim9script = ASCII_ISUPPER(*start)
2195 && current_sctx.sc_version == SCRIPT_VERSION_VIM9;
2196
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002197 /*
2198 * Copy the function name to allocated memory.
2199 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2200 * Accept <SNR>123_name() outside a script.
2201 */
2202 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002203 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002205 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206 if (!vim9script)
2207 lead = 3;
2208 if (vim9script || (lv.ll_exp_name != NULL
2209 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002210 || eval_fname_sid(*pp))
2211 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002213 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002214 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002215 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002216 goto theend;
2217 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002218 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 if (vim9script)
2220 extra = 3 + (int)STRLEN(sid_buf);
2221 else
2222 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002223 }
2224 }
2225 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2226 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002227 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002228 start);
2229 goto theend;
2230 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002231 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002232 {
2233 char_u *cp = vim_strchr(lv.ll_name, ':');
2234
2235 if (cp != NULL && cp < end)
2236 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002237 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002238 goto theend;
2239 }
2240 }
2241
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002243 if (name != NULL)
2244 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002245 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002246 {
2247 name[0] = K_SPECIAL;
2248 name[1] = KS_EXTRA;
2249 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002251 STRCPY(name + 3, sid_buf);
2252 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2254 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002255 }
2256 *pp = end;
2257
2258theend:
2259 clear_lval(&lv);
2260 return name;
2261}
2262
2263/*
2264 * ":function"
2265 */
2266 void
2267ex_function(exarg_T *eap)
2268{
2269 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002270 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002271 int j;
2272 int c;
2273 int saved_did_emsg;
2274 int saved_wait_return = need_wait_return;
2275 char_u *name = NULL;
2276 char_u *p;
2277 char_u *arg;
2278 char_u *line_arg = NULL;
2279 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002280 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002281 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002282 garray_T newlines;
2283 int varargs = FALSE;
2284 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002286 ufunc_T *fp;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002287 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002288 int indent;
2289 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290#define MAX_FUNC_NESTING 50
2291 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002292 dictitem_T *v;
2293 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002294 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002295 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002296 int todo;
2297 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002298 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002299 linenr_T sourcing_lnum_off;
2300 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002301 int is_heredoc = FALSE;
2302 char_u *skip_until = NULL;
2303 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002304
2305 /*
2306 * ":function" without argument: list functions.
2307 */
2308 if (ends_excmd(*eap->arg))
2309 {
2310 if (!eap->skip)
2311 {
2312 todo = (int)func_hashtab.ht_used;
2313 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2314 {
2315 if (!HASHITEM_EMPTY(hi))
2316 {
2317 --todo;
2318 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319 if ((fp->uf_flags & FC_DEAD)
2320 || message_filtered(fp->uf_name))
Bram Moolenaarf86db782018-10-25 13:31:37 +02002321 continue;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002322 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002323 list_func_head(fp, FALSE);
2324 }
2325 }
2326 }
2327 eap->nextcmd = check_nextcmd(eap->arg);
2328 return;
2329 }
2330
2331 /*
2332 * ":function /pat": list functions matching pattern.
2333 */
2334 if (*eap->arg == '/')
2335 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002336 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002337 if (!eap->skip)
2338 {
2339 regmatch_T regmatch;
2340
2341 c = *p;
2342 *p = NUL;
2343 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2344 *p = c;
2345 if (regmatch.regprog != NULL)
2346 {
2347 regmatch.rm_ic = p_ic;
2348
2349 todo = (int)func_hashtab.ht_used;
2350 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2351 {
2352 if (!HASHITEM_EMPTY(hi))
2353 {
2354 --todo;
2355 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002356 if ((fp->uf_flags & FC_DEAD) == 0
2357 && !isdigit(*fp->uf_name)
2358 && vim_regexec(&regmatch, fp->uf_name, 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002359 list_func_head(fp, FALSE);
2360 }
2361 }
2362 vim_regfree(regmatch.regprog);
2363 }
2364 }
2365 if (*p == '/')
2366 ++p;
2367 eap->nextcmd = check_nextcmd(p);
2368 return;
2369 }
2370
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371 ga_init(&newargs);
2372 ga_init(&argtypes);
2373 ga_init(&default_args);
2374
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002375 /*
2376 * Get the function name. There are these situations:
2377 * func normal function name
2378 * "name" == func, "fudi.fd_dict" == NULL
2379 * dict.func new dictionary entry
2380 * "name" == NULL, "fudi.fd_dict" set,
2381 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2382 * dict.func existing dict entry with a Funcref
2383 * "name" == func, "fudi.fd_dict" set,
2384 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2385 * dict.func existing dict entry that's not a Funcref
2386 * "name" == NULL, "fudi.fd_dict" set,
2387 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2388 * s:func script-local function name
2389 * g:func global function name, same as "func"
2390 */
2391 p = eap->arg;
Bram Moolenaar3388d332017-12-07 22:23:04 +01002392 name = trans_function_name(&p, eap->skip, TFN_NO_AUTOLOAD, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002393 paren = (vim_strchr(p, '(') != NULL);
2394 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
2395 {
2396 /*
2397 * Return on an invalid expression in braces, unless the expression
2398 * evaluation has been cancelled due to an aborting error, an
2399 * interrupt, or an exception.
2400 */
2401 if (!aborting())
2402 {
2403 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002404 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002405 vim_free(fudi.fd_newkey);
2406 return;
2407 }
2408 else
2409 eap->skip = TRUE;
2410 }
2411
Bram Moolenaare38eab22019-12-05 21:50:01 +01002412 // An error in a function call during evaluation of an expression in magic
2413 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002414 saved_did_emsg = did_emsg;
2415 did_emsg = FALSE;
2416
2417 /*
2418 * ":function func" with only function name: list function.
2419 */
2420 if (!paren)
2421 {
2422 if (!ends_excmd(*skipwhite(p)))
2423 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002424 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002425 goto ret_free;
2426 }
2427 eap->nextcmd = check_nextcmd(p);
2428 if (eap->nextcmd != NULL)
2429 *p = NUL;
2430 if (!eap->skip && !got_int)
2431 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 fp = find_func(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002433 if (fp != NULL)
2434 {
2435 list_func_head(fp, TRUE);
2436 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2437 {
2438 if (FUNCLINE(fp, j) == NULL)
2439 continue;
2440 msg_putchar('\n');
2441 msg_outnum((long)(j + 1));
2442 if (j < 9)
2443 msg_putchar(' ');
2444 if (j < 99)
2445 msg_putchar(' ');
2446 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002447 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002448 ui_breakcheck();
2449 }
2450 if (!got_int)
2451 {
2452 msg_putchar('\n');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453 if (fp->uf_dfunc_idx >= 0)
2454 msg_puts(" enddef");
2455 else
2456 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002457 }
2458 }
2459 else
2460 emsg_funcname(N_("E123: Undefined function: %s"), name);
2461 }
2462 goto ret_free;
2463 }
2464
2465 /*
2466 * ":function name(arg1, arg2)" Define function.
2467 */
2468 p = skipwhite(p);
2469 if (*p != '(')
2470 {
2471 if (!eap->skip)
2472 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002473 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002474 goto ret_free;
2475 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002476 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002477 if (vim_strchr(p, '(') != NULL)
2478 p = vim_strchr(p, '(');
2479 }
2480 p = skipwhite(p + 1);
2481
2482 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2483
2484 if (!eap->skip)
2485 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002486 // Check the name of the function. Unless it's a dictionary function
2487 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002488 if (name != NULL)
2489 arg = name;
2490 else
2491 arg = fudi.fd_newkey;
2492 if (arg != NULL && (fudi.fd_di == NULL
2493 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2494 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2495 {
2496 if (*arg == K_SPECIAL)
2497 j = 3;
2498 else
2499 j = 0;
2500 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2501 : eval_isnamec(arg[j])))
2502 ++j;
2503 if (arg[j] != NUL)
2504 emsg_funcname((char *)e_invarg2, arg);
2505 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002506 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002507 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002508 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002509 }
2510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 if (get_function_args(&p, ')', &newargs,
2512 eap->cmdidx == CMD_def ? &argtypes : NULL,
2513 &varargs, &default_args, eap->skip) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002514 goto errret_2;
2515
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002517 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 // find the return type: :def Func(): type
2519 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002520 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 ret_type = skipwhite(p + 1);
2522 p = skip_type(ret_type);
2523 if (p > ret_type)
2524 p = skipwhite(p);
2525 else
2526 semsg(_("E1056: expected a type: %s"), ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002527 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002528 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 else
2530 // find extra arguments "range", "dict", "abort" and "closure"
2531 for (;;)
2532 {
2533 p = skipwhite(p);
2534 if (STRNCMP(p, "range", 5) == 0)
2535 {
2536 flags |= FC_RANGE;
2537 p += 5;
2538 }
2539 else if (STRNCMP(p, "dict", 4) == 0)
2540 {
2541 flags |= FC_DICT;
2542 p += 4;
2543 }
2544 else if (STRNCMP(p, "abort", 5) == 0)
2545 {
2546 flags |= FC_ABORT;
2547 p += 5;
2548 }
2549 else if (STRNCMP(p, "closure", 7) == 0)
2550 {
2551 flags |= FC_CLOSURE;
2552 p += 7;
2553 if (current_funccal == NULL)
2554 {
2555 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
2556 name == NULL ? (char_u *)"" : name);
2557 goto erret;
2558 }
2559 }
2560 else
2561 break;
2562 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002563
Bram Moolenaare38eab22019-12-05 21:50:01 +01002564 // When there is a line break use what follows for the function body.
2565 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002566 if (*p == '\n')
2567 line_arg = p + 1;
2568 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002569 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002570
2571 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
2573 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002574 */
2575 if (KeyTyped)
2576 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002577 // Check if the function already exists, don't let the user type the
2578 // whole function before telling him it doesn't work! For a script we
2579 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002580 if (!eap->skip && !eap->forceit)
2581 {
2582 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002583 emsg(_(e_funcdict));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584 else if (name != NULL && find_func(name, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002585 emsg_funcname(e_funcexts, name);
2586 }
2587
2588 if (!eap->skip && did_emsg)
2589 goto erret;
2590
Bram Moolenaare38eab22019-12-05 21:50:01 +01002591 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002592 cmdline_row = msg_row;
2593 }
2594
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002595 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002596 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002597
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002598 indent = 2;
2599 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002601 for (;;)
2602 {
2603 if (KeyTyped)
2604 {
2605 msg_scroll = TRUE;
2606 saved_wait_return = FALSE;
2607 }
2608 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002609
2610 if (line_arg != NULL)
2611 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002612 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002613 theline = line_arg;
2614 p = vim_strchr(theline, '\n');
2615 if (p == NULL)
2616 line_arg += STRLEN(line_arg);
2617 else
2618 {
2619 *p = NUL;
2620 line_arg = p + 1;
2621 }
2622 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002623 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002624 {
2625 vim_free(line_to_free);
2626 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002627 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002628 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002629 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002630 line_to_free = theline;
2631 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002632 if (KeyTyped)
2633 lines_left = Rows - 1;
2634 if (theline == NULL)
2635 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 if (eap->cmdidx == CMD_def)
2637 emsg(_("E1057: Missing :enddef"));
2638 else
2639 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002640 goto erret;
2641 }
2642
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002643 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002644 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002645 if (SOURCING_LNUM < sourcing_lnum_off)
2646 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002647 else
2648 sourcing_lnum_off = 0;
2649
2650 if (skip_until != NULL)
2651 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002653 // * ":append" and "."
2654 // * ":python <<EOF" and "EOF"
2655 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2656 if (heredoc_trimmed == NULL
2657 || (is_heredoc && skipwhite(theline) == theline)
2658 || STRNCMP(theline, heredoc_trimmed,
2659 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002660 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002661 if (heredoc_trimmed == NULL)
2662 p = theline;
2663 else if (is_heredoc)
2664 p = skipwhite(theline) == theline
2665 ? theline : theline + STRLEN(heredoc_trimmed);
2666 else
2667 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002668 if (STRCMP(p, skip_until) == 0)
2669 {
2670 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002671 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002672 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002673 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002674 }
2675 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002676 }
2677 else
2678 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002679 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01002680 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002681 ;
2682
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683 // Check for "endfunction" or "enddef".
2684 if (checkforcmd(&p, nesting_def[nesting]
2685 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002686 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002687 char_u *nextcmd = NULL;
2688
Bram Moolenaar663bb232017-06-22 19:12:10 +02002689 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002690 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002691 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002692 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002693 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694 give_warning2(eap->cmdidx == CMD_def
2695 ? (char_u *)_("W1001: Text found after :enddef: %s")
2696 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002697 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002698 if (nextcmd != NULL)
2699 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002700 // Another command follows. If the line came from "eap" we
2701 // can simply point into it, otherwise we need to change
2702 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02002703 eap->nextcmd = nextcmd;
2704 if (line_to_free != NULL)
2705 {
2706 vim_free(*eap->cmdlinep);
2707 *eap->cmdlinep = line_to_free;
2708 line_to_free = NULL;
2709 }
2710 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002711 break;
2712 }
2713
Bram Moolenaare38eab22019-12-05 21:50:01 +01002714 // Increase indent inside "if", "while", "for" and "try", decrease
2715 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002717 indent -= 2;
2718 else if (STRNCMP(p, "if", 2) == 0
2719 || STRNCMP(p, "wh", 2) == 0
2720 || STRNCMP(p, "for", 3) == 0
2721 || STRNCMP(p, "try", 3) == 0)
2722 indent += 2;
2723
Bram Moolenaare38eab22019-12-05 21:50:01 +01002724 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01002725 // Only recognize "def" inside "def", not inside "function",
2726 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01002728 if (checkforcmd(&p, "function", 2)
2729 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002730 {
2731 if (*p == '!')
2732 p = skipwhite(p + 1);
2733 p += eval_fname_script(p);
2734 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
2735 if (*skipwhite(p) == '(')
2736 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002737 if (nesting == MAX_FUNC_NESTING - 1)
2738 emsg(_("E1058: function nesting too deep"));
2739 else
2740 {
2741 ++nesting;
2742 nesting_def[nesting] = (c == 'd');
2743 indent += 2;
2744 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002745 }
2746 }
2747
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002748 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002749 p = skip_range(p, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002750 if (eap->cmdidx != CMD_def
2751 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002752 || (p[0] == 'c'
2753 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2754 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2755 && (STRNCMP(&p[3], "nge", 3) != 0
2756 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002757 || (p[0] == 'i'
2758 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002759 && (!ASCII_ISALPHA(p[2])
2760 || (p[2] == 's'
2761 && (!ASCII_ISALPHA(p[3])
2762 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002763 skip_until = vim_strsave((char_u *)".");
2764
Bram Moolenaare38eab22019-12-05 21:50:01 +01002765 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002766 arg = skipwhite(skiptowhite(p));
2767 if (arg[0] == '<' && arg[1] =='<'
2768 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002769 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2770 || ((p[2] == '3' || p[2] == 'x')
2771 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002772 || (p[0] == 'p' && p[1] == 'e'
2773 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2774 || (p[0] == 't' && p[1] == 'c'
2775 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2776 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2777 && !ASCII_ISALPHA(p[3]))
2778 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2779 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2780 || (p[0] == 'm' && p[1] == 'z'
2781 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2782 ))
2783 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002784 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002785 p = skipwhite(arg + 2);
2786 if (*p == NUL)
2787 skip_until = vim_strsave((char_u *)".");
2788 else
2789 skip_until = vim_strsave(p);
2790 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002791
2792 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002793 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02002794 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002795 if (*arg == '[')
2796 arg = vim_strchr(arg, ']');
2797 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002798 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002799 arg = skipwhite(skiptowhite(arg));
2800 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
2801 && ((p[0] == 'l'
2802 && p[1] == 'e'
2803 && (!ASCII_ISALNUM(p[2])
2804 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02002805 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002806 p = skipwhite(arg + 3);
2807 if (STRNCMP(p, "trim", 4) == 0)
2808 {
2809 // Ignore leading white space.
2810 p = skipwhite(p + 4);
2811 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar8471e572019-05-19 21:37:18 +02002812 (int)(skipwhite(theline) - theline));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002813 }
2814 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2815 do_concat = FALSE;
2816 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002817 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002818 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002819 }
2820
Bram Moolenaare38eab22019-12-05 21:50:01 +01002821 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002822 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002823 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002824
Bram Moolenaare38eab22019-12-05 21:50:01 +01002825 // Copy the line to newly allocated memory. get_one_sourceline()
2826 // allocates 250 bytes per line, this saves 80% on average. The cost
2827 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002828 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002829 if (p == NULL)
2830 goto erret;
2831 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002832
Bram Moolenaare38eab22019-12-05 21:50:01 +01002833 // Add NULL lines for continuation lines, so that the line count is
2834 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002835 while (sourcing_lnum_off-- > 0)
2836 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2837
Bram Moolenaare38eab22019-12-05 21:50:01 +01002838 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002839 if (line_arg != NULL && *line_arg == NUL)
2840 line_arg = NULL;
2841 }
2842
Bram Moolenaare38eab22019-12-05 21:50:01 +01002843 // Don't define the function when skipping commands or when an error was
2844 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002845 if (eap->skip || did_emsg)
2846 goto erret;
2847
2848 /*
2849 * If there are no errors, add the function
2850 */
2851 if (fudi.fd_dict == NULL)
2852 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 hashtab_T *ht;
2854
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002855 v = find_var(name, &ht, FALSE);
2856 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
2857 {
2858 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
2859 name);
2860 goto erret;
2861 }
2862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 fp = find_func_even_dead(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002864 if (fp != NULL)
2865 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 int dead = fp->uf_flags & FC_DEAD;
2867
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002868 // Function can be replaced with "function!" and when sourcing the
2869 // same script again, but only once.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 if (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002871 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
2872 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002873 {
2874 emsg_funcname(e_funcexts, name);
2875 goto erret;
2876 }
2877 if (fp->uf_calls > 0)
2878 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002879 emsg_funcname(
2880 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002881 name);
2882 goto erret;
2883 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002884 if (fp->uf_refcount > 1)
2885 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002886 // This function is referenced somewhere, don't redefine it but
2887 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002888 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002889 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002890 fp = NULL;
2891 overwrite = TRUE;
2892 }
2893 else
2894 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002895 char_u *exp_name = fp->uf_name_exp;
2896
2897 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01002898 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002899 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002900 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002901 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002903#ifdef FEAT_PROFILE
2904 fp->uf_profiling = FALSE;
2905 fp->uf_prof_initialized = FALSE;
2906#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002907 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002908 }
2909 }
2910 else
2911 {
2912 char numbuf[20];
2913
2914 fp = NULL;
2915 if (fudi.fd_newkey == NULL && !eap->forceit)
2916 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002917 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002918 goto erret;
2919 }
2920 if (fudi.fd_di == NULL)
2921 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002922 // Can't add a function to a locked dictionary
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002923 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002924 goto erret;
2925 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002926 // Can't change an existing function if it is locked
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002927 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002928 goto erret;
2929
Bram Moolenaare38eab22019-12-05 21:50:01 +01002930 // Give the function a sequential number. Can only be used with a
2931 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002932 vim_free(name);
2933 sprintf(numbuf, "%d", ++func_nr);
2934 name = vim_strsave((char_u *)numbuf);
2935 if (name == NULL)
2936 goto erret;
2937 }
2938
2939 if (fp == NULL)
2940 {
2941 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
2942 {
2943 int slen, plen;
2944 char_u *scriptname;
2945
Bram Moolenaare38eab22019-12-05 21:50:01 +01002946 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002947 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002948 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002949 {
2950 scriptname = autoload_name(name);
2951 if (scriptname != NULL)
2952 {
2953 p = vim_strchr(scriptname, '/');
2954 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002955 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002956 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002957 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002958 j = OK;
2959 vim_free(scriptname);
2960 }
2961 }
2962 if (j == FAIL)
2963 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002964 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002965 goto erret;
2966 }
2967 }
2968
Bram Moolenaar47ed5532019-08-08 20:49:14 +02002969 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002970 if (fp == NULL)
2971 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 fp->uf_dfunc_idx = -1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002973
2974 if (fudi.fd_dict != NULL)
2975 {
2976 if (fudi.fd_di == NULL)
2977 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002978 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002979 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
2980 if (fudi.fd_di == NULL)
2981 {
2982 vim_free(fp);
2983 goto erret;
2984 }
2985 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
2986 {
2987 vim_free(fudi.fd_di);
2988 vim_free(fp);
2989 goto erret;
2990 }
2991 }
2992 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01002993 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002994 clear_tv(&fudi.fd_di->di_tv);
2995 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002996 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002997
Bram Moolenaare38eab22019-12-05 21:50:01 +01002998 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002999 flags |= FC_DICT;
3000 }
3001
Bram Moolenaare38eab22019-12-05 21:50:01 +01003002 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003003 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003004 if (overwrite)
3005 {
3006 hi = hash_find(&func_hashtab, name);
3007 hi->hi_key = UF2HIKEY(fp);
3008 }
3009 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003010 {
3011 vim_free(fp);
3012 goto erret;
3013 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003014 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003015 }
3016 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003017 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003019 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020
3021 if (eap->cmdidx == CMD_def)
3022 {
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003023 int lnum_save = SOURCING_LNUM;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003024
3025 // error messages are for the first function line
3026 SOURCING_LNUM = sourcing_lnum_top;
3027
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 // parse the argument types
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003029 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030
3031 if (argtypes.ga_len > 0)
3032 {
3033 // When "varargs" is set the last name/type goes into uf_va_name
3034 // and uf_va_type.
3035 int len = argtypes.ga_len - (varargs ? 1 : 0);
3036
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003037 if (len > 0)
3038 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 if (fp->uf_arg_types != NULL)
3040 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003041 int i;
3042 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043
3044 for (i = 0; i < len; ++ i)
3045 {
3046 p = ((char_u **)argtypes.ga_data)[i];
3047 if (p == NULL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003048 // will get the type from the default value
3049 type = &t_unknown;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 else
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003051 type = parse_type(&p, &fp->uf_type_list);
3052 if (type == NULL)
3053 {
3054 SOURCING_LNUM = lnum_save;
3055 goto errret_2;
3056 }
3057 fp->uf_arg_types[i] = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 }
3059 }
3060 if (varargs)
3061 {
3062 // Move the last argument "...name: type" to uf_va_name and
3063 // uf_va_type.
3064 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
3065 [fp->uf_args.ga_len - 1];
3066 --fp->uf_args.ga_len;
3067 p = ((char_u **)argtypes.ga_data)[len];
3068 if (p == NULL)
3069 // todo: get type from default value
3070 fp->uf_va_type = &t_any;
3071 else
3072 fp->uf_va_type = parse_type(&p, &fp->uf_type_list);
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003073 if (fp->uf_va_type == NULL)
3074 {
3075 SOURCING_LNUM = lnum_save;
3076 goto errret_2;
3077 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 }
3079 varargs = FALSE;
3080 }
3081
3082 // parse the return type, if any
3083 if (ret_type == NULL)
3084 fp->uf_ret_type = &t_void;
3085 else
3086 {
3087 p = ret_type;
3088 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list);
3089 }
3090 }
3091
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003092 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003093 if ((flags & FC_CLOSURE) != 0)
3094 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003095 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003096 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003097 }
3098 else
3099 fp->uf_scoped = NULL;
3100
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003101#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003102 if (prof_def_func())
3103 func_do_profile(fp);
3104#endif
3105 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003106 if (sandbox)
3107 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003108 fp->uf_flags = flags;
3109 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003110 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003111 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003112 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 if (is_export)
3114 {
3115 fp->uf_flags |= FC_EXPORT;
3116 // let ex_export() know the export worked.
3117 is_export = FALSE;
3118 }
3119
3120 // ":def Func()" needs to be compiled
3121 if (eap->cmdidx == CMD_def)
3122 compile_def_function(fp, FALSE);
3123
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003124 goto ret_free;
3125
3126erret:
3127 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003128 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003129errret_2:
3130 ga_clear_strings(&newlines);
3131ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003132 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003133 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003134 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003135 vim_free(fudi.fd_newkey);
3136 vim_free(name);
3137 did_emsg |= saved_did_emsg;
3138 need_wait_return |= saved_wait_return;
3139}
3140
3141/*
3142 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3143 * Return 2 if "p" starts with "s:".
3144 * Return 0 otherwise.
3145 */
3146 int
3147eval_fname_script(char_u *p)
3148{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003149 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3150 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003151 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3152 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3153 return 5;
3154 if (p[0] == 's' && p[1] == ':')
3155 return 2;
3156 return 0;
3157}
3158
3159 int
3160translated_function_exists(char_u *name)
3161{
3162 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003163 return has_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 return find_func(name, NULL) != NULL;
3165}
3166
3167/*
3168 * Return TRUE when "ufunc" has old-style "..." varargs
3169 * or named varargs "...name: type".
3170 */
3171 int
3172has_varargs(ufunc_T *ufunc)
3173{
3174 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003175}
3176
3177/*
3178 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003179 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003180 */
3181 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003182function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003183{
3184 char_u *nm = name;
3185 char_u *p;
3186 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003187 int flag;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003188
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003189 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3190 if (no_deref)
3191 flag |= TFN_NO_DEREF;
3192 p = trans_function_name(&nm, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003193 nm = skipwhite(nm);
3194
Bram Moolenaare38eab22019-12-05 21:50:01 +01003195 // Only accept "funcname", "funcname ", "funcname (..." and
3196 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003197 if (p != NULL && (*nm == NUL || *nm == '('))
3198 n = translated_function_exists(p);
3199 vim_free(p);
3200 return n;
3201}
3202
Bram Moolenaar113e1072019-01-20 15:30:40 +01003203#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003204 char_u *
3205get_expanded_name(char_u *name, int check)
3206{
3207 char_u *nm = name;
3208 char_u *p;
3209
3210 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
3211
3212 if (p != NULL && *nm == NUL)
3213 if (!check || translated_function_exists(p))
3214 return p;
3215
3216 vim_free(p);
3217 return NULL;
3218}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003219#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003220
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003221/*
3222 * Function given to ExpandGeneric() to obtain the list of user defined
3223 * function names.
3224 */
3225 char_u *
3226get_user_func_name(expand_T *xp, int idx)
3227{
3228 static long_u done;
3229 static hashitem_T *hi;
3230 ufunc_T *fp;
3231
3232 if (idx == 0)
3233 {
3234 done = 0;
3235 hi = func_hashtab.ht_array;
3236 }
3237 if (done < func_hashtab.ht_used)
3238 {
3239 if (done++ > 0)
3240 ++hi;
3241 while (HASHITEM_EMPTY(hi))
3242 ++hi;
3243 fp = HI2UF(hi);
3244
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 // don't show dead, dict and lambda functions
3246 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02003247 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003249
3250 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003251 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003252
3253 cat_func_name(IObuff, fp);
3254 if (xp->xp_context != EXPAND_USER_FUNC)
3255 {
3256 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003258 STRCAT(IObuff, ")");
3259 }
3260 return IObuff;
3261 }
3262 return NULL;
3263}
3264
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003265/*
3266 * ":delfunction {name}"
3267 */
3268 void
3269ex_delfunction(exarg_T *eap)
3270{
3271 ufunc_T *fp = NULL;
3272 char_u *p;
3273 char_u *name;
3274 funcdict_T fudi;
3275
3276 p = eap->arg;
3277 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
3278 vim_free(fudi.fd_newkey);
3279 if (name == NULL)
3280 {
3281 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003282 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003283 return;
3284 }
3285 if (!ends_excmd(*skipwhite(p)))
3286 {
3287 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003288 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003289 return;
3290 }
3291 eap->nextcmd = check_nextcmd(p);
3292 if (eap->nextcmd != NULL)
3293 *p = NUL;
3294
3295 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 fp = find_func(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003297 vim_free(name);
3298
3299 if (!eap->skip)
3300 {
3301 if (fp == NULL)
3302 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02003303 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003304 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003305 return;
3306 }
3307 if (fp->uf_calls > 0)
3308 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003309 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003310 return;
3311 }
3312
3313 if (fudi.fd_dict != NULL)
3314 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003315 // Delete the dict item that refers to the function, it will
3316 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003317 dictitem_remove(fudi.fd_dict, fudi.fd_di);
3318 }
3319 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003320 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003321 // A normal function (not a numbered function or lambda) has a
3322 // refcount of 1 for the entry in the hashtable. When deleting
3323 // it and the refcount is more than one, it should be kept.
3324 // A numbered function and lambda should be kept if the refcount is
3325 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003326 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003327 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003328 // Function is still referenced somewhere. Don't free it but
3329 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003330 if (func_remove(fp))
3331 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003332 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003333 }
3334 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003335 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003336 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003337 }
3338}
3339
3340/*
3341 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003342 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003343 */
3344 void
3345func_unref(char_u *name)
3346{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003347 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003348
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003349 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003350 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003352 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003353 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003354#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003355 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003356#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01003357 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003358 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003359 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003360 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003361 // Only delete it when it's not being used. Otherwise it's done
3362 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003363 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003364 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02003365 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003366}
3367
3368/*
3369 * Unreference a Function: decrement the reference count and free it when it
3370 * becomes zero.
3371 */
3372 void
3373func_ptr_unref(ufunc_T *fp)
3374{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003375 if (fp != NULL && --fp->uf_refcount <= 0)
3376 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003377 // Only delete it when it's not being used. Otherwise it's done
3378 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02003379 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003380 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003381 }
3382}
3383
3384/*
3385 * Count a reference to a Function.
3386 */
3387 void
3388func_ref(char_u *name)
3389{
3390 ufunc_T *fp;
3391
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003392 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003393 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003395 if (fp != NULL)
3396 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003397 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01003398 // Only give an error for a numbered function.
3399 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01003400 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003401}
3402
3403/*
3404 * Count a reference to a Function.
3405 */
3406 void
3407func_ptr_ref(ufunc_T *fp)
3408{
3409 if (fp != NULL)
3410 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003411}
3412
3413/*
3414 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3415 * referenced from anywhere that is in use.
3416 */
3417 static int
3418can_free_funccal(funccall_T *fc, int copyID)
3419{
3420 return (fc->l_varlist.lv_copyID != copyID
3421 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003422 && fc->l_avars.dv_copyID != copyID
3423 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003424}
3425
3426/*
3427 * ":return [expr]"
3428 */
3429 void
3430ex_return(exarg_T *eap)
3431{
3432 char_u *arg = eap->arg;
3433 typval_T rettv;
3434 int returning = FALSE;
3435
3436 if (current_funccal == NULL)
3437 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003438 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003439 return;
3440 }
3441
3442 if (eap->skip)
3443 ++emsg_skip;
3444
3445 eap->nextcmd = NULL;
3446 if ((*arg != NUL && *arg != '|' && *arg != '\n')
3447 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
3448 {
3449 if (!eap->skip)
3450 returning = do_return(eap, FALSE, TRUE, &rettv);
3451 else
3452 clear_tv(&rettv);
3453 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003454 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003455 else if (!eap->skip)
3456 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003457 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003458 update_force_abort();
3459
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003460 /*
3461 * Return unless the expression evaluation has been cancelled due to an
3462 * aborting error, an interrupt, or an exception.
3463 */
3464 if (!aborting())
3465 returning = do_return(eap, FALSE, TRUE, NULL);
3466 }
3467
Bram Moolenaare38eab22019-12-05 21:50:01 +01003468 // When skipping or the return gets pending, advance to the next command
3469 // in this line (!returning). Otherwise, ignore the rest of the line.
3470 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003471 if (returning)
3472 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003473 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003474 eap->nextcmd = check_nextcmd(arg);
3475
3476 if (eap->skip)
3477 --emsg_skip;
3478}
3479
3480/*
3481 * ":1,25call func(arg1, arg2)" function call.
3482 */
3483 void
3484ex_call(exarg_T *eap)
3485{
3486 char_u *arg = eap->arg;
3487 char_u *startarg;
3488 char_u *name;
3489 char_u *tofree;
3490 int len;
3491 typval_T rettv;
3492 linenr_T lnum;
3493 int doesrange;
3494 int failed = FALSE;
3495 funcdict_T fudi;
3496 partial_T *partial = NULL;
3497
3498 if (eap->skip)
3499 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003500 // trans_function_name() doesn't work well when skipping, use eval0()
3501 // instead to skip to any following command, e.g. for:
3502 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003503 ++emsg_skip;
3504 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3505 clear_tv(&rettv);
3506 --emsg_skip;
3507 return;
3508 }
3509
3510 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
3511 if (fudi.fd_newkey != NULL)
3512 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003513 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003514 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003515 vim_free(fudi.fd_newkey);
3516 }
3517 if (tofree == NULL)
3518 return;
3519
Bram Moolenaare38eab22019-12-05 21:50:01 +01003520 // Increase refcount on dictionary, it could get deleted when evaluating
3521 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003522 if (fudi.fd_dict != NULL)
3523 ++fudi.fd_dict->dv_refcount;
3524
Bram Moolenaare38eab22019-12-05 21:50:01 +01003525 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3526 // contents. For VAR_PARTIAL get its partial, unless we already have one
3527 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003528 len = (int)STRLEN(tofree);
3529 name = deref_func_name(tofree, &len,
3530 partial != NULL ? NULL : &partial, FALSE);
3531
Bram Moolenaare38eab22019-12-05 21:50:01 +01003532 // Skip white space to allow ":call func ()". Not good, but required for
3533 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003534 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003535 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003536
3537 if (*startarg != '(')
3538 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003540 goto end;
3541 }
3542
3543 /*
3544 * When skipping, evaluate the function once, to find the end of the
3545 * arguments.
3546 * When the function takes a range, this is discovered after the first
3547 * call, and the loop is broken.
3548 */
3549 if (eap->skip)
3550 {
3551 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003552 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003553 }
3554 else
3555 lnum = eap->line1;
3556 for ( ; lnum <= eap->line2; ++lnum)
3557 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003558 funcexe_T funcexe;
3559
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003560 if (!eap->skip && eap->addr_count > 0)
3561 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003562 if (lnum > curbuf->b_ml.ml_line_count)
3563 {
3564 // If the function deleted lines or switched to another buffer
3565 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003566 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003567 break;
3568 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003569 curwin->w_cursor.lnum = lnum;
3570 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003571 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003572 }
3573 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003574
Bram Moolenaarac92e252019-08-03 21:58:38 +02003575 vim_memset(&funcexe, 0, sizeof(funcexe));
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003576 funcexe.firstline = eap->line1;
3577 funcexe.lastline = eap->line2;
3578 funcexe.doesrange = &doesrange;
3579 funcexe.evaluate = !eap->skip;
3580 funcexe.partial = partial;
3581 funcexe.selfdict = fudi.fd_dict;
3582 if (get_func_tv(name, -1, &rettv, &arg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003583 {
3584 failed = TRUE;
3585 break;
3586 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003587 if (has_watchexpr())
3588 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003589
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003590 // Handle a function returning a Funcref, Dictionary or List.
3591 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE,
3592 name, &name) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003593 {
3594 failed = TRUE;
3595 break;
3596 }
3597
3598 clear_tv(&rettv);
3599 if (doesrange || eap->skip)
3600 break;
3601
Bram Moolenaare38eab22019-12-05 21:50:01 +01003602 // Stop when immediately aborting on error, or when an interrupt
3603 // occurred or an exception was thrown but not caught.
3604 // get_func_tv() returned OK, so that the check for trailing
3605 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003606 if (aborting())
3607 break;
3608 }
3609 if (eap->skip)
3610 --emsg_skip;
3611
Bram Moolenaare51bb172020-02-16 19:42:23 +01003612 // When inside :try we need to check for following "| catch".
3613 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003614 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003615 // Check for trailing illegal characters and a following command.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003616 if (!ends_excmd(*arg))
3617 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01003618 if (!failed)
3619 {
3620 emsg_severe = TRUE;
3621 emsg(_(e_trailing));
3622 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003623 }
3624 else
3625 eap->nextcmd = check_nextcmd(arg);
3626 }
3627
3628end:
3629 dict_unref(fudi.fd_dict);
3630 vim_free(tofree);
3631}
3632
3633/*
3634 * Return from a function. Possibly makes the return pending. Also called
3635 * for a pending return at the ":endtry" or after returning from an extra
3636 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3637 * when called due to a ":return" command. "rettv" may point to a typval_T
3638 * with the return rettv. Returns TRUE when the return can be carried out,
3639 * FALSE when the return gets pending.
3640 */
3641 int
3642do_return(
3643 exarg_T *eap,
3644 int reanimate,
3645 int is_cmd,
3646 void *rettv)
3647{
3648 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01003649 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003650
3651 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003652 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003653 current_funccal->returned = FALSE;
3654
3655 /*
3656 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3657 * not in its finally clause (which then is to be executed next) is found.
3658 * In this case, make the ":return" pending for execution at the ":endtry".
3659 * Otherwise, return normally.
3660 */
3661 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3662 if (idx >= 0)
3663 {
3664 cstack->cs_pending[idx] = CSTP_RETURN;
3665
3666 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003667 // A pending return again gets pending. "rettv" points to an
3668 // allocated variable with the rettv of the original ":return"'s
3669 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003670 cstack->cs_rettv[idx] = rettv;
3671 else
3672 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003673 // When undoing a return in order to make it pending, get the stored
3674 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003675 if (reanimate)
3676 rettv = current_funccal->rettv;
3677
3678 if (rettv != NULL)
3679 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003680 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003681 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3682 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3683 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003684 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003685 }
3686 else
3687 cstack->cs_rettv[idx] = NULL;
3688
3689 if (reanimate)
3690 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003691 // The pending return value could be overwritten by a ":return"
3692 // without argument in a finally clause; reset the default
3693 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003694 current_funccal->rettv->v_type = VAR_NUMBER;
3695 current_funccal->rettv->vval.v_number = 0;
3696 }
3697 }
3698 report_make_pending(CSTP_RETURN, rettv);
3699 }
3700 else
3701 {
3702 current_funccal->returned = TRUE;
3703
Bram Moolenaare38eab22019-12-05 21:50:01 +01003704 // If the return is carried out now, store the return value. For
3705 // a return immediately after reanimation, the value is already
3706 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003707 if (!reanimate && rettv != NULL)
3708 {
3709 clear_tv(current_funccal->rettv);
3710 *current_funccal->rettv = *(typval_T *)rettv;
3711 if (!is_cmd)
3712 vim_free(rettv);
3713 }
3714 }
3715
3716 return idx < 0;
3717}
3718
3719/*
3720 * Free the variable with a pending return value.
3721 */
3722 void
3723discard_pending_return(void *rettv)
3724{
3725 free_tv((typval_T *)rettv);
3726}
3727
3728/*
3729 * Generate a return command for producing the value of "rettv". The result
3730 * is an allocated string. Used by report_pending() for verbose messages.
3731 */
3732 char_u *
3733get_return_cmd(void *rettv)
3734{
3735 char_u *s = NULL;
3736 char_u *tofree = NULL;
3737 char_u numbuf[NUMBUFLEN];
3738
3739 if (rettv != NULL)
3740 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3741 if (s == NULL)
3742 s = (char_u *)"";
3743
3744 STRCPY(IObuff, ":return ");
3745 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3746 if (STRLEN(s) + 8 >= IOSIZE)
3747 STRCPY(IObuff + IOSIZE - 4, "...");
3748 vim_free(tofree);
3749 return vim_strsave(IObuff);
3750}
3751
3752/*
3753 * Get next function line.
3754 * Called by do_cmdline() to get the next line.
3755 * Returns allocated string, or NULL for end of function.
3756 */
3757 char_u *
3758get_func_line(
3759 int c UNUSED,
3760 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003761 int indent UNUSED,
3762 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003763{
3764 funccall_T *fcp = (funccall_T *)cookie;
3765 ufunc_T *fp = fcp->func;
3766 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003767 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003768
Bram Moolenaare38eab22019-12-05 21:50:01 +01003769 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003770 if (fcp->dbg_tick != debug_tick)
3771 {
3772 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003773 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003774 fcp->dbg_tick = debug_tick;
3775 }
3776#ifdef FEAT_PROFILE
3777 if (do_profiling == PROF_YES)
3778 func_line_end(cookie);
3779#endif
3780
3781 gap = &fp->uf_lines;
3782 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3783 || fcp->returned)
3784 retval = NULL;
3785 else
3786 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003787 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003788 while (fcp->linenr < gap->ga_len
3789 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3790 ++fcp->linenr;
3791 if (fcp->linenr >= gap->ga_len)
3792 retval = NULL;
3793 else
3794 {
3795 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003796 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003797#ifdef FEAT_PROFILE
3798 if (do_profiling == PROF_YES)
3799 func_line_start(cookie);
3800#endif
3801 }
3802 }
3803
Bram Moolenaare38eab22019-12-05 21:50:01 +01003804 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003805 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003806 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003807 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003808 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003809 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003810 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003811 fcp->dbg_tick = debug_tick;
3812 }
3813
3814 return retval;
3815}
3816
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003817/*
3818 * Return TRUE if the currently active function should be ended, because a
3819 * return was encountered or an error occurred. Used inside a ":while".
3820 */
3821 int
3822func_has_ended(void *cookie)
3823{
3824 funccall_T *fcp = (funccall_T *)cookie;
3825
Bram Moolenaare38eab22019-12-05 21:50:01 +01003826 // Ignore the "abort" flag if the abortion behavior has been changed due to
3827 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003828 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3829 || fcp->returned);
3830}
3831
3832/*
3833 * return TRUE if cookie indicates a function which "abort"s on errors.
3834 */
3835 int
3836func_has_abort(
3837 void *cookie)
3838{
3839 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
3840}
3841
3842
3843/*
3844 * Turn "dict.Func" into a partial for "Func" bound to "dict".
3845 * Don't do this when "Func" is already a partial that was bound
3846 * explicitly (pt_auto is FALSE).
3847 * Changes "rettv" in-place.
3848 * Returns the updated "selfdict_in".
3849 */
3850 dict_T *
3851make_partial(dict_T *selfdict_in, typval_T *rettv)
3852{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003853 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003854 char_u *tofree = NULL;
3855 ufunc_T *fp;
3856 char_u fname_buf[FLEN_FIXED + 1];
3857 int error;
3858 dict_T *selfdict = selfdict_in;
3859
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003860 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
3861 fp = rettv->vval.v_partial->pt_func;
3862 else
3863 {
3864 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
3865 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003866 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003867 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003869 vim_free(tofree);
3870 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003871
3872 if (fp != NULL && (fp->uf_flags & FC_DICT))
3873 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003874 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003875
3876 if (pt != NULL)
3877 {
3878 pt->pt_refcount = 1;
3879 pt->pt_dict = selfdict;
3880 pt->pt_auto = TRUE;
3881 selfdict = NULL;
3882 if (rettv->v_type == VAR_FUNC)
3883 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003884 // Just a function: Take over the function name and use
3885 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003886 pt->pt_name = rettv->vval.v_string;
3887 }
3888 else
3889 {
3890 partial_T *ret_pt = rettv->vval.v_partial;
3891 int i;
3892
Bram Moolenaare38eab22019-12-05 21:50:01 +01003893 // Partial: copy the function name, use selfdict and copy
3894 // args. Can't take over name or args, the partial might
3895 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003896 if (ret_pt->pt_name != NULL)
3897 {
3898 pt->pt_name = vim_strsave(ret_pt->pt_name);
3899 func_ref(pt->pt_name);
3900 }
3901 else
3902 {
3903 pt->pt_func = ret_pt->pt_func;
3904 func_ptr_ref(pt->pt_func);
3905 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003906 if (ret_pt->pt_argc > 0)
3907 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003908 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003909 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003910 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003911 pt->pt_argc = 0;
3912 else
3913 {
3914 pt->pt_argc = ret_pt->pt_argc;
3915 for (i = 0; i < pt->pt_argc; i++)
3916 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
3917 }
3918 }
3919 partial_unref(ret_pt);
3920 }
3921 rettv->v_type = VAR_PARTIAL;
3922 rettv->vval.v_partial = pt;
3923 }
3924 }
3925 return selfdict;
3926}
3927
3928/*
3929 * Return the name of the executed function.
3930 */
3931 char_u *
3932func_name(void *cookie)
3933{
3934 return ((funccall_T *)cookie)->func->uf_name;
3935}
3936
3937/*
3938 * Return the address holding the next breakpoint line for a funccall cookie.
3939 */
3940 linenr_T *
3941func_breakpoint(void *cookie)
3942{
3943 return &((funccall_T *)cookie)->breakpoint;
3944}
3945
3946/*
3947 * Return the address holding the debug tick for a funccall cookie.
3948 */
3949 int *
3950func_dbg_tick(void *cookie)
3951{
3952 return &((funccall_T *)cookie)->dbg_tick;
3953}
3954
3955/*
3956 * Return the nesting level for a funccall cookie.
3957 */
3958 int
3959func_level(void *cookie)
3960{
3961 return ((funccall_T *)cookie)->level;
3962}
3963
3964/*
3965 * Return TRUE when a function was ended by a ":return" command.
3966 */
3967 int
3968current_func_returned(void)
3969{
3970 return current_funccal->returned;
3971}
3972
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003973 int
3974free_unref_funccal(int copyID, int testing)
3975{
3976 int did_free = FALSE;
3977 int did_free_funccal = FALSE;
3978 funccall_T *fc, **pfc;
3979
3980 for (pfc = &previous_funccal; *pfc != NULL; )
3981 {
3982 if (can_free_funccal(*pfc, copyID))
3983 {
3984 fc = *pfc;
3985 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003986 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003987 did_free = TRUE;
3988 did_free_funccal = TRUE;
3989 }
3990 else
3991 pfc = &(*pfc)->caller;
3992 }
3993 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003994 // When a funccal was freed some more items might be garbage
3995 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003996 (void)garbage_collect(testing);
3997
3998 return did_free;
3999}
4000
4001/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004002 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004003 */
4004 static funccall_T *
4005get_funccal(void)
4006{
4007 int i;
4008 funccall_T *funccal;
4009 funccall_T *temp_funccal;
4010
4011 funccal = current_funccal;
4012 if (debug_backtrace_level > 0)
4013 {
4014 for (i = 0; i < debug_backtrace_level; i++)
4015 {
4016 temp_funccal = funccal->caller;
4017 if (temp_funccal)
4018 funccal = temp_funccal;
4019 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004020 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004021 debug_backtrace_level = i;
4022 }
4023 }
4024 return funccal;
4025}
4026
4027/*
4028 * Return the hashtable used for local variables in the current funccal.
4029 * Return NULL if there is no current funccal.
4030 */
4031 hashtab_T *
4032get_funccal_local_ht()
4033{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004035 return NULL;
4036 return &get_funccal()->l_vars.dv_hashtab;
4037}
4038
4039/*
4040 * Return the l: scope variable.
4041 * Return NULL if there is no current funccal.
4042 */
4043 dictitem_T *
4044get_funccal_local_var()
4045{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004047 return NULL;
4048 return &get_funccal()->l_vars_var;
4049}
4050
4051/*
4052 * Return the hashtable used for argument in the current funccal.
4053 * Return NULL if there is no current funccal.
4054 */
4055 hashtab_T *
4056get_funccal_args_ht()
4057{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004059 return NULL;
4060 return &get_funccal()->l_avars.dv_hashtab;
4061}
4062
4063/*
4064 * Return the a: scope variable.
4065 * Return NULL if there is no current funccal.
4066 */
4067 dictitem_T *
4068get_funccal_args_var()
4069{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004071 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004072 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004073}
4074
4075/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004076 * List function variables, if there is a function.
4077 */
4078 void
4079list_func_vars(int *first)
4080{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004082 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004083 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004084}
4085
4086/*
4087 * If "ht" is the hashtable for local variables in the current funccal, return
4088 * the dict that contains it.
4089 * Otherwise return NULL.
4090 */
4091 dict_T *
4092get_current_funccal_dict(hashtab_T *ht)
4093{
4094 if (current_funccal != NULL
4095 && ht == &current_funccal->l_vars.dv_hashtab)
4096 return &current_funccal->l_vars;
4097 return NULL;
4098}
4099
4100/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004101 * Search hashitem in parent scope.
4102 */
4103 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004104find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004105{
4106 funccall_T *old_current_funccal = current_funccal;
4107 hashtab_T *ht;
4108 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004109 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004110
4111 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4112 return NULL;
4113
Bram Moolenaare38eab22019-12-05 21:50:01 +01004114 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004115 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004116 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004117 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004118 ht = find_var_ht(name, &varname);
4119 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004120 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004121 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004122 if (!HASHITEM_EMPTY(hi))
4123 {
4124 *pht = ht;
4125 break;
4126 }
4127 }
4128 if (current_funccal == current_funccal->func->uf_scoped)
4129 break;
4130 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004131 }
4132 current_funccal = old_current_funccal;
4133
4134 return hi;
4135}
4136
4137/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004138 * Search variable in parent scope.
4139 */
4140 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004141find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004142{
4143 dictitem_T *v = NULL;
4144 funccall_T *old_current_funccal = current_funccal;
4145 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004146 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004147
4148 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4149 return NULL;
4150
Bram Moolenaare38eab22019-12-05 21:50:01 +01004151 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004152 current_funccal = current_funccal->func->uf_scoped;
4153 while (current_funccal)
4154 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004155 ht = find_var_ht(name, &varname);
4156 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004157 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004158 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004159 if (v != NULL)
4160 break;
4161 }
4162 if (current_funccal == current_funccal->func->uf_scoped)
4163 break;
4164 current_funccal = current_funccal->func->uf_scoped;
4165 }
4166 current_funccal = old_current_funccal;
4167
4168 return v;
4169}
4170
4171/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004172 * Set "copyID + 1" in previous_funccal and callers.
4173 */
4174 int
4175set_ref_in_previous_funccal(int copyID)
4176{
4177 int abort = FALSE;
4178 funccall_T *fc;
4179
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004180 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004181 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004182 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004183 abort = abort
4184 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4185 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004186 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004187 }
4188 return abort;
4189}
4190
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004191 static int
4192set_ref_in_funccal(funccall_T *fc, int copyID)
4193{
4194 int abort = FALSE;
4195
4196 if (fc->fc_copyID != copyID)
4197 {
4198 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004199 abort = abort
4200 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4201 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004202 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004203 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004204 }
4205 return abort;
4206}
4207
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004208/*
4209 * Set "copyID" in all local vars and arguments in the call stack.
4210 */
4211 int
4212set_ref_in_call_stack(int copyID)
4213{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004214 int abort = FALSE;
4215 funccall_T *fc;
4216 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004217
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004218 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004219 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004220
4221 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004222 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
4223 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004224 abort = abort || set_ref_in_funccal(fc, copyID);
4225
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004226 return abort;
4227}
4228
4229/*
4230 * Set "copyID" in all functions available by name.
4231 */
4232 int
4233set_ref_in_functions(int copyID)
4234{
4235 int todo;
4236 hashitem_T *hi = NULL;
4237 int abort = FALSE;
4238 ufunc_T *fp;
4239
4240 todo = (int)func_hashtab.ht_used;
4241 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004242 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004243 if (!HASHITEM_EMPTY(hi))
4244 {
4245 --todo;
4246 fp = HI2UF(hi);
4247 if (!func_name_refcount(fp->uf_name))
4248 abort = abort || set_ref_in_func(NULL, fp, copyID);
4249 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004250 }
4251 return abort;
4252}
4253
4254/*
4255 * Set "copyID" in all function arguments.
4256 */
4257 int
4258set_ref_in_func_args(int copyID)
4259{
4260 int i;
4261 int abort = FALSE;
4262
4263 for (i = 0; i < funcargs.ga_len; ++i)
4264 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
4265 copyID, NULL, NULL);
4266 return abort;
4267}
4268
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004269/*
4270 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004271 * Returns TRUE if setting references failed somehow.
4272 */
4273 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004274set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004275{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004276 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004277 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01004278 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004279 char_u fname_buf[FLEN_FIXED + 1];
4280 char_u *tofree = NULL;
4281 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004282 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004283
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004284 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004285 return FALSE;
4286
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004287 if (fp_in == NULL)
4288 {
4289 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004291 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004292 if (fp != NULL)
4293 {
4294 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004295 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004296 }
4297 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004298 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004299}
4300
Bram Moolenaare38eab22019-12-05 21:50:01 +01004301#endif // FEAT_EVAL