blob: 993697e46d6231967be97251f54136b34f470aab [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar93343722018-07-10 19:39:18 +020017// flags used in uf_flags
18#define FC_ABORT 0x01 // abort function on error
19#define FC_RANGE 0x02 // function accepts range
20#define FC_DICT 0x04 // Dict function, uses "self"
21#define FC_CLOSURE 0x08 // closure, uses outer scope variables
22#define FC_DELETED 0x10 // :delfunction used while uf_refcount > 0
23#define FC_REMOVED 0x20 // function redefined while uf_refcount > 0
24#define FC_SANDBOX 0x40 // function defined in the sandbox
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010025#define FC_DEAD 0x80 // function kept only for reference to dfunc
26#define FC_EXPORT 0x100 // "export def Func()"
Bram Moolenaarf10806b2020-04-02 18:34:35 +020027#define FC_NOARGS 0x200 // no a: variables in lambda
Bram Moolenaar4c17ad92020-04-27 22:47:51 +020028#define FC_VIM9 0x400 // defined in vim9 script file
Bram Moolenaara9b579f2016-07-17 18:29:19 +020029
Bram Moolenaara9b579f2016-07-17 18:29:19 +020030/*
31 * All user-defined functions are found in this hashtable.
32 */
33static hashtab_T func_hashtab;
34
Bram Moolenaare38eab22019-12-05 21:50:01 +010035// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020036static garray_T funcargs = GA_EMPTY;
37
Bram Moolenaar209b8e32019-03-14 13:43:24 +010038// pointer to funccal for currently active function
39static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020040
Bram Moolenaar209b8e32019-03-14 13:43:24 +010041// Pointer to list of previously used funccal, still around because some
42// item in it is still being used.
43static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020044
45static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
46static char *e_funcdict = N_("E717: Dictionary entry already exists");
47static char *e_funcref = N_("E718: Funcref required");
48static char *e_nofunc = N_("E130: Unknown function: %s");
49
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020050static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020051
52 void
53func_init()
54{
55 hash_init(&func_hashtab);
56}
57
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020058/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020059 * Return the function hash table
60 */
61 hashtab_T *
62func_tbl_get(void)
63{
64 return &func_hashtab;
65}
66
67/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020068 * Get one function argument.
69 * If "argtypes" is not NULL also get the type: "arg: type".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010070 * Return a pointer to after the type.
71 * When something is wrong return "arg".
72 */
73 static char_u *
74one_function_arg(char_u *arg, garray_T *newargs, garray_T *argtypes, int skip)
75{
Bram Moolenaar6e949782020-04-13 17:21:00 +020076 char_u *p = arg;
77 char_u *arg_copy = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010078
79 while (ASCII_ISALNUM(*p) || *p == '_')
80 ++p;
81 if (arg == p || isdigit(*arg)
82 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
83 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
84 {
85 if (!skip)
86 semsg(_("E125: Illegal argument: %s"), arg);
87 return arg;
88 }
89 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
90 return arg;
91 if (newargs != NULL)
92 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010093 int c;
94 int i;
95
96 c = *p;
97 *p = NUL;
98 arg_copy = vim_strsave(arg);
99 if (arg_copy == NULL)
100 {
101 *p = c;
102 return arg;
103 }
104
105 // Check for duplicate argument name.
106 for (i = 0; i < newargs->ga_len; ++i)
107 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
108 {
109 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
110 vim_free(arg_copy);
111 return arg;
112 }
113 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
114 newargs->ga_len++;
115
116 *p = c;
117 }
118
119 // get any type from "arg: type"
120 if (argtypes != NULL && ga_grow(argtypes, 1) == OK)
121 {
122 char_u *type = NULL;
123
Bram Moolenaar6e949782020-04-13 17:21:00 +0200124 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
125 {
126 semsg(_("E1059: No white space allowed before colon: %s"),
127 arg_copy == NULL ? arg : arg_copy);
128 p = skipwhite(p);
129 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100130 if (*p == ':')
131 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200132 ++p;
133 if (!VIM_ISWHITE(*p))
134 {
135 semsg(_(e_white_after), ":");
136 return arg;
137 }
138 type = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100139 p = skip_type(type);
140 type = vim_strnsave(type, p - type);
141 }
Bram Moolenaar6e949782020-04-13 17:21:00 +0200142 else if (*skipwhite(p) != '=')
143 {
144 semsg(_("E1077: Missing argument type for %s"),
145 arg_copy == NULL ? arg : arg_copy);
146 return arg;
147 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100148 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
149 }
150
151 return p;
152}
153
154/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200155 * Get function arguments.
156 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200158get_function_args(
159 char_u **argp,
160 char_u endchar,
161 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 garray_T *argtypes, // NULL unless using :def
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200163 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200164 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200165 int skip,
166 exarg_T *eap,
167 char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200168{
169 int mustend = FALSE;
170 char_u *arg = *argp;
171 char_u *p = arg;
172 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200173 int any_default = FALSE;
174 char_u *expr;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200175 char_u *whitep = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200176
177 if (newargs != NULL)
178 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 if (argtypes != NULL)
180 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200181 if (default_args != NULL)
182 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200183
184 if (varargs != NULL)
185 *varargs = FALSE;
186
187 /*
188 * Isolate the arguments: "arg1, arg2, ...)"
189 */
190 while (*p != endchar)
191 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200192 while (eap != NULL && eap->getline != NULL
193 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200194 {
195 char_u *theline;
196
197 // End of the line, get the next one.
198 theline = eap->getline(':', eap->cookie, 0, TRUE);
199 if (theline == NULL)
200 break;
201 vim_free(*line_to_free);
202 *line_to_free = theline;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200203 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200204 p = skipwhite(theline);
205 }
206
207 if (mustend && *p != endchar)
208 {
209 if (!skip)
210 semsg(_(e_invarg2), *argp);
211 break;
212 }
213 if (*p == endchar)
214 break;
215
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200216 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
217 {
218 if (varargs != NULL)
219 *varargs = TRUE;
220 p += 3;
221 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222
223 if (argtypes != NULL)
224 {
225 // ...name: list<type>
226 if (!ASCII_ISALPHA(*p))
227 {
228 emsg(_("E1055: Missing name after ..."));
229 break;
230 }
231
232 arg = p;
233 p = one_function_arg(p, newargs, argtypes, skip);
234 if (p == arg)
235 break;
236 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200237 }
238 else
239 {
240 arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100241 p = one_function_arg(p, newargs, argtypes, skip);
242 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200243 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200244
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200245 if (*skipwhite(p) == '=' && default_args != NULL)
246 {
247 typval_T rettv;
248
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100249 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200250 any_default = TRUE;
251 p = skipwhite(p) + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200252 whitep = p;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200253 p = skipwhite(p);
254 expr = p;
255 if (eval1(&p, &rettv, FALSE) != FAIL)
256 {
257 if (ga_grow(default_args, 1) == FAIL)
258 goto err_ret;
259
260 // trim trailing whitespace
261 while (p > expr && VIM_ISWHITE(p[-1]))
262 p--;
263 c = *p;
264 *p = NUL;
265 expr = vim_strsave(expr);
266 if (expr == NULL)
267 {
268 *p = c;
269 goto err_ret;
270 }
271 ((char_u **)(default_args->ga_data))
272 [default_args->ga_len] = expr;
273 default_args->ga_len++;
274 *p = c;
275 }
276 else
277 mustend = TRUE;
278 }
279 else if (any_default)
280 {
281 emsg(_("E989: Non-default argument follows default argument"));
282 mustend = TRUE;
283 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200284 if (*p == ',')
285 ++p;
286 else
287 mustend = TRUE;
288 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200289 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200290 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200291 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200292
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200293 if (*p != endchar)
294 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100295 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200296
297 *argp = p;
298 return OK;
299
300err_ret:
301 if (newargs != NULL)
302 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200303 if (default_args != NULL)
304 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200305 return FAIL;
306}
307
308/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200309 * Register function "fp" as using "current_funccal" as its scope.
310 */
311 static int
312register_closure(ufunc_T *fp)
313{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200314 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100315 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200316 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200317 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200318 fp->uf_scoped = current_funccal;
319 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200320
Bram Moolenaar58016442016-07-31 18:30:22 +0200321 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
322 return FAIL;
323 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
324 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200325 return OK;
326}
327
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100328 static void
329set_ufunc_name(ufunc_T *fp, char_u *name)
330{
331 STRCPY(fp->uf_name, name);
332
333 if (name[0] == K_SPECIAL)
334 {
335 fp->uf_name_exp = alloc(STRLEN(name) + 3);
336 if (fp->uf_name_exp != NULL)
337 {
338 STRCPY(fp->uf_name_exp, "<SNR>");
339 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
340 }
341 }
342}
343
Bram Moolenaar58016442016-07-31 18:30:22 +0200344/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200345 * Parse a lambda expression and get a Funcref from "*arg".
346 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
347 */
348 int
349get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
350{
351 garray_T newargs;
352 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200353 garray_T *pnewargs;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200354 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100355 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200356 int varargs;
357 int ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200358 char_u *start = skipwhite(*arg + 1);
359 char_u *s, *e;
360 static int lambda_no = 0;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200361 int *old_eval_lavars = eval_lavars_used;
362 int eval_lavars = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200363
364 ga_init(&newargs);
365 ga_init(&newlines);
366
Bram Moolenaare38eab22019-12-05 21:50:01 +0100367 // First, check if this is a lambda expression. "->" must exist.
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200368 ret = get_function_args(&start, '-', NULL, NULL, NULL, NULL, TRUE,
369 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200370 if (ret == FAIL || *start != '>')
371 return NOTDONE;
372
Bram Moolenaare38eab22019-12-05 21:50:01 +0100373 // Parse the arguments again.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200374 if (evaluate)
375 pnewargs = &newargs;
376 else
377 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200378 *arg = skipwhite(*arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100379 // TODO: argument types
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200380 ret = get_function_args(arg, '-', pnewargs, NULL, &varargs, NULL, FALSE,
381 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200382 if (ret == FAIL || **arg != '>')
383 goto errret;
384
Bram Moolenaare38eab22019-12-05 21:50:01 +0100385 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200386 if (evaluate)
387 eval_lavars_used = &eval_lavars;
388
Bram Moolenaare38eab22019-12-05 21:50:01 +0100389 // Get the start and the end of the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200390 *arg = skipwhite(*arg + 1);
391 s = *arg;
392 ret = skip_expr(arg);
393 if (ret == FAIL)
394 goto errret;
395 e = *arg;
396 *arg = skipwhite(*arg);
397 if (**arg != '}')
Bram Moolenaaree619e52020-03-28 21:38:06 +0100398 {
399 semsg(_("E451: Expected }: %s"), *arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200400 goto errret;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100401 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200402 ++*arg;
403
404 if (evaluate)
405 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200406 int len, flags = 0;
407 char_u *p;
408 char_u name[20];
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200409
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200410 sprintf((char*)name, "<lambda>%d", ++lambda_no);
411
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200412 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200413 if (fp == NULL)
414 goto errret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100415 fp->uf_dfunc_idx = -1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200416 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200417 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200418 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200419
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200420 ga_init2(&newlines, (int)sizeof(char_u *), 1);
421 if (ga_grow(&newlines, 1) == FAIL)
422 goto errret;
423
Bram Moolenaare38eab22019-12-05 21:50:01 +0100424 // Add "return " before the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200425 len = 7 + e - s + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200426 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200427 if (p == NULL)
428 goto errret;
429 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
430 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200431 vim_strncpy(p + 7, s, e - s);
Bram Moolenaarf10806b2020-04-02 18:34:35 +0200432 if (strstr((char *)p + 7, "a:") == NULL)
433 // No a: variables are used for sure.
434 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200435
436 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100437 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200438 hash_add(&func_hashtab, UF2HIKEY(fp));
439 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200440 ga_init(&fp->uf_def_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200441 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200442 if (current_funccal != NULL && eval_lavars)
443 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200444 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200445 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200446 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200447 }
448 else
449 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200450
451#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200452 if (prof_def_func())
453 func_do_profile(fp);
454#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200455 if (sandbox)
456 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100457 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200458 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200459 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200460 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200461 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100462 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200463
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200464 pt->pt_func = fp;
465 pt->pt_refcount = 1;
466 rettv->vval.v_partial = pt;
467 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200468 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200469
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200470 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200471 return OK;
472
473errret:
474 ga_clear_strings(&newargs);
475 ga_clear_strings(&newlines);
476 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100477 vim_free(pt);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200478 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200479 return FAIL;
480}
481
482/*
483 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
484 * name it contains, otherwise return "name".
485 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
486 * "partialp".
487 */
488 char_u *
489deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
490{
491 dictitem_T *v;
492 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200493 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200494
495 if (partialp != NULL)
496 *partialp = NULL;
497
498 cc = name[*lenp];
499 name[*lenp] = NUL;
500 v = find_var(name, NULL, no_autoload);
501 name[*lenp] = cc;
502 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
503 {
504 if (v->di_tv.vval.v_string == NULL)
505 {
506 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100507 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200508 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200509 s = v->di_tv.vval.v_string;
510 *lenp = (int)STRLEN(s);
511 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200512 }
513
514 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
515 {
516 partial_T *pt = v->di_tv.vval.v_partial;
517
518 if (pt == NULL)
519 {
520 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100521 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200522 }
523 if (partialp != NULL)
524 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200525 s = partial_name(pt);
526 *lenp = (int)STRLEN(s);
527 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200528 }
529
530 return name;
531}
532
533/*
534 * Give an error message with a function name. Handle <SNR> things.
535 * "ermsg" is to be passed without translation, use N_() instead of _().
536 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100537 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200538emsg_funcname(char *ermsg, char_u *name)
539{
540 char_u *p;
541
542 if (*name == K_SPECIAL)
543 p = concat_str((char_u *)"<SNR>", name + 3);
544 else
545 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100546 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200547 if (p != name)
548 vim_free(p);
549}
550
551/*
552 * Allocate a variable for the result of a function.
553 * Return OK or FAIL.
554 */
555 int
556get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200557 char_u *name, // name of the function
558 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200559 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200560 char_u **arg, // argument, pointing to the '('
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200561 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200562{
563 char_u *argp;
564 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100565 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
566 int argcount = 0; // number of arguments found
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200567
568 /*
569 * Get the arguments.
570 */
571 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200572 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
573 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200574 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100575 argp = skipwhite(argp + 1); // skip the '(' or ','
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200576 if (*argp == ')' || *argp == ',' || *argp == NUL)
577 break;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200578 if (eval1(&argp, &argvars[argcount], funcexe->evaluate) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200579 {
580 ret = FAIL;
581 break;
582 }
583 ++argcount;
584 if (*argp != ',')
585 break;
586 }
587 if (*argp == ')')
588 ++argp;
589 else
590 ret = FAIL;
591
592 if (ret == OK)
593 {
594 int i = 0;
595
596 if (get_vim_var_nr(VV_TESTING))
597 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100598 // Prepare for calling test_garbagecollect_now(), need to know
599 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200600 if (funcargs.ga_itemsize == 0)
601 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
602 for (i = 0; i < argcount; ++i)
603 if (ga_grow(&funcargs, 1) == OK)
604 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
605 &argvars[i];
606 }
607
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200608 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200609
610 funcargs.ga_len -= i;
611 }
612 else if (!aborting())
613 {
614 if (argcount == MAX_FUNC_ARGS)
615 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
616 else
617 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
618 }
619
620 while (--argcount >= 0)
621 clear_tv(&argvars[argcount]);
622
623 *arg = skipwhite(argp);
624 return ret;
625}
626
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200627/*
628 * Return TRUE if "p" starts with "<SID>" or "s:".
629 * Only works if eval_fname_script() returned non-zero for "p"!
630 */
631 static int
632eval_fname_sid(char_u *p)
633{
634 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
635}
636
637/*
638 * In a script change <SID>name() and s:name() to K_SNR 123_name().
639 * Change <SNR>123_name() to K_SNR 123_name().
640 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
641 * (slow).
642 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100643 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200644fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
645{
646 int llen;
647 char_u *fname;
648 int i;
649
650 llen = eval_fname_script(name);
651 if (llen > 0)
652 {
653 fname_buf[0] = K_SPECIAL;
654 fname_buf[1] = KS_EXTRA;
655 fname_buf[2] = (int)KE_SNR;
656 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100657 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200658 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200659 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100660 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200661 else
662 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200663 sprintf((char *)fname_buf + 3, "%ld_",
664 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200665 i = (int)STRLEN(fname_buf);
666 }
667 }
668 if (i + STRLEN(name + llen) < FLEN_FIXED)
669 {
670 STRCPY(fname_buf + i, name + llen);
671 fname = fname_buf;
672 }
673 else
674 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200675 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200676 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100677 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200678 else
679 {
680 *tofree = fname;
681 mch_memmove(fname, fname_buf, (size_t)i);
682 STRCPY(fname + i, name + llen);
683 }
684 }
685 }
686 else
687 fname = name;
688 return fname;
689}
690
691/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100692 * Find a function "name" in script "sid".
693 */
694 static ufunc_T *
695find_func_with_sid(char_u *name, int sid)
696{
697 hashitem_T *hi;
698 char_u buffer[200];
699
700 buffer[0] = K_SPECIAL;
701 buffer[1] = KS_EXTRA;
702 buffer[2] = (int)KE_SNR;
703 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
704 (long)sid, name);
705 hi = hash_find(&func_hashtab, buffer);
706 if (!HASHITEM_EMPTY(hi))
707 return HI2UF(hi);
708
709 return NULL;
710}
711
712/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200713 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200714 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200715 * Return NULL for unknown function.
716 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717 static ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200718find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200719{
720 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100721 ufunc_T *func;
722 imported_T *imported;
723
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200724 if (in_vim9script() && !is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725 {
726 // Find script-local function before global one.
727 func = find_func_with_sid(name, current_sctx.sc_sid);
728 if (func != NULL)
729 return func;
730
731 // Find imported funcion before global one.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +0100732 imported = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733 if (imported != NULL && imported->imp_funcname != NULL)
734 {
735 hi = hash_find(&func_hashtab, imported->imp_funcname);
736 if (!HASHITEM_EMPTY(hi))
737 return HI2UF(hi);
738 }
739 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200740
Bram Moolenaara26b9702020-04-18 19:53:28 +0200741 hi = hash_find(&func_hashtab,
742 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200743 if (!HASHITEM_EMPTY(hi))
744 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100745
746 return NULL;
747}
748
749/*
750 * Find a function by name, return pointer to it in ufuncs.
751 * "cctx" is passed in a :def function to find imported functions.
752 * Return NULL for unknown or dead function.
753 */
754 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200755find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200757 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758
759 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
760 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200761 return NULL;
762}
763
764/*
765 * Copy the function name of "fp" to buffer "buf".
766 * "buf" must be able to hold the function name plus three bytes.
767 * Takes care of script-local function names.
768 */
769 static void
770cat_func_name(char_u *buf, ufunc_T *fp)
771{
772 if (fp->uf_name[0] == K_SPECIAL)
773 {
774 STRCPY(buf, "<SNR>");
775 STRCAT(buf, fp->uf_name + 3);
776 }
777 else
778 STRCPY(buf, fp->uf_name);
779}
780
781/*
782 * Add a number variable "name" to dict "dp" with value "nr".
783 */
784 static void
785add_nr_var(
786 dict_T *dp,
787 dictitem_T *v,
788 char *name,
789 varnumber_T nr)
790{
791 STRCPY(v->di_key, name);
792 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
793 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
794 v->di_tv.v_type = VAR_NUMBER;
795 v->di_tv.v_lock = VAR_FIXED;
796 v->di_tv.vval.v_number = nr;
797}
798
799/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100800 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200801 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100802 static void
803free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200804{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100805 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200806
807 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
808 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100809 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200810
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100811 // When garbage collecting a funccall_T may be freed before the
812 // function that references it, clear its uf_scoped field.
813 // The function may have been redefined and point to another
814 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200815 if (fp != NULL && fp->uf_scoped == fc)
816 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200817 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200818 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200819
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200820 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200821 vim_free(fc);
822}
823
824/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100825 * Free "fc" and what it contains.
826 * Can be called only when "fc" is kept beyond the period of it called,
827 * i.e. after cleanup_function_call(fc).
828 */
829 static void
830free_funccal_contents(funccall_T *fc)
831{
832 listitem_T *li;
833
834 // Free all l: variables.
835 vars_clear(&fc->l_vars.dv_hashtab);
836
837 // Free all a: variables.
838 vars_clear(&fc->l_avars.dv_hashtab);
839
840 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200841 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100842 clear_tv(&li->li_tv);
843
844 free_funccal(fc);
845}
846
847/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200848 * Handle the last part of returning from a function: free the local hashtable.
849 * Unless it is still in use by a closure.
850 */
851 static void
852cleanup_function_call(funccall_T *fc)
853{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100854 int may_free_fc = fc->fc_refcount <= 0;
855 int free_fc = TRUE;
856
Bram Moolenaar6914c642017-04-01 21:21:30 +0200857 current_funccal = fc->caller;
858
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100859 // Free all l: variables if not referred.
860 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
861 vars_clear(&fc->l_vars.dv_hashtab);
862 else
863 free_fc = FALSE;
864
865 // If the a:000 list and the l: and a: dicts are not referenced and
866 // there is no closure using it, we can free the funccall_T and what's
867 // in it.
868 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
869 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200870 else
871 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100872 int todo;
873 hashitem_T *hi;
874 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200875
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100876 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200877
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100878 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200879 todo = (int)fc->l_avars.dv_hashtab.ht_used;
880 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
881 {
882 if (!HASHITEM_EMPTY(hi))
883 {
884 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100885 di = HI2DI(hi);
886 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200887 }
888 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100889 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200890
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100891 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
892 fc->l_varlist.lv_first = NULL;
893 else
894 {
895 listitem_T *li;
896
897 free_fc = FALSE;
898
899 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200900 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +0200901 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100902 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100903
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100904 if (free_fc)
905 free_funccal(fc);
906 else
907 {
908 static int made_copy = 0;
909
910 // "fc" is still in use. This can happen when returning "a:000",
911 // assigning "l:" to a global variable or defining a closure.
912 // Link "fc" in the list for garbage collection later.
913 fc->caller = previous_funccal;
914 previous_funccal = fc;
915
916 if (want_garbage_collect)
917 // If garbage collector is ready, clear count.
918 made_copy = 0;
919 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100920 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100921 // We have made a lot of copies, worth 4 Mbyte. This can happen
922 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100923 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100924 // too much memory.
925 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100926 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100927 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200928 }
929}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930/*
931 * Unreference "fc": decrement the reference count and free it when it
932 * becomes zero. "fp" is detached from "fc".
933 * When "force" is TRUE we are exiting.
934 */
935 static void
936funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
937{
938 funccall_T **pfc;
939 int i;
940
941 if (fc == NULL)
942 return;
943
944 if (--fc->fc_refcount <= 0 && (force || (
945 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
946 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
947 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
948 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
949 {
950 if (fc == *pfc)
951 {
952 *pfc = fc->caller;
953 free_funccal_contents(fc);
954 return;
955 }
956 }
957 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
958 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
959 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
960}
961
962/*
963 * Remove the function from the function hashtable. If the function was
964 * deleted while it still has references this was already done.
965 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
966 */
967 static int
968func_remove(ufunc_T *fp)
969{
970 hashitem_T *hi;
971
972 // Return if it was already virtually deleted.
973 if (fp->uf_flags & FC_DEAD)
974 return FALSE;
975
976 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
977 if (!HASHITEM_EMPTY(hi))
978 {
979 // When there is a def-function index do not actually remove the
980 // function, so we can find the index when defining the function again.
981 if (fp->uf_dfunc_idx >= 0)
982 fp->uf_flags |= FC_DEAD;
983 else
984 hash_remove(&func_hashtab, hi);
985 return TRUE;
986 }
987 return FALSE;
988}
989
990 static void
991func_clear_items(ufunc_T *fp)
992{
993 ga_clear_strings(&(fp->uf_args));
994 ga_clear_strings(&(fp->uf_def_args));
995 ga_clear_strings(&(fp->uf_lines));
996 VIM_CLEAR(fp->uf_name_exp);
997 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100998 VIM_CLEAR(fp->uf_def_arg_idx);
999 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001000 while (fp->uf_type_list.ga_len > 0)
1001 vim_free(((type_T **)fp->uf_type_list.ga_data)
1002 [--fp->uf_type_list.ga_len]);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003 ga_clear(&fp->uf_type_list);
1004#ifdef FEAT_PROFILE
1005 VIM_CLEAR(fp->uf_tml_count);
1006 VIM_CLEAR(fp->uf_tml_total);
1007 VIM_CLEAR(fp->uf_tml_self);
1008#endif
1009}
1010
1011/*
1012 * Free all things that a function contains. Does not free the function
1013 * itself, use func_free() for that.
1014 * When "force" is TRUE we are exiting.
1015 */
1016 static void
1017func_clear(ufunc_T *fp, int force)
1018{
1019 if (fp->uf_cleared)
1020 return;
1021 fp->uf_cleared = TRUE;
1022
1023 // clear this function
1024 func_clear_items(fp);
1025 funccal_unref(fp->uf_scoped, fp, force);
1026 delete_def_function(fp);
1027}
1028
1029/*
1030 * Free a function and remove it from the list of functions. Does not free
1031 * what a function contains, call func_clear() first.
1032 */
1033 static void
1034func_free(ufunc_T *fp)
1035{
1036 // Only remove it when not done already, otherwise we would remove a newer
1037 // version of the function with the same name.
1038 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1039 func_remove(fp);
1040
1041 if ((fp->uf_flags & FC_DEAD) == 0)
1042 vim_free(fp);
1043}
1044
1045/*
1046 * Free all things that a function contains and free the function itself.
1047 * When "force" is TRUE we are exiting.
1048 */
1049 static void
1050func_clear_free(ufunc_T *fp, int force)
1051{
1052 func_clear(fp, force);
1053 func_free(fp);
1054}
1055
Bram Moolenaar6914c642017-04-01 21:21:30 +02001056
1057/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001058 * Call a user function.
1059 */
1060 static void
1061call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001062 ufunc_T *fp, // pointer to function
1063 int argcount, // nr of args
1064 typval_T *argvars, // arguments
1065 typval_T *rettv, // return value
1066 linenr_T firstline, // first line of range
1067 linenr_T lastline, // last line of range
1068 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001069{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001070 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001071 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001072 funccall_T *fc;
1073 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001074 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001075 static int depth = 0;
1076 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001077 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001078 int i;
1079 int ai;
1080 int islambda = FALSE;
1081 char_u numbuf[NUMBUFLEN];
1082 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001083#ifdef FEAT_PROFILE
1084 proftime_T wait_start;
1085 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001086 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001087#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001088 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001089
Bram Moolenaare38eab22019-12-05 21:50:01 +01001090 // If depth of calling is getting too high, don't execute the function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001091 if (depth >= p_mfd)
1092 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001093 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001094 rettv->v_type = VAR_NUMBER;
1095 rettv->vval.v_number = -1;
1096 return;
1097 }
1098 ++depth;
1099
Bram Moolenaare38eab22019-12-05 21:50:01 +01001100 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001101
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001102 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001103 if (fc == NULL)
1104 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001105 fc->caller = current_funccal;
1106 current_funccal = fc;
1107 fc->func = fp;
1108 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001109 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001110 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001111 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1112 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001113 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001114 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001115 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001116
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001117 if (fp->uf_dfunc_idx >= 0)
1118 {
1119 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001120 save_current_sctx = current_sctx;
1121 current_sctx = fp->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122
1123 // Execute the compiled function.
1124 call_def_function(fp, argcount, argvars, rettv);
1125 --depth;
1126 current_funccal = fc->caller;
1127
1128 estack_pop();
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001129 current_sctx = save_current_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 free_funccal(fc);
1131 return;
1132 }
1133
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001134 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1135 islambda = TRUE;
1136
1137 /*
1138 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1139 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1140 * each argument variable and saves a lot of time.
1141 */
1142 /*
1143 * Init l: variables.
1144 */
1145 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1146 if (selfdict != NULL)
1147 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001148 // Set l:self to "selfdict". Use "name" to avoid a warning from
1149 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001150 v = &fc->fixvar[fixvar_idx++].var;
1151 name = v->di_key;
1152 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001153 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001154 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1155 v->di_tv.v_type = VAR_DICT;
1156 v->di_tv.v_lock = 0;
1157 v->di_tv.vval.v_dict = selfdict;
1158 ++selfdict->dv_refcount;
1159 }
1160
1161 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001162 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001163 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001164 * Set a:000 to a list with room for the "..." arguments.
1165 */
1166 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001167 if ((fp->uf_flags & FC_NOARGS) == 0)
1168 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001169 (varnumber_T)(argcount >= fp->uf_args.ga_len
1170 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001171 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001172 if ((fp->uf_flags & FC_NOARGS) == 0)
1173 {
1174 // Use "name" to avoid a warning from some compiler that checks the
1175 // destination size.
1176 v = &fc->fixvar[fixvar_idx++].var;
1177 name = v->di_key;
1178 STRCPY(name, "000");
1179 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1180 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1181 v->di_tv.v_type = VAR_LIST;
1182 v->di_tv.v_lock = VAR_FIXED;
1183 v->di_tv.vval.v_list = &fc->l_varlist;
1184 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001185 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001186 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1187 fc->l_varlist.lv_lock = VAR_FIXED;
1188
1189 /*
1190 * Set a:firstline to "firstline" and a:lastline to "lastline".
1191 * Set a:name to named arguments.
1192 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001193 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001194 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001195 if ((fp->uf_flags & FC_NOARGS) == 0)
1196 {
1197 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001198 (varnumber_T)firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001199 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001200 (varnumber_T)lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001201 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001202 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001203 {
1204 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001205 typval_T def_rettv;
1206 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001207
1208 ai = i - fp->uf_args.ga_len;
1209 if (ai < 0)
1210 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001211 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001212 name = FUNCARG(fp, i);
1213 if (islambda)
1214 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001215
1216 // evaluate named argument default expression
1217 isdefault = ai + fp->uf_def_args.ga_len >= 0
1218 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1219 && argvars[i].vval.v_number == VVAL_NONE));
1220 if (isdefault)
1221 {
1222 char_u *default_expr = NULL;
1223 def_rettv.v_type = VAR_NUMBER;
1224 def_rettv.vval.v_number = -1;
1225
1226 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1227 [ai + fp->uf_def_args.ga_len];
1228 if (eval1(&default_expr, &def_rettv, TRUE) == FAIL)
1229 {
1230 default_arg_err = 1;
1231 break;
1232 }
1233 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001234 }
1235 else
1236 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001237 if ((fp->uf_flags & FC_NOARGS) != 0)
1238 // Bail out if no a: arguments used (in lambda).
1239 break;
1240
Bram Moolenaare38eab22019-12-05 21:50:01 +01001241 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001242 sprintf((char *)numbuf, "%d", ai + 1);
1243 name = numbuf;
1244 }
1245 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1246 {
1247 v = &fc->fixvar[fixvar_idx++].var;
1248 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001249 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001250 }
1251 else
1252 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001253 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001254 if (v == NULL)
1255 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001256 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001257 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001258
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001259 // Note: the values are copied directly to avoid alloc/free.
1260 // "argvars" must have VAR_FIXED for v_lock.
1261 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001262 v->di_tv.v_lock = VAR_FIXED;
1263
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001264 if (addlocal)
1265 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001266 // Named arguments should be accessed without the "a:" prefix in
1267 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001268 copy_tv(&v->di_tv, &v->di_tv);
1269 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001270 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001271 else
1272 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001273
1274 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1275 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001276 listitem_T *li = &fc->l_listitems[ai];
1277
1278 li->li_tv = argvars[i];
1279 li->li_tv.v_lock = VAR_FIXED;
1280 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001281 }
1282 }
1283
Bram Moolenaare38eab22019-12-05 21:50:01 +01001284 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001285 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001286
1287 if (fp->uf_flags & FC_SANDBOX)
1288 {
1289 using_sandbox = TRUE;
1290 ++sandbox;
1291 }
1292
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001293 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001294 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001295 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001296 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001297 ++no_wait_return;
1298 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001299
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001300 smsg(_("calling %s"), SOURCING_NAME);
1301 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001302 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001303 char_u buf[MSG_BUF_LEN];
1304 char_u numbuf2[NUMBUFLEN];
1305 char_u *tofree;
1306 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001307
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001308 msg_puts("(");
1309 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001310 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001311 if (i > 0)
1312 msg_puts(", ");
1313 if (argvars[i].v_type == VAR_NUMBER)
1314 msg_outnum((long)argvars[i].vval.v_number);
1315 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001316 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001317 // Do not want errors such as E724 here.
1318 ++emsg_off;
1319 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1320 --emsg_off;
1321 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001322 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001323 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001324 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001325 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1326 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001327 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001328 msg_puts((char *)s);
1329 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001330 }
1331 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001332 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001333 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001334 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001335 msg_puts("\n"); // don't overwrite this either
1336
1337 verbose_leave_scroll();
1338 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001339 }
1340#ifdef FEAT_PROFILE
1341 if (do_profiling == PROF_YES)
1342 {
1343 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001344 {
1345 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001346 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001347 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001348 if (fp->uf_profiling
1349 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1350 {
1351 ++fp->uf_tm_count;
1352 profile_start(&call_start);
1353 profile_zero(&fp->uf_tm_children);
1354 }
1355 script_prof_save(&wait_start);
1356 }
1357#endif
1358
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001359 save_current_sctx = current_sctx;
1360 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001361 save_did_emsg = did_emsg;
1362 did_emsg = FALSE;
1363
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001364 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1365 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001366 else if (islambda)
1367 {
1368 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1369
1370 // A Lambda always has the command "return {expr}". It is much faster
1371 // to evaluate {expr} directly.
1372 ++ex_nesting_level;
Bram Moolenaard1e9dc22020-04-03 18:13:57 +02001373 (void)eval1(&p, rettv, TRUE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001374 --ex_nesting_level;
1375 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001376 else
1377 // call do_cmdline() to execute the lines
1378 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001379 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1380
1381 --RedrawingDisabled;
1382
Bram Moolenaare38eab22019-12-05 21:50:01 +01001383 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001384 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1385 {
1386 clear_tv(rettv);
1387 rettv->v_type = VAR_NUMBER;
1388 rettv->vval.v_number = -1;
1389 }
1390
1391#ifdef FEAT_PROFILE
1392 if (do_profiling == PROF_YES && (fp->uf_profiling
1393 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1394 {
1395 profile_end(&call_start);
1396 profile_sub_wait(&wait_start, &call_start);
1397 profile_add(&fp->uf_tm_total, &call_start);
1398 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1399 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1400 {
1401 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1402 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1403 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001404 if (started_profiling)
1405 // make a ":profdel func" stop profiling the function
1406 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001407 }
1408#endif
1409
Bram Moolenaare38eab22019-12-05 21:50:01 +01001410 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001411 if (p_verbose >= 12)
1412 {
1413 ++no_wait_return;
1414 verbose_enter_scroll();
1415
1416 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001417 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001418 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001419 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001420 (long)fc->rettv->vval.v_number);
1421 else
1422 {
1423 char_u buf[MSG_BUF_LEN];
1424 char_u numbuf2[NUMBUFLEN];
1425 char_u *tofree;
1426 char_u *s;
1427
Bram Moolenaare38eab22019-12-05 21:50:01 +01001428 // The value may be very long. Skip the middle part, so that we
1429 // have some idea how it starts and ends. smsg() would always
1430 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001431 ++emsg_off;
1432 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1433 --emsg_off;
1434 if (s != NULL)
1435 {
1436 if (vim_strsize(s) > MSG_BUF_CLEN)
1437 {
1438 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1439 s = buf;
1440 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001441 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001442 vim_free(tofree);
1443 }
1444 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001445 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001446
1447 verbose_leave_scroll();
1448 --no_wait_return;
1449 }
1450
Bram Moolenaare31ee862020-01-07 20:59:34 +01001451 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001452 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001453 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001454#ifdef FEAT_PROFILE
1455 if (do_profiling == PROF_YES)
1456 script_prof_restore(&wait_start);
1457#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001458 if (using_sandbox)
1459 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001460
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001461 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001462 {
1463 ++no_wait_return;
1464 verbose_enter_scroll();
1465
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001466 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001467 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001468
1469 verbose_leave_scroll();
1470 --no_wait_return;
1471 }
1472
1473 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001474 --depth;
1475
Bram Moolenaar6914c642017-04-01 21:21:30 +02001476 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001477}
1478
1479/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001480 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001481 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 int
1483call_user_func_check(
1484 ufunc_T *fp,
1485 int argcount,
1486 typval_T *argvars,
1487 typval_T *rettv,
1488 funcexe_T *funcexe,
1489 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001490{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 int error;
1492 int regular_args = fp->uf_args.ga_len;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1495 *funcexe->doesrange = TRUE;
1496 if (argcount < regular_args - fp->uf_def_args.ga_len)
1497 error = FCERR_TOOFEW;
1498 else if (!has_varargs(fp) && argcount > regular_args)
1499 error = FCERR_TOOMANY;
1500 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1501 error = FCERR_DICT;
1502 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001503 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 int did_save_redo = FALSE;
1505 save_redo_T save_redo;
1506
1507 /*
1508 * Call the user function.
1509 * Save and restore search patterns, script variables and
1510 * redo buffer.
1511 */
1512 save_search_patterns();
1513 if (!ins_compl_active())
1514 {
1515 saveRedobuff(&save_redo);
1516 did_save_redo = TRUE;
1517 }
1518 ++fp->uf_calls;
1519 call_user_func(fp, argcount, argvars, rettv,
1520 funcexe->firstline, funcexe->lastline,
1521 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
1522 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1523 // Function was unreferenced while being used, free it now.
1524 func_clear_free(fp, FALSE);
1525 if (did_save_redo)
1526 restoreRedobuff(&save_redo);
1527 restore_search_patterns();
1528 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001529 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001531}
1532
1533/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001534 * There are two kinds of function names:
1535 * 1. ordinary names, function defined with :function
1536 * 2. numbered functions and lambdas
1537 * For the first we only count the name stored in func_hashtab as a reference,
1538 * using function() does not count as a reference, because the function is
1539 * looked up by name.
1540 */
1541 static int
1542func_name_refcount(char_u *name)
1543{
1544 return isdigit(*name) || *name == '<';
1545}
1546
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001547static funccal_entry_T *funccal_stack = NULL;
1548
1549/*
1550 * Save the current function call pointer, and set it to NULL.
1551 * Used when executing autocommands and for ":source".
1552 */
1553 void
1554save_funccal(funccal_entry_T *entry)
1555{
1556 entry->top_funccal = current_funccal;
1557 entry->next = funccal_stack;
1558 funccal_stack = entry;
1559 current_funccal = NULL;
1560}
1561
1562 void
1563restore_funccal(void)
1564{
1565 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001566 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001567 else
1568 {
1569 current_funccal = funccal_stack->top_funccal;
1570 funccal_stack = funccal_stack->next;
1571 }
1572}
1573
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001574 funccall_T *
1575get_current_funccal(void)
1576{
1577 return current_funccal;
1578}
1579
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001580/*
1581 * Mark all functions of script "sid" as deleted.
1582 */
1583 void
1584delete_script_functions(int sid)
1585{
1586 hashitem_T *hi;
1587 ufunc_T *fp;
1588 long_u todo;
1589 char buf[30];
1590 size_t len;
1591
1592 buf[0] = K_SPECIAL;
1593 buf[1] = KS_EXTRA;
1594 buf[2] = (int)KE_SNR;
1595 sprintf(buf + 3, "%d_", sid);
1596 len = STRLEN(buf);
1597
1598 todo = func_hashtab.ht_used;
1599 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1600 if (!HASHITEM_EMPTY(hi))
1601 {
1602 if (STRNCMP(fp->uf_name, buf, len) == 0)
1603 {
1604 fp = HI2UF(hi);
1605 fp->uf_flags |= FC_DEAD;
1606 func_clear(fp, TRUE);
1607 }
1608 --todo;
1609 }
1610}
1611
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001612#if defined(EXITFREE) || defined(PROTO)
1613 void
1614free_all_functions(void)
1615{
1616 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001617 ufunc_T *fp;
1618 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001619 long_u todo = 1;
1620 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001621
Bram Moolenaare38eab22019-12-05 21:50:01 +01001622 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001623 while (current_funccal != NULL)
1624 {
1625 clear_tv(current_funccal->rettv);
1626 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001627 if (current_funccal == NULL && funccal_stack != NULL)
1628 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001629 }
1630
Bram Moolenaare38eab22019-12-05 21:50:01 +01001631 // First clear what the functions contain. Since this may lower the
1632 // reference count of a function, it may also free a function and change
1633 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001634 while (todo > 0)
1635 {
1636 todo = func_hashtab.ht_used;
1637 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1638 if (!HASHITEM_EMPTY(hi))
1639 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 // clear the def function index now
1641 fp = HI2UF(hi);
1642 fp->uf_flags &= ~FC_DEAD;
1643 fp->uf_dfunc_idx = -1;
1644
Bram Moolenaare38eab22019-12-05 21:50:01 +01001645 // Only free functions that are not refcounted, those are
1646 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001647 if (func_name_refcount(fp->uf_name))
1648 ++skipped;
1649 else
1650 {
1651 used = func_hashtab.ht_used;
1652 func_clear(fp, TRUE);
1653 if (used != func_hashtab.ht_used)
1654 {
1655 skipped = 0;
1656 break;
1657 }
1658 }
1659 --todo;
1660 }
1661 }
1662
Bram Moolenaare38eab22019-12-05 21:50:01 +01001663 // Now actually free the functions. Need to start all over every time,
1664 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001665 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001666 while (func_hashtab.ht_used > skipped)
1667 {
1668 todo = func_hashtab.ht_used;
1669 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001670 if (!HASHITEM_EMPTY(hi))
1671 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001672 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001673 // Only free functions that are not refcounted, those are
1674 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02001675 fp = HI2UF(hi);
1676 if (func_name_refcount(fp->uf_name))
1677 ++skipped;
1678 else
1679 {
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001680 func_free(fp);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001681 skipped = 0;
1682 break;
1683 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001684 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001685 }
1686 if (skipped == 0)
1687 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688
1689 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001690}
1691#endif
1692
1693/*
1694 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02001695 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001696 * "len" is the length of "name", or -1 for NUL terminated.
1697 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001699builtin_function(char_u *name, int len)
1700{
1701 char_u *p;
1702
Bram Moolenaara26b9702020-04-18 19:53:28 +02001703 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001704 return FALSE;
1705 p = vim_strchr(name, AUTOLOAD_CHAR);
1706 return p == NULL || (len > 0 && p > name + len);
1707}
1708
1709 int
1710func_call(
1711 char_u *name,
1712 typval_T *args,
1713 partial_T *partial,
1714 dict_T *selfdict,
1715 typval_T *rettv)
1716{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001717 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001718 listitem_T *item;
1719 typval_T argv[MAX_FUNC_ARGS + 1];
1720 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001721 int r = 0;
1722
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001723 range_list_materialize(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001724 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001725 {
1726 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1727 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001728 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001729 break;
1730 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001731 // Make a copy of each argument. This is needed to be able to set
1732 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001733 copy_tv(&item->li_tv, &argv[argc++]);
1734 }
1735
1736 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001737 {
1738 funcexe_T funcexe;
1739
Bram Moolenaara80faa82020-04-12 19:37:17 +02001740 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001741 funcexe.firstline = curwin->w_cursor.lnum;
1742 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001743 funcexe.evaluate = TRUE;
1744 funcexe.partial = partial;
1745 funcexe.selfdict = selfdict;
1746 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1747 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001748
Bram Moolenaare38eab22019-12-05 21:50:01 +01001749 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001750 while (argc > 0)
1751 clear_tv(&argv[--argc]);
1752
1753 return r;
1754}
1755
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001756static int callback_depth = 0;
1757
1758 int
1759get_callback_depth(void)
1760{
1761 return callback_depth;
1762}
1763
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001764/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001765 * Invoke call_func() with a callback.
1766 */
1767 int
1768call_callback(
1769 callback_T *callback,
1770 int len, // length of "name" or -1 to use strlen()
1771 typval_T *rettv, // return value goes here
1772 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001773 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001774 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001775{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001776 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001777 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001778
Bram Moolenaara80faa82020-04-12 19:37:17 +02001779 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001780 funcexe.evaluate = TRUE;
1781 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001782 ++callback_depth;
1783 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1784 --callback_depth;
1785 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001786}
1787
1788/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 * Give an error message for the result of a function.
1790 * Nothing if "error" is FCERR_NONE.
1791 */
1792 void
1793user_func_error(int error, char_u *name)
1794{
1795 switch (error)
1796 {
1797 case FCERR_UNKNOWN:
1798 emsg_funcname(e_unknownfunc, name);
1799 break;
1800 case FCERR_NOTMETHOD:
1801 emsg_funcname(
1802 N_("E276: Cannot use function as a method: %s"), name);
1803 break;
1804 case FCERR_DELETED:
1805 emsg_funcname(N_(e_func_deleted), name);
1806 break;
1807 case FCERR_TOOMANY:
1808 emsg_funcname((char *)e_toomanyarg, name);
1809 break;
1810 case FCERR_TOOFEW:
1811 emsg_funcname((char *)e_toofewarg, name);
1812 break;
1813 case FCERR_SCRIPT:
1814 emsg_funcname(
1815 N_("E120: Using <SID> not in a script context: %s"), name);
1816 break;
1817 case FCERR_DICT:
1818 emsg_funcname(
1819 N_("E725: Calling dict function without Dictionary: %s"),
1820 name);
1821 break;
1822 }
1823}
1824
1825/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001826 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001827 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001828 * Return FAIL when the function can't be called, OK otherwise.
1829 * Also returns OK when an error was encountered while executing the function.
1830 */
1831 int
1832call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001833 char_u *funcname, // name of the function
1834 int len, // length of "name" or -1 to use strlen()
1835 typval_T *rettv, // return value goes here
1836 int argcount_in, // number of "argvars"
1837 typval_T *argvars_in, // vars for arguments, must have "argcount"
1838 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001839 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001840{
1841 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01001842 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001843 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001844 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001845 char_u fname_buf[FLEN_FIXED + 1];
1846 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001847 char_u *fname = NULL;
1848 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001849 int argcount = argcount_in;
1850 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001851 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001852 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1853 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001854 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001855 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001856 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001857
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001858 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1859 // even when call_func() returns FAIL.
1860 rettv->v_type = VAR_UNKNOWN;
1861
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001862 if (partial != NULL)
1863 fp = partial->pt_func;
1864 if (fp == NULL)
1865 {
1866 // Make a copy of the name, if it comes from a funcref variable it
1867 // could be changed or deleted in the called function.
1868 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
1869 if (name == NULL)
1870 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001871
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001872 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1873 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001874
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001875 if (funcexe->doesrange != NULL)
1876 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001877
1878 if (partial != NULL)
1879 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001880 // When the function has a partial with a dict and there is a dict
1881 // argument, use the dict argument. That is backwards compatible.
1882 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001883 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001884 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01001885 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001886 {
1887 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001888 {
1889 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
1890 {
Bram Moolenaaref140542019-12-31 21:27:13 +01001891 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001892 goto theend;
1893 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001894 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001895 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001896 for (i = 0; i < argcount_in; ++i)
1897 argv[i + argv_clear] = argvars_in[i];
1898 argvars = argv;
1899 argcount = partial->pt_argc + argcount_in;
1900 }
1901 }
1902
Bram Moolenaaref140542019-12-31 21:27:13 +01001903 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001904 {
1905 char_u *rfname = fname;
1906
Bram Moolenaare38eab22019-12-05 21:50:01 +01001907 // Ignore "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001908 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001909 rfname = fname + 2;
1910
Bram Moolenaare38eab22019-12-05 21:50:01 +01001911 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001912 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01001913 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001914
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001915 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001916 {
1917 /*
1918 * User defined function.
1919 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001920 if (fp == NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001921 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001922
Bram Moolenaare38eab22019-12-05 21:50:01 +01001923 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001924 if (fp == NULL
1925 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001926 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001927 && !aborting())
1928 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001929 // executed an autocommand, search for the function again
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001930 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001931 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001932 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001933 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1934 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001935 // loaded a package, search for the function again
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001936 fp = find_func(rfname, FALSE, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001937 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02001938 if (fp == NULL)
1939 {
1940 char_u *p = untrans_function_name(rfname);
1941
1942 // If using Vim9 script try not local to the script.
1943 // TODO: should not do this if the name started with "s:".
1944 if (p != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001945 fp = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02001946 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001947
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001948 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01001949 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001950 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001951 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001952 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01001953 // postponed filling in the arguments, do it now
1954 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001955 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001956
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001957 if (funcexe->basetv != NULL)
1958 {
1959 // Method call: base->Method()
1960 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
1961 argv[0] = *funcexe->basetv;
1962 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001963 argvars = argv;
1964 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001965 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967 error = call_user_func_check(fp, argcount, argvars, rettv,
1968 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001969 }
1970 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001971 else if (funcexe->basetv != NULL)
1972 {
1973 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001974 * expr->method(): Find the method name in the table, call its
1975 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02001976 */
1977 error = call_internal_method(fname, argcount, argvars, rettv,
1978 funcexe->basetv);
1979 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001980 else
1981 {
1982 /*
1983 * Find the function name in the table, call its implementation.
1984 */
1985 error = call_internal_func(fname, argcount, argvars, rettv);
1986 }
1987 /*
1988 * The function call (or "FuncUndefined" autocommand sequence) might
1989 * have been aborted by an error, an interrupt, or an explicitly thrown
1990 * exception that has not been caught so far. This situation can be
1991 * tested for by calling aborting(). For an error in an internal
1992 * function or for the "E132" error in call_user_func(), however, the
1993 * throw point at which the "force_abort" flag (temporarily reset by
1994 * emsg()) is normally updated has not been reached yet. We need to
1995 * update that flag first to make aborting() reliable.
1996 */
1997 update_force_abort();
1998 }
Bram Moolenaaref140542019-12-31 21:27:13 +01001999 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002000 ret = OK;
2001
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002002theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002003 /*
2004 * Report an error unless the argument evaluation or function call has been
2005 * cancelled due to an aborting error, an interrupt, or an exception.
2006 */
2007 if (!aborting())
2008 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002009 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002010 }
2011
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002012 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002013 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002014 clear_tv(&argv[--argv_clear + argv_base]);
2015
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002016 vim_free(tofree);
2017 vim_free(name);
2018
2019 return ret;
2020}
2021
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 static char_u *
2023printable_func_name(ufunc_T *fp)
2024{
2025 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
2026}
2027
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002028/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002029 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002030 */
2031 static void
2032list_func_head(ufunc_T *fp, int indent)
2033{
2034 int j;
2035
2036 msg_start();
2037 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002038 msg_puts(" ");
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002039 if (fp->uf_dfunc_idx >= 0)
2040 msg_puts("def ");
2041 else
2042 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002044 msg_putchar('(');
2045 for (j = 0; j < fp->uf_args.ga_len; ++j)
2046 {
2047 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002048 msg_puts(", ");
2049 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 if (fp->uf_arg_types != NULL)
2051 {
2052 char *tofree;
2053
2054 msg_puts(": ");
2055 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
2056 vim_free(tofree);
2057 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002058 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
2059 {
2060 msg_puts(" = ");
2061 msg_puts(((char **)(fp->uf_def_args.ga_data))
2062 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
2063 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002064 }
2065 if (fp->uf_varargs)
2066 {
2067 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002068 msg_puts(", ");
2069 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002070 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 if (fp->uf_va_name != NULL)
2072 {
2073 if (j)
2074 msg_puts(", ");
2075 msg_puts("...");
2076 msg_puts((char *)fp->uf_va_name);
2077 if (fp->uf_va_type)
2078 {
2079 char *tofree;
2080
2081 msg_puts(": ");
2082 msg_puts(type_name(fp->uf_va_type, &tofree));
2083 vim_free(tofree);
2084 }
2085 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002086 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002087
2088 if (fp->uf_dfunc_idx >= 0)
2089 {
2090 if (fp->uf_ret_type != &t_void)
2091 {
2092 char *tofree;
2093
2094 msg_puts(": ");
2095 msg_puts(type_name(fp->uf_ret_type, &tofree));
2096 vim_free(tofree);
2097 }
2098 }
2099 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002100 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002101 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002102 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002103 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002104 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002105 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002106 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002107 msg_clr_eos();
2108 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002109 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002110}
2111
2112/*
2113 * Get a function name, translating "<SID>" and "<SNR>".
2114 * Also handles a Funcref in a List or Dictionary.
2115 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002116 * Set "*is_global" to TRUE when the function must be global, unless
2117 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002118 * flags:
2119 * TFN_INT: internal function name OK
2120 * TFN_QUIET: be quiet
2121 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002122 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002123 * Advances "pp" to just after the function name (if no error).
2124 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002125 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002126trans_function_name(
2127 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002128 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002129 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002130 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002131 funcdict_T *fdp, // return: info about dictionary used
2132 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002133{
2134 char_u *name = NULL;
2135 char_u *start;
2136 char_u *end;
2137 int lead;
2138 char_u sid_buf[20];
2139 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002141 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002143
2144 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002145 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002146 start = *pp;
2147
Bram Moolenaare38eab22019-12-05 21:50:01 +01002148 // Check for hard coded <SNR>: already translated function ID (from a user
2149 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002150 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2151 && (*pp)[2] == (int)KE_SNR)
2152 {
2153 *pp += 3;
2154 len = get_id_len(pp) + 3;
2155 return vim_strnsave(start, len);
2156 }
2157
Bram Moolenaare38eab22019-12-05 21:50:01 +01002158 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2159 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002160 lead = eval_fname_script(start);
2161 if (lead > 2)
2162 start += lead;
2163
Bram Moolenaare38eab22019-12-05 21:50:01 +01002164 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002165 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002166 lead > 2 ? 0 : FNE_CHECK_START);
2167 if (end == start)
2168 {
2169 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002170 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002171 goto theend;
2172 }
2173 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2174 {
2175 /*
2176 * Report an invalid expression in braces, unless the expression
2177 * evaluation has been cancelled due to an aborting error, an
2178 * interrupt, or an exception.
2179 */
2180 if (!aborting())
2181 {
2182 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002183 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002184 }
2185 else
2186 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2187 goto theend;
2188 }
2189
2190 if (lv.ll_tv != NULL)
2191 {
2192 if (fdp != NULL)
2193 {
2194 fdp->fd_dict = lv.ll_dict;
2195 fdp->fd_newkey = lv.ll_newkey;
2196 lv.ll_newkey = NULL;
2197 fdp->fd_di = lv.ll_di;
2198 }
2199 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2200 {
2201 name = vim_strsave(lv.ll_tv->vval.v_string);
2202 *pp = end;
2203 }
2204 else if (lv.ll_tv->v_type == VAR_PARTIAL
2205 && lv.ll_tv->vval.v_partial != NULL)
2206 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002207 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002208 *pp = end;
2209 if (partial != NULL)
2210 *partial = lv.ll_tv->vval.v_partial;
2211 }
2212 else
2213 {
2214 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2215 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002216 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002217 else
2218 *pp = end;
2219 name = NULL;
2220 }
2221 goto theend;
2222 }
2223
2224 if (lv.ll_name == NULL)
2225 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002226 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002227 *pp = end;
2228 goto theend;
2229 }
2230
Bram Moolenaare38eab22019-12-05 21:50:01 +01002231 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002232 if (lv.ll_exp_name != NULL)
2233 {
2234 len = (int)STRLEN(lv.ll_exp_name);
2235 name = deref_func_name(lv.ll_exp_name, &len, partial,
2236 flags & TFN_NO_AUTOLOAD);
2237 if (name == lv.ll_exp_name)
2238 name = NULL;
2239 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002240 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002241 {
2242 len = (int)(end - *pp);
2243 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2244 if (name == *pp)
2245 name = NULL;
2246 }
2247 if (name != NULL)
2248 {
2249 name = vim_strsave(name);
2250 *pp = end;
2251 if (STRNCMP(name, "<SNR>", 5) == 0)
2252 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002253 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002254 name[0] = K_SPECIAL;
2255 name[1] = KS_EXTRA;
2256 name[2] = (int)KE_SNR;
2257 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2258 }
2259 goto theend;
2260 }
2261
2262 if (lv.ll_exp_name != NULL)
2263 {
2264 len = (int)STRLEN(lv.ll_exp_name);
2265 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2266 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2267 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002268 // When there was "s:" already or the name expanded to get a
2269 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002270 lv.ll_name += 2;
2271 len -= 2;
2272 lead = 2;
2273 }
2274 }
2275 else
2276 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002277 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002278 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002279 {
2280 if (is_global != NULL && lv.ll_name[0] == 'g')
2281 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002282 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002283 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002284 len = (int)(end - lv.ll_name);
2285 }
2286
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287 // In Vim9 script a user function is script-local by default.
2288 vim9script = ASCII_ISUPPER(*start)
2289 && current_sctx.sc_version == SCRIPT_VERSION_VIM9;
2290
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002291 /*
2292 * Copy the function name to allocated memory.
2293 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2294 * Accept <SNR>123_name() outside a script.
2295 */
2296 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002297 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002299 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 if (!vim9script)
2301 lead = 3;
2302 if (vim9script || (lv.ll_exp_name != NULL
2303 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002304 || eval_fname_sid(*pp))
2305 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002307 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002308 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002309 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002310 goto theend;
2311 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002312 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 if (vim9script)
2314 extra = 3 + (int)STRLEN(sid_buf);
2315 else
2316 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002317 }
2318 }
2319 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002321 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002322 start);
2323 goto theend;
2324 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002325 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002326 {
2327 char_u *cp = vim_strchr(lv.ll_name, ':');
2328
2329 if (cp != NULL && cp < end)
2330 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002331 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002332 goto theend;
2333 }
2334 }
2335
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002337 if (name != NULL)
2338 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002339 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002340 {
2341 name[0] = K_SPECIAL;
2342 name[1] = KS_EXTRA;
2343 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002345 STRCPY(name + 3, sid_buf);
2346 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2348 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002349 }
2350 *pp = end;
2351
2352theend:
2353 clear_lval(&lv);
2354 return name;
2355}
2356
2357/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02002358 * Assuming "name" is the result of trans_function_name() and it was prefixed
2359 * to use the script-local name, return the unmodified name (points into
2360 * "name"). Otherwise return NULL.
2361 * This can be used to first search for a script-local function and fall back
2362 * to the global function if not found.
2363 */
2364 char_u *
2365untrans_function_name(char_u *name)
2366{
2367 char_u *p;
2368
2369 if (*name == K_SPECIAL && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2370 {
2371 p = vim_strchr(name, '_');
2372 if (p != NULL)
2373 return p + 1;
2374 }
2375 return NULL;
2376}
2377
2378/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002379 * ":function"
2380 */
2381 void
2382ex_function(exarg_T *eap)
2383{
2384 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002385 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002386 int j;
2387 int c;
2388 int saved_did_emsg;
2389 int saved_wait_return = need_wait_return;
2390 char_u *name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002391 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002392 char_u *p;
2393 char_u *arg;
2394 char_u *line_arg = NULL;
2395 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002397 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002398 garray_T newlines;
2399 int varargs = FALSE;
2400 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002402 ufunc_T *fp;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002403 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002404 int indent;
2405 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406#define MAX_FUNC_NESTING 50
2407 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002408 dictitem_T *v;
2409 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002410 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002411 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002412 int todo;
2413 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002414 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002415 linenr_T sourcing_lnum_off;
2416 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002417 int is_heredoc = FALSE;
2418 char_u *skip_until = NULL;
2419 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002420
2421 /*
2422 * ":function" without argument: list functions.
2423 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002424 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002425 {
2426 if (!eap->skip)
2427 {
2428 todo = (int)func_hashtab.ht_used;
2429 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2430 {
2431 if (!HASHITEM_EMPTY(hi))
2432 {
2433 --todo;
2434 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 if ((fp->uf_flags & FC_DEAD)
2436 || message_filtered(fp->uf_name))
Bram Moolenaarf86db782018-10-25 13:31:37 +02002437 continue;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002438 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002439 list_func_head(fp, FALSE);
2440 }
2441 }
2442 }
2443 eap->nextcmd = check_nextcmd(eap->arg);
2444 return;
2445 }
2446
2447 /*
2448 * ":function /pat": list functions matching pattern.
2449 */
2450 if (*eap->arg == '/')
2451 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002452 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002453 if (!eap->skip)
2454 {
2455 regmatch_T regmatch;
2456
2457 c = *p;
2458 *p = NUL;
2459 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2460 *p = c;
2461 if (regmatch.regprog != NULL)
2462 {
2463 regmatch.rm_ic = p_ic;
2464
2465 todo = (int)func_hashtab.ht_used;
2466 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2467 {
2468 if (!HASHITEM_EMPTY(hi))
2469 {
2470 --todo;
2471 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 if ((fp->uf_flags & FC_DEAD) == 0
2473 && !isdigit(*fp->uf_name)
2474 && vim_regexec(&regmatch, fp->uf_name, 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002475 list_func_head(fp, FALSE);
2476 }
2477 }
2478 vim_regfree(regmatch.regprog);
2479 }
2480 }
2481 if (*p == '/')
2482 ++p;
2483 eap->nextcmd = check_nextcmd(p);
2484 return;
2485 }
2486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 ga_init(&newargs);
2488 ga_init(&argtypes);
2489 ga_init(&default_args);
2490
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002491 /*
2492 * Get the function name. There are these situations:
2493 * func normal function name
2494 * "name" == func, "fudi.fd_dict" == NULL
2495 * dict.func new dictionary entry
2496 * "name" == NULL, "fudi.fd_dict" set,
2497 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2498 * dict.func existing dict entry with a Funcref
2499 * "name" == func, "fudi.fd_dict" set,
2500 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2501 * dict.func existing dict entry that's not a Funcref
2502 * "name" == NULL, "fudi.fd_dict" set,
2503 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2504 * s:func script-local function name
2505 * g:func global function name, same as "func"
2506 */
2507 p = eap->arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002508 name = trans_function_name(&p, &is_global, eap->skip,
2509 TFN_NO_AUTOLOAD, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002510 paren = (vim_strchr(p, '(') != NULL);
2511 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
2512 {
2513 /*
2514 * Return on an invalid expression in braces, unless the expression
2515 * evaluation has been cancelled due to an aborting error, an
2516 * interrupt, or an exception.
2517 */
2518 if (!aborting())
2519 {
2520 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002521 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002522 vim_free(fudi.fd_newkey);
2523 return;
2524 }
2525 else
2526 eap->skip = TRUE;
2527 }
2528
Bram Moolenaare38eab22019-12-05 21:50:01 +01002529 // An error in a function call during evaluation of an expression in magic
2530 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002531 saved_did_emsg = did_emsg;
2532 did_emsg = FALSE;
2533
2534 /*
2535 * ":function func" with only function name: list function.
2536 */
2537 if (!paren)
2538 {
2539 if (!ends_excmd(*skipwhite(p)))
2540 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002541 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002542 goto ret_free;
2543 }
2544 eap->nextcmd = check_nextcmd(p);
2545 if (eap->nextcmd != NULL)
2546 *p = NUL;
2547 if (!eap->skip && !got_int)
2548 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002549 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002550 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
2551 {
2552 char_u *up = untrans_function_name(name);
2553
2554 // With Vim9 script the name was made script-local, if not
2555 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02002556 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002557 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002558 }
2559
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002560 if (fp != NULL)
2561 {
2562 list_func_head(fp, TRUE);
2563 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2564 {
2565 if (FUNCLINE(fp, j) == NULL)
2566 continue;
2567 msg_putchar('\n');
2568 msg_outnum((long)(j + 1));
2569 if (j < 9)
2570 msg_putchar(' ');
2571 if (j < 99)
2572 msg_putchar(' ');
2573 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002574 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002575 ui_breakcheck();
2576 }
2577 if (!got_int)
2578 {
2579 msg_putchar('\n');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580 if (fp->uf_dfunc_idx >= 0)
2581 msg_puts(" enddef");
2582 else
2583 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002584 }
2585 }
2586 else
Bram Moolenaara26b9702020-04-18 19:53:28 +02002587 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002588 }
2589 goto ret_free;
2590 }
2591
2592 /*
2593 * ":function name(arg1, arg2)" Define function.
2594 */
2595 p = skipwhite(p);
2596 if (*p != '(')
2597 {
2598 if (!eap->skip)
2599 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002600 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002601 goto ret_free;
2602 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002603 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002604 if (vim_strchr(p, '(') != NULL)
2605 p = vim_strchr(p, '(');
2606 }
2607 p = skipwhite(p + 1);
2608
2609 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2610
2611 if (!eap->skip)
2612 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002613 // Check the name of the function. Unless it's a dictionary function
2614 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002615 if (name != NULL)
2616 arg = name;
2617 else
2618 arg = fudi.fd_newkey;
2619 if (arg != NULL && (fudi.fd_di == NULL
2620 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2621 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2622 {
2623 if (*arg == K_SPECIAL)
2624 j = 3;
2625 else
2626 j = 0;
2627 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2628 : eval_isnamec(arg[j])))
2629 ++j;
2630 if (arg[j] != NUL)
2631 emsg_funcname((char *)e_invarg2, arg);
2632 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002633 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002634 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002635 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002636 }
2637
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002638 // This may get more lines and make the pointers into the first line
2639 // invalid.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 if (get_function_args(&p, ')', &newargs,
2641 eap->cmdidx == CMD_def ? &argtypes : NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002642 &varargs, &default_args, eap->skip,
2643 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002644 goto errret_2;
2645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002647 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 // find the return type: :def Func(): type
2649 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002650 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 ret_type = skipwhite(p + 1);
2652 p = skip_type(ret_type);
2653 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002654 {
2655 ret_type = vim_strnsave(ret_type, (int)(p - ret_type));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002657 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002659 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 semsg(_("E1056: expected a type: %s"), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02002661 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002662 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002663 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002664 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 else
2666 // find extra arguments "range", "dict", "abort" and "closure"
2667 for (;;)
2668 {
2669 p = skipwhite(p);
2670 if (STRNCMP(p, "range", 5) == 0)
2671 {
2672 flags |= FC_RANGE;
2673 p += 5;
2674 }
2675 else if (STRNCMP(p, "dict", 4) == 0)
2676 {
2677 flags |= FC_DICT;
2678 p += 4;
2679 }
2680 else if (STRNCMP(p, "abort", 5) == 0)
2681 {
2682 flags |= FC_ABORT;
2683 p += 5;
2684 }
2685 else if (STRNCMP(p, "closure", 7) == 0)
2686 {
2687 flags |= FC_CLOSURE;
2688 p += 7;
2689 if (current_funccal == NULL)
2690 {
2691 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
2692 name == NULL ? (char_u *)"" : name);
2693 goto erret;
2694 }
2695 }
2696 else
2697 break;
2698 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002699
Bram Moolenaare38eab22019-12-05 21:50:01 +01002700 // When there is a line break use what follows for the function body.
2701 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002702 if (*p == '\n')
2703 line_arg = p + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002704 else if (*p != NUL && *p != '"' && !(eap->cmdidx == CMD_def && *p == '#')
2705 && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002706 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002707
2708 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
2710 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002711 */
2712 if (KeyTyped)
2713 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002714 // Check if the function already exists, don't let the user type the
2715 // whole function before telling him it doesn't work! For a script we
2716 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002717 if (!eap->skip && !eap->forceit)
2718 {
2719 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002720 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002721 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002722 emsg_funcname(e_funcexts, name);
2723 }
2724
2725 if (!eap->skip && did_emsg)
2726 goto erret;
2727
Bram Moolenaare38eab22019-12-05 21:50:01 +01002728 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002729 cmdline_row = msg_row;
2730 }
2731
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002732 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002733 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002734
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002735 indent = 2;
2736 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002737 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002738 for (;;)
2739 {
2740 if (KeyTyped)
2741 {
2742 msg_scroll = TRUE;
2743 saved_wait_return = FALSE;
2744 }
2745 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002746
2747 if (line_arg != NULL)
2748 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002749 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002750 theline = line_arg;
2751 p = vim_strchr(theline, '\n');
2752 if (p == NULL)
2753 line_arg += STRLEN(line_arg);
2754 else
2755 {
2756 *p = NUL;
2757 line_arg = p + 1;
2758 }
2759 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002760 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002761 {
2762 vim_free(line_to_free);
2763 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002764 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002765 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002766 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002767 line_to_free = theline;
2768 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002769 if (KeyTyped)
2770 lines_left = Rows - 1;
2771 if (theline == NULL)
2772 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 if (eap->cmdidx == CMD_def)
2774 emsg(_("E1057: Missing :enddef"));
2775 else
2776 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002777 goto erret;
2778 }
2779
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002780 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002781 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002782 if (SOURCING_LNUM < sourcing_lnum_off)
2783 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002784 else
2785 sourcing_lnum_off = 0;
2786
2787 if (skip_until != NULL)
2788 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002790 // * ":append" and "."
2791 // * ":python <<EOF" and "EOF"
2792 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2793 if (heredoc_trimmed == NULL
2794 || (is_heredoc && skipwhite(theline) == theline)
2795 || STRNCMP(theline, heredoc_trimmed,
2796 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002797 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002798 if (heredoc_trimmed == NULL)
2799 p = theline;
2800 else if (is_heredoc)
2801 p = skipwhite(theline) == theline
2802 ? theline : theline + STRLEN(heredoc_trimmed);
2803 else
2804 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002805 if (STRCMP(p, skip_until) == 0)
2806 {
2807 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002808 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002809 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002810 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002811 }
2812 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002813 }
2814 else
2815 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002816 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01002817 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002818 ;
2819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 // Check for "endfunction" or "enddef".
2821 if (checkforcmd(&p, nesting_def[nesting]
2822 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002823 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002824 char_u *nextcmd = NULL;
2825
Bram Moolenaar663bb232017-06-22 19:12:10 +02002826 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002827 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002828 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002829 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002830 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 give_warning2(eap->cmdidx == CMD_def
2832 ? (char_u *)_("W1001: Text found after :enddef: %s")
2833 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002834 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002835 if (nextcmd != NULL)
2836 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002837 // Another command follows. If the line came from "eap" we
2838 // can simply point into it, otherwise we need to change
2839 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02002840 eap->nextcmd = nextcmd;
2841 if (line_to_free != NULL)
2842 {
2843 vim_free(*eap->cmdlinep);
2844 *eap->cmdlinep = line_to_free;
2845 line_to_free = NULL;
2846 }
2847 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002848 break;
2849 }
2850
Bram Moolenaare38eab22019-12-05 21:50:01 +01002851 // Increase indent inside "if", "while", "for" and "try", decrease
2852 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002854 indent -= 2;
2855 else if (STRNCMP(p, "if", 2) == 0
2856 || STRNCMP(p, "wh", 2) == 0
2857 || STRNCMP(p, "for", 3) == 0
2858 || STRNCMP(p, "try", 3) == 0)
2859 indent += 2;
2860
Bram Moolenaare38eab22019-12-05 21:50:01 +01002861 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01002862 // Only recognize "def" inside "def", not inside "function",
2863 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01002865 if (checkforcmd(&p, "function", 2)
2866 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002867 {
2868 if (*p == '!')
2869 p = skipwhite(p + 1);
2870 p += eval_fname_script(p);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002871 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL, NULL));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002872 if (*skipwhite(p) == '(')
2873 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 if (nesting == MAX_FUNC_NESTING - 1)
2875 emsg(_("E1058: function nesting too deep"));
2876 else
2877 {
2878 ++nesting;
2879 nesting_def[nesting] = (c == 'd');
2880 indent += 2;
2881 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002882 }
2883 }
2884
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002885 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002886 p = skip_range(p, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002887 if (eap->cmdidx != CMD_def
2888 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002889 || (p[0] == 'c'
2890 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2891 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2892 && (STRNCMP(&p[3], "nge", 3) != 0
2893 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002894 || (p[0] == 'i'
2895 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002896 && (!ASCII_ISALPHA(p[2])
2897 || (p[2] == 's'
2898 && (!ASCII_ISALPHA(p[3])
2899 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002900 skip_until = vim_strsave((char_u *)".");
2901
Bram Moolenaare38eab22019-12-05 21:50:01 +01002902 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002903 arg = skipwhite(skiptowhite(p));
2904 if (arg[0] == '<' && arg[1] =='<'
2905 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002906 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2907 || ((p[2] == '3' || p[2] == 'x')
2908 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002909 || (p[0] == 'p' && p[1] == 'e'
2910 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2911 || (p[0] == 't' && p[1] == 'c'
2912 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2913 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2914 && !ASCII_ISALPHA(p[3]))
2915 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2916 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2917 || (p[0] == 'm' && p[1] == 'z'
2918 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2919 ))
2920 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002921 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002922 p = skipwhite(arg + 2);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02002923 if (STRNCMP(p, "trim", 4) == 0)
2924 {
2925 // Ignore leading white space.
2926 p = skipwhite(p + 4);
2927 heredoc_trimmed = vim_strnsave(theline,
2928 (int)(skipwhite(theline) - theline));
2929 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002930 if (*p == NUL)
2931 skip_until = vim_strsave((char_u *)".");
2932 else
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02002933 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2934 do_concat = FALSE;
2935 is_heredoc = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002936 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002937
2938 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002939 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02002940 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002941 if (*arg == '[')
2942 arg = vim_strchr(arg, ']');
2943 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002944 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002945 arg = skipwhite(skiptowhite(arg));
2946 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
2947 && ((p[0] == 'l'
2948 && p[1] == 'e'
2949 && (!ASCII_ISALNUM(p[2])
2950 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02002951 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002952 p = skipwhite(arg + 3);
2953 if (STRNCMP(p, "trim", 4) == 0)
2954 {
2955 // Ignore leading white space.
2956 p = skipwhite(p + 4);
2957 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar8471e572019-05-19 21:37:18 +02002958 (int)(skipwhite(theline) - theline));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002959 }
2960 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2961 do_concat = FALSE;
2962 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002963 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002964 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002965 }
2966
Bram Moolenaare38eab22019-12-05 21:50:01 +01002967 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002968 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002969 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002970
Bram Moolenaare38eab22019-12-05 21:50:01 +01002971 // Copy the line to newly allocated memory. get_one_sourceline()
2972 // allocates 250 bytes per line, this saves 80% on average. The cost
2973 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002974 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002975 if (p == NULL)
2976 goto erret;
2977 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002978
Bram Moolenaare38eab22019-12-05 21:50:01 +01002979 // Add NULL lines for continuation lines, so that the line count is
2980 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002981 while (sourcing_lnum_off-- > 0)
2982 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2983
Bram Moolenaare38eab22019-12-05 21:50:01 +01002984 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002985 if (line_arg != NULL && *line_arg == NUL)
2986 line_arg = NULL;
2987 }
2988
Bram Moolenaare38eab22019-12-05 21:50:01 +01002989 // Don't define the function when skipping commands or when an error was
2990 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002991 if (eap->skip || did_emsg)
2992 goto erret;
2993
2994 /*
2995 * If there are no errors, add the function
2996 */
2997 if (fudi.fd_dict == NULL)
2998 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 hashtab_T *ht;
3000
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003001 v = find_var(name, &ht, FALSE);
3002 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
3003 {
3004 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
3005 name);
3006 goto erret;
3007 }
3008
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003009 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003010 if (fp != NULL)
3011 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 int dead = fp->uf_flags & FC_DEAD;
3013
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003014 // Function can be replaced with "function!" and when sourcing the
3015 // same script again, but only once.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 if (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003017 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
3018 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003019 {
3020 emsg_funcname(e_funcexts, name);
3021 goto erret;
3022 }
3023 if (fp->uf_calls > 0)
3024 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003025 emsg_funcname(
3026 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003027 name);
3028 goto erret;
3029 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003030 if (fp->uf_refcount > 1)
3031 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003032 // This function is referenced somewhere, don't redefine it but
3033 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003034 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003035 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003036 fp = NULL;
3037 overwrite = TRUE;
3038 }
3039 else
3040 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003041 char_u *exp_name = fp->uf_name_exp;
3042
3043 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01003044 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003045 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003046 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003047 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003049#ifdef FEAT_PROFILE
3050 fp->uf_profiling = FALSE;
3051 fp->uf_prof_initialized = FALSE;
3052#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003053 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003054 }
3055 }
3056 else
3057 {
3058 char numbuf[20];
3059
3060 fp = NULL;
3061 if (fudi.fd_newkey == NULL && !eap->forceit)
3062 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003063 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003064 goto erret;
3065 }
3066 if (fudi.fd_di == NULL)
3067 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003068 // Can't add a function to a locked dictionary
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003069 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003070 goto erret;
3071 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003072 // Can't change an existing function if it is locked
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003073 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003074 goto erret;
3075
Bram Moolenaare38eab22019-12-05 21:50:01 +01003076 // Give the function a sequential number. Can only be used with a
3077 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003078 vim_free(name);
3079 sprintf(numbuf, "%d", ++func_nr);
3080 name = vim_strsave((char_u *)numbuf);
3081 if (name == NULL)
3082 goto erret;
3083 }
3084
3085 if (fp == NULL)
3086 {
3087 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3088 {
3089 int slen, plen;
3090 char_u *scriptname;
3091
Bram Moolenaare38eab22019-12-05 21:50:01 +01003092 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003093 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003094 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003095 {
3096 scriptname = autoload_name(name);
3097 if (scriptname != NULL)
3098 {
3099 p = vim_strchr(scriptname, '/');
3100 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003101 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003102 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003103 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003104 j = OK;
3105 vim_free(scriptname);
3106 }
3107 }
3108 if (j == FAIL)
3109 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003110 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003111 goto erret;
3112 }
3113 }
3114
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003115 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003116 if (fp == NULL)
3117 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 fp->uf_dfunc_idx = -1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003119
3120 if (fudi.fd_dict != NULL)
3121 {
3122 if (fudi.fd_di == NULL)
3123 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003124 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003125 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3126 if (fudi.fd_di == NULL)
3127 {
3128 vim_free(fp);
3129 goto erret;
3130 }
3131 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3132 {
3133 vim_free(fudi.fd_di);
3134 vim_free(fp);
3135 goto erret;
3136 }
3137 }
3138 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003139 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003140 clear_tv(&fudi.fd_di->di_tv);
3141 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003142 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003143
Bram Moolenaare38eab22019-12-05 21:50:01 +01003144 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003145 flags |= FC_DICT;
3146 }
3147
Bram Moolenaare38eab22019-12-05 21:50:01 +01003148 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003149 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003150 if (overwrite)
3151 {
3152 hi = hash_find(&func_hashtab, name);
3153 hi->hi_key = UF2HIKEY(fp);
3154 }
3155 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003156 {
3157 vim_free(fp);
3158 goto erret;
3159 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003160 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003161 }
3162 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003163 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003165 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166
3167 if (eap->cmdidx == CMD_def)
3168 {
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003169 int lnum_save = SOURCING_LNUM;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003170
3171 // error messages are for the first function line
3172 SOURCING_LNUM = sourcing_lnum_top;
3173
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 // parse the argument types
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003175 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176
3177 if (argtypes.ga_len > 0)
3178 {
3179 // When "varargs" is set the last name/type goes into uf_va_name
3180 // and uf_va_type.
3181 int len = argtypes.ga_len - (varargs ? 1 : 0);
3182
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003183 if (len > 0)
3184 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 if (fp->uf_arg_types != NULL)
3186 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003187 int i;
3188 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189
3190 for (i = 0; i < len; ++ i)
3191 {
3192 p = ((char_u **)argtypes.ga_data)[i];
3193 if (p == NULL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003194 // will get the type from the default value
3195 type = &t_unknown;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 else
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003197 type = parse_type(&p, &fp->uf_type_list);
3198 if (type == NULL)
3199 {
3200 SOURCING_LNUM = lnum_save;
3201 goto errret_2;
3202 }
3203 fp->uf_arg_types[i] = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 }
3205 }
3206 if (varargs)
3207 {
3208 // Move the last argument "...name: type" to uf_va_name and
3209 // uf_va_type.
3210 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
3211 [fp->uf_args.ga_len - 1];
3212 --fp->uf_args.ga_len;
3213 p = ((char_u **)argtypes.ga_data)[len];
3214 if (p == NULL)
3215 // todo: get type from default value
3216 fp->uf_va_type = &t_any;
3217 else
3218 fp->uf_va_type = parse_type(&p, &fp->uf_type_list);
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003219 if (fp->uf_va_type == NULL)
3220 {
3221 SOURCING_LNUM = lnum_save;
3222 goto errret_2;
3223 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 }
3225 varargs = FALSE;
3226 }
3227
3228 // parse the return type, if any
3229 if (ret_type == NULL)
3230 fp->uf_ret_type = &t_void;
3231 else
3232 {
3233 p = ret_type;
3234 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list);
3235 }
3236 }
3237
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003238 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003239 if ((flags & FC_CLOSURE) != 0)
3240 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003241 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003242 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003243 }
3244 else
3245 fp->uf_scoped = NULL;
3246
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003247#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003248 if (prof_def_func())
3249 func_do_profile(fp);
3250#endif
3251 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003252 if (sandbox)
3253 flags |= FC_SANDBOX;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003254 if (in_vim9script() && !ASCII_ISUPPER(*fp->uf_name))
3255 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003256 fp->uf_flags = flags;
3257 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003258 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003259 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003260 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 if (is_export)
3262 {
3263 fp->uf_flags |= FC_EXPORT;
3264 // let ex_export() know the export worked.
3265 is_export = FALSE;
3266 }
3267
3268 // ":def Func()" needs to be compiled
3269 if (eap->cmdidx == CMD_def)
3270 compile_def_function(fp, FALSE);
3271
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003272 goto ret_free;
3273
3274erret:
3275 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003276 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003277errret_2:
3278 ga_clear_strings(&newlines);
3279ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003280 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003281 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003282 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003283 vim_free(fudi.fd_newkey);
3284 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003285 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003286 did_emsg |= saved_did_emsg;
3287 need_wait_return |= saved_wait_return;
3288}
3289
3290/*
3291 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3292 * Return 2 if "p" starts with "s:".
3293 * Return 0 otherwise.
3294 */
3295 int
3296eval_fname_script(char_u *p)
3297{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003298 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3299 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003300 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3301 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3302 return 5;
3303 if (p[0] == 's' && p[1] == ':')
3304 return 2;
3305 return 0;
3306}
3307
3308 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003309translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003310{
3311 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003312 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003313 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314}
3315
3316/*
3317 * Return TRUE when "ufunc" has old-style "..." varargs
3318 * or named varargs "...name: type".
3319 */
3320 int
3321has_varargs(ufunc_T *ufunc)
3322{
3323 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003324}
3325
3326/*
3327 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003328 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003329 */
3330 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003331function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003332{
3333 char_u *nm = name;
3334 char_u *p;
3335 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003336 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003337 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003338
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003339 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3340 if (no_deref)
3341 flag |= TFN_NO_DEREF;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003342 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003343 nm = skipwhite(nm);
3344
Bram Moolenaare38eab22019-12-05 21:50:01 +01003345 // Only accept "funcname", "funcname ", "funcname (..." and
3346 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003347 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003348 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003349 vim_free(p);
3350 return n;
3351}
3352
Bram Moolenaar113e1072019-01-20 15:30:40 +01003353#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003354 char_u *
3355get_expanded_name(char_u *name, int check)
3356{
3357 char_u *nm = name;
3358 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003359 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003360
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003361 p = trans_function_name(&nm, &is_global, FALSE,
3362 TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003363
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003364 if (p != NULL && *nm == NUL
3365 && (!check || translated_function_exists(p, is_global)))
3366 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003367
3368 vim_free(p);
3369 return NULL;
3370}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003371#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003372
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003373/*
3374 * Function given to ExpandGeneric() to obtain the list of user defined
3375 * function names.
3376 */
3377 char_u *
3378get_user_func_name(expand_T *xp, int idx)
3379{
3380 static long_u done;
3381 static hashitem_T *hi;
3382 ufunc_T *fp;
3383
3384 if (idx == 0)
3385 {
3386 done = 0;
3387 hi = func_hashtab.ht_array;
3388 }
3389 if (done < func_hashtab.ht_used)
3390 {
3391 if (done++ > 0)
3392 ++hi;
3393 while (HASHITEM_EMPTY(hi))
3394 ++hi;
3395 fp = HI2UF(hi);
3396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 // don't show dead, dict and lambda functions
3398 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02003399 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003401
3402 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003403 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003404
3405 cat_func_name(IObuff, fp);
3406 if (xp->xp_context != EXPAND_USER_FUNC)
3407 {
3408 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003410 STRCAT(IObuff, ")");
3411 }
3412 return IObuff;
3413 }
3414 return NULL;
3415}
3416
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003417/*
3418 * ":delfunction {name}"
3419 */
3420 void
3421ex_delfunction(exarg_T *eap)
3422{
3423 ufunc_T *fp = NULL;
3424 char_u *p;
3425 char_u *name;
3426 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003427 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003428
3429 p = eap->arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003430 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003431 vim_free(fudi.fd_newkey);
3432 if (name == NULL)
3433 {
3434 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003435 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003436 return;
3437 }
3438 if (!ends_excmd(*skipwhite(p)))
3439 {
3440 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003441 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003442 return;
3443 }
3444 eap->nextcmd = check_nextcmd(p);
3445 if (eap->nextcmd != NULL)
3446 *p = NUL;
3447
3448 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003449 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003450 vim_free(name);
3451
3452 if (!eap->skip)
3453 {
3454 if (fp == NULL)
3455 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02003456 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003457 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003458 return;
3459 }
3460 if (fp->uf_calls > 0)
3461 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003462 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003463 return;
3464 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003465 if (fp->uf_flags & FC_VIM9)
3466 {
3467 semsg(_("E1084: Cannot delete Vim9 script function %s"), eap->arg);
3468 return;
3469 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003470
3471 if (fudi.fd_dict != NULL)
3472 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003473 // Delete the dict item that refers to the function, it will
3474 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003475 dictitem_remove(fudi.fd_dict, fudi.fd_di);
3476 }
3477 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003478 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003479 // A normal function (not a numbered function or lambda) has a
3480 // refcount of 1 for the entry in the hashtable. When deleting
3481 // it and the refcount is more than one, it should be kept.
3482 // A numbered function and lambda should be kept if the refcount is
3483 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003484 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003485 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003486 // Function is still referenced somewhere. Don't free it but
3487 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003488 if (func_remove(fp))
3489 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003490 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003491 }
3492 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003493 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003494 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003495 }
3496}
3497
3498/*
3499 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003500 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003501 */
3502 void
3503func_unref(char_u *name)
3504{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003505 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003506
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003507 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003508 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003509 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003510 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003511 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003512#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003513 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003514#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01003515 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003516 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003517 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003518 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003519 // Only delete it when it's not being used. Otherwise it's done
3520 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003521 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003522 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02003523 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003524}
3525
3526/*
3527 * Unreference a Function: decrement the reference count and free it when it
3528 * becomes zero.
3529 */
3530 void
3531func_ptr_unref(ufunc_T *fp)
3532{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003533 if (fp != NULL && --fp->uf_refcount <= 0)
3534 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003535 // Only delete it when it's not being used. Otherwise it's done
3536 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02003537 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003538 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003539 }
3540}
3541
3542/*
3543 * Count a reference to a Function.
3544 */
3545 void
3546func_ref(char_u *name)
3547{
3548 ufunc_T *fp;
3549
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003550 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003551 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003552 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003553 if (fp != NULL)
3554 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003555 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01003556 // Only give an error for a numbered function.
3557 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01003558 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003559}
3560
3561/*
3562 * Count a reference to a Function.
3563 */
3564 void
3565func_ptr_ref(ufunc_T *fp)
3566{
3567 if (fp != NULL)
3568 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003569}
3570
3571/*
3572 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3573 * referenced from anywhere that is in use.
3574 */
3575 static int
3576can_free_funccal(funccall_T *fc, int copyID)
3577{
3578 return (fc->l_varlist.lv_copyID != copyID
3579 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003580 && fc->l_avars.dv_copyID != copyID
3581 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003582}
3583
3584/*
3585 * ":return [expr]"
3586 */
3587 void
3588ex_return(exarg_T *eap)
3589{
3590 char_u *arg = eap->arg;
3591 typval_T rettv;
3592 int returning = FALSE;
3593
3594 if (current_funccal == NULL)
3595 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003596 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003597 return;
3598 }
3599
3600 if (eap->skip)
3601 ++emsg_skip;
3602
3603 eap->nextcmd = NULL;
3604 if ((*arg != NUL && *arg != '|' && *arg != '\n')
3605 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
3606 {
3607 if (!eap->skip)
3608 returning = do_return(eap, FALSE, TRUE, &rettv);
3609 else
3610 clear_tv(&rettv);
3611 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003612 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003613 else if (!eap->skip)
3614 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003615 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003616 update_force_abort();
3617
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003618 /*
3619 * Return unless the expression evaluation has been cancelled due to an
3620 * aborting error, an interrupt, or an exception.
3621 */
3622 if (!aborting())
3623 returning = do_return(eap, FALSE, TRUE, NULL);
3624 }
3625
Bram Moolenaare38eab22019-12-05 21:50:01 +01003626 // When skipping or the return gets pending, advance to the next command
3627 // in this line (!returning). Otherwise, ignore the rest of the line.
3628 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003629 if (returning)
3630 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003631 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003632 eap->nextcmd = check_nextcmd(arg);
3633
3634 if (eap->skip)
3635 --emsg_skip;
3636}
3637
3638/*
3639 * ":1,25call func(arg1, arg2)" function call.
3640 */
3641 void
3642ex_call(exarg_T *eap)
3643{
3644 char_u *arg = eap->arg;
3645 char_u *startarg;
3646 char_u *name;
3647 char_u *tofree;
3648 int len;
3649 typval_T rettv;
3650 linenr_T lnum;
3651 int doesrange;
3652 int failed = FALSE;
3653 funcdict_T fudi;
3654 partial_T *partial = NULL;
3655
3656 if (eap->skip)
3657 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003658 // trans_function_name() doesn't work well when skipping, use eval0()
3659 // instead to skip to any following command, e.g. for:
3660 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003661 ++emsg_skip;
3662 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3663 clear_tv(&rettv);
3664 --emsg_skip;
3665 return;
3666 }
3667
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003668 tofree = trans_function_name(&arg, NULL, eap->skip,
3669 TFN_INT, &fudi, &partial);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003670 if (fudi.fd_newkey != NULL)
3671 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003672 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003673 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003674 vim_free(fudi.fd_newkey);
3675 }
3676 if (tofree == NULL)
3677 return;
3678
Bram Moolenaare38eab22019-12-05 21:50:01 +01003679 // Increase refcount on dictionary, it could get deleted when evaluating
3680 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003681 if (fudi.fd_dict != NULL)
3682 ++fudi.fd_dict->dv_refcount;
3683
Bram Moolenaare38eab22019-12-05 21:50:01 +01003684 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3685 // contents. For VAR_PARTIAL get its partial, unless we already have one
3686 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003687 len = (int)STRLEN(tofree);
3688 name = deref_func_name(tofree, &len,
3689 partial != NULL ? NULL : &partial, FALSE);
3690
Bram Moolenaare38eab22019-12-05 21:50:01 +01003691 // Skip white space to allow ":call func ()". Not good, but required for
3692 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003693 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003694 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003695
3696 if (*startarg != '(')
3697 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003699 goto end;
3700 }
3701
3702 /*
3703 * When skipping, evaluate the function once, to find the end of the
3704 * arguments.
3705 * When the function takes a range, this is discovered after the first
3706 * call, and the loop is broken.
3707 */
3708 if (eap->skip)
3709 {
3710 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003711 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003712 }
3713 else
3714 lnum = eap->line1;
3715 for ( ; lnum <= eap->line2; ++lnum)
3716 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003717 funcexe_T funcexe;
3718
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003719 if (!eap->skip && eap->addr_count > 0)
3720 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003721 if (lnum > curbuf->b_ml.ml_line_count)
3722 {
3723 // If the function deleted lines or switched to another buffer
3724 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003725 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003726 break;
3727 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003728 curwin->w_cursor.lnum = lnum;
3729 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003730 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003731 }
3732 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003733
Bram Moolenaara80faa82020-04-12 19:37:17 +02003734 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003735 funcexe.firstline = eap->line1;
3736 funcexe.lastline = eap->line2;
3737 funcexe.doesrange = &doesrange;
3738 funcexe.evaluate = !eap->skip;
3739 funcexe.partial = partial;
3740 funcexe.selfdict = fudi.fd_dict;
3741 if (get_func_tv(name, -1, &rettv, &arg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003742 {
3743 failed = TRUE;
3744 break;
3745 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003746 if (has_watchexpr())
3747 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003748
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003749 // Handle a function returning a Funcref, Dictionary or List.
3750 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE,
3751 name, &name) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003752 {
3753 failed = TRUE;
3754 break;
3755 }
3756
3757 clear_tv(&rettv);
3758 if (doesrange || eap->skip)
3759 break;
3760
Bram Moolenaare38eab22019-12-05 21:50:01 +01003761 // Stop when immediately aborting on error, or when an interrupt
3762 // occurred or an exception was thrown but not caught.
3763 // get_func_tv() returned OK, so that the check for trailing
3764 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003765 if (aborting())
3766 break;
3767 }
3768 if (eap->skip)
3769 --emsg_skip;
3770
Bram Moolenaare51bb172020-02-16 19:42:23 +01003771 // When inside :try we need to check for following "| catch".
3772 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003773 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003774 // Check for trailing illegal characters and a following command.
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003775 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003776 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01003777 if (!failed)
3778 {
3779 emsg_severe = TRUE;
3780 emsg(_(e_trailing));
3781 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003782 }
3783 else
3784 eap->nextcmd = check_nextcmd(arg);
3785 }
3786
3787end:
3788 dict_unref(fudi.fd_dict);
3789 vim_free(tofree);
3790}
3791
3792/*
3793 * Return from a function. Possibly makes the return pending. Also called
3794 * for a pending return at the ":endtry" or after returning from an extra
3795 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3796 * when called due to a ":return" command. "rettv" may point to a typval_T
3797 * with the return rettv. Returns TRUE when the return can be carried out,
3798 * FALSE when the return gets pending.
3799 */
3800 int
3801do_return(
3802 exarg_T *eap,
3803 int reanimate,
3804 int is_cmd,
3805 void *rettv)
3806{
3807 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01003808 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003809
3810 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003811 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003812 current_funccal->returned = FALSE;
3813
3814 /*
3815 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3816 * not in its finally clause (which then is to be executed next) is found.
3817 * In this case, make the ":return" pending for execution at the ":endtry".
3818 * Otherwise, return normally.
3819 */
3820 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3821 if (idx >= 0)
3822 {
3823 cstack->cs_pending[idx] = CSTP_RETURN;
3824
3825 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003826 // A pending return again gets pending. "rettv" points to an
3827 // allocated variable with the rettv of the original ":return"'s
3828 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003829 cstack->cs_rettv[idx] = rettv;
3830 else
3831 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003832 // When undoing a return in order to make it pending, get the stored
3833 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003834 if (reanimate)
3835 rettv = current_funccal->rettv;
3836
3837 if (rettv != NULL)
3838 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003839 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003840 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3841 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3842 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003843 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003844 }
3845 else
3846 cstack->cs_rettv[idx] = NULL;
3847
3848 if (reanimate)
3849 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003850 // The pending return value could be overwritten by a ":return"
3851 // without argument in a finally clause; reset the default
3852 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003853 current_funccal->rettv->v_type = VAR_NUMBER;
3854 current_funccal->rettv->vval.v_number = 0;
3855 }
3856 }
3857 report_make_pending(CSTP_RETURN, rettv);
3858 }
3859 else
3860 {
3861 current_funccal->returned = TRUE;
3862
Bram Moolenaare38eab22019-12-05 21:50:01 +01003863 // If the return is carried out now, store the return value. For
3864 // a return immediately after reanimation, the value is already
3865 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003866 if (!reanimate && rettv != NULL)
3867 {
3868 clear_tv(current_funccal->rettv);
3869 *current_funccal->rettv = *(typval_T *)rettv;
3870 if (!is_cmd)
3871 vim_free(rettv);
3872 }
3873 }
3874
3875 return idx < 0;
3876}
3877
3878/*
3879 * Free the variable with a pending return value.
3880 */
3881 void
3882discard_pending_return(void *rettv)
3883{
3884 free_tv((typval_T *)rettv);
3885}
3886
3887/*
3888 * Generate a return command for producing the value of "rettv". The result
3889 * is an allocated string. Used by report_pending() for verbose messages.
3890 */
3891 char_u *
3892get_return_cmd(void *rettv)
3893{
3894 char_u *s = NULL;
3895 char_u *tofree = NULL;
3896 char_u numbuf[NUMBUFLEN];
3897
3898 if (rettv != NULL)
3899 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3900 if (s == NULL)
3901 s = (char_u *)"";
3902
3903 STRCPY(IObuff, ":return ");
3904 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3905 if (STRLEN(s) + 8 >= IOSIZE)
3906 STRCPY(IObuff + IOSIZE - 4, "...");
3907 vim_free(tofree);
3908 return vim_strsave(IObuff);
3909}
3910
3911/*
3912 * Get next function line.
3913 * Called by do_cmdline() to get the next line.
3914 * Returns allocated string, or NULL for end of function.
3915 */
3916 char_u *
3917get_func_line(
3918 int c UNUSED,
3919 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003920 int indent UNUSED,
3921 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003922{
3923 funccall_T *fcp = (funccall_T *)cookie;
3924 ufunc_T *fp = fcp->func;
3925 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003926 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003927
Bram Moolenaare38eab22019-12-05 21:50:01 +01003928 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003929 if (fcp->dbg_tick != debug_tick)
3930 {
3931 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003932 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003933 fcp->dbg_tick = debug_tick;
3934 }
3935#ifdef FEAT_PROFILE
3936 if (do_profiling == PROF_YES)
3937 func_line_end(cookie);
3938#endif
3939
3940 gap = &fp->uf_lines;
3941 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3942 || fcp->returned)
3943 retval = NULL;
3944 else
3945 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003946 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003947 while (fcp->linenr < gap->ga_len
3948 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3949 ++fcp->linenr;
3950 if (fcp->linenr >= gap->ga_len)
3951 retval = NULL;
3952 else
3953 {
3954 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003955 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003956#ifdef FEAT_PROFILE
3957 if (do_profiling == PROF_YES)
3958 func_line_start(cookie);
3959#endif
3960 }
3961 }
3962
Bram Moolenaare38eab22019-12-05 21:50:01 +01003963 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003964 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003965 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003966 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003967 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003968 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003969 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003970 fcp->dbg_tick = debug_tick;
3971 }
3972
3973 return retval;
3974}
3975
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003976/*
3977 * Return TRUE if the currently active function should be ended, because a
3978 * return was encountered or an error occurred. Used inside a ":while".
3979 */
3980 int
3981func_has_ended(void *cookie)
3982{
3983 funccall_T *fcp = (funccall_T *)cookie;
3984
Bram Moolenaare38eab22019-12-05 21:50:01 +01003985 // Ignore the "abort" flag if the abortion behavior has been changed due to
3986 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003987 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3988 || fcp->returned);
3989}
3990
3991/*
3992 * return TRUE if cookie indicates a function which "abort"s on errors.
3993 */
3994 int
3995func_has_abort(
3996 void *cookie)
3997{
3998 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
3999}
4000
4001
4002/*
4003 * Turn "dict.Func" into a partial for "Func" bound to "dict".
4004 * Don't do this when "Func" is already a partial that was bound
4005 * explicitly (pt_auto is FALSE).
4006 * Changes "rettv" in-place.
4007 * Returns the updated "selfdict_in".
4008 */
4009 dict_T *
4010make_partial(dict_T *selfdict_in, typval_T *rettv)
4011{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004012 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004013 char_u *tofree = NULL;
4014 ufunc_T *fp;
4015 char_u fname_buf[FLEN_FIXED + 1];
4016 int error;
4017 dict_T *selfdict = selfdict_in;
4018
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004019 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
4020 fp = rettv->vval.v_partial->pt_func;
4021 else
4022 {
4023 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
4024 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004025 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004026 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004027 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004028 vim_free(tofree);
4029 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004030
4031 if (fp != NULL && (fp->uf_flags & FC_DICT))
4032 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004033 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004034
4035 if (pt != NULL)
4036 {
4037 pt->pt_refcount = 1;
4038 pt->pt_dict = selfdict;
4039 pt->pt_auto = TRUE;
4040 selfdict = NULL;
4041 if (rettv->v_type == VAR_FUNC)
4042 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004043 // Just a function: Take over the function name and use
4044 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004045 pt->pt_name = rettv->vval.v_string;
4046 }
4047 else
4048 {
4049 partial_T *ret_pt = rettv->vval.v_partial;
4050 int i;
4051
Bram Moolenaare38eab22019-12-05 21:50:01 +01004052 // Partial: copy the function name, use selfdict and copy
4053 // args. Can't take over name or args, the partial might
4054 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004055 if (ret_pt->pt_name != NULL)
4056 {
4057 pt->pt_name = vim_strsave(ret_pt->pt_name);
4058 func_ref(pt->pt_name);
4059 }
4060 else
4061 {
4062 pt->pt_func = ret_pt->pt_func;
4063 func_ptr_ref(pt->pt_func);
4064 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004065 if (ret_pt->pt_argc > 0)
4066 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004067 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004068 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004069 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004070 pt->pt_argc = 0;
4071 else
4072 {
4073 pt->pt_argc = ret_pt->pt_argc;
4074 for (i = 0; i < pt->pt_argc; i++)
4075 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
4076 }
4077 }
4078 partial_unref(ret_pt);
4079 }
4080 rettv->v_type = VAR_PARTIAL;
4081 rettv->vval.v_partial = pt;
4082 }
4083 }
4084 return selfdict;
4085}
4086
4087/*
4088 * Return the name of the executed function.
4089 */
4090 char_u *
4091func_name(void *cookie)
4092{
4093 return ((funccall_T *)cookie)->func->uf_name;
4094}
4095
4096/*
4097 * Return the address holding the next breakpoint line for a funccall cookie.
4098 */
4099 linenr_T *
4100func_breakpoint(void *cookie)
4101{
4102 return &((funccall_T *)cookie)->breakpoint;
4103}
4104
4105/*
4106 * Return the address holding the debug tick for a funccall cookie.
4107 */
4108 int *
4109func_dbg_tick(void *cookie)
4110{
4111 return &((funccall_T *)cookie)->dbg_tick;
4112}
4113
4114/*
4115 * Return the nesting level for a funccall cookie.
4116 */
4117 int
4118func_level(void *cookie)
4119{
4120 return ((funccall_T *)cookie)->level;
4121}
4122
4123/*
4124 * Return TRUE when a function was ended by a ":return" command.
4125 */
4126 int
4127current_func_returned(void)
4128{
4129 return current_funccal->returned;
4130}
4131
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004132 int
4133free_unref_funccal(int copyID, int testing)
4134{
4135 int did_free = FALSE;
4136 int did_free_funccal = FALSE;
4137 funccall_T *fc, **pfc;
4138
4139 for (pfc = &previous_funccal; *pfc != NULL; )
4140 {
4141 if (can_free_funccal(*pfc, copyID))
4142 {
4143 fc = *pfc;
4144 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004145 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004146 did_free = TRUE;
4147 did_free_funccal = TRUE;
4148 }
4149 else
4150 pfc = &(*pfc)->caller;
4151 }
4152 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004153 // When a funccal was freed some more items might be garbage
4154 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004155 (void)garbage_collect(testing);
4156
4157 return did_free;
4158}
4159
4160/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004161 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004162 */
4163 static funccall_T *
4164get_funccal(void)
4165{
4166 int i;
4167 funccall_T *funccal;
4168 funccall_T *temp_funccal;
4169
4170 funccal = current_funccal;
4171 if (debug_backtrace_level > 0)
4172 {
4173 for (i = 0; i < debug_backtrace_level; i++)
4174 {
4175 temp_funccal = funccal->caller;
4176 if (temp_funccal)
4177 funccal = temp_funccal;
4178 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004179 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004180 debug_backtrace_level = i;
4181 }
4182 }
4183 return funccal;
4184}
4185
4186/*
4187 * Return the hashtable used for local variables in the current funccal.
4188 * Return NULL if there is no current funccal.
4189 */
4190 hashtab_T *
4191get_funccal_local_ht()
4192{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004194 return NULL;
4195 return &get_funccal()->l_vars.dv_hashtab;
4196}
4197
4198/*
4199 * Return the l: scope variable.
4200 * Return NULL if there is no current funccal.
4201 */
4202 dictitem_T *
4203get_funccal_local_var()
4204{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004206 return NULL;
4207 return &get_funccal()->l_vars_var;
4208}
4209
4210/*
4211 * Return the hashtable used for argument in the current funccal.
4212 * Return NULL if there is no current funccal.
4213 */
4214 hashtab_T *
4215get_funccal_args_ht()
4216{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004218 return NULL;
4219 return &get_funccal()->l_avars.dv_hashtab;
4220}
4221
4222/*
4223 * Return the a: scope variable.
4224 * Return NULL if there is no current funccal.
4225 */
4226 dictitem_T *
4227get_funccal_args_var()
4228{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004230 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004231 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004232}
4233
4234/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004235 * List function variables, if there is a function.
4236 */
4237 void
4238list_func_vars(int *first)
4239{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004241 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004242 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004243}
4244
4245/*
4246 * If "ht" is the hashtable for local variables in the current funccal, return
4247 * the dict that contains it.
4248 * Otherwise return NULL.
4249 */
4250 dict_T *
4251get_current_funccal_dict(hashtab_T *ht)
4252{
4253 if (current_funccal != NULL
4254 && ht == &current_funccal->l_vars.dv_hashtab)
4255 return &current_funccal->l_vars;
4256 return NULL;
4257}
4258
4259/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004260 * Search hashitem in parent scope.
4261 */
4262 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004263find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004264{
4265 funccall_T *old_current_funccal = current_funccal;
4266 hashtab_T *ht;
4267 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004268 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004269
4270 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4271 return NULL;
4272
Bram Moolenaare38eab22019-12-05 21:50:01 +01004273 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004274 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004275 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004276 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004277 ht = find_var_ht(name, &varname);
4278 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004279 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004280 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004281 if (!HASHITEM_EMPTY(hi))
4282 {
4283 *pht = ht;
4284 break;
4285 }
4286 }
4287 if (current_funccal == current_funccal->func->uf_scoped)
4288 break;
4289 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004290 }
4291 current_funccal = old_current_funccal;
4292
4293 return hi;
4294}
4295
4296/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004297 * Search variable in parent scope.
4298 */
4299 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004300find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004301{
4302 dictitem_T *v = NULL;
4303 funccall_T *old_current_funccal = current_funccal;
4304 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004305 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004306
4307 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4308 return NULL;
4309
Bram Moolenaare38eab22019-12-05 21:50:01 +01004310 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004311 current_funccal = current_funccal->func->uf_scoped;
4312 while (current_funccal)
4313 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004314 ht = find_var_ht(name, &varname);
4315 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004316 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004317 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004318 if (v != NULL)
4319 break;
4320 }
4321 if (current_funccal == current_funccal->func->uf_scoped)
4322 break;
4323 current_funccal = current_funccal->func->uf_scoped;
4324 }
4325 current_funccal = old_current_funccal;
4326
4327 return v;
4328}
4329
4330/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004331 * Set "copyID + 1" in previous_funccal and callers.
4332 */
4333 int
4334set_ref_in_previous_funccal(int copyID)
4335{
4336 int abort = FALSE;
4337 funccall_T *fc;
4338
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004339 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004340 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004341 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004342 abort = abort
4343 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4344 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004345 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004346 }
4347 return abort;
4348}
4349
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004350 static int
4351set_ref_in_funccal(funccall_T *fc, int copyID)
4352{
4353 int abort = FALSE;
4354
4355 if (fc->fc_copyID != copyID)
4356 {
4357 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004358 abort = abort
4359 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4360 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004361 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004362 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004363 }
4364 return abort;
4365}
4366
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004367/*
4368 * Set "copyID" in all local vars and arguments in the call stack.
4369 */
4370 int
4371set_ref_in_call_stack(int copyID)
4372{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004373 int abort = FALSE;
4374 funccall_T *fc;
4375 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004376
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004377 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004378 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004379
4380 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004381 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
4382 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004383 abort = abort || set_ref_in_funccal(fc, copyID);
4384
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004385 return abort;
4386}
4387
4388/*
4389 * Set "copyID" in all functions available by name.
4390 */
4391 int
4392set_ref_in_functions(int copyID)
4393{
4394 int todo;
4395 hashitem_T *hi = NULL;
4396 int abort = FALSE;
4397 ufunc_T *fp;
4398
4399 todo = (int)func_hashtab.ht_used;
4400 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004401 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004402 if (!HASHITEM_EMPTY(hi))
4403 {
4404 --todo;
4405 fp = HI2UF(hi);
4406 if (!func_name_refcount(fp->uf_name))
4407 abort = abort || set_ref_in_func(NULL, fp, copyID);
4408 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004409 }
4410 return abort;
4411}
4412
4413/*
4414 * Set "copyID" in all function arguments.
4415 */
4416 int
4417set_ref_in_func_args(int copyID)
4418{
4419 int i;
4420 int abort = FALSE;
4421
4422 for (i = 0; i < funcargs.ga_len; ++i)
4423 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
4424 copyID, NULL, NULL);
4425 return abort;
4426}
4427
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004428/*
4429 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004430 * Returns TRUE if setting references failed somehow.
4431 */
4432 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004433set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004434{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004435 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004436 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01004437 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004438 char_u fname_buf[FLEN_FIXED + 1];
4439 char_u *tofree = NULL;
4440 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004441 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004442
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004443 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004444 return FALSE;
4445
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004446 if (fp_in == NULL)
4447 {
4448 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004449 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004450 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004451 if (fp != NULL)
4452 {
4453 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004454 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004455 }
4456 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004457 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004458}
4459
Bram Moolenaare38eab22019-12-05 21:50:01 +01004460#endif // FEAT_EVAL