1 ;;;; miscellaneous functions which use INFO
3 ;;;; (In CMU CL, these were in globaldb.lisp. They've been moved here
4 ;;;; because references to INFO can't be compiled correctly until
5 ;;;; globaldb initialization is complete, and the SBCL technique for
6 ;;;; initializing the global database in the cross-compiler isn't
7 ;;;; completed until load time.)
9 ;;;; This software is part of the SBCL system. See the README file for
10 ;;;; more information.
12 ;;;; This software is derived from the CMU CL system, which was
13 ;;;; written at Carnegie Mellon University and released into the
14 ;;;; public domain. The software is in the public domain and is
15 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
16 ;;;; files for more information.
20 ;;;; internal utilities defined in terms of INFO
22 (defun check-variable-name (name &key
(context "local variable") (signal-via #'compiler-error
))
23 (unless (legal-variable-name-p name
)
24 (funcall signal-via
"~@<~S is ~:[not a symbol~;a keyword~] and ~
25 cannot be used as a ~A.~@:>"
26 name
(keywordp name
) context
))
29 ;;; Check that NAME is a valid function name, returning the name if
30 ;;; OK, and signalling an error if not. In addition to checking for
31 ;;; basic well-formedness, we also check that symbol names are not NIL
32 ;;; or the name of a special form.
33 (defun check-fun-name (name)
36 (unless (legal-fun-name-p name
)
37 (compiler-error "~@<Illegal function name: ~S.~@:>" name
)))
39 (when (eq (info :function
:kind name
) :special-form
)
40 (compiler-error "~@<Special form is an illegal function name: ~S.~@:>"
43 (compiler-error "~@<Illegal function name: ~S.~@:>" name
)))
46 ;;; Record a new function definition, and check its legality.
47 (defun proclaim-as-fun-name (name)
52 ;; KLUDGE: This can happen when eg. compiling a NAMED-LAMBDA, and isn't
53 ;; guarded against elsewhere -- so we want to assert package locks here. The
54 ;; reason we do it only when stomping on existing stuff is because we want
56 ;; (WITHOUT-PACKAGE-LOCKS (DEFUN LOCKED:FOO ...))
57 ;; viable, which requires no compile-time violations in the harmless cases.
58 (with-single-package-locked-error ()
60 (assert-symbol-home-package-unlocked name
"proclaiming ~S as a function")))
62 (let ((kind (info :function
:kind name
)))
63 ;; scrubbing old data I: possible collision with a macro
64 (when (and (fboundp name
) (eq :macro kind
))
66 (compiler-style-warn "~S was previously defined as a macro." name
)
67 (setf (info :function
:where-from name
) :assumed
)
68 (clear-info :function
:macro-function name
))
70 (unless (eq :function kind
)
72 (setf (info :function
:kind name
) :function
)))))
74 ;; scrubbing old data II: dangling forward references
76 ;; (This could happen if someone executes PROCLAIM FTYPE at
77 ;; macroexpansion time, which is bad style, or at compile time, e.g.
78 ;; in EVAL-WHEN (:COMPILE) inside something like DEFSTRUCT, in which
79 ;; case it's reasonable style. Either way, NAME is no longer a free
81 (when (boundp '*free-funs
*) ; when compiling
82 (remhash name
*free-funs
*))
86 ;;; This is called to do something about SETF functions that overlap
87 ;;; with SETF macros. Perhaps we should interact with the user to see
88 ;;; whether the macro should be blown away, but for now just give a
89 ;;; warning. Due to the weak semantics of the (SETF FUNCTION) name, we
90 ;;; can't assume that they aren't just naming a function (SETF FOO)
91 ;;; for the heck of it. NAME is already known to be well-formed.
92 (defun warn-if-setf-macro (name)
93 ;; Never warn about this situation when running the cross-compiler.
94 ;; SBCL provides expanders/inverses *and* functions for most SETFable things
95 ;; even when CLHS does not specifically state that #'(SETF x) exists.
96 #+sb-xc-host
(declare (ignore name
))
98 (let ((stem (second name
)))
99 (when (info :setf
:expander stem
)
101 "defining function ~S when ~S already has a SETF macro"
105 ;;; Make NAME no longer be a function name: clear everything back to
107 (defun undefine-fun-name (name)
109 (macrolet ((frob (&rest types
)
111 name
',(mapcar (lambda (x)
112 (meta-info-number (meta-info :function x
)))
114 ;; Note that this does not clear the :DEFINITION.
115 ;; That's correct, because if we lose the association between a
116 ;; symbol and its #<fdefn> object, it could lead to creation of
117 ;; a non-unique #<fdefn> for a name.
119 :type
; Hmm. What if it was proclaimed- shouldn't it stay?
124 :inline-expansion-designator
129 ;;; part of what happens with DEFUN, also with some PCL stuff: Make
130 ;;; NAME known to be a function definition.
131 (defun become-defined-fun-name (name)
132 (proclaim-as-fun-name name
)
133 (when (eq (info :function
:where-from name
) :assumed
)
134 (setf (info :function
:where-from name
) :defined
)
135 (if (info :function
:assumed-type name
)
136 (clear-info :function
:assumed-type name
))))
138 ;;; Trivially wrap (INFO :FUNCTION :INLINE-EXPANSION-DESIGNATOR FUN-NAME)
139 (declaim (ftype (function ((or symbol cons
)) list
) fun-name-inline-expansion
))
140 (defun fun-name-inline-expansion (fun-name)
141 (multiple-value-bind (answer winp
)
142 (info :function
:inline-expansion-designator fun-name
)
143 (when (and (not winp
) (symbolp fun-name
))
144 (let ((info (info :function
:type fun-name
)))
145 (when (typep info
'defstruct-description
)
146 (let ((spec (assq fun-name
(dd-constructors info
))))
148 (setq answer
`(lambda ,@(structure-ctor-lambda-parts
151 (values answer winp
)))
153 ;;;; ANSI Common Lisp functions which are defined in terms of the info
156 (defun sb!xc
:macro-function
(symbol &optional env
)
157 "If SYMBOL names a macro in ENV, returns the expansion function,
158 else returns NIL. If ENV is unspecified or NIL, use the global environment
160 ;; local function definitions (ordinary) can shadow a global macro
162 #!+(and sb-fasteval
(host-feature sb-xc
))
163 (sb!interpreter
:basic-env
164 (multiple-value-bind (kind def
)
165 (sb!interpreter
:find-lexical-fun env symbol
)
167 (return-from sb
!xc
:macro-function
(when (eq kind
:macro
) def
)))))
169 (let ((def (cdr (assoc symbol
(lexenv-funs env
)))))
171 (return-from sb
!xc
:macro-function
172 (when (typep def
'(cons (eql macro
))) (cdr def
)))))))
173 (values (info :function
:macro-function symbol
)))
175 (defun (setf sb
!xc
:macro-function
) (function symbol
&optional environment
)
176 (declare (symbol symbol
) (type function function
))
178 ;; Note: Technically there could be an ENV optional argument to SETF
179 ;; MACRO-FUNCTION, but since ANSI says that the consequences of
180 ;; supplying a non-nil one are undefined, we don't allow it.
181 ;; (Thus our implementation of this unspecified behavior is to
182 ;; complain. SInce the behavior is unspecified, this is conforming.:-)
183 (error "Non-NIL environment argument in SETF of MACRO-FUNCTION ~S: ~S"
185 (when (eq (info :function
:kind symbol
) :special-form
)
186 (error "~S names a special form." symbol
))
187 (with-single-package-locked-error (:symbol symbol
"setting the macro-function of ~S")
188 (clear-info :function
:type symbol
)
189 (setf (info :function
:kind symbol
) :macro
)
190 (setf (info :function
:macro-function symbol
) function
)
191 #-sb-xc-host
(install-guard-function symbol
`(:macro
,symbol
) nil
))
194 ;; Set (SYMBOL-FUNCTION SYMBOL) to a closure that signals an error,
195 ;; preventing funcall/apply of macros and special operators.
197 (defun install-guard-function (symbol fun-name docstring
)
199 (setf (random-documentation symbol
'function
) docstring
))
200 ;; (SETF SYMBOL-FUNCTION) goes out of its way to disallow this closure,
201 ;; but we can trivially replicate its low-level effect.
202 (let ((fdefn (find-or-create-fdefn symbol
))
204 (sb!impl
::set-closure-name
206 (declare (ignore args
))
207 ;; ANSI specification of FUNCALL says that this should be
208 ;; an error of type UNDEFINED-FUNCTION, not just SIMPLE-ERROR.
209 ;; SPECIAL-FORM-FUNCTION is a subtype of UNDEFINED-FUNCTION.
210 (error (if (eq (info :function
:kind symbol
) :special-form
)
211 'special-form-function
215 ;; For immobile-code, do something slightly different: fmakunbound,
216 ;; then assign the fdefn-fun slot to avoid consing a new closure trampoline.
218 (progn (fdefn-makunbound fdefn
)
219 ;; There is no :SET-TRANS for the primitive object's fdefn-fun slot,
220 ;; nor do we desire the full effect of %SET-FDEFN-FUN.
221 (setf (sap-ref-lispobj (int-sap (get-lisp-obj-address fdefn
))
222 (- (ash sb
!vm
:fdefn-fun-slot sb
!vm
:word-shift
)
223 sb
!vm
:other-pointer-lowtag
))
225 ;; The above would work, but there's no overhead when installing a closure
226 ;; the regular way, so just do that.
228 (setf (fdefn-fun fdefn
) closure
)))
230 (defun sb!xc
:compiler-macro-function
(name &optional env
)
231 "If NAME names a compiler-macro in ENV, return the expansion function, else
232 return NIL. Can be set with SETF when ENV is NIL."
233 (legal-fun-name-or-type-error name
)
234 ;; CLHS 3.2.2.1: Creating a lexical binding for the function name
235 ;; not only creates a new local function or macro definition, but
236 ;; also shadows[2] the compiler macro.
237 (unless (fun-locally-defined-p name env
)
238 ;; Note: CMU CL used to return NIL here when a NOTINLINE
239 ;; declaration was in force. That's fairly logical, given the
240 ;; specified effect of NOTINLINE declarations on compiler-macro
241 ;; expansion. However, (1) it doesn't seem to be consistent with
242 ;; the ANSI spec for COMPILER-MACRO-FUNCTION, and (2) it would
243 ;; give surprising behavior for (SETF (COMPILER-MACRO-FUNCTION
244 ;; FOO) ...) in the presence of a (PROCLAIM '(NOTINLINE FOO)). So
246 (values (info :function
:compiler-macro-function name
))))
248 ;;; FIXME: we don't generate redefinition warnings for these.
249 (defun (setf sb
!xc
:compiler-macro-function
) (function name
&optional env
)
250 (declare (type (or symbol list
) name
)
251 (type (or function null
) function
))
253 ;; ANSI says this operation is undefined.
254 (error "can't SETF COMPILER-MACRO-FUNCTION when ENV is non-NIL"))
255 (when (eq (info :function
:kind name
) :special-form
)
256 (error "~S names a special form." name
))
257 (with-single-package-locked-error
258 (:symbol name
"setting the compiler-macro-function of ~A")
259 (setf (info :function
:compiler-macro-function name
) function
)
262 ;;;; a subset of DOCUMENTATION functionality for bootstrapping
264 ;;; FDOCUMENTATION is like DOCUMENTATION, but with less functionality,
265 ;;; and implemented with DEFUN instead of DEFGENERIC so that it can
266 ;;; run before CLOS is set up. Supported DOC-TYPE values are
273 ;;; FIXME: Other types end up in INFO :RANDOM-DOCUMENTATION :STUFF. I
274 ;;; should add some code to monitor this and make sure that nothing is
275 ;;; unintentionally being sent to never never land this way.
276 ;;; FIXME: Rename FDOCUMENTATION to BDOCUMENTATION, by analogy with
277 ;;; DEF!STRUCT and so forth. And consider simply saving
278 ;;; all the BDOCUMENTATION entries in a *BDOCUMENTATION* hash table
279 ;;; and slamming them into PCL once PCL gets going.
280 (defun (setf fdocumentation
) (string name doc-type
)
281 (declare (type (or null string
) string
))
282 #+sb-xc-host
(declare (ignore name doc-type
))
285 (macrolet ((info-number (class type
)
286 (meta-info-number (meta-info class type
))))
288 (variable (info-number :variable
:documentation
))
290 (cond ((eq (info :type
:kind name
) :instance
)
291 (info-number :type
:documentation
))
292 ((info :typed-structure
:info name
)
293 (info-number :typed-structure
:documentation
))))
294 (type (info-number :type
:documentation
))
295 (setf (info-number :setf
:documentation
))))))
298 (set-info-value name info-number string
)
299 (clear-info-values name
(list info-number
))))
300 ((eq doc-type
'function
)
301 ;; FIXME: this silently loses
302 ;; * (setf (documentation '(a bad name) 'function) "x") => "x"
303 ;; * (documentation '(a bad name) 'function) => NIL
304 ;; which is fine because as noted in pcl/documentation.lsp
305 ;; even for supported doc types an implementation is permitted
306 ;; to discard docs at any time
307 ;; but should a warning be issued just as for an unknown DOC-TYPE?
309 ;; And there's additional weirdness if you do, in this order -
310 ;; * (setf (documentation 'foo 'function) "hi")
311 ;; * (defun foo () "hey" 1)
312 ;; * (documentation 'foo 'function) => "hi" ; should be "hey"
313 ;; CLHS says regarding DEFUN:
314 ;; " Documentation is attached as a documentation string to
315 ;; /name/ (as kind function) and to the /function object/."
316 (cond ((not (legal-fun-name-p name
)))
317 ((not (equal (real-function-name name
) name
))
318 (setf (random-documentation name
'function
) string
))
320 (setf (%fun-doc
(fdefinition name
)) string
))))
321 ((typep name
'(or symbol cons
))
322 (setf (random-documentation name doc-type
) string
))))
326 (defun real-function-name (name)
327 ;; Resolve the actual name of the function named by NAME
328 ;; e.g. (setf (name-function 'x) #'car)
329 ;; (real-function-name 'x) => CAR
330 (cond ((not (fboundp name
))
333 (macro-function name
))
334 (let ((name (%fun-name
(macro-function name
))))
336 (eq (car name
) 'macro-function
)
339 (%fun-name
(fdefinition name
)))))
342 (defun random-documentation (name type
)
343 (cdr (assoc type
(info :random-documentation
:stuff name
))))
346 (defun (setf random-documentation
) (new-value name type
)
347 (let ((pair (assoc type
(info :random-documentation
:stuff name
))))
349 (setf (cdr pair
) new-value
)
350 (push (cons type new-value
)
351 (info :random-documentation
:stuff name
))))
354 ;; Return the number of calls to NAME that IR2 emitted as full calls,
355 ;; not counting calls via #'F that went untracked.
356 ;; Return 0 if the answer is nonzero but a warning was already signaled
357 ;; about any full calls were emitted. This return convention satisfies the
358 ;; intended use of this statistic - to decide whether to generate a warning
359 ;; about failure to inline NAME, which is shown at most once per name
360 ;; to avoid unleashing a flood of identical warnings.
361 (defun emitted-full-call-count (name)
362 (let ((status (car (info :function
:emitted-full-calls name
))))
363 (and (integerp status
)
364 ;; Bit 0 tells whether any call was NOT in the presence of
365 ;; a 'notinline' declaration, thus eligible to be inline.
366 ;; Bit 1 tells whether any warning was emitted yet.
367 (= (logand status
3) #b01
)
368 (ash status -
2)))) ; the call count as tracked by IR2