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 ;;; This is used for undefined-fun-error restarts
22 (defun make-dummy-fdefn ()
23 #!-immobile-space
(make-fdefn nil
)
24 #!+immobile-space
(make-dummy-fdefn))
26 (defun make-fdefn (name)
27 #!-immobile-space
(make-fdefn name
)
28 ;; This is %primitive because it needs pseudo-atomic,
29 ;; otherwise it would just be an alien-funcall.
31 (let ((fdefn (truly-the (values fdefn
)
32 (%primitive sb
!vm
::alloc-immobile-fdefn name
))))
33 (%primitive fdefn-makunbound fdefn
)
36 (defun fdefn-name (fdefn)
37 (declare (type fdefn fdefn
))
40 (defun fdefn-fun (fdefn)
41 (declare (type fdefn fdefn
)
42 (values (or function null
)))
45 (defun (setf fdefn-fun
) (fun fdefn
)
46 (declare (type function fun
)
49 (setf (fdefn-fun fdefn
) fun
))
51 (defun fdefn-makunbound (fdefn)
52 (declare (type fdefn fdefn
))
53 (fdefn-makunbound fdefn
))
55 #!-sb-fluid
(declaim (inline symbol-fdefn
))
56 ;; Return SYMBOL's fdefinition, if any, or NIL. SYMBOL must already
57 ;; have been verified to be a symbol by the caller.
58 (defun symbol-fdefn (symbol)
59 (declare (optimize (safety 0)))
60 (info-vector-fdefn (symbol-info-vector symbol
)))
62 ;; Return the fdefn object for NAME, or NIL if there is no fdefn.
63 ;; Signal an error if name isn't valid.
64 ;; Assume that exists-p implies LEGAL-FUN-NAME-P.
66 (declaim (ftype (sfunction ((or symbol list
)) (or fdefn null
)) find-fdefn
))
67 (defun find-fdefn (name)
68 (declare (explicit-check))
69 (when (symbolp name
) ; Don't need LEGAL-FUN-NAME-P check
70 (return-from find-fdefn
(symbol-fdefn name
)))
71 ;; Technically the ALLOW-ATOM argument of NIL isn't needed, but
72 ;; the compiler isn't figuring out not to test SYMBOLP twice in a row.
73 (with-globaldb-name (key1 key2 nil
) name
75 ;; INFO-GETHASH returns NIL or a vector. INFO-VECTOR-FDEFN accepts
76 ;; either. If fdefn isn't found, fall through to the legality test.
77 (awhen (info-vector-fdefn (info-gethash name
*info-environment
*))
78 (return-from find-fdefn it
))
81 (awhen (symbol-info-vector key1
)
82 (multiple-value-bind (data-idx descriptor-idx field-idx
)
83 (info-find-aux-key/packed it key2
)
84 (declare (type index descriptor-idx
)
85 (type (integer 0 #.
+infos-per-word
+) field-idx
))
86 ;; Secondary names must have at least one info, so if a descriptor
87 ;; exists, there's no need to extract the n-infos field.
89 (when (eql (incf field-idx
) +infos-per-word
+)
90 (setq field-idx
0 descriptor-idx
(1+ descriptor-idx
)))
91 (when (eql (packed-info-field it descriptor-idx field-idx
)
93 (return-from find-fdefn
94 (aref it
(1- (the index data-idx
))))))))
95 (when (eq key1
'setf
) ; bypass the legality test
96 (return-from find-fdefn nil
))))
97 (legal-fun-name-or-type-error name
))
99 (declaim (ftype (sfunction (t) fdefn
) find-or-create-fdefn
))
100 (defun find-or-create-fdefn (name)
101 (or (find-fdefn name
)
102 ;; We won't reach here if the name was not legal
103 (get-info-value-initializing :function
:definition name
106 ;;; Remove NAME's FTYPE information unless it was explicitly PROCLAIMED.
107 ;;; The NEW-FUNCTION argument is presently unused, but could be used
108 ;;; for checking compatibility of the NEW-FUNCTION against a proclamation.
109 ;;; (We could issue a warning and/or remove the type if incompatible.)
110 (defun maybe-clobber-ftype (name new-function
)
111 (declare (ignore new-function
))
112 (unless (eq :declared
(info :function
:where-from name
))
113 (clear-info :function
:type name
)))
115 ;;; Return the fdefn-fun of NAME's fdefinition including any encapsulations.
116 ;;; LOOKUP-FN, defaulting to FIND-FDEFN, specifies how to lookup the fdefn.
117 ;;; As a special case it can be given as SYMBOL-FDEFN which is slightly quicker.
118 ;;; This is the core of the implementation of the standard FDEFINITION function,
119 ;;; but as we've defined FDEFINITION, that strips encapsulations.
120 (defmacro %coerce-name-to-fun
(name &optional
(lookup-fn 'find-fdefn
)
125 (let ((fdefn (,lookup-fn name
)))
127 (let ((f (fdefn-fun (truly-the fdefn fdefn
))))
128 ;; If STRICTLY-FUNCTIONP is true, we make sure not to return an error
129 ;; trampoline. This extra check ensures that full calls such as
130 ;; (MAPCAR 'OR '()) signal an error that OR isn't a function.
131 ;; This accords with the non-requirement that macros store strictly
132 ;; a function in the symbol that names them. In many implementations,
133 ;; (FUNCTIONP (SYMBOL-FUNCTION 'OR)) => NIL. We want to pretend that.
135 (,@(if strictly-functionp
136 '(unless (macro/special-guard-fun-p f
))
139 (restart-case (error 'undefined-function
:name name
)
141 :report
(lambda (stream)
142 (format stream
"Retry using ~s." name
)))
144 :report
(lambda (stream)
145 (format stream
"Use specified function"))
146 :interactive read-evaluated-form
147 (when (functionp value
)
149 (setf name
(the ,(if (eq lookup-fn
'symbol-fdefn
)
155 ;; Return T if FUNCTION is the error-signaling trampoline
156 ;; for a macro or a special operator. Test for this by seeing
157 ;; whether FUNCTION is the same closure as for a known macro.
158 ;; For cold-init to work, this must pick any macro defined before
159 ;; this function is. A safe choice is a macro from this same file.
160 (declaim (inline macro
/special-guard-fun-p
))
161 (defun macro/special-guard-fun-p
(function)
162 ;; When inlined, this is a few instructions shorter than CLOSUREP
163 ;; if we already know that FUNCTION is a function.
164 ;; It will signal a type error if not, which is the right thing to do anyway.
165 ;; (this isn't quite a true predicate)
166 (and (= (fun-subtype function
) sb
!vm
:closure-header-widetag
)
167 ;; Prior to cold-init fixing up the load-time-value, this compares
168 ;; %closure-fun to 0, which is ok - it returns NIL.
169 (eq (load-time-value (%closure-fun
(symbol-function '%coerce-name-to-fun
))
171 (%closure-fun function
))))
173 ;; Coerce CALLABLE (a function-designator) to a FUNCTION.
174 ;; The compiler emits this when someone tries to FUNCALL something.
175 ;; Extended-function-designators are not accepted,
176 ;; This function declares EXPLICIT-CHECK, and we avoid calling
177 ;; SYMBOL-FUNCTION because that would do another check.
178 ;; It would be great if this could change its error message
179 ;; depending on the input to either:
180 ;; "foo is not a function designator" if not a CALLABLE
181 ;; "foo does not designate a currently defined function"
182 ;; if a symbol does not satisfy FBOUNDP.
183 (defun %coerce-callable-to-fun
(callable)
184 (declare (explicit-check))
187 (symbol (%coerce-name-to-fun callable symbol-fdefn t
))))
190 ;;;; definition encapsulation
192 (defstruct (encapsulation-info (:constructor make-encapsulation-info
195 ;; This is definition's encapsulation type. The encapsulated
196 ;; definition is in the previous ENCAPSULATION-INFO element or
197 ;; installed as the global definition of some function name.
199 ;; the previous, encapsulated definition. This used to be installed
200 ;; as a global definition for some function name, but it was
201 ;; replaced by an encapsulation of type TYPE.
202 (definition nil
:type function
))
204 ;;; Replace the definition of NAME with a function that calls FUNCTION
205 ;;; with the original function and its arguments.
206 ;;; TYPE is whatever you would like to associate with this
207 ;;; encapsulation for identification in case you need multiple
208 ;;; encapsulations of the same name.
209 (defun encapsulate (name type function
)
210 (let* ((fdefn (find-fdefn name
))
211 (underlying-fun (sb!c
:safe-fdefn-fun fdefn
)))
212 (when (typep underlying-fun
'generic-function
)
213 (return-from encapsulate
214 (encapsulate-generic-function underlying-fun type function
)))
215 ;; We must bind and close over INFO. Consider the case where we
216 ;; encapsulate (the second) an encapsulated (the first)
217 ;; definition, and later someone unencapsulates the encapsulated
218 ;; (first) definition. We don't want our encapsulation (second) to
219 ;; bind basic-definition to the encapsulated (first) definition
220 ;; when it no longer exists. When unencapsulating, we make sure to
221 ;; clobber the appropriate INFO structure to allow
222 ;; basic-definition to be bound to the next definition instead of
223 ;; an encapsulation that no longer exists.
224 (let ((info (make-encapsulation-info type underlying-fun
)))
225 (setf (fdefn-fun fdefn
)
226 (named-lambda encapsulation
(&rest args
)
227 (apply function
(encapsulation-info-definition info
)
230 ;;; This is like FIND-IF, except that we do it on a compiled closure's
232 (defun find-if-in-closure (test closure
)
233 (declare (closure closure
))
234 (do-closure-values (value closure
)
235 (when (funcall test value
)
238 ;;; Find the encapsulation info that has been closed over.
239 (defun encapsulation-info (fun)
241 (find-if-in-closure #'encapsulation-info-p fun
)))
243 ;;; When removing an encapsulation, we must remember that
244 ;;; encapsulating definitions close over a reference to the
245 ;;; ENCAPSULATION-INFO that describes the encapsulating definition.
246 ;;; When you find an info with the target type, the previous info in
247 ;;; the chain has the ensulating definition of that type. We take the
248 ;;; encapsulated definition from the info with the target type, and we
249 ;;; store it in the previous info structure whose encapsulating
250 ;;; definition it describes looks to this previous info structure for
251 ;;; a definition to bind (see ENCAPSULATE). When removing the first
252 ;;; info structure, we do something conceptually equal, but
253 ;;; mechanically it is different.
254 (defun unencapsulate (name type
)
255 "Removes NAME's most recent encapsulation of the specified TYPE."
256 (let* ((fdefn (find-fdefn name
))
257 (encap-info (encapsulation-info (fdefn-fun fdefn
))))
258 (declare (type (or encapsulation-info null
) encap-info
))
259 (when (and fdefn
(typep (fdefn-fun fdefn
) 'generic-function
))
260 (return-from unencapsulate
261 (unencapsulate-generic-function (fdefn-fun fdefn
) type
)))
262 (cond ((not encap-info
)
263 ;; It disappeared on us, so don't worry about it.
265 ((eq (encapsulation-info-type encap-info
) type
)
266 ;; It's the first one, so change the fdefn object.
267 (setf (fdefn-fun fdefn
)
268 (encapsulation-info-definition encap-info
)))
270 ;; It must be an interior one, so find it.
272 (let ((next-info (encapsulation-info
273 (encapsulation-info-definition encap-info
))))
275 ;; Not there, so don't worry about it.
277 (when (eq (encapsulation-info-type next-info
) type
)
278 ;; This is it, so unlink us.
279 (setf (encapsulation-info-definition encap-info
)
280 (encapsulation-info-definition next-info
))
282 (setf encap-info next-info
))))))
285 ;;; Does NAME have an encapsulation of the given TYPE?
286 (defun encapsulated-p (name type
)
287 (let ((fdefn (find-fdefn name
)))
288 (when (and fdefn
(typep (fdefn-fun fdefn
) 'generic-function
))
289 (return-from encapsulated-p
290 (encapsulated-generic-function-p (fdefn-fun fdefn
) type
)))
291 (do ((encap-info (encapsulation-info (fdefn-fun fdefn
))
293 (encapsulation-info-definition encap-info
))))
294 ((null encap-info
) nil
)
295 (declare (type (or encapsulation-info null
) encap-info
))
296 (when (eq (encapsulation-info-type encap-info
) type
)
301 ;;; KLUDGE: Er, it looks as though this means that
302 ;;; (FUNCALL (FDEFINITION 'FOO))
303 ;;; doesn't do the same thing as
305 ;;; and (SYMBOL-FUNCTION 'FOO) isn't in general the same thing
306 ;;; as (FDEFINITION 'FOO). That doesn't look like ANSI behavior to me.
307 ;;; Look e.g. at the ANSI definition of TRACE: "Whenever a traced
308 ;;; function is invoked, information about the call, ..". Try this:
309 ;;; (DEFUN FOO () (PRINT "foo"))
312 ;;; (FUNCALL (FDEFINITION 'FOO))
313 ;;; What to do? ANSI says TRACE "Might change the definitions of the
314 ;;; functions named by function-names." Might it be OK to just get
315 ;;; punt all this encapsulation stuff and go back to a simple but
316 ;;; correct implementation of TRACE? We'd lose the ability to redefine
317 ;;; a TRACEd function and keep the trace in place, but that seems
318 ;;; tolerable to me. (Is the wrapper stuff needed for anything else
321 ;;; The only problem I can see with not having a wrapper: If tracing
322 ;;; EQ, EQL, EQUAL, or EQUALP causes its function address to change,
323 ;;; it will mess up the MAKE-HASH-TABLE logic which uses EQ tests
324 ;;; on those function values. But given the ANSI statement about
325 ;;; TRACE causing things to change, that doesn't seem too unreasonable;
326 ;;; and we might even be able to forbid tracing these functions.
327 ;;; -- WHN 2001-11-02
328 (defun fdefinition (name)
329 "Return name's global function definition taking care to respect any
330 encapsulations and to return the innermost encapsulated definition.
332 (declare (explicit-check))
333 (let ((fun (%coerce-name-to-fun name
)))
335 (let ((encap-info (encapsulation-info fun
)))
337 (setf fun
(encapsulation-info-definition encap-info
))
340 (defvar *setf-fdefinition-hook
* nil
341 "A list of functions that (SETF FDEFINITION) invokes before storing the
342 new value. The functions take the function name and the new value.")
344 ;; Reject any "object of implementation-dependent nature" that
345 ;; so happens to be a function in SBCL, but which must not be
346 ;; bound to a function-name by way of (SETF FEDFINITION).
347 (defun err-if-unacceptable-function (object setter
)
348 (declare (notinline macro
/special-guard-fun-p
)) ; not performance-critical
349 (when (macro/special-guard-fun-p object
)
350 (error 'simple-reference-error
351 :references
(list '(:ansi-cl
:function fdefinition
))
352 :format-control
"~S is not acceptable to ~S."
353 :format-arguments
(list object setter
))))
355 (defun %set-fdefinition
(name new-value
)
356 "Set NAME's global function definition."
357 (declare (type function new-value
) (optimize (safety 1)))
358 (declare (explicit-check))
359 (err-if-unacceptable-function new-value
'(setf fdefinition
))
360 (with-single-package-locked-error (:symbol name
"setting fdefinition of ~A")
361 (maybe-clobber-ftype name new-value
)
363 ;; Check for hash-table stuff. Woe onto him that mixes encapsulation
365 (when (and (symbolp name
) (fboundp name
)
366 (boundp '*user-hash-table-tests
*))
367 (let ((old (symbol-function name
)))
368 (declare (special *user-hash-table-tests
*))
369 (dolist (spec *user-hash-table-tests
*)
370 (cond ((eq old
(second spec
))
372 (setf (second spec
) new-value
))
373 ((eq old
(third spec
))
375 (setf (third spec
) new-value
))))))
377 ;; FIXME: This is a good hook to have, but we should probably
378 ;; reserve it for users.
379 (let ((fdefn (find-or-create-fdefn name
)))
380 ;; *SETF-FDEFINITION-HOOK* won't be bound when initially running
381 ;; top level forms in the kernel core startup.
382 (when (boundp '*setf-fdefinition-hook
*)
383 (dolist (f *setf-fdefinition-hook
*)
384 (declare (type function f
))
385 (funcall f name new-value
)))
387 (let ((encap-info (encapsulation-info (fdefn-fun fdefn
))))
392 (encapsulation-info-definition encap-info
))))
394 (setf encap-info more-info
)
396 (setf (encapsulation-info-definition encap-info
)
399 (setf (fdefn-fun fdefn
) new-value
)))))))
401 ;;;; FBOUNDP and FMAKUNBOUND
403 (defun fboundp (name)
404 "Return true if name has a global function definition."
405 (declare (explicit-check))
406 (let ((fdefn (find-fdefn name
)))
407 (and fdefn
(fdefn-fun fdefn
) t
)))
409 (defun fmakunbound (name)
410 "Make NAME have no global function definition."
411 (declare (explicit-check))
412 (with-single-package-locked-error
413 (:symbol name
"removing the function or macro definition of ~A")
414 (let ((fdefn (find-fdefn name
)))
416 (fdefn-makunbound fdefn
)))
417 (undefine-fun-name name
)