blob: 3c2673dde6d5140364294f0d45d933cda0f5acdb [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 Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaara9b579f2016-07-17 18:29:19 +020028/*
29 * All user-defined functions are found in this hashtable.
30 */
31static hashtab_T func_hashtab;
32
Bram Moolenaare38eab22019-12-05 21:50:01 +010033// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020034static garray_T funcargs = GA_EMPTY;
35
Bram Moolenaar209b8e32019-03-14 13:43:24 +010036// pointer to funccal for currently active function
37static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020038
Bram Moolenaar209b8e32019-03-14 13:43:24 +010039// Pointer to list of previously used funccal, still around because some
40// item in it is still being used.
41static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020042
43static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
44static char *e_funcdict = N_("E717: Dictionary entry already exists");
45static char *e_funcref = N_("E718: Funcref required");
46static char *e_nofunc = N_("E130: Unknown function: %s");
47
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020048static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020049
50 void
51func_init()
52{
53 hash_init(&func_hashtab);
54}
55
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020056/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020057 * Return the function hash table
58 */
59 hashtab_T *
60func_tbl_get(void)
61{
62 return &func_hashtab;
63}
64
65/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010066 * Get one function argument and an optional type: "arg: type".
67 * Return a pointer to after the type.
68 * When something is wrong return "arg".
69 */
70 static char_u *
71one_function_arg(char_u *arg, garray_T *newargs, garray_T *argtypes, int skip)
72{
73 char_u *p = arg;
74
75 while (ASCII_ISALNUM(*p) || *p == '_')
76 ++p;
77 if (arg == p || isdigit(*arg)
78 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
79 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
80 {
81 if (!skip)
82 semsg(_("E125: Illegal argument: %s"), arg);
83 return arg;
84 }
85 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
86 return arg;
87 if (newargs != NULL)
88 {
89 char_u *arg_copy;
90 int c;
91 int i;
92
93 c = *p;
94 *p = NUL;
95 arg_copy = vim_strsave(arg);
96 if (arg_copy == NULL)
97 {
98 *p = c;
99 return arg;
100 }
101
102 // Check for duplicate argument name.
103 for (i = 0; i < newargs->ga_len; ++i)
104 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
105 {
106 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
107 vim_free(arg_copy);
108 return arg;
109 }
110 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
111 newargs->ga_len++;
112
113 *p = c;
114 }
115
116 // get any type from "arg: type"
117 if (argtypes != NULL && ga_grow(argtypes, 1) == OK)
118 {
119 char_u *type = NULL;
120
121 if (*p == ':')
122 {
123 type = skipwhite(p + 1);
124 p = skip_type(type);
125 type = vim_strnsave(type, p - type);
126 }
127 else if (*skipwhite(p) == ':')
128 emsg(_("E1059: No white space allowed before :"));
129 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
130 }
131
132 return p;
133}
134
135/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200136 * Get function arguments.
137 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200139get_function_args(
140 char_u **argp,
141 char_u endchar,
142 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 garray_T *argtypes, // NULL unless using :def
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200144 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200145 garray_T *default_args,
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200146 int skip)
147{
148 int mustend = FALSE;
149 char_u *arg = *argp;
150 char_u *p = arg;
151 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200152 int any_default = FALSE;
153 char_u *expr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200154
155 if (newargs != NULL)
156 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157 if (argtypes != NULL)
158 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200159 if (default_args != NULL)
160 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200161
162 if (varargs != NULL)
163 *varargs = FALSE;
164
165 /*
166 * Isolate the arguments: "arg1, arg2, ...)"
167 */
168 while (*p != endchar)
169 {
170 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
171 {
172 if (varargs != NULL)
173 *varargs = TRUE;
174 p += 3;
175 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100176
177 if (argtypes != NULL)
178 {
179 // ...name: list<type>
180 if (!ASCII_ISALPHA(*p))
181 {
182 emsg(_("E1055: Missing name after ..."));
183 break;
184 }
185
186 arg = p;
187 p = one_function_arg(p, newargs, argtypes, skip);
188 if (p == arg)
189 break;
190 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200191 }
192 else
193 {
194 arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100195 p = one_function_arg(p, newargs, argtypes, skip);
196 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200197 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200198
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200199 if (*skipwhite(p) == '=' && default_args != NULL)
200 {
201 typval_T rettv;
202
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100203 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200204 any_default = TRUE;
205 p = skipwhite(p) + 1;
206 p = skipwhite(p);
207 expr = p;
208 if (eval1(&p, &rettv, FALSE) != FAIL)
209 {
210 if (ga_grow(default_args, 1) == FAIL)
211 goto err_ret;
212
213 // trim trailing whitespace
214 while (p > expr && VIM_ISWHITE(p[-1]))
215 p--;
216 c = *p;
217 *p = NUL;
218 expr = vim_strsave(expr);
219 if (expr == NULL)
220 {
221 *p = c;
222 goto err_ret;
223 }
224 ((char_u **)(default_args->ga_data))
225 [default_args->ga_len] = expr;
226 default_args->ga_len++;
227 *p = c;
228 }
229 else
230 mustend = TRUE;
231 }
232 else if (any_default)
233 {
234 emsg(_("E989: Non-default argument follows default argument"));
235 mustend = TRUE;
236 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200237 if (*p == ',')
238 ++p;
239 else
240 mustend = TRUE;
241 }
242 p = skipwhite(p);
243 if (mustend && *p != endchar)
244 {
245 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100246 semsg(_(e_invarg2), *argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200247 break;
248 }
249 }
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200250 if (*p != endchar)
251 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100252 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200253
254 *argp = p;
255 return OK;
256
257err_ret:
258 if (newargs != NULL)
259 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200260 if (default_args != NULL)
261 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200262 return FAIL;
263}
264
265/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200266 * Register function "fp" as using "current_funccal" as its scope.
267 */
268 static int
269register_closure(ufunc_T *fp)
270{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200271 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100272 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200273 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200274 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200275 fp->uf_scoped = current_funccal;
276 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200277
Bram Moolenaar58016442016-07-31 18:30:22 +0200278 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
279 return FAIL;
280 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
281 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200282 return OK;
283}
284
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100285 static void
286set_ufunc_name(ufunc_T *fp, char_u *name)
287{
288 STRCPY(fp->uf_name, name);
289
290 if (name[0] == K_SPECIAL)
291 {
292 fp->uf_name_exp = alloc(STRLEN(name) + 3);
293 if (fp->uf_name_exp != NULL)
294 {
295 STRCPY(fp->uf_name_exp, "<SNR>");
296 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
297 }
298 }
299}
300
Bram Moolenaar58016442016-07-31 18:30:22 +0200301/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200302 * Parse a lambda expression and get a Funcref from "*arg".
303 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
304 */
305 int
306get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
307{
308 garray_T newargs;
309 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200310 garray_T *pnewargs;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200311 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100312 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200313 int varargs;
314 int ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200315 char_u *start = skipwhite(*arg + 1);
316 char_u *s, *e;
317 static int lambda_no = 0;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200318 int *old_eval_lavars = eval_lavars_used;
319 int eval_lavars = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200320
321 ga_init(&newargs);
322 ga_init(&newlines);
323
Bram Moolenaare38eab22019-12-05 21:50:01 +0100324 // First, check if this is a lambda expression. "->" must exist.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100325 ret = get_function_args(&start, '-', NULL, NULL, NULL, NULL, TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200326 if (ret == FAIL || *start != '>')
327 return NOTDONE;
328
Bram Moolenaare38eab22019-12-05 21:50:01 +0100329 // Parse the arguments again.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200330 if (evaluate)
331 pnewargs = &newargs;
332 else
333 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200334 *arg = skipwhite(*arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100335 // TODO: argument types
336 ret = get_function_args(arg, '-', pnewargs, NULL, &varargs, NULL, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200337 if (ret == FAIL || **arg != '>')
338 goto errret;
339
Bram Moolenaare38eab22019-12-05 21:50:01 +0100340 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200341 if (evaluate)
342 eval_lavars_used = &eval_lavars;
343
Bram Moolenaare38eab22019-12-05 21:50:01 +0100344 // Get the start and the end of the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200345 *arg = skipwhite(*arg + 1);
346 s = *arg;
347 ret = skip_expr(arg);
348 if (ret == FAIL)
349 goto errret;
350 e = *arg;
351 *arg = skipwhite(*arg);
352 if (**arg != '}')
353 goto errret;
354 ++*arg;
355
356 if (evaluate)
357 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200358 int len, flags = 0;
359 char_u *p;
360 char_u name[20];
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200361
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200362 sprintf((char*)name, "<lambda>%d", ++lambda_no);
363
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200364 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200365 if (fp == NULL)
366 goto errret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100367 fp->uf_dfunc_idx = -1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200368 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200369 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200370 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200371
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200372 ga_init2(&newlines, (int)sizeof(char_u *), 1);
373 if (ga_grow(&newlines, 1) == FAIL)
374 goto errret;
375
Bram Moolenaare38eab22019-12-05 21:50:01 +0100376 // Add "return " before the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200377 len = 7 + e - s + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200378 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200379 if (p == NULL)
380 goto errret;
381 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
382 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200383 vim_strncpy(p + 7, s, e - s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200384
385 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100386 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200387 hash_add(&func_hashtab, UF2HIKEY(fp));
388 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200389 ga_init(&fp->uf_def_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200390 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200391 if (current_funccal != NULL && eval_lavars)
392 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200393 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200394 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200395 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200396 }
397 else
398 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200399
400#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200401 if (prof_def_func())
402 func_do_profile(fp);
403#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200404 if (sandbox)
405 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100406 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200407 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200408 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200409 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200410 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100411 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200412
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200413 pt->pt_func = fp;
414 pt->pt_refcount = 1;
415 rettv->vval.v_partial = pt;
416 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200417 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200418
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200419 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200420 return OK;
421
422errret:
423 ga_clear_strings(&newargs);
424 ga_clear_strings(&newlines);
425 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100426 vim_free(pt);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200427 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200428 return FAIL;
429}
430
431/*
432 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
433 * name it contains, otherwise return "name".
434 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
435 * "partialp".
436 */
437 char_u *
438deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
439{
440 dictitem_T *v;
441 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200442 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200443
444 if (partialp != NULL)
445 *partialp = NULL;
446
447 cc = name[*lenp];
448 name[*lenp] = NUL;
449 v = find_var(name, NULL, no_autoload);
450 name[*lenp] = cc;
451 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
452 {
453 if (v->di_tv.vval.v_string == NULL)
454 {
455 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100456 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200457 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200458 s = v->di_tv.vval.v_string;
459 *lenp = (int)STRLEN(s);
460 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200461 }
462
463 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
464 {
465 partial_T *pt = v->di_tv.vval.v_partial;
466
467 if (pt == NULL)
468 {
469 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100470 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200471 }
472 if (partialp != NULL)
473 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200474 s = partial_name(pt);
475 *lenp = (int)STRLEN(s);
476 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200477 }
478
479 return name;
480}
481
482/*
483 * Give an error message with a function name. Handle <SNR> things.
484 * "ermsg" is to be passed without translation, use N_() instead of _().
485 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100486 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200487emsg_funcname(char *ermsg, char_u *name)
488{
489 char_u *p;
490
491 if (*name == K_SPECIAL)
492 p = concat_str((char_u *)"<SNR>", name + 3);
493 else
494 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100495 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200496 if (p != name)
497 vim_free(p);
498}
499
500/*
501 * Allocate a variable for the result of a function.
502 * Return OK or FAIL.
503 */
504 int
505get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200506 char_u *name, // name of the function
507 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200508 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200509 char_u **arg, // argument, pointing to the '('
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200510 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200511{
512 char_u *argp;
513 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100514 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
515 int argcount = 0; // number of arguments found
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200516
517 /*
518 * Get the arguments.
519 */
520 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200521 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
522 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200523 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100524 argp = skipwhite(argp + 1); // skip the '(' or ','
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200525 if (*argp == ')' || *argp == ',' || *argp == NUL)
526 break;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200527 if (eval1(&argp, &argvars[argcount], funcexe->evaluate) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200528 {
529 ret = FAIL;
530 break;
531 }
532 ++argcount;
533 if (*argp != ',')
534 break;
535 }
536 if (*argp == ')')
537 ++argp;
538 else
539 ret = FAIL;
540
541 if (ret == OK)
542 {
543 int i = 0;
544
545 if (get_vim_var_nr(VV_TESTING))
546 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100547 // Prepare for calling test_garbagecollect_now(), need to know
548 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200549 if (funcargs.ga_itemsize == 0)
550 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
551 for (i = 0; i < argcount; ++i)
552 if (ga_grow(&funcargs, 1) == OK)
553 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
554 &argvars[i];
555 }
556
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200557 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200558
559 funcargs.ga_len -= i;
560 }
561 else if (!aborting())
562 {
563 if (argcount == MAX_FUNC_ARGS)
564 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
565 else
566 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
567 }
568
569 while (--argcount >= 0)
570 clear_tv(&argvars[argcount]);
571
572 *arg = skipwhite(argp);
573 return ret;
574}
575
576#define FLEN_FIXED 40
577
578/*
579 * Return TRUE if "p" starts with "<SID>" or "s:".
580 * Only works if eval_fname_script() returned non-zero for "p"!
581 */
582 static int
583eval_fname_sid(char_u *p)
584{
585 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
586}
587
588/*
589 * In a script change <SID>name() and s:name() to K_SNR 123_name().
590 * Change <SNR>123_name() to K_SNR 123_name().
591 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
592 * (slow).
593 */
594 static char_u *
595fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
596{
597 int llen;
598 char_u *fname;
599 int i;
600
601 llen = eval_fname_script(name);
602 if (llen > 0)
603 {
604 fname_buf[0] = K_SPECIAL;
605 fname_buf[1] = KS_EXTRA;
606 fname_buf[2] = (int)KE_SNR;
607 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100608 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200609 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200610 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100611 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200612 else
613 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200614 sprintf((char *)fname_buf + 3, "%ld_",
615 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200616 i = (int)STRLEN(fname_buf);
617 }
618 }
619 if (i + STRLEN(name + llen) < FLEN_FIXED)
620 {
621 STRCPY(fname_buf + i, name + llen);
622 fname = fname_buf;
623 }
624 else
625 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200626 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200627 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100628 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200629 else
630 {
631 *tofree = fname;
632 mch_memmove(fname, fname_buf, (size_t)i);
633 STRCPY(fname + i, name + llen);
634 }
635 }
636 }
637 else
638 fname = name;
639 return fname;
640}
641
642/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 * Find a function "name" in script "sid".
644 */
645 static ufunc_T *
646find_func_with_sid(char_u *name, int sid)
647{
648 hashitem_T *hi;
649 char_u buffer[200];
650
651 buffer[0] = K_SPECIAL;
652 buffer[1] = KS_EXTRA;
653 buffer[2] = (int)KE_SNR;
654 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
655 (long)sid, name);
656 hi = hash_find(&func_hashtab, buffer);
657 if (!HASHITEM_EMPTY(hi))
658 return HI2UF(hi);
659
660 return NULL;
661}
662
663/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200664 * Find a function by name, return pointer to it in ufuncs.
665 * Return NULL for unknown function.
666 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100667 static ufunc_T *
668find_func_even_dead(char_u *name, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200669{
670 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100671 ufunc_T *func;
672 imported_T *imported;
673
674 if (in_vim9script())
675 {
676 // Find script-local function before global one.
677 func = find_func_with_sid(name, current_sctx.sc_sid);
678 if (func != NULL)
679 return func;
680
681 // Find imported funcion before global one.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +0100682 imported = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 if (imported != NULL && imported->imp_funcname != NULL)
684 {
685 hi = hash_find(&func_hashtab, imported->imp_funcname);
686 if (!HASHITEM_EMPTY(hi))
687 return HI2UF(hi);
688 }
689 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200690
691 hi = hash_find(&func_hashtab, name);
692 if (!HASHITEM_EMPTY(hi))
693 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694
695 return NULL;
696}
697
698/*
699 * Find a function by name, return pointer to it in ufuncs.
700 * "cctx" is passed in a :def function to find imported functions.
701 * Return NULL for unknown or dead function.
702 */
703 ufunc_T *
704find_func(char_u *name, cctx_T *cctx)
705{
706 ufunc_T *fp = find_func_even_dead(name, cctx);
707
708 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
709 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200710 return NULL;
711}
712
713/*
714 * Copy the function name of "fp" to buffer "buf".
715 * "buf" must be able to hold the function name plus three bytes.
716 * Takes care of script-local function names.
717 */
718 static void
719cat_func_name(char_u *buf, ufunc_T *fp)
720{
721 if (fp->uf_name[0] == K_SPECIAL)
722 {
723 STRCPY(buf, "<SNR>");
724 STRCAT(buf, fp->uf_name + 3);
725 }
726 else
727 STRCPY(buf, fp->uf_name);
728}
729
730/*
731 * Add a number variable "name" to dict "dp" with value "nr".
732 */
733 static void
734add_nr_var(
735 dict_T *dp,
736 dictitem_T *v,
737 char *name,
738 varnumber_T nr)
739{
740 STRCPY(v->di_key, name);
741 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
742 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
743 v->di_tv.v_type = VAR_NUMBER;
744 v->di_tv.v_lock = VAR_FIXED;
745 v->di_tv.vval.v_number = nr;
746}
747
748/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100749 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200750 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100751 static void
752free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200753{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100754 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200755
756 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
757 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100758 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200759
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100760 // When garbage collecting a funccall_T may be freed before the
761 // function that references it, clear its uf_scoped field.
762 // The function may have been redefined and point to another
763 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200764 if (fp != NULL && fp->uf_scoped == fc)
765 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200766 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200767 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200768
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200769 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200770 vim_free(fc);
771}
772
773/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100774 * Free "fc" and what it contains.
775 * Can be called only when "fc" is kept beyond the period of it called,
776 * i.e. after cleanup_function_call(fc).
777 */
778 static void
779free_funccal_contents(funccall_T *fc)
780{
781 listitem_T *li;
782
783 // Free all l: variables.
784 vars_clear(&fc->l_vars.dv_hashtab);
785
786 // Free all a: variables.
787 vars_clear(&fc->l_avars.dv_hashtab);
788
789 // Free the a:000 variables.
790 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
791 clear_tv(&li->li_tv);
792
793 free_funccal(fc);
794}
795
796/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200797 * Handle the last part of returning from a function: free the local hashtable.
798 * Unless it is still in use by a closure.
799 */
800 static void
801cleanup_function_call(funccall_T *fc)
802{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100803 int may_free_fc = fc->fc_refcount <= 0;
804 int free_fc = TRUE;
805
Bram Moolenaar6914c642017-04-01 21:21:30 +0200806 current_funccal = fc->caller;
807
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100808 // Free all l: variables if not referred.
809 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
810 vars_clear(&fc->l_vars.dv_hashtab);
811 else
812 free_fc = FALSE;
813
814 // If the a:000 list and the l: and a: dicts are not referenced and
815 // there is no closure using it, we can free the funccall_T and what's
816 // in it.
817 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
818 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200819 else
820 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100821 int todo;
822 hashitem_T *hi;
823 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200824
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100825 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200826
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100827 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200828 todo = (int)fc->l_avars.dv_hashtab.ht_used;
829 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
830 {
831 if (!HASHITEM_EMPTY(hi))
832 {
833 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100834 di = HI2DI(hi);
835 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200836 }
837 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100838 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200839
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100840 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
841 fc->l_varlist.lv_first = NULL;
842 else
843 {
844 listitem_T *li;
845
846 free_fc = FALSE;
847
848 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200849 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
850 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100851 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100852
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100853 if (free_fc)
854 free_funccal(fc);
855 else
856 {
857 static int made_copy = 0;
858
859 // "fc" is still in use. This can happen when returning "a:000",
860 // assigning "l:" to a global variable or defining a closure.
861 // Link "fc" in the list for garbage collection later.
862 fc->caller = previous_funccal;
863 previous_funccal = fc;
864
865 if (want_garbage_collect)
866 // If garbage collector is ready, clear count.
867 made_copy = 0;
868 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100869 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100870 // We have made a lot of copies, worth 4 Mbyte. This can happen
871 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100872 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100873 // too much memory.
874 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100875 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100876 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200877 }
878}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100879/*
880 * Unreference "fc": decrement the reference count and free it when it
881 * becomes zero. "fp" is detached from "fc".
882 * When "force" is TRUE we are exiting.
883 */
884 static void
885funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
886{
887 funccall_T **pfc;
888 int i;
889
890 if (fc == NULL)
891 return;
892
893 if (--fc->fc_refcount <= 0 && (force || (
894 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
895 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
896 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
897 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
898 {
899 if (fc == *pfc)
900 {
901 *pfc = fc->caller;
902 free_funccal_contents(fc);
903 return;
904 }
905 }
906 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
907 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
908 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
909}
910
911/*
912 * Remove the function from the function hashtable. If the function was
913 * deleted while it still has references this was already done.
914 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
915 */
916 static int
917func_remove(ufunc_T *fp)
918{
919 hashitem_T *hi;
920
921 // Return if it was already virtually deleted.
922 if (fp->uf_flags & FC_DEAD)
923 return FALSE;
924
925 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
926 if (!HASHITEM_EMPTY(hi))
927 {
928 // When there is a def-function index do not actually remove the
929 // function, so we can find the index when defining the function again.
930 if (fp->uf_dfunc_idx >= 0)
931 fp->uf_flags |= FC_DEAD;
932 else
933 hash_remove(&func_hashtab, hi);
934 return TRUE;
935 }
936 return FALSE;
937}
938
939 static void
940func_clear_items(ufunc_T *fp)
941{
942 ga_clear_strings(&(fp->uf_args));
943 ga_clear_strings(&(fp->uf_def_args));
944 ga_clear_strings(&(fp->uf_lines));
945 VIM_CLEAR(fp->uf_name_exp);
946 VIM_CLEAR(fp->uf_arg_types);
947 ga_clear(&fp->uf_type_list);
948#ifdef FEAT_PROFILE
949 VIM_CLEAR(fp->uf_tml_count);
950 VIM_CLEAR(fp->uf_tml_total);
951 VIM_CLEAR(fp->uf_tml_self);
952#endif
953}
954
955/*
956 * Free all things that a function contains. Does not free the function
957 * itself, use func_free() for that.
958 * When "force" is TRUE we are exiting.
959 */
960 static void
961func_clear(ufunc_T *fp, int force)
962{
963 if (fp->uf_cleared)
964 return;
965 fp->uf_cleared = TRUE;
966
967 // clear this function
968 func_clear_items(fp);
969 funccal_unref(fp->uf_scoped, fp, force);
970 delete_def_function(fp);
971}
972
973/*
974 * Free a function and remove it from the list of functions. Does not free
975 * what a function contains, call func_clear() first.
976 */
977 static void
978func_free(ufunc_T *fp)
979{
980 // Only remove it when not done already, otherwise we would remove a newer
981 // version of the function with the same name.
982 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
983 func_remove(fp);
984
985 if ((fp->uf_flags & FC_DEAD) == 0)
986 vim_free(fp);
987}
988
989/*
990 * Free all things that a function contains and free the function itself.
991 * When "force" is TRUE we are exiting.
992 */
993 static void
994func_clear_free(ufunc_T *fp, int force)
995{
996 func_clear(fp, force);
997 func_free(fp);
998}
999
Bram Moolenaar6914c642017-04-01 21:21:30 +02001000
1001/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001002 * Call a user function.
1003 */
1004 static void
1005call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001006 ufunc_T *fp, // pointer to function
1007 int argcount, // nr of args
1008 typval_T *argvars, // arguments
1009 typval_T *rettv, // return value
1010 linenr_T firstline, // first line of range
1011 linenr_T lastline, // last line of range
1012 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001013{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001014 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001015 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001016 funccall_T *fc;
1017 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001018 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001019 static int depth = 0;
1020 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001021 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001022 int i;
1023 int ai;
1024 int islambda = FALSE;
1025 char_u numbuf[NUMBUFLEN];
1026 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001027#ifdef FEAT_PROFILE
1028 proftime_T wait_start;
1029 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001030 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001031#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001032 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001033
Bram Moolenaare38eab22019-12-05 21:50:01 +01001034 // If depth of calling is getting too high, don't execute the function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001035 if (depth >= p_mfd)
1036 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001037 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001038 rettv->v_type = VAR_NUMBER;
1039 rettv->vval.v_number = -1;
1040 return;
1041 }
1042 ++depth;
1043
Bram Moolenaare38eab22019-12-05 21:50:01 +01001044 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001045
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001046 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001047 if (fc == NULL)
1048 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001049 fc->caller = current_funccal;
1050 current_funccal = fc;
1051 fc->func = fp;
1052 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001053 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001054 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001055 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1056 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001057 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001058 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001059 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001060
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001061 if (fp->uf_dfunc_idx >= 0)
1062 {
1063 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001064 save_current_sctx = current_sctx;
1065 current_sctx = fp->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066
1067 // Execute the compiled function.
1068 call_def_function(fp, argcount, argvars, rettv);
1069 --depth;
1070 current_funccal = fc->caller;
1071
1072 estack_pop();
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001073 current_sctx = save_current_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074 free_funccal(fc);
1075 return;
1076 }
1077
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001078 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1079 islambda = TRUE;
1080
1081 /*
1082 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1083 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1084 * each argument variable and saves a lot of time.
1085 */
1086 /*
1087 * Init l: variables.
1088 */
1089 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1090 if (selfdict != NULL)
1091 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001092 // Set l:self to "selfdict". Use "name" to avoid a warning from
1093 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001094 v = &fc->fixvar[fixvar_idx++].var;
1095 name = v->di_key;
1096 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001097 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001098 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1099 v->di_tv.v_type = VAR_DICT;
1100 v->di_tv.v_lock = 0;
1101 v->di_tv.vval.v_dict = selfdict;
1102 ++selfdict->dv_refcount;
1103 }
1104
1105 /*
1106 * Init a: variables.
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001107 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001108 * Set a:000 to a list with room for the "..." arguments.
1109 */
1110 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
1111 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001112 (varnumber_T)(argcount >= fp->uf_args.ga_len
1113 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001114 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001115 // Use "name" to avoid a warning from some compiler that checks the
1116 // destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001117 v = &fc->fixvar[fixvar_idx++].var;
1118 name = v->di_key;
1119 STRCPY(name, "000");
1120 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1121 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1122 v->di_tv.v_type = VAR_LIST;
1123 v->di_tv.v_lock = VAR_FIXED;
1124 v->di_tv.vval.v_list = &fc->l_varlist;
1125 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
1126 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1127 fc->l_varlist.lv_lock = VAR_FIXED;
1128
1129 /*
1130 * Set a:firstline to "firstline" and a:lastline to "lastline".
1131 * Set a:name to named arguments.
1132 * Set a:N to the "..." arguments.
1133 */
1134 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
1135 (varnumber_T)firstline);
1136 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
1137 (varnumber_T)lastline);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001138 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001139 {
1140 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001141 typval_T def_rettv;
1142 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001143
1144 ai = i - fp->uf_args.ga_len;
1145 if (ai < 0)
1146 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001147 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001148 name = FUNCARG(fp, i);
1149 if (islambda)
1150 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001151
1152 // evaluate named argument default expression
1153 isdefault = ai + fp->uf_def_args.ga_len >= 0
1154 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1155 && argvars[i].vval.v_number == VVAL_NONE));
1156 if (isdefault)
1157 {
1158 char_u *default_expr = NULL;
1159 def_rettv.v_type = VAR_NUMBER;
1160 def_rettv.vval.v_number = -1;
1161
1162 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1163 [ai + fp->uf_def_args.ga_len];
1164 if (eval1(&default_expr, &def_rettv, TRUE) == FAIL)
1165 {
1166 default_arg_err = 1;
1167 break;
1168 }
1169 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001170 }
1171 else
1172 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001173 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001174 sprintf((char *)numbuf, "%d", ai + 1);
1175 name = numbuf;
1176 }
1177 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1178 {
1179 v = &fc->fixvar[fixvar_idx++].var;
1180 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001181 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001182 }
1183 else
1184 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001185 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001186 if (v == NULL)
1187 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001188 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001189 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001190
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001191 // Note: the values are copied directly to avoid alloc/free.
1192 // "argvars" must have VAR_FIXED for v_lock.
1193 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001194 v->di_tv.v_lock = VAR_FIXED;
1195
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001196 if (addlocal)
1197 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001198 // Named arguments should be accessed without the "a:" prefix in
1199 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001200 copy_tv(&v->di_tv, &v->di_tv);
1201 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001202 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001203 else
1204 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001205
1206 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1207 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001208 listitem_T *li = &fc->l_listitems[ai];
1209
1210 li->li_tv = argvars[i];
1211 li->li_tv.v_lock = VAR_FIXED;
1212 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001213 }
1214 }
1215
Bram Moolenaare38eab22019-12-05 21:50:01 +01001216 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001217 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001218
1219 if (fp->uf_flags & FC_SANDBOX)
1220 {
1221 using_sandbox = TRUE;
1222 ++sandbox;
1223 }
1224
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001225 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001226 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001227 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001228 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001229 ++no_wait_return;
1230 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001231
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001232 smsg(_("calling %s"), SOURCING_NAME);
1233 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001234 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001235 char_u buf[MSG_BUF_LEN];
1236 char_u numbuf2[NUMBUFLEN];
1237 char_u *tofree;
1238 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001239
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001240 msg_puts("(");
1241 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001242 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001243 if (i > 0)
1244 msg_puts(", ");
1245 if (argvars[i].v_type == VAR_NUMBER)
1246 msg_outnum((long)argvars[i].vval.v_number);
1247 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001248 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001249 // Do not want errors such as E724 here.
1250 ++emsg_off;
1251 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1252 --emsg_off;
1253 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001254 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001255 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001256 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001257 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1258 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001259 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001260 msg_puts((char *)s);
1261 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001262 }
1263 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001264 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001265 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001266 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001267 msg_puts("\n"); // don't overwrite this either
1268
1269 verbose_leave_scroll();
1270 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001271 }
1272#ifdef FEAT_PROFILE
1273 if (do_profiling == PROF_YES)
1274 {
1275 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001276 {
1277 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001278 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001279 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001280 if (fp->uf_profiling
1281 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1282 {
1283 ++fp->uf_tm_count;
1284 profile_start(&call_start);
1285 profile_zero(&fp->uf_tm_children);
1286 }
1287 script_prof_save(&wait_start);
1288 }
1289#endif
1290
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001291 save_current_sctx = current_sctx;
1292 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001293 save_did_emsg = did_emsg;
1294 did_emsg = FALSE;
1295
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001296 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1297 did_emsg = TRUE;
1298 else
1299 // call do_cmdline() to execute the lines
1300 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001301 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1302
1303 --RedrawingDisabled;
1304
Bram Moolenaare38eab22019-12-05 21:50:01 +01001305 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001306 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1307 {
1308 clear_tv(rettv);
1309 rettv->v_type = VAR_NUMBER;
1310 rettv->vval.v_number = -1;
1311 }
1312
1313#ifdef FEAT_PROFILE
1314 if (do_profiling == PROF_YES && (fp->uf_profiling
1315 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1316 {
1317 profile_end(&call_start);
1318 profile_sub_wait(&wait_start, &call_start);
1319 profile_add(&fp->uf_tm_total, &call_start);
1320 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1321 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1322 {
1323 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1324 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1325 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001326 if (started_profiling)
1327 // make a ":profdel func" stop profiling the function
1328 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001329 }
1330#endif
1331
Bram Moolenaare38eab22019-12-05 21:50:01 +01001332 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001333 if (p_verbose >= 12)
1334 {
1335 ++no_wait_return;
1336 verbose_enter_scroll();
1337
1338 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001339 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001340 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001341 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001342 (long)fc->rettv->vval.v_number);
1343 else
1344 {
1345 char_u buf[MSG_BUF_LEN];
1346 char_u numbuf2[NUMBUFLEN];
1347 char_u *tofree;
1348 char_u *s;
1349
Bram Moolenaare38eab22019-12-05 21:50:01 +01001350 // The value may be very long. Skip the middle part, so that we
1351 // have some idea how it starts and ends. smsg() would always
1352 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001353 ++emsg_off;
1354 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1355 --emsg_off;
1356 if (s != NULL)
1357 {
1358 if (vim_strsize(s) > MSG_BUF_CLEN)
1359 {
1360 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1361 s = buf;
1362 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001363 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001364 vim_free(tofree);
1365 }
1366 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001367 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001368
1369 verbose_leave_scroll();
1370 --no_wait_return;
1371 }
1372
Bram Moolenaare31ee862020-01-07 20:59:34 +01001373 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001374 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001375 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001376#ifdef FEAT_PROFILE
1377 if (do_profiling == PROF_YES)
1378 script_prof_restore(&wait_start);
1379#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001380 if (using_sandbox)
1381 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001382
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001383 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001384 {
1385 ++no_wait_return;
1386 verbose_enter_scroll();
1387
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001388 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001389 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001390
1391 verbose_leave_scroll();
1392 --no_wait_return;
1393 }
1394
1395 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001396 --depth;
1397
Bram Moolenaar6914c642017-04-01 21:21:30 +02001398 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001399}
1400
1401/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001403 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404 int
1405call_user_func_check(
1406 ufunc_T *fp,
1407 int argcount,
1408 typval_T *argvars,
1409 typval_T *rettv,
1410 funcexe_T *funcexe,
1411 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001412{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 int error;
1414 int regular_args = fp->uf_args.ga_len;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1417 *funcexe->doesrange = TRUE;
1418 if (argcount < regular_args - fp->uf_def_args.ga_len)
1419 error = FCERR_TOOFEW;
1420 else if (!has_varargs(fp) && argcount > regular_args)
1421 error = FCERR_TOOMANY;
1422 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1423 error = FCERR_DICT;
1424 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001425 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001426 int did_save_redo = FALSE;
1427 save_redo_T save_redo;
1428
1429 /*
1430 * Call the user function.
1431 * Save and restore search patterns, script variables and
1432 * redo buffer.
1433 */
1434 save_search_patterns();
1435 if (!ins_compl_active())
1436 {
1437 saveRedobuff(&save_redo);
1438 did_save_redo = TRUE;
1439 }
1440 ++fp->uf_calls;
1441 call_user_func(fp, argcount, argvars, rettv,
1442 funcexe->firstline, funcexe->lastline,
1443 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
1444 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1445 // Function was unreferenced while being used, free it now.
1446 func_clear_free(fp, FALSE);
1447 if (did_save_redo)
1448 restoreRedobuff(&save_redo);
1449 restore_search_patterns();
1450 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001451 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001452 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001453}
1454
1455/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001456 * There are two kinds of function names:
1457 * 1. ordinary names, function defined with :function
1458 * 2. numbered functions and lambdas
1459 * For the first we only count the name stored in func_hashtab as a reference,
1460 * using function() does not count as a reference, because the function is
1461 * looked up by name.
1462 */
1463 static int
1464func_name_refcount(char_u *name)
1465{
1466 return isdigit(*name) || *name == '<';
1467}
1468
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001469static funccal_entry_T *funccal_stack = NULL;
1470
1471/*
1472 * Save the current function call pointer, and set it to NULL.
1473 * Used when executing autocommands and for ":source".
1474 */
1475 void
1476save_funccal(funccal_entry_T *entry)
1477{
1478 entry->top_funccal = current_funccal;
1479 entry->next = funccal_stack;
1480 funccal_stack = entry;
1481 current_funccal = NULL;
1482}
1483
1484 void
1485restore_funccal(void)
1486{
1487 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001488 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001489 else
1490 {
1491 current_funccal = funccal_stack->top_funccal;
1492 funccal_stack = funccal_stack->next;
1493 }
1494}
1495
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001496 funccall_T *
1497get_current_funccal(void)
1498{
1499 return current_funccal;
1500}
1501
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001502#if defined(EXITFREE) || defined(PROTO)
1503 void
1504free_all_functions(void)
1505{
1506 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001507 ufunc_T *fp;
1508 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001509 long_u todo = 1;
1510 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001511
Bram Moolenaare38eab22019-12-05 21:50:01 +01001512 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001513 while (current_funccal != NULL)
1514 {
1515 clear_tv(current_funccal->rettv);
1516 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001517 if (current_funccal == NULL && funccal_stack != NULL)
1518 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001519 }
1520
Bram Moolenaare38eab22019-12-05 21:50:01 +01001521 // First clear what the functions contain. Since this may lower the
1522 // reference count of a function, it may also free a function and change
1523 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001524 while (todo > 0)
1525 {
1526 todo = func_hashtab.ht_used;
1527 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1528 if (!HASHITEM_EMPTY(hi))
1529 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 // clear the def function index now
1531 fp = HI2UF(hi);
1532 fp->uf_flags &= ~FC_DEAD;
1533 fp->uf_dfunc_idx = -1;
1534
Bram Moolenaare38eab22019-12-05 21:50:01 +01001535 // Only free functions that are not refcounted, those are
1536 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001537 if (func_name_refcount(fp->uf_name))
1538 ++skipped;
1539 else
1540 {
1541 used = func_hashtab.ht_used;
1542 func_clear(fp, TRUE);
1543 if (used != func_hashtab.ht_used)
1544 {
1545 skipped = 0;
1546 break;
1547 }
1548 }
1549 --todo;
1550 }
1551 }
1552
Bram Moolenaare38eab22019-12-05 21:50:01 +01001553 // Now actually free the functions. Need to start all over every time,
1554 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001555 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001556 while (func_hashtab.ht_used > skipped)
1557 {
1558 todo = func_hashtab.ht_used;
1559 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001560 if (!HASHITEM_EMPTY(hi))
1561 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001562 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001563 // Only free functions that are not refcounted, those are
1564 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02001565 fp = HI2UF(hi);
1566 if (func_name_refcount(fp->uf_name))
1567 ++skipped;
1568 else
1569 {
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001570 func_free(fp);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001571 skipped = 0;
1572 break;
1573 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001574 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001575 }
1576 if (skipped == 0)
1577 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578
1579 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001580}
1581#endif
1582
1583/*
1584 * Return TRUE if "name" looks like a builtin function name: starts with a
1585 * lower case letter and doesn't contain AUTOLOAD_CHAR.
1586 * "len" is the length of "name", or -1 for NUL terminated.
1587 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001589builtin_function(char_u *name, int len)
1590{
1591 char_u *p;
1592
1593 if (!ASCII_ISLOWER(name[0]))
1594 return FALSE;
1595 p = vim_strchr(name, AUTOLOAD_CHAR);
1596 return p == NULL || (len > 0 && p > name + len);
1597}
1598
1599 int
1600func_call(
1601 char_u *name,
1602 typval_T *args,
1603 partial_T *partial,
1604 dict_T *selfdict,
1605 typval_T *rettv)
1606{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001607 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001608 listitem_T *item;
1609 typval_T argv[MAX_FUNC_ARGS + 1];
1610 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001611 int r = 0;
1612
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001613 range_list_materialize(l);
1614 for (item = l->lv_first; item != NULL; item = item->li_next)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001615 {
1616 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1617 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001618 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001619 break;
1620 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001621 // Make a copy of each argument. This is needed to be able to set
1622 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001623 copy_tv(&item->li_tv, &argv[argc++]);
1624 }
1625
1626 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001627 {
1628 funcexe_T funcexe;
1629
Bram Moolenaarac92e252019-08-03 21:58:38 +02001630 vim_memset(&funcexe, 0, sizeof(funcexe));
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001631 funcexe.firstline = curwin->w_cursor.lnum;
1632 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001633 funcexe.evaluate = TRUE;
1634 funcexe.partial = partial;
1635 funcexe.selfdict = selfdict;
1636 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1637 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001638
Bram Moolenaare38eab22019-12-05 21:50:01 +01001639 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001640 while (argc > 0)
1641 clear_tv(&argv[--argc]);
1642
1643 return r;
1644}
1645
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001646static int callback_depth = 0;
1647
1648 int
1649get_callback_depth(void)
1650{
1651 return callback_depth;
1652}
1653
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001654/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001655 * Invoke call_func() with a callback.
1656 */
1657 int
1658call_callback(
1659 callback_T *callback,
1660 int len, // length of "name" or -1 to use strlen()
1661 typval_T *rettv, // return value goes here
1662 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001663 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001664 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001665{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001666 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001667 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001668
1669 vim_memset(&funcexe, 0, sizeof(funcexe));
1670 funcexe.evaluate = TRUE;
1671 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001672 ++callback_depth;
1673 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1674 --callback_depth;
1675 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001676}
1677
1678/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 * Give an error message for the result of a function.
1680 * Nothing if "error" is FCERR_NONE.
1681 */
1682 void
1683user_func_error(int error, char_u *name)
1684{
1685 switch (error)
1686 {
1687 case FCERR_UNKNOWN:
1688 emsg_funcname(e_unknownfunc, name);
1689 break;
1690 case FCERR_NOTMETHOD:
1691 emsg_funcname(
1692 N_("E276: Cannot use function as a method: %s"), name);
1693 break;
1694 case FCERR_DELETED:
1695 emsg_funcname(N_(e_func_deleted), name);
1696 break;
1697 case FCERR_TOOMANY:
1698 emsg_funcname((char *)e_toomanyarg, name);
1699 break;
1700 case FCERR_TOOFEW:
1701 emsg_funcname((char *)e_toofewarg, name);
1702 break;
1703 case FCERR_SCRIPT:
1704 emsg_funcname(
1705 N_("E120: Using <SID> not in a script context: %s"), name);
1706 break;
1707 case FCERR_DICT:
1708 emsg_funcname(
1709 N_("E725: Calling dict function without Dictionary: %s"),
1710 name);
1711 break;
1712 }
1713}
1714
1715/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001716 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001717 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001718 * Return FAIL when the function can't be called, OK otherwise.
1719 * Also returns OK when an error was encountered while executing the function.
1720 */
1721 int
1722call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001723 char_u *funcname, // name of the function
1724 int len, // length of "name" or -1 to use strlen()
1725 typval_T *rettv, // return value goes here
1726 int argcount_in, // number of "argvars"
1727 typval_T *argvars_in, // vars for arguments, must have "argcount"
1728 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001729 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001730{
1731 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01001732 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001733 int i;
1734 ufunc_T *fp;
1735 char_u fname_buf[FLEN_FIXED + 1];
1736 char_u *tofree = NULL;
1737 char_u *fname;
1738 char_u *name;
1739 int argcount = argcount_in;
1740 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001741 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001742 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1743 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001744 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001745 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001746 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001747
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001748 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1749 // even when call_func() returns FAIL.
1750 rettv->v_type = VAR_UNKNOWN;
1751
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001752 // Make a copy of the name, if it comes from a funcref variable it could
1753 // be changed or deleted in the called function.
1754 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001755 if (name == NULL)
1756 return ret;
1757
1758 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1759
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001760 if (funcexe->doesrange != NULL)
1761 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001762
1763 if (partial != NULL)
1764 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001765 // When the function has a partial with a dict and there is a dict
1766 // argument, use the dict argument. That is backwards compatible.
1767 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001768 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001769 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01001770 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001771 {
1772 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001773 {
1774 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
1775 {
Bram Moolenaaref140542019-12-31 21:27:13 +01001776 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001777 goto theend;
1778 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001779 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001780 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001781 for (i = 0; i < argcount_in; ++i)
1782 argv[i + argv_clear] = argvars_in[i];
1783 argvars = argv;
1784 argcount = partial->pt_argc + argcount_in;
1785 }
1786 }
1787
Bram Moolenaaref140542019-12-31 21:27:13 +01001788 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001789 {
1790 char_u *rfname = fname;
1791
Bram Moolenaare38eab22019-12-05 21:50:01 +01001792 // Ignore "g:" before a function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001793 if (fname[0] == 'g' && fname[1] == ':')
1794 rfname = fname + 2;
1795
Bram Moolenaare38eab22019-12-05 21:50:01 +01001796 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001797 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01001798 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001799
1800 if (!builtin_function(rfname, -1))
1801 {
1802 /*
1803 * User defined function.
1804 */
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001805 if (partial != NULL && partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001806 fp = partial->pt_func;
1807 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001809
Bram Moolenaare38eab22019-12-05 21:50:01 +01001810 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001811 if (fp == NULL
1812 && apply_autocmds(EVENT_FUNCUNDEFINED,
1813 rfname, rfname, TRUE, NULL)
1814 && !aborting())
1815 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001816 // executed an autocommand, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001818 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001819 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001820 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1821 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001822 // loaded a package, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001823 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001824 }
1825
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001826 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01001827 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001828 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001829 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001830 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01001831 // postponed filling in the arguments, do it now
1832 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001833 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001834
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001835 if (funcexe->basetv != NULL)
1836 {
1837 // Method call: base->Method()
1838 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
1839 argv[0] = *funcexe->basetv;
1840 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001841 argvars = argv;
1842 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001843 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001844
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845 error = call_user_func_check(fp, argcount, argvars, rettv,
1846 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001847 }
1848 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001849 else if (funcexe->basetv != NULL)
1850 {
1851 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001852 * expr->method(): Find the method name in the table, call its
1853 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02001854 */
1855 error = call_internal_method(fname, argcount, argvars, rettv,
1856 funcexe->basetv);
1857 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001858 else
1859 {
1860 /*
1861 * Find the function name in the table, call its implementation.
1862 */
1863 error = call_internal_func(fname, argcount, argvars, rettv);
1864 }
1865 /*
1866 * The function call (or "FuncUndefined" autocommand sequence) might
1867 * have been aborted by an error, an interrupt, or an explicitly thrown
1868 * exception that has not been caught so far. This situation can be
1869 * tested for by calling aborting(). For an error in an internal
1870 * function or for the "E132" error in call_user_func(), however, the
1871 * throw point at which the "force_abort" flag (temporarily reset by
1872 * emsg()) is normally updated has not been reached yet. We need to
1873 * update that flag first to make aborting() reliable.
1874 */
1875 update_force_abort();
1876 }
Bram Moolenaaref140542019-12-31 21:27:13 +01001877 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001878 ret = OK;
1879
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001880theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001881 /*
1882 * Report an error unless the argument evaluation or function call has been
1883 * cancelled due to an aborting error, an interrupt, or an exception.
1884 */
1885 if (!aborting())
1886 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 user_func_error(error, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001888 }
1889
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001890 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001891 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001892 clear_tv(&argv[--argv_clear + argv_base]);
1893
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001894 vim_free(tofree);
1895 vim_free(name);
1896
1897 return ret;
1898}
1899
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900 static char_u *
1901printable_func_name(ufunc_T *fp)
1902{
1903 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
1904}
1905
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001906/*
1907 * List the head of the function: "name(arg1, arg2)".
1908 */
1909 static void
1910list_func_head(ufunc_T *fp, int indent)
1911{
1912 int j;
1913
1914 msg_start();
1915 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001916 msg_puts(" ");
1917 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001919 msg_putchar('(');
1920 for (j = 0; j < fp->uf_args.ga_len; ++j)
1921 {
1922 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001923 msg_puts(", ");
1924 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 if (fp->uf_arg_types != NULL)
1926 {
1927 char *tofree;
1928
1929 msg_puts(": ");
1930 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
1931 vim_free(tofree);
1932 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001933 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
1934 {
1935 msg_puts(" = ");
1936 msg_puts(((char **)(fp->uf_def_args.ga_data))
1937 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
1938 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001939 }
1940 if (fp->uf_varargs)
1941 {
1942 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001943 msg_puts(", ");
1944 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001945 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946 if (fp->uf_va_name != NULL)
1947 {
1948 if (j)
1949 msg_puts(", ");
1950 msg_puts("...");
1951 msg_puts((char *)fp->uf_va_name);
1952 if (fp->uf_va_type)
1953 {
1954 char *tofree;
1955
1956 msg_puts(": ");
1957 msg_puts(type_name(fp->uf_va_type, &tofree));
1958 vim_free(tofree);
1959 }
1960 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001961 msg_putchar(')');
1962 if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001963 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001964 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001965 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001966 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001967 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001968 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001969 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001970 msg_clr_eos();
1971 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001972 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001973}
1974
1975/*
1976 * Get a function name, translating "<SID>" and "<SNR>".
1977 * Also handles a Funcref in a List or Dictionary.
1978 * Returns the function name in allocated memory, or NULL for failure.
1979 * flags:
1980 * TFN_INT: internal function name OK
1981 * TFN_QUIET: be quiet
1982 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001983 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001984 * Advances "pp" to just after the function name (if no error).
1985 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001986 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001987trans_function_name(
1988 char_u **pp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01001989 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001990 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01001991 funcdict_T *fdp, // return: info about dictionary used
1992 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001993{
1994 char_u *name = NULL;
1995 char_u *start;
1996 char_u *end;
1997 int lead;
1998 char_u sid_buf[20];
1999 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002001 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002003
2004 if (fdp != NULL)
2005 vim_memset(fdp, 0, sizeof(funcdict_T));
2006 start = *pp;
2007
Bram Moolenaare38eab22019-12-05 21:50:01 +01002008 // Check for hard coded <SNR>: already translated function ID (from a user
2009 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002010 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2011 && (*pp)[2] == (int)KE_SNR)
2012 {
2013 *pp += 3;
2014 len = get_id_len(pp) + 3;
2015 return vim_strnsave(start, len);
2016 }
2017
Bram Moolenaare38eab22019-12-05 21:50:01 +01002018 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2019 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002020 lead = eval_fname_script(start);
2021 if (lead > 2)
2022 start += lead;
2023
Bram Moolenaare38eab22019-12-05 21:50:01 +01002024 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002025 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002026 lead > 2 ? 0 : FNE_CHECK_START);
2027 if (end == start)
2028 {
2029 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002030 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002031 goto theend;
2032 }
2033 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2034 {
2035 /*
2036 * Report an invalid expression in braces, unless the expression
2037 * evaluation has been cancelled due to an aborting error, an
2038 * interrupt, or an exception.
2039 */
2040 if (!aborting())
2041 {
2042 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002043 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002044 }
2045 else
2046 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2047 goto theend;
2048 }
2049
2050 if (lv.ll_tv != NULL)
2051 {
2052 if (fdp != NULL)
2053 {
2054 fdp->fd_dict = lv.ll_dict;
2055 fdp->fd_newkey = lv.ll_newkey;
2056 lv.ll_newkey = NULL;
2057 fdp->fd_di = lv.ll_di;
2058 }
2059 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2060 {
2061 name = vim_strsave(lv.ll_tv->vval.v_string);
2062 *pp = end;
2063 }
2064 else if (lv.ll_tv->v_type == VAR_PARTIAL
2065 && lv.ll_tv->vval.v_partial != NULL)
2066 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002067 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002068 *pp = end;
2069 if (partial != NULL)
2070 *partial = lv.ll_tv->vval.v_partial;
2071 }
2072 else
2073 {
2074 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2075 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002076 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002077 else
2078 *pp = end;
2079 name = NULL;
2080 }
2081 goto theend;
2082 }
2083
2084 if (lv.ll_name == NULL)
2085 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002086 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002087 *pp = end;
2088 goto theend;
2089 }
2090
Bram Moolenaare38eab22019-12-05 21:50:01 +01002091 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002092 if (lv.ll_exp_name != NULL)
2093 {
2094 len = (int)STRLEN(lv.ll_exp_name);
2095 name = deref_func_name(lv.ll_exp_name, &len, partial,
2096 flags & TFN_NO_AUTOLOAD);
2097 if (name == lv.ll_exp_name)
2098 name = NULL;
2099 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002100 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002101 {
2102 len = (int)(end - *pp);
2103 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2104 if (name == *pp)
2105 name = NULL;
2106 }
2107 if (name != NULL)
2108 {
2109 name = vim_strsave(name);
2110 *pp = end;
2111 if (STRNCMP(name, "<SNR>", 5) == 0)
2112 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002113 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002114 name[0] = K_SPECIAL;
2115 name[1] = KS_EXTRA;
2116 name[2] = (int)KE_SNR;
2117 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2118 }
2119 goto theend;
2120 }
2121
2122 if (lv.ll_exp_name != NULL)
2123 {
2124 len = (int)STRLEN(lv.ll_exp_name);
2125 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2126 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2127 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002128 // When there was "s:" already or the name expanded to get a
2129 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002130 lv.ll_name += 2;
2131 len -= 2;
2132 lead = 2;
2133 }
2134 }
2135 else
2136 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002137 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002138 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
2139 lv.ll_name += 2;
2140 len = (int)(end - lv.ll_name);
2141 }
2142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 // In Vim9 script a user function is script-local by default.
2144 vim9script = ASCII_ISUPPER(*start)
2145 && current_sctx.sc_version == SCRIPT_VERSION_VIM9;
2146
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002147 /*
2148 * Copy the function name to allocated memory.
2149 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2150 * Accept <SNR>123_name() outside a script.
2151 */
2152 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002153 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002155 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156 if (!vim9script)
2157 lead = 3;
2158 if (vim9script || (lv.ll_exp_name != NULL
2159 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002160 || eval_fname_sid(*pp))
2161 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002163 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002164 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002165 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002166 goto theend;
2167 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002168 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 if (vim9script)
2170 extra = 3 + (int)STRLEN(sid_buf);
2171 else
2172 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002173 }
2174 }
2175 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2176 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002177 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002178 start);
2179 goto theend;
2180 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002181 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002182 {
2183 char_u *cp = vim_strchr(lv.ll_name, ':');
2184
2185 if (cp != NULL && cp < end)
2186 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002187 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002188 goto theend;
2189 }
2190 }
2191
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002192 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002193 if (name != NULL)
2194 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002195 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002196 {
2197 name[0] = K_SPECIAL;
2198 name[1] = KS_EXTRA;
2199 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002201 STRCPY(name + 3, sid_buf);
2202 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002203 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2204 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002205 }
2206 *pp = end;
2207
2208theend:
2209 clear_lval(&lv);
2210 return name;
2211}
2212
2213/*
2214 * ":function"
2215 */
2216 void
2217ex_function(exarg_T *eap)
2218{
2219 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002220 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002221 int j;
2222 int c;
2223 int saved_did_emsg;
2224 int saved_wait_return = need_wait_return;
2225 char_u *name = NULL;
2226 char_u *p;
2227 char_u *arg;
2228 char_u *line_arg = NULL;
2229 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002231 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002232 garray_T newlines;
2233 int varargs = FALSE;
2234 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002236 ufunc_T *fp;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002237 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002238 int indent;
2239 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002240#define MAX_FUNC_NESTING 50
2241 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002242 dictitem_T *v;
2243 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002244 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002245 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002246 int todo;
2247 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002248 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002249 linenr_T sourcing_lnum_off;
2250 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002251 int is_heredoc = FALSE;
2252 char_u *skip_until = NULL;
2253 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002254
2255 /*
2256 * ":function" without argument: list functions.
2257 */
2258 if (ends_excmd(*eap->arg))
2259 {
2260 if (!eap->skip)
2261 {
2262 todo = (int)func_hashtab.ht_used;
2263 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2264 {
2265 if (!HASHITEM_EMPTY(hi))
2266 {
2267 --todo;
2268 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269 if ((fp->uf_flags & FC_DEAD)
2270 || message_filtered(fp->uf_name))
Bram Moolenaarf86db782018-10-25 13:31:37 +02002271 continue;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002272 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002273 list_func_head(fp, FALSE);
2274 }
2275 }
2276 }
2277 eap->nextcmd = check_nextcmd(eap->arg);
2278 return;
2279 }
2280
2281 /*
2282 * ":function /pat": list functions matching pattern.
2283 */
2284 if (*eap->arg == '/')
2285 {
2286 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
2287 if (!eap->skip)
2288 {
2289 regmatch_T regmatch;
2290
2291 c = *p;
2292 *p = NUL;
2293 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2294 *p = c;
2295 if (regmatch.regprog != NULL)
2296 {
2297 regmatch.rm_ic = p_ic;
2298
2299 todo = (int)func_hashtab.ht_used;
2300 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2301 {
2302 if (!HASHITEM_EMPTY(hi))
2303 {
2304 --todo;
2305 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306 if ((fp->uf_flags & FC_DEAD) == 0
2307 && !isdigit(*fp->uf_name)
2308 && vim_regexec(&regmatch, fp->uf_name, 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002309 list_func_head(fp, FALSE);
2310 }
2311 }
2312 vim_regfree(regmatch.regprog);
2313 }
2314 }
2315 if (*p == '/')
2316 ++p;
2317 eap->nextcmd = check_nextcmd(p);
2318 return;
2319 }
2320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 ga_init(&newargs);
2322 ga_init(&argtypes);
2323 ga_init(&default_args);
2324
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002325 /*
2326 * Get the function name. There are these situations:
2327 * func normal function name
2328 * "name" == func, "fudi.fd_dict" == NULL
2329 * dict.func new dictionary entry
2330 * "name" == NULL, "fudi.fd_dict" set,
2331 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2332 * dict.func existing dict entry with a Funcref
2333 * "name" == func, "fudi.fd_dict" set,
2334 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2335 * dict.func existing dict entry that's not a Funcref
2336 * "name" == NULL, "fudi.fd_dict" set,
2337 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2338 * s:func script-local function name
2339 * g:func global function name, same as "func"
2340 */
2341 p = eap->arg;
Bram Moolenaar3388d332017-12-07 22:23:04 +01002342 name = trans_function_name(&p, eap->skip, TFN_NO_AUTOLOAD, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002343 paren = (vim_strchr(p, '(') != NULL);
2344 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
2345 {
2346 /*
2347 * Return on an invalid expression in braces, unless the expression
2348 * evaluation has been cancelled due to an aborting error, an
2349 * interrupt, or an exception.
2350 */
2351 if (!aborting())
2352 {
2353 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002354 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002355 vim_free(fudi.fd_newkey);
2356 return;
2357 }
2358 else
2359 eap->skip = TRUE;
2360 }
2361
Bram Moolenaare38eab22019-12-05 21:50:01 +01002362 // An error in a function call during evaluation of an expression in magic
2363 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002364 saved_did_emsg = did_emsg;
2365 did_emsg = FALSE;
2366
2367 /*
2368 * ":function func" with only function name: list function.
2369 */
2370 if (!paren)
2371 {
2372 if (!ends_excmd(*skipwhite(p)))
2373 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002374 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002375 goto ret_free;
2376 }
2377 eap->nextcmd = check_nextcmd(p);
2378 if (eap->nextcmd != NULL)
2379 *p = NUL;
2380 if (!eap->skip && !got_int)
2381 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382 fp = find_func(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002383 if (fp != NULL)
2384 {
2385 list_func_head(fp, TRUE);
2386 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2387 {
2388 if (FUNCLINE(fp, j) == NULL)
2389 continue;
2390 msg_putchar('\n');
2391 msg_outnum((long)(j + 1));
2392 if (j < 9)
2393 msg_putchar(' ');
2394 if (j < 99)
2395 msg_putchar(' ');
2396 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002397 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002398 ui_breakcheck();
2399 }
2400 if (!got_int)
2401 {
2402 msg_putchar('\n');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 if (fp->uf_dfunc_idx >= 0)
2404 msg_puts(" enddef");
2405 else
2406 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002407 }
2408 }
2409 else
2410 emsg_funcname(N_("E123: Undefined function: %s"), name);
2411 }
2412 goto ret_free;
2413 }
2414
2415 /*
2416 * ":function name(arg1, arg2)" Define function.
2417 */
2418 p = skipwhite(p);
2419 if (*p != '(')
2420 {
2421 if (!eap->skip)
2422 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002423 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002424 goto ret_free;
2425 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002426 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002427 if (vim_strchr(p, '(') != NULL)
2428 p = vim_strchr(p, '(');
2429 }
2430 p = skipwhite(p + 1);
2431
2432 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2433
2434 if (!eap->skip)
2435 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002436 // Check the name of the function. Unless it's a dictionary function
2437 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002438 if (name != NULL)
2439 arg = name;
2440 else
2441 arg = fudi.fd_newkey;
2442 if (arg != NULL && (fudi.fd_di == NULL
2443 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2444 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2445 {
2446 if (*arg == K_SPECIAL)
2447 j = 3;
2448 else
2449 j = 0;
2450 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2451 : eval_isnamec(arg[j])))
2452 ++j;
2453 if (arg[j] != NUL)
2454 emsg_funcname((char *)e_invarg2, arg);
2455 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002456 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002457 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002458 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002459 }
2460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461 if (get_function_args(&p, ')', &newargs,
2462 eap->cmdidx == CMD_def ? &argtypes : NULL,
2463 &varargs, &default_args, eap->skip) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002464 goto errret_2;
2465
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002467 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468 // find the return type: :def Func(): type
2469 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002470 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 ret_type = skipwhite(p + 1);
2472 p = skip_type(ret_type);
2473 if (p > ret_type)
2474 p = skipwhite(p);
2475 else
2476 semsg(_("E1056: expected a type: %s"), ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002477 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002478 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 else
2480 // find extra arguments "range", "dict", "abort" and "closure"
2481 for (;;)
2482 {
2483 p = skipwhite(p);
2484 if (STRNCMP(p, "range", 5) == 0)
2485 {
2486 flags |= FC_RANGE;
2487 p += 5;
2488 }
2489 else if (STRNCMP(p, "dict", 4) == 0)
2490 {
2491 flags |= FC_DICT;
2492 p += 4;
2493 }
2494 else if (STRNCMP(p, "abort", 5) == 0)
2495 {
2496 flags |= FC_ABORT;
2497 p += 5;
2498 }
2499 else if (STRNCMP(p, "closure", 7) == 0)
2500 {
2501 flags |= FC_CLOSURE;
2502 p += 7;
2503 if (current_funccal == NULL)
2504 {
2505 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
2506 name == NULL ? (char_u *)"" : name);
2507 goto erret;
2508 }
2509 }
2510 else
2511 break;
2512 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002513
Bram Moolenaare38eab22019-12-05 21:50:01 +01002514 // When there is a line break use what follows for the function body.
2515 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002516 if (*p == '\n')
2517 line_arg = p + 1;
2518 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002519 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002520
2521 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
2523 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002524 */
2525 if (KeyTyped)
2526 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002527 // Check if the function already exists, don't let the user type the
2528 // whole function before telling him it doesn't work! For a script we
2529 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002530 if (!eap->skip && !eap->forceit)
2531 {
2532 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002533 emsg(_(e_funcdict));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 else if (name != NULL && find_func(name, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002535 emsg_funcname(e_funcexts, name);
2536 }
2537
2538 if (!eap->skip && did_emsg)
2539 goto erret;
2540
Bram Moolenaare38eab22019-12-05 21:50:01 +01002541 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002542 cmdline_row = msg_row;
2543 }
2544
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002545 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002546 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002547
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002548 indent = 2;
2549 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002551 for (;;)
2552 {
2553 if (KeyTyped)
2554 {
2555 msg_scroll = TRUE;
2556 saved_wait_return = FALSE;
2557 }
2558 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002559
2560 if (line_arg != NULL)
2561 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002562 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002563 theline = line_arg;
2564 p = vim_strchr(theline, '\n');
2565 if (p == NULL)
2566 line_arg += STRLEN(line_arg);
2567 else
2568 {
2569 *p = NUL;
2570 line_arg = p + 1;
2571 }
2572 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002573 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002574 {
2575 vim_free(line_to_free);
2576 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002577 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002578 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002579 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002580 line_to_free = theline;
2581 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002582 if (KeyTyped)
2583 lines_left = Rows - 1;
2584 if (theline == NULL)
2585 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 if (eap->cmdidx == CMD_def)
2587 emsg(_("E1057: Missing :enddef"));
2588 else
2589 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002590 goto erret;
2591 }
2592
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002593 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002594 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002595 if (SOURCING_LNUM < sourcing_lnum_off)
2596 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002597 else
2598 sourcing_lnum_off = 0;
2599
2600 if (skip_until != NULL)
2601 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002603 // * ":append" and "."
2604 // * ":python <<EOF" and "EOF"
2605 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2606 if (heredoc_trimmed == NULL
2607 || (is_heredoc && skipwhite(theline) == theline)
2608 || STRNCMP(theline, heredoc_trimmed,
2609 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002610 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002611 if (heredoc_trimmed == NULL)
2612 p = theline;
2613 else if (is_heredoc)
2614 p = skipwhite(theline) == theline
2615 ? theline : theline + STRLEN(heredoc_trimmed);
2616 else
2617 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002618 if (STRCMP(p, skip_until) == 0)
2619 {
2620 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002621 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002622 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002623 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002624 }
2625 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002626 }
2627 else
2628 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002629 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01002630 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002631 ;
2632
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 // Check for "endfunction" or "enddef".
2634 if (checkforcmd(&p, nesting_def[nesting]
2635 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002636 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002637 char_u *nextcmd = NULL;
2638
Bram Moolenaar663bb232017-06-22 19:12:10 +02002639 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002640 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002641 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002642 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002643 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 give_warning2(eap->cmdidx == CMD_def
2645 ? (char_u *)_("W1001: Text found after :enddef: %s")
2646 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002647 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002648 if (nextcmd != NULL)
2649 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002650 // Another command follows. If the line came from "eap" we
2651 // can simply point into it, otherwise we need to change
2652 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02002653 eap->nextcmd = nextcmd;
2654 if (line_to_free != NULL)
2655 {
2656 vim_free(*eap->cmdlinep);
2657 *eap->cmdlinep = line_to_free;
2658 line_to_free = NULL;
2659 }
2660 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002661 break;
2662 }
2663
Bram Moolenaare38eab22019-12-05 21:50:01 +01002664 // Increase indent inside "if", "while", "for" and "try", decrease
2665 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002667 indent -= 2;
2668 else if (STRNCMP(p, "if", 2) == 0
2669 || STRNCMP(p, "wh", 2) == 0
2670 || STRNCMP(p, "for", 3) == 0
2671 || STRNCMP(p, "try", 3) == 0)
2672 indent += 2;
2673
Bram Moolenaare38eab22019-12-05 21:50:01 +01002674 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01002675 // Only recognize "def" inside "def", not inside "function",
2676 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01002678 if (checkforcmd(&p, "function", 2)
2679 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002680 {
2681 if (*p == '!')
2682 p = skipwhite(p + 1);
2683 p += eval_fname_script(p);
2684 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
2685 if (*skipwhite(p) == '(')
2686 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 if (nesting == MAX_FUNC_NESTING - 1)
2688 emsg(_("E1058: function nesting too deep"));
2689 else
2690 {
2691 ++nesting;
2692 nesting_def[nesting] = (c == 'd');
2693 indent += 2;
2694 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002695 }
2696 }
2697
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002698 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002699 p = skip_range(p, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002700 if (eap->cmdidx != CMD_def
2701 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002702 || (p[0] == 'c'
2703 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2704 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2705 && (STRNCMP(&p[3], "nge", 3) != 0
2706 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002707 || (p[0] == 'i'
2708 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002709 && (!ASCII_ISALPHA(p[2])
2710 || (p[2] == 's'
2711 && (!ASCII_ISALPHA(p[3])
2712 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002713 skip_until = vim_strsave((char_u *)".");
2714
Bram Moolenaare38eab22019-12-05 21:50:01 +01002715 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002716 arg = skipwhite(skiptowhite(p));
2717 if (arg[0] == '<' && arg[1] =='<'
2718 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002719 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2720 || ((p[2] == '3' || p[2] == 'x')
2721 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002722 || (p[0] == 'p' && p[1] == 'e'
2723 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2724 || (p[0] == 't' && p[1] == 'c'
2725 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2726 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2727 && !ASCII_ISALPHA(p[3]))
2728 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2729 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2730 || (p[0] == 'm' && p[1] == 'z'
2731 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2732 ))
2733 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002734 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002735 p = skipwhite(arg + 2);
2736 if (*p == NUL)
2737 skip_until = vim_strsave((char_u *)".");
2738 else
2739 skip_until = vim_strsave(p);
2740 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002741
2742 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002743 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02002744 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002745 if (*arg == '[')
2746 arg = vim_strchr(arg, ']');
2747 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002748 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002749 arg = skipwhite(skiptowhite(arg));
2750 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
2751 && ((p[0] == 'l'
2752 && p[1] == 'e'
2753 && (!ASCII_ISALNUM(p[2])
2754 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02002755 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002756 p = skipwhite(arg + 3);
2757 if (STRNCMP(p, "trim", 4) == 0)
2758 {
2759 // Ignore leading white space.
2760 p = skipwhite(p + 4);
2761 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar8471e572019-05-19 21:37:18 +02002762 (int)(skipwhite(theline) - theline));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002763 }
2764 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2765 do_concat = FALSE;
2766 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002767 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002768 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002769 }
2770
Bram Moolenaare38eab22019-12-05 21:50:01 +01002771 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002772 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002773 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002774
Bram Moolenaare38eab22019-12-05 21:50:01 +01002775 // Copy the line to newly allocated memory. get_one_sourceline()
2776 // allocates 250 bytes per line, this saves 80% on average. The cost
2777 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002778 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002779 if (p == NULL)
2780 goto erret;
2781 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002782
Bram Moolenaare38eab22019-12-05 21:50:01 +01002783 // Add NULL lines for continuation lines, so that the line count is
2784 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002785 while (sourcing_lnum_off-- > 0)
2786 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2787
Bram Moolenaare38eab22019-12-05 21:50:01 +01002788 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002789 if (line_arg != NULL && *line_arg == NUL)
2790 line_arg = NULL;
2791 }
2792
Bram Moolenaare38eab22019-12-05 21:50:01 +01002793 // Don't define the function when skipping commands or when an error was
2794 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002795 if (eap->skip || did_emsg)
2796 goto erret;
2797
2798 /*
2799 * If there are no errors, add the function
2800 */
2801 if (fudi.fd_dict == NULL)
2802 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 hashtab_T *ht;
2804
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002805 v = find_var(name, &ht, FALSE);
2806 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
2807 {
2808 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
2809 name);
2810 goto erret;
2811 }
2812
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 fp = find_func_even_dead(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002814 if (fp != NULL)
2815 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 int dead = fp->uf_flags & FC_DEAD;
2817
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002818 // Function can be replaced with "function!" and when sourcing the
2819 // same script again, but only once.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 if (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002821 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
2822 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002823 {
2824 emsg_funcname(e_funcexts, name);
2825 goto erret;
2826 }
2827 if (fp->uf_calls > 0)
2828 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002829 emsg_funcname(
2830 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002831 name);
2832 goto erret;
2833 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002834 if (fp->uf_refcount > 1)
2835 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002836 // This function is referenced somewhere, don't redefine it but
2837 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002838 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002839 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002840 fp = NULL;
2841 overwrite = TRUE;
2842 }
2843 else
2844 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002845 char_u *exp_name = fp->uf_name_exp;
2846
2847 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01002848 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002849 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002850 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002851 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002853#ifdef FEAT_PROFILE
2854 fp->uf_profiling = FALSE;
2855 fp->uf_prof_initialized = FALSE;
2856#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002857 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002858 }
2859 }
2860 else
2861 {
2862 char numbuf[20];
2863
2864 fp = NULL;
2865 if (fudi.fd_newkey == NULL && !eap->forceit)
2866 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002867 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002868 goto erret;
2869 }
2870 if (fudi.fd_di == NULL)
2871 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002872 // Can't add a function to a locked dictionary
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002873 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002874 goto erret;
2875 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002876 // Can't change an existing function if it is locked
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002877 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002878 goto erret;
2879
Bram Moolenaare38eab22019-12-05 21:50:01 +01002880 // Give the function a sequential number. Can only be used with a
2881 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002882 vim_free(name);
2883 sprintf(numbuf, "%d", ++func_nr);
2884 name = vim_strsave((char_u *)numbuf);
2885 if (name == NULL)
2886 goto erret;
2887 }
2888
2889 if (fp == NULL)
2890 {
2891 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
2892 {
2893 int slen, plen;
2894 char_u *scriptname;
2895
Bram Moolenaare38eab22019-12-05 21:50:01 +01002896 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002897 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002898 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002899 {
2900 scriptname = autoload_name(name);
2901 if (scriptname != NULL)
2902 {
2903 p = vim_strchr(scriptname, '/');
2904 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002905 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002906 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002907 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002908 j = OK;
2909 vim_free(scriptname);
2910 }
2911 }
2912 if (j == FAIL)
2913 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002914 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002915 goto erret;
2916 }
2917 }
2918
Bram Moolenaar47ed5532019-08-08 20:49:14 +02002919 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002920 if (fp == NULL)
2921 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 fp->uf_dfunc_idx = -1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002923
2924 if (fudi.fd_dict != NULL)
2925 {
2926 if (fudi.fd_di == NULL)
2927 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002928 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002929 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
2930 if (fudi.fd_di == NULL)
2931 {
2932 vim_free(fp);
2933 goto erret;
2934 }
2935 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
2936 {
2937 vim_free(fudi.fd_di);
2938 vim_free(fp);
2939 goto erret;
2940 }
2941 }
2942 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01002943 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002944 clear_tv(&fudi.fd_di->di_tv);
2945 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002946 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002947
Bram Moolenaare38eab22019-12-05 21:50:01 +01002948 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002949 flags |= FC_DICT;
2950 }
2951
Bram Moolenaare38eab22019-12-05 21:50:01 +01002952 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002953 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002954 if (overwrite)
2955 {
2956 hi = hash_find(&func_hashtab, name);
2957 hi->hi_key = UF2HIKEY(fp);
2958 }
2959 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002960 {
2961 vim_free(fp);
2962 goto erret;
2963 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002964 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002965 }
2966 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002967 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 fp->uf_ret_type = &t_any;
2969
2970 if (eap->cmdidx == CMD_def)
2971 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01002972 int lnum_save = SOURCING_LNUM;
2973
2974 // error messages are for the first function line
2975 SOURCING_LNUM = sourcing_lnum_top;
2976
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 // parse the argument types
2978 ga_init2(&fp->uf_type_list, sizeof(type_T), 5);
2979
2980 if (argtypes.ga_len > 0)
2981 {
2982 // When "varargs" is set the last name/type goes into uf_va_name
2983 // and uf_va_type.
2984 int len = argtypes.ga_len - (varargs ? 1 : 0);
2985
2986 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
2987 if (fp->uf_arg_types != NULL)
2988 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01002989 int i;
2990 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991
2992 for (i = 0; i < len; ++ i)
2993 {
2994 p = ((char_u **)argtypes.ga_data)[i];
2995 if (p == NULL)
2996 // todo: get type from default value
Bram Moolenaarbfe12042020-02-04 21:54:07 +01002997 type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 else
Bram Moolenaarbfe12042020-02-04 21:54:07 +01002999 type = parse_type(&p, &fp->uf_type_list);
3000 if (type == NULL)
3001 {
3002 SOURCING_LNUM = lnum_save;
3003 goto errret_2;
3004 }
3005 fp->uf_arg_types[i] = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 }
3007 }
3008 if (varargs)
3009 {
3010 // Move the last argument "...name: type" to uf_va_name and
3011 // uf_va_type.
3012 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
3013 [fp->uf_args.ga_len - 1];
3014 --fp->uf_args.ga_len;
3015 p = ((char_u **)argtypes.ga_data)[len];
3016 if (p == NULL)
3017 // todo: get type from default value
3018 fp->uf_va_type = &t_any;
3019 else
3020 fp->uf_va_type = parse_type(&p, &fp->uf_type_list);
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003021 if (fp->uf_va_type == NULL)
3022 {
3023 SOURCING_LNUM = lnum_save;
3024 goto errret_2;
3025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 }
3027 varargs = FALSE;
3028 }
3029
3030 // parse the return type, if any
3031 if (ret_type == NULL)
3032 fp->uf_ret_type = &t_void;
3033 else
3034 {
3035 p = ret_type;
3036 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list);
3037 }
3038 }
3039
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003040 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003041 if ((flags & FC_CLOSURE) != 0)
3042 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003043 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003044 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003045 }
3046 else
3047 fp->uf_scoped = NULL;
3048
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003049#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003050 if (prof_def_func())
3051 func_do_profile(fp);
3052#endif
3053 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003054 if (sandbox)
3055 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003056 fp->uf_flags = flags;
3057 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003058 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003059 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 if (is_export)
3061 {
3062 fp->uf_flags |= FC_EXPORT;
3063 // let ex_export() know the export worked.
3064 is_export = FALSE;
3065 }
3066
3067 // ":def Func()" needs to be compiled
3068 if (eap->cmdidx == CMD_def)
3069 compile_def_function(fp, FALSE);
3070
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003071 goto ret_free;
3072
3073erret:
3074 ga_clear_strings(&newargs);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 ga_clear_strings(&argtypes);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003076 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003077errret_2:
3078 ga_clear_strings(&newlines);
3079ret_free:
3080 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003081 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003082 vim_free(fudi.fd_newkey);
3083 vim_free(name);
3084 did_emsg |= saved_did_emsg;
3085 need_wait_return |= saved_wait_return;
3086}
3087
3088/*
3089 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3090 * Return 2 if "p" starts with "s:".
3091 * Return 0 otherwise.
3092 */
3093 int
3094eval_fname_script(char_u *p)
3095{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003096 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3097 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003098 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3099 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3100 return 5;
3101 if (p[0] == 's' && p[1] == ':')
3102 return 2;
3103 return 0;
3104}
3105
3106 int
3107translated_function_exists(char_u *name)
3108{
3109 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003110 return has_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 return find_func(name, NULL) != NULL;
3112}
3113
3114/*
3115 * Return TRUE when "ufunc" has old-style "..." varargs
3116 * or named varargs "...name: type".
3117 */
3118 int
3119has_varargs(ufunc_T *ufunc)
3120{
3121 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003122}
3123
3124/*
3125 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003126 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003127 */
3128 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003129function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003130{
3131 char_u *nm = name;
3132 char_u *p;
3133 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003134 int flag;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003135
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003136 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3137 if (no_deref)
3138 flag |= TFN_NO_DEREF;
3139 p = trans_function_name(&nm, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003140 nm = skipwhite(nm);
3141
Bram Moolenaare38eab22019-12-05 21:50:01 +01003142 // Only accept "funcname", "funcname ", "funcname (..." and
3143 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003144 if (p != NULL && (*nm == NUL || *nm == '('))
3145 n = translated_function_exists(p);
3146 vim_free(p);
3147 return n;
3148}
3149
Bram Moolenaar113e1072019-01-20 15:30:40 +01003150#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003151 char_u *
3152get_expanded_name(char_u *name, int check)
3153{
3154 char_u *nm = name;
3155 char_u *p;
3156
3157 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
3158
3159 if (p != NULL && *nm == NUL)
3160 if (!check || translated_function_exists(p))
3161 return p;
3162
3163 vim_free(p);
3164 return NULL;
3165}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003166#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003167
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003168/*
3169 * Function given to ExpandGeneric() to obtain the list of user defined
3170 * function names.
3171 */
3172 char_u *
3173get_user_func_name(expand_T *xp, int idx)
3174{
3175 static long_u done;
3176 static hashitem_T *hi;
3177 ufunc_T *fp;
3178
3179 if (idx == 0)
3180 {
3181 done = 0;
3182 hi = func_hashtab.ht_array;
3183 }
3184 if (done < func_hashtab.ht_used)
3185 {
3186 if (done++ > 0)
3187 ++hi;
3188 while (HASHITEM_EMPTY(hi))
3189 ++hi;
3190 fp = HI2UF(hi);
3191
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 // don't show dead, dict and lambda functions
3193 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02003194 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003196
3197 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003198 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003199
3200 cat_func_name(IObuff, fp);
3201 if (xp->xp_context != EXPAND_USER_FUNC)
3202 {
3203 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003205 STRCAT(IObuff, ")");
3206 }
3207 return IObuff;
3208 }
3209 return NULL;
3210}
3211
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003212/*
3213 * ":delfunction {name}"
3214 */
3215 void
3216ex_delfunction(exarg_T *eap)
3217{
3218 ufunc_T *fp = NULL;
3219 char_u *p;
3220 char_u *name;
3221 funcdict_T fudi;
3222
3223 p = eap->arg;
3224 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
3225 vim_free(fudi.fd_newkey);
3226 if (name == NULL)
3227 {
3228 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003229 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003230 return;
3231 }
3232 if (!ends_excmd(*skipwhite(p)))
3233 {
3234 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003235 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003236 return;
3237 }
3238 eap->nextcmd = check_nextcmd(p);
3239 if (eap->nextcmd != NULL)
3240 *p = NUL;
3241
3242 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 fp = find_func(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003244 vim_free(name);
3245
3246 if (!eap->skip)
3247 {
3248 if (fp == NULL)
3249 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02003250 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003251 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003252 return;
3253 }
3254 if (fp->uf_calls > 0)
3255 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003256 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003257 return;
3258 }
3259
3260 if (fudi.fd_dict != NULL)
3261 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003262 // Delete the dict item that refers to the function, it will
3263 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003264 dictitem_remove(fudi.fd_dict, fudi.fd_di);
3265 }
3266 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003267 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003268 // A normal function (not a numbered function or lambda) has a
3269 // refcount of 1 for the entry in the hashtable. When deleting
3270 // it and the refcount is more than one, it should be kept.
3271 // A numbered function and lambda should be kept if the refcount is
3272 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003273 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003274 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003275 // Function is still referenced somewhere. Don't free it but
3276 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003277 if (func_remove(fp))
3278 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003279 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003280 }
3281 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003282 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003283 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003284 }
3285}
3286
3287/*
3288 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003289 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003290 */
3291 void
3292func_unref(char_u *name)
3293{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003294 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003295
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003296 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003297 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003299 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003300 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003301#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003302 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003303#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01003304 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003305 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003306 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003307 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003308 // Only delete it when it's not being used. Otherwise it's done
3309 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003310 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003311 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02003312 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003313}
3314
3315/*
3316 * Unreference a Function: decrement the reference count and free it when it
3317 * becomes zero.
3318 */
3319 void
3320func_ptr_unref(ufunc_T *fp)
3321{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003322 if (fp != NULL && --fp->uf_refcount <= 0)
3323 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003324 // Only delete it when it's not being used. Otherwise it's done
3325 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02003326 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003327 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003328 }
3329}
3330
3331/*
3332 * Count a reference to a Function.
3333 */
3334 void
3335func_ref(char_u *name)
3336{
3337 ufunc_T *fp;
3338
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003339 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003340 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003342 if (fp != NULL)
3343 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003344 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01003345 // Only give an error for a numbered function.
3346 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01003347 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003348}
3349
3350/*
3351 * Count a reference to a Function.
3352 */
3353 void
3354func_ptr_ref(ufunc_T *fp)
3355{
3356 if (fp != NULL)
3357 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003358}
3359
3360/*
3361 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3362 * referenced from anywhere that is in use.
3363 */
3364 static int
3365can_free_funccal(funccall_T *fc, int copyID)
3366{
3367 return (fc->l_varlist.lv_copyID != copyID
3368 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003369 && fc->l_avars.dv_copyID != copyID
3370 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003371}
3372
3373/*
3374 * ":return [expr]"
3375 */
3376 void
3377ex_return(exarg_T *eap)
3378{
3379 char_u *arg = eap->arg;
3380 typval_T rettv;
3381 int returning = FALSE;
3382
3383 if (current_funccal == NULL)
3384 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003385 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003386 return;
3387 }
3388
3389 if (eap->skip)
3390 ++emsg_skip;
3391
3392 eap->nextcmd = NULL;
3393 if ((*arg != NUL && *arg != '|' && *arg != '\n')
3394 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
3395 {
3396 if (!eap->skip)
3397 returning = do_return(eap, FALSE, TRUE, &rettv);
3398 else
3399 clear_tv(&rettv);
3400 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003401 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003402 else if (!eap->skip)
3403 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003404 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003405 update_force_abort();
3406
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003407 /*
3408 * Return unless the expression evaluation has been cancelled due to an
3409 * aborting error, an interrupt, or an exception.
3410 */
3411 if (!aborting())
3412 returning = do_return(eap, FALSE, TRUE, NULL);
3413 }
3414
Bram Moolenaare38eab22019-12-05 21:50:01 +01003415 // When skipping or the return gets pending, advance to the next command
3416 // in this line (!returning). Otherwise, ignore the rest of the line.
3417 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003418 if (returning)
3419 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003420 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003421 eap->nextcmd = check_nextcmd(arg);
3422
3423 if (eap->skip)
3424 --emsg_skip;
3425}
3426
3427/*
3428 * ":1,25call func(arg1, arg2)" function call.
3429 */
3430 void
3431ex_call(exarg_T *eap)
3432{
3433 char_u *arg = eap->arg;
3434 char_u *startarg;
3435 char_u *name;
3436 char_u *tofree;
3437 int len;
3438 typval_T rettv;
3439 linenr_T lnum;
3440 int doesrange;
3441 int failed = FALSE;
3442 funcdict_T fudi;
3443 partial_T *partial = NULL;
3444
3445 if (eap->skip)
3446 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003447 // trans_function_name() doesn't work well when skipping, use eval0()
3448 // instead to skip to any following command, e.g. for:
3449 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003450 ++emsg_skip;
3451 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3452 clear_tv(&rettv);
3453 --emsg_skip;
3454 return;
3455 }
3456
3457 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
3458 if (fudi.fd_newkey != NULL)
3459 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003460 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003461 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003462 vim_free(fudi.fd_newkey);
3463 }
3464 if (tofree == NULL)
3465 return;
3466
Bram Moolenaare38eab22019-12-05 21:50:01 +01003467 // Increase refcount on dictionary, it could get deleted when evaluating
3468 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003469 if (fudi.fd_dict != NULL)
3470 ++fudi.fd_dict->dv_refcount;
3471
Bram Moolenaare38eab22019-12-05 21:50:01 +01003472 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3473 // contents. For VAR_PARTIAL get its partial, unless we already have one
3474 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003475 len = (int)STRLEN(tofree);
3476 name = deref_func_name(tofree, &len,
3477 partial != NULL ? NULL : &partial, FALSE);
3478
Bram Moolenaare38eab22019-12-05 21:50:01 +01003479 // Skip white space to allow ":call func ()". Not good, but required for
3480 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003481 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003482 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003483
3484 if (*startarg != '(')
3485 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003487 goto end;
3488 }
3489
3490 /*
3491 * When skipping, evaluate the function once, to find the end of the
3492 * arguments.
3493 * When the function takes a range, this is discovered after the first
3494 * call, and the loop is broken.
3495 */
3496 if (eap->skip)
3497 {
3498 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003499 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003500 }
3501 else
3502 lnum = eap->line1;
3503 for ( ; lnum <= eap->line2; ++lnum)
3504 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003505 funcexe_T funcexe;
3506
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003507 if (!eap->skip && eap->addr_count > 0)
3508 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003509 if (lnum > curbuf->b_ml.ml_line_count)
3510 {
3511 // If the function deleted lines or switched to another buffer
3512 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003513 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003514 break;
3515 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003516 curwin->w_cursor.lnum = lnum;
3517 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003518 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003519 }
3520 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003521
Bram Moolenaarac92e252019-08-03 21:58:38 +02003522 vim_memset(&funcexe, 0, sizeof(funcexe));
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003523 funcexe.firstline = eap->line1;
3524 funcexe.lastline = eap->line2;
3525 funcexe.doesrange = &doesrange;
3526 funcexe.evaluate = !eap->skip;
3527 funcexe.partial = partial;
3528 funcexe.selfdict = fudi.fd_dict;
3529 if (get_func_tv(name, -1, &rettv, &arg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003530 {
3531 failed = TRUE;
3532 break;
3533 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003534 if (has_watchexpr())
3535 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003536
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003537 // Handle a function returning a Funcref, Dictionary or List.
3538 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE,
3539 name, &name) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003540 {
3541 failed = TRUE;
3542 break;
3543 }
3544
3545 clear_tv(&rettv);
3546 if (doesrange || eap->skip)
3547 break;
3548
Bram Moolenaare38eab22019-12-05 21:50:01 +01003549 // Stop when immediately aborting on error, or when an interrupt
3550 // occurred or an exception was thrown but not caught.
3551 // get_func_tv() returned OK, so that the check for trailing
3552 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003553 if (aborting())
3554 break;
3555 }
3556 if (eap->skip)
3557 --emsg_skip;
3558
3559 if (!failed)
3560 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003561 // Check for trailing illegal characters and a following command.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003562 if (!ends_excmd(*arg))
3563 {
3564 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003565 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003566 }
3567 else
3568 eap->nextcmd = check_nextcmd(arg);
3569 }
3570
3571end:
3572 dict_unref(fudi.fd_dict);
3573 vim_free(tofree);
3574}
3575
3576/*
3577 * Return from a function. Possibly makes the return pending. Also called
3578 * for a pending return at the ":endtry" or after returning from an extra
3579 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3580 * when called due to a ":return" command. "rettv" may point to a typval_T
3581 * with the return rettv. Returns TRUE when the return can be carried out,
3582 * FALSE when the return gets pending.
3583 */
3584 int
3585do_return(
3586 exarg_T *eap,
3587 int reanimate,
3588 int is_cmd,
3589 void *rettv)
3590{
3591 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01003592 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003593
3594 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003595 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003596 current_funccal->returned = FALSE;
3597
3598 /*
3599 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3600 * not in its finally clause (which then is to be executed next) is found.
3601 * In this case, make the ":return" pending for execution at the ":endtry".
3602 * Otherwise, return normally.
3603 */
3604 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3605 if (idx >= 0)
3606 {
3607 cstack->cs_pending[idx] = CSTP_RETURN;
3608
3609 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003610 // A pending return again gets pending. "rettv" points to an
3611 // allocated variable with the rettv of the original ":return"'s
3612 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003613 cstack->cs_rettv[idx] = rettv;
3614 else
3615 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003616 // When undoing a return in order to make it pending, get the stored
3617 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003618 if (reanimate)
3619 rettv = current_funccal->rettv;
3620
3621 if (rettv != NULL)
3622 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003623 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003624 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3625 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3626 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003627 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003628 }
3629 else
3630 cstack->cs_rettv[idx] = NULL;
3631
3632 if (reanimate)
3633 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003634 // The pending return value could be overwritten by a ":return"
3635 // without argument in a finally clause; reset the default
3636 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003637 current_funccal->rettv->v_type = VAR_NUMBER;
3638 current_funccal->rettv->vval.v_number = 0;
3639 }
3640 }
3641 report_make_pending(CSTP_RETURN, rettv);
3642 }
3643 else
3644 {
3645 current_funccal->returned = TRUE;
3646
Bram Moolenaare38eab22019-12-05 21:50:01 +01003647 // If the return is carried out now, store the return value. For
3648 // a return immediately after reanimation, the value is already
3649 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003650 if (!reanimate && rettv != NULL)
3651 {
3652 clear_tv(current_funccal->rettv);
3653 *current_funccal->rettv = *(typval_T *)rettv;
3654 if (!is_cmd)
3655 vim_free(rettv);
3656 }
3657 }
3658
3659 return idx < 0;
3660}
3661
3662/*
3663 * Free the variable with a pending return value.
3664 */
3665 void
3666discard_pending_return(void *rettv)
3667{
3668 free_tv((typval_T *)rettv);
3669}
3670
3671/*
3672 * Generate a return command for producing the value of "rettv". The result
3673 * is an allocated string. Used by report_pending() for verbose messages.
3674 */
3675 char_u *
3676get_return_cmd(void *rettv)
3677{
3678 char_u *s = NULL;
3679 char_u *tofree = NULL;
3680 char_u numbuf[NUMBUFLEN];
3681
3682 if (rettv != NULL)
3683 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3684 if (s == NULL)
3685 s = (char_u *)"";
3686
3687 STRCPY(IObuff, ":return ");
3688 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3689 if (STRLEN(s) + 8 >= IOSIZE)
3690 STRCPY(IObuff + IOSIZE - 4, "...");
3691 vim_free(tofree);
3692 return vim_strsave(IObuff);
3693}
3694
3695/*
3696 * Get next function line.
3697 * Called by do_cmdline() to get the next line.
3698 * Returns allocated string, or NULL for end of function.
3699 */
3700 char_u *
3701get_func_line(
3702 int c UNUSED,
3703 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003704 int indent UNUSED,
3705 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003706{
3707 funccall_T *fcp = (funccall_T *)cookie;
3708 ufunc_T *fp = fcp->func;
3709 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003710 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003711
Bram Moolenaare38eab22019-12-05 21:50:01 +01003712 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003713 if (fcp->dbg_tick != debug_tick)
3714 {
3715 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003716 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003717 fcp->dbg_tick = debug_tick;
3718 }
3719#ifdef FEAT_PROFILE
3720 if (do_profiling == PROF_YES)
3721 func_line_end(cookie);
3722#endif
3723
3724 gap = &fp->uf_lines;
3725 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3726 || fcp->returned)
3727 retval = NULL;
3728 else
3729 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003730 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003731 while (fcp->linenr < gap->ga_len
3732 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3733 ++fcp->linenr;
3734 if (fcp->linenr >= gap->ga_len)
3735 retval = NULL;
3736 else
3737 {
3738 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003739 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003740#ifdef FEAT_PROFILE
3741 if (do_profiling == PROF_YES)
3742 func_line_start(cookie);
3743#endif
3744 }
3745 }
3746
Bram Moolenaare38eab22019-12-05 21:50:01 +01003747 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003748 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003749 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003750 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003751 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003752 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003753 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003754 fcp->dbg_tick = debug_tick;
3755 }
3756
3757 return retval;
3758}
3759
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003760/*
3761 * Return TRUE if the currently active function should be ended, because a
3762 * return was encountered or an error occurred. Used inside a ":while".
3763 */
3764 int
3765func_has_ended(void *cookie)
3766{
3767 funccall_T *fcp = (funccall_T *)cookie;
3768
Bram Moolenaare38eab22019-12-05 21:50:01 +01003769 // Ignore the "abort" flag if the abortion behavior has been changed due to
3770 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003771 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3772 || fcp->returned);
3773}
3774
3775/*
3776 * return TRUE if cookie indicates a function which "abort"s on errors.
3777 */
3778 int
3779func_has_abort(
3780 void *cookie)
3781{
3782 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
3783}
3784
3785
3786/*
3787 * Turn "dict.Func" into a partial for "Func" bound to "dict".
3788 * Don't do this when "Func" is already a partial that was bound
3789 * explicitly (pt_auto is FALSE).
3790 * Changes "rettv" in-place.
3791 * Returns the updated "selfdict_in".
3792 */
3793 dict_T *
3794make_partial(dict_T *selfdict_in, typval_T *rettv)
3795{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003796 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003797 char_u *tofree = NULL;
3798 ufunc_T *fp;
3799 char_u fname_buf[FLEN_FIXED + 1];
3800 int error;
3801 dict_T *selfdict = selfdict_in;
3802
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003803 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
3804 fp = rettv->vval.v_partial->pt_func;
3805 else
3806 {
3807 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
3808 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003809 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003810 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003812 vim_free(tofree);
3813 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003814
3815 if (fp != NULL && (fp->uf_flags & FC_DICT))
3816 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003817 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003818
3819 if (pt != NULL)
3820 {
3821 pt->pt_refcount = 1;
3822 pt->pt_dict = selfdict;
3823 pt->pt_auto = TRUE;
3824 selfdict = NULL;
3825 if (rettv->v_type == VAR_FUNC)
3826 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003827 // Just a function: Take over the function name and use
3828 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003829 pt->pt_name = rettv->vval.v_string;
3830 }
3831 else
3832 {
3833 partial_T *ret_pt = rettv->vval.v_partial;
3834 int i;
3835
Bram Moolenaare38eab22019-12-05 21:50:01 +01003836 // Partial: copy the function name, use selfdict and copy
3837 // args. Can't take over name or args, the partial might
3838 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003839 if (ret_pt->pt_name != NULL)
3840 {
3841 pt->pt_name = vim_strsave(ret_pt->pt_name);
3842 func_ref(pt->pt_name);
3843 }
3844 else
3845 {
3846 pt->pt_func = ret_pt->pt_func;
3847 func_ptr_ref(pt->pt_func);
3848 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003849 if (ret_pt->pt_argc > 0)
3850 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003851 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003852 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003853 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003854 pt->pt_argc = 0;
3855 else
3856 {
3857 pt->pt_argc = ret_pt->pt_argc;
3858 for (i = 0; i < pt->pt_argc; i++)
3859 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
3860 }
3861 }
3862 partial_unref(ret_pt);
3863 }
3864 rettv->v_type = VAR_PARTIAL;
3865 rettv->vval.v_partial = pt;
3866 }
3867 }
3868 return selfdict;
3869}
3870
3871/*
3872 * Return the name of the executed function.
3873 */
3874 char_u *
3875func_name(void *cookie)
3876{
3877 return ((funccall_T *)cookie)->func->uf_name;
3878}
3879
3880/*
3881 * Return the address holding the next breakpoint line for a funccall cookie.
3882 */
3883 linenr_T *
3884func_breakpoint(void *cookie)
3885{
3886 return &((funccall_T *)cookie)->breakpoint;
3887}
3888
3889/*
3890 * Return the address holding the debug tick for a funccall cookie.
3891 */
3892 int *
3893func_dbg_tick(void *cookie)
3894{
3895 return &((funccall_T *)cookie)->dbg_tick;
3896}
3897
3898/*
3899 * Return the nesting level for a funccall cookie.
3900 */
3901 int
3902func_level(void *cookie)
3903{
3904 return ((funccall_T *)cookie)->level;
3905}
3906
3907/*
3908 * Return TRUE when a function was ended by a ":return" command.
3909 */
3910 int
3911current_func_returned(void)
3912{
3913 return current_funccal->returned;
3914}
3915
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003916 int
3917free_unref_funccal(int copyID, int testing)
3918{
3919 int did_free = FALSE;
3920 int did_free_funccal = FALSE;
3921 funccall_T *fc, **pfc;
3922
3923 for (pfc = &previous_funccal; *pfc != NULL; )
3924 {
3925 if (can_free_funccal(*pfc, copyID))
3926 {
3927 fc = *pfc;
3928 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003929 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003930 did_free = TRUE;
3931 did_free_funccal = TRUE;
3932 }
3933 else
3934 pfc = &(*pfc)->caller;
3935 }
3936 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003937 // When a funccal was freed some more items might be garbage
3938 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003939 (void)garbage_collect(testing);
3940
3941 return did_free;
3942}
3943
3944/*
Bram Moolenaarba209902016-08-24 22:06:38 +02003945 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003946 */
3947 static funccall_T *
3948get_funccal(void)
3949{
3950 int i;
3951 funccall_T *funccal;
3952 funccall_T *temp_funccal;
3953
3954 funccal = current_funccal;
3955 if (debug_backtrace_level > 0)
3956 {
3957 for (i = 0; i < debug_backtrace_level; i++)
3958 {
3959 temp_funccal = funccal->caller;
3960 if (temp_funccal)
3961 funccal = temp_funccal;
3962 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003963 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003964 debug_backtrace_level = i;
3965 }
3966 }
3967 return funccal;
3968}
3969
3970/*
3971 * Return the hashtable used for local variables in the current funccal.
3972 * Return NULL if there is no current funccal.
3973 */
3974 hashtab_T *
3975get_funccal_local_ht()
3976{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003978 return NULL;
3979 return &get_funccal()->l_vars.dv_hashtab;
3980}
3981
3982/*
3983 * Return the l: scope variable.
3984 * Return NULL if there is no current funccal.
3985 */
3986 dictitem_T *
3987get_funccal_local_var()
3988{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003990 return NULL;
3991 return &get_funccal()->l_vars_var;
3992}
3993
3994/*
3995 * Return the hashtable used for argument in the current funccal.
3996 * Return NULL if there is no current funccal.
3997 */
3998 hashtab_T *
3999get_funccal_args_ht()
4000{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004002 return NULL;
4003 return &get_funccal()->l_avars.dv_hashtab;
4004}
4005
4006/*
4007 * Return the a: scope variable.
4008 * Return NULL if there is no current funccal.
4009 */
4010 dictitem_T *
4011get_funccal_args_var()
4012{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004014 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004015 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004016}
4017
4018/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004019 * List function variables, if there is a function.
4020 */
4021 void
4022list_func_vars(int *first)
4023{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004025 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004026 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004027}
4028
4029/*
4030 * If "ht" is the hashtable for local variables in the current funccal, return
4031 * the dict that contains it.
4032 * Otherwise return NULL.
4033 */
4034 dict_T *
4035get_current_funccal_dict(hashtab_T *ht)
4036{
4037 if (current_funccal != NULL
4038 && ht == &current_funccal->l_vars.dv_hashtab)
4039 return &current_funccal->l_vars;
4040 return NULL;
4041}
4042
4043/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004044 * Search hashitem in parent scope.
4045 */
4046 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004047find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004048{
4049 funccall_T *old_current_funccal = current_funccal;
4050 hashtab_T *ht;
4051 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004052 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004053
4054 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4055 return NULL;
4056
Bram Moolenaare38eab22019-12-05 21:50:01 +01004057 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004058 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004059 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004060 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004061 ht = find_var_ht(name, &varname);
4062 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004063 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004064 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004065 if (!HASHITEM_EMPTY(hi))
4066 {
4067 *pht = ht;
4068 break;
4069 }
4070 }
4071 if (current_funccal == current_funccal->func->uf_scoped)
4072 break;
4073 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004074 }
4075 current_funccal = old_current_funccal;
4076
4077 return hi;
4078}
4079
4080/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004081 * Search variable in parent scope.
4082 */
4083 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004084find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004085{
4086 dictitem_T *v = NULL;
4087 funccall_T *old_current_funccal = current_funccal;
4088 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004089 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004090
4091 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4092 return NULL;
4093
Bram Moolenaare38eab22019-12-05 21:50:01 +01004094 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004095 current_funccal = current_funccal->func->uf_scoped;
4096 while (current_funccal)
4097 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004098 ht = find_var_ht(name, &varname);
4099 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004100 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004101 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004102 if (v != NULL)
4103 break;
4104 }
4105 if (current_funccal == current_funccal->func->uf_scoped)
4106 break;
4107 current_funccal = current_funccal->func->uf_scoped;
4108 }
4109 current_funccal = old_current_funccal;
4110
4111 return v;
4112}
4113
4114/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004115 * Set "copyID + 1" in previous_funccal and callers.
4116 */
4117 int
4118set_ref_in_previous_funccal(int copyID)
4119{
4120 int abort = FALSE;
4121 funccall_T *fc;
4122
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004123 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004124 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004125 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004126 abort = abort
4127 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4128 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004129 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004130 }
4131 return abort;
4132}
4133
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004134 static int
4135set_ref_in_funccal(funccall_T *fc, int copyID)
4136{
4137 int abort = FALSE;
4138
4139 if (fc->fc_copyID != copyID)
4140 {
4141 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004142 abort = abort
4143 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4144 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004145 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004146 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004147 }
4148 return abort;
4149}
4150
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004151/*
4152 * Set "copyID" in all local vars and arguments in the call stack.
4153 */
4154 int
4155set_ref_in_call_stack(int copyID)
4156{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004157 int abort = FALSE;
4158 funccall_T *fc;
4159 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004160
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004161 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004162 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004163
4164 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004165 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
4166 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004167 abort = abort || set_ref_in_funccal(fc, copyID);
4168
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004169 return abort;
4170}
4171
4172/*
4173 * Set "copyID" in all functions available by name.
4174 */
4175 int
4176set_ref_in_functions(int copyID)
4177{
4178 int todo;
4179 hashitem_T *hi = NULL;
4180 int abort = FALSE;
4181 ufunc_T *fp;
4182
4183 todo = (int)func_hashtab.ht_used;
4184 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004185 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004186 if (!HASHITEM_EMPTY(hi))
4187 {
4188 --todo;
4189 fp = HI2UF(hi);
4190 if (!func_name_refcount(fp->uf_name))
4191 abort = abort || set_ref_in_func(NULL, fp, copyID);
4192 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004193 }
4194 return abort;
4195}
4196
4197/*
4198 * Set "copyID" in all function arguments.
4199 */
4200 int
4201set_ref_in_func_args(int copyID)
4202{
4203 int i;
4204 int abort = FALSE;
4205
4206 for (i = 0; i < funcargs.ga_len; ++i)
4207 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
4208 copyID, NULL, NULL);
4209 return abort;
4210}
4211
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004212/*
4213 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004214 * Returns TRUE if setting references failed somehow.
4215 */
4216 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004217set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004218{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004219 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004220 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01004221 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004222 char_u fname_buf[FLEN_FIXED + 1];
4223 char_u *tofree = NULL;
4224 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004225 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004226
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004227 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004228 return FALSE;
4229
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004230 if (fp_in == NULL)
4231 {
4232 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004234 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004235 if (fp != NULL)
4236 {
4237 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004238 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004239 }
4240 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004241 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004242}
4243
Bram Moolenaare38eab22019-12-05 21:50:01 +01004244#endif // FEAT_EVAL