blob: 121079149aeffd019040202fd6a4e2ff1a2bec89 [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 Moolenaar5deeb3f2020-04-05 17:08:17 +02001072 while (fp->uf_type_list.ga_len > 0)
1073 vim_free(((type_T **)fp->uf_type_list.ga_data)
1074 [--fp->uf_type_list.ga_len]);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075 ga_clear(&fp->uf_type_list);
Bram Moolenaar801ab062020-06-25 19:27:56 +02001076
1077#ifdef FEAT_LUA
1078 if (fp->uf_cb_free != NULL)
1079 {
1080 fp->uf_cb_free(fp->uf_cb_state);
1081 fp->uf_cb_free = NULL;
1082 }
1083
1084 fp->uf_cb_state = NULL;
1085 fp->uf_cb = NULL;
1086#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087#ifdef FEAT_PROFILE
1088 VIM_CLEAR(fp->uf_tml_count);
1089 VIM_CLEAR(fp->uf_tml_total);
1090 VIM_CLEAR(fp->uf_tml_self);
1091#endif
1092}
1093
1094/*
1095 * Free all things that a function contains. Does not free the function
1096 * itself, use func_free() for that.
1097 * When "force" is TRUE we are exiting.
1098 */
1099 static void
1100func_clear(ufunc_T *fp, int force)
1101{
1102 if (fp->uf_cleared)
1103 return;
1104 fp->uf_cleared = TRUE;
1105
1106 // clear this function
1107 func_clear_items(fp);
1108 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001109 clear_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110}
1111
1112/*
1113 * Free a function and remove it from the list of functions. Does not free
1114 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001115 * When "force" is TRUE we are exiting.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 */
1117 static void
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001118func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119{
1120 // Only remove it when not done already, otherwise we would remove a newer
1121 // version of the function with the same name.
1122 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1123 func_remove(fp);
1124
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001125 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 vim_free(fp);
1127}
1128
1129/*
1130 * Free all things that a function contains and free the function itself.
1131 * When "force" is TRUE we are exiting.
1132 */
1133 static void
1134func_clear_free(ufunc_T *fp, int force)
1135{
1136 func_clear(fp, force);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001137 if (force || fp->uf_dfunc_idx == 0)
1138 func_free(fp, force);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139}
1140
Bram Moolenaar6914c642017-04-01 21:21:30 +02001141
1142/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001143 * Call a user function.
1144 */
1145 static void
1146call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001147 ufunc_T *fp, // pointer to function
1148 int argcount, // nr of args
1149 typval_T *argvars, // arguments
1150 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001151 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01001152 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001153{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001154 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001155 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001156 funccall_T *fc;
1157 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001158 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001159 static int depth = 0;
1160 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001161 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001162 int i;
1163 int ai;
1164 int islambda = FALSE;
1165 char_u numbuf[NUMBUFLEN];
1166 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001167#ifdef FEAT_PROFILE
1168 proftime_T wait_start;
1169 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001170 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001171#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001172 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001173
Bram Moolenaare38eab22019-12-05 21:50:01 +01001174 // If depth of calling is getting too high, don't execute the function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001175 if (depth >= p_mfd)
1176 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001177 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001178 rettv->v_type = VAR_NUMBER;
1179 rettv->vval.v_number = -1;
1180 return;
1181 }
1182 ++depth;
1183
Bram Moolenaare38eab22019-12-05 21:50:01 +01001184 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001185
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001186 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001187 if (fc == NULL)
1188 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001189 fc->caller = current_funccal;
1190 current_funccal = fc;
1191 fc->func = fp;
1192 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001193 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001194 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001195 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1196 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001197 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001198 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001199 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001200
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001201 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202 {
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001203 estack_push_ufunc(fp, 1);
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001204 save_current_sctx = current_sctx;
1205 current_sctx = fp->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001207 // Execute the function, possibly compiling it first.
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001208 call_def_function(fp, argcount, argvars, funcexe->partial, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209 --depth;
1210 current_funccal = fc->caller;
1211
1212 estack_pop();
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001213 current_sctx = save_current_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214 free_funccal(fc);
1215 return;
1216 }
1217
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001218 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1219 islambda = TRUE;
1220
1221 /*
1222 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1223 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1224 * each argument variable and saves a lot of time.
1225 */
1226 /*
1227 * Init l: variables.
1228 */
1229 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1230 if (selfdict != NULL)
1231 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001232 // Set l:self to "selfdict". Use "name" to avoid a warning from
1233 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001234 v = &fc->fixvar[fixvar_idx++].var;
1235 name = v->di_key;
1236 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001237 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001238 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1239 v->di_tv.v_type = VAR_DICT;
1240 v->di_tv.v_lock = 0;
1241 v->di_tv.vval.v_dict = selfdict;
1242 ++selfdict->dv_refcount;
1243 }
1244
1245 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001246 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001247 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001248 * Set a:000 to a list with room for the "..." arguments.
1249 */
1250 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001251 if ((fp->uf_flags & FC_NOARGS) == 0)
1252 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001253 (varnumber_T)(argcount >= fp->uf_args.ga_len
1254 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001255 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001256 if ((fp->uf_flags & FC_NOARGS) == 0)
1257 {
1258 // Use "name" to avoid a warning from some compiler that checks the
1259 // destination size.
1260 v = &fc->fixvar[fixvar_idx++].var;
1261 name = v->di_key;
1262 STRCPY(name, "000");
1263 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1264 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1265 v->di_tv.v_type = VAR_LIST;
1266 v->di_tv.v_lock = VAR_FIXED;
1267 v->di_tv.vval.v_list = &fc->l_varlist;
1268 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001269 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001270 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1271 fc->l_varlist.lv_lock = VAR_FIXED;
1272
1273 /*
1274 * Set a:firstline to "firstline" and a:lastline to "lastline".
1275 * Set a:name to named arguments.
1276 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001277 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001278 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001279 if ((fp->uf_flags & FC_NOARGS) == 0)
1280 {
1281 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001282 (varnumber_T)funcexe->firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001283 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001284 (varnumber_T)funcexe->lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001285 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001286 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001287 {
1288 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001289 typval_T def_rettv;
1290 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001291
1292 ai = i - fp->uf_args.ga_len;
1293 if (ai < 0)
1294 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001295 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001296 name = FUNCARG(fp, i);
1297 if (islambda)
1298 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001299
1300 // evaluate named argument default expression
1301 isdefault = ai + fp->uf_def_args.ga_len >= 0
1302 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1303 && argvars[i].vval.v_number == VVAL_NONE));
1304 if (isdefault)
1305 {
1306 char_u *default_expr = NULL;
1307 def_rettv.v_type = VAR_NUMBER;
1308 def_rettv.vval.v_number = -1;
1309
1310 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1311 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001312 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001313 {
1314 default_arg_err = 1;
1315 break;
1316 }
1317 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001318 }
1319 else
1320 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001321 if ((fp->uf_flags & FC_NOARGS) != 0)
1322 // Bail out if no a: arguments used (in lambda).
1323 break;
1324
Bram Moolenaare38eab22019-12-05 21:50:01 +01001325 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001326 sprintf((char *)numbuf, "%d", ai + 1);
1327 name = numbuf;
1328 }
1329 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1330 {
1331 v = &fc->fixvar[fixvar_idx++].var;
1332 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001333 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001334 }
1335 else
1336 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001337 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001338 if (v == NULL)
1339 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001340 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001341 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001342
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001343 // Note: the values are copied directly to avoid alloc/free.
1344 // "argvars" must have VAR_FIXED for v_lock.
1345 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001346 v->di_tv.v_lock = VAR_FIXED;
1347
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001348 if (addlocal)
1349 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001350 // Named arguments should be accessed without the "a:" prefix in
1351 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001352 copy_tv(&v->di_tv, &v->di_tv);
1353 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001354 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001355 else
1356 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001357
1358 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1359 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001360 listitem_T *li = &fc->l_listitems[ai];
1361
1362 li->li_tv = argvars[i];
1363 li->li_tv.v_lock = VAR_FIXED;
1364 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001365 }
1366 }
1367
Bram Moolenaare38eab22019-12-05 21:50:01 +01001368 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001369 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001370
1371 if (fp->uf_flags & FC_SANDBOX)
1372 {
1373 using_sandbox = TRUE;
1374 ++sandbox;
1375 }
1376
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001377 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001378 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001379 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001380 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001381 ++no_wait_return;
1382 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001383
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001384 smsg(_("calling %s"), SOURCING_NAME);
1385 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001386 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001387 char_u buf[MSG_BUF_LEN];
1388 char_u numbuf2[NUMBUFLEN];
1389 char_u *tofree;
1390 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001391
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001392 msg_puts("(");
1393 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001394 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001395 if (i > 0)
1396 msg_puts(", ");
1397 if (argvars[i].v_type == VAR_NUMBER)
1398 msg_outnum((long)argvars[i].vval.v_number);
1399 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001400 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001401 // Do not want errors such as E724 here.
1402 ++emsg_off;
1403 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1404 --emsg_off;
1405 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001406 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001407 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001408 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001409 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1410 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001411 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001412 msg_puts((char *)s);
1413 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001414 }
1415 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001416 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001417 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001418 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001419 msg_puts("\n"); // don't overwrite this either
1420
1421 verbose_leave_scroll();
1422 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001423 }
1424#ifdef FEAT_PROFILE
1425 if (do_profiling == PROF_YES)
1426 {
1427 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001428 {
1429 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001430 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001431 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001432 if (fp->uf_profiling
1433 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1434 {
1435 ++fp->uf_tm_count;
1436 profile_start(&call_start);
1437 profile_zero(&fp->uf_tm_children);
1438 }
1439 script_prof_save(&wait_start);
1440 }
1441#endif
1442
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001443 save_current_sctx = current_sctx;
1444 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001445 save_did_emsg = did_emsg;
1446 did_emsg = FALSE;
1447
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001448 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1449 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001450 else if (islambda)
1451 {
1452 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1453
1454 // A Lambda always has the command "return {expr}". It is much faster
1455 // to evaluate {expr} directly.
1456 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001457 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001458 --ex_nesting_level;
1459 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001460 else
1461 // call do_cmdline() to execute the lines
1462 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001463 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1464
1465 --RedrawingDisabled;
1466
Bram Moolenaare38eab22019-12-05 21:50:01 +01001467 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001468 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1469 {
1470 clear_tv(rettv);
1471 rettv->v_type = VAR_NUMBER;
1472 rettv->vval.v_number = -1;
1473 }
1474
1475#ifdef FEAT_PROFILE
1476 if (do_profiling == PROF_YES && (fp->uf_profiling
1477 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1478 {
1479 profile_end(&call_start);
1480 profile_sub_wait(&wait_start, &call_start);
1481 profile_add(&fp->uf_tm_total, &call_start);
1482 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1483 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1484 {
1485 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1486 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1487 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001488 if (started_profiling)
1489 // make a ":profdel func" stop profiling the function
1490 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001491 }
1492#endif
1493
Bram Moolenaare38eab22019-12-05 21:50:01 +01001494 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001495 if (p_verbose >= 12)
1496 {
1497 ++no_wait_return;
1498 verbose_enter_scroll();
1499
1500 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001501 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001502 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001503 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001504 (long)fc->rettv->vval.v_number);
1505 else
1506 {
1507 char_u buf[MSG_BUF_LEN];
1508 char_u numbuf2[NUMBUFLEN];
1509 char_u *tofree;
1510 char_u *s;
1511
Bram Moolenaare38eab22019-12-05 21:50:01 +01001512 // The value may be very long. Skip the middle part, so that we
1513 // have some idea how it starts and ends. smsg() would always
1514 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001515 ++emsg_off;
1516 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1517 --emsg_off;
1518 if (s != NULL)
1519 {
1520 if (vim_strsize(s) > MSG_BUF_CLEN)
1521 {
1522 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1523 s = buf;
1524 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001525 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001526 vim_free(tofree);
1527 }
1528 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001529 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001530
1531 verbose_leave_scroll();
1532 --no_wait_return;
1533 }
1534
Bram Moolenaare31ee862020-01-07 20:59:34 +01001535 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001536 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001537 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001538#ifdef FEAT_PROFILE
1539 if (do_profiling == PROF_YES)
1540 script_prof_restore(&wait_start);
1541#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001542 if (using_sandbox)
1543 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001544
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001545 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001546 {
1547 ++no_wait_return;
1548 verbose_enter_scroll();
1549
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001550 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001551 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001552
1553 verbose_leave_scroll();
1554 --no_wait_return;
1555 }
1556
1557 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001558 --depth;
1559
Bram Moolenaar6914c642017-04-01 21:21:30 +02001560 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001561}
1562
1563/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001565 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566 int
1567call_user_func_check(
1568 ufunc_T *fp,
1569 int argcount,
1570 typval_T *argvars,
1571 typval_T *rettv,
1572 funcexe_T *funcexe,
1573 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001574{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 int error;
1576 int regular_args = fp->uf_args.ga_len;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001577
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1579 *funcexe->doesrange = TRUE;
1580 if (argcount < regular_args - fp->uf_def_args.ga_len)
1581 error = FCERR_TOOFEW;
1582 else if (!has_varargs(fp) && argcount > regular_args)
1583 error = FCERR_TOOMANY;
1584 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1585 error = FCERR_DICT;
1586 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001587 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 int did_save_redo = FALSE;
1589 save_redo_T save_redo;
1590
1591 /*
1592 * Call the user function.
1593 * Save and restore search patterns, script variables and
1594 * redo buffer.
1595 */
1596 save_search_patterns();
1597 if (!ins_compl_active())
1598 {
1599 saveRedobuff(&save_redo);
1600 did_save_redo = TRUE;
1601 }
1602 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001603 call_user_func(fp, argcount, argvars, rettv, funcexe,
1604 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1606 // Function was unreferenced while being used, free it now.
1607 func_clear_free(fp, FALSE);
1608 if (did_save_redo)
1609 restoreRedobuff(&save_redo);
1610 restore_search_patterns();
1611 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001612 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001614}
1615
1616/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001617 * There are two kinds of function names:
1618 * 1. ordinary names, function defined with :function
1619 * 2. numbered functions and lambdas
1620 * For the first we only count the name stored in func_hashtab as a reference,
1621 * using function() does not count as a reference, because the function is
1622 * looked up by name.
1623 */
1624 static int
1625func_name_refcount(char_u *name)
1626{
1627 return isdigit(*name) || *name == '<';
1628}
1629
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001630static funccal_entry_T *funccal_stack = NULL;
1631
1632/*
1633 * Save the current function call pointer, and set it to NULL.
1634 * Used when executing autocommands and for ":source".
1635 */
1636 void
1637save_funccal(funccal_entry_T *entry)
1638{
1639 entry->top_funccal = current_funccal;
1640 entry->next = funccal_stack;
1641 funccal_stack = entry;
1642 current_funccal = NULL;
1643}
1644
1645 void
1646restore_funccal(void)
1647{
1648 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001649 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001650 else
1651 {
1652 current_funccal = funccal_stack->top_funccal;
1653 funccal_stack = funccal_stack->next;
1654 }
1655}
1656
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001657 funccall_T *
1658get_current_funccal(void)
1659{
1660 return current_funccal;
1661}
1662
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001663/*
1664 * Mark all functions of script "sid" as deleted.
1665 */
1666 void
1667delete_script_functions(int sid)
1668{
1669 hashitem_T *hi;
1670 ufunc_T *fp;
1671 long_u todo;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02001672 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001673 size_t len;
1674
1675 buf[0] = K_SPECIAL;
1676 buf[1] = KS_EXTRA;
1677 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02001678 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001679 len = STRLEN(buf);
1680
1681 todo = func_hashtab.ht_used;
1682 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1683 if (!HASHITEM_EMPTY(hi))
1684 {
Bram Moolenaar03afdcf2020-04-27 23:39:30 +02001685 fp = HI2UF(hi);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001686 if (STRNCMP(fp->uf_name, buf, len) == 0)
1687 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001688 fp->uf_flags |= FC_DEAD;
1689 func_clear(fp, TRUE);
1690 }
1691 --todo;
1692 }
1693}
1694
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001695#if defined(EXITFREE) || defined(PROTO)
1696 void
1697free_all_functions(void)
1698{
1699 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001700 ufunc_T *fp;
1701 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001702 long_u todo = 1;
1703 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001704
Bram Moolenaare38eab22019-12-05 21:50:01 +01001705 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001706 while (current_funccal != NULL)
1707 {
1708 clear_tv(current_funccal->rettv);
1709 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001710 if (current_funccal == NULL && funccal_stack != NULL)
1711 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001712 }
1713
Bram Moolenaare38eab22019-12-05 21:50:01 +01001714 // First clear what the functions contain. Since this may lower the
1715 // reference count of a function, it may also free a function and change
1716 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001717 while (todo > 0)
1718 {
1719 todo = func_hashtab.ht_used;
1720 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1721 if (!HASHITEM_EMPTY(hi))
1722 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 // clear the def function index now
1724 fp = HI2UF(hi);
1725 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001726 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727
Bram Moolenaare38eab22019-12-05 21:50:01 +01001728 // Only free functions that are not refcounted, those are
1729 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001730 if (func_name_refcount(fp->uf_name))
1731 ++skipped;
1732 else
1733 {
1734 used = func_hashtab.ht_used;
1735 func_clear(fp, TRUE);
1736 if (used != func_hashtab.ht_used)
1737 {
1738 skipped = 0;
1739 break;
1740 }
1741 }
1742 --todo;
1743 }
1744 }
1745
Bram Moolenaare38eab22019-12-05 21:50:01 +01001746 // Now actually free the functions. Need to start all over every time,
1747 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001748 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001749 while (func_hashtab.ht_used > skipped)
1750 {
1751 todo = func_hashtab.ht_used;
1752 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001753 if (!HASHITEM_EMPTY(hi))
1754 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001755 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001756 // Only free functions that are not refcounted, those are
1757 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02001758 fp = HI2UF(hi);
1759 if (func_name_refcount(fp->uf_name))
1760 ++skipped;
1761 else
1762 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001763 func_free(fp, FALSE);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001764 skipped = 0;
1765 break;
1766 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001767 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001768 }
1769 if (skipped == 0)
1770 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771
1772 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001773}
1774#endif
1775
1776/*
1777 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02001778 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001779 * "len" is the length of "name", or -1 for NUL terminated.
1780 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001782builtin_function(char_u *name, int len)
1783{
1784 char_u *p;
1785
Bram Moolenaara26b9702020-04-18 19:53:28 +02001786 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001787 return FALSE;
1788 p = vim_strchr(name, AUTOLOAD_CHAR);
1789 return p == NULL || (len > 0 && p > name + len);
1790}
1791
1792 int
1793func_call(
1794 char_u *name,
1795 typval_T *args,
1796 partial_T *partial,
1797 dict_T *selfdict,
1798 typval_T *rettv)
1799{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001800 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001801 listitem_T *item;
1802 typval_T argv[MAX_FUNC_ARGS + 1];
1803 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001804 int r = 0;
1805
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001806 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001807 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001808 {
1809 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1810 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001811 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001812 break;
1813 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001814 // Make a copy of each argument. This is needed to be able to set
1815 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001816 copy_tv(&item->li_tv, &argv[argc++]);
1817 }
1818
1819 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001820 {
1821 funcexe_T funcexe;
1822
Bram Moolenaara80faa82020-04-12 19:37:17 +02001823 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001824 funcexe.firstline = curwin->w_cursor.lnum;
1825 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001826 funcexe.evaluate = TRUE;
1827 funcexe.partial = partial;
1828 funcexe.selfdict = selfdict;
1829 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1830 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001831
Bram Moolenaare38eab22019-12-05 21:50:01 +01001832 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001833 while (argc > 0)
1834 clear_tv(&argv[--argc]);
1835
1836 return r;
1837}
1838
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001839static int callback_depth = 0;
1840
1841 int
1842get_callback_depth(void)
1843{
1844 return callback_depth;
1845}
1846
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001847/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001848 * Invoke call_func() with a callback.
1849 */
1850 int
1851call_callback(
1852 callback_T *callback,
1853 int len, // length of "name" or -1 to use strlen()
1854 typval_T *rettv, // return value goes here
1855 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001856 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001857 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001858{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001859 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001860 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001861
Bram Moolenaara80faa82020-04-12 19:37:17 +02001862 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001863 funcexe.evaluate = TRUE;
1864 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001865 ++callback_depth;
1866 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1867 --callback_depth;
1868 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001869}
1870
1871/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872 * Give an error message for the result of a function.
1873 * Nothing if "error" is FCERR_NONE.
1874 */
1875 void
1876user_func_error(int error, char_u *name)
1877{
1878 switch (error)
1879 {
1880 case FCERR_UNKNOWN:
1881 emsg_funcname(e_unknownfunc, name);
1882 break;
1883 case FCERR_NOTMETHOD:
1884 emsg_funcname(
1885 N_("E276: Cannot use function as a method: %s"), name);
1886 break;
1887 case FCERR_DELETED:
1888 emsg_funcname(N_(e_func_deleted), name);
1889 break;
1890 case FCERR_TOOMANY:
1891 emsg_funcname((char *)e_toomanyarg, name);
1892 break;
1893 case FCERR_TOOFEW:
1894 emsg_funcname((char *)e_toofewarg, name);
1895 break;
1896 case FCERR_SCRIPT:
1897 emsg_funcname(
1898 N_("E120: Using <SID> not in a script context: %s"), name);
1899 break;
1900 case FCERR_DICT:
1901 emsg_funcname(
1902 N_("E725: Calling dict function without Dictionary: %s"),
1903 name);
1904 break;
1905 }
1906}
1907
1908/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001909 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001910 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001911 * Return FAIL when the function can't be called, OK otherwise.
1912 * Also returns OK when an error was encountered while executing the function.
1913 */
1914 int
1915call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001916 char_u *funcname, // name of the function
1917 int len, // length of "name" or -1 to use strlen()
1918 typval_T *rettv, // return value goes here
1919 int argcount_in, // number of "argvars"
1920 typval_T *argvars_in, // vars for arguments, must have "argcount"
1921 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001922 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001923{
1924 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01001925 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001926 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001927 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001928 char_u fname_buf[FLEN_FIXED + 1];
1929 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001930 char_u *fname = NULL;
1931 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001932 int argcount = argcount_in;
1933 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001934 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001935 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1936 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001937 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001938 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001939 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001940
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001941 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1942 // even when call_func() returns FAIL.
1943 rettv->v_type = VAR_UNKNOWN;
1944
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001945 if (partial != NULL)
1946 fp = partial->pt_func;
1947 if (fp == NULL)
1948 {
1949 // Make a copy of the name, if it comes from a funcref variable it
1950 // could be changed or deleted in the called function.
1951 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
1952 if (name == NULL)
1953 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001954
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001955 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1956 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001957
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001958 if (funcexe->doesrange != NULL)
1959 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001960
1961 if (partial != NULL)
1962 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001963 // When the function has a partial with a dict and there is a dict
1964 // argument, use the dict argument. That is backwards compatible.
1965 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001966 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001967 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01001968 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001969 {
1970 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001971 {
1972 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
1973 {
Bram Moolenaaref140542019-12-31 21:27:13 +01001974 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001975 goto theend;
1976 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001977 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001978 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001979 for (i = 0; i < argcount_in; ++i)
1980 argv[i + argv_clear] = argvars_in[i];
1981 argvars = argv;
1982 argcount = partial->pt_argc + argcount_in;
1983 }
1984 }
1985
Bram Moolenaaref140542019-12-31 21:27:13 +01001986 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001987 {
1988 char_u *rfname = fname;
1989
Bram Moolenaare38eab22019-12-05 21:50:01 +01001990 // Ignore "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001991 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001992 rfname = fname + 2;
1993
Bram Moolenaare38eab22019-12-05 21:50:01 +01001994 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001995 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01001996 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001997
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001998 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001999 {
2000 /*
2001 * User defined function.
2002 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002003 if (fp == NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002004 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002005
Bram Moolenaare38eab22019-12-05 21:50:01 +01002006 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002007 if (fp == NULL
2008 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002009 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002010 && !aborting())
2011 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002012 // executed an autocommand, search for the function again
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002013 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002014 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002015 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002016 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
2017 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002018 // loaded a package, search for the function again
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002019 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002020 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02002021 if (fp == NULL)
2022 {
2023 char_u *p = untrans_function_name(rfname);
2024
2025 // If using Vim9 script try not local to the script.
2026 // TODO: should not do this if the name started with "s:".
2027 if (p != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002028 fp = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002029 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002030
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002031 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01002032 error = FCERR_DELETED;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002033#ifdef FEAT_LUA
2034 else if (fp != NULL && (fp->uf_flags & FC_CFUNC))
2035 {
2036 cfunc_T cb = fp->uf_cb;
2037
2038 error = (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
2039 }
2040#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002041 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002042 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002043 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01002044 // postponed filling in the arguments, do it now
2045 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002046 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002047
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002048 if (funcexe->basetv != NULL)
2049 {
2050 // Method call: base->Method()
2051 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
2052 argv[0] = *funcexe->basetv;
2053 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002054 argvars = argv;
2055 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002056 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002057
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 error = call_user_func_check(fp, argcount, argvars, rettv,
2059 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002060 }
2061 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002062 else if (funcexe->basetv != NULL)
2063 {
2064 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002065 * expr->method(): Find the method name in the table, call its
2066 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02002067 */
2068 error = call_internal_method(fname, argcount, argvars, rettv,
2069 funcexe->basetv);
2070 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002071 else
2072 {
2073 /*
2074 * Find the function name in the table, call its implementation.
2075 */
2076 error = call_internal_func(fname, argcount, argvars, rettv);
2077 }
2078 /*
2079 * The function call (or "FuncUndefined" autocommand sequence) might
2080 * have been aborted by an error, an interrupt, or an explicitly thrown
2081 * exception that has not been caught so far. This situation can be
2082 * tested for by calling aborting(). For an error in an internal
2083 * function or for the "E132" error in call_user_func(), however, the
2084 * throw point at which the "force_abort" flag (temporarily reset by
2085 * emsg()) is normally updated has not been reached yet. We need to
2086 * update that flag first to make aborting() reliable.
2087 */
2088 update_force_abort();
2089 }
Bram Moolenaaref140542019-12-31 21:27:13 +01002090 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002091 ret = OK;
2092
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002093theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002094 /*
2095 * Report an error unless the argument evaluation or function call has been
2096 * cancelled due to an aborting error, an interrupt, or an exception.
2097 */
2098 if (!aborting())
2099 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002100 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002101 }
2102
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002103 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002104 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002105 clear_tv(&argv[--argv_clear + argv_base]);
2106
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002107 vim_free(tofree);
2108 vim_free(name);
2109
2110 return ret;
2111}
2112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 static char_u *
2114printable_func_name(ufunc_T *fp)
2115{
2116 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
2117}
2118
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002119/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002120 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002121 */
2122 static void
2123list_func_head(ufunc_T *fp, int indent)
2124{
2125 int j;
2126
2127 msg_start();
2128 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002129 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002130 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002131 msg_puts("def ");
2132 else
2133 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002135 msg_putchar('(');
2136 for (j = 0; j < fp->uf_args.ga_len; ++j)
2137 {
2138 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002139 msg_puts(", ");
2140 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141 if (fp->uf_arg_types != NULL)
2142 {
2143 char *tofree;
2144
2145 msg_puts(": ");
2146 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
2147 vim_free(tofree);
2148 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002149 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
2150 {
2151 msg_puts(" = ");
2152 msg_puts(((char **)(fp->uf_def_args.ga_data))
2153 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
2154 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002155 }
2156 if (fp->uf_varargs)
2157 {
2158 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002159 msg_puts(", ");
2160 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002161 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 if (fp->uf_va_name != NULL)
2163 {
2164 if (j)
2165 msg_puts(", ");
2166 msg_puts("...");
2167 msg_puts((char *)fp->uf_va_name);
2168 if (fp->uf_va_type)
2169 {
2170 char *tofree;
2171
2172 msg_puts(": ");
2173 msg_puts(type_name(fp->uf_va_type, &tofree));
2174 vim_free(tofree);
2175 }
2176 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002177 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002178
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002179 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002180 {
2181 if (fp->uf_ret_type != &t_void)
2182 {
2183 char *tofree;
2184
2185 msg_puts(": ");
2186 msg_puts(type_name(fp->uf_ret_type, &tofree));
2187 vim_free(tofree);
2188 }
2189 }
2190 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002191 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002192 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002193 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002194 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002195 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002196 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002197 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002198 msg_clr_eos();
2199 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002200 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002201}
2202
2203/*
2204 * Get a function name, translating "<SID>" and "<SNR>".
2205 * Also handles a Funcref in a List or Dictionary.
2206 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002207 * Set "*is_global" to TRUE when the function must be global, unless
2208 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002209 * flags:
2210 * TFN_INT: internal function name OK
2211 * TFN_QUIET: be quiet
2212 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002213 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002214 * Advances "pp" to just after the function name (if no error).
2215 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002216 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002217trans_function_name(
2218 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002219 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002220 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002221 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002222 funcdict_T *fdp, // return: info about dictionary used
2223 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002224{
2225 char_u *name = NULL;
2226 char_u *start;
2227 char_u *end;
2228 int lead;
2229 char_u sid_buf[20];
2230 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002232 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002234
2235 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002236 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002237 start = *pp;
2238
Bram Moolenaare38eab22019-12-05 21:50:01 +01002239 // Check for hard coded <SNR>: already translated function ID (from a user
2240 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002241 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2242 && (*pp)[2] == (int)KE_SNR)
2243 {
2244 *pp += 3;
2245 len = get_id_len(pp) + 3;
2246 return vim_strnsave(start, len);
2247 }
2248
Bram Moolenaare38eab22019-12-05 21:50:01 +01002249 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2250 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002251 lead = eval_fname_script(start);
2252 if (lead > 2)
2253 start += lead;
2254
Bram Moolenaare38eab22019-12-05 21:50:01 +01002255 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002256 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002257 lead > 2 ? 0 : FNE_CHECK_START);
2258 if (end == start)
2259 {
2260 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002261 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002262 goto theend;
2263 }
2264 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2265 {
2266 /*
2267 * Report an invalid expression in braces, unless the expression
2268 * evaluation has been cancelled due to an aborting error, an
2269 * interrupt, or an exception.
2270 */
2271 if (!aborting())
2272 {
2273 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002274 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002275 }
2276 else
2277 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2278 goto theend;
2279 }
2280
2281 if (lv.ll_tv != NULL)
2282 {
2283 if (fdp != NULL)
2284 {
2285 fdp->fd_dict = lv.ll_dict;
2286 fdp->fd_newkey = lv.ll_newkey;
2287 lv.ll_newkey = NULL;
2288 fdp->fd_di = lv.ll_di;
2289 }
2290 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2291 {
2292 name = vim_strsave(lv.ll_tv->vval.v_string);
2293 *pp = end;
2294 }
2295 else if (lv.ll_tv->v_type == VAR_PARTIAL
2296 && lv.ll_tv->vval.v_partial != NULL)
2297 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002298 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002299 *pp = end;
2300 if (partial != NULL)
2301 *partial = lv.ll_tv->vval.v_partial;
2302 }
2303 else
2304 {
2305 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2306 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002307 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002308 else
2309 *pp = end;
2310 name = NULL;
2311 }
2312 goto theend;
2313 }
2314
2315 if (lv.ll_name == NULL)
2316 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002317 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002318 *pp = end;
2319 goto theend;
2320 }
2321
Bram Moolenaare38eab22019-12-05 21:50:01 +01002322 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002323 if (lv.ll_exp_name != NULL)
2324 {
2325 len = (int)STRLEN(lv.ll_exp_name);
2326 name = deref_func_name(lv.ll_exp_name, &len, partial,
2327 flags & TFN_NO_AUTOLOAD);
2328 if (name == lv.ll_exp_name)
2329 name = NULL;
2330 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002331 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002332 {
2333 len = (int)(end - *pp);
2334 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2335 if (name == *pp)
2336 name = NULL;
2337 }
2338 if (name != NULL)
2339 {
2340 name = vim_strsave(name);
2341 *pp = end;
2342 if (STRNCMP(name, "<SNR>", 5) == 0)
2343 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002344 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002345 name[0] = K_SPECIAL;
2346 name[1] = KS_EXTRA;
2347 name[2] = (int)KE_SNR;
2348 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2349 }
2350 goto theend;
2351 }
2352
2353 if (lv.ll_exp_name != NULL)
2354 {
2355 len = (int)STRLEN(lv.ll_exp_name);
2356 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2357 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2358 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002359 // When there was "s:" already or the name expanded to get a
2360 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002361 lv.ll_name += 2;
2362 len -= 2;
2363 lead = 2;
2364 }
2365 }
2366 else
2367 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002368 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002369 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002370 {
2371 if (is_global != NULL && lv.ll_name[0] == 'g')
2372 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002373 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002374 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002375 len = (int)(end - lv.ll_name);
2376 }
2377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002378 // In Vim9 script a user function is script-local by default.
2379 vim9script = ASCII_ISUPPER(*start)
2380 && current_sctx.sc_version == SCRIPT_VERSION_VIM9;
2381
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002382 /*
2383 * Copy the function name to allocated memory.
2384 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2385 * Accept <SNR>123_name() outside a script.
2386 */
2387 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002388 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002390 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 if (!vim9script)
2392 lead = 3;
2393 if (vim9script || (lv.ll_exp_name != NULL
2394 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002395 || eval_fname_sid(*pp))
2396 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002398 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002399 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002400 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002401 goto theend;
2402 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002403 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404 if (vim9script)
2405 extra = 3 + (int)STRLEN(sid_buf);
2406 else
2407 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002408 }
2409 }
2410 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2411 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002412 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002413 start);
2414 goto theend;
2415 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002416 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002417 {
2418 char_u *cp = vim_strchr(lv.ll_name, ':');
2419
2420 if (cp != NULL && cp < end)
2421 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002422 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002423 goto theend;
2424 }
2425 }
2426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002427 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002428 if (name != NULL)
2429 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002430 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002431 {
2432 name[0] = K_SPECIAL;
2433 name[1] = KS_EXTRA;
2434 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002436 STRCPY(name + 3, sid_buf);
2437 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2439 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002440 }
2441 *pp = end;
2442
2443theend:
2444 clear_lval(&lv);
2445 return name;
2446}
2447
2448/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02002449 * Assuming "name" is the result of trans_function_name() and it was prefixed
2450 * to use the script-local name, return the unmodified name (points into
2451 * "name"). Otherwise return NULL.
2452 * This can be used to first search for a script-local function and fall back
2453 * to the global function if not found.
2454 */
2455 char_u *
2456untrans_function_name(char_u *name)
2457{
2458 char_u *p;
2459
2460 if (*name == K_SPECIAL && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2461 {
2462 p = vim_strchr(name, '_');
2463 if (p != NULL)
2464 return p + 1;
2465 }
2466 return NULL;
2467}
2468
2469/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002470 * List functions. When "regmatch" is NULL all of then.
2471 * Otherwise functions matching "regmatch".
2472 */
2473 static void
2474list_functions(regmatch_T *regmatch)
2475{
2476 long_u used = func_hashtab.ht_used;
2477 long_u todo = used;
2478 hashitem_T *ht_array = func_hashtab.ht_array;
2479 hashitem_T *hi;
2480
2481 for (hi = ht_array; todo > 0 && !got_int; ++hi)
2482 {
2483 if (!HASHITEM_EMPTY(hi))
2484 {
2485 ufunc_T *fp = HI2UF(hi);
2486
2487 --todo;
2488 if ((fp->uf_flags & FC_DEAD) == 0
2489 && (regmatch == NULL
2490 ? !message_filtered(fp->uf_name)
2491 && !func_name_refcount(fp->uf_name)
2492 : !isdigit(*fp->uf_name)
2493 && vim_regexec(regmatch, fp->uf_name, 0)))
2494 {
2495 list_func_head(fp, FALSE);
2496 if (used != func_hashtab.ht_used
2497 || ht_array != func_hashtab.ht_array)
2498 {
2499 emsg(_("E454: function list was modified"));
2500 return;
2501 }
2502 }
2503 }
2504 }
2505}
2506
2507/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02002508 * ":function" also supporting nested ":def".
2509 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002510 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02002511 ufunc_T *
Bram Moolenaar822ba242020-05-24 23:00:18 +02002512def_function(exarg_T *eap, char_u *name_arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002513{
2514 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002515 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002516 int j;
2517 int c;
2518 int saved_did_emsg;
2519 int saved_wait_return = need_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002520 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002521 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002522 char_u *p;
2523 char_u *arg;
2524 char_u *line_arg = NULL;
2525 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002527 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002528 garray_T newlines;
2529 int varargs = FALSE;
2530 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002532 ufunc_T *fp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002533 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002534 int indent;
2535 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536#define MAX_FUNC_NESTING 50
2537 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002538 dictitem_T *v;
2539 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002540 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002541 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002542 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002543 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002544 linenr_T sourcing_lnum_off;
2545 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002546 int is_heredoc = FALSE;
2547 char_u *skip_until = NULL;
2548 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002549
Bram Moolenaar822ba242020-05-24 23:00:18 +02002550 if (in_vim9script() && eap->forceit)
2551 {
2552 emsg(_(e_nobang));
2553 return NULL;
2554 }
2555
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002556 /*
2557 * ":function" without argument: list functions.
2558 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002559 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002560 {
2561 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002562 list_functions(NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002563 eap->nextcmd = check_nextcmd(eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02002564 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002565 }
2566
2567 /*
2568 * ":function /pat": list functions matching pattern.
2569 */
2570 if (*eap->arg == '/')
2571 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002572 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002573 if (!eap->skip)
2574 {
2575 regmatch_T regmatch;
2576
2577 c = *p;
2578 *p = NUL;
2579 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2580 *p = c;
2581 if (regmatch.regprog != NULL)
2582 {
2583 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002584 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002585 vim_regfree(regmatch.regprog);
2586 }
2587 }
2588 if (*p == '/')
2589 ++p;
2590 eap->nextcmd = check_nextcmd(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02002591 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002592 }
2593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 ga_init(&newargs);
2595 ga_init(&argtypes);
2596 ga_init(&default_args);
2597
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002598 /*
2599 * Get the function name. There are these situations:
2600 * func normal function name
2601 * "name" == func, "fudi.fd_dict" == NULL
2602 * dict.func new dictionary entry
2603 * "name" == NULL, "fudi.fd_dict" set,
2604 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2605 * dict.func existing dict entry with a Funcref
2606 * "name" == func, "fudi.fd_dict" set,
2607 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2608 * dict.func existing dict entry that's not a Funcref
2609 * "name" == NULL, "fudi.fd_dict" set,
2610 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2611 * s:func script-local function name
2612 * g:func global function name, same as "func"
2613 */
2614 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002615 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002616 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02002617 // nested function, argument is (args).
2618 paren = TRUE;
2619 CLEAR_FIELD(fudi);
2620 }
2621 else
2622 {
2623 name = trans_function_name(&p, &is_global, eap->skip,
2624 TFN_NO_AUTOLOAD, &fudi, NULL);
2625 paren = (vim_strchr(p, '(') != NULL);
2626 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002627 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02002628 /*
2629 * Return on an invalid expression in braces, unless the expression
2630 * evaluation has been cancelled due to an aborting error, an
2631 * interrupt, or an exception.
2632 */
2633 if (!aborting())
2634 {
2635 if (!eap->skip && fudi.fd_newkey != NULL)
2636 semsg(_(e_dictkey), fudi.fd_newkey);
2637 vim_free(fudi.fd_newkey);
2638 return NULL;
2639 }
2640 else
2641 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002642 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002643 }
2644
Bram Moolenaare38eab22019-12-05 21:50:01 +01002645 // An error in a function call during evaluation of an expression in magic
2646 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002647 saved_did_emsg = did_emsg;
2648 did_emsg = FALSE;
2649
2650 /*
2651 * ":function func" with only function name: list function.
2652 */
2653 if (!paren)
2654 {
2655 if (!ends_excmd(*skipwhite(p)))
2656 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002657 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002658 goto ret_free;
2659 }
2660 eap->nextcmd = check_nextcmd(p);
2661 if (eap->nextcmd != NULL)
2662 *p = NUL;
2663 if (!eap->skip && !got_int)
2664 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002665 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002666 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
2667 {
2668 char_u *up = untrans_function_name(name);
2669
2670 // With Vim9 script the name was made script-local, if not
2671 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02002672 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002673 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002674 }
2675
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002676 if (fp != NULL)
2677 {
2678 list_func_head(fp, TRUE);
2679 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2680 {
2681 if (FUNCLINE(fp, j) == NULL)
2682 continue;
2683 msg_putchar('\n');
2684 msg_outnum((long)(j + 1));
2685 if (j < 9)
2686 msg_putchar(' ');
2687 if (j < 99)
2688 msg_putchar(' ');
2689 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002690 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002691 ui_breakcheck();
2692 }
2693 if (!got_int)
2694 {
2695 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002696 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 msg_puts(" enddef");
2698 else
2699 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002700 }
2701 }
2702 else
Bram Moolenaara26b9702020-04-18 19:53:28 +02002703 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002704 }
2705 goto ret_free;
2706 }
2707
2708 /*
2709 * ":function name(arg1, arg2)" Define function.
2710 */
2711 p = skipwhite(p);
2712 if (*p != '(')
2713 {
2714 if (!eap->skip)
2715 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002716 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002717 goto ret_free;
2718 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002719 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002720 if (vim_strchr(p, '(') != NULL)
2721 p = vim_strchr(p, '(');
2722 }
2723 p = skipwhite(p + 1);
2724
2725 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2726
Bram Moolenaar04b12692020-05-04 23:24:44 +02002727 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002728 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002729 // Check the name of the function. Unless it's a dictionary function
2730 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002731 if (name != NULL)
2732 arg = name;
2733 else
2734 arg = fudi.fd_newkey;
2735 if (arg != NULL && (fudi.fd_di == NULL
2736 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2737 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2738 {
2739 if (*arg == K_SPECIAL)
2740 j = 3;
2741 else
2742 j = 0;
2743 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2744 : eval_isnamec(arg[j])))
2745 ++j;
2746 if (arg[j] != NUL)
2747 emsg_funcname((char *)e_invarg2, arg);
2748 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002749 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002750 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002751 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002752 }
2753
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002754 // This may get more lines and make the pointers into the first line
2755 // invalid.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 if (get_function_args(&p, ')', &newargs,
2757 eap->cmdidx == CMD_def ? &argtypes : NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002758 &varargs, &default_args, eap->skip,
2759 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002760 goto errret_2;
2761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002763 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 // find the return type: :def Func(): type
2765 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002766 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 ret_type = skipwhite(p + 1);
2768 p = skip_type(ret_type);
2769 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002770 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002771 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002773 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002775 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 semsg(_("E1056: expected a type: %s"), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02002777 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002778 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002779 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002780 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781 else
2782 // find extra arguments "range", "dict", "abort" and "closure"
2783 for (;;)
2784 {
2785 p = skipwhite(p);
2786 if (STRNCMP(p, "range", 5) == 0)
2787 {
2788 flags |= FC_RANGE;
2789 p += 5;
2790 }
2791 else if (STRNCMP(p, "dict", 4) == 0)
2792 {
2793 flags |= FC_DICT;
2794 p += 4;
2795 }
2796 else if (STRNCMP(p, "abort", 5) == 0)
2797 {
2798 flags |= FC_ABORT;
2799 p += 5;
2800 }
2801 else if (STRNCMP(p, "closure", 7) == 0)
2802 {
2803 flags |= FC_CLOSURE;
2804 p += 7;
2805 if (current_funccal == NULL)
2806 {
2807 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
2808 name == NULL ? (char_u *)"" : name);
2809 goto erret;
2810 }
2811 }
2812 else
2813 break;
2814 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002815
Bram Moolenaare38eab22019-12-05 21:50:01 +01002816 // When there is a line break use what follows for the function body.
2817 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002818 if (*p == '\n')
2819 line_arg = p + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002820 else if (*p != NUL && *p != '"' && !(eap->cmdidx == CMD_def && *p == '#')
2821 && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002822 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002823
2824 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
2826 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002827 */
2828 if (KeyTyped)
2829 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002830 // Check if the function already exists, don't let the user type the
2831 // whole function before telling him it doesn't work! For a script we
2832 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002833 if (!eap->skip && !eap->forceit)
2834 {
2835 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002836 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002837 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002838 emsg_funcname(e_funcexts, name);
2839 }
2840
2841 if (!eap->skip && did_emsg)
2842 goto erret;
2843
Bram Moolenaare38eab22019-12-05 21:50:01 +01002844 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002845 cmdline_row = msg_row;
2846 }
2847
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002848 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002849 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002850
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002851 indent = 2;
2852 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002854 for (;;)
2855 {
2856 if (KeyTyped)
2857 {
2858 msg_scroll = TRUE;
2859 saved_wait_return = FALSE;
2860 }
2861 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002862
2863 if (line_arg != NULL)
2864 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002865 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002866 theline = line_arg;
2867 p = vim_strchr(theline, '\n');
2868 if (p == NULL)
2869 line_arg += STRLEN(line_arg);
2870 else
2871 {
2872 *p = NUL;
2873 line_arg = p + 1;
2874 }
2875 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002876 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002877 {
2878 vim_free(line_to_free);
2879 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002880 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002881 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002882 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002883 line_to_free = theline;
2884 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002885 if (KeyTyped)
2886 lines_left = Rows - 1;
2887 if (theline == NULL)
2888 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 if (eap->cmdidx == CMD_def)
2890 emsg(_("E1057: Missing :enddef"));
2891 else
2892 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002893 goto erret;
2894 }
2895
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002896 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002897 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002898 if (SOURCING_LNUM < sourcing_lnum_off)
2899 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002900 else
2901 sourcing_lnum_off = 0;
2902
2903 if (skip_until != NULL)
2904 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002906 // * ":append" and "."
2907 // * ":python <<EOF" and "EOF"
2908 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2909 if (heredoc_trimmed == NULL
2910 || (is_heredoc && skipwhite(theline) == theline)
2911 || STRNCMP(theline, heredoc_trimmed,
2912 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002913 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002914 if (heredoc_trimmed == NULL)
2915 p = theline;
2916 else if (is_heredoc)
2917 p = skipwhite(theline) == theline
2918 ? theline : theline + STRLEN(heredoc_trimmed);
2919 else
2920 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002921 if (STRCMP(p, skip_until) == 0)
2922 {
2923 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002924 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002925 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002926 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002927 }
2928 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002929 }
2930 else
2931 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002932 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01002933 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002934 ;
2935
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 // Check for "endfunction" or "enddef".
2937 if (checkforcmd(&p, nesting_def[nesting]
2938 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002939 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002940 char_u *nextcmd = NULL;
2941
Bram Moolenaar663bb232017-06-22 19:12:10 +02002942 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002943 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002944 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002945 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002946 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 give_warning2(eap->cmdidx == CMD_def
2948 ? (char_u *)_("W1001: Text found after :enddef: %s")
2949 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002950 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002951 if (nextcmd != NULL)
2952 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002953 // Another command follows. If the line came from "eap" we
2954 // can simply point into it, otherwise we need to change
2955 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02002956 eap->nextcmd = nextcmd;
2957 if (line_to_free != NULL)
2958 {
2959 vim_free(*eap->cmdlinep);
2960 *eap->cmdlinep = line_to_free;
2961 line_to_free = NULL;
2962 }
2963 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002964 break;
2965 }
2966
Bram Moolenaare38eab22019-12-05 21:50:01 +01002967 // Increase indent inside "if", "while", "for" and "try", decrease
2968 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002970 indent -= 2;
2971 else if (STRNCMP(p, "if", 2) == 0
2972 || STRNCMP(p, "wh", 2) == 0
2973 || STRNCMP(p, "for", 3) == 0
2974 || STRNCMP(p, "try", 3) == 0)
2975 indent += 2;
2976
Bram Moolenaare38eab22019-12-05 21:50:01 +01002977 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01002978 // Only recognize "def" inside "def", not inside "function",
2979 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01002981 if (checkforcmd(&p, "function", 2)
2982 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002983 {
2984 if (*p == '!')
2985 p = skipwhite(p + 1);
2986 p += eval_fname_script(p);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002987 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL, NULL));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002988 if (*skipwhite(p) == '(')
2989 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 if (nesting == MAX_FUNC_NESTING - 1)
2991 emsg(_("E1058: function nesting too deep"));
2992 else
2993 {
2994 ++nesting;
2995 nesting_def[nesting] = (c == 'd');
2996 indent += 2;
2997 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002998 }
2999 }
3000
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003001 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003002 p = skip_range(p, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003003 if (eap->cmdidx != CMD_def
3004 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01003005 || (p[0] == 'c'
3006 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
3007 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
3008 && (STRNCMP(&p[3], "nge", 3) != 0
3009 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003010 || (p[0] == 'i'
3011 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003012 && (!ASCII_ISALPHA(p[2])
3013 || (p[2] == 's'
3014 && (!ASCII_ISALPHA(p[3])
3015 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003016 skip_until = vim_strsave((char_u *)".");
3017
Bram Moolenaare38eab22019-12-05 21:50:01 +01003018 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003019 arg = skipwhite(skiptowhite(p));
3020 if (arg[0] == '<' && arg[1] =='<'
3021 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003022 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
3023 || ((p[2] == '3' || p[2] == 'x')
3024 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003025 || (p[0] == 'p' && p[1] == 'e'
3026 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
3027 || (p[0] == 't' && p[1] == 'c'
3028 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
3029 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
3030 && !ASCII_ISALPHA(p[3]))
3031 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
3032 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
3033 || (p[0] == 'm' && p[1] == 'z'
3034 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
3035 ))
3036 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003037 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003038 p = skipwhite(arg + 2);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003039 if (STRNCMP(p, "trim", 4) == 0)
3040 {
3041 // Ignore leading white space.
3042 p = skipwhite(p + 4);
3043 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003044 skipwhite(theline) - theline);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003045 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003046 if (*p == NUL)
3047 skip_until = vim_strsave((char_u *)".");
3048 else
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003049 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003050 do_concat = FALSE;
3051 is_heredoc = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003052 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003053
3054 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003055 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02003056 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003057 if (*arg == '[')
3058 arg = vim_strchr(arg, ']');
3059 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003060 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003061 arg = skipwhite(skiptowhite(arg));
3062 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
3063 && ((p[0] == 'l'
3064 && p[1] == 'e'
3065 && (!ASCII_ISALNUM(p[2])
3066 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02003067 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003068 p = skipwhite(arg + 3);
3069 if (STRNCMP(p, "trim", 4) == 0)
3070 {
3071 // Ignore leading white space.
3072 p = skipwhite(p + 4);
3073 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003074 skipwhite(theline) - theline);
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003075 }
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003076 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003077 do_concat = FALSE;
3078 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003079 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003080 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003081 }
3082
Bram Moolenaare38eab22019-12-05 21:50:01 +01003083 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003084 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003085 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003086
Bram Moolenaare38eab22019-12-05 21:50:01 +01003087 // Copy the line to newly allocated memory. get_one_sourceline()
3088 // allocates 250 bytes per line, this saves 80% on average. The cost
3089 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003090 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003091 if (p == NULL)
3092 goto erret;
3093 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003094
Bram Moolenaare38eab22019-12-05 21:50:01 +01003095 // Add NULL lines for continuation lines, so that the line count is
3096 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003097 while (sourcing_lnum_off-- > 0)
3098 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3099
Bram Moolenaare38eab22019-12-05 21:50:01 +01003100 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003101 if (line_arg != NULL && *line_arg == NUL)
3102 line_arg = NULL;
3103 }
3104
Bram Moolenaare38eab22019-12-05 21:50:01 +01003105 // Don't define the function when skipping commands or when an error was
3106 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003107 if (eap->skip || did_emsg)
3108 goto erret;
3109
3110 /*
3111 * If there are no errors, add the function
3112 */
3113 if (fudi.fd_dict == NULL)
3114 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 hashtab_T *ht;
3116
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003117 v = find_var(name, &ht, FALSE);
3118 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
3119 {
3120 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
3121 name);
3122 goto erret;
3123 }
3124
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003125 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003126 if (fp != NULL)
3127 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 int dead = fp->uf_flags & FC_DEAD;
3129
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003130 // Function can be replaced with "function!" and when sourcing the
3131 // same script again, but only once.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 if (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003133 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
3134 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003135 {
3136 emsg_funcname(e_funcexts, name);
3137 goto erret;
3138 }
3139 if (fp->uf_calls > 0)
3140 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003141 emsg_funcname(
3142 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003143 name);
3144 goto erret;
3145 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003146 if (fp->uf_refcount > 1)
3147 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003148 // This function is referenced somewhere, don't redefine it but
3149 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003150 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003151 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003152 fp = NULL;
3153 overwrite = TRUE;
3154 }
3155 else
3156 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003157 char_u *exp_name = fp->uf_name_exp;
3158
3159 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01003160 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003161 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003162 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003163 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003165#ifdef FEAT_PROFILE
3166 fp->uf_profiling = FALSE;
3167 fp->uf_prof_initialized = FALSE;
3168#endif
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003169 clear_def_function(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003170 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003171 }
3172 }
3173 else
3174 {
3175 char numbuf[20];
3176
3177 fp = NULL;
3178 if (fudi.fd_newkey == NULL && !eap->forceit)
3179 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003180 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003181 goto erret;
3182 }
3183 if (fudi.fd_di == NULL)
3184 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003185 // Can't add a function to a locked dictionary
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003186 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003187 goto erret;
3188 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003189 // Can't change an existing function if it is locked
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003190 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003191 goto erret;
3192
Bram Moolenaare38eab22019-12-05 21:50:01 +01003193 // Give the function a sequential number. Can only be used with a
3194 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003195 vim_free(name);
3196 sprintf(numbuf, "%d", ++func_nr);
3197 name = vim_strsave((char_u *)numbuf);
3198 if (name == NULL)
3199 goto erret;
3200 }
3201
3202 if (fp == NULL)
3203 {
3204 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3205 {
3206 int slen, plen;
3207 char_u *scriptname;
3208
Bram Moolenaare38eab22019-12-05 21:50:01 +01003209 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003210 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003211 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003212 {
3213 scriptname = autoload_name(name);
3214 if (scriptname != NULL)
3215 {
3216 p = vim_strchr(scriptname, '/');
3217 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003218 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003219 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003220 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003221 j = OK;
3222 vim_free(scriptname);
3223 }
3224 }
3225 if (j == FAIL)
3226 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003227 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003228 goto erret;
3229 }
3230 }
3231
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003232 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003233 if (fp == NULL)
3234 goto erret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003235 fp->uf_def_status = eap->cmdidx == CMD_def ? UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003236 : UF_NOT_COMPILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003237
3238 if (fudi.fd_dict != NULL)
3239 {
3240 if (fudi.fd_di == NULL)
3241 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003242 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003243 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3244 if (fudi.fd_di == NULL)
3245 {
3246 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003247 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003248 goto erret;
3249 }
3250 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3251 {
3252 vim_free(fudi.fd_di);
3253 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003254 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003255 goto erret;
3256 }
3257 }
3258 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003259 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003260 clear_tv(&fudi.fd_di->di_tv);
3261 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003262 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003263
Bram Moolenaare38eab22019-12-05 21:50:01 +01003264 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003265 flags |= FC_DICT;
3266 }
3267
Bram Moolenaare38eab22019-12-05 21:50:01 +01003268 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003269 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003270 if (overwrite)
3271 {
3272 hi = hash_find(&func_hashtab, name);
3273 hi->hi_key = UF2HIKEY(fp);
3274 }
3275 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003276 {
3277 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003278 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003279 goto erret;
3280 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003281 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003282 }
3283 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003284 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003286 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287
3288 if (eap->cmdidx == CMD_def)
3289 {
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003290 int lnum_save = SOURCING_LNUM;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003291
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003292 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003293
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003294 // error messages are for the first function line
3295 SOURCING_LNUM = sourcing_lnum_top;
3296
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 // parse the argument types
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003298 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299
3300 if (argtypes.ga_len > 0)
3301 {
3302 // When "varargs" is set the last name/type goes into uf_va_name
3303 // and uf_va_type.
3304 int len = argtypes.ga_len - (varargs ? 1 : 0);
3305
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003306 if (len > 0)
3307 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 if (fp->uf_arg_types != NULL)
3309 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003310 int i;
3311 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312
3313 for (i = 0; i < len; ++ i)
3314 {
3315 p = ((char_u **)argtypes.ga_data)[i];
3316 if (p == NULL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003317 // will get the type from the default value
3318 type = &t_unknown;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 else
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003320 type = parse_type(&p, &fp->uf_type_list);
3321 if (type == NULL)
3322 {
3323 SOURCING_LNUM = lnum_save;
3324 goto errret_2;
3325 }
3326 fp->uf_arg_types[i] = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 }
3328 }
3329 if (varargs)
3330 {
3331 // Move the last argument "...name: type" to uf_va_name and
3332 // uf_va_type.
3333 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
3334 [fp->uf_args.ga_len - 1];
3335 --fp->uf_args.ga_len;
3336 p = ((char_u **)argtypes.ga_data)[len];
3337 if (p == NULL)
3338 // todo: get type from default value
3339 fp->uf_va_type = &t_any;
3340 else
3341 fp->uf_va_type = parse_type(&p, &fp->uf_type_list);
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003342 if (fp->uf_va_type == NULL)
3343 {
3344 SOURCING_LNUM = lnum_save;
3345 goto errret_2;
3346 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 }
3348 varargs = FALSE;
3349 }
3350
3351 // parse the return type, if any
3352 if (ret_type == NULL)
3353 fp->uf_ret_type = &t_void;
3354 else
3355 {
3356 p = ret_type;
3357 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list);
3358 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02003359 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003361 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003362 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003364 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003365 if ((flags & FC_CLOSURE) != 0)
3366 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003367 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003368 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003369 }
3370 else
3371 fp->uf_scoped = NULL;
3372
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003373#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003374 if (prof_def_func())
3375 func_do_profile(fp);
3376#endif
3377 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003378 if (sandbox)
3379 flags |= FC_SANDBOX;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003380 if (in_vim9script() && !ASCII_ISUPPER(*fp->uf_name))
3381 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003382 fp->uf_flags = flags;
3383 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003384 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003385 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003386 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387 if (is_export)
3388 {
3389 fp->uf_flags |= FC_EXPORT;
3390 // let ex_export() know the export worked.
3391 is_export = FALSE;
3392 }
3393
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003394 if (eap->cmdidx == CMD_def)
3395 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02003396 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
3397 // :func does not use Vim9 script syntax, even in a Vim9 script file
3398 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003399
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003400 goto ret_free;
3401
3402erret:
3403 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003404 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003405errret_2:
3406 ga_clear_strings(&newlines);
3407ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003408 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003409 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003410 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003411 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003412 if (name != name_arg)
3413 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003414 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003415 did_emsg |= saved_did_emsg;
3416 need_wait_return |= saved_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003417
3418 return fp;
3419}
3420
3421/*
3422 * ":function"
3423 */
3424 void
3425ex_function(exarg_T *eap)
3426{
Bram Moolenaar822ba242020-05-24 23:00:18 +02003427 (void)def_function(eap, NULL);
3428}
3429
3430/*
3431 * :defcompile - compile all :def functions in the current script.
3432 */
3433 void
3434ex_defcompile(exarg_T *eap UNUSED)
3435{
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003436 long_u ht_used = func_hashtab.ht_used;
3437 int todo = (int)ht_used;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003438 hashitem_T *hi;
3439 ufunc_T *ufunc;
3440
3441 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
3442 {
3443 if (!HASHITEM_EMPTY(hi))
3444 {
3445 --todo;
3446 ufunc = HI2UF(hi);
3447 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003448 && ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003449 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02003450 compile_def_function(ufunc, FALSE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003451
3452 if (func_hashtab.ht_used != ht_used)
3453 {
3454 // another function has been defined, need to start over
3455 hi = func_hashtab.ht_array;
3456 ht_used = func_hashtab.ht_used;
3457 todo = (int)ht_used;
Bram Moolenaar285b1892020-05-26 11:37:26 +02003458 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003459 }
3460 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003461 }
3462 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003463}
3464
3465/*
3466 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3467 * Return 2 if "p" starts with "s:".
3468 * Return 0 otherwise.
3469 */
3470 int
3471eval_fname_script(char_u *p)
3472{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003473 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3474 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003475 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3476 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3477 return 5;
3478 if (p[0] == 's' && p[1] == ':')
3479 return 2;
3480 return 0;
3481}
3482
3483 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003484translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003485{
3486 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003487 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003488 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489}
3490
3491/*
3492 * Return TRUE when "ufunc" has old-style "..." varargs
3493 * or named varargs "...name: type".
3494 */
3495 int
3496has_varargs(ufunc_T *ufunc)
3497{
3498 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003499}
3500
3501/*
3502 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003503 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003504 */
3505 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003506function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003507{
3508 char_u *nm = name;
3509 char_u *p;
3510 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003511 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003512 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003513
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003514 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3515 if (no_deref)
3516 flag |= TFN_NO_DEREF;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003517 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003518 nm = skipwhite(nm);
3519
Bram Moolenaare38eab22019-12-05 21:50:01 +01003520 // Only accept "funcname", "funcname ", "funcname (..." and
3521 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003522 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003523 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003524 vim_free(p);
3525 return n;
3526}
3527
Bram Moolenaar113e1072019-01-20 15:30:40 +01003528#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003529 char_u *
3530get_expanded_name(char_u *name, int check)
3531{
3532 char_u *nm = name;
3533 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003534 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003535
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003536 p = trans_function_name(&nm, &is_global, FALSE,
3537 TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003538
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003539 if (p != NULL && *nm == NUL
3540 && (!check || translated_function_exists(p, is_global)))
3541 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003542
3543 vim_free(p);
3544 return NULL;
3545}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003546#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003547
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003548/*
3549 * Function given to ExpandGeneric() to obtain the list of user defined
3550 * function names.
3551 */
3552 char_u *
3553get_user_func_name(expand_T *xp, int idx)
3554{
3555 static long_u done;
3556 static hashitem_T *hi;
3557 ufunc_T *fp;
3558
3559 if (idx == 0)
3560 {
3561 done = 0;
3562 hi = func_hashtab.ht_array;
3563 }
3564 if (done < func_hashtab.ht_used)
3565 {
3566 if (done++ > 0)
3567 ++hi;
3568 while (HASHITEM_EMPTY(hi))
3569 ++hi;
3570 fp = HI2UF(hi);
3571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 // don't show dead, dict and lambda functions
3573 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02003574 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003576
3577 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003578 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003579
3580 cat_func_name(IObuff, fp);
3581 if (xp->xp_context != EXPAND_USER_FUNC)
3582 {
3583 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003585 STRCAT(IObuff, ")");
3586 }
3587 return IObuff;
3588 }
3589 return NULL;
3590}
3591
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003592/*
3593 * ":delfunction {name}"
3594 */
3595 void
3596ex_delfunction(exarg_T *eap)
3597{
3598 ufunc_T *fp = NULL;
3599 char_u *p;
3600 char_u *name;
3601 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003602 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003603
3604 p = eap->arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003605 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003606 vim_free(fudi.fd_newkey);
3607 if (name == NULL)
3608 {
3609 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003610 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003611 return;
3612 }
3613 if (!ends_excmd(*skipwhite(p)))
3614 {
3615 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003616 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003617 return;
3618 }
3619 eap->nextcmd = check_nextcmd(p);
3620 if (eap->nextcmd != NULL)
3621 *p = NUL;
3622
3623 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003624 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003625 vim_free(name);
3626
3627 if (!eap->skip)
3628 {
3629 if (fp == NULL)
3630 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02003631 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003632 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003633 return;
3634 }
3635 if (fp->uf_calls > 0)
3636 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003637 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003638 return;
3639 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003640 if (fp->uf_flags & FC_VIM9)
3641 {
3642 semsg(_("E1084: Cannot delete Vim9 script function %s"), eap->arg);
3643 return;
3644 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003645
3646 if (fudi.fd_dict != NULL)
3647 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003648 // Delete the dict item that refers to the function, it will
3649 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003650 dictitem_remove(fudi.fd_dict, fudi.fd_di);
3651 }
3652 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003653 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003654 // A normal function (not a numbered function or lambda) has a
3655 // refcount of 1 for the entry in the hashtable. When deleting
3656 // it and the refcount is more than one, it should be kept.
3657 // A numbered function and lambda should be kept if the refcount is
3658 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003659 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003660 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003661 // Function is still referenced somewhere. Don't free it but
3662 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003663 if (func_remove(fp))
3664 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003665 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003666 }
3667 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003668 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003669 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003670 }
3671}
3672
3673/*
3674 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003675 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003676 */
3677 void
3678func_unref(char_u *name)
3679{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003680 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003681
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003682 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003683 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003684 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003685 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003686 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003687#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003688 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003689#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01003690 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003691 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003692 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003693 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003694 // Only delete it when it's not being used. Otherwise it's done
3695 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003696 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003697 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02003698 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003699}
3700
3701/*
3702 * Unreference a Function: decrement the reference count and free it when it
3703 * becomes zero.
3704 */
3705 void
3706func_ptr_unref(ufunc_T *fp)
3707{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003708 if (fp != NULL && --fp->uf_refcount <= 0)
3709 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003710 // Only delete it when it's not being used. Otherwise it's done
3711 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02003712 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003713 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003714 }
3715}
3716
3717/*
3718 * Count a reference to a Function.
3719 */
3720 void
3721func_ref(char_u *name)
3722{
3723 ufunc_T *fp;
3724
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003725 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003726 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003727 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003728 if (fp != NULL)
3729 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003730 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01003731 // Only give an error for a numbered function.
3732 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01003733 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003734}
3735
3736/*
3737 * Count a reference to a Function.
3738 */
3739 void
3740func_ptr_ref(ufunc_T *fp)
3741{
3742 if (fp != NULL)
3743 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003744}
3745
3746/*
3747 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3748 * referenced from anywhere that is in use.
3749 */
3750 static int
3751can_free_funccal(funccall_T *fc, int copyID)
3752{
3753 return (fc->l_varlist.lv_copyID != copyID
3754 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003755 && fc->l_avars.dv_copyID != copyID
3756 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003757}
3758
3759/*
3760 * ":return [expr]"
3761 */
3762 void
3763ex_return(exarg_T *eap)
3764{
3765 char_u *arg = eap->arg;
3766 typval_T rettv;
3767 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003768 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003769
3770 if (current_funccal == NULL)
3771 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003772 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003773 return;
3774 }
3775
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003776 CLEAR_FIELD(evalarg);
3777 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
3778
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003779 if (eap->skip)
3780 ++emsg_skip;
3781
3782 eap->nextcmd = NULL;
3783 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003784 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003785 {
3786 if (!eap->skip)
3787 returning = do_return(eap, FALSE, TRUE, &rettv);
3788 else
3789 clear_tv(&rettv);
3790 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003791 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003792 else if (!eap->skip)
3793 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003794 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003795 update_force_abort();
3796
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003797 /*
3798 * Return unless the expression evaluation has been cancelled due to an
3799 * aborting error, an interrupt, or an exception.
3800 */
3801 if (!aborting())
3802 returning = do_return(eap, FALSE, TRUE, NULL);
3803 }
3804
Bram Moolenaare38eab22019-12-05 21:50:01 +01003805 // When skipping or the return gets pending, advance to the next command
3806 // in this line (!returning). Otherwise, ignore the rest of the line.
3807 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003808 if (returning)
3809 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003810 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003811 eap->nextcmd = check_nextcmd(arg);
3812
3813 if (eap->skip)
3814 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02003815 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003816}
3817
3818/*
3819 * ":1,25call func(arg1, arg2)" function call.
3820 */
3821 void
3822ex_call(exarg_T *eap)
3823{
3824 char_u *arg = eap->arg;
3825 char_u *startarg;
3826 char_u *name;
3827 char_u *tofree;
3828 int len;
3829 typval_T rettv;
3830 linenr_T lnum;
3831 int doesrange;
3832 int failed = FALSE;
3833 funcdict_T fudi;
3834 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003835 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003836
Bram Moolenaare6b53242020-07-01 17:28:33 +02003837 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003838 if (eap->skip)
3839 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003840 // trans_function_name() doesn't work well when skipping, use eval0()
3841 // instead to skip to any following command, e.g. for:
3842 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003843 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003844 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003845 clear_tv(&rettv);
3846 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003847 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003848 return;
3849 }
3850
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003851 tofree = trans_function_name(&arg, NULL, eap->skip,
3852 TFN_INT, &fudi, &partial);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003853 if (fudi.fd_newkey != NULL)
3854 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003855 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003856 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003857 vim_free(fudi.fd_newkey);
3858 }
3859 if (tofree == NULL)
3860 return;
3861
Bram Moolenaare38eab22019-12-05 21:50:01 +01003862 // Increase refcount on dictionary, it could get deleted when evaluating
3863 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003864 if (fudi.fd_dict != NULL)
3865 ++fudi.fd_dict->dv_refcount;
3866
Bram Moolenaare38eab22019-12-05 21:50:01 +01003867 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3868 // contents. For VAR_PARTIAL get its partial, unless we already have one
3869 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003870 len = (int)STRLEN(tofree);
3871 name = deref_func_name(tofree, &len,
3872 partial != NULL ? NULL : &partial, FALSE);
3873
Bram Moolenaare38eab22019-12-05 21:50:01 +01003874 // Skip white space to allow ":call func ()". Not good, but required for
3875 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003876 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003877 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003878
3879 if (*startarg != '(')
3880 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003882 goto end;
3883 }
3884
3885 /*
3886 * When skipping, evaluate the function once, to find the end of the
3887 * arguments.
3888 * When the function takes a range, this is discovered after the first
3889 * call, and the loop is broken.
3890 */
3891 if (eap->skip)
3892 {
3893 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003894 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003895 }
3896 else
3897 lnum = eap->line1;
3898 for ( ; lnum <= eap->line2; ++lnum)
3899 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003900 funcexe_T funcexe;
3901
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003902 if (!eap->skip && eap->addr_count > 0)
3903 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003904 if (lnum > curbuf->b_ml.ml_line_count)
3905 {
3906 // If the function deleted lines or switched to another buffer
3907 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003908 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003909 break;
3910 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003911 curwin->w_cursor.lnum = lnum;
3912 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003913 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003914 }
3915 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003916
Bram Moolenaara80faa82020-04-12 19:37:17 +02003917 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003918 funcexe.firstline = eap->line1;
3919 funcexe.lastline = eap->line2;
3920 funcexe.doesrange = &doesrange;
3921 funcexe.evaluate = !eap->skip;
3922 funcexe.partial = partial;
3923 funcexe.selfdict = fudi.fd_dict;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003924 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003925 {
3926 failed = TRUE;
3927 break;
3928 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003929 if (has_watchexpr())
3930 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003931
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003932 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003933 if (handle_subscript(&arg, &rettv,
3934 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003935 {
3936 failed = TRUE;
3937 break;
3938 }
3939
3940 clear_tv(&rettv);
3941 if (doesrange || eap->skip)
3942 break;
3943
Bram Moolenaare38eab22019-12-05 21:50:01 +01003944 // Stop when immediately aborting on error, or when an interrupt
3945 // occurred or an exception was thrown but not caught.
3946 // get_func_tv() returned OK, so that the check for trailing
3947 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003948 if (aborting())
3949 break;
3950 }
3951 if (eap->skip)
3952 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003953 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003954
Bram Moolenaare51bb172020-02-16 19:42:23 +01003955 // When inside :try we need to check for following "| catch".
3956 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003957 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003958 // Check for trailing illegal characters and a following command.
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003959 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003960 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01003961 if (!failed)
3962 {
3963 emsg_severe = TRUE;
3964 emsg(_(e_trailing));
3965 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003966 }
3967 else
3968 eap->nextcmd = check_nextcmd(arg);
3969 }
3970
3971end:
3972 dict_unref(fudi.fd_dict);
3973 vim_free(tofree);
3974}
3975
3976/*
3977 * Return from a function. Possibly makes the return pending. Also called
3978 * for a pending return at the ":endtry" or after returning from an extra
3979 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3980 * when called due to a ":return" command. "rettv" may point to a typval_T
3981 * with the return rettv. Returns TRUE when the return can be carried out,
3982 * FALSE when the return gets pending.
3983 */
3984 int
3985do_return(
3986 exarg_T *eap,
3987 int reanimate,
3988 int is_cmd,
3989 void *rettv)
3990{
3991 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01003992 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003993
3994 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003995 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003996 current_funccal->returned = FALSE;
3997
3998 /*
3999 * Cleanup (and inactivate) conditionals, but stop when a try conditional
4000 * not in its finally clause (which then is to be executed next) is found.
4001 * In this case, make the ":return" pending for execution at the ":endtry".
4002 * Otherwise, return normally.
4003 */
4004 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
4005 if (idx >= 0)
4006 {
4007 cstack->cs_pending[idx] = CSTP_RETURN;
4008
4009 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004010 // A pending return again gets pending. "rettv" points to an
4011 // allocated variable with the rettv of the original ":return"'s
4012 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004013 cstack->cs_rettv[idx] = rettv;
4014 else
4015 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004016 // When undoing a return in order to make it pending, get the stored
4017 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004018 if (reanimate)
4019 rettv = current_funccal->rettv;
4020
4021 if (rettv != NULL)
4022 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004023 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004024 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
4025 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
4026 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004027 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004028 }
4029 else
4030 cstack->cs_rettv[idx] = NULL;
4031
4032 if (reanimate)
4033 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004034 // The pending return value could be overwritten by a ":return"
4035 // without argument in a finally clause; reset the default
4036 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004037 current_funccal->rettv->v_type = VAR_NUMBER;
4038 current_funccal->rettv->vval.v_number = 0;
4039 }
4040 }
4041 report_make_pending(CSTP_RETURN, rettv);
4042 }
4043 else
4044 {
4045 current_funccal->returned = TRUE;
4046
Bram Moolenaare38eab22019-12-05 21:50:01 +01004047 // If the return is carried out now, store the return value. For
4048 // a return immediately after reanimation, the value is already
4049 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004050 if (!reanimate && rettv != NULL)
4051 {
4052 clear_tv(current_funccal->rettv);
4053 *current_funccal->rettv = *(typval_T *)rettv;
4054 if (!is_cmd)
4055 vim_free(rettv);
4056 }
4057 }
4058
4059 return idx < 0;
4060}
4061
4062/*
4063 * Free the variable with a pending return value.
4064 */
4065 void
4066discard_pending_return(void *rettv)
4067{
4068 free_tv((typval_T *)rettv);
4069}
4070
4071/*
4072 * Generate a return command for producing the value of "rettv". The result
4073 * is an allocated string. Used by report_pending() for verbose messages.
4074 */
4075 char_u *
4076get_return_cmd(void *rettv)
4077{
4078 char_u *s = NULL;
4079 char_u *tofree = NULL;
4080 char_u numbuf[NUMBUFLEN];
4081
4082 if (rettv != NULL)
4083 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
4084 if (s == NULL)
4085 s = (char_u *)"";
4086
4087 STRCPY(IObuff, ":return ");
4088 STRNCPY(IObuff + 8, s, IOSIZE - 8);
4089 if (STRLEN(s) + 8 >= IOSIZE)
4090 STRCPY(IObuff + IOSIZE - 4, "...");
4091 vim_free(tofree);
4092 return vim_strsave(IObuff);
4093}
4094
4095/*
4096 * Get next function line.
4097 * Called by do_cmdline() to get the next line.
4098 * Returns allocated string, or NULL for end of function.
4099 */
4100 char_u *
4101get_func_line(
4102 int c UNUSED,
4103 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02004104 int indent UNUSED,
4105 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004106{
4107 funccall_T *fcp = (funccall_T *)cookie;
4108 ufunc_T *fp = fcp->func;
4109 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004110 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004111
Bram Moolenaare38eab22019-12-05 21:50:01 +01004112 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004113 if (fcp->dbg_tick != debug_tick)
4114 {
4115 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004116 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004117 fcp->dbg_tick = debug_tick;
4118 }
4119#ifdef FEAT_PROFILE
4120 if (do_profiling == PROF_YES)
4121 func_line_end(cookie);
4122#endif
4123
4124 gap = &fp->uf_lines;
4125 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4126 || fcp->returned)
4127 retval = NULL;
4128 else
4129 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004130 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004131 while (fcp->linenr < gap->ga_len
4132 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
4133 ++fcp->linenr;
4134 if (fcp->linenr >= gap->ga_len)
4135 retval = NULL;
4136 else
4137 {
4138 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004139 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004140#ifdef FEAT_PROFILE
4141 if (do_profiling == PROF_YES)
4142 func_line_start(cookie);
4143#endif
4144 }
4145 }
4146
Bram Moolenaare38eab22019-12-05 21:50:01 +01004147 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004148 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004149 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004150 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004151 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004152 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004153 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004154 fcp->dbg_tick = debug_tick;
4155 }
4156
4157 return retval;
4158}
4159
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004160/*
4161 * Return TRUE if the currently active function should be ended, because a
4162 * return was encountered or an error occurred. Used inside a ":while".
4163 */
4164 int
4165func_has_ended(void *cookie)
4166{
4167 funccall_T *fcp = (funccall_T *)cookie;
4168
Bram Moolenaare38eab22019-12-05 21:50:01 +01004169 // Ignore the "abort" flag if the abortion behavior has been changed due to
4170 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004171 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4172 || fcp->returned);
4173}
4174
4175/*
4176 * return TRUE if cookie indicates a function which "abort"s on errors.
4177 */
4178 int
4179func_has_abort(
4180 void *cookie)
4181{
4182 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
4183}
4184
4185
4186/*
4187 * Turn "dict.Func" into a partial for "Func" bound to "dict".
4188 * Don't do this when "Func" is already a partial that was bound
4189 * explicitly (pt_auto is FALSE).
4190 * Changes "rettv" in-place.
4191 * Returns the updated "selfdict_in".
4192 */
4193 dict_T *
4194make_partial(dict_T *selfdict_in, typval_T *rettv)
4195{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004196 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004197 char_u *tofree = NULL;
4198 ufunc_T *fp;
4199 char_u fname_buf[FLEN_FIXED + 1];
4200 int error;
4201 dict_T *selfdict = selfdict_in;
4202
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004203 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
4204 fp = rettv->vval.v_partial->pt_func;
4205 else
4206 {
4207 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
4208 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004209 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004210 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004211 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004212 vim_free(tofree);
4213 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004214
4215 if (fp != NULL && (fp->uf_flags & FC_DICT))
4216 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004217 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004218
4219 if (pt != NULL)
4220 {
4221 pt->pt_refcount = 1;
4222 pt->pt_dict = selfdict;
4223 pt->pt_auto = TRUE;
4224 selfdict = NULL;
4225 if (rettv->v_type == VAR_FUNC)
4226 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004227 // Just a function: Take over the function name and use
4228 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004229 pt->pt_name = rettv->vval.v_string;
4230 }
4231 else
4232 {
4233 partial_T *ret_pt = rettv->vval.v_partial;
4234 int i;
4235
Bram Moolenaare38eab22019-12-05 21:50:01 +01004236 // Partial: copy the function name, use selfdict and copy
4237 // args. Can't take over name or args, the partial might
4238 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004239 if (ret_pt->pt_name != NULL)
4240 {
4241 pt->pt_name = vim_strsave(ret_pt->pt_name);
4242 func_ref(pt->pt_name);
4243 }
4244 else
4245 {
4246 pt->pt_func = ret_pt->pt_func;
4247 func_ptr_ref(pt->pt_func);
4248 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004249 if (ret_pt->pt_argc > 0)
4250 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004251 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004252 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004253 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004254 pt->pt_argc = 0;
4255 else
4256 {
4257 pt->pt_argc = ret_pt->pt_argc;
4258 for (i = 0; i < pt->pt_argc; i++)
4259 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
4260 }
4261 }
4262 partial_unref(ret_pt);
4263 }
4264 rettv->v_type = VAR_PARTIAL;
4265 rettv->vval.v_partial = pt;
4266 }
4267 }
4268 return selfdict;
4269}
4270
4271/*
4272 * Return the name of the executed function.
4273 */
4274 char_u *
4275func_name(void *cookie)
4276{
4277 return ((funccall_T *)cookie)->func->uf_name;
4278}
4279
4280/*
4281 * Return the address holding the next breakpoint line for a funccall cookie.
4282 */
4283 linenr_T *
4284func_breakpoint(void *cookie)
4285{
4286 return &((funccall_T *)cookie)->breakpoint;
4287}
4288
4289/*
4290 * Return the address holding the debug tick for a funccall cookie.
4291 */
4292 int *
4293func_dbg_tick(void *cookie)
4294{
4295 return &((funccall_T *)cookie)->dbg_tick;
4296}
4297
4298/*
4299 * Return the nesting level for a funccall cookie.
4300 */
4301 int
4302func_level(void *cookie)
4303{
4304 return ((funccall_T *)cookie)->level;
4305}
4306
4307/*
4308 * Return TRUE when a function was ended by a ":return" command.
4309 */
4310 int
4311current_func_returned(void)
4312{
4313 return current_funccal->returned;
4314}
4315
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004316 int
4317free_unref_funccal(int copyID, int testing)
4318{
4319 int did_free = FALSE;
4320 int did_free_funccal = FALSE;
4321 funccall_T *fc, **pfc;
4322
4323 for (pfc = &previous_funccal; *pfc != NULL; )
4324 {
4325 if (can_free_funccal(*pfc, copyID))
4326 {
4327 fc = *pfc;
4328 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004329 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004330 did_free = TRUE;
4331 did_free_funccal = TRUE;
4332 }
4333 else
4334 pfc = &(*pfc)->caller;
4335 }
4336 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004337 // When a funccal was freed some more items might be garbage
4338 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004339 (void)garbage_collect(testing);
4340
4341 return did_free;
4342}
4343
4344/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004345 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004346 */
4347 static funccall_T *
4348get_funccal(void)
4349{
4350 int i;
4351 funccall_T *funccal;
4352 funccall_T *temp_funccal;
4353
4354 funccal = current_funccal;
4355 if (debug_backtrace_level > 0)
4356 {
4357 for (i = 0; i < debug_backtrace_level; i++)
4358 {
4359 temp_funccal = funccal->caller;
4360 if (temp_funccal)
4361 funccal = temp_funccal;
4362 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004363 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004364 debug_backtrace_level = i;
4365 }
4366 }
4367 return funccal;
4368}
4369
4370/*
4371 * Return the hashtable used for local variables in the current funccal.
4372 * Return NULL if there is no current funccal.
4373 */
4374 hashtab_T *
4375get_funccal_local_ht()
4376{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004378 return NULL;
4379 return &get_funccal()->l_vars.dv_hashtab;
4380}
4381
4382/*
4383 * Return the l: scope variable.
4384 * Return NULL if there is no current funccal.
4385 */
4386 dictitem_T *
4387get_funccal_local_var()
4388{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004390 return NULL;
4391 return &get_funccal()->l_vars_var;
4392}
4393
4394/*
4395 * Return the hashtable used for argument in the current funccal.
4396 * Return NULL if there is no current funccal.
4397 */
4398 hashtab_T *
4399get_funccal_args_ht()
4400{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004402 return NULL;
4403 return &get_funccal()->l_avars.dv_hashtab;
4404}
4405
4406/*
4407 * Return the a: scope variable.
4408 * Return NULL if there is no current funccal.
4409 */
4410 dictitem_T *
4411get_funccal_args_var()
4412{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004414 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004415 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004416}
4417
4418/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004419 * List function variables, if there is a function.
4420 */
4421 void
4422list_func_vars(int *first)
4423{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004425 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004426 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004427}
4428
4429/*
4430 * If "ht" is the hashtable for local variables in the current funccal, return
4431 * the dict that contains it.
4432 * Otherwise return NULL.
4433 */
4434 dict_T *
4435get_current_funccal_dict(hashtab_T *ht)
4436{
4437 if (current_funccal != NULL
4438 && ht == &current_funccal->l_vars.dv_hashtab)
4439 return &current_funccal->l_vars;
4440 return NULL;
4441}
4442
4443/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004444 * Search hashitem in parent scope.
4445 */
4446 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004447find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004448{
4449 funccall_T *old_current_funccal = current_funccal;
4450 hashtab_T *ht;
4451 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004452 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004453
4454 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4455 return NULL;
4456
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004457 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004458 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004459 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004460 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004461 ht = find_var_ht(name, &varname);
4462 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004463 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004464 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004465 if (!HASHITEM_EMPTY(hi))
4466 {
4467 *pht = ht;
4468 break;
4469 }
4470 }
4471 if (current_funccal == current_funccal->func->uf_scoped)
4472 break;
4473 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004474 }
4475 current_funccal = old_current_funccal;
4476
4477 return hi;
4478}
4479
4480/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004481 * Search variable in parent scope.
4482 */
4483 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004484find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004485{
4486 dictitem_T *v = NULL;
4487 funccall_T *old_current_funccal = current_funccal;
4488 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004489 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004490
4491 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4492 return NULL;
4493
Bram Moolenaare38eab22019-12-05 21:50:01 +01004494 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004495 current_funccal = current_funccal->func->uf_scoped;
4496 while (current_funccal)
4497 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004498 ht = find_var_ht(name, &varname);
4499 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004500 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004501 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004502 if (v != NULL)
4503 break;
4504 }
4505 if (current_funccal == current_funccal->func->uf_scoped)
4506 break;
4507 current_funccal = current_funccal->func->uf_scoped;
4508 }
4509 current_funccal = old_current_funccal;
4510
4511 return v;
4512}
4513
4514/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004515 * Set "copyID + 1" in previous_funccal and callers.
4516 */
4517 int
4518set_ref_in_previous_funccal(int copyID)
4519{
4520 int abort = FALSE;
4521 funccall_T *fc;
4522
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004523 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004524 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004525 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004526 abort = abort
4527 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4528 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004529 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004530 }
4531 return abort;
4532}
4533
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004534 static int
4535set_ref_in_funccal(funccall_T *fc, int copyID)
4536{
4537 int abort = FALSE;
4538
4539 if (fc->fc_copyID != copyID)
4540 {
4541 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004542 abort = abort
4543 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4544 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004545 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004546 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004547 }
4548 return abort;
4549}
4550
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004551/*
4552 * Set "copyID" in all local vars and arguments in the call stack.
4553 */
4554 int
4555set_ref_in_call_stack(int copyID)
4556{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004557 int abort = FALSE;
4558 funccall_T *fc;
4559 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004560
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004561 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004562 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004563
4564 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004565 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
4566 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004567 abort = abort || set_ref_in_funccal(fc, copyID);
4568
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004569 return abort;
4570}
4571
4572/*
4573 * Set "copyID" in all functions available by name.
4574 */
4575 int
4576set_ref_in_functions(int copyID)
4577{
4578 int todo;
4579 hashitem_T *hi = NULL;
4580 int abort = FALSE;
4581 ufunc_T *fp;
4582
4583 todo = (int)func_hashtab.ht_used;
4584 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004585 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004586 if (!HASHITEM_EMPTY(hi))
4587 {
4588 --todo;
4589 fp = HI2UF(hi);
4590 if (!func_name_refcount(fp->uf_name))
4591 abort = abort || set_ref_in_func(NULL, fp, copyID);
4592 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004593 }
4594 return abort;
4595}
4596
4597/*
4598 * Set "copyID" in all function arguments.
4599 */
4600 int
4601set_ref_in_func_args(int copyID)
4602{
4603 int i;
4604 int abort = FALSE;
4605
4606 for (i = 0; i < funcargs.ga_len; ++i)
4607 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
4608 copyID, NULL, NULL);
4609 return abort;
4610}
4611
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004612/*
4613 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004614 * Returns TRUE if setting references failed somehow.
4615 */
4616 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004617set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004618{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004619 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004620 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01004621 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004622 char_u fname_buf[FLEN_FIXED + 1];
4623 char_u *tofree = NULL;
4624 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004625 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004626
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004627 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004628 return FALSE;
4629
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004630 if (fp_in == NULL)
4631 {
4632 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004633 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004634 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004635 if (fp != NULL)
4636 {
4637 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004638 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004639 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004640
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004641 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004642 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004643}
4644
Bram Moolenaare38eab22019-12-05 21:50:01 +01004645#endif // FEAT_EVAL