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-function (fdefn)
29 (declare (type fdefn fdefn)
30 (values (or function null)))
31 (fdefn-function fdefn))
33 (defun (setf fdefn-function) (fun fdefn)
34 (declare (type function fun)
37 (setf (fdefn-function fdefn) fun))
39 (defun fdefn-makunbound (fdefn)
40 (declare (type fdefn fdefn))
41 (fdefn-makunbound fdefn))
43 ;;; This function is called by !COLD-INIT after the globaldb has been
44 ;;; initialized, but before anything else. We need to install these
45 ;;; fdefn objects into the globaldb before any top level forms run, or
46 ;;; we will end up with two different fdefn objects being used for the
47 ;;; same function name. *!INITIAL-FDEFN-OBJECTS* is set up by GENESIS.
48 (defvar *!initial-fdefn-objects*)
49 (defun !fdefn-cold-init ()
50 (dolist (fdefn *!initial-fdefn-objects*)
51 (setf (info :function :definition (fdefn-name fdefn)) fdefn)))
53 (defun fdefinition-object (name create)
55 "Return the fdefn object for NAME. If it doesn't already exist and CREATE
56 is non-NIL, create a new (unbound) one."
57 (declare (values (or fdefn null)))
58 (unless (or (symbolp name)
61 (let ((cdr (cdr name)))
65 (error 'simple-type-error
67 :expected-type '(or symbol list)
68 :format-control "invalid function name: ~S"
69 :format-arguments (list name)))
70 (let ((fdefn (info :function :definition name)))
71 (if (and (null fdefn) create)
72 (setf (info :function :definition name) (make-fdefn name))
75 ;;; FIXME: If the fundamental operation performed when
76 ;;; funcalling a symbol is %COERCE-NAME-TO-FUNCTION, which expands into
77 ;;; FDEFINITION-OBJECT, which does (INFO :FUNCTION :DEFINITION NAME),
78 ;;; that's a horrendously heavyweight way to implement SYMBOL-FUNCTION.
79 ;;; What compelling reason is there for all this hairiness? The only
80 ;;; thing I can think of is that it does give a place to store
81 ;;; SETF functions, but I don't think that's a good enough reason.
82 ;;; It might even be that the FDEFINITION arrangement saves a little
83 ;;; space, if the proportion of function-less symbols is high enough,
84 ;;; but I don't think that's a good enough reason, either.
85 ;;; I'd really like to wipe out FDEFN stuff root and branch, and
86 ;;; just store SETF functions in the symbol property list.
88 ;;; One problem with just doing the simple thing: What happens when
89 ;;; people call symbols which have no function definitions?
90 ;;; 1. Just hit "undefined function" error -- with no clue as to
91 ;;; what undefined function it was. (This might actually not be
92 ;;; too horrible, since the compiler warns you about undefined
93 ;;; functions and the debugger aims, with incomplete success,
94 ;;; to show you what form caused an error.)
95 ;;; 2. various solutions involving closures in the function slot,
96 ;;; all of which have the drawback of extra memory use and extra
97 ;;; difficulty in detecting when functions are undefined
98 ;;; 2a. Have every single symbol have an undefined function closure
99 ;;; which points back to it to tell you which undefined symbol it
100 ;;; was. (4 extra words per undefined symbol)
101 ;;; 2b. Play tricks with FDEFINITION, where the default SYMBOL-FUNCTION
102 ;;; for any function is an anonymous "undefined function" error
103 ;;; which doesn't tell you what the problem was, but if FDEFINITION
104 ;;; is ever called on an undefined symbol, it helpfully changes the
105 ;;; function definition to point to a closure which knows which
106 ;;; symbol caused the problem.
107 ;;; 4. Just don't sweat it except when DEBUG>SPEED, where the calling
108 ;;; convention gets tweaked to test for the undefined-function
109 ;;; function at call time and bail out with helpful information
111 ;;; 5. Require that the function calling convention be stereotyped
112 ;;; along the lines of
113 ;;; mov %ebx, local_immediate_3 ; Point to symbol.
114 ;;; mov %eax, symbol_function_offset(%eax) ; Point to function.
115 ;;; call *function_code_pointer(%eax) ; Go.
116 ;;; That way, it's guaranteed that on entry to a function, %EBX points
117 ;;; back to the symbol which was used to indirect into the function,
118 ;;; so the undefined function handler can base its complaint on that.
120 ;;; Another problem with doing the simple thing: people will want to
121 ;;; indirect through something in order to get to SETF functions, in
122 ;;; order to be able to redefine them. What will they indirect
123 ;;; through? This could be done with a hack, making an anonymous
124 ;;; symbol and linking it to the main symbol's SB!KERNEL:SETF-FUNCTION
125 ;;; property. The anonymous symbol could even point back to the symbol
126 ;;; it's the SETF function for, so that if the SETF function was
127 ;;; undefined at the time a call was made, the debugger could say
128 ;;; which function caused the problem. It'd probably be cleaner,
129 ;;; though, to use a new type of primitive object (SYMBOLOID?)
130 ;;; instead. It could probably be like symbol except that its name
131 ;;; could be any object and its value points back to the symbol which
132 ;;; owns it. Then the setf functions for FOO could be on the list (GET
133 ;;; FOO 'SB!KERNEL:SYMBOLOIDS)
135 ;;; FIXME: Oh, my. Now that I've started thinking about it, I
136 ;;; appreciate more fully how weird and twisted FDEFNs might be. Look
137 ;;; at the calling sequence for full calls. It goes and reads the
138 ;;; address of a function object from its own table of immediate
139 ;;; values, then jumps into that. Consider how weird that is. Not only
140 ;;; is it not doing indirection through a symbol (which I'd already
141 ;;; realized) but it's not doing indirection through
143 ;;; The compiler emits calls to this when someone tries to funcall a symbol.
144 (defun %coerce-name-to-function (name)
146 "Returns the definition for name, including any encapsulations. Settable
148 (let ((fdefn (fdefinition-object name nil)))
149 (or (and fdefn (fdefn-function fdefn))
150 (error 'undefined-function :name name))))
152 (defun %coerce-callable-to-function (callable)
153 (if (functionp callable)
155 (%coerce-name-to-function callable)))
157 ;;; This is just another name for %COERCE-NAME-TO-FUNCTION.
158 #!-sb-fluid (declaim (inline raw-definition))
159 (defun raw-definition (name)
160 ;; We know that we are calling %COERCE-NAME-TO-FUNCTION, so don't remind us.
161 (declare (optimize (inhibit-warnings 3)))
162 (%coerce-name-to-function name))
163 (defun (setf raw-definition) (function name)
164 (let ((fdefn (fdefinition-object name t)))
165 (setf (fdefn-function fdefn) function)))
167 ;;; FIXME: There seems to be no good reason to have both
168 ;;; %COERCE-NAME-TO-FUNCTION and RAW-DEFINITION names for the same
169 ;;; thing. And despite what the doc string of %COERCE-NAME-TO-FUNCTION
170 ;;; says, it's doesn't look settable. Perhaps we could collapse
171 ;;; %COERCE-TO-FUNCTION, RAW-DEFINITION, and (SETF RAW-DEFINITION)
172 ;;; into RAW-FDEFINITION and (SETF RAW-FDEFINITION), or
173 ;;; OUTER-FDEFINITION and (SETF OUTER-FDEFINITION).
175 ;;;; definition encapsulation
177 (defstruct (encapsulation-info (:constructor make-encapsulation-info
179 ;; This is definition's encapsulation type. The encapsulated
180 ;; definition is in the previous encapsulation-info element or
181 ;; installed as the global definition of some function name.
183 ;; the previous, encapsulated definition. This used to be installed
184 ;; as a global definition for some function name, but it was
185 ;; replaced by an encapsulation of type TYPE.
186 (definition nil :type function))
188 ;;; We must bind and close over info. Consider the case where we
189 ;;; encapsulate (the second) an encapsulated (the first) definition,
190 ;;; and later someone unencapsulates the encapsulated (first)
191 ;;; definition. We don't want our encapsulation (second) to bind
192 ;;; basic-definition to the encapsulated (first) definition when it no
193 ;;; longer exists. When unencapsulating, we make sure to clobber the
194 ;;; appropriate info structure to allow basic-definition to be bound
195 ;;; to the next definition instead of an encapsulation that no longer
197 (defun encapsulate (name type body)
199 "Replaces the definition of NAME with a function that binds name's arguments
200 a variable named argument-list, binds name's definition to a variable named
201 basic-definition, and evaluates BODY in that context. TYPE is
202 whatever you would like to associate with this encapsulation for
203 identification in case you need multiple encapsuations of the same name."
204 (let ((fdefn (fdefinition-object name nil)))
205 (unless (and fdefn (fdefn-function fdefn))
206 (error 'undefined-function :name name))
207 (let ((info (make-encapsulation-info type (fdefn-function fdefn))))
208 (setf (fdefn-function fdefn)
209 #'(lambda (&rest argument-list)
210 (declare (special argument-list))
211 (let ((basic-definition (encapsulation-info-definition info)))
212 (declare (special basic-definition))
215 ;;; Finds the encapsulation info that has been closed over.
216 (defun encapsulation-info (fun)
218 (= (get-type fun) sb!vm:closure-header-type)
219 (find-if-in-closure #'encapsulation-info-p fun)))
221 ;;; When removing an encapsulation, we must remember that
222 ;;; encapsulating definitions close over a reference to the
223 ;;; encapsulation-info that describes the encapsulating definition.
224 ;;; When you find an info with the target type, the previous info in
225 ;;; the chain has the ensulating definition of that type. We take the
226 ;;; encapsulated definition from the info with the target type, and we
227 ;;; store it in the previous info structure whose encapsulating
228 ;;; definition it describes looks to this previous info structure for
229 ;;; a definition to bind (see ENCAPSULATE). When removing the first
230 ;;; info structure, we do something conceptually equal, but
231 ;;; mechanically it is different.
232 (defun unencapsulate (name type)
234 "Removes NAME's most recent encapsulation of the specified TYPE."
235 (let* ((fdefn (fdefinition-object name nil))
236 (encap-info (encapsulation-info (fdefn-function fdefn))))
237 (declare (type (or encapsulation-info null) encap-info))
238 (cond ((not encap-info)
239 ;; It disappeared on us, so don't worry about it.
241 ((eq (encapsulation-info-type encap-info) type)
242 ;; It's the first one, so change the fdefn object.
243 (setf (fdefn-function fdefn)
244 (encapsulation-info-definition encap-info)))
246 ;; It must be an interior one, so find it.
248 (let ((next-info (encapsulation-info
249 (encapsulation-info-definition encap-info))))
251 ;; Not there, so don't worry about it.
253 (when (eq (encapsulation-info-type next-info) type)
254 ;; This is it, so unlink us.
255 (setf (encapsulation-info-definition encap-info)
256 (encapsulation-info-definition next-info))
258 (setf encap-info next-info))))))
261 (defun encapsulated-p (name type)
263 "Returns t if name has an encapsulation of the given type, otherwise nil."
264 (let ((fdefn (fdefinition-object name nil)))
265 (do ((encap-info (encapsulation-info (fdefn-function fdefn))
267 (encapsulation-info-definition encap-info))))
268 ((null encap-info) nil)
269 (declare (type (or encapsulation-info null) encap-info))
270 (when (eq (encapsulation-info-type encap-info) type)
275 ;;; KLUDGE: Er, it looks as though this means that
276 ;;; (FUNCALL (FDEFINITION 'FOO))
277 ;;; doesn't do the same thing as
279 ;;; That doesn't look like ANSI behavior to me. Look e.g. at the
280 ;;; ANSI definition of TRACE: "Whenever a traced function is invoked,
281 ;;; information about the call, ..". Try this:
282 ;;; (DEFUN FOO () (PRINT "foo"))
285 ;;; (FUNCALL (FDEFINITION 'FOO))
286 ;;; What to do? ANSI says TRACE "Might change the definitions of the functions
287 ;;; named by function-names." Might it be OK to just get punt all this
288 ;;; encapsulation stuff and go back to a simple but correct implementation of
289 ;;; TRACE? We'd lose the ability to redefine a TRACEd function and keep the
290 ;;; trace in place, but that seems tolerable to me. (Is the wrapper stuff
291 ;;; needed for anything else besides TRACE?)
293 ;;; The only problem I can see with not having a wrapper: If tracing
294 ;;; EQ, EQL, EQUAL, or EQUALP causes its function address to change,
295 ;;; it will mess up the MAKE-HASH-TABLE logic which uses EQ tests
296 ;;; on those function values. -- WHN 19990906
297 (defun fdefinition (name)
299 "Return name's global function definition taking care to respect any
300 encapsulations and to return the innermost encapsulated definition.
302 (let ((fun (raw-definition name)))
304 (let ((encap-info (encapsulation-info fun)))
306 (setf fun (encapsulation-info-definition encap-info))
309 (defvar *setf-fdefinition-hook* nil
311 "This holds functions that (SETF FDEFINITION) invokes before storing the
312 new value. These functions take the function name and the new value.")
314 (defun %set-fdefinition (name new-value)
316 "Set NAME's global function definition."
317 (declare (type function new-value) (optimize (safety 1)))
318 (let ((fdefn (fdefinition-object name t)))
319 ;; *SETF-FDEFINITION-HOOK* won't be bound when initially running top-level
320 ;; forms in the kernel core startup.
321 (when (boundp '*setf-fdefinition-hook*)
322 (dolist (f *setf-fdefinition-hook*)
323 (funcall f name new-value)))
325 (let ((encap-info (encapsulation-info (fdefn-function fdefn))))
330 (encapsulation-info-definition encap-info))))
332 (setf encap-info more-info)
334 (setf (encapsulation-info-definition encap-info)
337 (setf (fdefn-function fdefn) new-value))))))
339 ;;;; FBOUNDP and FMAKUNBOUND
341 (defun fboundp (name)
343 "Return true if name has a global function definition."
344 (let ((fdefn (fdefinition-object name nil)))
345 (and fdefn (fdefn-function fdefn) t)))
347 (defun fmakunbound (name)
349 "Make Name have no global function definition."
350 (let ((fdefn (fdefinition-object name nil)))
352 (fdefn-makunbound fdefn)))