blob: 4da1dc03863bdfba85fa79e14c0b9c2dba900343 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020017/*
18 * All user-defined functions are found in this hashtable.
19 */
20static hashtab_T func_hashtab;
21
Bram Moolenaare38eab22019-12-05 21:50:01 +010022// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020023static garray_T funcargs = GA_EMPTY;
24
Bram Moolenaar209b8e32019-03-14 13:43:24 +010025// pointer to funccal for currently active function
26static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaar209b8e32019-03-14 13:43:24 +010028// Pointer to list of previously used funccal, still around because some
29// item in it is still being used.
30static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020031
32static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
33static char *e_funcdict = N_("E717: Dictionary entry already exists");
34static char *e_funcref = N_("E718: Funcref required");
35static char *e_nofunc = N_("E130: Unknown function: %s");
36
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020037static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020038
39 void
40func_init()
41{
42 hash_init(&func_hashtab);
43}
44
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020045/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020046 * Return the function hash table
47 */
48 hashtab_T *
49func_tbl_get(void)
50{
51 return &func_hashtab;
52}
53
54/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020055 * Get one function argument.
56 * If "argtypes" is not NULL also get the type: "arg: type".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010057 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010058 * Return a pointer to after the type.
59 * When something is wrong return "arg".
60 */
61 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010062one_function_arg(
63 char_u *arg,
64 garray_T *newargs,
65 garray_T *argtypes,
66 int types_optional,
67 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010068{
Bram Moolenaar6e949782020-04-13 17:21:00 +020069 char_u *p = arg;
70 char_u *arg_copy = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010071
72 while (ASCII_ISALNUM(*p) || *p == '_')
73 ++p;
74 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020075 || (argtypes == NULL
76 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
77 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010078 {
79 if (!skip)
80 semsg(_("E125: Illegal argument: %s"), arg);
81 return arg;
82 }
83 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
84 return arg;
85 if (newargs != NULL)
86 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087 int c;
88 int i;
89
90 c = *p;
91 *p = NUL;
92 arg_copy = vim_strsave(arg);
93 if (arg_copy == NULL)
94 {
95 *p = c;
96 return arg;
97 }
98
99 // Check for duplicate argument name.
100 for (i = 0; i < newargs->ga_len; ++i)
101 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
102 {
103 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
104 vim_free(arg_copy);
105 return arg;
106 }
107 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
108 newargs->ga_len++;
109
110 *p = c;
111 }
112
113 // get any type from "arg: type"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100114 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115 {
116 char_u *type = NULL;
117
Bram Moolenaar6e949782020-04-13 17:21:00 +0200118 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
119 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200120 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200121 arg_copy == NULL ? arg : arg_copy);
122 p = skipwhite(p);
123 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 if (*p == ':')
125 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200126 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100127 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200128 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200129 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200130 return arg;
131 }
132 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200133 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100134 if (!skip)
135 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100136 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100137 else if (*skipwhite(p) != '=' && !types_optional)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200138 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200139 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200140 arg_copy == NULL ? arg : arg_copy);
141 return arg;
142 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100143 if (!skip)
144 {
145 if (type == NULL && types_optional)
146 // lambda arguments default to "any" type
147 type = vim_strsave((char_u *)"any");
148 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
149 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150 }
151
152 return p;
153}
154
155/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200156 * Get function arguments.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100157 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200158 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200160get_function_args(
161 char_u **argp,
162 char_u endchar,
163 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100165 int types_optional, // types optional if "argtypes" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200166 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200167 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200168 int skip,
169 exarg_T *eap,
170 char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200171{
172 int mustend = FALSE;
173 char_u *arg = *argp;
174 char_u *p = arg;
175 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200176 int any_default = FALSE;
177 char_u *expr;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200178 char_u *whitep = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200179
180 if (newargs != NULL)
181 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 if (argtypes != NULL)
183 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200184 if (default_args != NULL)
185 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200186
187 if (varargs != NULL)
188 *varargs = FALSE;
189
190 /*
191 * Isolate the arguments: "arg1, arg2, ...)"
192 */
193 while (*p != endchar)
194 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200195 while (eap != NULL && eap->getline != NULL
196 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200197 {
198 char_u *theline;
199
200 // End of the line, get the next one.
201 theline = eap->getline(':', eap->cookie, 0, TRUE);
202 if (theline == NULL)
203 break;
204 vim_free(*line_to_free);
205 *line_to_free = theline;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200206 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200207 p = skipwhite(theline);
208 }
209
210 if (mustend && *p != endchar)
211 {
212 if (!skip)
213 semsg(_(e_invarg2), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100214 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200215 }
216 if (*p == endchar)
217 break;
218
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200219 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
220 {
221 if (varargs != NULL)
222 *varargs = TRUE;
223 p += 3;
224 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100225
226 if (argtypes != NULL)
227 {
228 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200229 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100231 if (!skip)
232 emsg(_(e_missing_name_after_dots));
233 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234 }
235
236 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100237 p = one_function_arg(p, newargs, argtypes, types_optional,
238 skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 if (p == arg)
240 break;
241 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200242 }
243 else
244 {
245 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100246 p = one_function_arg(p, newargs, argtypes, types_optional, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200248 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200249
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200250 if (*skipwhite(p) == '=' && default_args != NULL)
251 {
252 typval_T rettv;
253
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100254 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200255 any_default = TRUE;
256 p = skipwhite(p) + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200257 whitep = p;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200258 p = skipwhite(p);
259 expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200260 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200261 {
262 if (ga_grow(default_args, 1) == FAIL)
263 goto err_ret;
264
265 // trim trailing whitespace
266 while (p > expr && VIM_ISWHITE(p[-1]))
267 p--;
268 c = *p;
269 *p = NUL;
270 expr = vim_strsave(expr);
271 if (expr == NULL)
272 {
273 *p = c;
274 goto err_ret;
275 }
276 ((char_u **)(default_args->ga_data))
277 [default_args->ga_len] = expr;
278 default_args->ga_len++;
279 *p = c;
280 }
281 else
282 mustend = TRUE;
283 }
284 else if (any_default)
285 {
286 emsg(_("E989: Non-default argument follows default argument"));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200287 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200288 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200289 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200290 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200291 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200292 // Don't give this error when skipping, it makes the "->" not
293 // found in "{k,v -> x}" and give a confusing error.
294 if (!skip && in_vim9script()
295 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
296 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200297 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200298 goto err_ret;
299 }
300 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200301 else
302 mustend = TRUE;
303 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200304 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200305 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200306 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200307
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200308 if (*p != endchar)
309 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100310 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200311
312 *argp = p;
313 return OK;
314
315err_ret:
316 if (newargs != NULL)
317 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200318 if (default_args != NULL)
319 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200320 return FAIL;
321}
322
323/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100324 * Parse the argument types, filling "fp->uf_arg_types".
325 * Return OK or FAIL.
326 */
327 static int
328parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
329{
330 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
331 if (argtypes->ga_len > 0)
332 {
333 // When "varargs" is set the last name/type goes into uf_va_name
334 // and uf_va_type.
335 int len = argtypes->ga_len - (varargs ? 1 : 0);
336
337 if (len > 0)
338 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
339 if (fp->uf_arg_types != NULL)
340 {
341 int i;
342 type_T *type;
343
344 for (i = 0; i < len; ++ i)
345 {
346 char_u *p = ((char_u **)argtypes->ga_data)[i];
347
348 if (p == NULL)
349 // will get the type from the default value
350 type = &t_unknown;
351 else
352 type = parse_type(&p, &fp->uf_type_list);
353 if (type == NULL)
354 return FAIL;
355 fp->uf_arg_types[i] = type;
356 }
357 }
358 if (varargs)
359 {
360 char_u *p;
361
362 // Move the last argument "...name: type" to uf_va_name and
363 // uf_va_type.
364 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
365 [fp->uf_args.ga_len - 1];
366 --fp->uf_args.ga_len;
367 p = ((char_u **)argtypes->ga_data)[len];
368 if (p == NULL)
369 // todo: get type from default value
370 fp->uf_va_type = &t_any;
371 else
372 fp->uf_va_type = parse_type(&p, &fp->uf_type_list);
373 if (fp->uf_va_type == NULL)
374 return FAIL;
375 }
376 }
377 return OK;
378}
379
380/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200381 * Register function "fp" as using "current_funccal" as its scope.
382 */
383 static int
384register_closure(ufunc_T *fp)
385{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200386 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100387 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200388 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200389 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200390 fp->uf_scoped = current_funccal;
391 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200392
Bram Moolenaar58016442016-07-31 18:30:22 +0200393 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
394 return FAIL;
395 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
396 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200397 return OK;
398}
399
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100400 static void
401set_ufunc_name(ufunc_T *fp, char_u *name)
402{
403 STRCPY(fp->uf_name, name);
404
405 if (name[0] == K_SPECIAL)
406 {
407 fp->uf_name_exp = alloc(STRLEN(name) + 3);
408 if (fp->uf_name_exp != NULL)
409 {
410 STRCPY(fp->uf_name_exp, "<SNR>");
411 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
412 }
413 }
414}
415
Bram Moolenaar58016442016-07-31 18:30:22 +0200416/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200417 * Get a name for a lambda. Returned in static memory.
418 */
419 char_u *
420get_lambda_name(void)
421{
422 static char_u name[30];
423 static int lambda_no = 0;
424
425 sprintf((char*)name, "<lambda>%d", ++lambda_no);
426 return name;
427}
428
Bram Moolenaar801ab062020-06-25 19:27:56 +0200429#if defined(FEAT_LUA) || defined(PROTO)
430/*
431 * Registers a native C callback which can be called from Vim script.
432 * Returns the name of the Vim script function.
433 */
434 char_u *
435register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
436{
437 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200438 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200439
440 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
441 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200442 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200443
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200444 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200445 fp->uf_refcount = 1;
446 fp->uf_varargs = TRUE;
447 fp->uf_flags = FC_CFUNC;
448 fp->uf_calls = 0;
449 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200450 fp->uf_cb = cb;
451 fp->uf_cb_free = cb_free;
452 fp->uf_cb_state = state;
453
454 set_ufunc_name(fp, name);
455 hash_add(&func_hashtab, UF2HIKEY(fp));
456
457 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200458}
459#endif
460
Bram Moolenaar04b12692020-05-04 23:24:44 +0200461/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100462 * Skip over "->" or "=>" after the arguments of a lambda.
463 * Return NULL if no valid arrow found.
464 */
465 static char_u *
466skip_arrow(char_u *start, int equal_arrow)
467{
468 char_u *s = start;
469
470 if (equal_arrow)
471 {
472 if (*s == ':')
473 s = skip_type(skipwhite(s + 1), TRUE);
474 s = skipwhite(s);
475 if (*s != '=')
476 return NULL;
477 ++s;
478 }
479 if (*s != '>')
480 return NULL;
481 return skipwhite(s + 1);
482}
483
484/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200485 * Parse a lambda expression and get a Funcref from "*arg".
Bram Moolenaar65c44152020-12-24 15:14:01 +0100486 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100487 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200488 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
489 */
490 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100491get_lambda_tv(
492 char_u **arg,
493 typval_T *rettv,
494 int types_optional,
495 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200496{
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200497 int evaluate = evalarg != NULL
498 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200499 garray_T newargs;
500 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200501 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100502 garray_T argtypes;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200503 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100504 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200505 int varargs;
506 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200507 char_u *s;
508 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200509 int *old_eval_lavars = eval_lavars_used;
510 int eval_lavars = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200511 char_u *tofree = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100512 int equal_arrow = **arg == '(';
513
514 if (equal_arrow && !in_vim9script())
515 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200516
517 ga_init(&newargs);
518 ga_init(&newlines);
519
Bram Moolenaar65c44152020-12-24 15:14:01 +0100520 // First, check if this is a lambda expression. "->" or "=>" must exist.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200521 s = skipwhite(*arg + 1);
Bram Moolenaar65c44152020-12-24 15:14:01 +0100522 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100523 types_optional ? &argtypes : NULL, types_optional,
524 NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar65c44152020-12-24 15:14:01 +0100525 if (ret == FAIL || skip_arrow(s, equal_arrow) == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200526 return NOTDONE;
527
Bram Moolenaare38eab22019-12-05 21:50:01 +0100528 // Parse the arguments again.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200529 if (evaluate)
530 pnewargs = &newargs;
531 else
532 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200533 *arg = skipwhite(*arg + 1);
Bram Moolenaar65c44152020-12-24 15:14:01 +0100534 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100535 types_optional ? &argtypes : NULL, types_optional,
536 &varargs, NULL, FALSE, NULL, NULL);
Bram Moolenaar65c44152020-12-24 15:14:01 +0100537 if (ret == FAIL || (*arg = skip_arrow(*arg, equal_arrow)) == NULL)
538 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200539
Bram Moolenaare38eab22019-12-05 21:50:01 +0100540 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200541 if (evaluate)
542 eval_lavars_used = &eval_lavars;
543
Bram Moolenaar65c44152020-12-24 15:14:01 +0100544 *arg = skipwhite_and_linebreak(*arg, evalarg);
545
546 // Only recognize "{" as the start of a function body when followed by
547 // white space, "{key: val}" is a dict.
548 if (equal_arrow && **arg == '{' && IS_WHITE_OR_NUL((*arg)[1]))
549 {
550 // TODO: process the function body upto the "}".
551 emsg("Lambda function body not supported yet");
552 goto errret;
553 }
554
Bram Moolenaare38eab22019-12-05 21:50:01 +0100555 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200556 start = *arg;
557 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200558 if (ret == FAIL)
559 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200560 if (evalarg != NULL)
561 {
562 // avoid that the expression gets freed when another line break follows
563 tofree = evalarg->eval_tofree;
564 evalarg->eval_tofree = NULL;
565 }
566
Bram Moolenaar65c44152020-12-24 15:14:01 +0100567 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +0100568 {
Bram Moolenaar65c44152020-12-24 15:14:01 +0100569 *arg = skipwhite_and_linebreak(*arg, evalarg);
570 if (**arg != '}')
571 {
572 semsg(_("E451: Expected }: %s"), *arg);
573 goto errret;
574 }
575 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100576 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200577
578 if (evaluate)
579 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200580 int len;
581 int flags = 0;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200582 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200583 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200584
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200585 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200586 if (fp == NULL)
587 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200588 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200589 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200590 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200591 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200592
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200593 ga_init2(&newlines, (int)sizeof(char_u *), 1);
594 if (ga_grow(&newlines, 1) == FAIL)
595 goto errret;
596
Bram Moolenaare38eab22019-12-05 21:50:01 +0100597 // Add "return " before the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200598 len = 7 + (int)(end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200599 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200600 if (p == NULL)
601 goto errret;
602 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
603 STRCPY(p, "return ");
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200604 vim_strncpy(p + 7, start, end - start);
Bram Moolenaarf10806b2020-04-02 18:34:35 +0200605 if (strstr((char *)p + 7, "a:") == NULL)
606 // No a: variables are used for sure.
607 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200608
609 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100610 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200611 hash_add(&func_hashtab, UF2HIKEY(fp));
612 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200613 ga_init(&fp->uf_def_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100614 if (types_optional
615 && parse_argument_types(fp, &argtypes, FALSE) == FAIL)
616 goto errret;
617
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200618 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200619 if (current_funccal != NULL && eval_lavars)
620 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200621 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200622 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200623 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200624 }
625 else
626 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200627
628#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200629 if (prof_def_func())
630 func_do_profile(fp);
631#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200632 if (sandbox)
633 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200635 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200636 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200637 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200638 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100639 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200640
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200641 pt->pt_func = fp;
642 pt->pt_refcount = 1;
643 rettv->vval.v_partial = pt;
644 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200645 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200646
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200647 eval_lavars_used = old_eval_lavars;
Bram Moolenaarefaaaa62020-07-08 22:24:09 +0200648 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200649 evalarg->eval_tofree = tofree;
650 else
651 vim_free(tofree);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100652 if (types_optional)
653 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200654 return OK;
655
656errret:
657 ga_clear_strings(&newargs);
658 ga_clear_strings(&newlines);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100659 if (types_optional)
660 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200661 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100662 vim_free(pt);
Bram Moolenaarefaaaa62020-07-08 22:24:09 +0200663 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200664 evalarg->eval_tofree = tofree;
665 else
666 vim_free(tofree);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200667 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200668 return FAIL;
669}
670
671/*
672 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
673 * name it contains, otherwise return "name".
674 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
675 * "partialp".
676 */
677 char_u *
678deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
679{
680 dictitem_T *v;
681 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200682 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200683
684 if (partialp != NULL)
685 *partialp = NULL;
686
687 cc = name[*lenp];
688 name[*lenp] = NUL;
689 v = find_var(name, NULL, no_autoload);
690 name[*lenp] = cc;
691 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
692 {
693 if (v->di_tv.vval.v_string == NULL)
694 {
695 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100696 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200697 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200698 s = v->di_tv.vval.v_string;
699 *lenp = (int)STRLEN(s);
700 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200701 }
702
703 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
704 {
705 partial_T *pt = v->di_tv.vval.v_partial;
706
707 if (pt == NULL)
708 {
709 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100710 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200711 }
712 if (partialp != NULL)
713 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200714 s = partial_name(pt);
715 *lenp = (int)STRLEN(s);
716 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200717 }
718
719 return name;
720}
721
722/*
723 * Give an error message with a function name. Handle <SNR> things.
724 * "ermsg" is to be passed without translation, use N_() instead of _().
725 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100726 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200727emsg_funcname(char *ermsg, char_u *name)
728{
729 char_u *p;
730
731 if (*name == K_SPECIAL)
732 p = concat_str((char_u *)"<SNR>", name + 3);
733 else
734 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100735 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200736 if (p != name)
737 vim_free(p);
738}
739
740/*
741 * Allocate a variable for the result of a function.
742 * Return OK or FAIL.
743 */
744 int
745get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200746 char_u *name, // name of the function
747 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200748 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200749 char_u **arg, // argument, pointing to the '('
Bram Moolenaare6b53242020-07-01 17:28:33 +0200750 evalarg_T *evalarg, // for line continuation
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200751 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200752{
753 char_u *argp;
754 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100755 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
756 int argcount = 0; // number of arguments found
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200757 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200758
759 /*
760 * Get the arguments.
761 */
762 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200763 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
764 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200765 {
Bram Moolenaare6b53242020-07-01 17:28:33 +0200766 // skip the '(' or ',' and possibly line breaks
767 argp = skipwhite_and_linebreak(argp + 1, evalarg);
768
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200769 if (*argp == ')' || *argp == ',' || *argp == NUL)
770 break;
Bram Moolenaare6b53242020-07-01 17:28:33 +0200771 if (eval1(&argp, &argvars[argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200772 {
773 ret = FAIL;
774 break;
775 }
776 ++argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +0200777 // The comma should come right after the argument, but this wasn't
778 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200779 if (vim9script)
780 {
781 if (*argp != ',' && *skipwhite(argp) == ',')
782 {
783 semsg(_(e_no_white_space_allowed_before_str), ",");
784 ret = FAIL;
785 break;
786 }
787 }
788 else
Bram Moolenaar9d489562020-07-30 20:08:50 +0200789 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200790 if (*argp != ',')
791 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200792 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
793 {
794 semsg(_(e_white_space_required_after_str), ",");
795 ret = FAIL;
796 break;
797 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200798 }
Bram Moolenaare6b53242020-07-01 17:28:33 +0200799 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200800 if (*argp == ')')
801 ++argp;
802 else
803 ret = FAIL;
804
805 if (ret == OK)
806 {
807 int i = 0;
808
809 if (get_vim_var_nr(VV_TESTING))
810 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100811 // Prepare for calling test_garbagecollect_now(), need to know
812 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200813 if (funcargs.ga_itemsize == 0)
814 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
815 for (i = 0; i < argcount; ++i)
816 if (ga_grow(&funcargs, 1) == OK)
817 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
818 &argvars[i];
819 }
820
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200821 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200822
823 funcargs.ga_len -= i;
824 }
825 else if (!aborting())
826 {
827 if (argcount == MAX_FUNC_ARGS)
828 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
829 else
830 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
831 }
832
833 while (--argcount >= 0)
834 clear_tv(&argvars[argcount]);
835
Bram Moolenaar8294d492020-08-10 22:40:56 +0200836 if (in_vim9script())
837 *arg = argp;
838 else
839 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200840 return ret;
841}
842
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200843/*
844 * Return TRUE if "p" starts with "<SID>" or "s:".
845 * Only works if eval_fname_script() returned non-zero for "p"!
846 */
847 static int
848eval_fname_sid(char_u *p)
849{
850 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
851}
852
853/*
854 * In a script change <SID>name() and s:name() to K_SNR 123_name().
855 * Change <SNR>123_name() to K_SNR 123_name().
856 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
857 * (slow).
858 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100859 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200860fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
861{
862 int llen;
863 char_u *fname;
864 int i;
865
866 llen = eval_fname_script(name);
867 if (llen > 0)
868 {
869 fname_buf[0] = K_SPECIAL;
870 fname_buf[1] = KS_EXTRA;
871 fname_buf[2] = (int)KE_SNR;
872 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100873 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200874 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200875 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100876 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200877 else
878 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200879 sprintf((char *)fname_buf + 3, "%ld_",
880 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200881 i = (int)STRLEN(fname_buf);
882 }
883 }
884 if (i + STRLEN(name + llen) < FLEN_FIXED)
885 {
886 STRCPY(fname_buf + i, name + llen);
887 fname = fname_buf;
888 }
889 else
890 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200891 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200892 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100893 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200894 else
895 {
896 *tofree = fname;
897 mch_memmove(fname, fname_buf, (size_t)i);
898 STRCPY(fname + i, name + llen);
899 }
900 }
901 }
902 else
903 fname = name;
904 return fname;
905}
906
907/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908 * Find a function "name" in script "sid".
909 */
910 static ufunc_T *
911find_func_with_sid(char_u *name, int sid)
912{
913 hashitem_T *hi;
914 char_u buffer[200];
915
916 buffer[0] = K_SPECIAL;
917 buffer[1] = KS_EXTRA;
918 buffer[2] = (int)KE_SNR;
919 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
920 (long)sid, name);
921 hi = hash_find(&func_hashtab, buffer);
922 if (!HASHITEM_EMPTY(hi))
923 return HI2UF(hi);
924
925 return NULL;
926}
927
928/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200929 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200930 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200931 * Return NULL for unknown function.
932 */
Bram Moolenaarce658352020-07-31 23:47:12 +0200933 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200934find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200935{
936 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100937 ufunc_T *func;
938 imported_T *imported;
939
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200940 if (!is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100941 {
Bram Moolenaar333894b2020-08-01 18:53:07 +0200942 char_u *after_script = NULL;
Bram Moolenaarefa94442020-08-08 22:16:00 +0200943 long sid = 0;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200944 int find_script_local = in_vim9script()
945 && eval_isnamec1(*name) && name[1] != ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100946
Bram Moolenaar95006e32020-08-29 17:47:08 +0200947 if (find_script_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200949 // Find script-local function before global one.
950 func = find_func_with_sid(name, current_sctx.sc_sid);
951 if (func != NULL)
952 return func;
953 }
954
Bram Moolenaarefa94442020-08-08 22:16:00 +0200955 if (name[0] == K_SPECIAL
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200956 && name[1] == KS_EXTRA
957 && name[2] == KE_SNR)
958 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200959 // Caller changes s: to <SNR>99_name.
960
961 after_script = name + 3;
962 sid = getdigits(&after_script);
Bram Moolenaarefa94442020-08-08 22:16:00 +0200963 if (*after_script == '_')
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200964 ++after_script;
965 else
966 after_script = NULL;
967 }
Bram Moolenaar95006e32020-08-29 17:47:08 +0200968 if (find_script_local || after_script != NULL)
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200969 {
970 // Find imported function before global one.
Bram Moolenaarefa94442020-08-08 22:16:00 +0200971 if (after_script != NULL && sid != current_sctx.sc_sid)
972 imported = find_imported_in_script(after_script, 0, sid);
973 else
974 imported = find_imported(after_script == NULL
975 ? name : after_script, 0, cctx);
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200976 if (imported != NULL && imported->imp_funcname != NULL)
977 {
978 hi = hash_find(&func_hashtab, imported->imp_funcname);
979 if (!HASHITEM_EMPTY(hi))
980 return HI2UF(hi);
981 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982 }
983 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200984
Bram Moolenaara26b9702020-04-18 19:53:28 +0200985 hi = hash_find(&func_hashtab,
986 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200987 if (!HASHITEM_EMPTY(hi))
988 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100989
990 return NULL;
991}
992
993/*
994 * Find a function by name, return pointer to it in ufuncs.
995 * "cctx" is passed in a :def function to find imported functions.
996 * Return NULL for unknown or dead function.
997 */
998 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200999find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001000{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001001 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002
1003 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
1004 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001005 return NULL;
1006}
1007
1008/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02001009 * Return TRUE if "ufunc" is a global function.
1010 */
1011 int
1012func_is_global(ufunc_T *ufunc)
1013{
1014 return ufunc->uf_name[0] != K_SPECIAL;
1015}
1016
1017/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001018 * Copy the function name of "fp" to buffer "buf".
1019 * "buf" must be able to hold the function name plus three bytes.
1020 * Takes care of script-local function names.
1021 */
1022 static void
1023cat_func_name(char_u *buf, ufunc_T *fp)
1024{
Bram Moolenaar0f769812020-09-12 18:32:34 +02001025 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001026 {
1027 STRCPY(buf, "<SNR>");
1028 STRCAT(buf, fp->uf_name + 3);
1029 }
1030 else
1031 STRCPY(buf, fp->uf_name);
1032}
1033
1034/*
1035 * Add a number variable "name" to dict "dp" with value "nr".
1036 */
1037 static void
1038add_nr_var(
1039 dict_T *dp,
1040 dictitem_T *v,
1041 char *name,
1042 varnumber_T nr)
1043{
1044 STRCPY(v->di_key, name);
1045 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1046 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
1047 v->di_tv.v_type = VAR_NUMBER;
1048 v->di_tv.v_lock = VAR_FIXED;
1049 v->di_tv.vval.v_number = nr;
1050}
1051
1052/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001053 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001054 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001055 static void
1056free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001057{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001058 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001059
1060 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1061 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001062 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001063
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001064 // When garbage collecting a funccall_T may be freed before the
1065 // function that references it, clear its uf_scoped field.
1066 // The function may have been redefined and point to another
1067 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001068 if (fp != NULL && fp->uf_scoped == fc)
1069 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001070 }
Bram Moolenaar58016442016-07-31 18:30:22 +02001071 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001072
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001073 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001074 vim_free(fc);
1075}
1076
1077/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001078 * Free "fc" and what it contains.
1079 * Can be called only when "fc" is kept beyond the period of it called,
1080 * i.e. after cleanup_function_call(fc).
1081 */
1082 static void
1083free_funccal_contents(funccall_T *fc)
1084{
1085 listitem_T *li;
1086
1087 // Free all l: variables.
1088 vars_clear(&fc->l_vars.dv_hashtab);
1089
1090 // Free all a: variables.
1091 vars_clear(&fc->l_avars.dv_hashtab);
1092
1093 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001094 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001095 clear_tv(&li->li_tv);
1096
1097 free_funccal(fc);
1098}
1099
1100/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02001101 * Handle the last part of returning from a function: free the local hashtable.
1102 * Unless it is still in use by a closure.
1103 */
1104 static void
1105cleanup_function_call(funccall_T *fc)
1106{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001107 int may_free_fc = fc->fc_refcount <= 0;
1108 int free_fc = TRUE;
1109
Bram Moolenaar6914c642017-04-01 21:21:30 +02001110 current_funccal = fc->caller;
1111
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001112 // Free all l: variables if not referred.
1113 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
1114 vars_clear(&fc->l_vars.dv_hashtab);
1115 else
1116 free_fc = FALSE;
1117
1118 // If the a:000 list and the l: and a: dicts are not referenced and
1119 // there is no closure using it, we can free the funccall_T and what's
1120 // in it.
1121 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
1122 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001123 else
1124 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001125 int todo;
1126 hashitem_T *hi;
1127 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001128
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001129 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001130
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001131 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001132 todo = (int)fc->l_avars.dv_hashtab.ht_used;
1133 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
1134 {
1135 if (!HASHITEM_EMPTY(hi))
1136 {
1137 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001138 di = HI2DI(hi);
1139 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001140 }
1141 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001142 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001143
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001144 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
1145 fc->l_varlist.lv_first = NULL;
1146 else
1147 {
1148 listitem_T *li;
1149
1150 free_fc = FALSE;
1151
1152 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001153 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02001154 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001155 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001156
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001157 if (free_fc)
1158 free_funccal(fc);
1159 else
1160 {
1161 static int made_copy = 0;
1162
1163 // "fc" is still in use. This can happen when returning "a:000",
1164 // assigning "l:" to a global variable or defining a closure.
1165 // Link "fc" in the list for garbage collection later.
1166 fc->caller = previous_funccal;
1167 previous_funccal = fc;
1168
1169 if (want_garbage_collect)
1170 // If garbage collector is ready, clear count.
1171 made_copy = 0;
1172 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001173 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001174 // We have made a lot of copies, worth 4 Mbyte. This can happen
1175 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001176 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001177 // too much memory.
1178 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001179 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001180 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001181 }
1182}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001183
1184/*
1185 * There are two kinds of function names:
1186 * 1. ordinary names, function defined with :function or :def
1187 * 2. numbered functions and lambdas
1188 * For the first we only count the name stored in func_hashtab as a reference,
1189 * using function() does not count as a reference, because the function is
1190 * looked up by name.
1191 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02001192 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001193func_name_refcount(char_u *name)
1194{
1195 return isdigit(*name) || *name == '<';
1196}
1197
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198/*
1199 * Unreference "fc": decrement the reference count and free it when it
1200 * becomes zero. "fp" is detached from "fc".
1201 * When "force" is TRUE we are exiting.
1202 */
1203 static void
1204funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
1205{
1206 funccall_T **pfc;
1207 int i;
1208
1209 if (fc == NULL)
1210 return;
1211
1212 if (--fc->fc_refcount <= 0 && (force || (
1213 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
1214 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
1215 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
1216 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
1217 {
1218 if (fc == *pfc)
1219 {
1220 *pfc = fc->caller;
1221 free_funccal_contents(fc);
1222 return;
1223 }
1224 }
1225 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1226 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
1227 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
1228}
1229
1230/*
1231 * Remove the function from the function hashtable. If the function was
1232 * deleted while it still has references this was already done.
1233 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
1234 */
1235 static int
1236func_remove(ufunc_T *fp)
1237{
1238 hashitem_T *hi;
1239
1240 // Return if it was already virtually deleted.
1241 if (fp->uf_flags & FC_DEAD)
1242 return FALSE;
1243
1244 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1245 if (!HASHITEM_EMPTY(hi))
1246 {
1247 // When there is a def-function index do not actually remove the
1248 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001249 // Do remove it when it's a copy.
1250 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251 fp->uf_flags |= FC_DEAD;
1252 else
1253 hash_remove(&func_hashtab, hi);
1254 return TRUE;
1255 }
1256 return FALSE;
1257}
1258
1259 static void
1260func_clear_items(ufunc_T *fp)
1261{
1262 ga_clear_strings(&(fp->uf_args));
1263 ga_clear_strings(&(fp->uf_def_args));
1264 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001266 VIM_CLEAR(fp->uf_def_arg_idx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001267 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001268 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02001269 clear_type_list(&fp->uf_type_list);
Bram Moolenaarf112f302020-12-20 17:47:52 +01001270 partial_unref(fp->uf_partial);
1271 fp->uf_partial = NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +02001272
1273#ifdef FEAT_LUA
1274 if (fp->uf_cb_free != NULL)
1275 {
1276 fp->uf_cb_free(fp->uf_cb_state);
1277 fp->uf_cb_free = NULL;
1278 }
1279
1280 fp->uf_cb_state = NULL;
1281 fp->uf_cb = NULL;
1282#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283#ifdef FEAT_PROFILE
1284 VIM_CLEAR(fp->uf_tml_count);
1285 VIM_CLEAR(fp->uf_tml_total);
1286 VIM_CLEAR(fp->uf_tml_self);
1287#endif
1288}
1289
1290/*
1291 * Free all things that a function contains. Does not free the function
1292 * itself, use func_free() for that.
1293 * When "force" is TRUE we are exiting.
1294 */
1295 static void
1296func_clear(ufunc_T *fp, int force)
1297{
1298 if (fp->uf_cleared)
1299 return;
1300 fp->uf_cleared = TRUE;
1301
1302 // clear this function
1303 func_clear_items(fp);
1304 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001305 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306}
1307
1308/*
1309 * Free a function and remove it from the list of functions. Does not free
1310 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001311 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02001312 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02001314 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001315func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316{
1317 // Only remove it when not done already, otherwise we would remove a newer
1318 // version of the function with the same name.
1319 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1320 func_remove(fp);
1321
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001322 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001323 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001324 if (fp->uf_dfunc_idx > 0)
1325 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001326 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02001328 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001329 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02001330 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331}
1332
1333/*
1334 * Free all things that a function contains and free the function itself.
1335 * When "force" is TRUE we are exiting.
1336 */
1337 static void
1338func_clear_free(ufunc_T *fp, int force)
1339{
1340 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001341 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
1342 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001343 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02001344 else
1345 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346}
1347
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001348/*
1349 * Copy already defined function "lambda" to a new function with name "global".
1350 * This is for when a compiled function defines a global function.
1351 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001352 int
1353copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001354{
1355 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01001356 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001357
1358 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001359 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001360 semsg(_(e_lambda_function_not_found_str), lambda);
1361 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001362 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001363
1364 // TODO: handle ! to overwrite
1365 fp = find_func(global, TRUE, NULL);
1366 if (fp != NULL)
1367 {
1368 semsg(_(e_funcexts), global);
1369 return FAIL;
1370 }
1371
1372 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
1373 if (fp == NULL)
1374 return FAIL;
1375
1376 fp->uf_varargs = ufunc->uf_varargs;
1377 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
1378 fp->uf_def_status = ufunc->uf_def_status;
1379 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
1380 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
1381 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
1382 == FAIL
1383 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
1384 goto failed;
1385
1386 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
1387 : vim_strsave(ufunc->uf_name_exp);
1388 if (ufunc->uf_arg_types != NULL)
1389 {
1390 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
1391 if (fp->uf_arg_types == NULL)
1392 goto failed;
1393 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
1394 sizeof(type_T *) * fp->uf_args.ga_len);
1395 }
1396 if (ufunc->uf_def_arg_idx != NULL)
1397 {
1398 fp->uf_def_arg_idx = ALLOC_MULT(int, fp->uf_def_args.ga_len + 1);
1399 if (fp->uf_def_arg_idx == NULL)
1400 goto failed;
1401 mch_memmove(fp->uf_def_arg_idx, ufunc->uf_def_arg_idx,
1402 sizeof(int) * fp->uf_def_args.ga_len + 1);
1403 }
1404 if (ufunc->uf_va_name != NULL)
1405 {
1406 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
1407 if (fp->uf_va_name == NULL)
1408 goto failed;
1409 }
1410 fp->uf_ret_type = ufunc->uf_ret_type;
1411
1412 fp->uf_refcount = 1;
1413 STRCPY(fp->uf_name, global);
1414 hash_add(&func_hashtab, UF2HIKEY(fp));
1415
1416 // the referenced dfunc_T is now used one more time
1417 link_def_function(fp);
1418
1419 // Create a partial to store the context of the function, if not done
1420 // already.
1421 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
1422 {
1423 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
1424
1425 if (pt == NULL)
1426 goto failed;
1427 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
1428 goto failed;
1429 ufunc->uf_partial = pt;
1430 --pt->pt_refcount; // not referenced here yet
1431 }
1432 if (ufunc->uf_partial != NULL)
1433 {
1434 fp->uf_partial = ufunc->uf_partial;
1435 ++fp->uf_partial->pt_refcount;
1436 }
1437
1438 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001439
1440failed:
1441 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001442 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001443}
1444
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001445static int funcdepth = 0;
1446
1447/*
1448 * Increment the function call depth count.
1449 * Return FAIL when going over 'maxfuncdepth'.
1450 * Otherwise return OK, must call funcdepth_decrement() later!
1451 */
1452 int
1453funcdepth_increment(void)
1454{
1455 if (funcdepth >= p_mfd)
1456 {
1457 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
1458 return FAIL;
1459 }
1460 ++funcdepth;
1461 return OK;
1462}
1463
1464 void
1465funcdepth_decrement(void)
1466{
1467 --funcdepth;
1468}
1469
1470/*
1471 * Get the current function call depth.
1472 */
1473 int
1474funcdepth_get(void)
1475{
1476 return funcdepth;
1477}
1478
1479/*
1480 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001481 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001482 * times as funcdepth_increment().
1483 */
1484 void
1485funcdepth_restore(int depth)
1486{
1487 funcdepth = depth;
1488}
Bram Moolenaar6914c642017-04-01 21:21:30 +02001489
1490/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001491 * Call a user function.
1492 */
1493 static void
1494call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001495 ufunc_T *fp, // pointer to function
1496 int argcount, // nr of args
1497 typval_T *argvars, // arguments
1498 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001499 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01001500 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001501{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001502 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001503 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001504 funccall_T *fc;
1505 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001506 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001507 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001508 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001509 int i;
1510 int ai;
1511 int islambda = FALSE;
1512 char_u numbuf[NUMBUFLEN];
1513 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001514#ifdef FEAT_PROFILE
1515 proftime_T wait_start;
1516 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001517 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001518#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001519 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001520
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001521 // If depth of calling is getting too high, don't execute the function.
1522 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001523 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001524 rettv->v_type = VAR_NUMBER;
1525 rettv->vval.v_number = -1;
1526 return;
1527 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001528
Bram Moolenaare38eab22019-12-05 21:50:01 +01001529 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001530
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001531 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001532 if (fc == NULL)
1533 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001534 fc->caller = current_funccal;
1535 current_funccal = fc;
1536 fc->func = fp;
1537 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001538 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001539 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001540 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1541 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001542 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001543 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001544 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001545
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001546 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 {
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001548 // Execute the function, possibly compiling it first.
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001549 call_def_function(fp, argcount, argvars, funcexe->partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001550 funcdepth_decrement();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 free_funccal(fc);
1553 return;
1554 }
1555
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001556 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1557 islambda = TRUE;
1558
1559 /*
1560 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1561 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1562 * each argument variable and saves a lot of time.
1563 */
1564 /*
1565 * Init l: variables.
1566 */
1567 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1568 if (selfdict != NULL)
1569 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001570 // Set l:self to "selfdict". Use "name" to avoid a warning from
1571 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001572 v = &fc->fixvar[fixvar_idx++].var;
1573 name = v->di_key;
1574 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001575 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001576 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1577 v->di_tv.v_type = VAR_DICT;
1578 v->di_tv.v_lock = 0;
1579 v->di_tv.vval.v_dict = selfdict;
1580 ++selfdict->dv_refcount;
1581 }
1582
1583 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001584 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001585 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001586 * Set a:000 to a list with room for the "..." arguments.
1587 */
1588 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001589 if ((fp->uf_flags & FC_NOARGS) == 0)
1590 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001591 (varnumber_T)(argcount >= fp->uf_args.ga_len
1592 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001593 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001594 if ((fp->uf_flags & FC_NOARGS) == 0)
1595 {
1596 // Use "name" to avoid a warning from some compiler that checks the
1597 // destination size.
1598 v = &fc->fixvar[fixvar_idx++].var;
1599 name = v->di_key;
1600 STRCPY(name, "000");
1601 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1602 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1603 v->di_tv.v_type = VAR_LIST;
1604 v->di_tv.v_lock = VAR_FIXED;
1605 v->di_tv.vval.v_list = &fc->l_varlist;
1606 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001607 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001608 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1609 fc->l_varlist.lv_lock = VAR_FIXED;
1610
1611 /*
1612 * Set a:firstline to "firstline" and a:lastline to "lastline".
1613 * Set a:name to named arguments.
1614 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001615 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001616 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001617 if ((fp->uf_flags & FC_NOARGS) == 0)
1618 {
1619 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001620 (varnumber_T)funcexe->firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001621 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001622 (varnumber_T)funcexe->lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001623 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001624 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001625 {
1626 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001627 typval_T def_rettv;
1628 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001629
1630 ai = i - fp->uf_args.ga_len;
1631 if (ai < 0)
1632 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001633 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001634 name = FUNCARG(fp, i);
1635 if (islambda)
1636 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001637
1638 // evaluate named argument default expression
1639 isdefault = ai + fp->uf_def_args.ga_len >= 0
1640 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1641 && argvars[i].vval.v_number == VVAL_NONE));
1642 if (isdefault)
1643 {
1644 char_u *default_expr = NULL;
1645 def_rettv.v_type = VAR_NUMBER;
1646 def_rettv.vval.v_number = -1;
1647
1648 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1649 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001650 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001651 {
1652 default_arg_err = 1;
1653 break;
1654 }
1655 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001656 }
1657 else
1658 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001659 if ((fp->uf_flags & FC_NOARGS) != 0)
1660 // Bail out if no a: arguments used (in lambda).
1661 break;
1662
Bram Moolenaare38eab22019-12-05 21:50:01 +01001663 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001664 sprintf((char *)numbuf, "%d", ai + 1);
1665 name = numbuf;
1666 }
1667 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1668 {
1669 v = &fc->fixvar[fixvar_idx++].var;
1670 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001671 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001672 }
1673 else
1674 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001675 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001676 if (v == NULL)
1677 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001678 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001679 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001680
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001681 // Note: the values are copied directly to avoid alloc/free.
1682 // "argvars" must have VAR_FIXED for v_lock.
1683 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001684 v->di_tv.v_lock = VAR_FIXED;
1685
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001686 if (addlocal)
1687 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001688 // Named arguments should be accessed without the "a:" prefix in
1689 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001690 copy_tv(&v->di_tv, &v->di_tv);
1691 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001692 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001693 else
1694 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001695
1696 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1697 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001698 listitem_T *li = &fc->l_listitems[ai];
1699
1700 li->li_tv = argvars[i];
1701 li->li_tv.v_lock = VAR_FIXED;
1702 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001703 }
1704 }
1705
Bram Moolenaare38eab22019-12-05 21:50:01 +01001706 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001707 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001708
1709 if (fp->uf_flags & FC_SANDBOX)
1710 {
1711 using_sandbox = TRUE;
1712 ++sandbox;
1713 }
1714
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001715 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001716 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001717 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001718 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001719 ++no_wait_return;
1720 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001721
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001722 smsg(_("calling %s"), SOURCING_NAME);
1723 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001724 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001725 char_u buf[MSG_BUF_LEN];
1726 char_u numbuf2[NUMBUFLEN];
1727 char_u *tofree;
1728 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001729
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001730 msg_puts("(");
1731 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001732 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001733 if (i > 0)
1734 msg_puts(", ");
1735 if (argvars[i].v_type == VAR_NUMBER)
1736 msg_outnum((long)argvars[i].vval.v_number);
1737 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001738 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001739 // Do not want errors such as E724 here.
1740 ++emsg_off;
1741 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1742 --emsg_off;
1743 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001744 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001745 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001746 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001747 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1748 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001749 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001750 msg_puts((char *)s);
1751 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001752 }
1753 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001754 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001755 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001756 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001757 msg_puts("\n"); // don't overwrite this either
1758
1759 verbose_leave_scroll();
1760 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001761 }
1762#ifdef FEAT_PROFILE
1763 if (do_profiling == PROF_YES)
1764 {
1765 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001766 {
1767 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001768 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001769 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001770 if (fp->uf_profiling
1771 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1772 {
1773 ++fp->uf_tm_count;
1774 profile_start(&call_start);
1775 profile_zero(&fp->uf_tm_children);
1776 }
1777 script_prof_save(&wait_start);
1778 }
1779#endif
1780
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001781 save_current_sctx = current_sctx;
1782 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001783 save_did_emsg = did_emsg;
1784 did_emsg = FALSE;
1785
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001786 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1787 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001788 else if (islambda)
1789 {
1790 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1791
1792 // A Lambda always has the command "return {expr}". It is much faster
1793 // to evaluate {expr} directly.
1794 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001795 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001796 --ex_nesting_level;
1797 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001798 else
1799 // call do_cmdline() to execute the lines
1800 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001801 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1802
1803 --RedrawingDisabled;
1804
Bram Moolenaare38eab22019-12-05 21:50:01 +01001805 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001806 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1807 {
1808 clear_tv(rettv);
1809 rettv->v_type = VAR_NUMBER;
1810 rettv->vval.v_number = -1;
1811 }
1812
1813#ifdef FEAT_PROFILE
1814 if (do_profiling == PROF_YES && (fp->uf_profiling
1815 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1816 {
1817 profile_end(&call_start);
1818 profile_sub_wait(&wait_start, &call_start);
1819 profile_add(&fp->uf_tm_total, &call_start);
1820 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1821 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1822 {
1823 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1824 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1825 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001826 if (started_profiling)
1827 // make a ":profdel func" stop profiling the function
1828 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001829 }
1830#endif
1831
Bram Moolenaare38eab22019-12-05 21:50:01 +01001832 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001833 if (p_verbose >= 12)
1834 {
1835 ++no_wait_return;
1836 verbose_enter_scroll();
1837
1838 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001839 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001840 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001841 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001842 (long)fc->rettv->vval.v_number);
1843 else
1844 {
1845 char_u buf[MSG_BUF_LEN];
1846 char_u numbuf2[NUMBUFLEN];
1847 char_u *tofree;
1848 char_u *s;
1849
Bram Moolenaare38eab22019-12-05 21:50:01 +01001850 // The value may be very long. Skip the middle part, so that we
1851 // have some idea how it starts and ends. smsg() would always
1852 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001853 ++emsg_off;
1854 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1855 --emsg_off;
1856 if (s != NULL)
1857 {
1858 if (vim_strsize(s) > MSG_BUF_CLEN)
1859 {
1860 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1861 s = buf;
1862 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001863 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001864 vim_free(tofree);
1865 }
1866 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001867 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001868
1869 verbose_leave_scroll();
1870 --no_wait_return;
1871 }
1872
Bram Moolenaare31ee862020-01-07 20:59:34 +01001873 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001874 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001875 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001876#ifdef FEAT_PROFILE
1877 if (do_profiling == PROF_YES)
1878 script_prof_restore(&wait_start);
1879#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001880 if (using_sandbox)
1881 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001882
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001883 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001884 {
1885 ++no_wait_return;
1886 verbose_enter_scroll();
1887
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001888 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001889 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001890
1891 verbose_leave_scroll();
1892 --no_wait_return;
1893 }
1894
1895 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001896 funcdepth_decrement();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001897 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001898}
1899
1900/*
Bram Moolenaar50824712020-12-20 21:10:17 +01001901 * Check the argument count for user function "fp".
1902 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
1903 */
1904 int
1905check_user_func_argcount(ufunc_T *fp, int argcount)
1906{
1907 int regular_args = fp->uf_args.ga_len;
1908
1909 if (argcount < regular_args - fp->uf_def_args.ga_len)
1910 return FCERR_TOOFEW;
1911 else if (!has_varargs(fp) && argcount > regular_args)
1912 return FCERR_TOOMANY;
1913 return FCERR_UNKNOWN;
1914}
1915
1916/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001918 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919 int
1920call_user_func_check(
1921 ufunc_T *fp,
1922 int argcount,
1923 typval_T *argvars,
1924 typval_T *rettv,
1925 funcexe_T *funcexe,
1926 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001927{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001929
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001930 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1931 *funcexe->doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01001932 error = check_user_func_argcount(fp, argcount);
1933 if (error != FCERR_UNKNOWN)
1934 return error;
1935 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001936 error = FCERR_DICT;
1937 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001938 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 int did_save_redo = FALSE;
1940 save_redo_T save_redo;
1941
1942 /*
1943 * Call the user function.
1944 * Save and restore search patterns, script variables and
1945 * redo buffer.
1946 */
1947 save_search_patterns();
1948 if (!ins_compl_active())
1949 {
1950 saveRedobuff(&save_redo);
1951 did_save_redo = TRUE;
1952 }
1953 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001954 call_user_func(fp, argcount, argvars, rettv, funcexe,
1955 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1957 // Function was unreferenced while being used, free it now.
1958 func_clear_free(fp, FALSE);
1959 if (did_save_redo)
1960 restoreRedobuff(&save_redo);
1961 restore_search_patterns();
1962 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001963 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001965}
1966
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001967static funccal_entry_T *funccal_stack = NULL;
1968
1969/*
1970 * Save the current function call pointer, and set it to NULL.
1971 * Used when executing autocommands and for ":source".
1972 */
1973 void
1974save_funccal(funccal_entry_T *entry)
1975{
1976 entry->top_funccal = current_funccal;
1977 entry->next = funccal_stack;
1978 funccal_stack = entry;
1979 current_funccal = NULL;
1980}
1981
1982 void
1983restore_funccal(void)
1984{
1985 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001986 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001987 else
1988 {
1989 current_funccal = funccal_stack->top_funccal;
1990 funccal_stack = funccal_stack->next;
1991 }
1992}
1993
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001994 funccall_T *
1995get_current_funccal(void)
1996{
1997 return current_funccal;
1998}
1999
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002000/*
2001 * Mark all functions of script "sid" as deleted.
2002 */
2003 void
2004delete_script_functions(int sid)
2005{
2006 hashitem_T *hi;
2007 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02002008 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002009 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002010 size_t len;
2011
2012 buf[0] = K_SPECIAL;
2013 buf[1] = KS_EXTRA;
2014 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002015 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002016 len = STRLEN(buf);
2017
Bram Moolenaarce658352020-07-31 23:47:12 +02002018 while (todo > 0)
2019 {
2020 todo = func_hashtab.ht_used;
2021 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2022 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002023 {
Bram Moolenaarce658352020-07-31 23:47:12 +02002024 fp = HI2UF(hi);
2025 if (STRNCMP(fp->uf_name, buf, len) == 0)
2026 {
2027 int changed = func_hashtab.ht_changed;
2028
2029 fp->uf_flags |= FC_DEAD;
2030 func_clear(fp, TRUE);
2031 // When clearing a function another function can be cleared
2032 // as a side effect. When that happens start over.
2033 if (changed != func_hashtab.ht_changed)
2034 break;
2035 }
2036 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002037 }
Bram Moolenaarce658352020-07-31 23:47:12 +02002038 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002039}
2040
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002041#if defined(EXITFREE) || defined(PROTO)
2042 void
2043free_all_functions(void)
2044{
2045 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002046 ufunc_T *fp;
2047 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002048 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002049 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002050
Bram Moolenaare38eab22019-12-05 21:50:01 +01002051 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002052 while (current_funccal != NULL)
2053 {
2054 clear_tv(current_funccal->rettv);
2055 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002056 if (current_funccal == NULL && funccal_stack != NULL)
2057 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02002058 }
2059
Bram Moolenaare38eab22019-12-05 21:50:01 +01002060 // First clear what the functions contain. Since this may lower the
2061 // reference count of a function, it may also free a function and change
2062 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002063 while (todo > 0)
2064 {
2065 todo = func_hashtab.ht_used;
2066 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2067 if (!HASHITEM_EMPTY(hi))
2068 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069 // clear the def function index now
2070 fp = HI2UF(hi);
2071 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002072 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073
Bram Moolenaare38eab22019-12-05 21:50:01 +01002074 // Only free functions that are not refcounted, those are
2075 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002076 if (func_name_refcount(fp->uf_name))
2077 ++skipped;
2078 else
2079 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002080 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002081 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002082 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002083 {
2084 skipped = 0;
2085 break;
2086 }
2087 }
2088 --todo;
2089 }
2090 }
2091
Bram Moolenaare38eab22019-12-05 21:50:01 +01002092 // Now actually free the functions. Need to start all over every time,
2093 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002094 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002095 while (func_hashtab.ht_used > skipped)
2096 {
2097 todo = func_hashtab.ht_used;
2098 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002099 if (!HASHITEM_EMPTY(hi))
2100 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02002101 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002102 // Only free functions that are not refcounted, those are
2103 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02002104 fp = HI2UF(hi);
2105 if (func_name_refcount(fp->uf_name))
2106 ++skipped;
2107 else
2108 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02002109 if (func_free(fp, FALSE) == OK)
2110 {
2111 skipped = 0;
2112 break;
2113 }
2114 // did not actually free it
2115 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002116 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002117 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02002118 }
2119 if (skipped == 0)
2120 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121
2122 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002123}
2124#endif
2125
2126/*
2127 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02002128 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002129 * "len" is the length of "name", or -1 for NUL terminated.
2130 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002132builtin_function(char_u *name, int len)
2133{
2134 char_u *p;
2135
Bram Moolenaara26b9702020-04-18 19:53:28 +02002136 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002137 return FALSE;
2138 p = vim_strchr(name, AUTOLOAD_CHAR);
2139 return p == NULL || (len > 0 && p > name + len);
2140}
2141
2142 int
2143func_call(
2144 char_u *name,
2145 typval_T *args,
2146 partial_T *partial,
2147 dict_T *selfdict,
2148 typval_T *rettv)
2149{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002150 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002151 listitem_T *item;
2152 typval_T argv[MAX_FUNC_ARGS + 1];
2153 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002154 int r = 0;
2155
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002156 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002157 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002158 {
2159 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
2160 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002161 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002162 break;
2163 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002164 // Make a copy of each argument. This is needed to be able to set
2165 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002166 copy_tv(&item->li_tv, &argv[argc++]);
2167 }
2168
2169 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002170 {
2171 funcexe_T funcexe;
2172
Bram Moolenaara80faa82020-04-12 19:37:17 +02002173 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002174 funcexe.firstline = curwin->w_cursor.lnum;
2175 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002176 funcexe.evaluate = TRUE;
2177 funcexe.partial = partial;
2178 funcexe.selfdict = selfdict;
2179 r = call_func(name, -1, rettv, argc, argv, &funcexe);
2180 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002181
Bram Moolenaare38eab22019-12-05 21:50:01 +01002182 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002183 while (argc > 0)
2184 clear_tv(&argv[--argc]);
2185
2186 return r;
2187}
2188
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002189static int callback_depth = 0;
2190
2191 int
2192get_callback_depth(void)
2193{
2194 return callback_depth;
2195}
2196
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002197/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002198 * Invoke call_func() with a callback.
2199 */
2200 int
2201call_callback(
2202 callback_T *callback,
2203 int len, // length of "name" or -1 to use strlen()
2204 typval_T *rettv, // return value goes here
2205 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002206 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002207 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002208{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002209 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002210 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002211
Bram Moolenaara80faa82020-04-12 19:37:17 +02002212 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002213 funcexe.evaluate = TRUE;
2214 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002215 ++callback_depth;
2216 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
2217 --callback_depth;
2218 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002219}
2220
2221/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 * Give an error message for the result of a function.
2223 * Nothing if "error" is FCERR_NONE.
2224 */
2225 void
2226user_func_error(int error, char_u *name)
2227{
2228 switch (error)
2229 {
2230 case FCERR_UNKNOWN:
2231 emsg_funcname(e_unknownfunc, name);
2232 break;
2233 case FCERR_NOTMETHOD:
2234 emsg_funcname(
2235 N_("E276: Cannot use function as a method: %s"), name);
2236 break;
2237 case FCERR_DELETED:
2238 emsg_funcname(N_(e_func_deleted), name);
2239 break;
2240 case FCERR_TOOMANY:
2241 emsg_funcname((char *)e_toomanyarg, name);
2242 break;
2243 case FCERR_TOOFEW:
2244 emsg_funcname((char *)e_toofewarg, name);
2245 break;
2246 case FCERR_SCRIPT:
2247 emsg_funcname(
2248 N_("E120: Using <SID> not in a script context: %s"), name);
2249 break;
2250 case FCERR_DICT:
2251 emsg_funcname(
2252 N_("E725: Calling dict function without Dictionary: %s"),
2253 name);
2254 break;
2255 }
2256}
2257
2258/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002259 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002260 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002261 * Return FAIL when the function can't be called, OK otherwise.
2262 * Also returns OK when an error was encountered while executing the function.
2263 */
2264 int
2265call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02002266 char_u *funcname, // name of the function
2267 int len, // length of "name" or -1 to use strlen()
2268 typval_T *rettv, // return value goes here
2269 int argcount_in, // number of "argvars"
2270 typval_T *argvars_in, // vars for arguments, must have "argcount"
2271 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002272 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002273{
2274 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01002275 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002276 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002277 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002278 char_u fname_buf[FLEN_FIXED + 1];
2279 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002280 char_u *fname = NULL;
2281 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002282 int argcount = argcount_in;
2283 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002284 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002285 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
2286 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002287 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002288 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002289 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002290
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02002291 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
2292 // even when call_func() returns FAIL.
2293 rettv->v_type = VAR_UNKNOWN;
2294
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002295 if (partial != NULL)
2296 fp = partial->pt_func;
2297 if (fp == NULL)
2298 {
2299 // Make a copy of the name, if it comes from a funcref variable it
2300 // could be changed or deleted in the called function.
2301 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
2302 if (name == NULL)
2303 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002304
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002305 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
2306 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002307
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002308 if (funcexe->doesrange != NULL)
2309 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002310
2311 if (partial != NULL)
2312 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002313 // When the function has a partial with a dict and there is a dict
2314 // argument, use the dict argument. That is backwards compatible.
2315 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002316 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002317 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01002318 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002319 {
2320 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002321 {
2322 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
2323 {
Bram Moolenaaref140542019-12-31 21:27:13 +01002324 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002325 goto theend;
2326 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002327 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002328 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002329 for (i = 0; i < argcount_in; ++i)
2330 argv[i + argv_clear] = argvars_in[i];
2331 argvars = argv;
2332 argcount = partial->pt_argc + argcount_in;
2333 }
2334 }
2335
Bram Moolenaaref140542019-12-31 21:27:13 +01002336 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002337 {
2338 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02002339 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002340
Bram Moolenaar333894b2020-08-01 18:53:07 +02002341 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002342 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02002343 {
2344 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002345 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02002346 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002347
Bram Moolenaare38eab22019-12-05 21:50:01 +01002348 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002349 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01002350 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002351
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002352 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002353 {
2354 /*
2355 * User defined function.
2356 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002357 if (fp == NULL)
Bram Moolenaar333894b2020-08-01 18:53:07 +02002358 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002359
Bram Moolenaare38eab22019-12-05 21:50:01 +01002360 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002361 if (fp == NULL
2362 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002363 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002364 && !aborting())
2365 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002366 // executed an autocommand, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02002367 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002368 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002369 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002370 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
2371 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002372 // loaded a package, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02002373 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002374 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02002375 if (fp == NULL)
2376 {
2377 char_u *p = untrans_function_name(rfname);
2378
2379 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02002380 // Don't do this if the name starts with "s:".
2381 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaar333894b2020-08-01 18:53:07 +02002382 fp = find_func(p, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002383 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002384
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002385 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01002386 error = FCERR_DELETED;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002387#ifdef FEAT_LUA
2388 else if (fp != NULL && (fp->uf_flags & FC_CFUNC))
2389 {
2390 cfunc_T cb = fp->uf_cb;
2391
2392 error = (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
2393 }
2394#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002395 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002396 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002397 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01002398 // postponed filling in the arguments, do it now
2399 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002400 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002401
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002402 if (funcexe->basetv != NULL)
2403 {
2404 // Method call: base->Method()
2405 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
2406 argv[0] = *funcexe->basetv;
2407 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002408 argvars = argv;
2409 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002410 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 error = call_user_func_check(fp, argcount, argvars, rettv,
2413 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002414 }
2415 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002416 else if (funcexe->basetv != NULL)
2417 {
2418 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002419 * expr->method(): Find the method name in the table, call its
2420 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02002421 */
2422 error = call_internal_method(fname, argcount, argvars, rettv,
2423 funcexe->basetv);
2424 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002425 else
2426 {
2427 /*
2428 * Find the function name in the table, call its implementation.
2429 */
2430 error = call_internal_func(fname, argcount, argvars, rettv);
2431 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02002432
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002433 /*
2434 * The function call (or "FuncUndefined" autocommand sequence) might
2435 * have been aborted by an error, an interrupt, or an explicitly thrown
2436 * exception that has not been caught so far. This situation can be
2437 * tested for by calling aborting(). For an error in an internal
2438 * function or for the "E132" error in call_user_func(), however, the
2439 * throw point at which the "force_abort" flag (temporarily reset by
2440 * emsg()) is normally updated has not been reached yet. We need to
2441 * update that flag first to make aborting() reliable.
2442 */
2443 update_force_abort();
2444 }
Bram Moolenaaref140542019-12-31 21:27:13 +01002445 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002446 ret = OK;
2447
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002448theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002449 /*
2450 * Report an error unless the argument evaluation or function call has been
2451 * cancelled due to an aborting error, an interrupt, or an exception.
2452 */
2453 if (!aborting())
2454 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002455 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002456 }
2457
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002458 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002459 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002460 clear_tv(&argv[--argv_clear + argv_base]);
2461
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002462 vim_free(tofree);
2463 vim_free(name);
2464
2465 return ret;
2466}
2467
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002468 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469printable_func_name(ufunc_T *fp)
2470{
2471 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
2472}
2473
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002474/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002475 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002476 */
2477 static void
2478list_func_head(ufunc_T *fp, int indent)
2479{
2480 int j;
2481
2482 msg_start();
2483 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002484 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002485 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002486 msg_puts("def ");
2487 else
2488 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002490 msg_putchar('(');
2491 for (j = 0; j < fp->uf_args.ga_len; ++j)
2492 {
2493 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002494 msg_puts(", ");
2495 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 if (fp->uf_arg_types != NULL)
2497 {
2498 char *tofree;
2499
2500 msg_puts(": ");
2501 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
2502 vim_free(tofree);
2503 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002504 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
2505 {
2506 msg_puts(" = ");
2507 msg_puts(((char **)(fp->uf_def_args.ga_data))
2508 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
2509 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002510 }
2511 if (fp->uf_varargs)
2512 {
2513 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002514 msg_puts(", ");
2515 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002516 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 if (fp->uf_va_name != NULL)
2518 {
2519 if (j)
2520 msg_puts(", ");
2521 msg_puts("...");
2522 msg_puts((char *)fp->uf_va_name);
2523 if (fp->uf_va_type)
2524 {
2525 char *tofree;
2526
2527 msg_puts(": ");
2528 msg_puts(type_name(fp->uf_va_type, &tofree));
2529 vim_free(tofree);
2530 }
2531 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002532 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002533
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002534 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002535 {
2536 if (fp->uf_ret_type != &t_void)
2537 {
2538 char *tofree;
2539
2540 msg_puts(": ");
2541 msg_puts(type_name(fp->uf_ret_type, &tofree));
2542 vim_free(tofree);
2543 }
2544 }
2545 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002546 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002547 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002548 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002549 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002550 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002551 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002552 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002553 msg_clr_eos();
2554 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002555 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002556}
2557
2558/*
2559 * Get a function name, translating "<SID>" and "<SNR>".
2560 * Also handles a Funcref in a List or Dictionary.
2561 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002562 * Set "*is_global" to TRUE when the function must be global, unless
2563 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002564 * flags:
2565 * TFN_INT: internal function name OK
2566 * TFN_QUIET: be quiet
2567 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002568 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002569 * Advances "pp" to just after the function name (if no error).
2570 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002571 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002572trans_function_name(
2573 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002574 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002575 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002576 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002577 funcdict_T *fdp, // return: info about dictionary used
2578 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002579{
2580 char_u *name = NULL;
2581 char_u *start;
2582 char_u *end;
2583 int lead;
2584 char_u sid_buf[20];
2585 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002587 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 int vim9script;
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002589 static char *e_function_name = N_("E129: Function name required");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002590
2591 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002592 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002593 start = *pp;
2594
Bram Moolenaare38eab22019-12-05 21:50:01 +01002595 // Check for hard coded <SNR>: already translated function ID (from a user
2596 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002597 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2598 && (*pp)[2] == (int)KE_SNR)
2599 {
2600 *pp += 3;
2601 len = get_id_len(pp) + 3;
2602 return vim_strnsave(start, len);
2603 }
2604
Bram Moolenaare38eab22019-12-05 21:50:01 +01002605 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2606 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002607 lead = eval_fname_script(start);
2608 if (lead > 2)
2609 start += lead;
2610
Bram Moolenaare38eab22019-12-05 21:50:01 +01002611 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002612 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002613 lead > 2 ? 0 : FNE_CHECK_START);
2614 if (end == start)
2615 {
2616 if (!skip)
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002617 emsg(_(e_function_name));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002618 goto theend;
2619 }
2620 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2621 {
2622 /*
2623 * Report an invalid expression in braces, unless the expression
2624 * evaluation has been cancelled due to an aborting error, an
2625 * interrupt, or an exception.
2626 */
2627 if (!aborting())
2628 {
2629 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002630 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002631 }
2632 else
2633 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2634 goto theend;
2635 }
2636
2637 if (lv.ll_tv != NULL)
2638 {
2639 if (fdp != NULL)
2640 {
2641 fdp->fd_dict = lv.ll_dict;
2642 fdp->fd_newkey = lv.ll_newkey;
2643 lv.ll_newkey = NULL;
2644 fdp->fd_di = lv.ll_di;
2645 }
2646 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2647 {
2648 name = vim_strsave(lv.ll_tv->vval.v_string);
2649 *pp = end;
2650 }
2651 else if (lv.ll_tv->v_type == VAR_PARTIAL
2652 && lv.ll_tv->vval.v_partial != NULL)
2653 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002654 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002655 *pp = end;
2656 if (partial != NULL)
2657 *partial = lv.ll_tv->vval.v_partial;
2658 }
2659 else
2660 {
2661 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2662 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002663 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002664 else
2665 *pp = end;
2666 name = NULL;
2667 }
2668 goto theend;
2669 }
2670
2671 if (lv.ll_name == NULL)
2672 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002673 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002674 *pp = end;
2675 goto theend;
2676 }
2677
Bram Moolenaare38eab22019-12-05 21:50:01 +01002678 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002679 if (lv.ll_exp_name != NULL)
2680 {
2681 len = (int)STRLEN(lv.ll_exp_name);
2682 name = deref_func_name(lv.ll_exp_name, &len, partial,
2683 flags & TFN_NO_AUTOLOAD);
2684 if (name == lv.ll_exp_name)
2685 name = NULL;
2686 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002687 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002688 {
2689 len = (int)(end - *pp);
2690 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2691 if (name == *pp)
2692 name = NULL;
2693 }
2694 if (name != NULL)
2695 {
2696 name = vim_strsave(name);
2697 *pp = end;
2698 if (STRNCMP(name, "<SNR>", 5) == 0)
2699 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002700 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002701 name[0] = K_SPECIAL;
2702 name[1] = KS_EXTRA;
2703 name[2] = (int)KE_SNR;
2704 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2705 }
2706 goto theend;
2707 }
2708
2709 if (lv.ll_exp_name != NULL)
2710 {
2711 len = (int)STRLEN(lv.ll_exp_name);
2712 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2713 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2714 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002715 // When there was "s:" already or the name expanded to get a
2716 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002717 lv.ll_name += 2;
2718 len -= 2;
2719 lead = 2;
2720 }
2721 }
2722 else
2723 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002724 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002725 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002726 {
2727 if (is_global != NULL && lv.ll_name[0] == 'g')
2728 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002729 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002730 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002731 len = (int)(end - lv.ll_name);
2732 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002733 if (len <= 0)
2734 {
2735 if (!skip)
2736 emsg(_(e_function_name));
2737 goto theend;
2738 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002739
Bram Moolenaar17f700a2020-12-19 21:23:42 +01002740 // In Vim9 script a user function is script-local by default, unless it
2741 // starts with a lower case character: dict.func().
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002742 vim9script = ASCII_ISUPPER(*start) && in_vim9script();
Bram Moolenaar17f700a2020-12-19 21:23:42 +01002743 if (vim9script)
2744 {
2745 char_u *p;
2746
2747 // SomeScript#func() is a global function.
2748 for (p = start; *p != NUL && *p != '('; ++p)
2749 if (*p == AUTOLOAD_CHAR)
2750 vim9script = FALSE;
2751 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002753 /*
2754 * Copy the function name to allocated memory.
2755 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2756 * Accept <SNR>123_name() outside a script.
2757 */
2758 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002759 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002761 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 if (!vim9script)
2763 lead = 3;
2764 if (vim9script || (lv.ll_exp_name != NULL
2765 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002766 || eval_fname_sid(*pp))
2767 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002769 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002770 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002771 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002772 goto theend;
2773 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002774 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 if (vim9script)
2776 extra = 3 + (int)STRLEN(sid_buf);
2777 else
2778 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002779 }
2780 }
2781 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2782 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002783 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002784 start);
2785 goto theend;
2786 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002787 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002788 {
2789 char_u *cp = vim_strchr(lv.ll_name, ':');
2790
2791 if (cp != NULL && cp < end)
2792 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002793 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002794 goto theend;
2795 }
2796 }
2797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002799 if (name != NULL)
2800 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002801 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002802 {
2803 name[0] = K_SPECIAL;
2804 name[1] = KS_EXTRA;
2805 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002807 STRCPY(name + 3, sid_buf);
2808 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2810 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002811 }
2812 *pp = end;
2813
2814theend:
2815 clear_lval(&lv);
2816 return name;
2817}
2818
2819/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02002820 * Assuming "name" is the result of trans_function_name() and it was prefixed
2821 * to use the script-local name, return the unmodified name (points into
2822 * "name"). Otherwise return NULL.
2823 * This can be used to first search for a script-local function and fall back
2824 * to the global function if not found.
2825 */
2826 char_u *
2827untrans_function_name(char_u *name)
2828{
2829 char_u *p;
2830
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002831 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02002832 {
2833 p = vim_strchr(name, '_');
2834 if (p != NULL)
2835 return p + 1;
2836 }
2837 return NULL;
2838}
2839
2840/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002841 * List functions. When "regmatch" is NULL all of then.
2842 * Otherwise functions matching "regmatch".
2843 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002844 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002845list_functions(regmatch_T *regmatch)
2846{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002847 int changed = func_hashtab.ht_changed;
2848 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002849 hashitem_T *hi;
2850
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002851 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002852 {
2853 if (!HASHITEM_EMPTY(hi))
2854 {
2855 ufunc_T *fp = HI2UF(hi);
2856
2857 --todo;
2858 if ((fp->uf_flags & FC_DEAD) == 0
2859 && (regmatch == NULL
2860 ? !message_filtered(fp->uf_name)
2861 && !func_name_refcount(fp->uf_name)
2862 : !isdigit(*fp->uf_name)
2863 && vim_regexec(regmatch, fp->uf_name, 0)))
2864 {
2865 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002866 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002867 {
2868 emsg(_("E454: function list was modified"));
2869 return;
2870 }
2871 }
2872 }
2873 }
2874}
2875
2876/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02002877 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002878 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
2879 * the function name.
Bram Moolenaar04b12692020-05-04 23:24:44 +02002880 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002881 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02002882 ufunc_T *
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002883define_function(exarg_T *eap, char_u *name_arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002884{
2885 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002886 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002887 int j;
2888 int c;
2889 int saved_did_emsg;
2890 int saved_wait_return = need_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002891 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002892 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002893 char_u *p;
2894 char_u *arg;
2895 char_u *line_arg = NULL;
2896 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002898 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002899 garray_T newlines;
2900 int varargs = FALSE;
2901 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002903 ufunc_T *fp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002904 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002905 int indent;
2906 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907#define MAX_FUNC_NESTING 50
2908 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002909 dictitem_T *v;
2910 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002911 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002912 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002913 hashitem_T *hi;
Bram Moolenaar66250c92020-08-20 15:02:42 +02002914 getline_opt_T getline_options = GETLINE_CONCAT_CONT;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002915 linenr_T sourcing_lnum_off;
2916 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002917 int is_heredoc = FALSE;
2918 char_u *skip_until = NULL;
2919 char_u *heredoc_trimmed = NULL;
Bram Moolenaare7e48382020-07-22 18:17:08 +02002920 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02002921 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002922
2923 /*
2924 * ":function" without argument: list functions.
2925 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002926 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002927 {
2928 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002929 list_functions(NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002930 eap->nextcmd = check_nextcmd(eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02002931 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002932 }
2933
2934 /*
2935 * ":function /pat": list functions matching pattern.
2936 */
2937 if (*eap->arg == '/')
2938 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002939 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002940 if (!eap->skip)
2941 {
2942 regmatch_T regmatch;
2943
2944 c = *p;
2945 *p = NUL;
2946 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2947 *p = c;
2948 if (regmatch.regprog != NULL)
2949 {
2950 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002951 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002952 vim_regfree(regmatch.regprog);
2953 }
2954 }
2955 if (*p == '/')
2956 ++p;
2957 eap->nextcmd = check_nextcmd(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02002958 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002959 }
2960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 ga_init(&newargs);
2962 ga_init(&argtypes);
2963 ga_init(&default_args);
2964
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002965 /*
2966 * Get the function name. There are these situations:
2967 * func normal function name
2968 * "name" == func, "fudi.fd_dict" == NULL
2969 * dict.func new dictionary entry
2970 * "name" == NULL, "fudi.fd_dict" set,
2971 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2972 * dict.func existing dict entry with a Funcref
2973 * "name" == func, "fudi.fd_dict" set,
2974 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2975 * dict.func existing dict entry that's not a Funcref
2976 * "name" == NULL, "fudi.fd_dict" set,
2977 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2978 * s:func script-local function name
2979 * g:func global function name, same as "func"
2980 */
2981 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002982 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002983 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02002984 // nested function, argument is (args).
2985 paren = TRUE;
2986 CLEAR_FIELD(fudi);
2987 }
2988 else
2989 {
2990 name = trans_function_name(&p, &is_global, eap->skip,
2991 TFN_NO_AUTOLOAD, &fudi, NULL);
2992 paren = (vim_strchr(p, '(') != NULL);
2993 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002994 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02002995 /*
2996 * Return on an invalid expression in braces, unless the expression
2997 * evaluation has been cancelled due to an aborting error, an
2998 * interrupt, or an exception.
2999 */
3000 if (!aborting())
3001 {
3002 if (!eap->skip && fudi.fd_newkey != NULL)
3003 semsg(_(e_dictkey), fudi.fd_newkey);
3004 vim_free(fudi.fd_newkey);
3005 return NULL;
3006 }
3007 else
3008 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003009 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003010 }
3011
Bram Moolenaare38eab22019-12-05 21:50:01 +01003012 // An error in a function call during evaluation of an expression in magic
3013 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003014 saved_did_emsg = did_emsg;
3015 did_emsg = FALSE;
3016
3017 /*
3018 * ":function func" with only function name: list function.
3019 */
3020 if (!paren)
3021 {
3022 if (!ends_excmd(*skipwhite(p)))
3023 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003024 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003025 goto ret_free;
3026 }
3027 eap->nextcmd = check_nextcmd(p);
3028 if (eap->nextcmd != NULL)
3029 *p = NUL;
3030 if (!eap->skip && !got_int)
3031 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003032 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003033 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
3034 {
3035 char_u *up = untrans_function_name(name);
3036
3037 // With Vim9 script the name was made script-local, if not
3038 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02003039 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003040 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003041 }
3042
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003043 if (fp != NULL)
3044 {
3045 list_func_head(fp, TRUE);
3046 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
3047 {
3048 if (FUNCLINE(fp, j) == NULL)
3049 continue;
3050 msg_putchar('\n');
3051 msg_outnum((long)(j + 1));
3052 if (j < 9)
3053 msg_putchar(' ');
3054 if (j < 99)
3055 msg_putchar(' ');
3056 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003057 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003058 ui_breakcheck();
3059 }
3060 if (!got_int)
3061 {
3062 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003063 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 msg_puts(" enddef");
3065 else
3066 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003067 }
3068 }
3069 else
Bram Moolenaara26b9702020-04-18 19:53:28 +02003070 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003071 }
3072 goto ret_free;
3073 }
3074
3075 /*
3076 * ":function name(arg1, arg2)" Define function.
3077 */
3078 p = skipwhite(p);
3079 if (*p != '(')
3080 {
3081 if (!eap->skip)
3082 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003083 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003084 goto ret_free;
3085 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003086 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003087 if (vim_strchr(p, '(') != NULL)
3088 p = vim_strchr(p, '(');
3089 }
3090 p = skipwhite(p + 1);
3091
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02003092 // In Vim9 script only global functions can be redefined.
3093 if (vim9script && eap->forceit && !is_global)
3094 {
3095 emsg(_(e_nobang));
3096 goto ret_free;
3097 }
3098
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003099 ga_init2(&newlines, (int)sizeof(char_u *), 3);
3100
Bram Moolenaar04b12692020-05-04 23:24:44 +02003101 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003102 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003103 // Check the name of the function. Unless it's a dictionary function
3104 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003105 if (name != NULL)
3106 arg = name;
3107 else
3108 arg = fudi.fd_newkey;
3109 if (arg != NULL && (fudi.fd_di == NULL
3110 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
3111 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
3112 {
3113 if (*arg == K_SPECIAL)
3114 j = 3;
3115 else
3116 j = 0;
3117 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
3118 : eval_isnamec(arg[j])))
3119 ++j;
3120 if (arg[j] != NUL)
3121 emsg_funcname((char *)e_invarg2, arg);
3122 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003123 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003124 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003125 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003126 }
3127
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003128 // This may get more lines and make the pointers into the first line
3129 // invalid.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003131 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003132 &varargs, &default_args, eap->skip,
3133 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003134 goto errret_2;
3135
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003137 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 // find the return type: :def Func(): type
3139 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003140 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02003142 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003144 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003145 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003147 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003149 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003150 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02003151 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003152 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003153 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02003154 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003155 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 else
3157 // find extra arguments "range", "dict", "abort" and "closure"
3158 for (;;)
3159 {
3160 p = skipwhite(p);
3161 if (STRNCMP(p, "range", 5) == 0)
3162 {
3163 flags |= FC_RANGE;
3164 p += 5;
3165 }
3166 else if (STRNCMP(p, "dict", 4) == 0)
3167 {
3168 flags |= FC_DICT;
3169 p += 4;
3170 }
3171 else if (STRNCMP(p, "abort", 5) == 0)
3172 {
3173 flags |= FC_ABORT;
3174 p += 5;
3175 }
3176 else if (STRNCMP(p, "closure", 7) == 0)
3177 {
3178 flags |= FC_CLOSURE;
3179 p += 7;
3180 if (current_funccal == NULL)
3181 {
3182 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
3183 name == NULL ? (char_u *)"" : name);
3184 goto erret;
3185 }
3186 }
3187 else
3188 break;
3189 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003190
Bram Moolenaare38eab22019-12-05 21:50:01 +01003191 // When there is a line break use what follows for the function body.
3192 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003193 if (*p == '\n')
3194 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003195 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02003196 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
3197 && eap->cmdidx != CMD_def)
Bram Moolenaare7e48382020-07-22 18:17:08 +02003198 && !(*p == '#' && (vim9script || eap->cmdidx == CMD_def))
3199 && !eap->skip
3200 && !did_emsg)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003201 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003202
3203 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
3205 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003206 */
3207 if (KeyTyped)
3208 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003209 // Check if the function already exists, don't let the user type the
3210 // whole function before telling him it doesn't work! For a script we
3211 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003212 if (!eap->skip && !eap->forceit)
3213 {
3214 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003215 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003216 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003217 emsg_funcname(e_funcexts, name);
3218 }
3219
3220 if (!eap->skip && did_emsg)
3221 goto erret;
3222
Bram Moolenaare38eab22019-12-05 21:50:01 +01003223 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003224 cmdline_row = msg_row;
3225 }
3226
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003227 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003228 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003229
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02003230 // Detect having skipped over comment lines to find the return
3231 // type. Add NULL lines to keep the line count correct.
3232 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
3233 if (SOURCING_LNUM < sourcing_lnum_off)
3234 {
3235 sourcing_lnum_off -= SOURCING_LNUM;
3236 if (ga_grow(&newlines, sourcing_lnum_off) == FAIL)
3237 goto erret;
3238 while (sourcing_lnum_off-- > 0)
3239 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3240 }
3241
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003242 indent = 2;
3243 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003245 for (;;)
3246 {
3247 if (KeyTyped)
3248 {
3249 msg_scroll = TRUE;
3250 saved_wait_return = FALSE;
3251 }
3252 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003253
3254 if (line_arg != NULL)
3255 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003256 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003257 theline = line_arg;
3258 p = vim_strchr(theline, '\n');
3259 if (p == NULL)
3260 line_arg += STRLEN(line_arg);
3261 else
3262 {
3263 *p = NUL;
3264 line_arg = p + 1;
3265 }
3266 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003267 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02003268 {
3269 vim_free(line_to_free);
3270 if (eap->getline == NULL)
Bram Moolenaar66250c92020-08-20 15:02:42 +02003271 theline = getcmdline(':', 0L, indent, getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003272 else
Bram Moolenaar66250c92020-08-20 15:02:42 +02003273 theline = eap->getline(':', eap->cookie, indent,
3274 getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003275 line_to_free = theline;
3276 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003277 if (KeyTyped)
3278 lines_left = Rows - 1;
3279 if (theline == NULL)
3280 {
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003281 if (skip_until != NULL)
3282 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
3283 else if (eap->cmdidx == CMD_def)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003284 emsg(_(e_missing_enddef));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 else
3286 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003287 goto erret;
3288 }
3289
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003290 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003291 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003292 if (SOURCING_LNUM < sourcing_lnum_off)
3293 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003294 else
3295 sourcing_lnum_off = 0;
3296
3297 if (skip_until != NULL)
3298 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003300 // * ":append" and "."
3301 // * ":python <<EOF" and "EOF"
3302 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
3303 if (heredoc_trimmed == NULL
3304 || (is_heredoc && skipwhite(theline) == theline)
3305 || STRNCMP(theline, heredoc_trimmed,
3306 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003307 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003308 if (heredoc_trimmed == NULL)
3309 p = theline;
3310 else if (is_heredoc)
3311 p = skipwhite(theline) == theline
3312 ? theline : theline + STRLEN(heredoc_trimmed);
3313 else
3314 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02003315 if (STRCMP(p, skip_until) == 0)
3316 {
3317 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003318 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003319 getline_options = GETLINE_CONCAT_CONT;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003320 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003321 }
3322 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003323 }
3324 else
3325 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003326 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01003327 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003328 ;
3329
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330 // Check for "endfunction" or "enddef".
3331 if (checkforcmd(&p, nesting_def[nesting]
3332 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003333 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02003334 char_u *nextcmd = NULL;
3335
Bram Moolenaar663bb232017-06-22 19:12:10 +02003336 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02003337 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02003338 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02003339 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02003340 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 give_warning2(eap->cmdidx == CMD_def
3342 ? (char_u *)_("W1001: Text found after :enddef: %s")
3343 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003344 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003345 if (nextcmd != NULL)
3346 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003347 // Another command follows. If the line came from "eap" we
3348 // can simply point into it, otherwise we need to change
3349 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02003350 eap->nextcmd = nextcmd;
3351 if (line_to_free != NULL)
3352 {
3353 vim_free(*eap->cmdlinep);
3354 *eap->cmdlinep = line_to_free;
3355 line_to_free = NULL;
3356 }
3357 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003358 break;
3359 }
3360
Bram Moolenaare38eab22019-12-05 21:50:01 +01003361 // Increase indent inside "if", "while", "for" and "try", decrease
3362 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003364 indent -= 2;
3365 else if (STRNCMP(p, "if", 2) == 0
3366 || STRNCMP(p, "wh", 2) == 0
3367 || STRNCMP(p, "for", 3) == 0
3368 || STRNCMP(p, "try", 3) == 0)
3369 indent += 2;
3370
Bram Moolenaare38eab22019-12-05 21:50:01 +01003371 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01003372 // Only recognize "def" inside "def", not inside "function",
3373 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01003375 if (checkforcmd(&p, "function", 2)
3376 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003377 {
3378 if (*p == '!')
3379 p = skipwhite(p + 1);
3380 p += eval_fname_script(p);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003381 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL, NULL));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003382 if (*skipwhite(p) == '(')
3383 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 if (nesting == MAX_FUNC_NESTING - 1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003385 emsg(_(e_function_nesting_too_deep));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 else
3387 {
3388 ++nesting;
3389 nesting_def[nesting] = (c == 'd');
3390 indent += 2;
3391 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003392 }
3393 }
3394
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003395 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003396 p = skip_range(p, FALSE, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003397 if (eap->cmdidx != CMD_def
3398 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01003399 || (p[0] == 'c'
3400 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
3401 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
3402 && (STRNCMP(&p[3], "nge", 3) != 0
3403 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003404 || (p[0] == 'i'
3405 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003406 && (!ASCII_ISALPHA(p[2])
3407 || (p[2] == 's'
3408 && (!ASCII_ISALPHA(p[3])
3409 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003410 skip_until = vim_strsave((char_u *)".");
3411
Bram Moolenaare38eab22019-12-05 21:50:01 +01003412 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003413 arg = skipwhite(skiptowhite(p));
3414 if (arg[0] == '<' && arg[1] =='<'
3415 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003416 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
3417 || ((p[2] == '3' || p[2] == 'x')
3418 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003419 || (p[0] == 'p' && p[1] == 'e'
3420 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
3421 || (p[0] == 't' && p[1] == 'c'
3422 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
3423 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
3424 && !ASCII_ISALPHA(p[3]))
3425 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
3426 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
3427 || (p[0] == 'm' && p[1] == 'z'
3428 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
3429 ))
3430 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003431 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003432 p = skipwhite(arg + 2);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003433 if (STRNCMP(p, "trim", 4) == 0)
3434 {
3435 // Ignore leading white space.
3436 p = skipwhite(p + 4);
3437 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003438 skipwhite(theline) - theline);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003439 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003440 if (*p == NUL)
3441 skip_until = vim_strsave((char_u *)".");
3442 else
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003443 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003444 getline_options = GETLINE_NONE;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003445 is_heredoc = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003446 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003447
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003448 // Check for ":cmd v =<< [trim] EOF"
3449 // and ":cmd [a, b] =<< [trim] EOF"
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003450 // and "lines =<< [trim] EOF" for Vim9
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003451 // Where "cmd" can be "let", "var", "final" or "const".
Bram Moolenaar8471e572019-05-19 21:37:18 +02003452 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003453 if (*arg == '[')
3454 arg = vim_strchr(arg, ']');
3455 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003456 {
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003457 int found = (eap->cmdidx == CMD_def && arg[0] == '='
3458 && arg[1] == '<' && arg[2] =='<');
3459
3460 if (!found)
3461 // skip over the argument after "cmd"
3462 arg = skipwhite(skiptowhite(arg));
3463 if (found || (arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003464 && (checkforcmd(&p, "let", 2)
3465 || checkforcmd(&p, "var", 3)
3466 || checkforcmd(&p, "final", 5)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003467 || checkforcmd(&p, "const", 5))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02003468 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003469 p = skipwhite(arg + 3);
3470 if (STRNCMP(p, "trim", 4) == 0)
3471 {
3472 // Ignore leading white space.
3473 p = skipwhite(p + 4);
3474 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003475 skipwhite(theline) - theline);
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003476 }
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003477 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003478 getline_options = GETLINE_NONE;
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003479 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003480 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003481 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003482 }
3483
Bram Moolenaare38eab22019-12-05 21:50:01 +01003484 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003485 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003486 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003487
Bram Moolenaare38eab22019-12-05 21:50:01 +01003488 // Copy the line to newly allocated memory. get_one_sourceline()
3489 // allocates 250 bytes per line, this saves 80% on average. The cost
3490 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003491 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003492 if (p == NULL)
3493 goto erret;
3494 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003495
Bram Moolenaare38eab22019-12-05 21:50:01 +01003496 // Add NULL lines for continuation lines, so that the line count is
3497 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003498 while (sourcing_lnum_off-- > 0)
3499 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3500
Bram Moolenaare38eab22019-12-05 21:50:01 +01003501 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003502 if (line_arg != NULL && *line_arg == NUL)
3503 line_arg = NULL;
3504 }
3505
Bram Moolenaare38eab22019-12-05 21:50:01 +01003506 // Don't define the function when skipping commands or when an error was
3507 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003508 if (eap->skip || did_emsg)
3509 goto erret;
3510
3511 /*
3512 * If there are no errors, add the function
3513 */
3514 if (fudi.fd_dict == NULL)
3515 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003516 hashtab_T *ht;
3517
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003518 v = find_var(name, &ht, FALSE);
3519 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
3520 {
3521 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
3522 name);
3523 goto erret;
3524 }
3525
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003526 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaareef21022020-08-01 22:16:43 +02003527 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003528 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003529 char_u *uname = untrans_function_name(name);
3530
3531 import = find_imported(uname == NULL ? name : uname, 0, NULL);
3532 }
3533
3534 if (fp != NULL || import != NULL)
3535 {
3536 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003538 // Function can be replaced with "function!" and when sourcing the
3539 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02003540 // A name that is used by an import can not be overruled.
3541 if (import != NULL
3542 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003543 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02003544 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003545 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003546 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003547 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02003548 else
3549 emsg_funcname(e_funcexts, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003550 goto erret;
3551 }
3552 if (fp->uf_calls > 0)
3553 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003554 emsg_funcname(
3555 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003556 name);
3557 goto erret;
3558 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003559 if (fp->uf_refcount > 1)
3560 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003561 // This function is referenced somewhere, don't redefine it but
3562 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003563 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003564 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003565 fp = NULL;
3566 overwrite = TRUE;
3567 }
3568 else
3569 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003570 char_u *exp_name = fp->uf_name_exp;
3571
3572 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01003573 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003574 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003575 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003576 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003578#ifdef FEAT_PROFILE
3579 fp->uf_profiling = FALSE;
3580 fp->uf_prof_initialized = FALSE;
3581#endif
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003582 unlink_def_function(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003583 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003584 }
3585 }
3586 else
3587 {
3588 char numbuf[20];
3589
3590 fp = NULL;
3591 if (fudi.fd_newkey == NULL && !eap->forceit)
3592 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003593 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003594 goto erret;
3595 }
3596 if (fudi.fd_di == NULL)
3597 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003598 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02003599 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003600 goto erret;
3601 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003602 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02003603 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003604 goto erret;
3605
Bram Moolenaare38eab22019-12-05 21:50:01 +01003606 // Give the function a sequential number. Can only be used with a
3607 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003608 vim_free(name);
3609 sprintf(numbuf, "%d", ++func_nr);
3610 name = vim_strsave((char_u *)numbuf);
3611 if (name == NULL)
3612 goto erret;
3613 }
3614
3615 if (fp == NULL)
3616 {
3617 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3618 {
3619 int slen, plen;
3620 char_u *scriptname;
3621
Bram Moolenaare38eab22019-12-05 21:50:01 +01003622 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003623 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003624 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003625 {
3626 scriptname = autoload_name(name);
3627 if (scriptname != NULL)
3628 {
3629 p = vim_strchr(scriptname, '/');
3630 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003631 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003632 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003633 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003634 j = OK;
3635 vim_free(scriptname);
3636 }
3637 }
3638 if (j == FAIL)
3639 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003640 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003641 goto erret;
3642 }
3643 }
3644
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003645 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003646 if (fp == NULL)
3647 goto erret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003648 fp->uf_def_status = eap->cmdidx == CMD_def ? UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003649 : UF_NOT_COMPILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003650
3651 if (fudi.fd_dict != NULL)
3652 {
3653 if (fudi.fd_di == NULL)
3654 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003655 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003656 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3657 if (fudi.fd_di == NULL)
3658 {
3659 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003660 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003661 goto erret;
3662 }
3663 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3664 {
3665 vim_free(fudi.fd_di);
3666 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003667 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003668 goto erret;
3669 }
3670 }
3671 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003672 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003673 clear_tv(&fudi.fd_di->di_tv);
3674 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003675 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003676
Bram Moolenaare38eab22019-12-05 21:50:01 +01003677 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003678 flags |= FC_DICT;
3679 }
3680
Bram Moolenaare38eab22019-12-05 21:50:01 +01003681 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003682 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003683 if (overwrite)
3684 {
3685 hi = hash_find(&func_hashtab, name);
3686 hi->hi_key = UF2HIKEY(fp);
3687 }
3688 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003689 {
3690 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003691 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003692 goto erret;
3693 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003694 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003695 }
3696 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003697 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003699 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700
3701 if (eap->cmdidx == CMD_def)
3702 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003703 int lnum_save = SOURCING_LNUM;
3704 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003705
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003706 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003707
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003708 // error messages are for the first function line
3709 SOURCING_LNUM = sourcing_lnum_top;
3710
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003711 if (cstack != NULL && cstack->cs_idx >= 0)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003712 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003713 int count = cstack->cs_idx + 1;
3714 int i;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003715
3716 // The block context may be needed for script variables declared in
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003717 // a block that is visible now but not when the function is called
3718 // later.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003719 fp->uf_block_ids = ALLOC_MULT(int, count);
3720 if (fp->uf_block_ids != NULL)
3721 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003722 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003723 sizeof(int) * count);
3724 fp->uf_block_depth = count;
3725 }
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003726
3727 // Set flag in each block to indicate a function was defined. This
3728 // is used to keep the variable when leaving the block, see
3729 // hide_script_var().
3730 for (i = 0; i <= cstack->cs_idx; ++i)
3731 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003732 }
3733
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003734 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003736 SOURCING_LNUM = lnum_save;
3737 goto errret_2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003739 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740
3741 // parse the return type, if any
3742 if (ret_type == NULL)
3743 fp->uf_ret_type = &t_void;
3744 else
3745 {
3746 p = ret_type;
3747 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list);
3748 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02003749 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003751 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003752 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003754 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003755 if ((flags & FC_CLOSURE) != 0)
3756 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003757 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003758 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003759 }
3760 else
3761 fp->uf_scoped = NULL;
3762
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003763#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003764 if (prof_def_func())
3765 func_do_profile(fp);
3766#endif
3767 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003768 if (sandbox)
3769 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003770 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003771 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003772 fp->uf_flags = flags;
3773 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003774 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003775 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02003776 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003777 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 if (is_export)
3779 {
3780 fp->uf_flags |= FC_EXPORT;
3781 // let ex_export() know the export worked.
3782 is_export = FALSE;
3783 }
3784
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003785 if (eap->cmdidx == CMD_def)
3786 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02003787 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
3788 // :func does not use Vim9 script syntax, even in a Vim9 script file
3789 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003790
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003791 goto ret_free;
3792
3793erret:
3794 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003795 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003796errret_2:
3797 ga_clear_strings(&newlines);
3798ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003799 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003800 vim_free(skip_until);
Bram Moolenaarfffdf472020-12-13 21:16:55 +01003801 vim_free(heredoc_trimmed);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003802 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003803 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003804 if (name != name_arg)
3805 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003806 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003807 did_emsg |= saved_did_emsg;
3808 need_wait_return |= saved_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003809
3810 return fp;
3811}
3812
3813/*
3814 * ":function"
3815 */
3816 void
3817ex_function(exarg_T *eap)
3818{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003819 (void)define_function(eap, NULL);
Bram Moolenaar822ba242020-05-24 23:00:18 +02003820}
3821
3822/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02003823 * :defcompile - compile all :def functions in the current script that need to
3824 * be compiled. Except dead functions.
Bram Moolenaar822ba242020-05-24 23:00:18 +02003825 */
3826 void
3827ex_defcompile(exarg_T *eap UNUSED)
3828{
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003829 long todo = (long)func_hashtab.ht_used;
3830 int changed = func_hashtab.ht_changed;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003831 hashitem_T *hi;
3832 ufunc_T *ufunc;
3833
3834 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
3835 {
3836 if (!HASHITEM_EMPTY(hi))
3837 {
3838 --todo;
3839 ufunc = HI2UF(hi);
3840 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar96f8f492020-09-09 17:08:51 +02003841 && ufunc->uf_def_status == UF_TO_BE_COMPILED
3842 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003843 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02003844 compile_def_function(ufunc, FALSE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003845
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003846 if (func_hashtab.ht_changed != changed)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003847 {
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003848 // a function has been added or removed, need to start over
3849 todo = (long)func_hashtab.ht_used;
3850 changed = func_hashtab.ht_changed;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003851 hi = func_hashtab.ht_array;
Bram Moolenaar285b1892020-05-26 11:37:26 +02003852 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003853 }
3854 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003855 }
3856 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003857}
3858
3859/*
3860 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3861 * Return 2 if "p" starts with "s:".
3862 * Return 0 otherwise.
3863 */
3864 int
3865eval_fname_script(char_u *p)
3866{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003867 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3868 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003869 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3870 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3871 return 5;
3872 if (p[0] == 's' && p[1] == ':')
3873 return 2;
3874 return 0;
3875}
3876
3877 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003878translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003879{
3880 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003881 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003882 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883}
3884
3885/*
3886 * Return TRUE when "ufunc" has old-style "..." varargs
3887 * or named varargs "...name: type".
3888 */
3889 int
3890has_varargs(ufunc_T *ufunc)
3891{
3892 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003893}
3894
3895/*
3896 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003897 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003898 */
3899 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003900function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003901{
3902 char_u *nm = name;
3903 char_u *p;
3904 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003905 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003906 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003907
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003908 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3909 if (no_deref)
3910 flag |= TFN_NO_DEREF;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003911 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003912 nm = skipwhite(nm);
3913
Bram Moolenaare38eab22019-12-05 21:50:01 +01003914 // Only accept "funcname", "funcname ", "funcname (..." and
3915 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003916 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003917 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003918 vim_free(p);
3919 return n;
3920}
3921
Bram Moolenaar113e1072019-01-20 15:30:40 +01003922#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003923 char_u *
3924get_expanded_name(char_u *name, int check)
3925{
3926 char_u *nm = name;
3927 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003928 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003929
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003930 p = trans_function_name(&nm, &is_global, FALSE,
3931 TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003932
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003933 if (p != NULL && *nm == NUL
3934 && (!check || translated_function_exists(p, is_global)))
3935 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003936
3937 vim_free(p);
3938 return NULL;
3939}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003940#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003941
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003942/*
3943 * Function given to ExpandGeneric() to obtain the list of user defined
3944 * function names.
3945 */
3946 char_u *
3947get_user_func_name(expand_T *xp, int idx)
3948{
3949 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003950 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003951 static hashitem_T *hi;
3952 ufunc_T *fp;
3953
3954 if (idx == 0)
3955 {
3956 done = 0;
3957 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003958 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003959 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003960 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003961 {
3962 if (done++ > 0)
3963 ++hi;
3964 while (HASHITEM_EMPTY(hi))
3965 ++hi;
3966 fp = HI2UF(hi);
3967
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 // don't show dead, dict and lambda functions
3969 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02003970 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003972
3973 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003974 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003975
3976 cat_func_name(IObuff, fp);
3977 if (xp->xp_context != EXPAND_USER_FUNC)
3978 {
3979 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003981 STRCAT(IObuff, ")");
3982 }
3983 return IObuff;
3984 }
3985 return NULL;
3986}
3987
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003988/*
3989 * ":delfunction {name}"
3990 */
3991 void
3992ex_delfunction(exarg_T *eap)
3993{
3994 ufunc_T *fp = NULL;
3995 char_u *p;
3996 char_u *name;
3997 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003998 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003999
4000 p = eap->arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004001 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004002 vim_free(fudi.fd_newkey);
4003 if (name == NULL)
4004 {
4005 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004006 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004007 return;
4008 }
4009 if (!ends_excmd(*skipwhite(p)))
4010 {
4011 vim_free(name);
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004012 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004013 return;
4014 }
4015 eap->nextcmd = check_nextcmd(p);
4016 if (eap->nextcmd != NULL)
4017 *p = NUL;
4018
4019 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004020 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004021 vim_free(name);
4022
4023 if (!eap->skip)
4024 {
4025 if (fp == NULL)
4026 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02004027 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004028 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004029 return;
4030 }
4031 if (fp->uf_calls > 0)
4032 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004033 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004034 return;
4035 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004036 if (fp->uf_flags & FC_VIM9)
4037 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004038 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004039 return;
4040 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004041
4042 if (fudi.fd_dict != NULL)
4043 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004044 // Delete the dict item that refers to the function, it will
4045 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004046 dictitem_remove(fudi.fd_dict, fudi.fd_di);
4047 }
4048 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004049 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004050 // A normal function (not a numbered function or lambda) has a
4051 // refcount of 1 for the entry in the hashtable. When deleting
4052 // it and the refcount is more than one, it should be kept.
4053 // A numbered function and lambda should be kept if the refcount is
4054 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004055 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004056 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004057 // Function is still referenced somewhere. Don't free it but
4058 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004059 if (func_remove(fp))
4060 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004061 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004062 }
4063 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004064 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004065 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004066 }
4067}
4068
4069/*
4070 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004071 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004072 */
4073 void
4074func_unref(char_u *name)
4075{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004076 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004077
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004078 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004079 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004080 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004081 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004082 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004083#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004084 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004085#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01004086 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004087 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004088 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004089 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004090 // Only delete it when it's not being used. Otherwise it's done
4091 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004092 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004093 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02004094 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004095}
4096
4097/*
4098 * Unreference a Function: decrement the reference count and free it when it
4099 * becomes zero.
4100 */
4101 void
4102func_ptr_unref(ufunc_T *fp)
4103{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004104 if (fp != NULL && --fp->uf_refcount <= 0)
4105 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004106 // Only delete it when it's not being used. Otherwise it's done
4107 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02004108 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004109 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004110 }
4111}
4112
4113/*
4114 * Count a reference to a Function.
4115 */
4116 void
4117func_ref(char_u *name)
4118{
4119 ufunc_T *fp;
4120
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004121 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004122 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004123 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004124 if (fp != NULL)
4125 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004126 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01004127 // Only give an error for a numbered function.
4128 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01004129 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004130}
4131
4132/*
4133 * Count a reference to a Function.
4134 */
4135 void
4136func_ptr_ref(ufunc_T *fp)
4137{
4138 if (fp != NULL)
4139 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004140}
4141
4142/*
4143 * Return TRUE if items in "fc" do not have "copyID". That means they are not
4144 * referenced from anywhere that is in use.
4145 */
4146 static int
4147can_free_funccal(funccall_T *fc, int copyID)
4148{
4149 return (fc->l_varlist.lv_copyID != copyID
4150 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004151 && fc->l_avars.dv_copyID != copyID
4152 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004153}
4154
4155/*
4156 * ":return [expr]"
4157 */
4158 void
4159ex_return(exarg_T *eap)
4160{
4161 char_u *arg = eap->arg;
4162 typval_T rettv;
4163 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004164 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004165
4166 if (current_funccal == NULL)
4167 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004168 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004169 return;
4170 }
4171
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004172 CLEAR_FIELD(evalarg);
4173 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
4174
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004175 if (eap->skip)
4176 ++emsg_skip;
4177
4178 eap->nextcmd = NULL;
4179 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004180 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004181 {
4182 if (!eap->skip)
4183 returning = do_return(eap, FALSE, TRUE, &rettv);
4184 else
4185 clear_tv(&rettv);
4186 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004187 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004188 else if (!eap->skip)
4189 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004190 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01004191 update_force_abort();
4192
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004193 /*
4194 * Return unless the expression evaluation has been cancelled due to an
4195 * aborting error, an interrupt, or an exception.
4196 */
4197 if (!aborting())
4198 returning = do_return(eap, FALSE, TRUE, NULL);
4199 }
4200
Bram Moolenaare38eab22019-12-05 21:50:01 +01004201 // When skipping or the return gets pending, advance to the next command
4202 // in this line (!returning). Otherwise, ignore the rest of the line.
4203 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004204 if (returning)
4205 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004206 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004207 eap->nextcmd = check_nextcmd(arg);
4208
4209 if (eap->skip)
4210 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02004211 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004212}
4213
4214/*
4215 * ":1,25call func(arg1, arg2)" function call.
4216 */
4217 void
4218ex_call(exarg_T *eap)
4219{
4220 char_u *arg = eap->arg;
4221 char_u *startarg;
4222 char_u *name;
4223 char_u *tofree;
4224 int len;
4225 typval_T rettv;
4226 linenr_T lnum;
4227 int doesrange;
4228 int failed = FALSE;
4229 funcdict_T fudi;
4230 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004231 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004232
Bram Moolenaare6b53242020-07-01 17:28:33 +02004233 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004234 if (eap->skip)
4235 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004236 // trans_function_name() doesn't work well when skipping, use eval0()
4237 // instead to skip to any following command, e.g. for:
4238 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004239 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004240 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004241 clear_tv(&rettv);
4242 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004243 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004244 return;
4245 }
4246
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004247 tofree = trans_function_name(&arg, NULL, eap->skip,
4248 TFN_INT, &fudi, &partial);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004249 if (fudi.fd_newkey != NULL)
4250 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004251 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004252 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004253 vim_free(fudi.fd_newkey);
4254 }
4255 if (tofree == NULL)
4256 return;
4257
Bram Moolenaare38eab22019-12-05 21:50:01 +01004258 // Increase refcount on dictionary, it could get deleted when evaluating
4259 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004260 if (fudi.fd_dict != NULL)
4261 ++fudi.fd_dict->dv_refcount;
4262
Bram Moolenaare38eab22019-12-05 21:50:01 +01004263 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
4264 // contents. For VAR_PARTIAL get its partial, unless we already have one
4265 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004266 len = (int)STRLEN(tofree);
4267 name = deref_func_name(tofree, &len,
4268 partial != NULL ? NULL : &partial, FALSE);
4269
Bram Moolenaare38eab22019-12-05 21:50:01 +01004270 // Skip white space to allow ":call func ()". Not good, but required for
4271 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004272 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004273 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004274
4275 if (*startarg != '(')
4276 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004278 goto end;
4279 }
4280
4281 /*
4282 * When skipping, evaluate the function once, to find the end of the
4283 * arguments.
4284 * When the function takes a range, this is discovered after the first
4285 * call, and the loop is broken.
4286 */
4287 if (eap->skip)
4288 {
4289 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004290 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004291 }
4292 else
4293 lnum = eap->line1;
4294 for ( ; lnum <= eap->line2; ++lnum)
4295 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004296 funcexe_T funcexe;
4297
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004298 if (!eap->skip && eap->addr_count > 0)
4299 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004300 if (lnum > curbuf->b_ml.ml_line_count)
4301 {
4302 // If the function deleted lines or switched to another buffer
4303 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004304 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004305 break;
4306 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004307 curwin->w_cursor.lnum = lnum;
4308 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004309 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004310 }
4311 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004312
Bram Moolenaara80faa82020-04-12 19:37:17 +02004313 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004314 funcexe.firstline = eap->line1;
4315 funcexe.lastline = eap->line2;
4316 funcexe.doesrange = &doesrange;
4317 funcexe.evaluate = !eap->skip;
4318 funcexe.partial = partial;
4319 funcexe.selfdict = fudi.fd_dict;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004320 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004321 {
4322 failed = TRUE;
4323 break;
4324 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01004325 if (has_watchexpr())
4326 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004327
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004328 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004329 if (handle_subscript(&arg, &rettv,
4330 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004331 {
4332 failed = TRUE;
4333 break;
4334 }
4335
4336 clear_tv(&rettv);
4337 if (doesrange || eap->skip)
4338 break;
4339
Bram Moolenaare38eab22019-12-05 21:50:01 +01004340 // Stop when immediately aborting on error, or when an interrupt
4341 // occurred or an exception was thrown but not caught.
4342 // get_func_tv() returned OK, so that the check for trailing
4343 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004344 if (aborting())
4345 break;
4346 }
4347 if (eap->skip)
4348 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004349 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004350
Bram Moolenaare51bb172020-02-16 19:42:23 +01004351 // When inside :try we need to check for following "| catch".
4352 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004353 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004354 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02004355 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004356 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004357 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004358 if (!failed)
4359 {
4360 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004361 semsg(_(e_trailing_arg), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004362 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004363 }
4364 else
4365 eap->nextcmd = check_nextcmd(arg);
4366 }
4367
4368end:
4369 dict_unref(fudi.fd_dict);
4370 vim_free(tofree);
4371}
4372
4373/*
4374 * Return from a function. Possibly makes the return pending. Also called
4375 * for a pending return at the ":endtry" or after returning from an extra
4376 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
4377 * when called due to a ":return" command. "rettv" may point to a typval_T
4378 * with the return rettv. Returns TRUE when the return can be carried out,
4379 * FALSE when the return gets pending.
4380 */
4381 int
4382do_return(
4383 exarg_T *eap,
4384 int reanimate,
4385 int is_cmd,
4386 void *rettv)
4387{
4388 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01004389 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004390
4391 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004392 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004393 current_funccal->returned = FALSE;
4394
4395 /*
4396 * Cleanup (and inactivate) conditionals, but stop when a try conditional
4397 * not in its finally clause (which then is to be executed next) is found.
4398 * In this case, make the ":return" pending for execution at the ":endtry".
4399 * Otherwise, return normally.
4400 */
4401 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
4402 if (idx >= 0)
4403 {
4404 cstack->cs_pending[idx] = CSTP_RETURN;
4405
4406 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004407 // A pending return again gets pending. "rettv" points to an
4408 // allocated variable with the rettv of the original ":return"'s
4409 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004410 cstack->cs_rettv[idx] = rettv;
4411 else
4412 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004413 // When undoing a return in order to make it pending, get the stored
4414 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004415 if (reanimate)
4416 rettv = current_funccal->rettv;
4417
4418 if (rettv != NULL)
4419 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004420 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004421 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
4422 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
4423 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004424 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004425 }
4426 else
4427 cstack->cs_rettv[idx] = NULL;
4428
4429 if (reanimate)
4430 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004431 // The pending return value could be overwritten by a ":return"
4432 // without argument in a finally clause; reset the default
4433 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004434 current_funccal->rettv->v_type = VAR_NUMBER;
4435 current_funccal->rettv->vval.v_number = 0;
4436 }
4437 }
4438 report_make_pending(CSTP_RETURN, rettv);
4439 }
4440 else
4441 {
4442 current_funccal->returned = TRUE;
4443
Bram Moolenaare38eab22019-12-05 21:50:01 +01004444 // If the return is carried out now, store the return value. For
4445 // a return immediately after reanimation, the value is already
4446 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004447 if (!reanimate && rettv != NULL)
4448 {
4449 clear_tv(current_funccal->rettv);
4450 *current_funccal->rettv = *(typval_T *)rettv;
4451 if (!is_cmd)
4452 vim_free(rettv);
4453 }
4454 }
4455
4456 return idx < 0;
4457}
4458
4459/*
4460 * Free the variable with a pending return value.
4461 */
4462 void
4463discard_pending_return(void *rettv)
4464{
4465 free_tv((typval_T *)rettv);
4466}
4467
4468/*
4469 * Generate a return command for producing the value of "rettv". The result
4470 * is an allocated string. Used by report_pending() for verbose messages.
4471 */
4472 char_u *
4473get_return_cmd(void *rettv)
4474{
4475 char_u *s = NULL;
4476 char_u *tofree = NULL;
4477 char_u numbuf[NUMBUFLEN];
4478
4479 if (rettv != NULL)
4480 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
4481 if (s == NULL)
4482 s = (char_u *)"";
4483
4484 STRCPY(IObuff, ":return ");
4485 STRNCPY(IObuff + 8, s, IOSIZE - 8);
4486 if (STRLEN(s) + 8 >= IOSIZE)
4487 STRCPY(IObuff + IOSIZE - 4, "...");
4488 vim_free(tofree);
4489 return vim_strsave(IObuff);
4490}
4491
4492/*
4493 * Get next function line.
4494 * Called by do_cmdline() to get the next line.
4495 * Returns allocated string, or NULL for end of function.
4496 */
4497 char_u *
4498get_func_line(
4499 int c UNUSED,
4500 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02004501 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004502 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004503{
4504 funccall_T *fcp = (funccall_T *)cookie;
4505 ufunc_T *fp = fcp->func;
4506 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004507 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004508
Bram Moolenaare38eab22019-12-05 21:50:01 +01004509 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004510 if (fcp->dbg_tick != debug_tick)
4511 {
4512 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004513 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004514 fcp->dbg_tick = debug_tick;
4515 }
4516#ifdef FEAT_PROFILE
4517 if (do_profiling == PROF_YES)
4518 func_line_end(cookie);
4519#endif
4520
4521 gap = &fp->uf_lines;
4522 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4523 || fcp->returned)
4524 retval = NULL;
4525 else
4526 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004527 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004528 while (fcp->linenr < gap->ga_len
4529 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
4530 ++fcp->linenr;
4531 if (fcp->linenr >= gap->ga_len)
4532 retval = NULL;
4533 else
4534 {
4535 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004536 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004537#ifdef FEAT_PROFILE
4538 if (do_profiling == PROF_YES)
4539 func_line_start(cookie);
4540#endif
4541 }
4542 }
4543
Bram Moolenaare38eab22019-12-05 21:50:01 +01004544 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004545 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004546 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004547 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004548 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004549 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004550 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004551 fcp->dbg_tick = debug_tick;
4552 }
4553
4554 return retval;
4555}
4556
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004557/*
4558 * Return TRUE if the currently active function should be ended, because a
4559 * return was encountered or an error occurred. Used inside a ":while".
4560 */
4561 int
4562func_has_ended(void *cookie)
4563{
4564 funccall_T *fcp = (funccall_T *)cookie;
4565
Bram Moolenaare38eab22019-12-05 21:50:01 +01004566 // Ignore the "abort" flag if the abortion behavior has been changed due to
4567 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004568 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4569 || fcp->returned);
4570}
4571
4572/*
4573 * return TRUE if cookie indicates a function which "abort"s on errors.
4574 */
4575 int
4576func_has_abort(
4577 void *cookie)
4578{
4579 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
4580}
4581
4582
4583/*
4584 * Turn "dict.Func" into a partial for "Func" bound to "dict".
4585 * Don't do this when "Func" is already a partial that was bound
4586 * explicitly (pt_auto is FALSE).
4587 * Changes "rettv" in-place.
4588 * Returns the updated "selfdict_in".
4589 */
4590 dict_T *
4591make_partial(dict_T *selfdict_in, typval_T *rettv)
4592{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004593 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004594 char_u *tofree = NULL;
4595 ufunc_T *fp;
4596 char_u fname_buf[FLEN_FIXED + 1];
4597 int error;
4598 dict_T *selfdict = selfdict_in;
4599
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004600 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
4601 fp = rettv->vval.v_partial->pt_func;
4602 else
4603 {
4604 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
4605 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004606 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004607 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004608 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004609 vim_free(tofree);
4610 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004611
4612 if (fp != NULL && (fp->uf_flags & FC_DICT))
4613 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004614 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004615
4616 if (pt != NULL)
4617 {
4618 pt->pt_refcount = 1;
4619 pt->pt_dict = selfdict;
4620 pt->pt_auto = TRUE;
4621 selfdict = NULL;
4622 if (rettv->v_type == VAR_FUNC)
4623 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004624 // Just a function: Take over the function name and use
4625 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004626 pt->pt_name = rettv->vval.v_string;
4627 }
4628 else
4629 {
4630 partial_T *ret_pt = rettv->vval.v_partial;
4631 int i;
4632
Bram Moolenaare38eab22019-12-05 21:50:01 +01004633 // Partial: copy the function name, use selfdict and copy
4634 // args. Can't take over name or args, the partial might
4635 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004636 if (ret_pt->pt_name != NULL)
4637 {
4638 pt->pt_name = vim_strsave(ret_pt->pt_name);
4639 func_ref(pt->pt_name);
4640 }
4641 else
4642 {
4643 pt->pt_func = ret_pt->pt_func;
4644 func_ptr_ref(pt->pt_func);
4645 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004646 if (ret_pt->pt_argc > 0)
4647 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004648 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004649 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004650 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004651 pt->pt_argc = 0;
4652 else
4653 {
4654 pt->pt_argc = ret_pt->pt_argc;
4655 for (i = 0; i < pt->pt_argc; i++)
4656 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
4657 }
4658 }
4659 partial_unref(ret_pt);
4660 }
4661 rettv->v_type = VAR_PARTIAL;
4662 rettv->vval.v_partial = pt;
4663 }
4664 }
4665 return selfdict;
4666}
4667
4668/*
4669 * Return the name of the executed function.
4670 */
4671 char_u *
4672func_name(void *cookie)
4673{
4674 return ((funccall_T *)cookie)->func->uf_name;
4675}
4676
4677/*
4678 * Return the address holding the next breakpoint line for a funccall cookie.
4679 */
4680 linenr_T *
4681func_breakpoint(void *cookie)
4682{
4683 return &((funccall_T *)cookie)->breakpoint;
4684}
4685
4686/*
4687 * Return the address holding the debug tick for a funccall cookie.
4688 */
4689 int *
4690func_dbg_tick(void *cookie)
4691{
4692 return &((funccall_T *)cookie)->dbg_tick;
4693}
4694
4695/*
4696 * Return the nesting level for a funccall cookie.
4697 */
4698 int
4699func_level(void *cookie)
4700{
4701 return ((funccall_T *)cookie)->level;
4702}
4703
4704/*
4705 * Return TRUE when a function was ended by a ":return" command.
4706 */
4707 int
4708current_func_returned(void)
4709{
4710 return current_funccal->returned;
4711}
4712
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004713 int
4714free_unref_funccal(int copyID, int testing)
4715{
4716 int did_free = FALSE;
4717 int did_free_funccal = FALSE;
4718 funccall_T *fc, **pfc;
4719
4720 for (pfc = &previous_funccal; *pfc != NULL; )
4721 {
4722 if (can_free_funccal(*pfc, copyID))
4723 {
4724 fc = *pfc;
4725 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004726 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004727 did_free = TRUE;
4728 did_free_funccal = TRUE;
4729 }
4730 else
4731 pfc = &(*pfc)->caller;
4732 }
4733 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004734 // When a funccal was freed some more items might be garbage
4735 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004736 (void)garbage_collect(testing);
4737
4738 return did_free;
4739}
4740
4741/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004742 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004743 */
4744 static funccall_T *
4745get_funccal(void)
4746{
4747 int i;
4748 funccall_T *funccal;
4749 funccall_T *temp_funccal;
4750
4751 funccal = current_funccal;
4752 if (debug_backtrace_level > 0)
4753 {
4754 for (i = 0; i < debug_backtrace_level; i++)
4755 {
4756 temp_funccal = funccal->caller;
4757 if (temp_funccal)
4758 funccal = temp_funccal;
4759 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004760 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004761 debug_backtrace_level = i;
4762 }
4763 }
4764 return funccal;
4765}
4766
4767/*
4768 * Return the hashtable used for local variables in the current funccal.
4769 * Return NULL if there is no current funccal.
4770 */
4771 hashtab_T *
4772get_funccal_local_ht()
4773{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004774 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004775 return NULL;
4776 return &get_funccal()->l_vars.dv_hashtab;
4777}
4778
4779/*
4780 * Return the l: scope variable.
4781 * Return NULL if there is no current funccal.
4782 */
4783 dictitem_T *
4784get_funccal_local_var()
4785{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004787 return NULL;
4788 return &get_funccal()->l_vars_var;
4789}
4790
4791/*
4792 * Return the hashtable used for argument in the current funccal.
4793 * Return NULL if there is no current funccal.
4794 */
4795 hashtab_T *
4796get_funccal_args_ht()
4797{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004799 return NULL;
4800 return &get_funccal()->l_avars.dv_hashtab;
4801}
4802
4803/*
4804 * Return the a: scope variable.
4805 * Return NULL if there is no current funccal.
4806 */
4807 dictitem_T *
4808get_funccal_args_var()
4809{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004810 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004811 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004812 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004813}
4814
4815/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004816 * List function variables, if there is a function.
4817 */
4818 void
4819list_func_vars(int *first)
4820{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004821 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004822 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004823 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004824}
4825
4826/*
4827 * If "ht" is the hashtable for local variables in the current funccal, return
4828 * the dict that contains it.
4829 * Otherwise return NULL.
4830 */
4831 dict_T *
4832get_current_funccal_dict(hashtab_T *ht)
4833{
4834 if (current_funccal != NULL
4835 && ht == &current_funccal->l_vars.dv_hashtab)
4836 return &current_funccal->l_vars;
4837 return NULL;
4838}
4839
4840/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004841 * Search hashitem in parent scope.
4842 */
4843 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004844find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004845{
4846 funccall_T *old_current_funccal = current_funccal;
4847 hashtab_T *ht;
4848 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004849 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004850
4851 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4852 return NULL;
4853
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004854 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004855 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004856 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004857 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004858 ht = find_var_ht(name, &varname);
4859 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004860 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004861 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004862 if (!HASHITEM_EMPTY(hi))
4863 {
4864 *pht = ht;
4865 break;
4866 }
4867 }
4868 if (current_funccal == current_funccal->func->uf_scoped)
4869 break;
4870 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004871 }
4872 current_funccal = old_current_funccal;
4873
4874 return hi;
4875}
4876
4877/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004878 * Search variable in parent scope.
4879 */
4880 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004881find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004882{
4883 dictitem_T *v = NULL;
4884 funccall_T *old_current_funccal = current_funccal;
4885 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004886 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004887
4888 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4889 return NULL;
4890
Bram Moolenaare38eab22019-12-05 21:50:01 +01004891 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004892 current_funccal = current_funccal->func->uf_scoped;
4893 while (current_funccal)
4894 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004895 ht = find_var_ht(name, &varname);
4896 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004897 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004898 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004899 if (v != NULL)
4900 break;
4901 }
4902 if (current_funccal == current_funccal->func->uf_scoped)
4903 break;
4904 current_funccal = current_funccal->func->uf_scoped;
4905 }
4906 current_funccal = old_current_funccal;
4907
4908 return v;
4909}
4910
4911/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004912 * Set "copyID + 1" in previous_funccal and callers.
4913 */
4914 int
4915set_ref_in_previous_funccal(int copyID)
4916{
4917 int abort = FALSE;
4918 funccall_T *fc;
4919
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004920 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004921 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004922 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004923 abort = abort
4924 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4925 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004926 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004927 }
4928 return abort;
4929}
4930
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004931 static int
4932set_ref_in_funccal(funccall_T *fc, int copyID)
4933{
4934 int abort = FALSE;
4935
4936 if (fc->fc_copyID != copyID)
4937 {
4938 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004939 abort = abort
4940 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4941 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004942 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004943 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004944 }
4945 return abort;
4946}
4947
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004948/*
4949 * Set "copyID" in all local vars and arguments in the call stack.
4950 */
4951 int
4952set_ref_in_call_stack(int copyID)
4953{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004954 int abort = FALSE;
4955 funccall_T *fc;
4956 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004957
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004958 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004959 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004960
4961 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004962 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
4963 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004964 abort = abort || set_ref_in_funccal(fc, copyID);
4965
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004966 return abort;
4967}
4968
4969/*
4970 * Set "copyID" in all functions available by name.
4971 */
4972 int
4973set_ref_in_functions(int copyID)
4974{
4975 int todo;
4976 hashitem_T *hi = NULL;
4977 int abort = FALSE;
4978 ufunc_T *fp;
4979
4980 todo = (int)func_hashtab.ht_used;
4981 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004982 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004983 if (!HASHITEM_EMPTY(hi))
4984 {
4985 --todo;
4986 fp = HI2UF(hi);
4987 if (!func_name_refcount(fp->uf_name))
4988 abort = abort || set_ref_in_func(NULL, fp, copyID);
4989 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004990 }
4991 return abort;
4992}
4993
4994/*
4995 * Set "copyID" in all function arguments.
4996 */
4997 int
4998set_ref_in_func_args(int copyID)
4999{
5000 int i;
5001 int abort = FALSE;
5002
5003 for (i = 0; i < funcargs.ga_len; ++i)
5004 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
5005 copyID, NULL, NULL);
5006 return abort;
5007}
5008
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005009/*
5010 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005011 * Returns TRUE if setting references failed somehow.
5012 */
5013 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005014set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005015{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005016 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005017 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01005018 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005019 char_u fname_buf[FLEN_FIXED + 1];
5020 char_u *tofree = NULL;
5021 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005022 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005023
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005024 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005025 return FALSE;
5026
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005027 if (fp_in == NULL)
5028 {
5029 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005030 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005031 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005032 if (fp != NULL)
5033 {
5034 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005035 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005036 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02005037
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005038 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005039 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005040}
5041
Bram Moolenaare38eab22019-12-05 21:50:01 +01005042#endif // FEAT_EVAL