1 ;;;; This file contains functions that hack on the global function
2 ;;;; namespace (primarily concerned with SETF functions here). Also,
3 ;;;; function encapsulation and routines that set and return
4 ;;;; definitions disregarding whether they might be encapsulated.
6 ;;;; This software is part of the SBCL system. See the README file for
9 ;;;; This software is derived from the CMU CL system, which was
10 ;;;; written at Carnegie Mellon University and released into the
11 ;;;; public domain. The software is in the public domain and is
12 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
13 ;;;; files for more information.
15 (in-package "SB!IMPL")
17 (sb!int
::/show0
"fdefinition.lisp 22")
19 ;;;; fdefinition (fdefn) objects
21 (defun make-fdefn (name)
24 (defun fdefn-name (fdefn)
25 (declare (type fdefn fdefn
))
28 (defun fdefn-fun (fdefn)
29 (declare (type fdefn fdefn
)
30 (values (or function null
)))
33 (defun (setf fdefn-fun
) (fun fdefn
)
34 (declare (type function fun
)
37 (setf (fdefn-fun fdefn
) fun
))
39 (defun fdefn-makunbound (fdefn)
40 (declare (type fdefn fdefn
))
41 (fdefn-makunbound fdefn
))
43 #!-sb-fluid
(declaim (inline symbol-fdefn
))
44 ;; Return SYMBOL's fdefinition, if any, or NIL. SYMBOL must already
45 ;; have been verified to be a symbol by the caller.
46 (defun symbol-fdefn (symbol)
47 (declare (optimize (safety 0)))
48 (info-vector-fdefn (symbol-info-vector symbol
)))
50 ;; Return the fdefn object for NAME, or NIL if there is no fdefn.
51 ;; Signal an error if name isn't valid.
52 ;; Assume that exists-p implies LEGAL-FUN-NAME-P.
54 (declaim (ftype (sfunction ((or symbol list
)) (or fdefn null
)) find-fdefn
))
55 (defun find-fdefn (name)
56 (declare (explicit-check))
57 (when (symbolp name
) ; Don't need LEGAL-FUN-NAME-P check
58 (return-from find-fdefn
(symbol-fdefn name
)))
59 ;; Technically the ALLOW-ATOM argument of NIL isn't needed, but
60 ;; the compiler isn't figuring out not to test SYMBOLP twice in a row.
61 (with-globaldb-name (key1 key2 nil
) name
63 ;; INFO-GETHASH returns NIL or a vector. INFO-VECTOR-FDEFN accepts
64 ;; either. If fdefn isn't found, fall through to the legality test.
65 (awhen (info-vector-fdefn (info-gethash name
*info-environment
*))
66 (return-from find-fdefn it
))
69 (awhen (symbol-info-vector key1
)
70 (multiple-value-bind (data-idx descriptor-idx field-idx
)
71 (info-find-aux-key/packed it key2
)
72 (declare (type index descriptor-idx
)
73 (type (integer 0 #.
+infos-per-word
+) field-idx
))
74 ;; Secondary names must have at least one info, so if a descriptor
75 ;; exists, there's no need to extract the n-infos field.
77 (when (eql (incf field-idx
) +infos-per-word
+)
78 (setq field-idx
0 descriptor-idx
(1+ descriptor-idx
)))
79 (when (eql (packed-info-field it descriptor-idx field-idx
)
81 (return-from find-fdefn
82 (aref it
(1- (the index data-idx
))))))))
83 (when (eq key1
'setf
) ; bypass the legality test
84 (return-from find-fdefn nil
))))
85 (legal-fun-name-or-type-error name
))
87 (declaim (ftype (sfunction (t) fdefn
) find-or-create-fdefn
))
88 (defun find-or-create-fdefn (name)
90 ;; We won't reach here if the name was not legal
91 (get-info-value-initializing :function
:definition name
94 ;;; Remove NAME's FTYPE information unless it was explicitly PROCLAIMED.
95 ;;; The NEW-FUNCTION argument is presently unused, but could be used
96 ;;; for checking compatibility of the NEW-FUNCTION against a proclamation.
97 ;;; (We could issue a warning and/or remove the type if incompatible.)
98 (defun maybe-clobber-ftype (name new-function
)
99 (declare (ignore new-function
))
100 (unless (eq :declared
(info :function
:where-from name
))
101 (clear-info :function
:type name
)))
103 ;;; Return the fdefn-fun of NAME's fdefinition including any encapsulations.
104 ;;; LOOKUP-FN, defaulting to FIND-FDEFN, specifies how to lookup the fdefn.
105 ;;; As a special case it can be given as SYMBOL-FDEFN which is slightly quicker.
106 ;;; This is the core of the implementation of the standard FDEFINITION function,
107 ;;; but as we've defined FDEFINITION, that strips encapsulations.
108 (defmacro %coerce-name-to-fun
(name &optional
(lookup-fn 'find-fdefn
))
109 `(let* ((name ,name
) (fdefn (,lookup-fn name
)))
111 (sb!c
:safe-fdefn-fun fdefn
)
112 (error 'undefined-function
:name name
))))
114 ;; Coerce CALLABLE (a function-designator) to a FUNCTION.
115 ;; The compiler emits this when someone tries to FUNCALL something.
116 ;; Extended-function-designators are not accepted,
117 ;; This function declares EXPLICIT-CHECK, and we avoid calling
118 ;; SYMBOL-FUNCTION because that would do another check.
119 ;; It would be great if this could change its error message
120 ;; depending on the input to either:
121 ;; "foo is not a function designator" if not a CALLABLE
122 ;; "foo does not designate a currently defined function"
123 ;; if a symbol does not satisfy FBOUNDP.
124 (defun %coerce-callable-to-fun
(callable)
125 (declare (explicit-check))
128 (symbol (%coerce-name-to-fun callable symbol-fdefn
))))
131 ;;;; definition encapsulation
133 (defstruct (encapsulation-info (:constructor make-encapsulation-info
136 ;; This is definition's encapsulation type. The encapsulated
137 ;; definition is in the previous ENCAPSULATION-INFO element or
138 ;; installed as the global definition of some function name.
140 ;; the previous, encapsulated definition. This used to be installed
141 ;; as a global definition for some function name, but it was
142 ;; replaced by an encapsulation of type TYPE.
143 (definition nil
:type function
))
145 ;;; Replace the definition of NAME with a function that calls FUNCTION
146 ;;; with the original function and its arguments.
147 ;;; TYPE is whatever you would like to associate with this
148 ;;; encapsulation for identification in case you need multiple
149 ;;; encapsulations of the same name.
150 (defun encapsulate (name type function
)
151 (let* ((fdefn (find-fdefn name
))
152 (underlying-fun (sb!c
:safe-fdefn-fun fdefn
)))
153 (when (typep underlying-fun
'generic-function
)
154 (return-from encapsulate
155 (encapsulate-generic-function underlying-fun type function
)))
156 ;; We must bind and close over INFO. Consider the case where we
157 ;; encapsulate (the second) an encapsulated (the first)
158 ;; definition, and later someone unencapsulates the encapsulated
159 ;; (first) definition. We don't want our encapsulation (second) to
160 ;; bind basic-definition to the encapsulated (first) definition
161 ;; when it no longer exists. When unencapsulating, we make sure to
162 ;; clobber the appropriate INFO structure to allow
163 ;; basic-definition to be bound to the next definition instead of
164 ;; an encapsulation that no longer exists.
165 (let ((info (make-encapsulation-info type underlying-fun
)))
166 (setf (fdefn-fun fdefn
)
167 (named-lambda encapsulation
(&rest args
)
168 (apply function
(encapsulation-info-definition info
)
171 ;;; This is like FIND-IF, except that we do it on a compiled closure's
173 (defun find-if-in-closure (test closure
)
174 (declare (closure closure
))
175 (do-closure-values (value closure
)
176 (when (funcall test value
)
179 ;;; Find the encapsulation info that has been closed over.
180 (defun encapsulation-info (fun)
182 (find-if-in-closure #'encapsulation-info-p fun
)))
184 ;;; When removing an encapsulation, we must remember that
185 ;;; encapsulating definitions close over a reference to the
186 ;;; ENCAPSULATION-INFO that describes the encapsulating definition.
187 ;;; When you find an info with the target type, the previous info in
188 ;;; the chain has the ensulating definition of that type. We take the
189 ;;; encapsulated definition from the info with the target type, and we
190 ;;; store it in the previous info structure whose encapsulating
191 ;;; definition it describes looks to this previous info structure for
192 ;;; a definition to bind (see ENCAPSULATE). When removing the first
193 ;;; info structure, we do something conceptually equal, but
194 ;;; mechanically it is different.
195 (defun unencapsulate (name type
)
197 "Removes NAME's most recent encapsulation of the specified TYPE."
198 (let* ((fdefn (find-fdefn name
))
199 (encap-info (encapsulation-info (fdefn-fun fdefn
))))
200 (declare (type (or encapsulation-info null
) encap-info
))
201 (when (and fdefn
(typep (fdefn-fun fdefn
) 'generic-function
))
202 (return-from unencapsulate
203 (unencapsulate-generic-function (fdefn-fun fdefn
) type
)))
204 (cond ((not encap-info
)
205 ;; It disappeared on us, so don't worry about it.
207 ((eq (encapsulation-info-type encap-info
) type
)
208 ;; It's the first one, so change the fdefn object.
209 (setf (fdefn-fun fdefn
)
210 (encapsulation-info-definition encap-info
)))
212 ;; It must be an interior one, so find it.
214 (let ((next-info (encapsulation-info
215 (encapsulation-info-definition encap-info
))))
217 ;; Not there, so don't worry about it.
219 (when (eq (encapsulation-info-type next-info
) type
)
220 ;; This is it, so unlink us.
221 (setf (encapsulation-info-definition encap-info
)
222 (encapsulation-info-definition next-info
))
224 (setf encap-info next-info
))))))
227 ;;; Does NAME have an encapsulation of the given TYPE?
228 (defun encapsulated-p (name type
)
229 (let ((fdefn (find-fdefn name
)))
230 (when (and fdefn
(typep (fdefn-fun fdefn
) 'generic-function
))
231 (return-from encapsulated-p
232 (encapsulated-generic-function-p (fdefn-fun fdefn
) type
)))
233 (do ((encap-info (encapsulation-info (fdefn-fun fdefn
))
235 (encapsulation-info-definition encap-info
))))
236 ((null encap-info
) nil
)
237 (declare (type (or encapsulation-info null
) encap-info
))
238 (when (eq (encapsulation-info-type encap-info
) type
)
243 ;;; KLUDGE: Er, it looks as though this means that
244 ;;; (FUNCALL (FDEFINITION 'FOO))
245 ;;; doesn't do the same thing as
247 ;;; and (SYMBOL-FUNCTION 'FOO) isn't in general the same thing
248 ;;; as (FDEFINITION 'FOO). That doesn't look like ANSI behavior to me.
249 ;;; Look e.g. at the ANSI definition of TRACE: "Whenever a traced
250 ;;; function is invoked, information about the call, ..". Try this:
251 ;;; (DEFUN FOO () (PRINT "foo"))
254 ;;; (FUNCALL (FDEFINITION 'FOO))
255 ;;; What to do? ANSI says TRACE "Might change the definitions of the
256 ;;; functions named by function-names." Might it be OK to just get
257 ;;; punt all this encapsulation stuff and go back to a simple but
258 ;;; correct implementation of TRACE? We'd lose the ability to redefine
259 ;;; a TRACEd function and keep the trace in place, but that seems
260 ;;; tolerable to me. (Is the wrapper stuff needed for anything else
263 ;;; The only problem I can see with not having a wrapper: If tracing
264 ;;; EQ, EQL, EQUAL, or EQUALP causes its function address to change,
265 ;;; it will mess up the MAKE-HASH-TABLE logic which uses EQ tests
266 ;;; on those function values. But given the ANSI statement about
267 ;;; TRACE causing things to change, that doesn't seem too unreasonable;
268 ;;; and we might even be able to forbid tracing these functions.
269 ;;; -- WHN 2001-11-02
270 (defun fdefinition (name)
272 "Return name's global function definition taking care to respect any
273 encapsulations and to return the innermost encapsulated definition.
275 (declare (explicit-check))
276 (let ((fun (%coerce-name-to-fun name
)))
278 (let ((encap-info (encapsulation-info fun
)))
280 (setf fun
(encapsulation-info-definition encap-info
))
283 (defvar *setf-fdefinition-hook
* nil
285 "A list of functions that (SETF FDEFINITION) invokes before storing the
286 new value. The functions take the function name and the new value.")
288 ;; Return T if FUNCTION is the error-signaling trampoline
289 ;; for a macro or a special operator. Test for this by seeing
290 ;; whether FUNCTION is the same closure as for a known macro.
291 ;; For cold-init to work, this must pick any macro defined before
292 ;; this function is. A safe choice is a macro from this same file.
293 (defun macro/special-guard-fun-p
(function)
294 (and (closurep function
)
295 ;; Prior to cold-init fixing up the load-time-value, this compares
296 ;; %closure-fun to 0, which is ok - it returns NIL.
298 (%closure-fun
(symbol-function '%coerce-name-to-fun
)) t
)
299 (%closure-fun function
))))
301 ;; Reject any "object of implementation-dependent nature" that
302 ;; so happens to be a function in SBCL, but which must not be
303 ;; bound to a function-name by way of (SETF FEDFINITION).
304 (defun err-if-unacceptable-function (object setter
)
305 (when (macro/special-guard-fun-p object
)
306 (error 'simple-reference-error
307 :references
(list '(:ansi-cl
:function fdefinition
))
308 :format-control
"~S is not acceptable to ~S."
309 :format-arguments
(list object setter
))))
311 (defun %set-fdefinition
(name new-value
)
313 "Set NAME's global function definition."
314 (declare (type function new-value
) (optimize (safety 1)))
315 (declare (explicit-check))
316 (err-if-unacceptable-function new-value
'(setf fdefinition
))
317 (with-single-package-locked-error (:symbol name
"setting fdefinition of ~A")
318 (maybe-clobber-ftype name new-value
)
320 ;; Check for hash-table stuff. Woe onto him that mixes encapsulation
322 (when (and (symbolp name
) (fboundp name
)
323 (boundp '*user-hash-table-tests
*))
324 (let ((old (symbol-function name
)))
325 (declare (special *user-hash-table-tests
*))
326 (dolist (spec *user-hash-table-tests
*)
327 (cond ((eq old
(second spec
))
329 (setf (second spec
) new-value
))
330 ((eq old
(third spec
))
332 (setf (third spec
) new-value
))))))
334 ;; FIXME: This is a good hook to have, but we should probably
335 ;; reserve it for users.
336 (let ((fdefn (find-or-create-fdefn name
)))
337 ;; *SETF-FDEFINITION-HOOK* won't be bound when initially running
338 ;; top level forms in the kernel core startup.
339 (when (boundp '*setf-fdefinition-hook
*)
340 (dolist (f *setf-fdefinition-hook
*)
341 (declare (type function f
))
342 (funcall f name new-value
)))
344 (let ((encap-info (encapsulation-info (fdefn-fun fdefn
))))
349 (encapsulation-info-definition encap-info
))))
351 (setf encap-info more-info
)
353 (setf (encapsulation-info-definition encap-info
)
356 (setf (fdefn-fun fdefn
) new-value
)))))))
358 ;;;; FBOUNDP and FMAKUNBOUND
360 (defun fboundp (name)
362 "Return true if name has a global function definition."
363 (declare (explicit-check))
364 (let ((fdefn (find-fdefn name
)))
365 (and fdefn
(fdefn-fun fdefn
) t
)))
367 (defun fmakunbound (name)
369 "Make NAME have no global function definition."
370 (declare (explicit-check))
371 (with-single-package-locked-error
372 (:symbol name
"removing the function or macro definition of ~A")
373 (let ((fdefn (find-fdefn name
)))
375 (fdefn-makunbound fdefn
)))
376 (undefine-fun-name name
)