comparison src/userfunc.c @ 19181:94eda51ba9ba v8.2.0149

patch 8.2.0149: maintaining a Vim9 branch separately is more work Commit: https://github.com/vim/vim/commit/8a7d6542b33e5d2b352262305c3bfdb2d14e1cf8 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Jan 26 15:56:19 2020 +0100 patch 8.2.0149: maintaining a Vim9 branch separately is more work Problem: Maintaining a Vim9 branch separately is more work. Solution: Merge the Vim9 script changes.
author Bram Moolenaar <Bram@vim.org>
date Sun, 26 Jan 2020 16:00:05 +0100
parents af1eca322b9e
children 1168c53d1b49
comparison
equal deleted inserted replaced
19180:8edf0aeb71b9 19181:94eda51ba9ba
20 #define FC_DICT 0x04 // Dict function, uses "self" 20 #define FC_DICT 0x04 // Dict function, uses "self"
21 #define FC_CLOSURE 0x08 // closure, uses outer scope variables 21 #define FC_CLOSURE 0x08 // closure, uses outer scope variables
22 #define FC_DELETED 0x10 // :delfunction used while uf_refcount > 0 22 #define FC_DELETED 0x10 // :delfunction used while uf_refcount > 0
23 #define FC_REMOVED 0x20 // function redefined while uf_refcount > 0 23 #define FC_REMOVED 0x20 // function redefined while uf_refcount > 0
24 #define FC_SANDBOX 0x40 // function defined in the sandbox 24 #define FC_SANDBOX 0x40 // function defined in the sandbox
25 25 #define FC_DEAD 0x80 // function kept only for reference to dfunc
26 #define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j] 26 #define FC_EXPORT 0x100 // "export def Func()"
27 27
28 /* 28 /*
29 * All user-defined functions are found in this hashtable. 29 * All user-defined functions are found in this hashtable.
30 */ 30 */
31 static hashtab_T func_hashtab; 31 static hashtab_T func_hashtab;
61 { 61 {
62 return &func_hashtab; 62 return &func_hashtab;
63 } 63 }
64 64
65 /* 65 /*
66 * Get one function argument and an optional type: "arg: type".
67 * Return a pointer to after the type.
68 * When something is wrong return "arg".
69 */
70 static char_u *
71 one_function_arg(char_u *arg, garray_T *newargs, garray_T *argtypes, int skip)
72 {
73 char_u *p = arg;
74
75 while (ASCII_ISALNUM(*p) || *p == '_')
76 ++p;
77 if (arg == p || isdigit(*arg)
78 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
79 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
80 {
81 if (!skip)
82 semsg(_("E125: Illegal argument: %s"), arg);
83 return arg;
84 }
85 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
86 return arg;
87 if (newargs != NULL)
88 {
89 char_u *arg_copy;
90 int c;
91 int i;
92
93 c = *p;
94 *p = NUL;
95 arg_copy = vim_strsave(arg);
96 if (arg_copy == NULL)
97 {
98 *p = c;
99 return arg;
100 }
101
102 // Check for duplicate argument name.
103 for (i = 0; i < newargs->ga_len; ++i)
104 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
105 {
106 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
107 vim_free(arg_copy);
108 return arg;
109 }
110 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
111 newargs->ga_len++;
112
113 *p = c;
114 }
115
116 // get any type from "arg: type"
117 if (argtypes != NULL && ga_grow(argtypes, 1) == OK)
118 {
119 char_u *type = NULL;
120
121 if (*p == ':')
122 {
123 type = skipwhite(p + 1);
124 p = skip_type(type);
125 type = vim_strnsave(type, p - type);
126 }
127 else if (*skipwhite(p) == ':')
128 emsg(_("E1059: No white space allowed before :"));
129 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
130 }
131
132 return p;
133 }
134
135 /*
66 * Get function arguments. 136 * Get function arguments.
67 */ 137 */
68 static int 138 int
69 get_function_args( 139 get_function_args(
70 char_u **argp, 140 char_u **argp,
71 char_u endchar, 141 char_u endchar,
72 garray_T *newargs, 142 garray_T *newargs,
143 garray_T *argtypes, // NULL unless using :def
73 int *varargs, 144 int *varargs,
74 garray_T *default_args, 145 garray_T *default_args,
75 int skip) 146 int skip)
76 { 147 {
77 int mustend = FALSE; 148 int mustend = FALSE;
78 char_u *arg = *argp; 149 char_u *arg = *argp;
79 char_u *p = arg; 150 char_u *p = arg;
80 int c; 151 int c;
81 int i;
82 int any_default = FALSE; 152 int any_default = FALSE;
83 char_u *expr; 153 char_u *expr;
84 154
85 if (newargs != NULL) 155 if (newargs != NULL)
86 ga_init2(newargs, (int)sizeof(char_u *), 3); 156 ga_init2(newargs, (int)sizeof(char_u *), 3);
157 if (argtypes != NULL)
158 ga_init2(argtypes, (int)sizeof(char_u *), 3);
87 if (default_args != NULL) 159 if (default_args != NULL)
88 ga_init2(default_args, (int)sizeof(char_u *), 3); 160 ga_init2(default_args, (int)sizeof(char_u *), 3);
89 161
90 if (varargs != NULL) 162 if (varargs != NULL)
91 *varargs = FALSE; 163 *varargs = FALSE;
99 { 171 {
100 if (varargs != NULL) 172 if (varargs != NULL)
101 *varargs = TRUE; 173 *varargs = TRUE;
102 p += 3; 174 p += 3;
103 mustend = TRUE; 175 mustend = TRUE;
176
177 if (argtypes != NULL)
178 {
179 // ...name: list<type>
180 if (!ASCII_ISALPHA(*p))
181 {
182 emsg(_("E1055: Missing name after ..."));
183 break;
184 }
185
186 arg = p;
187 p = one_function_arg(p, newargs, argtypes, skip);
188 if (p == arg)
189 break;
190 }
104 } 191 }
105 else 192 else
106 { 193 {
107 arg = p; 194 arg = p;
108 while (ASCII_ISALNUM(*p) || *p == '_') 195 p = one_function_arg(p, newargs, argtypes, skip);
109 ++p; 196 if (p == arg)
110 if (arg == p || isdigit(*arg)
111 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
112 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
113 {
114 if (!skip)
115 semsg(_("E125: Illegal argument: %s"), arg);
116 break; 197 break;
117 } 198
118 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
119 goto err_ret;
120 if (newargs != NULL)
121 {
122 c = *p;
123 *p = NUL;
124 arg = vim_strsave(arg);
125 if (arg == NULL)
126 {
127 *p = c;
128 goto err_ret;
129 }
130
131 // Check for duplicate argument name.
132 for (i = 0; i < newargs->ga_len; ++i)
133 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg) == 0)
134 {
135 semsg(_("E853: Duplicate argument name: %s"), arg);
136 vim_free(arg);
137 goto err_ret;
138 }
139 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg;
140 newargs->ga_len++;
141
142 *p = c;
143 }
144 if (*skipwhite(p) == '=' && default_args != NULL) 199 if (*skipwhite(p) == '=' && default_args != NULL)
145 { 200 {
146 typval_T rettv; 201 typval_T rettv;
147 202
148 any_default = TRUE; 203 any_default = TRUE;
264 319
265 ga_init(&newargs); 320 ga_init(&newargs);
266 ga_init(&newlines); 321 ga_init(&newlines);
267 322
268 // First, check if this is a lambda expression. "->" must exist. 323 // First, check if this is a lambda expression. "->" must exist.
269 ret = get_function_args(&start, '-', NULL, NULL, NULL, TRUE); 324 ret = get_function_args(&start, '-', NULL, NULL, NULL, NULL, TRUE);
270 if (ret == FAIL || *start != '>') 325 if (ret == FAIL || *start != '>')
271 return NOTDONE; 326 return NOTDONE;
272 327
273 // Parse the arguments again. 328 // Parse the arguments again.
274 if (evaluate) 329 if (evaluate)
275 pnewargs = &newargs; 330 pnewargs = &newargs;
276 else 331 else
277 pnewargs = NULL; 332 pnewargs = NULL;
278 *arg = skipwhite(*arg + 1); 333 *arg = skipwhite(*arg + 1);
279 ret = get_function_args(arg, '-', pnewargs, &varargs, NULL, FALSE); 334 // TODO: argument types
335 ret = get_function_args(arg, '-', pnewargs, NULL, &varargs, NULL, FALSE);
280 if (ret == FAIL || **arg != '>') 336 if (ret == FAIL || **arg != '>')
281 goto errret; 337 goto errret;
282 338
283 // Set up a flag for checking local variables and arguments. 339 // Set up a flag for checking local variables and arguments.
284 if (evaluate) 340 if (evaluate)
305 sprintf((char*)name, "<lambda>%d", ++lambda_no); 361 sprintf((char*)name, "<lambda>%d", ++lambda_no);
306 362
307 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1); 363 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
308 if (fp == NULL) 364 if (fp == NULL)
309 goto errret; 365 goto errret;
366 fp->uf_dfunc_idx = -1;
310 pt = ALLOC_CLEAR_ONE(partial_T); 367 pt = ALLOC_CLEAR_ONE(partial_T);
311 if (pt == NULL) 368 if (pt == NULL)
312 goto errret; 369 goto errret;
313 370
314 ga_init2(&newlines, (int)sizeof(char_u *), 1); 371 ga_init2(&newlines, (int)sizeof(char_u *), 1);
343 if (prof_def_func()) 400 if (prof_def_func())
344 func_do_profile(fp); 401 func_do_profile(fp);
345 #endif 402 #endif
346 if (sandbox) 403 if (sandbox)
347 flags |= FC_SANDBOX; 404 flags |= FC_SANDBOX;
405 // can be called with more args than uf_args.ga_len
348 fp->uf_varargs = TRUE; 406 fp->uf_varargs = TRUE;
349 fp->uf_flags = flags; 407 fp->uf_flags = flags;
350 fp->uf_calls = 0; 408 fp->uf_calls = 0;
351 fp->uf_script_ctx = current_sctx; 409 fp->uf_script_ctx = current_sctx;
352 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len; 410 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
579 fname = name; 637 fname = name;
580 return fname; 638 return fname;
581 } 639 }
582 640
583 /* 641 /*
642 * Find a function "name" in script "sid".
643 */
644 static ufunc_T *
645 find_func_with_sid(char_u *name, int sid)
646 {
647 hashitem_T *hi;
648 char_u buffer[200];
649
650 buffer[0] = K_SPECIAL;
651 buffer[1] = KS_EXTRA;
652 buffer[2] = (int)KE_SNR;
653 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
654 (long)sid, name);
655 hi = hash_find(&func_hashtab, buffer);
656 if (!HASHITEM_EMPTY(hi))
657 return HI2UF(hi);
658
659 return NULL;
660 }
661
662 /*
584 * Find a function by name, return pointer to it in ufuncs. 663 * Find a function by name, return pointer to it in ufuncs.
585 * Return NULL for unknown function. 664 * Return NULL for unknown function.
586 */ 665 */
587 ufunc_T * 666 static ufunc_T *
588 find_func(char_u *name) 667 find_func_even_dead(char_u *name, cctx_T *cctx)
589 { 668 {
590 hashitem_T *hi; 669 hashitem_T *hi;
670 ufunc_T *func;
671 imported_T *imported;
672
673 if (in_vim9script())
674 {
675 // Find script-local function before global one.
676 func = find_func_with_sid(name, current_sctx.sc_sid);
677 if (func != NULL)
678 return func;
679
680 // Find imported funcion before global one.
681 imported = find_imported(name, cctx);
682 if (imported != NULL && imported->imp_funcname != NULL)
683 {
684 hi = hash_find(&func_hashtab, imported->imp_funcname);
685 if (!HASHITEM_EMPTY(hi))
686 return HI2UF(hi);
687 }
688 }
591 689
592 hi = hash_find(&func_hashtab, name); 690 hi = hash_find(&func_hashtab, name);
593 if (!HASHITEM_EMPTY(hi)) 691 if (!HASHITEM_EMPTY(hi))
594 return HI2UF(hi); 692 return HI2UF(hi);
693
694 return NULL;
695 }
696
697 /*
698 * Find a function by name, return pointer to it in ufuncs.
699 * "cctx" is passed in a :def function to find imported functions.
700 * Return NULL for unknown or dead function.
701 */
702 ufunc_T *
703 find_func(char_u *name, cctx_T *cctx)
704 {
705 ufunc_T *fp = find_func_even_dead(name, cctx);
706
707 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
708 return fp;
595 return NULL; 709 return NULL;
596 } 710 }
597 711
598 /* 712 /*
599 * Copy the function name of "fp" to buffer "buf". 713 * Copy the function name of "fp" to buffer "buf".
759 made_copy = 0; 873 made_copy = 0;
760 want_garbage_collect = TRUE; 874 want_garbage_collect = TRUE;
761 } 875 }
762 } 876 }
763 } 877 }
878 /*
879 * Unreference "fc": decrement the reference count and free it when it
880 * becomes zero. "fp" is detached from "fc".
881 * When "force" is TRUE we are exiting.
882 */
883 static void
884 funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
885 {
886 funccall_T **pfc;
887 int i;
888
889 if (fc == NULL)
890 return;
891
892 if (--fc->fc_refcount <= 0 && (force || (
893 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
894 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
895 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
896 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
897 {
898 if (fc == *pfc)
899 {
900 *pfc = fc->caller;
901 free_funccal_contents(fc);
902 return;
903 }
904 }
905 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
906 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
907 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
908 }
909
910 /*
911 * Remove the function from the function hashtable. If the function was
912 * deleted while it still has references this was already done.
913 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
914 */
915 static int
916 func_remove(ufunc_T *fp)
917 {
918 hashitem_T *hi;
919
920 // Return if it was already virtually deleted.
921 if (fp->uf_flags & FC_DEAD)
922 return FALSE;
923
924 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
925 if (!HASHITEM_EMPTY(hi))
926 {
927 // When there is a def-function index do not actually remove the
928 // function, so we can find the index when defining the function again.
929 if (fp->uf_dfunc_idx >= 0)
930 fp->uf_flags |= FC_DEAD;
931 else
932 hash_remove(&func_hashtab, hi);
933 return TRUE;
934 }
935 return FALSE;
936 }
937
938 static void
939 func_clear_items(ufunc_T *fp)
940 {
941 ga_clear_strings(&(fp->uf_args));
942 ga_clear_strings(&(fp->uf_def_args));
943 ga_clear_strings(&(fp->uf_lines));
944 VIM_CLEAR(fp->uf_name_exp);
945 VIM_CLEAR(fp->uf_arg_types);
946 ga_clear(&fp->uf_type_list);
947 #ifdef FEAT_PROFILE
948 VIM_CLEAR(fp->uf_tml_count);
949 VIM_CLEAR(fp->uf_tml_total);
950 VIM_CLEAR(fp->uf_tml_self);
951 #endif
952 }
953
954 /*
955 * Free all things that a function contains. Does not free the function
956 * itself, use func_free() for that.
957 * When "force" is TRUE we are exiting.
958 */
959 static void
960 func_clear(ufunc_T *fp, int force)
961 {
962 if (fp->uf_cleared)
963 return;
964 fp->uf_cleared = TRUE;
965
966 // clear this function
967 func_clear_items(fp);
968 funccal_unref(fp->uf_scoped, fp, force);
969 delete_def_function(fp);
970 }
971
972 /*
973 * Free a function and remove it from the list of functions. Does not free
974 * what a function contains, call func_clear() first.
975 */
976 static void
977 func_free(ufunc_T *fp)
978 {
979 // Only remove it when not done already, otherwise we would remove a newer
980 // version of the function with the same name.
981 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
982 func_remove(fp);
983
984 if ((fp->uf_flags & FC_DEAD) == 0)
985 vim_free(fp);
986 }
987
988 /*
989 * Free all things that a function contains and free the function itself.
990 * When "force" is TRUE we are exiting.
991 */
992 static void
993 func_clear_free(ufunc_T *fp, int force)
994 {
995 func_clear(fp, force);
996 func_free(fp);
997 }
998
764 999
765 /* 1000 /*
766 * Call a user function. 1001 * Call a user function.
767 */ 1002 */
768 static void 1003 static void
819 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0); 1054 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
820 fc->dbg_tick = debug_tick; 1055 fc->dbg_tick = debug_tick;
821 // Set up fields for closure. 1056 // Set up fields for closure.
822 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1); 1057 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
823 func_ptr_ref(fp); 1058 func_ptr_ref(fp);
1059
1060 if (fp->uf_dfunc_idx >= 0)
1061 {
1062 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
1063
1064 // Execute the compiled function.
1065 call_def_function(fp, argcount, argvars, rettv);
1066 --depth;
1067 current_funccal = fc->caller;
1068
1069 estack_pop();
1070 free_funccal(fc);
1071 return;
1072 }
824 1073
825 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0) 1074 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
826 islambda = TRUE; 1075 islambda = TRUE;
827 1076
828 /* 1077 /*
1144 1393
1145 cleanup_function_call(fc); 1394 cleanup_function_call(fc);
1146 } 1395 }
1147 1396
1148 /* 1397 /*
1149 * Unreference "fc": decrement the reference count and free it when it 1398 * Call a user function after checking the arguments.
1150 * becomes zero. "fp" is detached from "fc". 1399 */
1151 * When "force" is TRUE we are exiting. 1400 int
1152 */ 1401 call_user_func_check(
1153 static void 1402 ufunc_T *fp,
1154 funccal_unref(funccall_T *fc, ufunc_T *fp, int force) 1403 int argcount,
1155 { 1404 typval_T *argvars,
1156 funccall_T **pfc; 1405 typval_T *rettv,
1157 int i; 1406 funcexe_T *funcexe,
1158 1407 dict_T *selfdict)
1159 if (fc == NULL) 1408 {
1160 return; 1409 int error;
1161 1410 int regular_args = fp->uf_args.ga_len;
1162 if (--fc->fc_refcount <= 0 && (force || ( 1411
1163 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT 1412 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1164 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT 1413 *funcexe->doesrange = TRUE;
1165 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT))) 1414 if (argcount < regular_args - fp->uf_def_args.ga_len)
1166 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller) 1415 error = FCERR_TOOFEW;
1167 { 1416 else if (!has_varargs(fp) && argcount > regular_args)
1168 if (fc == *pfc) 1417 error = FCERR_TOOMANY;
1169 { 1418 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1170 *pfc = fc->caller; 1419 error = FCERR_DICT;
1171 free_funccal_contents(fc); 1420 else
1172 return; 1421 {
1173 } 1422 int did_save_redo = FALSE;
1174 } 1423 save_redo_T save_redo;
1175 for (i = 0; i < fc->fc_funcs.ga_len; ++i) 1424
1176 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp) 1425 /*
1177 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL; 1426 * Call the user function.
1178 } 1427 * Save and restore search patterns, script variables and
1179 1428 * redo buffer.
1180 /* 1429 */
1181 * Remove the function from the function hashtable. If the function was 1430 save_search_patterns();
1182 * deleted while it still has references this was already done. 1431 if (!ins_compl_active())
1183 * Return TRUE if the entry was deleted, FALSE if it wasn't found. 1432 {
1184 */ 1433 saveRedobuff(&save_redo);
1185 static int 1434 did_save_redo = TRUE;
1186 func_remove(ufunc_T *fp) 1435 }
1187 { 1436 ++fp->uf_calls;
1188 hashitem_T *hi = hash_find(&func_hashtab, UF2HIKEY(fp)); 1437 call_user_func(fp, argcount, argvars, rettv,
1189 1438 funcexe->firstline, funcexe->lastline,
1190 if (!HASHITEM_EMPTY(hi)) 1439 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
1191 { 1440 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1192 hash_remove(&func_hashtab, hi); 1441 // Function was unreferenced while being used, free it now.
1193 return TRUE; 1442 func_clear_free(fp, FALSE);
1194 } 1443 if (did_save_redo)
1195 return FALSE; 1444 restoreRedobuff(&save_redo);
1196 } 1445 restore_search_patterns();
1197 1446 error = FCERR_NONE;
1198 static void 1447 }
1199 func_clear_items(ufunc_T *fp) 1448 return error;
1200 {
1201 ga_clear_strings(&(fp->uf_args));
1202 ga_clear_strings(&(fp->uf_def_args));
1203 ga_clear_strings(&(fp->uf_lines));
1204 VIM_CLEAR(fp->uf_name_exp);
1205 #ifdef FEAT_PROFILE
1206 VIM_CLEAR(fp->uf_tml_count);
1207 VIM_CLEAR(fp->uf_tml_total);
1208 VIM_CLEAR(fp->uf_tml_self);
1209 #endif
1210 }
1211
1212 /*
1213 * Free all things that a function contains. Does not free the function
1214 * itself, use func_free() for that.
1215 * When "force" is TRUE we are exiting.
1216 */
1217 static void
1218 func_clear(ufunc_T *fp, int force)
1219 {
1220 if (fp->uf_cleared)
1221 return;
1222 fp->uf_cleared = TRUE;
1223
1224 // clear this function
1225 func_clear_items(fp);
1226 funccal_unref(fp->uf_scoped, fp, force);
1227 }
1228
1229 /*
1230 * Free a function and remove it from the list of functions. Does not free
1231 * what a function contains, call func_clear() first.
1232 */
1233 static void
1234 func_free(ufunc_T *fp)
1235 {
1236 // only remove it when not done already, otherwise we would remove a newer
1237 // version of the function
1238 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1239 func_remove(fp);
1240
1241 vim_free(fp);
1242 }
1243
1244 /*
1245 * Free all things that a function contains and free the function itself.
1246 * When "force" is TRUE we are exiting.
1247 */
1248 static void
1249 func_clear_free(ufunc_T *fp, int force)
1250 {
1251 func_clear(fp, force);
1252 func_free(fp);
1253 } 1449 }
1254 1450
1255 /* 1451 /*
1256 * There are two kinds of function names: 1452 * There are two kinds of function names:
1257 * 1. ordinary names, function defined with :function 1453 * 1. ordinary names, function defined with :function
1325 { 1521 {
1326 todo = func_hashtab.ht_used; 1522 todo = func_hashtab.ht_used;
1327 for (hi = func_hashtab.ht_array; todo > 0; ++hi) 1523 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1328 if (!HASHITEM_EMPTY(hi)) 1524 if (!HASHITEM_EMPTY(hi))
1329 { 1525 {
1526 // clear the def function index now
1527 fp = HI2UF(hi);
1528 fp->uf_flags &= ~FC_DEAD;
1529 fp->uf_dfunc_idx = -1;
1530
1330 // Only free functions that are not refcounted, those are 1531 // Only free functions that are not refcounted, those are
1331 // supposed to be freed when no longer referenced. 1532 // supposed to be freed when no longer referenced.
1332 fp = HI2UF(hi);
1333 if (func_name_refcount(fp->uf_name)) 1533 if (func_name_refcount(fp->uf_name))
1334 ++skipped; 1534 ++skipped;
1335 else 1535 else
1336 { 1536 {
1337 used = func_hashtab.ht_used; 1537 used = func_hashtab.ht_used;
1369 } 1569 }
1370 } 1570 }
1371 } 1571 }
1372 if (skipped == 0) 1572 if (skipped == 0)
1373 hash_clear(&func_hashtab); 1573 hash_clear(&func_hashtab);
1574
1575 free_def_functions();
1374 } 1576 }
1375 #endif 1577 #endif
1376 1578
1377 /* 1579 /*
1378 * Return TRUE if "name" looks like a builtin function name: starts with a 1580 * Return TRUE if "name" looks like a builtin function name: starts with a
1379 * lower case letter and doesn't contain AUTOLOAD_CHAR. 1581 * lower case letter and doesn't contain AUTOLOAD_CHAR.
1380 * "len" is the length of "name", or -1 for NUL terminated. 1582 * "len" is the length of "name", or -1 for NUL terminated.
1381 */ 1583 */
1382 static int 1584 int
1383 builtin_function(char_u *name, int len) 1585 builtin_function(char_u *name, int len)
1384 { 1586 {
1385 char_u *p; 1587 char_u *p;
1386 1588
1387 if (!ASCII_ISLOWER(name[0])) 1589 if (!ASCII_ISLOWER(name[0]))
1464 funcexe.partial = callback->cb_partial; 1666 funcexe.partial = callback->cb_partial;
1465 ++callback_depth; 1667 ++callback_depth;
1466 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe); 1668 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1467 --callback_depth; 1669 --callback_depth;
1468 return ret; 1670 return ret;
1671 }
1672
1673 /*
1674 * Give an error message for the result of a function.
1675 * Nothing if "error" is FCERR_NONE.
1676 */
1677 void
1678 user_func_error(int error, char_u *name)
1679 {
1680 switch (error)
1681 {
1682 case FCERR_UNKNOWN:
1683 emsg_funcname(e_unknownfunc, name);
1684 break;
1685 case FCERR_NOTMETHOD:
1686 emsg_funcname(
1687 N_("E276: Cannot use function as a method: %s"), name);
1688 break;
1689 case FCERR_DELETED:
1690 emsg_funcname(N_(e_func_deleted), name);
1691 break;
1692 case FCERR_TOOMANY:
1693 emsg_funcname((char *)e_toomanyarg, name);
1694 break;
1695 case FCERR_TOOFEW:
1696 emsg_funcname((char *)e_toofewarg, name);
1697 break;
1698 case FCERR_SCRIPT:
1699 emsg_funcname(
1700 N_("E120: Using <SID> not in a script context: %s"), name);
1701 break;
1702 case FCERR_DICT:
1703 emsg_funcname(
1704 N_("E725: Calling dict function without Dictionary: %s"),
1705 name);
1706 break;
1707 }
1469 } 1708 }
1470 1709
1471 /* 1710 /*
1472 * Call a function with its resolved parameters 1711 * Call a function with its resolved parameters
1473 * 1712 *
1559 * User defined function. 1798 * User defined function.
1560 */ 1799 */
1561 if (partial != NULL && partial->pt_func != NULL) 1800 if (partial != NULL && partial->pt_func != NULL)
1562 fp = partial->pt_func; 1801 fp = partial->pt_func;
1563 else 1802 else
1564 fp = find_func(rfname); 1803 fp = find_func(rfname, NULL);
1565 1804
1566 // Trigger FuncUndefined event, may load the function. 1805 // Trigger FuncUndefined event, may load the function.
1567 if (fp == NULL 1806 if (fp == NULL
1568 && apply_autocmds(EVENT_FUNCUNDEFINED, 1807 && apply_autocmds(EVENT_FUNCUNDEFINED,
1569 rfname, rfname, TRUE, NULL) 1808 rfname, rfname, TRUE, NULL)
1570 && !aborting()) 1809 && !aborting())
1571 { 1810 {
1572 // executed an autocommand, search for the function again 1811 // executed an autocommand, search for the function again
1573 fp = find_func(rfname); 1812 fp = find_func(rfname, NULL);
1574 } 1813 }
1575 // Try loading a package. 1814 // Try loading a package.
1576 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting()) 1815 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1577 { 1816 {
1578 // loaded a package, search for the function again 1817 // loaded a package, search for the function again
1579 fp = find_func(rfname); 1818 fp = find_func(rfname, NULL);
1580 } 1819 }
1581 1820
1582 if (fp != NULL && (fp->uf_flags & FC_DELETED)) 1821 if (fp != NULL && (fp->uf_flags & FC_DELETED))
1583 error = FCERR_DELETED; 1822 error = FCERR_DELETED;
1584 else if (fp != NULL) 1823 else if (fp != NULL)
1596 argcount++; 1835 argcount++;
1597 argvars = argv; 1836 argvars = argv;
1598 argv_base = 1; 1837 argv_base = 1;
1599 } 1838 }
1600 1839
1601 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL) 1840 error = call_user_func_check(fp, argcount, argvars, rettv,
1602 *funcexe->doesrange = TRUE; 1841 funcexe, selfdict);
1603 if (argcount < fp->uf_args.ga_len - fp->uf_def_args.ga_len)
1604 error = FCERR_TOOFEW;
1605 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
1606 error = FCERR_TOOMANY;
1607 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1608 error = FCERR_DICT;
1609 else
1610 {
1611 int did_save_redo = FALSE;
1612 save_redo_T save_redo;
1613
1614 /*
1615 * Call the user function.
1616 * Save and restore search patterns, script variables and
1617 * redo buffer.
1618 */
1619 save_search_patterns();
1620 if (!ins_compl_active())
1621 {
1622 saveRedobuff(&save_redo);
1623 did_save_redo = TRUE;
1624 }
1625 ++fp->uf_calls;
1626 call_user_func(fp, argcount, argvars, rettv,
1627 funcexe->firstline, funcexe->lastline,
1628 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
1629 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1630 // Function was unreferenced while being used, free it
1631 // now.
1632 func_clear_free(fp, FALSE);
1633 if (did_save_redo)
1634 restoreRedobuff(&save_redo);
1635 restore_search_patterns();
1636 error = FCERR_NONE;
1637 }
1638 } 1842 }
1639 } 1843 }
1640 else if (funcexe->basetv != NULL) 1844 else if (funcexe->basetv != NULL)
1641 { 1845 {
1642 /* 1846 /*
1673 * Report an error unless the argument evaluation or function call has been 1877 * Report an error unless the argument evaluation or function call has been
1674 * cancelled due to an aborting error, an interrupt, or an exception. 1878 * cancelled due to an aborting error, an interrupt, or an exception.
1675 */ 1879 */
1676 if (!aborting()) 1880 if (!aborting())
1677 { 1881 {
1678 switch (error) 1882 user_func_error(error, name);
1679 {
1680 case FCERR_UNKNOWN:
1681 emsg_funcname(N_("E117: Unknown function: %s"), name);
1682 break;
1683 case FCERR_NOTMETHOD:
1684 emsg_funcname(
1685 N_("E276: Cannot use function as a method: %s"),
1686 name);
1687 break;
1688 case FCERR_DELETED:
1689 emsg_funcname(N_("E933: Function was deleted: %s"), name);
1690 break;
1691 case FCERR_TOOMANY:
1692 emsg_funcname((char *)e_toomanyarg, name);
1693 break;
1694 case FCERR_TOOFEW:
1695 emsg_funcname(
1696 N_("E119: Not enough arguments for function: %s"),
1697 name);
1698 break;
1699 case FCERR_SCRIPT:
1700 emsg_funcname(
1701 N_("E120: Using <SID> not in a script context: %s"),
1702 name);
1703 break;
1704 case FCERR_DICT:
1705 emsg_funcname(
1706 N_("E725: Calling dict function without Dictionary: %s"),
1707 name);
1708 break;
1709 }
1710 } 1883 }
1711 1884
1712 // clear the copies made from the partial 1885 // clear the copies made from the partial
1713 while (argv_clear > 0) 1886 while (argv_clear > 0)
1714 clear_tv(&argv[--argv_clear + argv_base]); 1887 clear_tv(&argv[--argv_clear + argv_base]);
1715 1888
1716 vim_free(tofree); 1889 vim_free(tofree);
1717 vim_free(name); 1890 vim_free(name);
1718 1891
1719 return ret; 1892 return ret;
1893 }
1894
1895 static char_u *
1896 printable_func_name(ufunc_T *fp)
1897 {
1898 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
1720 } 1899 }
1721 1900
1722 /* 1901 /*
1723 * List the head of the function: "name(arg1, arg2)". 1902 * List the head of the function: "name(arg1, arg2)".
1724 */ 1903 */
1729 1908
1730 msg_start(); 1909 msg_start();
1731 if (indent) 1910 if (indent)
1732 msg_puts(" "); 1911 msg_puts(" ");
1733 msg_puts("function "); 1912 msg_puts("function ");
1734 if (fp->uf_name_exp != NULL) 1913 msg_puts((char *)printable_func_name(fp));
1735 msg_puts((char *)fp->uf_name_exp);
1736 else
1737 msg_puts((char *)fp->uf_name);
1738 msg_putchar('('); 1914 msg_putchar('(');
1739 for (j = 0; j < fp->uf_args.ga_len; ++j) 1915 for (j = 0; j < fp->uf_args.ga_len; ++j)
1740 { 1916 {
1741 if (j) 1917 if (j)
1742 msg_puts(", "); 1918 msg_puts(", ");
1743 msg_puts((char *)FUNCARG(fp, j)); 1919 msg_puts((char *)FUNCARG(fp, j));
1920 if (fp->uf_arg_types != NULL)
1921 {
1922 char *tofree;
1923
1924 msg_puts(": ");
1925 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
1926 vim_free(tofree);
1927 }
1744 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len) 1928 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
1745 { 1929 {
1746 msg_puts(" = "); 1930 msg_puts(" = ");
1747 msg_puts(((char **)(fp->uf_def_args.ga_data)) 1931 msg_puts(((char **)(fp->uf_def_args.ga_data))
1748 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]); 1932 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
1751 if (fp->uf_varargs) 1935 if (fp->uf_varargs)
1752 { 1936 {
1753 if (j) 1937 if (j)
1754 msg_puts(", "); 1938 msg_puts(", ");
1755 msg_puts("..."); 1939 msg_puts("...");
1940 }
1941 if (fp->uf_va_name != NULL)
1942 {
1943 if (j)
1944 msg_puts(", ");
1945 msg_puts("...");
1946 msg_puts((char *)fp->uf_va_name);
1947 if (fp->uf_va_type)
1948 {
1949 char *tofree;
1950
1951 msg_puts(": ");
1952 msg_puts(type_name(fp->uf_va_type, &tofree));
1953 vim_free(tofree);
1954 }
1756 } 1955 }
1757 msg_putchar(')'); 1956 msg_putchar(')');
1758 if (fp->uf_flags & FC_ABORT) 1957 if (fp->uf_flags & FC_ABORT)
1759 msg_puts(" abort"); 1958 msg_puts(" abort");
1760 if (fp->uf_flags & FC_RANGE) 1959 if (fp->uf_flags & FC_RANGE)
1791 char_u *start; 1990 char_u *start;
1792 char_u *end; 1991 char_u *end;
1793 int lead; 1992 int lead;
1794 char_u sid_buf[20]; 1993 char_u sid_buf[20];
1795 int len; 1994 int len;
1995 int extra = 0;
1796 lval_T lv; 1996 lval_T lv;
1997 int vim9script;
1797 1998
1798 if (fdp != NULL) 1999 if (fdp != NULL)
1799 vim_memset(fdp, 0, sizeof(funcdict_T)); 2000 vim_memset(fdp, 0, sizeof(funcdict_T));
1800 start = *pp; 2001 start = *pp;
1801 2002
1931 // skip over "s:" and "g:" 2132 // skip over "s:" and "g:"
1932 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':')) 2133 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
1933 lv.ll_name += 2; 2134 lv.ll_name += 2;
1934 len = (int)(end - lv.ll_name); 2135 len = (int)(end - lv.ll_name);
1935 } 2136 }
2137
2138 // In Vim9 script a user function is script-local by default.
2139 vim9script = ASCII_ISUPPER(*start)
2140 && current_sctx.sc_version == SCRIPT_VERSION_VIM9;
1936 2141
1937 /* 2142 /*
1938 * Copy the function name to allocated memory. 2143 * Copy the function name to allocated memory.
1939 * Accept <SID>name() inside a script, translate into <SNR>123_name(). 2144 * Accept <SID>name() inside a script, translate into <SNR>123_name().
1940 * Accept <SNR>123_name() outside a script. 2145 * Accept <SNR>123_name() outside a script.
1941 */ 2146 */
1942 if (skip) 2147 if (skip)
1943 lead = 0; // do nothing 2148 lead = 0; // do nothing
1944 else if (lead > 0) 2149 else if (lead > 0 || vim9script)
1945 { 2150 {
1946 lead = 3; 2151 if (!vim9script)
1947 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name)) 2152 lead = 3;
2153 if (vim9script || (lv.ll_exp_name != NULL
2154 && eval_fname_sid(lv.ll_exp_name))
1948 || eval_fname_sid(*pp)) 2155 || eval_fname_sid(*pp))
1949 { 2156 {
1950 // It's "s:" or "<SID>" 2157 // It's script-local, "s:" or "<SID>"
1951 if (current_sctx.sc_sid <= 0) 2158 if (current_sctx.sc_sid <= 0)
1952 { 2159 {
1953 emsg(_(e_usingsid)); 2160 emsg(_(e_usingsid));
1954 goto theend; 2161 goto theend;
1955 } 2162 }
1956 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid); 2163 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
1957 lead += (int)STRLEN(sid_buf); 2164 if (vim9script)
2165 extra = 3 + (int)STRLEN(sid_buf);
2166 else
2167 lead += (int)STRLEN(sid_buf);
1958 } 2168 }
1959 } 2169 }
1960 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len)) 2170 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
1961 { 2171 {
1962 semsg(_("E128: Function name must start with a capital or \"s:\": %s"), 2172 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
1972 semsg(_("E884: Function name cannot contain a colon: %s"), start); 2182 semsg(_("E884: Function name cannot contain a colon: %s"), start);
1973 goto theend; 2183 goto theend;
1974 } 2184 }
1975 } 2185 }
1976 2186
1977 name = alloc(len + lead + 1); 2187 name = alloc(len + lead + extra + 1);
1978 if (name != NULL) 2188 if (name != NULL)
1979 { 2189 {
1980 if (lead > 0) 2190 if (lead > 0 || vim9script)
1981 { 2191 {
1982 name[0] = K_SPECIAL; 2192 name[0] = K_SPECIAL;
1983 name[1] = KS_EXTRA; 2193 name[1] = KS_EXTRA;
1984 name[2] = (int)KE_SNR; 2194 name[2] = (int)KE_SNR;
1985 if (lead > 3) // If it's "<SID>" 2195 if (vim9script || lead > 3) // If it's "<SID>"
1986 STRCPY(name + 3, sid_buf); 2196 STRCPY(name + 3, sid_buf);
1987 } 2197 }
1988 mch_memmove(name + lead, lv.ll_name, (size_t)len); 2198 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
1989 name[lead + len] = NUL; 2199 name[lead + extra + len] = NUL;
1990 } 2200 }
1991 *pp = end; 2201 *pp = end;
1992 2202
1993 theend: 2203 theend:
1994 clear_lval(&lv); 2204 clear_lval(&lv);
2010 char_u *name = NULL; 2220 char_u *name = NULL;
2011 char_u *p; 2221 char_u *p;
2012 char_u *arg; 2222 char_u *arg;
2013 char_u *line_arg = NULL; 2223 char_u *line_arg = NULL;
2014 garray_T newargs; 2224 garray_T newargs;
2225 garray_T argtypes;
2015 garray_T default_args; 2226 garray_T default_args;
2016 garray_T newlines; 2227 garray_T newlines;
2017 int varargs = FALSE; 2228 int varargs = FALSE;
2018 int flags = 0; 2229 int flags = 0;
2230 char_u *ret_type = NULL;
2019 ufunc_T *fp; 2231 ufunc_T *fp;
2020 int overwrite = FALSE; 2232 int overwrite = FALSE;
2021 int indent; 2233 int indent;
2022 int nesting; 2234 int nesting;
2235 #define MAX_FUNC_NESTING 50
2236 char nesting_def[MAX_FUNC_NESTING];
2023 dictitem_T *v; 2237 dictitem_T *v;
2024 funcdict_T fudi; 2238 funcdict_T fudi;
2025 static int func_nr = 0; // number for nameless function 2239 static int func_nr = 0; // number for nameless function
2026 int paren; 2240 int paren;
2027 hashtab_T *ht;
2028 int todo; 2241 int todo;
2029 hashitem_T *hi; 2242 hashitem_T *hi;
2030 int do_concat = TRUE; 2243 int do_concat = TRUE;
2031 linenr_T sourcing_lnum_off; 2244 linenr_T sourcing_lnum_off;
2032 linenr_T sourcing_lnum_top; 2245 linenr_T sourcing_lnum_top;
2046 { 2259 {
2047 if (!HASHITEM_EMPTY(hi)) 2260 if (!HASHITEM_EMPTY(hi))
2048 { 2261 {
2049 --todo; 2262 --todo;
2050 fp = HI2UF(hi); 2263 fp = HI2UF(hi);
2051 if (message_filtered(fp->uf_name)) 2264 if ((fp->uf_flags & FC_DEAD)
2265 || message_filtered(fp->uf_name))
2052 continue; 2266 continue;
2053 if (!func_name_refcount(fp->uf_name)) 2267 if (!func_name_refcount(fp->uf_name))
2054 list_func_head(fp, FALSE); 2268 list_func_head(fp, FALSE);
2055 } 2269 }
2056 } 2270 }
2082 { 2296 {
2083 if (!HASHITEM_EMPTY(hi)) 2297 if (!HASHITEM_EMPTY(hi))
2084 { 2298 {
2085 --todo; 2299 --todo;
2086 fp = HI2UF(hi); 2300 fp = HI2UF(hi);
2087 if (!isdigit(*fp->uf_name) 2301 if ((fp->uf_flags & FC_DEAD) == 0
2088 && vim_regexec(&regmatch, fp->uf_name, 0)) 2302 && !isdigit(*fp->uf_name)
2303 && vim_regexec(&regmatch, fp->uf_name, 0))
2089 list_func_head(fp, FALSE); 2304 list_func_head(fp, FALSE);
2090 } 2305 }
2091 } 2306 }
2092 vim_regfree(regmatch.regprog); 2307 vim_regfree(regmatch.regprog);
2093 } 2308 }
2095 if (*p == '/') 2310 if (*p == '/')
2096 ++p; 2311 ++p;
2097 eap->nextcmd = check_nextcmd(p); 2312 eap->nextcmd = check_nextcmd(p);
2098 return; 2313 return;
2099 } 2314 }
2315
2316 ga_init(&newargs);
2317 ga_init(&argtypes);
2318 ga_init(&default_args);
2100 2319
2101 /* 2320 /*
2102 * Get the function name. There are these situations: 2321 * Get the function name. There are these situations:
2103 * func normal function name 2322 * func normal function name
2104 * "name" == func, "fudi.fd_dict" == NULL 2323 * "name" == func, "fudi.fd_dict" == NULL
2153 eap->nextcmd = check_nextcmd(p); 2372 eap->nextcmd = check_nextcmd(p);
2154 if (eap->nextcmd != NULL) 2373 if (eap->nextcmd != NULL)
2155 *p = NUL; 2374 *p = NUL;
2156 if (!eap->skip && !got_int) 2375 if (!eap->skip && !got_int)
2157 { 2376 {
2158 fp = find_func(name); 2377 fp = find_func(name, NULL);
2159 if (fp != NULL) 2378 if (fp != NULL)
2160 { 2379 {
2161 list_func_head(fp, TRUE); 2380 list_func_head(fp, TRUE);
2162 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j) 2381 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2163 { 2382 {
2174 ui_breakcheck(); 2393 ui_breakcheck();
2175 } 2394 }
2176 if (!got_int) 2395 if (!got_int)
2177 { 2396 {
2178 msg_putchar('\n'); 2397 msg_putchar('\n');
2179 msg_puts(" endfunction"); 2398 if (fp->uf_dfunc_idx >= 0)
2399 msg_puts(" enddef");
2400 else
2401 msg_puts(" endfunction");
2180 } 2402 }
2181 } 2403 }
2182 else 2404 else
2183 emsg_funcname(N_("E123: Undefined function: %s"), name); 2405 emsg_funcname(N_("E123: Undefined function: %s"), name);
2184 } 2406 }
2229 // Disallow using the g: dict. 2451 // Disallow using the g: dict.
2230 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE) 2452 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
2231 emsg(_("E862: Cannot use g: here")); 2453 emsg(_("E862: Cannot use g: here"));
2232 } 2454 }
2233 2455
2234 if (get_function_args(&p, ')', &newargs, &varargs, 2456 if (get_function_args(&p, ')', &newargs,
2235 &default_args, eap->skip) == FAIL) 2457 eap->cmdidx == CMD_def ? &argtypes : NULL,
2458 &varargs, &default_args, eap->skip) == FAIL)
2236 goto errret_2; 2459 goto errret_2;
2237 2460
2238 // find extra arguments "range", "dict", "abort" and "closure" 2461 if (eap->cmdidx == CMD_def)
2239 for (;;) 2462 {
2240 { 2463 // find the return type: :def Func(): type
2241 p = skipwhite(p); 2464 if (*p == ':')
2242 if (STRNCMP(p, "range", 5) == 0) 2465 {
2243 { 2466 ret_type = skipwhite(p + 1);
2244 flags |= FC_RANGE; 2467 p = skip_type(ret_type);
2245 p += 5; 2468 if (p > ret_type)
2246 } 2469 p = skipwhite(p);
2247 else if (STRNCMP(p, "dict", 4) == 0) 2470 else
2248 { 2471 semsg(_("E1056: expected a type: %s"), ret_type);
2249 flags |= FC_DICT; 2472 }
2250 p += 4; 2473 }
2251 } 2474 else
2252 else if (STRNCMP(p, "abort", 5) == 0) 2475 // find extra arguments "range", "dict", "abort" and "closure"
2253 { 2476 for (;;)
2254 flags |= FC_ABORT; 2477 {
2255 p += 5; 2478 p = skipwhite(p);
2256 } 2479 if (STRNCMP(p, "range", 5) == 0)
2257 else if (STRNCMP(p, "closure", 7) == 0) 2480 {
2258 { 2481 flags |= FC_RANGE;
2259 flags |= FC_CLOSURE; 2482 p += 5;
2260 p += 7; 2483 }
2261 if (current_funccal == NULL) 2484 else if (STRNCMP(p, "dict", 4) == 0)
2262 { 2485 {
2263 emsg_funcname(N_("E932: Closure function should not be at top level: %s"), 2486 flags |= FC_DICT;
2264 name == NULL ? (char_u *)"" : name); 2487 p += 4;
2265 goto erret; 2488 }
2266 } 2489 else if (STRNCMP(p, "abort", 5) == 0)
2267 } 2490 {
2268 else 2491 flags |= FC_ABORT;
2269 break; 2492 p += 5;
2270 } 2493 }
2494 else if (STRNCMP(p, "closure", 7) == 0)
2495 {
2496 flags |= FC_CLOSURE;
2497 p += 7;
2498 if (current_funccal == NULL)
2499 {
2500 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
2501 name == NULL ? (char_u *)"" : name);
2502 goto erret;
2503 }
2504 }
2505 else
2506 break;
2507 }
2271 2508
2272 // When there is a line break use what follows for the function body. 2509 // When there is a line break use what follows for the function body.
2273 // Makes 'exe "func Test()\n...\nendfunc"' work. 2510 // Makes 'exe "func Test()\n...\nendfunc"' work.
2274 if (*p == '\n') 2511 if (*p == '\n')
2275 line_arg = p + 1; 2512 line_arg = p + 1;
2276 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg) 2513 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
2277 emsg(_(e_trailing)); 2514 emsg(_(e_trailing));
2278 2515
2279 /* 2516 /*
2280 * Read the body of the function, until ":endfunction" is found. 2517 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
2518 * found.
2281 */ 2519 */
2282 if (KeyTyped) 2520 if (KeyTyped)
2283 { 2521 {
2284 // Check if the function already exists, don't let the user type the 2522 // Check if the function already exists, don't let the user type the
2285 // whole function before telling him it doesn't work! For a script we 2523 // whole function before telling him it doesn't work! For a script we
2286 // need to skip the body to be able to find what follows. 2524 // need to skip the body to be able to find what follows.
2287 if (!eap->skip && !eap->forceit) 2525 if (!eap->skip && !eap->forceit)
2288 { 2526 {
2289 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL) 2527 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
2290 emsg(_(e_funcdict)); 2528 emsg(_(e_funcdict));
2291 else if (name != NULL && find_func(name) != NULL) 2529 else if (name != NULL && find_func(name, NULL) != NULL)
2292 emsg_funcname(e_funcexts, name); 2530 emsg_funcname(e_funcexts, name);
2293 } 2531 }
2294 2532
2295 if (!eap->skip && did_emsg) 2533 if (!eap->skip && did_emsg)
2296 goto erret; 2534 goto erret;
2302 // Save the starting line number. 2540 // Save the starting line number.
2303 sourcing_lnum_top = SOURCING_LNUM; 2541 sourcing_lnum_top = SOURCING_LNUM;
2304 2542
2305 indent = 2; 2543 indent = 2;
2306 nesting = 0; 2544 nesting = 0;
2545 nesting_def[nesting] = (eap->cmdidx == CMD_def);
2307 for (;;) 2546 for (;;)
2308 { 2547 {
2309 if (KeyTyped) 2548 if (KeyTyped)
2310 { 2549 {
2311 msg_scroll = TRUE; 2550 msg_scroll = TRUE;
2337 } 2576 }
2338 if (KeyTyped) 2577 if (KeyTyped)
2339 lines_left = Rows - 1; 2578 lines_left = Rows - 1;
2340 if (theline == NULL) 2579 if (theline == NULL)
2341 { 2580 {
2342 emsg(_("E126: Missing :endfunction")); 2581 if (eap->cmdidx == CMD_def)
2582 emsg(_("E1057: Missing :enddef"));
2583 else
2584 emsg(_("E126: Missing :endfunction"));
2343 goto erret; 2585 goto erret;
2344 } 2586 }
2345 2587
2346 // Detect line continuation: SOURCING_LNUM increased more than one. 2588 // Detect line continuation: SOURCING_LNUM increased more than one.
2347 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie); 2589 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
2350 else 2592 else
2351 sourcing_lnum_off = 0; 2593 sourcing_lnum_off = 0;
2352 2594
2353 if (skip_until != NULL) 2595 if (skip_until != NULL)
2354 { 2596 {
2355 // Don't check for ":endfunc" between 2597 // Don't check for ":endfunc"/":enddef" between
2356 // * ":append" and "." 2598 // * ":append" and "."
2357 // * ":python <<EOF" and "EOF" 2599 // * ":python <<EOF" and "EOF"
2358 // * ":let {var-name} =<< [trim] {marker}" and "{marker}" 2600 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2359 if (heredoc_trimmed == NULL 2601 if (heredoc_trimmed == NULL
2360 || (is_heredoc && skipwhite(theline) == theline) 2602 || (is_heredoc && skipwhite(theline) == theline)
2381 { 2623 {
2382 // skip ':' and blanks 2624 // skip ':' and blanks
2383 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p) 2625 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
2384 ; 2626 ;
2385 2627
2386 // Check for "endfunction". 2628 // Check for "endfunction" or "enddef".
2387 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0) 2629 if (checkforcmd(&p, nesting_def[nesting]
2630 ? "enddef" : "endfunction", 4) && nesting-- == 0)
2388 { 2631 {
2389 char_u *nextcmd = NULL; 2632 char_u *nextcmd = NULL;
2390 2633
2391 if (*p == '|') 2634 if (*p == '|')
2392 nextcmd = p + 1; 2635 nextcmd = p + 1;
2393 else if (line_arg != NULL && *skipwhite(line_arg) != NUL) 2636 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
2394 nextcmd = line_arg; 2637 nextcmd = line_arg;
2395 else if (*p != NUL && *p != '"' && p_verbose > 0) 2638 else if (*p != NUL && *p != '"' && p_verbose > 0)
2396 give_warning2( 2639 give_warning2(eap->cmdidx == CMD_def
2397 (char_u *)_("W22: Text found after :endfunction: %s"), 2640 ? (char_u *)_("W1001: Text found after :enddef: %s")
2641 : (char_u *)_("W22: Text found after :endfunction: %s"),
2398 p, TRUE); 2642 p, TRUE);
2399 if (nextcmd != NULL) 2643 if (nextcmd != NULL)
2400 { 2644 {
2401 // Another command follows. If the line came from "eap" we 2645 // Another command follows. If the line came from "eap" we
2402 // can simply point into it, otherwise we need to change 2646 // can simply point into it, otherwise we need to change
2412 break; 2656 break;
2413 } 2657 }
2414 2658
2415 // Increase indent inside "if", "while", "for" and "try", decrease 2659 // Increase indent inside "if", "while", "for" and "try", decrease
2416 // at "end". 2660 // at "end".
2417 if (indent > 2 && STRNCMP(p, "end", 3) == 0) 2661 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
2418 indent -= 2; 2662 indent -= 2;
2419 else if (STRNCMP(p, "if", 2) == 0 2663 else if (STRNCMP(p, "if", 2) == 0
2420 || STRNCMP(p, "wh", 2) == 0 2664 || STRNCMP(p, "wh", 2) == 0
2421 || STRNCMP(p, "for", 3) == 0 2665 || STRNCMP(p, "for", 3) == 0
2422 || STRNCMP(p, "try", 3) == 0) 2666 || STRNCMP(p, "try", 3) == 0)
2423 indent += 2; 2667 indent += 2;
2424 2668
2425 // Check for defining a function inside this function. 2669 // Check for defining a function inside this function.
2426 if (checkforcmd(&p, "function", 2)) 2670 c = *p;
2671 if (checkforcmd(&p, "function", 2) || checkforcmd(&p, "def", 3))
2427 { 2672 {
2428 if (*p == '!') 2673 if (*p == '!')
2429 p = skipwhite(p + 1); 2674 p = skipwhite(p + 1);
2430 p += eval_fname_script(p); 2675 p += eval_fname_script(p);
2431 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL)); 2676 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
2432 if (*skipwhite(p) == '(') 2677 if (*skipwhite(p) == '(')
2433 { 2678 {
2434 ++nesting; 2679 if (nesting == MAX_FUNC_NESTING - 1)
2435 indent += 2; 2680 emsg(_("E1058: function nesting too deep"));
2681 else
2682 {
2683 ++nesting;
2684 nesting_def[nesting] = (c == 'd');
2685 indent += 2;
2686 }
2436 } 2687 }
2437 } 2688 }
2438 2689
2439 // Check for ":append", ":change", ":insert". 2690 // Check for ":append", ":change", ":insert".
2440 p = skip_range(p, NULL); 2691 p = skip_range(p, NULL);
2535 /* 2786 /*
2536 * If there are no errors, add the function 2787 * If there are no errors, add the function
2537 */ 2788 */
2538 if (fudi.fd_dict == NULL) 2789 if (fudi.fd_dict == NULL)
2539 { 2790 {
2791 hashtab_T *ht;
2792
2540 v = find_var(name, &ht, FALSE); 2793 v = find_var(name, &ht, FALSE);
2541 if (v != NULL && v->di_tv.v_type == VAR_FUNC) 2794 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
2542 { 2795 {
2543 emsg_funcname(N_("E707: Function name conflicts with variable: %s"), 2796 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
2544 name); 2797 name);
2545 goto erret; 2798 goto erret;
2546 } 2799 }
2547 2800
2548 fp = find_func(name); 2801 fp = find_func_even_dead(name, NULL);
2549 if (fp != NULL) 2802 if (fp != NULL)
2550 { 2803 {
2804 int dead = fp->uf_flags & FC_DEAD;
2805
2551 // Function can be replaced with "function!" and when sourcing the 2806 // Function can be replaced with "function!" and when sourcing the
2552 // same script again, but only once. 2807 // same script again, but only once.
2553 if (!eap->forceit 2808 if (!dead && !eap->forceit
2554 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid 2809 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
2555 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)) 2810 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
2556 { 2811 {
2557 emsg_funcname(e_funcexts, name); 2812 emsg_funcname(e_funcexts, name);
2558 goto erret; 2813 goto erret;
2580 // redefine existing function, keep the expanded name 2835 // redefine existing function, keep the expanded name
2581 VIM_CLEAR(name); 2836 VIM_CLEAR(name);
2582 fp->uf_name_exp = NULL; 2837 fp->uf_name_exp = NULL;
2583 func_clear_items(fp); 2838 func_clear_items(fp);
2584 fp->uf_name_exp = exp_name; 2839 fp->uf_name_exp = exp_name;
2840 fp->uf_flags &= ~FC_DEAD;
2585 #ifdef FEAT_PROFILE 2841 #ifdef FEAT_PROFILE
2586 fp->uf_profiling = FALSE; 2842 fp->uf_profiling = FALSE;
2587 fp->uf_prof_initialized = FALSE; 2843 fp->uf_prof_initialized = FALSE;
2588 #endif 2844 #endif
2589 } 2845 }
2649 } 2905 }
2650 2906
2651 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1); 2907 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
2652 if (fp == NULL) 2908 if (fp == NULL)
2653 goto erret; 2909 goto erret;
2910 fp->uf_dfunc_idx = -1;
2654 2911
2655 if (fudi.fd_dict != NULL) 2912 if (fudi.fd_dict != NULL)
2656 { 2913 {
2657 if (fudi.fd_di == NULL) 2914 if (fudi.fd_di == NULL)
2658 { 2915 {
2694 } 2951 }
2695 fp->uf_refcount = 1; 2952 fp->uf_refcount = 1;
2696 } 2953 }
2697 fp->uf_args = newargs; 2954 fp->uf_args = newargs;
2698 fp->uf_def_args = default_args; 2955 fp->uf_def_args = default_args;
2956 fp->uf_ret_type = &t_any;
2957
2958 if (eap->cmdidx == CMD_def)
2959 {
2960 // parse the argument types
2961 ga_init2(&fp->uf_type_list, sizeof(type_T), 5);
2962
2963 if (argtypes.ga_len > 0)
2964 {
2965 // When "varargs" is set the last name/type goes into uf_va_name
2966 // and uf_va_type.
2967 int len = argtypes.ga_len - (varargs ? 1 : 0);
2968
2969 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
2970 if (fp->uf_arg_types != NULL)
2971 {
2972 int i;
2973
2974 for (i = 0; i < len; ++ i)
2975 {
2976 p = ((char_u **)argtypes.ga_data)[i];
2977 if (p == NULL)
2978 // todo: get type from default value
2979 fp->uf_arg_types[i] = &t_any;
2980 else
2981 fp->uf_arg_types[i] = parse_type(&p, &fp->uf_type_list);
2982 }
2983 }
2984 if (varargs)
2985 {
2986 // Move the last argument "...name: type" to uf_va_name and
2987 // uf_va_type.
2988 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
2989 [fp->uf_args.ga_len - 1];
2990 --fp->uf_args.ga_len;
2991 p = ((char_u **)argtypes.ga_data)[len];
2992 if (p == NULL)
2993 // todo: get type from default value
2994 fp->uf_va_type = &t_any;
2995 else
2996 fp->uf_va_type = parse_type(&p, &fp->uf_type_list);
2997 }
2998 varargs = FALSE;
2999 }
3000
3001 // parse the return type, if any
3002 if (ret_type == NULL)
3003 fp->uf_ret_type = &t_void;
3004 else
3005 {
3006 p = ret_type;
3007 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list);
3008 }
3009 }
3010
2699 fp->uf_lines = newlines; 3011 fp->uf_lines = newlines;
2700 if ((flags & FC_CLOSURE) != 0) 3012 if ((flags & FC_CLOSURE) != 0)
2701 { 3013 {
2702 if (register_closure(fp) == FAIL) 3014 if (register_closure(fp) == FAIL)
2703 goto erret; 3015 goto erret;
2714 flags |= FC_SANDBOX; 3026 flags |= FC_SANDBOX;
2715 fp->uf_flags = flags; 3027 fp->uf_flags = flags;
2716 fp->uf_calls = 0; 3028 fp->uf_calls = 0;
2717 fp->uf_script_ctx = current_sctx; 3029 fp->uf_script_ctx = current_sctx;
2718 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top; 3030 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
3031 if (is_export)
3032 {
3033 fp->uf_flags |= FC_EXPORT;
3034 // let ex_export() know the export worked.
3035 is_export = FALSE;
3036 }
3037
3038 // ":def Func()" needs to be compiled
3039 if (eap->cmdidx == CMD_def)
3040 compile_def_function(fp, FALSE);
3041
2719 goto ret_free; 3042 goto ret_free;
2720 3043
2721 erret: 3044 erret:
2722 ga_clear_strings(&newargs); 3045 ga_clear_strings(&newargs);
3046 ga_clear_strings(&argtypes);
2723 ga_clear_strings(&default_args); 3047 ga_clear_strings(&default_args);
2724 errret_2: 3048 errret_2:
2725 ga_clear_strings(&newlines); 3049 ga_clear_strings(&newlines);
2726 ret_free: 3050 ret_free:
2727 vim_free(skip_until); 3051 vim_free(skip_until);
2753 int 3077 int
2754 translated_function_exists(char_u *name) 3078 translated_function_exists(char_u *name)
2755 { 3079 {
2756 if (builtin_function(name, -1)) 3080 if (builtin_function(name, -1))
2757 return has_internal_func(name); 3081 return has_internal_func(name);
2758 return find_func(name) != NULL; 3082 return find_func(name, NULL) != NULL;
3083 }
3084
3085 /*
3086 * Return TRUE when "ufunc" has old-style "..." varargs
3087 * or named varargs "...name: type".
3088 */
3089 int
3090 has_varargs(ufunc_T *ufunc)
3091 {
3092 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
2759 } 3093 }
2760 3094
2761 /* 3095 /*
2762 * Return TRUE if a function "name" exists. 3096 * Return TRUE if a function "name" exists.
2763 * If "no_defef" is TRUE, do not dereference a Funcref. 3097 * If "no_defef" is TRUE, do not dereference a Funcref.
2824 ++hi; 3158 ++hi;
2825 while (HASHITEM_EMPTY(hi)) 3159 while (HASHITEM_EMPTY(hi))
2826 ++hi; 3160 ++hi;
2827 fp = HI2UF(hi); 3161 fp = HI2UF(hi);
2828 3162
2829 if ((fp->uf_flags & FC_DICT) 3163 // don't show dead, dict and lambda functions
3164 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
2830 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0) 3165 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
2831 return (char_u *)""; // don't show dict and lambda functions 3166 return (char_u *)"";
2832 3167
2833 if (STRLEN(fp->uf_name) + 4 >= IOSIZE) 3168 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
2834 return fp->uf_name; // prevents overflow 3169 return fp->uf_name; // prevents overflow
2835 3170
2836 cat_func_name(IObuff, fp); 3171 cat_func_name(IObuff, fp);
2837 if (xp->xp_context != EXPAND_USER_FUNC) 3172 if (xp->xp_context != EXPAND_USER_FUNC)
2838 { 3173 {
2839 STRCAT(IObuff, "("); 3174 STRCAT(IObuff, "(");
2840 if (!fp->uf_varargs && fp->uf_args.ga_len == 0) 3175 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
2841 STRCAT(IObuff, ")"); 3176 STRCAT(IObuff, ")");
2842 } 3177 }
2843 return IObuff; 3178 return IObuff;
2844 } 3179 }
2845 return NULL; 3180 return NULL;
2874 eap->nextcmd = check_nextcmd(p); 3209 eap->nextcmd = check_nextcmd(p);
2875 if (eap->nextcmd != NULL) 3210 if (eap->nextcmd != NULL)
2876 *p = NUL; 3211 *p = NUL;
2877 3212
2878 if (!eap->skip) 3213 if (!eap->skip)
2879 fp = find_func(name); 3214 fp = find_func(name, NULL);
2880 vim_free(name); 3215 vim_free(name);
2881 3216
2882 if (!eap->skip) 3217 if (!eap->skip)
2883 { 3218 {
2884 if (fp == NULL) 3219 if (fp == NULL)
2929 { 3264 {
2930 ufunc_T *fp = NULL; 3265 ufunc_T *fp = NULL;
2931 3266
2932 if (name == NULL || !func_name_refcount(name)) 3267 if (name == NULL || !func_name_refcount(name))
2933 return; 3268 return;
2934 fp = find_func(name); 3269 fp = find_func(name, NULL);
2935 if (fp == NULL && isdigit(*name)) 3270 if (fp == NULL && isdigit(*name))
2936 { 3271 {
2937 #ifdef EXITFREE 3272 #ifdef EXITFREE
2938 if (!entered_free_all_mem) 3273 if (!entered_free_all_mem)
2939 #endif 3274 #endif
2972 { 3307 {
2973 ufunc_T *fp; 3308 ufunc_T *fp;
2974 3309
2975 if (name == NULL || !func_name_refcount(name)) 3310 if (name == NULL || !func_name_refcount(name))
2976 return; 3311 return;
2977 fp = find_func(name); 3312 fp = find_func(name, NULL);
2978 if (fp != NULL) 3313 if (fp != NULL)
2979 ++fp->uf_refcount; 3314 ++fp->uf_refcount;
2980 else if (isdigit(*name)) 3315 else if (isdigit(*name))
2981 // Only give an error for a numbered function. 3316 // Only give an error for a numbered function.
2982 // Fail silently, when named or lambda function isn't found. 3317 // Fail silently, when named or lambda function isn't found.
3117 startarg = skipwhite(arg); 3452 startarg = skipwhite(arg);
3118 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this 3453 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
3119 3454
3120 if (*startarg != '(') 3455 if (*startarg != '(')
3121 { 3456 {
3122 semsg(_(e_missingparen), eap->arg); 3457 semsg(_(e_missing_paren), eap->arg);
3123 goto end; 3458 goto end;
3124 } 3459 }
3125 3460
3126 /* 3461 /*
3127 * When skipping, evaluate the function once, to find the end of the 3462 * When skipping, evaluate the function once, to find the end of the
3442 { 3777 {
3443 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string 3778 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
3444 : rettv->vval.v_partial->pt_name; 3779 : rettv->vval.v_partial->pt_name;
3445 // Translate "s:func" to the stored function name. 3780 // Translate "s:func" to the stored function name.
3446 fname = fname_trans_sid(fname, fname_buf, &tofree, &error); 3781 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
3447 fp = find_func(fname); 3782 fp = find_func(fname, NULL);
3448 vim_free(tofree); 3783 vim_free(tofree);
3449 } 3784 }
3450 3785
3451 if (fp != NULL && (fp->uf_flags & FC_DICT)) 3786 if (fp != NULL && (fp->uf_flags & FC_DICT))
3452 { 3787 {
3608 * Return NULL if there is no current funccal. 3943 * Return NULL if there is no current funccal.
3609 */ 3944 */
3610 hashtab_T * 3945 hashtab_T *
3611 get_funccal_local_ht() 3946 get_funccal_local_ht()
3612 { 3947 {
3613 if (current_funccal == NULL) 3948 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
3614 return NULL; 3949 return NULL;
3615 return &get_funccal()->l_vars.dv_hashtab; 3950 return &get_funccal()->l_vars.dv_hashtab;
3616 } 3951 }
3617 3952
3618 /* 3953 /*
3620 * Return NULL if there is no current funccal. 3955 * Return NULL if there is no current funccal.
3621 */ 3956 */
3622 dictitem_T * 3957 dictitem_T *
3623 get_funccal_local_var() 3958 get_funccal_local_var()
3624 { 3959 {
3625 if (current_funccal == NULL) 3960 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
3626 return NULL; 3961 return NULL;
3627 return &get_funccal()->l_vars_var; 3962 return &get_funccal()->l_vars_var;
3628 } 3963 }
3629 3964
3630 /* 3965 /*
3632 * Return NULL if there is no current funccal. 3967 * Return NULL if there is no current funccal.
3633 */ 3968 */
3634 hashtab_T * 3969 hashtab_T *
3635 get_funccal_args_ht() 3970 get_funccal_args_ht()
3636 { 3971 {
3637 if (current_funccal == NULL) 3972 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
3638 return NULL; 3973 return NULL;
3639 return &get_funccal()->l_avars.dv_hashtab; 3974 return &get_funccal()->l_avars.dv_hashtab;
3640 } 3975 }
3641 3976
3642 /* 3977 /*
3644 * Return NULL if there is no current funccal. 3979 * Return NULL if there is no current funccal.
3645 */ 3980 */
3646 dictitem_T * 3981 dictitem_T *
3647 get_funccal_args_var() 3982 get_funccal_args_var()
3648 { 3983 {
3649 if (current_funccal == NULL) 3984 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
3650 return NULL; 3985 return NULL;
3651 return &get_funccal()->l_avars_var; 3986 return &get_funccal()->l_avars_var;
3652 } 3987 }
3653 3988
3654 /* 3989 /*
3655 * List function variables, if there is a function. 3990 * List function variables, if there is a function.
3656 */ 3991 */
3657 void 3992 void
3658 list_func_vars(int *first) 3993 list_func_vars(int *first)
3659 { 3994 {
3660 if (current_funccal != NULL) 3995 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
3661 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab, 3996 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
3662 "l:", FALSE, first); 3997 "l:", FALSE, first);
3663 } 3998 }
3664 3999
3665 /* 4000 /*
3864 return FALSE; 4199 return FALSE;
3865 4200
3866 if (fp_in == NULL) 4201 if (fp_in == NULL)
3867 { 4202 {
3868 fname = fname_trans_sid(name, fname_buf, &tofree, &error); 4203 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3869 fp = find_func(fname); 4204 fp = find_func(fname, NULL);
3870 } 4205 }
3871 if (fp != NULL) 4206 if (fp != NULL)
3872 { 4207 {
3873 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped) 4208 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
3874 abort = abort || set_ref_in_funccal(fc, copyID); 4209 abort = abort || set_ref_in_funccal(fc, copyID);