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