blob: 01c97dedd4910c9b3ff1a62a22ddc7bc3c14433b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020017/*
18 * All user-defined functions are found in this hashtable.
19 */
20static hashtab_T func_hashtab;
21
Bram Moolenaare38eab22019-12-05 21:50:01 +010022// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020023static garray_T funcargs = GA_EMPTY;
24
Bram Moolenaar209b8e32019-03-14 13:43:24 +010025// pointer to funccal for currently active function
26static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaar209b8e32019-03-14 13:43:24 +010028// Pointer to list of previously used funccal, still around because some
29// item in it is still being used.
30static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020031
32static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
33static char *e_funcdict = N_("E717: Dictionary entry already exists");
34static char *e_funcref = N_("E718: Funcref required");
35static char *e_nofunc = N_("E130: Unknown function: %s");
36
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020037static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020038
39 void
40func_init()
41{
42 hash_init(&func_hashtab);
43}
44
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020045/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020046 * Return the function hash table
47 */
48 hashtab_T *
49func_tbl_get(void)
50{
51 return &func_hashtab;
52}
53
54/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020055 * Get one function argument.
56 * If "argtypes" is not NULL also get the type: "arg: type".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010057 * Return a pointer to after the type.
58 * When something is wrong return "arg".
59 */
60 static char_u *
61one_function_arg(char_u *arg, garray_T *newargs, garray_T *argtypes, int skip)
62{
Bram Moolenaar6e949782020-04-13 17:21:00 +020063 char_u *p = arg;
64 char_u *arg_copy = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010065
66 while (ASCII_ISALNUM(*p) || *p == '_')
67 ++p;
68 if (arg == p || isdigit(*arg)
69 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
70 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
71 {
72 if (!skip)
73 semsg(_("E125: Illegal argument: %s"), arg);
74 return arg;
75 }
76 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
77 return arg;
78 if (newargs != NULL)
79 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010080 int c;
81 int i;
82
83 c = *p;
84 *p = NUL;
85 arg_copy = vim_strsave(arg);
86 if (arg_copy == NULL)
87 {
88 *p = c;
89 return arg;
90 }
91
92 // Check for duplicate argument name.
93 for (i = 0; i < newargs->ga_len; ++i)
94 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
95 {
96 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
97 vim_free(arg_copy);
98 return arg;
99 }
100 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
101 newargs->ga_len++;
102
103 *p = c;
104 }
105
106 // get any type from "arg: type"
107 if (argtypes != NULL && ga_grow(argtypes, 1) == OK)
108 {
109 char_u *type = NULL;
110
Bram Moolenaar6e949782020-04-13 17:21:00 +0200111 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
112 {
113 semsg(_("E1059: No white space allowed before colon: %s"),
114 arg_copy == NULL ? arg : arg_copy);
115 p = skipwhite(p);
116 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100117 if (*p == ':')
118 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200119 ++p;
120 if (!VIM_ISWHITE(*p))
121 {
122 semsg(_(e_white_after), ":");
123 return arg;
124 }
125 type = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100126 p = skip_type(type);
127 type = vim_strnsave(type, p - type);
128 }
Bram Moolenaar6e949782020-04-13 17:21:00 +0200129 else if (*skipwhite(p) != '=')
130 {
131 semsg(_("E1077: Missing argument type for %s"),
132 arg_copy == NULL ? arg : arg_copy);
133 return arg;
134 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
136 }
137
138 return p;
139}
140
141/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200142 * Get function arguments.
143 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200145get_function_args(
146 char_u **argp,
147 char_u endchar,
148 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149 garray_T *argtypes, // NULL unless using :def
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200150 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200151 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200152 int skip,
153 exarg_T *eap,
154 char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200155{
156 int mustend = FALSE;
157 char_u *arg = *argp;
158 char_u *p = arg;
159 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200160 int any_default = FALSE;
161 char_u *expr;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200162 char_u *whitep = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200163
164 if (newargs != NULL)
165 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 if (argtypes != NULL)
167 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200168 if (default_args != NULL)
169 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200170
171 if (varargs != NULL)
172 *varargs = FALSE;
173
174 /*
175 * Isolate the arguments: "arg1, arg2, ...)"
176 */
177 while (*p != endchar)
178 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200179 while (eap != NULL && eap->getline != NULL
180 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200181 {
182 char_u *theline;
183
184 // End of the line, get the next one.
185 theline = eap->getline(':', eap->cookie, 0, TRUE);
186 if (theline == NULL)
187 break;
188 vim_free(*line_to_free);
189 *line_to_free = theline;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200190 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200191 p = skipwhite(theline);
192 }
193
194 if (mustend && *p != endchar)
195 {
196 if (!skip)
197 semsg(_(e_invarg2), *argp);
198 break;
199 }
200 if (*p == endchar)
201 break;
202
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200203 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
204 {
205 if (varargs != NULL)
206 *varargs = TRUE;
207 p += 3;
208 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100209
210 if (argtypes != NULL)
211 {
212 // ...name: list<type>
213 if (!ASCII_ISALPHA(*p))
214 {
215 emsg(_("E1055: Missing name after ..."));
216 break;
217 }
218
219 arg = p;
220 p = one_function_arg(p, newargs, argtypes, skip);
221 if (p == arg)
222 break;
223 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200224 }
225 else
226 {
227 arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 p = one_function_arg(p, newargs, argtypes, skip);
229 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200230 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200231
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200232 if (*skipwhite(p) == '=' && default_args != NULL)
233 {
234 typval_T rettv;
235
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100236 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200237 any_default = TRUE;
238 p = skipwhite(p) + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200239 whitep = p;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200240 p = skipwhite(p);
241 expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200242 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200243 {
244 if (ga_grow(default_args, 1) == FAIL)
245 goto err_ret;
246
247 // trim trailing whitespace
248 while (p > expr && VIM_ISWHITE(p[-1]))
249 p--;
250 c = *p;
251 *p = NUL;
252 expr = vim_strsave(expr);
253 if (expr == NULL)
254 {
255 *p = c;
256 goto err_ret;
257 }
258 ((char_u **)(default_args->ga_data))
259 [default_args->ga_len] = expr;
260 default_args->ga_len++;
261 *p = c;
262 }
263 else
264 mustend = TRUE;
265 }
266 else if (any_default)
267 {
268 emsg(_("E989: Non-default argument follows default argument"));
269 mustend = TRUE;
270 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200271 if (*p == ',')
272 ++p;
273 else
274 mustend = TRUE;
275 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200276 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200277 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200278 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200279
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200280 if (*p != endchar)
281 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100282 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200283
284 *argp = p;
285 return OK;
286
287err_ret:
288 if (newargs != NULL)
289 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200290 if (default_args != NULL)
291 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200292 return FAIL;
293}
294
295/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200296 * Register function "fp" as using "current_funccal" as its scope.
297 */
298 static int
299register_closure(ufunc_T *fp)
300{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200301 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100302 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200303 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200304 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200305 fp->uf_scoped = current_funccal;
306 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200307
Bram Moolenaar58016442016-07-31 18:30:22 +0200308 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
309 return FAIL;
310 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
311 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200312 return OK;
313}
314
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100315 static void
316set_ufunc_name(ufunc_T *fp, char_u *name)
317{
318 STRCPY(fp->uf_name, name);
319
320 if (name[0] == K_SPECIAL)
321 {
322 fp->uf_name_exp = alloc(STRLEN(name) + 3);
323 if (fp->uf_name_exp != NULL)
324 {
325 STRCPY(fp->uf_name_exp, "<SNR>");
326 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
327 }
328 }
329}
330
Bram Moolenaar58016442016-07-31 18:30:22 +0200331/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200332 * Get a name for a lambda. Returned in static memory.
333 */
334 char_u *
335get_lambda_name(void)
336{
337 static char_u name[30];
338 static int lambda_no = 0;
339
340 sprintf((char*)name, "<lambda>%d", ++lambda_no);
341 return name;
342}
343
Bram Moolenaar801ab062020-06-25 19:27:56 +0200344#if defined(FEAT_LUA) || defined(PROTO)
345/*
346 * Registers a native C callback which can be called from Vim script.
347 * Returns the name of the Vim script function.
348 */
349 char_u *
350register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
351{
352 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200353 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200354
355 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
356 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200357 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200358
359 fp->uf_dfunc_idx = UF_NOT_COMPILED;
360 fp->uf_refcount = 1;
361 fp->uf_varargs = TRUE;
362 fp->uf_flags = FC_CFUNC;
363 fp->uf_calls = 0;
364 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200365 fp->uf_cb = cb;
366 fp->uf_cb_free = cb_free;
367 fp->uf_cb_state = state;
368
369 set_ufunc_name(fp, name);
370 hash_add(&func_hashtab, UF2HIKEY(fp));
371
372 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200373}
374#endif
375
Bram Moolenaar04b12692020-05-04 23:24:44 +0200376/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200377 * Parse a lambda expression and get a Funcref from "*arg".
378 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
379 */
380 int
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200381get_lambda_tv(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200382{
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200383 int evaluate = evalarg != NULL
384 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200385 garray_T newargs;
386 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200387 garray_T *pnewargs;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200388 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100389 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200390 int varargs;
391 int ret;
Bram Moolenaar9215f012020-06-27 21:18:00 +0200392 char_u *start;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200393 char_u *s, *e;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200394 int *old_eval_lavars = eval_lavars_used;
395 int eval_lavars = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200396 char_u *tofree = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200397
398 ga_init(&newargs);
399 ga_init(&newlines);
400
Bram Moolenaare38eab22019-12-05 21:50:01 +0100401 // First, check if this is a lambda expression. "->" must exist.
Bram Moolenaar9215f012020-06-27 21:18:00 +0200402 start = skipwhite(*arg + 1);
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200403 ret = get_function_args(&start, '-', NULL, NULL, NULL, NULL, TRUE,
404 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200405 if (ret == FAIL || *start != '>')
406 return NOTDONE;
407
Bram Moolenaare38eab22019-12-05 21:50:01 +0100408 // Parse the arguments again.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200409 if (evaluate)
410 pnewargs = &newargs;
411 else
412 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200413 *arg = skipwhite(*arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 // TODO: argument types
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200415 ret = get_function_args(arg, '-', pnewargs, NULL, &varargs, NULL, FALSE,
416 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200417 if (ret == FAIL || **arg != '>')
418 goto errret;
419
Bram Moolenaare38eab22019-12-05 21:50:01 +0100420 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200421 if (evaluate)
422 eval_lavars_used = &eval_lavars;
423
Bram Moolenaare38eab22019-12-05 21:50:01 +0100424 // Get the start and the end of the expression.
Bram Moolenaar9215f012020-06-27 21:18:00 +0200425 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200426 s = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200427 ret = skip_expr_concatenate(&s, arg, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200428 if (ret == FAIL)
429 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200430 if (evalarg != NULL)
431 {
432 // avoid that the expression gets freed when another line break follows
433 tofree = evalarg->eval_tofree;
434 evalarg->eval_tofree = NULL;
435 }
436
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200437 e = *arg;
Bram Moolenaar9215f012020-06-27 21:18:00 +0200438 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200439 if (**arg != '}')
Bram Moolenaaree619e52020-03-28 21:38:06 +0100440 {
441 semsg(_("E451: Expected }: %s"), *arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200442 goto errret;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100443 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200444 ++*arg;
445
446 if (evaluate)
447 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200448 int len;
449 int flags = 0;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200450 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200451 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200452
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200453 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200454 if (fp == NULL)
455 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200456 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200457 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200458 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200459 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200460
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200461 ga_init2(&newlines, (int)sizeof(char_u *), 1);
462 if (ga_grow(&newlines, 1) == FAIL)
463 goto errret;
464
Bram Moolenaare38eab22019-12-05 21:50:01 +0100465 // Add "return " before the expression.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200466 len = 7 + (int)(e - s) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200467 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200468 if (p == NULL)
469 goto errret;
470 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
471 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200472 vim_strncpy(p + 7, s, e - s);
Bram Moolenaarf10806b2020-04-02 18:34:35 +0200473 if (strstr((char *)p + 7, "a:") == NULL)
474 // No a: variables are used for sure.
475 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200476
477 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100478 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200479 hash_add(&func_hashtab, UF2HIKEY(fp));
480 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200481 ga_init(&fp->uf_def_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200482 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200483 if (current_funccal != NULL && eval_lavars)
484 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200485 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200486 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200487 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200488 }
489 else
490 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200491
492#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200493 if (prof_def_func())
494 func_do_profile(fp);
495#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200496 if (sandbox)
497 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100498 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200499 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200500 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200501 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200502 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100503 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200504
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200505 pt->pt_func = fp;
506 pt->pt_refcount = 1;
507 rettv->vval.v_partial = pt;
508 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200509 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200510
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200511 eval_lavars_used = old_eval_lavars;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200512 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200513 return OK;
514
515errret:
516 ga_clear_strings(&newargs);
517 ga_clear_strings(&newlines);
518 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100519 vim_free(pt);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200520 vim_free(tofree);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200521 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200522 return FAIL;
523}
524
525/*
526 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
527 * name it contains, otherwise return "name".
528 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
529 * "partialp".
530 */
531 char_u *
532deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
533{
534 dictitem_T *v;
535 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200536 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200537
538 if (partialp != NULL)
539 *partialp = NULL;
540
541 cc = name[*lenp];
542 name[*lenp] = NUL;
543 v = find_var(name, NULL, no_autoload);
544 name[*lenp] = cc;
545 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
546 {
547 if (v->di_tv.vval.v_string == NULL)
548 {
549 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100550 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200551 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200552 s = v->di_tv.vval.v_string;
553 *lenp = (int)STRLEN(s);
554 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200555 }
556
557 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
558 {
559 partial_T *pt = v->di_tv.vval.v_partial;
560
561 if (pt == NULL)
562 {
563 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100564 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200565 }
566 if (partialp != NULL)
567 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200568 s = partial_name(pt);
569 *lenp = (int)STRLEN(s);
570 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200571 }
572
573 return name;
574}
575
576/*
577 * Give an error message with a function name. Handle <SNR> things.
578 * "ermsg" is to be passed without translation, use N_() instead of _().
579 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100580 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200581emsg_funcname(char *ermsg, char_u *name)
582{
583 char_u *p;
584
585 if (*name == K_SPECIAL)
586 p = concat_str((char_u *)"<SNR>", name + 3);
587 else
588 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100589 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200590 if (p != name)
591 vim_free(p);
592}
593
594/*
595 * Allocate a variable for the result of a function.
596 * Return OK or FAIL.
597 */
598 int
599get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200600 char_u *name, // name of the function
601 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200602 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200603 char_u **arg, // argument, pointing to the '('
Bram Moolenaare6b53242020-07-01 17:28:33 +0200604 evalarg_T *evalarg, // for line continuation
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200605 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200606{
607 char_u *argp;
608 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100609 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
610 int argcount = 0; // number of arguments found
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200611
612 /*
613 * Get the arguments.
614 */
615 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200616 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
617 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200618 {
Bram Moolenaare6b53242020-07-01 17:28:33 +0200619 // skip the '(' or ',' and possibly line breaks
620 argp = skipwhite_and_linebreak(argp + 1, evalarg);
621
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200622 if (*argp == ')' || *argp == ',' || *argp == NUL)
623 break;
Bram Moolenaare6b53242020-07-01 17:28:33 +0200624 if (eval1(&argp, &argvars[argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200625 {
626 ret = FAIL;
627 break;
628 }
629 ++argcount;
630 if (*argp != ',')
631 break;
632 }
Bram Moolenaare6b53242020-07-01 17:28:33 +0200633 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200634 if (*argp == ')')
635 ++argp;
636 else
637 ret = FAIL;
638
639 if (ret == OK)
640 {
641 int i = 0;
642
643 if (get_vim_var_nr(VV_TESTING))
644 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100645 // Prepare for calling test_garbagecollect_now(), need to know
646 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200647 if (funcargs.ga_itemsize == 0)
648 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
649 for (i = 0; i < argcount; ++i)
650 if (ga_grow(&funcargs, 1) == OK)
651 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
652 &argvars[i];
653 }
654
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200655 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200656
657 funcargs.ga_len -= i;
658 }
659 else if (!aborting())
660 {
661 if (argcount == MAX_FUNC_ARGS)
662 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
663 else
664 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
665 }
666
667 while (--argcount >= 0)
668 clear_tv(&argvars[argcount]);
669
670 *arg = skipwhite(argp);
671 return ret;
672}
673
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200674/*
675 * Return TRUE if "p" starts with "<SID>" or "s:".
676 * Only works if eval_fname_script() returned non-zero for "p"!
677 */
678 static int
679eval_fname_sid(char_u *p)
680{
681 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
682}
683
684/*
685 * In a script change <SID>name() and s:name() to K_SNR 123_name().
686 * Change <SNR>123_name() to K_SNR 123_name().
687 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
688 * (slow).
689 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100690 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200691fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
692{
693 int llen;
694 char_u *fname;
695 int i;
696
697 llen = eval_fname_script(name);
698 if (llen > 0)
699 {
700 fname_buf[0] = K_SPECIAL;
701 fname_buf[1] = KS_EXTRA;
702 fname_buf[2] = (int)KE_SNR;
703 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100704 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200705 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200706 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100707 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200708 else
709 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200710 sprintf((char *)fname_buf + 3, "%ld_",
711 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200712 i = (int)STRLEN(fname_buf);
713 }
714 }
715 if (i + STRLEN(name + llen) < FLEN_FIXED)
716 {
717 STRCPY(fname_buf + i, name + llen);
718 fname = fname_buf;
719 }
720 else
721 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200722 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200723 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100724 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200725 else
726 {
727 *tofree = fname;
728 mch_memmove(fname, fname_buf, (size_t)i);
729 STRCPY(fname + i, name + llen);
730 }
731 }
732 }
733 else
734 fname = name;
735 return fname;
736}
737
738/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100739 * Find a function "name" in script "sid".
740 */
741 static ufunc_T *
742find_func_with_sid(char_u *name, int sid)
743{
744 hashitem_T *hi;
745 char_u buffer[200];
746
747 buffer[0] = K_SPECIAL;
748 buffer[1] = KS_EXTRA;
749 buffer[2] = (int)KE_SNR;
750 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
751 (long)sid, name);
752 hi = hash_find(&func_hashtab, buffer);
753 if (!HASHITEM_EMPTY(hi))
754 return HI2UF(hi);
755
756 return NULL;
757}
758
759/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200760 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200761 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200762 * Return NULL for unknown function.
763 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 static ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200765find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200766{
767 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768 ufunc_T *func;
769 imported_T *imported;
770
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200771 if (!is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200773 char_u *after_script = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200775 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200777 // Find script-local function before global one.
778 func = find_func_with_sid(name, current_sctx.sc_sid);
779 if (func != NULL)
780 return func;
781 }
782
783 if (!in_vim9script()
784 && name[0] == K_SPECIAL
785 && name[1] == KS_EXTRA
786 && name[2] == KE_SNR)
787 {
788 long sid;
789
790 // Caller changes s: to <SNR>99_name.
791
792 after_script = name + 3;
793 sid = getdigits(&after_script);
794 if (sid == current_sctx.sc_sid && *after_script == '_')
795 ++after_script;
796 else
797 after_script = NULL;
798 }
799 if (in_vim9script() || after_script != NULL)
800 {
801 // Find imported function before global one.
802 imported = find_imported(
803 after_script == NULL ? name : after_script, 0, cctx);
804 if (imported != NULL && imported->imp_funcname != NULL)
805 {
806 hi = hash_find(&func_hashtab, imported->imp_funcname);
807 if (!HASHITEM_EMPTY(hi))
808 return HI2UF(hi);
809 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 }
811 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200812
Bram Moolenaara26b9702020-04-18 19:53:28 +0200813 hi = hash_find(&func_hashtab,
814 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200815 if (!HASHITEM_EMPTY(hi))
816 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817
818 return NULL;
819}
820
821/*
822 * Find a function by name, return pointer to it in ufuncs.
823 * "cctx" is passed in a :def function to find imported functions.
824 * Return NULL for unknown or dead function.
825 */
826 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200827find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100828{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200829 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830
831 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
832 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200833 return NULL;
834}
835
836/*
837 * Copy the function name of "fp" to buffer "buf".
838 * "buf" must be able to hold the function name plus three bytes.
839 * Takes care of script-local function names.
840 */
841 static void
842cat_func_name(char_u *buf, ufunc_T *fp)
843{
844 if (fp->uf_name[0] == K_SPECIAL)
845 {
846 STRCPY(buf, "<SNR>");
847 STRCAT(buf, fp->uf_name + 3);
848 }
849 else
850 STRCPY(buf, fp->uf_name);
851}
852
853/*
854 * Add a number variable "name" to dict "dp" with value "nr".
855 */
856 static void
857add_nr_var(
858 dict_T *dp,
859 dictitem_T *v,
860 char *name,
861 varnumber_T nr)
862{
863 STRCPY(v->di_key, name);
864 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
865 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
866 v->di_tv.v_type = VAR_NUMBER;
867 v->di_tv.v_lock = VAR_FIXED;
868 v->di_tv.vval.v_number = nr;
869}
870
871/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100872 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200873 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100874 static void
875free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200876{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100877 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200878
879 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
880 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100881 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200882
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100883 // When garbage collecting a funccall_T may be freed before the
884 // function that references it, clear its uf_scoped field.
885 // The function may have been redefined and point to another
886 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200887 if (fp != NULL && fp->uf_scoped == fc)
888 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200889 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200890 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200891
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200892 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200893 vim_free(fc);
894}
895
896/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100897 * Free "fc" and what it contains.
898 * Can be called only when "fc" is kept beyond the period of it called,
899 * i.e. after cleanup_function_call(fc).
900 */
901 static void
902free_funccal_contents(funccall_T *fc)
903{
904 listitem_T *li;
905
906 // Free all l: variables.
907 vars_clear(&fc->l_vars.dv_hashtab);
908
909 // Free all a: variables.
910 vars_clear(&fc->l_avars.dv_hashtab);
911
912 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200913 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100914 clear_tv(&li->li_tv);
915
916 free_funccal(fc);
917}
918
919/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200920 * Handle the last part of returning from a function: free the local hashtable.
921 * Unless it is still in use by a closure.
922 */
923 static void
924cleanup_function_call(funccall_T *fc)
925{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100926 int may_free_fc = fc->fc_refcount <= 0;
927 int free_fc = TRUE;
928
Bram Moolenaar6914c642017-04-01 21:21:30 +0200929 current_funccal = fc->caller;
930
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100931 // Free all l: variables if not referred.
932 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
933 vars_clear(&fc->l_vars.dv_hashtab);
934 else
935 free_fc = FALSE;
936
937 // If the a:000 list and the l: and a: dicts are not referenced and
938 // there is no closure using it, we can free the funccall_T and what's
939 // in it.
940 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
941 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200942 else
943 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100944 int todo;
945 hashitem_T *hi;
946 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200947
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100948 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200949
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100950 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200951 todo = (int)fc->l_avars.dv_hashtab.ht_used;
952 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
953 {
954 if (!HASHITEM_EMPTY(hi))
955 {
956 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100957 di = HI2DI(hi);
958 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200959 }
960 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100961 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200962
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100963 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
964 fc->l_varlist.lv_first = NULL;
965 else
966 {
967 listitem_T *li;
968
969 free_fc = FALSE;
970
971 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200972 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +0200973 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100974 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100975
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100976 if (free_fc)
977 free_funccal(fc);
978 else
979 {
980 static int made_copy = 0;
981
982 // "fc" is still in use. This can happen when returning "a:000",
983 // assigning "l:" to a global variable or defining a closure.
984 // Link "fc" in the list for garbage collection later.
985 fc->caller = previous_funccal;
986 previous_funccal = fc;
987
988 if (want_garbage_collect)
989 // If garbage collector is ready, clear count.
990 made_copy = 0;
991 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100992 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100993 // We have made a lot of copies, worth 4 Mbyte. This can happen
994 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100995 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100996 // too much memory.
997 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100998 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100999 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001000 }
1001}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002/*
1003 * Unreference "fc": decrement the reference count and free it when it
1004 * becomes zero. "fp" is detached from "fc".
1005 * When "force" is TRUE we are exiting.
1006 */
1007 static void
1008funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
1009{
1010 funccall_T **pfc;
1011 int i;
1012
1013 if (fc == NULL)
1014 return;
1015
1016 if (--fc->fc_refcount <= 0 && (force || (
1017 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
1018 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
1019 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
1020 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
1021 {
1022 if (fc == *pfc)
1023 {
1024 *pfc = fc->caller;
1025 free_funccal_contents(fc);
1026 return;
1027 }
1028 }
1029 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1030 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
1031 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
1032}
1033
1034/*
1035 * Remove the function from the function hashtable. If the function was
1036 * deleted while it still has references this was already done.
1037 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
1038 */
1039 static int
1040func_remove(ufunc_T *fp)
1041{
1042 hashitem_T *hi;
1043
1044 // Return if it was already virtually deleted.
1045 if (fp->uf_flags & FC_DEAD)
1046 return FALSE;
1047
1048 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1049 if (!HASHITEM_EMPTY(hi))
1050 {
1051 // When there is a def-function index do not actually remove the
1052 // function, so we can find the index when defining the function again.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001053 if (fp->uf_def_status == UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001054 fp->uf_flags |= FC_DEAD;
1055 else
1056 hash_remove(&func_hashtab, hi);
1057 return TRUE;
1058 }
1059 return FALSE;
1060}
1061
1062 static void
1063func_clear_items(ufunc_T *fp)
1064{
1065 ga_clear_strings(&(fp->uf_args));
1066 ga_clear_strings(&(fp->uf_def_args));
1067 ga_clear_strings(&(fp->uf_lines));
1068 VIM_CLEAR(fp->uf_name_exp);
1069 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001070 VIM_CLEAR(fp->uf_def_arg_idx);
1071 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02001072 clear_type_list(&fp->uf_type_list);
Bram Moolenaar801ab062020-06-25 19:27:56 +02001073
1074#ifdef FEAT_LUA
1075 if (fp->uf_cb_free != NULL)
1076 {
1077 fp->uf_cb_free(fp->uf_cb_state);
1078 fp->uf_cb_free = NULL;
1079 }
1080
1081 fp->uf_cb_state = NULL;
1082 fp->uf_cb = NULL;
1083#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084#ifdef FEAT_PROFILE
1085 VIM_CLEAR(fp->uf_tml_count);
1086 VIM_CLEAR(fp->uf_tml_total);
1087 VIM_CLEAR(fp->uf_tml_self);
1088#endif
1089}
1090
1091/*
1092 * Free all things that a function contains. Does not free the function
1093 * itself, use func_free() for that.
1094 * When "force" is TRUE we are exiting.
1095 */
1096 static void
1097func_clear(ufunc_T *fp, int force)
1098{
1099 if (fp->uf_cleared)
1100 return;
1101 fp->uf_cleared = TRUE;
1102
1103 // clear this function
1104 func_clear_items(fp);
1105 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001106 clear_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001107}
1108
1109/*
1110 * Free a function and remove it from the list of functions. Does not free
1111 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001112 * When "force" is TRUE we are exiting.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113 */
1114 static void
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001115func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116{
1117 // Only remove it when not done already, otherwise we would remove a newer
1118 // version of the function with the same name.
1119 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1120 func_remove(fp);
1121
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001122 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123 vim_free(fp);
1124}
1125
1126/*
1127 * Free all things that a function contains and free the function itself.
1128 * When "force" is TRUE we are exiting.
1129 */
1130 static void
1131func_clear_free(ufunc_T *fp, int force)
1132{
1133 func_clear(fp, force);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001134 if (force || fp->uf_dfunc_idx == 0)
1135 func_free(fp, force);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001136}
1137
Bram Moolenaar6914c642017-04-01 21:21:30 +02001138
1139/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001140 * Call a user function.
1141 */
1142 static void
1143call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001144 ufunc_T *fp, // pointer to function
1145 int argcount, // nr of args
1146 typval_T *argvars, // arguments
1147 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001148 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01001149 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001150{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001151 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001152 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001153 funccall_T *fc;
1154 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001155 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001156 static int depth = 0;
1157 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001158 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001159 int i;
1160 int ai;
1161 int islambda = FALSE;
1162 char_u numbuf[NUMBUFLEN];
1163 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001164#ifdef FEAT_PROFILE
1165 proftime_T wait_start;
1166 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001167 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001168#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001169 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001170
Bram Moolenaare38eab22019-12-05 21:50:01 +01001171 // If depth of calling is getting too high, don't execute the function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001172 if (depth >= p_mfd)
1173 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001174 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001175 rettv->v_type = VAR_NUMBER;
1176 rettv->vval.v_number = -1;
1177 return;
1178 }
1179 ++depth;
1180
Bram Moolenaare38eab22019-12-05 21:50:01 +01001181 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001182
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001183 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001184 if (fc == NULL)
1185 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001186 fc->caller = current_funccal;
1187 current_funccal = fc;
1188 fc->func = fp;
1189 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001190 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001191 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001192 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1193 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001194 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001195 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001196 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001197
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001198 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199 {
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001200 estack_push_ufunc(fp, 1);
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001201 save_current_sctx = current_sctx;
1202 current_sctx = fp->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001204 // Execute the function, possibly compiling it first.
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001205 call_def_function(fp, argcount, argvars, funcexe->partial, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 --depth;
1207 current_funccal = fc->caller;
1208
1209 estack_pop();
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001210 current_sctx = save_current_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211 free_funccal(fc);
1212 return;
1213 }
1214
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001215 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1216 islambda = TRUE;
1217
1218 /*
1219 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1220 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1221 * each argument variable and saves a lot of time.
1222 */
1223 /*
1224 * Init l: variables.
1225 */
1226 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1227 if (selfdict != NULL)
1228 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001229 // Set l:self to "selfdict". Use "name" to avoid a warning from
1230 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001231 v = &fc->fixvar[fixvar_idx++].var;
1232 name = v->di_key;
1233 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001234 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001235 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1236 v->di_tv.v_type = VAR_DICT;
1237 v->di_tv.v_lock = 0;
1238 v->di_tv.vval.v_dict = selfdict;
1239 ++selfdict->dv_refcount;
1240 }
1241
1242 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001243 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001244 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001245 * Set a:000 to a list with room for the "..." arguments.
1246 */
1247 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001248 if ((fp->uf_flags & FC_NOARGS) == 0)
1249 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001250 (varnumber_T)(argcount >= fp->uf_args.ga_len
1251 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001252 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001253 if ((fp->uf_flags & FC_NOARGS) == 0)
1254 {
1255 // Use "name" to avoid a warning from some compiler that checks the
1256 // destination size.
1257 v = &fc->fixvar[fixvar_idx++].var;
1258 name = v->di_key;
1259 STRCPY(name, "000");
1260 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1261 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1262 v->di_tv.v_type = VAR_LIST;
1263 v->di_tv.v_lock = VAR_FIXED;
1264 v->di_tv.vval.v_list = &fc->l_varlist;
1265 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001266 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001267 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1268 fc->l_varlist.lv_lock = VAR_FIXED;
1269
1270 /*
1271 * Set a:firstline to "firstline" and a:lastline to "lastline".
1272 * Set a:name to named arguments.
1273 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001274 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001275 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001276 if ((fp->uf_flags & FC_NOARGS) == 0)
1277 {
1278 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001279 (varnumber_T)funcexe->firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001280 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001281 (varnumber_T)funcexe->lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001282 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001283 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001284 {
1285 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001286 typval_T def_rettv;
1287 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001288
1289 ai = i - fp->uf_args.ga_len;
1290 if (ai < 0)
1291 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001292 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001293 name = FUNCARG(fp, i);
1294 if (islambda)
1295 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001296
1297 // evaluate named argument default expression
1298 isdefault = ai + fp->uf_def_args.ga_len >= 0
1299 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1300 && argvars[i].vval.v_number == VVAL_NONE));
1301 if (isdefault)
1302 {
1303 char_u *default_expr = NULL;
1304 def_rettv.v_type = VAR_NUMBER;
1305 def_rettv.vval.v_number = -1;
1306
1307 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1308 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001309 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001310 {
1311 default_arg_err = 1;
1312 break;
1313 }
1314 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001315 }
1316 else
1317 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001318 if ((fp->uf_flags & FC_NOARGS) != 0)
1319 // Bail out if no a: arguments used (in lambda).
1320 break;
1321
Bram Moolenaare38eab22019-12-05 21:50:01 +01001322 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001323 sprintf((char *)numbuf, "%d", ai + 1);
1324 name = numbuf;
1325 }
1326 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1327 {
1328 v = &fc->fixvar[fixvar_idx++].var;
1329 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001330 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001331 }
1332 else
1333 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001334 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001335 if (v == NULL)
1336 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001337 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001338 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001339
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001340 // Note: the values are copied directly to avoid alloc/free.
1341 // "argvars" must have VAR_FIXED for v_lock.
1342 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001343 v->di_tv.v_lock = VAR_FIXED;
1344
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001345 if (addlocal)
1346 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001347 // Named arguments should be accessed without the "a:" prefix in
1348 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001349 copy_tv(&v->di_tv, &v->di_tv);
1350 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001351 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001352 else
1353 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001354
1355 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1356 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001357 listitem_T *li = &fc->l_listitems[ai];
1358
1359 li->li_tv = argvars[i];
1360 li->li_tv.v_lock = VAR_FIXED;
1361 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001362 }
1363 }
1364
Bram Moolenaare38eab22019-12-05 21:50:01 +01001365 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001366 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001367
1368 if (fp->uf_flags & FC_SANDBOX)
1369 {
1370 using_sandbox = TRUE;
1371 ++sandbox;
1372 }
1373
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001374 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001375 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001376 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001377 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001378 ++no_wait_return;
1379 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001380
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001381 smsg(_("calling %s"), SOURCING_NAME);
1382 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001383 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001384 char_u buf[MSG_BUF_LEN];
1385 char_u numbuf2[NUMBUFLEN];
1386 char_u *tofree;
1387 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001388
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001389 msg_puts("(");
1390 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001391 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001392 if (i > 0)
1393 msg_puts(", ");
1394 if (argvars[i].v_type == VAR_NUMBER)
1395 msg_outnum((long)argvars[i].vval.v_number);
1396 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001397 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001398 // Do not want errors such as E724 here.
1399 ++emsg_off;
1400 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1401 --emsg_off;
1402 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001403 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001404 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001405 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001406 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1407 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001408 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001409 msg_puts((char *)s);
1410 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001411 }
1412 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001413 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001414 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001415 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001416 msg_puts("\n"); // don't overwrite this either
1417
1418 verbose_leave_scroll();
1419 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001420 }
1421#ifdef FEAT_PROFILE
1422 if (do_profiling == PROF_YES)
1423 {
1424 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001425 {
1426 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001427 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001428 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001429 if (fp->uf_profiling
1430 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1431 {
1432 ++fp->uf_tm_count;
1433 profile_start(&call_start);
1434 profile_zero(&fp->uf_tm_children);
1435 }
1436 script_prof_save(&wait_start);
1437 }
1438#endif
1439
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001440 save_current_sctx = current_sctx;
1441 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001442 save_did_emsg = did_emsg;
1443 did_emsg = FALSE;
1444
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001445 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1446 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001447 else if (islambda)
1448 {
1449 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1450
1451 // A Lambda always has the command "return {expr}". It is much faster
1452 // to evaluate {expr} directly.
1453 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001454 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001455 --ex_nesting_level;
1456 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001457 else
1458 // call do_cmdline() to execute the lines
1459 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001460 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1461
1462 --RedrawingDisabled;
1463
Bram Moolenaare38eab22019-12-05 21:50:01 +01001464 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001465 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1466 {
1467 clear_tv(rettv);
1468 rettv->v_type = VAR_NUMBER;
1469 rettv->vval.v_number = -1;
1470 }
1471
1472#ifdef FEAT_PROFILE
1473 if (do_profiling == PROF_YES && (fp->uf_profiling
1474 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1475 {
1476 profile_end(&call_start);
1477 profile_sub_wait(&wait_start, &call_start);
1478 profile_add(&fp->uf_tm_total, &call_start);
1479 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1480 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1481 {
1482 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1483 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1484 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001485 if (started_profiling)
1486 // make a ":profdel func" stop profiling the function
1487 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001488 }
1489#endif
1490
Bram Moolenaare38eab22019-12-05 21:50:01 +01001491 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001492 if (p_verbose >= 12)
1493 {
1494 ++no_wait_return;
1495 verbose_enter_scroll();
1496
1497 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001498 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001499 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001500 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001501 (long)fc->rettv->vval.v_number);
1502 else
1503 {
1504 char_u buf[MSG_BUF_LEN];
1505 char_u numbuf2[NUMBUFLEN];
1506 char_u *tofree;
1507 char_u *s;
1508
Bram Moolenaare38eab22019-12-05 21:50:01 +01001509 // The value may be very long. Skip the middle part, so that we
1510 // have some idea how it starts and ends. smsg() would always
1511 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001512 ++emsg_off;
1513 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1514 --emsg_off;
1515 if (s != NULL)
1516 {
1517 if (vim_strsize(s) > MSG_BUF_CLEN)
1518 {
1519 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1520 s = buf;
1521 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001522 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001523 vim_free(tofree);
1524 }
1525 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001526 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001527
1528 verbose_leave_scroll();
1529 --no_wait_return;
1530 }
1531
Bram Moolenaare31ee862020-01-07 20:59:34 +01001532 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001533 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001534 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001535#ifdef FEAT_PROFILE
1536 if (do_profiling == PROF_YES)
1537 script_prof_restore(&wait_start);
1538#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001539 if (using_sandbox)
1540 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001541
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001542 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001543 {
1544 ++no_wait_return;
1545 verbose_enter_scroll();
1546
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001547 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001548 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001549
1550 verbose_leave_scroll();
1551 --no_wait_return;
1552 }
1553
1554 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001555 --depth;
1556
Bram Moolenaar6914c642017-04-01 21:21:30 +02001557 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001558}
1559
1560/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001562 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 int
1564call_user_func_check(
1565 ufunc_T *fp,
1566 int argcount,
1567 typval_T *argvars,
1568 typval_T *rettv,
1569 funcexe_T *funcexe,
1570 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001571{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 int error;
1573 int regular_args = fp->uf_args.ga_len;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1576 *funcexe->doesrange = TRUE;
1577 if (argcount < regular_args - fp->uf_def_args.ga_len)
1578 error = FCERR_TOOFEW;
1579 else if (!has_varargs(fp) && argcount > regular_args)
1580 error = FCERR_TOOMANY;
1581 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1582 error = FCERR_DICT;
1583 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001584 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 int did_save_redo = FALSE;
1586 save_redo_T save_redo;
1587
1588 /*
1589 * Call the user function.
1590 * Save and restore search patterns, script variables and
1591 * redo buffer.
1592 */
1593 save_search_patterns();
1594 if (!ins_compl_active())
1595 {
1596 saveRedobuff(&save_redo);
1597 did_save_redo = TRUE;
1598 }
1599 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001600 call_user_func(fp, argcount, argvars, rettv, funcexe,
1601 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1603 // Function was unreferenced while being used, free it now.
1604 func_clear_free(fp, FALSE);
1605 if (did_save_redo)
1606 restoreRedobuff(&save_redo);
1607 restore_search_patterns();
1608 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001609 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001611}
1612
1613/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001614 * There are two kinds of function names:
1615 * 1. ordinary names, function defined with :function
1616 * 2. numbered functions and lambdas
1617 * For the first we only count the name stored in func_hashtab as a reference,
1618 * using function() does not count as a reference, because the function is
1619 * looked up by name.
1620 */
1621 static int
1622func_name_refcount(char_u *name)
1623{
1624 return isdigit(*name) || *name == '<';
1625}
1626
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001627static funccal_entry_T *funccal_stack = NULL;
1628
1629/*
1630 * Save the current function call pointer, and set it to NULL.
1631 * Used when executing autocommands and for ":source".
1632 */
1633 void
1634save_funccal(funccal_entry_T *entry)
1635{
1636 entry->top_funccal = current_funccal;
1637 entry->next = funccal_stack;
1638 funccal_stack = entry;
1639 current_funccal = NULL;
1640}
1641
1642 void
1643restore_funccal(void)
1644{
1645 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001646 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001647 else
1648 {
1649 current_funccal = funccal_stack->top_funccal;
1650 funccal_stack = funccal_stack->next;
1651 }
1652}
1653
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001654 funccall_T *
1655get_current_funccal(void)
1656{
1657 return current_funccal;
1658}
1659
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001660/*
1661 * Mark all functions of script "sid" as deleted.
1662 */
1663 void
1664delete_script_functions(int sid)
1665{
1666 hashitem_T *hi;
1667 ufunc_T *fp;
1668 long_u todo;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02001669 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001670 size_t len;
1671
1672 buf[0] = K_SPECIAL;
1673 buf[1] = KS_EXTRA;
1674 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02001675 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001676 len = STRLEN(buf);
1677
1678 todo = func_hashtab.ht_used;
1679 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1680 if (!HASHITEM_EMPTY(hi))
1681 {
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001682 fp = HI2UF(hi);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001683 if (STRNCMP(fp->uf_name, buf, len) == 0)
1684 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001685 fp->uf_flags |= FC_DEAD;
1686 func_clear(fp, TRUE);
1687 }
1688 --todo;
1689 }
1690}
1691
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001692#if defined(EXITFREE) || defined(PROTO)
1693 void
1694free_all_functions(void)
1695{
1696 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001697 ufunc_T *fp;
1698 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001699 long_u todo = 1;
1700 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001701
Bram Moolenaare38eab22019-12-05 21:50:01 +01001702 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001703 while (current_funccal != NULL)
1704 {
1705 clear_tv(current_funccal->rettv);
1706 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001707 if (current_funccal == NULL && funccal_stack != NULL)
1708 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001709 }
1710
Bram Moolenaare38eab22019-12-05 21:50:01 +01001711 // First clear what the functions contain. Since this may lower the
1712 // reference count of a function, it may also free a function and change
1713 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001714 while (todo > 0)
1715 {
1716 todo = func_hashtab.ht_used;
1717 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1718 if (!HASHITEM_EMPTY(hi))
1719 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 // clear the def function index now
1721 fp = HI2UF(hi);
1722 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001723 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724
Bram Moolenaare38eab22019-12-05 21:50:01 +01001725 // Only free functions that are not refcounted, those are
1726 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001727 if (func_name_refcount(fp->uf_name))
1728 ++skipped;
1729 else
1730 {
1731 used = func_hashtab.ht_used;
1732 func_clear(fp, TRUE);
1733 if (used != func_hashtab.ht_used)
1734 {
1735 skipped = 0;
1736 break;
1737 }
1738 }
1739 --todo;
1740 }
1741 }
1742
Bram Moolenaare38eab22019-12-05 21:50:01 +01001743 // Now actually free the functions. Need to start all over every time,
1744 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001745 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001746 while (func_hashtab.ht_used > skipped)
1747 {
1748 todo = func_hashtab.ht_used;
1749 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001750 if (!HASHITEM_EMPTY(hi))
1751 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001752 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001753 // Only free functions that are not refcounted, those are
1754 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02001755 fp = HI2UF(hi);
1756 if (func_name_refcount(fp->uf_name))
1757 ++skipped;
1758 else
1759 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001760 func_free(fp, FALSE);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001761 skipped = 0;
1762 break;
1763 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001764 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001765 }
1766 if (skipped == 0)
1767 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768
1769 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001770}
1771#endif
1772
1773/*
1774 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02001775 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001776 * "len" is the length of "name", or -1 for NUL terminated.
1777 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001779builtin_function(char_u *name, int len)
1780{
1781 char_u *p;
1782
Bram Moolenaara26b9702020-04-18 19:53:28 +02001783 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001784 return FALSE;
1785 p = vim_strchr(name, AUTOLOAD_CHAR);
1786 return p == NULL || (len > 0 && p > name + len);
1787}
1788
1789 int
1790func_call(
1791 char_u *name,
1792 typval_T *args,
1793 partial_T *partial,
1794 dict_T *selfdict,
1795 typval_T *rettv)
1796{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001797 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001798 listitem_T *item;
1799 typval_T argv[MAX_FUNC_ARGS + 1];
1800 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001801 int r = 0;
1802
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001803 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001804 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805 {
1806 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1807 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001808 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001809 break;
1810 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001811 // Make a copy of each argument. This is needed to be able to set
1812 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001813 copy_tv(&item->li_tv, &argv[argc++]);
1814 }
1815
1816 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001817 {
1818 funcexe_T funcexe;
1819
Bram Moolenaara80faa82020-04-12 19:37:17 +02001820 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001821 funcexe.firstline = curwin->w_cursor.lnum;
1822 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001823 funcexe.evaluate = TRUE;
1824 funcexe.partial = partial;
1825 funcexe.selfdict = selfdict;
1826 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1827 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001828
Bram Moolenaare38eab22019-12-05 21:50:01 +01001829 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001830 while (argc > 0)
1831 clear_tv(&argv[--argc]);
1832
1833 return r;
1834}
1835
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001836static int callback_depth = 0;
1837
1838 int
1839get_callback_depth(void)
1840{
1841 return callback_depth;
1842}
1843
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001844/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001845 * Invoke call_func() with a callback.
1846 */
1847 int
1848call_callback(
1849 callback_T *callback,
1850 int len, // length of "name" or -1 to use strlen()
1851 typval_T *rettv, // return value goes here
1852 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001853 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001854 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001855{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001856 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001857 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001858
Bram Moolenaara80faa82020-04-12 19:37:17 +02001859 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001860 funcexe.evaluate = TRUE;
1861 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001862 ++callback_depth;
1863 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1864 --callback_depth;
1865 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001866}
1867
1868/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869 * Give an error message for the result of a function.
1870 * Nothing if "error" is FCERR_NONE.
1871 */
1872 void
1873user_func_error(int error, char_u *name)
1874{
1875 switch (error)
1876 {
1877 case FCERR_UNKNOWN:
1878 emsg_funcname(e_unknownfunc, name);
1879 break;
1880 case FCERR_NOTMETHOD:
1881 emsg_funcname(
1882 N_("E276: Cannot use function as a method: %s"), name);
1883 break;
1884 case FCERR_DELETED:
1885 emsg_funcname(N_(e_func_deleted), name);
1886 break;
1887 case FCERR_TOOMANY:
1888 emsg_funcname((char *)e_toomanyarg, name);
1889 break;
1890 case FCERR_TOOFEW:
1891 emsg_funcname((char *)e_toofewarg, name);
1892 break;
1893 case FCERR_SCRIPT:
1894 emsg_funcname(
1895 N_("E120: Using <SID> not in a script context: %s"), name);
1896 break;
1897 case FCERR_DICT:
1898 emsg_funcname(
1899 N_("E725: Calling dict function without Dictionary: %s"),
1900 name);
1901 break;
1902 }
1903}
1904
1905/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001906 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001907 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001908 * Return FAIL when the function can't be called, OK otherwise.
1909 * Also returns OK when an error was encountered while executing the function.
1910 */
1911 int
1912call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001913 char_u *funcname, // name of the function
1914 int len, // length of "name" or -1 to use strlen()
1915 typval_T *rettv, // return value goes here
1916 int argcount_in, // number of "argvars"
1917 typval_T *argvars_in, // vars for arguments, must have "argcount"
1918 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001919 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001920{
1921 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01001922 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001923 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001924 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001925 char_u fname_buf[FLEN_FIXED + 1];
1926 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001927 char_u *fname = NULL;
1928 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001929 int argcount = argcount_in;
1930 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001931 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001932 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1933 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001934 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001935 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001936 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001937
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001938 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1939 // even when call_func() returns FAIL.
1940 rettv->v_type = VAR_UNKNOWN;
1941
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001942 if (partial != NULL)
1943 fp = partial->pt_func;
1944 if (fp == NULL)
1945 {
1946 // Make a copy of the name, if it comes from a funcref variable it
1947 // could be changed or deleted in the called function.
1948 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
1949 if (name == NULL)
1950 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001951
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001952 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1953 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001954
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001955 if (funcexe->doesrange != NULL)
1956 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001957
1958 if (partial != NULL)
1959 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001960 // When the function has a partial with a dict and there is a dict
1961 // argument, use the dict argument. That is backwards compatible.
1962 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001963 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001964 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01001965 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001966 {
1967 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001968 {
1969 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
1970 {
Bram Moolenaaref140542019-12-31 21:27:13 +01001971 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001972 goto theend;
1973 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001974 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001975 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001976 for (i = 0; i < argcount_in; ++i)
1977 argv[i + argv_clear] = argvars_in[i];
1978 argvars = argv;
1979 argcount = partial->pt_argc + argcount_in;
1980 }
1981 }
1982
Bram Moolenaaref140542019-12-31 21:27:13 +01001983 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001984 {
1985 char_u *rfname = fname;
1986
Bram Moolenaare38eab22019-12-05 21:50:01 +01001987 // Ignore "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001988 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001989 rfname = fname + 2;
1990
Bram Moolenaare38eab22019-12-05 21:50:01 +01001991 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001992 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01001993 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001994
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001995 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001996 {
1997 /*
1998 * User defined function.
1999 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002000 if (fp == NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002001 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002002
Bram Moolenaare38eab22019-12-05 21:50:01 +01002003 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002004 if (fp == NULL
2005 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002006 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002007 && !aborting())
2008 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002009 // executed an autocommand, search for the function again
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002010 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002011 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002012 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002013 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
2014 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002015 // loaded a package, search for the function again
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002016 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002017 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02002018 if (fp == NULL)
2019 {
2020 char_u *p = untrans_function_name(rfname);
2021
2022 // If using Vim9 script try not local to the script.
2023 // TODO: should not do this if the name started with "s:".
2024 if (p != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002025 fp = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002026 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002027
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002028 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01002029 error = FCERR_DELETED;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002030#ifdef FEAT_LUA
2031 else if (fp != NULL && (fp->uf_flags & FC_CFUNC))
2032 {
2033 cfunc_T cb = fp->uf_cb;
2034
2035 error = (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
2036 }
2037#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002038 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002039 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002040 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01002041 // postponed filling in the arguments, do it now
2042 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002043 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002044
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002045 if (funcexe->basetv != NULL)
2046 {
2047 // Method call: base->Method()
2048 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
2049 argv[0] = *funcexe->basetv;
2050 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002051 argvars = argv;
2052 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002053 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002054
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002055 error = call_user_func_check(fp, argcount, argvars, rettv,
2056 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002057 }
2058 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002059 else if (funcexe->basetv != NULL)
2060 {
2061 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002062 * expr->method(): Find the method name in the table, call its
2063 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02002064 */
2065 error = call_internal_method(fname, argcount, argvars, rettv,
2066 funcexe->basetv);
2067 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002068 else
2069 {
2070 /*
2071 * Find the function name in the table, call its implementation.
2072 */
2073 error = call_internal_func(fname, argcount, argvars, rettv);
2074 }
2075 /*
2076 * The function call (or "FuncUndefined" autocommand sequence) might
2077 * have been aborted by an error, an interrupt, or an explicitly thrown
2078 * exception that has not been caught so far. This situation can be
2079 * tested for by calling aborting(). For an error in an internal
2080 * function or for the "E132" error in call_user_func(), however, the
2081 * throw point at which the "force_abort" flag (temporarily reset by
2082 * emsg()) is normally updated has not been reached yet. We need to
2083 * update that flag first to make aborting() reliable.
2084 */
2085 update_force_abort();
2086 }
Bram Moolenaaref140542019-12-31 21:27:13 +01002087 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002088 ret = OK;
2089
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002090theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002091 /*
2092 * Report an error unless the argument evaluation or function call has been
2093 * cancelled due to an aborting error, an interrupt, or an exception.
2094 */
2095 if (!aborting())
2096 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002097 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002098 }
2099
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002100 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002101 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002102 clear_tv(&argv[--argv_clear + argv_base]);
2103
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002104 vim_free(tofree);
2105 vim_free(name);
2106
2107 return ret;
2108}
2109
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110 static char_u *
2111printable_func_name(ufunc_T *fp)
2112{
2113 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
2114}
2115
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002116/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002117 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002118 */
2119 static void
2120list_func_head(ufunc_T *fp, int indent)
2121{
2122 int j;
2123
2124 msg_start();
2125 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002126 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002127 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002128 msg_puts("def ");
2129 else
2130 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002132 msg_putchar('(');
2133 for (j = 0; j < fp->uf_args.ga_len; ++j)
2134 {
2135 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002136 msg_puts(", ");
2137 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 if (fp->uf_arg_types != NULL)
2139 {
2140 char *tofree;
2141
2142 msg_puts(": ");
2143 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
2144 vim_free(tofree);
2145 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002146 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
2147 {
2148 msg_puts(" = ");
2149 msg_puts(((char **)(fp->uf_def_args.ga_data))
2150 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
2151 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002152 }
2153 if (fp->uf_varargs)
2154 {
2155 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002156 msg_puts(", ");
2157 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002158 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 if (fp->uf_va_name != NULL)
2160 {
2161 if (j)
2162 msg_puts(", ");
2163 msg_puts("...");
2164 msg_puts((char *)fp->uf_va_name);
2165 if (fp->uf_va_type)
2166 {
2167 char *tofree;
2168
2169 msg_puts(": ");
2170 msg_puts(type_name(fp->uf_va_type, &tofree));
2171 vim_free(tofree);
2172 }
2173 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002174 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002175
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002176 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002177 {
2178 if (fp->uf_ret_type != &t_void)
2179 {
2180 char *tofree;
2181
2182 msg_puts(": ");
2183 msg_puts(type_name(fp->uf_ret_type, &tofree));
2184 vim_free(tofree);
2185 }
2186 }
2187 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002188 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002189 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002190 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002191 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002192 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002193 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002194 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002195 msg_clr_eos();
2196 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002197 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002198}
2199
2200/*
2201 * Get a function name, translating "<SID>" and "<SNR>".
2202 * Also handles a Funcref in a List or Dictionary.
2203 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002204 * Set "*is_global" to TRUE when the function must be global, unless
2205 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002206 * flags:
2207 * TFN_INT: internal function name OK
2208 * TFN_QUIET: be quiet
2209 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002210 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002211 * Advances "pp" to just after the function name (if no error).
2212 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002213 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002214trans_function_name(
2215 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002216 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002217 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002218 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002219 funcdict_T *fdp, // return: info about dictionary used
2220 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002221{
2222 char_u *name = NULL;
2223 char_u *start;
2224 char_u *end;
2225 int lead;
2226 char_u sid_buf[20];
2227 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002229 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002231
2232 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002233 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002234 start = *pp;
2235
Bram Moolenaare38eab22019-12-05 21:50:01 +01002236 // Check for hard coded <SNR>: already translated function ID (from a user
2237 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002238 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2239 && (*pp)[2] == (int)KE_SNR)
2240 {
2241 *pp += 3;
2242 len = get_id_len(pp) + 3;
2243 return vim_strnsave(start, len);
2244 }
2245
Bram Moolenaare38eab22019-12-05 21:50:01 +01002246 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2247 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002248 lead = eval_fname_script(start);
2249 if (lead > 2)
2250 start += lead;
2251
Bram Moolenaare38eab22019-12-05 21:50:01 +01002252 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002253 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002254 lead > 2 ? 0 : FNE_CHECK_START);
2255 if (end == start)
2256 {
2257 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002258 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002259 goto theend;
2260 }
2261 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2262 {
2263 /*
2264 * Report an invalid expression in braces, unless the expression
2265 * evaluation has been cancelled due to an aborting error, an
2266 * interrupt, or an exception.
2267 */
2268 if (!aborting())
2269 {
2270 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002271 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002272 }
2273 else
2274 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2275 goto theend;
2276 }
2277
2278 if (lv.ll_tv != NULL)
2279 {
2280 if (fdp != NULL)
2281 {
2282 fdp->fd_dict = lv.ll_dict;
2283 fdp->fd_newkey = lv.ll_newkey;
2284 lv.ll_newkey = NULL;
2285 fdp->fd_di = lv.ll_di;
2286 }
2287 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2288 {
2289 name = vim_strsave(lv.ll_tv->vval.v_string);
2290 *pp = end;
2291 }
2292 else if (lv.ll_tv->v_type == VAR_PARTIAL
2293 && lv.ll_tv->vval.v_partial != NULL)
2294 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002295 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002296 *pp = end;
2297 if (partial != NULL)
2298 *partial = lv.ll_tv->vval.v_partial;
2299 }
2300 else
2301 {
2302 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2303 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002304 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002305 else
2306 *pp = end;
2307 name = NULL;
2308 }
2309 goto theend;
2310 }
2311
2312 if (lv.ll_name == NULL)
2313 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002314 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002315 *pp = end;
2316 goto theend;
2317 }
2318
Bram Moolenaare38eab22019-12-05 21:50:01 +01002319 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002320 if (lv.ll_exp_name != NULL)
2321 {
2322 len = (int)STRLEN(lv.ll_exp_name);
2323 name = deref_func_name(lv.ll_exp_name, &len, partial,
2324 flags & TFN_NO_AUTOLOAD);
2325 if (name == lv.ll_exp_name)
2326 name = NULL;
2327 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002328 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002329 {
2330 len = (int)(end - *pp);
2331 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2332 if (name == *pp)
2333 name = NULL;
2334 }
2335 if (name != NULL)
2336 {
2337 name = vim_strsave(name);
2338 *pp = end;
2339 if (STRNCMP(name, "<SNR>", 5) == 0)
2340 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002341 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002342 name[0] = K_SPECIAL;
2343 name[1] = KS_EXTRA;
2344 name[2] = (int)KE_SNR;
2345 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2346 }
2347 goto theend;
2348 }
2349
2350 if (lv.ll_exp_name != NULL)
2351 {
2352 len = (int)STRLEN(lv.ll_exp_name);
2353 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2354 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2355 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002356 // When there was "s:" already or the name expanded to get a
2357 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002358 lv.ll_name += 2;
2359 len -= 2;
2360 lead = 2;
2361 }
2362 }
2363 else
2364 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002365 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002366 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002367 {
2368 if (is_global != NULL && lv.ll_name[0] == 'g')
2369 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002370 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002371 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002372 len = (int)(end - lv.ll_name);
2373 }
2374
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002375 // In Vim9 script a user function is script-local by default.
2376 vim9script = ASCII_ISUPPER(*start)
2377 && current_sctx.sc_version == SCRIPT_VERSION_VIM9;
2378
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002379 /*
2380 * Copy the function name to allocated memory.
2381 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2382 * Accept <SNR>123_name() outside a script.
2383 */
2384 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002385 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002387 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 if (!vim9script)
2389 lead = 3;
2390 if (vim9script || (lv.ll_exp_name != NULL
2391 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002392 || eval_fname_sid(*pp))
2393 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002395 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002396 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002397 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002398 goto theend;
2399 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002400 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 if (vim9script)
2402 extra = 3 + (int)STRLEN(sid_buf);
2403 else
2404 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002405 }
2406 }
2407 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2408 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002409 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002410 start);
2411 goto theend;
2412 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002413 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002414 {
2415 char_u *cp = vim_strchr(lv.ll_name, ':');
2416
2417 if (cp != NULL && cp < end)
2418 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002419 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002420 goto theend;
2421 }
2422 }
2423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002425 if (name != NULL)
2426 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002427 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002428 {
2429 name[0] = K_SPECIAL;
2430 name[1] = KS_EXTRA;
2431 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002433 STRCPY(name + 3, sid_buf);
2434 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2436 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002437 }
2438 *pp = end;
2439
2440theend:
2441 clear_lval(&lv);
2442 return name;
2443}
2444
2445/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02002446 * Assuming "name" is the result of trans_function_name() and it was prefixed
2447 * to use the script-local name, return the unmodified name (points into
2448 * "name"). Otherwise return NULL.
2449 * This can be used to first search for a script-local function and fall back
2450 * to the global function if not found.
2451 */
2452 char_u *
2453untrans_function_name(char_u *name)
2454{
2455 char_u *p;
2456
2457 if (*name == K_SPECIAL && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2458 {
2459 p = vim_strchr(name, '_');
2460 if (p != NULL)
2461 return p + 1;
2462 }
2463 return NULL;
2464}
2465
2466/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002467 * List functions. When "regmatch" is NULL all of then.
2468 * Otherwise functions matching "regmatch".
2469 */
2470 static void
2471list_functions(regmatch_T *regmatch)
2472{
2473 long_u used = func_hashtab.ht_used;
2474 long_u todo = used;
2475 hashitem_T *ht_array = func_hashtab.ht_array;
2476 hashitem_T *hi;
2477
2478 for (hi = ht_array; todo > 0 && !got_int; ++hi)
2479 {
2480 if (!HASHITEM_EMPTY(hi))
2481 {
2482 ufunc_T *fp = HI2UF(hi);
2483
2484 --todo;
2485 if ((fp->uf_flags & FC_DEAD) == 0
2486 && (regmatch == NULL
2487 ? !message_filtered(fp->uf_name)
2488 && !func_name_refcount(fp->uf_name)
2489 : !isdigit(*fp->uf_name)
2490 && vim_regexec(regmatch, fp->uf_name, 0)))
2491 {
2492 list_func_head(fp, FALSE);
2493 if (used != func_hashtab.ht_used
2494 || ht_array != func_hashtab.ht_array)
2495 {
2496 emsg(_("E454: function list was modified"));
2497 return;
2498 }
2499 }
2500 }
2501 }
2502}
2503
2504/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02002505 * ":function" also supporting nested ":def".
2506 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002507 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02002508 ufunc_T *
Bram Moolenaar822ba242020-05-24 23:00:18 +02002509def_function(exarg_T *eap, char_u *name_arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002510{
2511 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002512 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002513 int j;
2514 int c;
2515 int saved_did_emsg;
2516 int saved_wait_return = need_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002517 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002518 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002519 char_u *p;
2520 char_u *arg;
2521 char_u *line_arg = NULL;
2522 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002524 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002525 garray_T newlines;
2526 int varargs = FALSE;
2527 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002529 ufunc_T *fp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002530 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002531 int indent;
2532 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533#define MAX_FUNC_NESTING 50
2534 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002535 dictitem_T *v;
2536 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002537 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002538 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002539 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002540 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002541 linenr_T sourcing_lnum_off;
2542 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002543 int is_heredoc = FALSE;
2544 char_u *skip_until = NULL;
2545 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002546
Bram Moolenaar822ba242020-05-24 23:00:18 +02002547 if (in_vim9script() && eap->forceit)
2548 {
2549 emsg(_(e_nobang));
2550 return NULL;
2551 }
2552
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002553 /*
2554 * ":function" without argument: list functions.
2555 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002556 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002557 {
2558 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002559 list_functions(NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002560 eap->nextcmd = check_nextcmd(eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02002561 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002562 }
2563
2564 /*
2565 * ":function /pat": list functions matching pattern.
2566 */
2567 if (*eap->arg == '/')
2568 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002569 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002570 if (!eap->skip)
2571 {
2572 regmatch_T regmatch;
2573
2574 c = *p;
2575 *p = NUL;
2576 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2577 *p = c;
2578 if (regmatch.regprog != NULL)
2579 {
2580 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002581 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002582 vim_regfree(regmatch.regprog);
2583 }
2584 }
2585 if (*p == '/')
2586 ++p;
2587 eap->nextcmd = check_nextcmd(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02002588 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002589 }
2590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 ga_init(&newargs);
2592 ga_init(&argtypes);
2593 ga_init(&default_args);
2594
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002595 /*
2596 * Get the function name. There are these situations:
2597 * func normal function name
2598 * "name" == func, "fudi.fd_dict" == NULL
2599 * dict.func new dictionary entry
2600 * "name" == NULL, "fudi.fd_dict" set,
2601 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2602 * dict.func existing dict entry with a Funcref
2603 * "name" == func, "fudi.fd_dict" set,
2604 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2605 * dict.func existing dict entry that's not a Funcref
2606 * "name" == NULL, "fudi.fd_dict" set,
2607 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2608 * s:func script-local function name
2609 * g:func global function name, same as "func"
2610 */
2611 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002612 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002613 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02002614 // nested function, argument is (args).
2615 paren = TRUE;
2616 CLEAR_FIELD(fudi);
2617 }
2618 else
2619 {
2620 name = trans_function_name(&p, &is_global, eap->skip,
2621 TFN_NO_AUTOLOAD, &fudi, NULL);
2622 paren = (vim_strchr(p, '(') != NULL);
2623 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002624 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02002625 /*
2626 * Return on an invalid expression in braces, unless the expression
2627 * evaluation has been cancelled due to an aborting error, an
2628 * interrupt, or an exception.
2629 */
2630 if (!aborting())
2631 {
2632 if (!eap->skip && fudi.fd_newkey != NULL)
2633 semsg(_(e_dictkey), fudi.fd_newkey);
2634 vim_free(fudi.fd_newkey);
2635 return NULL;
2636 }
2637 else
2638 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002639 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002640 }
2641
Bram Moolenaare38eab22019-12-05 21:50:01 +01002642 // An error in a function call during evaluation of an expression in magic
2643 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002644 saved_did_emsg = did_emsg;
2645 did_emsg = FALSE;
2646
2647 /*
2648 * ":function func" with only function name: list function.
2649 */
2650 if (!paren)
2651 {
2652 if (!ends_excmd(*skipwhite(p)))
2653 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002654 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002655 goto ret_free;
2656 }
2657 eap->nextcmd = check_nextcmd(p);
2658 if (eap->nextcmd != NULL)
2659 *p = NUL;
2660 if (!eap->skip && !got_int)
2661 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002662 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002663 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
2664 {
2665 char_u *up = untrans_function_name(name);
2666
2667 // With Vim9 script the name was made script-local, if not
2668 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02002669 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002670 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002671 }
2672
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002673 if (fp != NULL)
2674 {
2675 list_func_head(fp, TRUE);
2676 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2677 {
2678 if (FUNCLINE(fp, j) == NULL)
2679 continue;
2680 msg_putchar('\n');
2681 msg_outnum((long)(j + 1));
2682 if (j < 9)
2683 msg_putchar(' ');
2684 if (j < 99)
2685 msg_putchar(' ');
2686 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002687 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002688 ui_breakcheck();
2689 }
2690 if (!got_int)
2691 {
2692 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002693 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694 msg_puts(" enddef");
2695 else
2696 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002697 }
2698 }
2699 else
Bram Moolenaara26b9702020-04-18 19:53:28 +02002700 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002701 }
2702 goto ret_free;
2703 }
2704
2705 /*
2706 * ":function name(arg1, arg2)" Define function.
2707 */
2708 p = skipwhite(p);
2709 if (*p != '(')
2710 {
2711 if (!eap->skip)
2712 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002713 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002714 goto ret_free;
2715 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002716 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002717 if (vim_strchr(p, '(') != NULL)
2718 p = vim_strchr(p, '(');
2719 }
2720 p = skipwhite(p + 1);
2721
2722 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2723
Bram Moolenaar04b12692020-05-04 23:24:44 +02002724 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002725 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002726 // Check the name of the function. Unless it's a dictionary function
2727 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002728 if (name != NULL)
2729 arg = name;
2730 else
2731 arg = fudi.fd_newkey;
2732 if (arg != NULL && (fudi.fd_di == NULL
2733 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2734 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2735 {
2736 if (*arg == K_SPECIAL)
2737 j = 3;
2738 else
2739 j = 0;
2740 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2741 : eval_isnamec(arg[j])))
2742 ++j;
2743 if (arg[j] != NUL)
2744 emsg_funcname((char *)e_invarg2, arg);
2745 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002746 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002747 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002748 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002749 }
2750
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002751 // This may get more lines and make the pointers into the first line
2752 // invalid.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 if (get_function_args(&p, ')', &newargs,
2754 eap->cmdidx == CMD_def ? &argtypes : NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002755 &varargs, &default_args, eap->skip,
2756 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002757 goto errret_2;
2758
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002760 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 // find the return type: :def Func(): type
2762 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002763 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 ret_type = skipwhite(p + 1);
2765 p = skip_type(ret_type);
2766 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002767 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002768 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002769 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002770 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002772 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 semsg(_("E1056: expected a type: %s"), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02002774 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002775 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002776 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002777 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 else
2779 // find extra arguments "range", "dict", "abort" and "closure"
2780 for (;;)
2781 {
2782 p = skipwhite(p);
2783 if (STRNCMP(p, "range", 5) == 0)
2784 {
2785 flags |= FC_RANGE;
2786 p += 5;
2787 }
2788 else if (STRNCMP(p, "dict", 4) == 0)
2789 {
2790 flags |= FC_DICT;
2791 p += 4;
2792 }
2793 else if (STRNCMP(p, "abort", 5) == 0)
2794 {
2795 flags |= FC_ABORT;
2796 p += 5;
2797 }
2798 else if (STRNCMP(p, "closure", 7) == 0)
2799 {
2800 flags |= FC_CLOSURE;
2801 p += 7;
2802 if (current_funccal == NULL)
2803 {
2804 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
2805 name == NULL ? (char_u *)"" : name);
2806 goto erret;
2807 }
2808 }
2809 else
2810 break;
2811 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002812
Bram Moolenaare38eab22019-12-05 21:50:01 +01002813 // When there is a line break use what follows for the function body.
2814 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002815 if (*p == '\n')
2816 line_arg = p + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002817 else if (*p != NUL && *p != '"' && !(eap->cmdidx == CMD_def && *p == '#')
2818 && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002819 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002820
2821 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
2823 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002824 */
2825 if (KeyTyped)
2826 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002827 // Check if the function already exists, don't let the user type the
2828 // whole function before telling him it doesn't work! For a script we
2829 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002830 if (!eap->skip && !eap->forceit)
2831 {
2832 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002833 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002834 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002835 emsg_funcname(e_funcexts, name);
2836 }
2837
2838 if (!eap->skip && did_emsg)
2839 goto erret;
2840
Bram Moolenaare38eab22019-12-05 21:50:01 +01002841 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002842 cmdline_row = msg_row;
2843 }
2844
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002845 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002846 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002847
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002848 indent = 2;
2849 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002851 for (;;)
2852 {
2853 if (KeyTyped)
2854 {
2855 msg_scroll = TRUE;
2856 saved_wait_return = FALSE;
2857 }
2858 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002859
2860 if (line_arg != NULL)
2861 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002862 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002863 theline = line_arg;
2864 p = vim_strchr(theline, '\n');
2865 if (p == NULL)
2866 line_arg += STRLEN(line_arg);
2867 else
2868 {
2869 *p = NUL;
2870 line_arg = p + 1;
2871 }
2872 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002873 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002874 {
2875 vim_free(line_to_free);
2876 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002877 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002878 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002879 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002880 line_to_free = theline;
2881 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002882 if (KeyTyped)
2883 lines_left = Rows - 1;
2884 if (theline == NULL)
2885 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 if (eap->cmdidx == CMD_def)
2887 emsg(_("E1057: Missing :enddef"));
2888 else
2889 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002890 goto erret;
2891 }
2892
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002893 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002894 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002895 if (SOURCING_LNUM < sourcing_lnum_off)
2896 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002897 else
2898 sourcing_lnum_off = 0;
2899
2900 if (skip_until != NULL)
2901 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002903 // * ":append" and "."
2904 // * ":python <<EOF" and "EOF"
2905 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2906 if (heredoc_trimmed == NULL
2907 || (is_heredoc && skipwhite(theline) == theline)
2908 || STRNCMP(theline, heredoc_trimmed,
2909 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002910 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002911 if (heredoc_trimmed == NULL)
2912 p = theline;
2913 else if (is_heredoc)
2914 p = skipwhite(theline) == theline
2915 ? theline : theline + STRLEN(heredoc_trimmed);
2916 else
2917 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002918 if (STRCMP(p, skip_until) == 0)
2919 {
2920 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002921 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002922 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002923 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002924 }
2925 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002926 }
2927 else
2928 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002929 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01002930 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002931 ;
2932
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 // Check for "endfunction" or "enddef".
2934 if (checkforcmd(&p, nesting_def[nesting]
2935 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002936 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002937 char_u *nextcmd = NULL;
2938
Bram Moolenaar663bb232017-06-22 19:12:10 +02002939 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002940 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002941 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002942 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002943 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 give_warning2(eap->cmdidx == CMD_def
2945 ? (char_u *)_("W1001: Text found after :enddef: %s")
2946 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002947 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002948 if (nextcmd != NULL)
2949 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002950 // Another command follows. If the line came from "eap" we
2951 // can simply point into it, otherwise we need to change
2952 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02002953 eap->nextcmd = nextcmd;
2954 if (line_to_free != NULL)
2955 {
2956 vim_free(*eap->cmdlinep);
2957 *eap->cmdlinep = line_to_free;
2958 line_to_free = NULL;
2959 }
2960 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002961 break;
2962 }
2963
Bram Moolenaare38eab22019-12-05 21:50:01 +01002964 // Increase indent inside "if", "while", "for" and "try", decrease
2965 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002967 indent -= 2;
2968 else if (STRNCMP(p, "if", 2) == 0
2969 || STRNCMP(p, "wh", 2) == 0
2970 || STRNCMP(p, "for", 3) == 0
2971 || STRNCMP(p, "try", 3) == 0)
2972 indent += 2;
2973
Bram Moolenaare38eab22019-12-05 21:50:01 +01002974 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01002975 // Only recognize "def" inside "def", not inside "function",
2976 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01002978 if (checkforcmd(&p, "function", 2)
2979 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002980 {
2981 if (*p == '!')
2982 p = skipwhite(p + 1);
2983 p += eval_fname_script(p);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002984 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL, NULL));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002985 if (*skipwhite(p) == '(')
2986 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 if (nesting == MAX_FUNC_NESTING - 1)
2988 emsg(_("E1058: function nesting too deep"));
2989 else
2990 {
2991 ++nesting;
2992 nesting_def[nesting] = (c == 'd');
2993 indent += 2;
2994 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002995 }
2996 }
2997
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002998 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002999 p = skip_range(p, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003000 if (eap->cmdidx != CMD_def
3001 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01003002 || (p[0] == 'c'
3003 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
3004 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
3005 && (STRNCMP(&p[3], "nge", 3) != 0
3006 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003007 || (p[0] == 'i'
3008 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003009 && (!ASCII_ISALPHA(p[2])
3010 || (p[2] == 's'
3011 && (!ASCII_ISALPHA(p[3])
3012 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003013 skip_until = vim_strsave((char_u *)".");
3014
Bram Moolenaare38eab22019-12-05 21:50:01 +01003015 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003016 arg = skipwhite(skiptowhite(p));
3017 if (arg[0] == '<' && arg[1] =='<'
3018 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003019 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
3020 || ((p[2] == '3' || p[2] == 'x')
3021 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003022 || (p[0] == 'p' && p[1] == 'e'
3023 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
3024 || (p[0] == 't' && p[1] == 'c'
3025 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
3026 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
3027 && !ASCII_ISALPHA(p[3]))
3028 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
3029 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
3030 || (p[0] == 'm' && p[1] == 'z'
3031 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
3032 ))
3033 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003034 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003035 p = skipwhite(arg + 2);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003036 if (STRNCMP(p, "trim", 4) == 0)
3037 {
3038 // Ignore leading white space.
3039 p = skipwhite(p + 4);
3040 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003041 skipwhite(theline) - theline);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003042 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003043 if (*p == NUL)
3044 skip_until = vim_strsave((char_u *)".");
3045 else
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003046 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003047 do_concat = FALSE;
3048 is_heredoc = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003049 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003050
3051 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003052 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02003053 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003054 if (*arg == '[')
3055 arg = vim_strchr(arg, ']');
3056 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003057 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003058 arg = skipwhite(skiptowhite(arg));
3059 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
3060 && ((p[0] == 'l'
3061 && p[1] == 'e'
3062 && (!ASCII_ISALNUM(p[2])
3063 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02003064 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003065 p = skipwhite(arg + 3);
3066 if (STRNCMP(p, "trim", 4) == 0)
3067 {
3068 // Ignore leading white space.
3069 p = skipwhite(p + 4);
3070 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003071 skipwhite(theline) - theline);
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003072 }
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003073 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003074 do_concat = FALSE;
3075 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003076 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003077 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003078 }
3079
Bram Moolenaare38eab22019-12-05 21:50:01 +01003080 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003081 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003082 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003083
Bram Moolenaare38eab22019-12-05 21:50:01 +01003084 // Copy the line to newly allocated memory. get_one_sourceline()
3085 // allocates 250 bytes per line, this saves 80% on average. The cost
3086 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003087 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003088 if (p == NULL)
3089 goto erret;
3090 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003091
Bram Moolenaare38eab22019-12-05 21:50:01 +01003092 // Add NULL lines for continuation lines, so that the line count is
3093 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003094 while (sourcing_lnum_off-- > 0)
3095 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3096
Bram Moolenaare38eab22019-12-05 21:50:01 +01003097 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003098 if (line_arg != NULL && *line_arg == NUL)
3099 line_arg = NULL;
3100 }
3101
Bram Moolenaare38eab22019-12-05 21:50:01 +01003102 // Don't define the function when skipping commands or when an error was
3103 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003104 if (eap->skip || did_emsg)
3105 goto erret;
3106
3107 /*
3108 * If there are no errors, add the function
3109 */
3110 if (fudi.fd_dict == NULL)
3111 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 hashtab_T *ht;
3113
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003114 v = find_var(name, &ht, FALSE);
3115 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
3116 {
3117 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
3118 name);
3119 goto erret;
3120 }
3121
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003122 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003123 if (fp != NULL)
3124 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 int dead = fp->uf_flags & FC_DEAD;
3126
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003127 // Function can be replaced with "function!" and when sourcing the
3128 // same script again, but only once.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 if (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003130 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
3131 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003132 {
3133 emsg_funcname(e_funcexts, name);
3134 goto erret;
3135 }
3136 if (fp->uf_calls > 0)
3137 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003138 emsg_funcname(
3139 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003140 name);
3141 goto erret;
3142 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003143 if (fp->uf_refcount > 1)
3144 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003145 // This function is referenced somewhere, don't redefine it but
3146 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003147 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003148 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003149 fp = NULL;
3150 overwrite = TRUE;
3151 }
3152 else
3153 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003154 char_u *exp_name = fp->uf_name_exp;
3155
3156 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01003157 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003158 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003159 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003160 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003162#ifdef FEAT_PROFILE
3163 fp->uf_profiling = FALSE;
3164 fp->uf_prof_initialized = FALSE;
3165#endif
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003166 clear_def_function(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003167 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003168 }
3169 }
3170 else
3171 {
3172 char numbuf[20];
3173
3174 fp = NULL;
3175 if (fudi.fd_newkey == NULL && !eap->forceit)
3176 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003177 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003178 goto erret;
3179 }
3180 if (fudi.fd_di == NULL)
3181 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003182 // Can't add a function to a locked dictionary
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003183 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003184 goto erret;
3185 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003186 // Can't change an existing function if it is locked
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003187 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003188 goto erret;
3189
Bram Moolenaare38eab22019-12-05 21:50:01 +01003190 // Give the function a sequential number. Can only be used with a
3191 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003192 vim_free(name);
3193 sprintf(numbuf, "%d", ++func_nr);
3194 name = vim_strsave((char_u *)numbuf);
3195 if (name == NULL)
3196 goto erret;
3197 }
3198
3199 if (fp == NULL)
3200 {
3201 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3202 {
3203 int slen, plen;
3204 char_u *scriptname;
3205
Bram Moolenaare38eab22019-12-05 21:50:01 +01003206 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003207 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003208 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003209 {
3210 scriptname = autoload_name(name);
3211 if (scriptname != NULL)
3212 {
3213 p = vim_strchr(scriptname, '/');
3214 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003215 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003216 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003217 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003218 j = OK;
3219 vim_free(scriptname);
3220 }
3221 }
3222 if (j == FAIL)
3223 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003224 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003225 goto erret;
3226 }
3227 }
3228
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003229 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003230 if (fp == NULL)
3231 goto erret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003232 fp->uf_def_status = eap->cmdidx == CMD_def ? UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003233 : UF_NOT_COMPILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003234
3235 if (fudi.fd_dict != NULL)
3236 {
3237 if (fudi.fd_di == NULL)
3238 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003239 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003240 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3241 if (fudi.fd_di == NULL)
3242 {
3243 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003244 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003245 goto erret;
3246 }
3247 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3248 {
3249 vim_free(fudi.fd_di);
3250 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003251 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003252 goto erret;
3253 }
3254 }
3255 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003256 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003257 clear_tv(&fudi.fd_di->di_tv);
3258 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003259 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003260
Bram Moolenaare38eab22019-12-05 21:50:01 +01003261 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003262 flags |= FC_DICT;
3263 }
3264
Bram Moolenaare38eab22019-12-05 21:50:01 +01003265 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003266 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003267 if (overwrite)
3268 {
3269 hi = hash_find(&func_hashtab, name);
3270 hi->hi_key = UF2HIKEY(fp);
3271 }
3272 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003273 {
3274 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003275 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003276 goto erret;
3277 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003278 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003279 }
3280 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003281 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003283 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284
3285 if (eap->cmdidx == CMD_def)
3286 {
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003287 int lnum_save = SOURCING_LNUM;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003288
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003289 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003290
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003291 // error messages are for the first function line
3292 SOURCING_LNUM = sourcing_lnum_top;
3293
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 // parse the argument types
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003295 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296
3297 if (argtypes.ga_len > 0)
3298 {
3299 // When "varargs" is set the last name/type goes into uf_va_name
3300 // and uf_va_type.
3301 int len = argtypes.ga_len - (varargs ? 1 : 0);
3302
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003303 if (len > 0)
3304 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 if (fp->uf_arg_types != NULL)
3306 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003307 int i;
3308 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309
3310 for (i = 0; i < len; ++ i)
3311 {
3312 p = ((char_u **)argtypes.ga_data)[i];
3313 if (p == NULL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003314 // will get the type from the default value
3315 type = &t_unknown;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 else
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003317 type = parse_type(&p, &fp->uf_type_list);
3318 if (type == NULL)
3319 {
3320 SOURCING_LNUM = lnum_save;
3321 goto errret_2;
3322 }
3323 fp->uf_arg_types[i] = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 }
3325 }
3326 if (varargs)
3327 {
3328 // Move the last argument "...name: type" to uf_va_name and
3329 // uf_va_type.
3330 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
3331 [fp->uf_args.ga_len - 1];
3332 --fp->uf_args.ga_len;
3333 p = ((char_u **)argtypes.ga_data)[len];
3334 if (p == NULL)
3335 // todo: get type from default value
3336 fp->uf_va_type = &t_any;
3337 else
3338 fp->uf_va_type = parse_type(&p, &fp->uf_type_list);
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003339 if (fp->uf_va_type == NULL)
3340 {
3341 SOURCING_LNUM = lnum_save;
3342 goto errret_2;
3343 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344 }
3345 varargs = FALSE;
3346 }
3347
3348 // parse the return type, if any
3349 if (ret_type == NULL)
3350 fp->uf_ret_type = &t_void;
3351 else
3352 {
3353 p = ret_type;
3354 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list);
3355 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02003356 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003358 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003359 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003361 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003362 if ((flags & FC_CLOSURE) != 0)
3363 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003364 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003365 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003366 }
3367 else
3368 fp->uf_scoped = NULL;
3369
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003370#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003371 if (prof_def_func())
3372 func_do_profile(fp);
3373#endif
3374 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003375 if (sandbox)
3376 flags |= FC_SANDBOX;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003377 if (in_vim9script() && !ASCII_ISUPPER(*fp->uf_name))
3378 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003379 fp->uf_flags = flags;
3380 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003381 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003382 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003383 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 if (is_export)
3385 {
3386 fp->uf_flags |= FC_EXPORT;
3387 // let ex_export() know the export worked.
3388 is_export = FALSE;
3389 }
3390
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003391 if (eap->cmdidx == CMD_def)
3392 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02003393 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
3394 // :func does not use Vim9 script syntax, even in a Vim9 script file
3395 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003396
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003397 goto ret_free;
3398
3399erret:
3400 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003401 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003402errret_2:
3403 ga_clear_strings(&newlines);
3404ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003405 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003406 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003407 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003408 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003409 if (name != name_arg)
3410 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003411 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003412 did_emsg |= saved_did_emsg;
3413 need_wait_return |= saved_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003414
3415 return fp;
3416}
3417
3418/*
3419 * ":function"
3420 */
3421 void
3422ex_function(exarg_T *eap)
3423{
Bram Moolenaar822ba242020-05-24 23:00:18 +02003424 (void)def_function(eap, NULL);
3425}
3426
3427/*
3428 * :defcompile - compile all :def functions in the current script.
3429 */
3430 void
3431ex_defcompile(exarg_T *eap UNUSED)
3432{
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003433 long_u ht_used = func_hashtab.ht_used;
3434 int todo = (int)ht_used;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003435 hashitem_T *hi;
3436 ufunc_T *ufunc;
3437
3438 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
3439 {
3440 if (!HASHITEM_EMPTY(hi))
3441 {
3442 --todo;
3443 ufunc = HI2UF(hi);
3444 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003445 && ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003446 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02003447 compile_def_function(ufunc, FALSE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003448
3449 if (func_hashtab.ht_used != ht_used)
3450 {
3451 // another function has been defined, need to start over
3452 hi = func_hashtab.ht_array;
3453 ht_used = func_hashtab.ht_used;
3454 todo = (int)ht_used;
Bram Moolenaar285b1892020-05-26 11:37:26 +02003455 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003456 }
3457 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003458 }
3459 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003460}
3461
3462/*
3463 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3464 * Return 2 if "p" starts with "s:".
3465 * Return 0 otherwise.
3466 */
3467 int
3468eval_fname_script(char_u *p)
3469{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003470 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3471 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003472 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3473 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3474 return 5;
3475 if (p[0] == 's' && p[1] == ':')
3476 return 2;
3477 return 0;
3478}
3479
3480 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003481translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003482{
3483 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003484 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003485 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486}
3487
3488/*
3489 * Return TRUE when "ufunc" has old-style "..." varargs
3490 * or named varargs "...name: type".
3491 */
3492 int
3493has_varargs(ufunc_T *ufunc)
3494{
3495 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003496}
3497
3498/*
3499 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003500 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003501 */
3502 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003503function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003504{
3505 char_u *nm = name;
3506 char_u *p;
3507 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003508 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003509 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003510
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003511 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3512 if (no_deref)
3513 flag |= TFN_NO_DEREF;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003514 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003515 nm = skipwhite(nm);
3516
Bram Moolenaare38eab22019-12-05 21:50:01 +01003517 // Only accept "funcname", "funcname ", "funcname (..." and
3518 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003519 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003520 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003521 vim_free(p);
3522 return n;
3523}
3524
Bram Moolenaar113e1072019-01-20 15:30:40 +01003525#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003526 char_u *
3527get_expanded_name(char_u *name, int check)
3528{
3529 char_u *nm = name;
3530 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003531 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003532
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003533 p = trans_function_name(&nm, &is_global, FALSE,
3534 TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003535
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003536 if (p != NULL && *nm == NUL
3537 && (!check || translated_function_exists(p, is_global)))
3538 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003539
3540 vim_free(p);
3541 return NULL;
3542}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003543#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003544
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003545/*
3546 * Function given to ExpandGeneric() to obtain the list of user defined
3547 * function names.
3548 */
3549 char_u *
3550get_user_func_name(expand_T *xp, int idx)
3551{
3552 static long_u done;
3553 static hashitem_T *hi;
3554 ufunc_T *fp;
3555
3556 if (idx == 0)
3557 {
3558 done = 0;
3559 hi = func_hashtab.ht_array;
3560 }
3561 if (done < func_hashtab.ht_used)
3562 {
3563 if (done++ > 0)
3564 ++hi;
3565 while (HASHITEM_EMPTY(hi))
3566 ++hi;
3567 fp = HI2UF(hi);
3568
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 // don't show dead, dict and lambda functions
3570 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02003571 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003573
3574 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003575 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003576
3577 cat_func_name(IObuff, fp);
3578 if (xp->xp_context != EXPAND_USER_FUNC)
3579 {
3580 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003582 STRCAT(IObuff, ")");
3583 }
3584 return IObuff;
3585 }
3586 return NULL;
3587}
3588
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003589/*
3590 * ":delfunction {name}"
3591 */
3592 void
3593ex_delfunction(exarg_T *eap)
3594{
3595 ufunc_T *fp = NULL;
3596 char_u *p;
3597 char_u *name;
3598 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003599 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003600
3601 p = eap->arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003602 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003603 vim_free(fudi.fd_newkey);
3604 if (name == NULL)
3605 {
3606 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003607 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003608 return;
3609 }
3610 if (!ends_excmd(*skipwhite(p)))
3611 {
3612 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003613 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003614 return;
3615 }
3616 eap->nextcmd = check_nextcmd(p);
3617 if (eap->nextcmd != NULL)
3618 *p = NUL;
3619
3620 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003621 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003622 vim_free(name);
3623
3624 if (!eap->skip)
3625 {
3626 if (fp == NULL)
3627 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02003628 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003629 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003630 return;
3631 }
3632 if (fp->uf_calls > 0)
3633 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003634 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003635 return;
3636 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003637 if (fp->uf_flags & FC_VIM9)
3638 {
3639 semsg(_("E1084: Cannot delete Vim9 script function %s"), eap->arg);
3640 return;
3641 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003642
3643 if (fudi.fd_dict != NULL)
3644 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003645 // Delete the dict item that refers to the function, it will
3646 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003647 dictitem_remove(fudi.fd_dict, fudi.fd_di);
3648 }
3649 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003650 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003651 // A normal function (not a numbered function or lambda) has a
3652 // refcount of 1 for the entry in the hashtable. When deleting
3653 // it and the refcount is more than one, it should be kept.
3654 // A numbered function and lambda should be kept if the refcount is
3655 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003656 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003657 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003658 // Function is still referenced somewhere. Don't free it but
3659 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003660 if (func_remove(fp))
3661 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003662 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003663 }
3664 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003665 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003666 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003667 }
3668}
3669
3670/*
3671 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003672 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003673 */
3674 void
3675func_unref(char_u *name)
3676{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003677 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003678
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003679 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003680 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003681 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003682 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003683 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003684#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003685 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003686#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01003687 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003688 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003689 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003690 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003691 // Only delete it when it's not being used. Otherwise it's done
3692 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003693 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003694 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02003695 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003696}
3697
3698/*
3699 * Unreference a Function: decrement the reference count and free it when it
3700 * becomes zero.
3701 */
3702 void
3703func_ptr_unref(ufunc_T *fp)
3704{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003705 if (fp != NULL && --fp->uf_refcount <= 0)
3706 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003707 // Only delete it when it's not being used. Otherwise it's done
3708 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02003709 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003710 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003711 }
3712}
3713
3714/*
3715 * Count a reference to a Function.
3716 */
3717 void
3718func_ref(char_u *name)
3719{
3720 ufunc_T *fp;
3721
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003722 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003723 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003724 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003725 if (fp != NULL)
3726 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003727 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01003728 // Only give an error for a numbered function.
3729 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01003730 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003731}
3732
3733/*
3734 * Count a reference to a Function.
3735 */
3736 void
3737func_ptr_ref(ufunc_T *fp)
3738{
3739 if (fp != NULL)
3740 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003741}
3742
3743/*
3744 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3745 * referenced from anywhere that is in use.
3746 */
3747 static int
3748can_free_funccal(funccall_T *fc, int copyID)
3749{
3750 return (fc->l_varlist.lv_copyID != copyID
3751 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003752 && fc->l_avars.dv_copyID != copyID
3753 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003754}
3755
3756/*
3757 * ":return [expr]"
3758 */
3759 void
3760ex_return(exarg_T *eap)
3761{
3762 char_u *arg = eap->arg;
3763 typval_T rettv;
3764 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003765 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003766
3767 if (current_funccal == NULL)
3768 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003769 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003770 return;
3771 }
3772
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003773 CLEAR_FIELD(evalarg);
3774 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
3775
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003776 if (eap->skip)
3777 ++emsg_skip;
3778
3779 eap->nextcmd = NULL;
3780 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003781 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003782 {
3783 if (!eap->skip)
3784 returning = do_return(eap, FALSE, TRUE, &rettv);
3785 else
3786 clear_tv(&rettv);
3787 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003788 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003789 else if (!eap->skip)
3790 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003791 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003792 update_force_abort();
3793
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003794 /*
3795 * Return unless the expression evaluation has been cancelled due to an
3796 * aborting error, an interrupt, or an exception.
3797 */
3798 if (!aborting())
3799 returning = do_return(eap, FALSE, TRUE, NULL);
3800 }
3801
Bram Moolenaare38eab22019-12-05 21:50:01 +01003802 // When skipping or the return gets pending, advance to the next command
3803 // in this line (!returning). Otherwise, ignore the rest of the line.
3804 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003805 if (returning)
3806 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003807 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003808 eap->nextcmd = check_nextcmd(arg);
3809
3810 if (eap->skip)
3811 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02003812 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003813}
3814
3815/*
3816 * ":1,25call func(arg1, arg2)" function call.
3817 */
3818 void
3819ex_call(exarg_T *eap)
3820{
3821 char_u *arg = eap->arg;
3822 char_u *startarg;
3823 char_u *name;
3824 char_u *tofree;
3825 int len;
3826 typval_T rettv;
3827 linenr_T lnum;
3828 int doesrange;
3829 int failed = FALSE;
3830 funcdict_T fudi;
3831 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003832 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003833
Bram Moolenaare6b53242020-07-01 17:28:33 +02003834 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003835 if (eap->skip)
3836 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003837 // trans_function_name() doesn't work well when skipping, use eval0()
3838 // instead to skip to any following command, e.g. for:
3839 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003840 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003841 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003842 clear_tv(&rettv);
3843 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003844 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003845 return;
3846 }
3847
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003848 tofree = trans_function_name(&arg, NULL, eap->skip,
3849 TFN_INT, &fudi, &partial);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003850 if (fudi.fd_newkey != NULL)
3851 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003852 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003853 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003854 vim_free(fudi.fd_newkey);
3855 }
3856 if (tofree == NULL)
3857 return;
3858
Bram Moolenaare38eab22019-12-05 21:50:01 +01003859 // Increase refcount on dictionary, it could get deleted when evaluating
3860 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003861 if (fudi.fd_dict != NULL)
3862 ++fudi.fd_dict->dv_refcount;
3863
Bram Moolenaare38eab22019-12-05 21:50:01 +01003864 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3865 // contents. For VAR_PARTIAL get its partial, unless we already have one
3866 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003867 len = (int)STRLEN(tofree);
3868 name = deref_func_name(tofree, &len,
3869 partial != NULL ? NULL : &partial, FALSE);
3870
Bram Moolenaare38eab22019-12-05 21:50:01 +01003871 // Skip white space to allow ":call func ()". Not good, but required for
3872 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003873 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003874 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003875
3876 if (*startarg != '(')
3877 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003879 goto end;
3880 }
3881
3882 /*
3883 * When skipping, evaluate the function once, to find the end of the
3884 * arguments.
3885 * When the function takes a range, this is discovered after the first
3886 * call, and the loop is broken.
3887 */
3888 if (eap->skip)
3889 {
3890 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003891 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003892 }
3893 else
3894 lnum = eap->line1;
3895 for ( ; lnum <= eap->line2; ++lnum)
3896 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003897 funcexe_T funcexe;
3898
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003899 if (!eap->skip && eap->addr_count > 0)
3900 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003901 if (lnum > curbuf->b_ml.ml_line_count)
3902 {
3903 // If the function deleted lines or switched to another buffer
3904 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003905 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003906 break;
3907 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003908 curwin->w_cursor.lnum = lnum;
3909 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003910 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003911 }
3912 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003913
Bram Moolenaara80faa82020-04-12 19:37:17 +02003914 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003915 funcexe.firstline = eap->line1;
3916 funcexe.lastline = eap->line2;
3917 funcexe.doesrange = &doesrange;
3918 funcexe.evaluate = !eap->skip;
3919 funcexe.partial = partial;
3920 funcexe.selfdict = fudi.fd_dict;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003921 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003922 {
3923 failed = TRUE;
3924 break;
3925 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003926 if (has_watchexpr())
3927 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003928
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003929 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003930 if (handle_subscript(&arg, &rettv,
3931 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003932 {
3933 failed = TRUE;
3934 break;
3935 }
3936
3937 clear_tv(&rettv);
3938 if (doesrange || eap->skip)
3939 break;
3940
Bram Moolenaare38eab22019-12-05 21:50:01 +01003941 // Stop when immediately aborting on error, or when an interrupt
3942 // occurred or an exception was thrown but not caught.
3943 // get_func_tv() returned OK, so that the check for trailing
3944 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003945 if (aborting())
3946 break;
3947 }
3948 if (eap->skip)
3949 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003950 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003951
Bram Moolenaare51bb172020-02-16 19:42:23 +01003952 // When inside :try we need to check for following "| catch".
3953 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003954 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003955 // Check for trailing illegal characters and a following command.
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003956 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003957 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01003958 if (!failed)
3959 {
3960 emsg_severe = TRUE;
3961 emsg(_(e_trailing));
3962 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003963 }
3964 else
3965 eap->nextcmd = check_nextcmd(arg);
3966 }
3967
3968end:
3969 dict_unref(fudi.fd_dict);
3970 vim_free(tofree);
3971}
3972
3973/*
3974 * Return from a function. Possibly makes the return pending. Also called
3975 * for a pending return at the ":endtry" or after returning from an extra
3976 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3977 * when called due to a ":return" command. "rettv" may point to a typval_T
3978 * with the return rettv. Returns TRUE when the return can be carried out,
3979 * FALSE when the return gets pending.
3980 */
3981 int
3982do_return(
3983 exarg_T *eap,
3984 int reanimate,
3985 int is_cmd,
3986 void *rettv)
3987{
3988 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01003989 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003990
3991 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003992 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003993 current_funccal->returned = FALSE;
3994
3995 /*
3996 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3997 * not in its finally clause (which then is to be executed next) is found.
3998 * In this case, make the ":return" pending for execution at the ":endtry".
3999 * Otherwise, return normally.
4000 */
4001 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
4002 if (idx >= 0)
4003 {
4004 cstack->cs_pending[idx] = CSTP_RETURN;
4005
4006 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004007 // A pending return again gets pending. "rettv" points to an
4008 // allocated variable with the rettv of the original ":return"'s
4009 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004010 cstack->cs_rettv[idx] = rettv;
4011 else
4012 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004013 // When undoing a return in order to make it pending, get the stored
4014 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004015 if (reanimate)
4016 rettv = current_funccal->rettv;
4017
4018 if (rettv != NULL)
4019 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004020 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004021 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
4022 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
4023 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004024 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004025 }
4026 else
4027 cstack->cs_rettv[idx] = NULL;
4028
4029 if (reanimate)
4030 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004031 // The pending return value could be overwritten by a ":return"
4032 // without argument in a finally clause; reset the default
4033 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004034 current_funccal->rettv->v_type = VAR_NUMBER;
4035 current_funccal->rettv->vval.v_number = 0;
4036 }
4037 }
4038 report_make_pending(CSTP_RETURN, rettv);
4039 }
4040 else
4041 {
4042 current_funccal->returned = TRUE;
4043
Bram Moolenaare38eab22019-12-05 21:50:01 +01004044 // If the return is carried out now, store the return value. For
4045 // a return immediately after reanimation, the value is already
4046 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004047 if (!reanimate && rettv != NULL)
4048 {
4049 clear_tv(current_funccal->rettv);
4050 *current_funccal->rettv = *(typval_T *)rettv;
4051 if (!is_cmd)
4052 vim_free(rettv);
4053 }
4054 }
4055
4056 return idx < 0;
4057}
4058
4059/*
4060 * Free the variable with a pending return value.
4061 */
4062 void
4063discard_pending_return(void *rettv)
4064{
4065 free_tv((typval_T *)rettv);
4066}
4067
4068/*
4069 * Generate a return command for producing the value of "rettv". The result
4070 * is an allocated string. Used by report_pending() for verbose messages.
4071 */
4072 char_u *
4073get_return_cmd(void *rettv)
4074{
4075 char_u *s = NULL;
4076 char_u *tofree = NULL;
4077 char_u numbuf[NUMBUFLEN];
4078
4079 if (rettv != NULL)
4080 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
4081 if (s == NULL)
4082 s = (char_u *)"";
4083
4084 STRCPY(IObuff, ":return ");
4085 STRNCPY(IObuff + 8, s, IOSIZE - 8);
4086 if (STRLEN(s) + 8 >= IOSIZE)
4087 STRCPY(IObuff + IOSIZE - 4, "...");
4088 vim_free(tofree);
4089 return vim_strsave(IObuff);
4090}
4091
4092/*
4093 * Get next function line.
4094 * Called by do_cmdline() to get the next line.
4095 * Returns allocated string, or NULL for end of function.
4096 */
4097 char_u *
4098get_func_line(
4099 int c UNUSED,
4100 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02004101 int indent UNUSED,
4102 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004103{
4104 funccall_T *fcp = (funccall_T *)cookie;
4105 ufunc_T *fp = fcp->func;
4106 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004107 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004108
Bram Moolenaare38eab22019-12-05 21:50:01 +01004109 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004110 if (fcp->dbg_tick != debug_tick)
4111 {
4112 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004113 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004114 fcp->dbg_tick = debug_tick;
4115 }
4116#ifdef FEAT_PROFILE
4117 if (do_profiling == PROF_YES)
4118 func_line_end(cookie);
4119#endif
4120
4121 gap = &fp->uf_lines;
4122 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4123 || fcp->returned)
4124 retval = NULL;
4125 else
4126 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004127 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004128 while (fcp->linenr < gap->ga_len
4129 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
4130 ++fcp->linenr;
4131 if (fcp->linenr >= gap->ga_len)
4132 retval = NULL;
4133 else
4134 {
4135 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004136 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004137#ifdef FEAT_PROFILE
4138 if (do_profiling == PROF_YES)
4139 func_line_start(cookie);
4140#endif
4141 }
4142 }
4143
Bram Moolenaare38eab22019-12-05 21:50:01 +01004144 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004145 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004146 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004147 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004148 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004149 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004150 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004151 fcp->dbg_tick = debug_tick;
4152 }
4153
4154 return retval;
4155}
4156
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004157/*
4158 * Return TRUE if the currently active function should be ended, because a
4159 * return was encountered or an error occurred. Used inside a ":while".
4160 */
4161 int
4162func_has_ended(void *cookie)
4163{
4164 funccall_T *fcp = (funccall_T *)cookie;
4165
Bram Moolenaare38eab22019-12-05 21:50:01 +01004166 // Ignore the "abort" flag if the abortion behavior has been changed due to
4167 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004168 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4169 || fcp->returned);
4170}
4171
4172/*
4173 * return TRUE if cookie indicates a function which "abort"s on errors.
4174 */
4175 int
4176func_has_abort(
4177 void *cookie)
4178{
4179 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
4180}
4181
4182
4183/*
4184 * Turn "dict.Func" into a partial for "Func" bound to "dict".
4185 * Don't do this when "Func" is already a partial that was bound
4186 * explicitly (pt_auto is FALSE).
4187 * Changes "rettv" in-place.
4188 * Returns the updated "selfdict_in".
4189 */
4190 dict_T *
4191make_partial(dict_T *selfdict_in, typval_T *rettv)
4192{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004193 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004194 char_u *tofree = NULL;
4195 ufunc_T *fp;
4196 char_u fname_buf[FLEN_FIXED + 1];
4197 int error;
4198 dict_T *selfdict = selfdict_in;
4199
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004200 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
4201 fp = rettv->vval.v_partial->pt_func;
4202 else
4203 {
4204 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
4205 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004206 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004207 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004208 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004209 vim_free(tofree);
4210 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004211
4212 if (fp != NULL && (fp->uf_flags & FC_DICT))
4213 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004214 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004215
4216 if (pt != NULL)
4217 {
4218 pt->pt_refcount = 1;
4219 pt->pt_dict = selfdict;
4220 pt->pt_auto = TRUE;
4221 selfdict = NULL;
4222 if (rettv->v_type == VAR_FUNC)
4223 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004224 // Just a function: Take over the function name and use
4225 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004226 pt->pt_name = rettv->vval.v_string;
4227 }
4228 else
4229 {
4230 partial_T *ret_pt = rettv->vval.v_partial;
4231 int i;
4232
Bram Moolenaare38eab22019-12-05 21:50:01 +01004233 // Partial: copy the function name, use selfdict and copy
4234 // args. Can't take over name or args, the partial might
4235 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004236 if (ret_pt->pt_name != NULL)
4237 {
4238 pt->pt_name = vim_strsave(ret_pt->pt_name);
4239 func_ref(pt->pt_name);
4240 }
4241 else
4242 {
4243 pt->pt_func = ret_pt->pt_func;
4244 func_ptr_ref(pt->pt_func);
4245 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004246 if (ret_pt->pt_argc > 0)
4247 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004248 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004249 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004250 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004251 pt->pt_argc = 0;
4252 else
4253 {
4254 pt->pt_argc = ret_pt->pt_argc;
4255 for (i = 0; i < pt->pt_argc; i++)
4256 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
4257 }
4258 }
4259 partial_unref(ret_pt);
4260 }
4261 rettv->v_type = VAR_PARTIAL;
4262 rettv->vval.v_partial = pt;
4263 }
4264 }
4265 return selfdict;
4266}
4267
4268/*
4269 * Return the name of the executed function.
4270 */
4271 char_u *
4272func_name(void *cookie)
4273{
4274 return ((funccall_T *)cookie)->func->uf_name;
4275}
4276
4277/*
4278 * Return the address holding the next breakpoint line for a funccall cookie.
4279 */
4280 linenr_T *
4281func_breakpoint(void *cookie)
4282{
4283 return &((funccall_T *)cookie)->breakpoint;
4284}
4285
4286/*
4287 * Return the address holding the debug tick for a funccall cookie.
4288 */
4289 int *
4290func_dbg_tick(void *cookie)
4291{
4292 return &((funccall_T *)cookie)->dbg_tick;
4293}
4294
4295/*
4296 * Return the nesting level for a funccall cookie.
4297 */
4298 int
4299func_level(void *cookie)
4300{
4301 return ((funccall_T *)cookie)->level;
4302}
4303
4304/*
4305 * Return TRUE when a function was ended by a ":return" command.
4306 */
4307 int
4308current_func_returned(void)
4309{
4310 return current_funccal->returned;
4311}
4312
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004313 int
4314free_unref_funccal(int copyID, int testing)
4315{
4316 int did_free = FALSE;
4317 int did_free_funccal = FALSE;
4318 funccall_T *fc, **pfc;
4319
4320 for (pfc = &previous_funccal; *pfc != NULL; )
4321 {
4322 if (can_free_funccal(*pfc, copyID))
4323 {
4324 fc = *pfc;
4325 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004326 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004327 did_free = TRUE;
4328 did_free_funccal = TRUE;
4329 }
4330 else
4331 pfc = &(*pfc)->caller;
4332 }
4333 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004334 // When a funccal was freed some more items might be garbage
4335 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004336 (void)garbage_collect(testing);
4337
4338 return did_free;
4339}
4340
4341/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004342 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004343 */
4344 static funccall_T *
4345get_funccal(void)
4346{
4347 int i;
4348 funccall_T *funccal;
4349 funccall_T *temp_funccal;
4350
4351 funccal = current_funccal;
4352 if (debug_backtrace_level > 0)
4353 {
4354 for (i = 0; i < debug_backtrace_level; i++)
4355 {
4356 temp_funccal = funccal->caller;
4357 if (temp_funccal)
4358 funccal = temp_funccal;
4359 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004360 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004361 debug_backtrace_level = i;
4362 }
4363 }
4364 return funccal;
4365}
4366
4367/*
4368 * Return the hashtable used for local variables in the current funccal.
4369 * Return NULL if there is no current funccal.
4370 */
4371 hashtab_T *
4372get_funccal_local_ht()
4373{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004374 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004375 return NULL;
4376 return &get_funccal()->l_vars.dv_hashtab;
4377}
4378
4379/*
4380 * Return the l: scope variable.
4381 * Return NULL if there is no current funccal.
4382 */
4383 dictitem_T *
4384get_funccal_local_var()
4385{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004387 return NULL;
4388 return &get_funccal()->l_vars_var;
4389}
4390
4391/*
4392 * Return the hashtable used for argument in the current funccal.
4393 * Return NULL if there is no current funccal.
4394 */
4395 hashtab_T *
4396get_funccal_args_ht()
4397{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004399 return NULL;
4400 return &get_funccal()->l_avars.dv_hashtab;
4401}
4402
4403/*
4404 * Return the a: scope variable.
4405 * Return NULL if there is no current funccal.
4406 */
4407 dictitem_T *
4408get_funccal_args_var()
4409{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004411 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004412 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004413}
4414
4415/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004416 * List function variables, if there is a function.
4417 */
4418 void
4419list_func_vars(int *first)
4420{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004422 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004423 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004424}
4425
4426/*
4427 * If "ht" is the hashtable for local variables in the current funccal, return
4428 * the dict that contains it.
4429 * Otherwise return NULL.
4430 */
4431 dict_T *
4432get_current_funccal_dict(hashtab_T *ht)
4433{
4434 if (current_funccal != NULL
4435 && ht == &current_funccal->l_vars.dv_hashtab)
4436 return &current_funccal->l_vars;
4437 return NULL;
4438}
4439
4440/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004441 * Search hashitem in parent scope.
4442 */
4443 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004444find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004445{
4446 funccall_T *old_current_funccal = current_funccal;
4447 hashtab_T *ht;
4448 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004449 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004450
4451 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4452 return NULL;
4453
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004454 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004455 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004456 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004457 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004458 ht = find_var_ht(name, &varname);
4459 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004460 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004461 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004462 if (!HASHITEM_EMPTY(hi))
4463 {
4464 *pht = ht;
4465 break;
4466 }
4467 }
4468 if (current_funccal == current_funccal->func->uf_scoped)
4469 break;
4470 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004471 }
4472 current_funccal = old_current_funccal;
4473
4474 return hi;
4475}
4476
4477/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004478 * Search variable in parent scope.
4479 */
4480 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004481find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004482{
4483 dictitem_T *v = NULL;
4484 funccall_T *old_current_funccal = current_funccal;
4485 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004486 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004487
4488 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4489 return NULL;
4490
Bram Moolenaare38eab22019-12-05 21:50:01 +01004491 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004492 current_funccal = current_funccal->func->uf_scoped;
4493 while (current_funccal)
4494 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004495 ht = find_var_ht(name, &varname);
4496 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004497 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004498 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004499 if (v != NULL)
4500 break;
4501 }
4502 if (current_funccal == current_funccal->func->uf_scoped)
4503 break;
4504 current_funccal = current_funccal->func->uf_scoped;
4505 }
4506 current_funccal = old_current_funccal;
4507
4508 return v;
4509}
4510
4511/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004512 * Set "copyID + 1" in previous_funccal and callers.
4513 */
4514 int
4515set_ref_in_previous_funccal(int copyID)
4516{
4517 int abort = FALSE;
4518 funccall_T *fc;
4519
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004520 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004521 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004522 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004523 abort = abort
4524 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4525 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004526 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004527 }
4528 return abort;
4529}
4530
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004531 static int
4532set_ref_in_funccal(funccall_T *fc, int copyID)
4533{
4534 int abort = FALSE;
4535
4536 if (fc->fc_copyID != copyID)
4537 {
4538 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004539 abort = abort
4540 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4541 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004542 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004543 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004544 }
4545 return abort;
4546}
4547
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004548/*
4549 * Set "copyID" in all local vars and arguments in the call stack.
4550 */
4551 int
4552set_ref_in_call_stack(int copyID)
4553{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004554 int abort = FALSE;
4555 funccall_T *fc;
4556 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004557
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004558 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004559 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004560
4561 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004562 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
4563 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004564 abort = abort || set_ref_in_funccal(fc, copyID);
4565
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004566 return abort;
4567}
4568
4569/*
4570 * Set "copyID" in all functions available by name.
4571 */
4572 int
4573set_ref_in_functions(int copyID)
4574{
4575 int todo;
4576 hashitem_T *hi = NULL;
4577 int abort = FALSE;
4578 ufunc_T *fp;
4579
4580 todo = (int)func_hashtab.ht_used;
4581 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004582 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004583 if (!HASHITEM_EMPTY(hi))
4584 {
4585 --todo;
4586 fp = HI2UF(hi);
4587 if (!func_name_refcount(fp->uf_name))
4588 abort = abort || set_ref_in_func(NULL, fp, copyID);
4589 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004590 }
4591 return abort;
4592}
4593
4594/*
4595 * Set "copyID" in all function arguments.
4596 */
4597 int
4598set_ref_in_func_args(int copyID)
4599{
4600 int i;
4601 int abort = FALSE;
4602
4603 for (i = 0; i < funcargs.ga_len; ++i)
4604 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
4605 copyID, NULL, NULL);
4606 return abort;
4607}
4608
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004609/*
4610 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004611 * Returns TRUE if setting references failed somehow.
4612 */
4613 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004614set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004615{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004616 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004617 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01004618 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004619 char_u fname_buf[FLEN_FIXED + 1];
4620 char_u *tofree = NULL;
4621 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004622 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004623
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004624 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004625 return FALSE;
4626
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004627 if (fp_in == NULL)
4628 {
4629 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004630 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004631 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004632 if (fp != NULL)
4633 {
4634 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004635 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004636 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004637
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004638 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004639 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004640}
4641
Bram Moolenaare38eab22019-12-05 21:50:01 +01004642#endif // FEAT_EVAL