1 ;;;; most of the DESCRIBE system -- that part which isn't derived
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB-IMPL") ;(SB-IMPL, not SB!IMPL, since we're built in warm load.)
15 (declaim (ftype (function (t stream)) describe-object))
16 (defgeneric describe-object (x stream))
18 (defun describe (x &optional (stream-designator *standard-output*))
20 "Print a description of the object X."
21 (let ((stream (out-synonym-of stream-designator)))
22 ;; Until sbcl-0.8.0.x, we did
23 ;; (FRESH-LINE STREAM)
24 ;; (PPRINT-LOGICAL-BLOCK (STREAM NIL)
26 ;; here. However, ANSI's specification of DEFUN DESCRIBE,
27 ;; DESCRIBE exists as an interface primarily to manage argument
28 ;; defaulting (including conversion of arguments T and NIL into
29 ;; stream objects) and to inhibit any return values from
31 ;; doesn't mention either FRESH-LINEing or PPRINT-LOGICAL-BLOCKing,
32 ;; and the example of typical DESCRIBE-OBJECT behavior in ANSI's
33 ;; specification of DESCRIBE-OBJECT will work poorly if we do them
34 ;; here. (The example method for DESCRIBE-OBJECT does its own
35 ;; FRESH-LINEing, which is a physical directive which works poorly
36 ;; inside a pretty-printer logical block.)
37 (describe-object x stream)
38 ;; We don't TERPRI here either (any more since sbcl-0.8.0.x), because
39 ;; again ANSI's specification of DESCRIBE doesn't mention it and
40 ;; ANSI's example of DESCRIBE-OBJECT does its own final TERPRI.
44 ;;;; miscellaneous DESCRIBE-OBJECT methods
46 (defmethod describe-object ((x t) s)
47 (format s "~&~@<~S ~_is a ~S.~:>~%" x (type-of x)))
49 (defmethod describe-object ((x cons) s)
51 (when (and (legal-fun-name-p x)
53 (%describe-fun (fdefinition x) s :function x)
54 ;;was: (format s "~@:_Its FDEFINITION is ~S.~@:_" (fdefinition x))
55 ;; TO DO: should check for SETF documentation.
56 ;; TO DO: should make it clear whether the definition is a
57 ;; DEFUN (SETF FOO) or DEFSETF FOO or what.
60 (defmethod describe-object ((x array) s)
62 (pprint-logical-block (s nil)
63 (let ((rank (array-rank x)))
66 "~S is a ~:[~;displaced ~]vector of length ~S." x
67 (and (array-header-p x)
68 (%array-displaced-p x)
70 (when (array-has-fill-pointer-p x)
71 (format s "~@:_It has a fill pointer, currently ~S."
74 (format s "~S ~_is " x)
75 (write-string (if (%array-displaced-p x) "a displaced" "an") s)
76 (format s " array of rank ~S." rank)
77 (format s "~@:_Its dimensions are ~S." (array-dimensions x)))))
78 (let ((array-element-type (array-element-type x)))
79 (unless (eq array-element-type t)
81 "~@:_Its element type is specialized to ~S."
82 array-element-type))))
85 (defmethod describe-object ((x hash-table) s)
86 (declare (type stream s))
87 (format s "~&~@<~S ~_is an ~S hash table.~:>" x (hash-table-test x))
88 (format s "~&Its SIZE is ~S." (hash-table-size x))
90 "~&~@<Its REHASH-SIZE is ~S. ~_Its REHASH-THRESHOLD is ~S.~:>"
91 (hash-table-rehash-size x)
92 (hash-table-rehash-threshold x))
94 (pprint-logical-block (s nil)
95 (let ((count (hash-table-count x)))
96 (format s "It holds ~S key/value pair~:P~:[: ~2I~_~;.~]"
99 (declare (type index n))
102 (write-char #\space s))
104 (when (and *print-length* (> n *print-length*))
107 (format s "~:_(~@<~S ~:_~S~:>)" k v)))))
110 (defmethod describe-object ((condition condition) s)
111 (sb-kernel:describe-condition condition s))
113 ;;;; DESCRIBE-OBJECT methods for symbols and functions, including all
114 ;;;; sorts of messy stuff about documentation, type information,
115 ;;;; packaging, function implementation, etc...
117 ;;; Print the specified kind of documentation about the given NAME. If
118 ;;; NAME is null, or not a valid name, then don't print anything.
119 (declaim (ftype (function (t stream t t) (values)) %describe-doc))
120 (defun %describe-doc (name s kind kind-doc)
121 (when (and name (typep name '(or symbol cons)))
122 (let ((doc (fdocumentation name kind)))
124 (format s "~&~@(~A documentation:~)~% ~A"
125 (or kind-doc kind) doc))))
128 ;;; Describe various stuff about the functional semantics attached to
129 ;;; the specified NAME, if NAME is the kind of thing you can look
130 ;;; up as a name. (In the case of anonymous closures and other
131 ;;; things, it might not be.) TYPE-SPEC is the function type specifier
132 ;;; extracted from the definition, or NIL if none.
133 (declaim (ftype (function (t stream t)) %describe-fun-name))
134 (defun %describe-fun-name (name s type-spec)
135 (when (and name (typep name '(or symbol cons)))
136 (multiple-value-bind (type where)
137 (if (legal-fun-name-p name)
138 (values (type-specifier (info :function :type name))
139 (info :function :where-from name))
140 (values type-spec :defined))
142 (format s "~&Its ~(~A~) argument types are:~% ~S"
144 (format s "~&Its result type is:~% ~S" (third type))))
145 (let ((inlinep (info :function :inlinep name)))
148 "~&It is currently declared ~(~A~);~
149 ~:[no~;~] expansion is available."
150 inlinep (info :function :inline-expansion-designator name))))))
152 ;;; Print information from the debug-info about where CODE-OBJ was
154 (defun %describe-compiled-from (code-obj s)
155 (declare (type stream s))
156 (let ((info (sb-kernel:%code-debug-info code-obj)))
158 (let ((sources (sb-c::debug-info-source info)))
160 (format s "~&On ~A it was compiled from:"
161 ;; FIXME: The FORMAT-UNIVERSAL-TIME calls in the system
162 ;; should become more consistent, probably not using
163 ;; any nondefault options.
164 (format-universal-time nil
165 (sb-c::debug-source-compiled
167 :style :abbreviated))
168 (dolist (source sources)
169 (let ((name (sb-c::debug-source-name source)))
170 (ecase (sb-c::debug-source-from source)
172 (format s "~&~A~@:_ Created: " (namestring name))
173 (format-universal-time s (sb-c::debug-source-created
175 (:lisp (format s "~&~S" name))))))))))
177 ;;; Describe a compiled function. The closure case calls us to print
179 (defun %describe-fun-compiled (x s kind name)
180 (declare (type stream s))
181 (let ((args (%simple-fun-arglist x)))
183 (write-string " There are no arguments." s))
185 (format s "~&~@(The ~@[~A's ~]arguments are:~@:_~)" kind)
187 (let ((*print-pretty* t)
191 (pprint-logical-block (s nil)
192 (pprint-indent :current 2)
193 (format s "~A" args))))))
194 (let ((name (or name (%simple-fun-name x))))
195 (%describe-doc name s 'function kind)
196 (unless (eq kind :macro)
197 (%describe-fun-name name s (%simple-fun-type x))))
198 (%describe-compiled-from (sb-kernel:fun-code-header x) s))
200 ;;; Describe a function object. KIND and NAME provide some information
201 ;;; about where the function came from.
202 (defun %describe-fun (x s &optional (kind :function) (name nil))
203 (declare (type function x))
204 (declare (type stream s))
205 (declare (type (member :macro :function) kind))
207 (pprint-logical-block (s nil)
209 (:macro (format s "Macro-function: ~S" x))
211 (format s "Function: ~S" x)
212 (format s "~S is a function." x))))
213 (format s "~@:_~@<Its associated name (as in ~S) is ~2I~_~S.~:>"
214 'function-lambda-expression
217 (#.sb-vm:closure-header-widetag
218 (%describe-fun-compiled (%closure-fun x) s kind name)
219 (format s "~@:_Its closure environment is:")
220 (pprint-logical-block (s nil)
221 (pprint-indent :current 8)
222 (dotimes (i (- (get-closure-length x) (1- sb-vm:closure-info-offset)))
223 (format s "~@:_~S: ~S" i (%closure-index-ref x i)))))
224 ((#.sb-vm:simple-fun-header-widetag #.sb-vm:closure-fun-header-widetag)
225 (%describe-fun-compiled x s kind name))
226 (#.sb-vm:funcallable-instance-header-widetag
227 ;; Only STANDARD-GENERIC-FUNCTION would be handled here, but
228 ;; since it has its own DESCRIBE-OBJECT method, it should've been
229 ;; picked off before getting here. So hopefully we never get here.
230 (format s "~@:_It is an unknown type of funcallable instance."))
232 (format s "~@:_It is an unknown type of function."))))
235 (defmethod describe-object ((x function) s)
236 (%describe-fun x s :function))
238 (defgeneric describe-symbol-fdefinition (function stream &key name))
240 (defmethod describe-symbol-fdefinition ((fun function) stream &key name)
241 (%describe-fun fun stream :function name))
243 (defmethod describe-symbol-fdefinition ((fun standard-generic-function) stream
245 (declare (ignore name))
247 (describe-object fun stream))
249 (defmethod describe-object ((x symbol) s)
250 (declare (type stream s))
252 ;; Describe the packaging.
253 (let ((package (symbol-package x)))
255 (multiple-value-bind (symbol status)
256 (find-symbol (symbol-name x) package)
257 (declare (ignore symbol))
258 (format s "~&~@<~S is ~_an ~(~A~) symbol ~_in ~S.~:>"
259 x status (symbol-package x)))
260 (format s "~&~@<~S is ~_an uninterned symbol.~:>" x)))
261 ;; TO DO: We could grovel over all packages looking for and
262 ;; reporting other phenomena, e.g. IMPORT and SHADOW, or
263 ;; availability in some package even after (SYMBOL-PACKAGE X) has
266 ;; Describe the value cell.
267 (let* ((kind (info :variable :kind x))
269 (:special "special variable")
270 (:macro "symbol macro")
271 (:constant "constant")
272 (:global "undefined variable")
274 (pprint-logical-block (s nil)
277 (let ((info (info :variable :alien-info x)))
278 (format s "~&~@<It is an alien at #X~8,'0X of type ~3I~:_~S.~:>"
279 (sap-int (eval (sb-alien::heap-alien-info-sap-form info)))
280 (sb-alien-internals:unparse-alien-type
281 (sb-alien::heap-alien-info-type info)))
282 (format s "~&~@<Its current value is ~3I~:_~S.~:>"
285 (let ((expansion (info :variable :macro-expansion x)))
286 (format s "~&It is a ~A with expansion ~S." wot expansion)))
288 (format s "~&~@<It is a ~A; its ~_value is ~S.~:>"
289 wot (symbol-value x)))
290 ((not (eq kind :global))
291 (format s "~&~@<It is a ~A; no current value.~:>" wot)))
293 (when (eq (info :variable :where-from x) :declared)
294 (format s "~&~@<Its declared type ~_is ~S.~:>"
295 (type-specifier (info :variable :type x)))))
297 (%describe-doc x s 'variable kind))
299 ;; Print out properties.
300 (format s "~@[~&Its SYMBOL-PLIST is ~@<~2I~_~S~:>.~]" (symbol-plist x))
302 ;; Describe the function cell.
303 (cond ((macro-function x)
304 (%describe-fun (macro-function x) s :macro x))
305 ((special-operator-p x)
306 (%describe-doc x s :function "Special form"))
308 (describe-symbol-fdefinition (fdefinition x) s :name x)))
310 ;; Print other documentation.
311 (%describe-doc x s 'structure "Structure")
312 (%describe-doc x s 'type "Type")
313 (%describe-doc x s 'setf "Setf macro")
314 (dolist (assoc (info :random-documentation :stuff x))
316 "~&~@<Documentation on the ~(~A~):~@:_~A~:>"
320 ;; Mention the associated type information, if any.
322 ;; As of sbcl-0.7.2, (INFO :TYPE :KIND X) might be
323 ;; * :PRIMITIVE, which is handled by the FIND-CLASS case.
324 ;; * :DEFINED, which is handled specially.
325 ;; * :INSTANCE, which is handled by the FIND-CLASS case.
326 ;; * :FORTHCOMING-DEFCLASS-TYPE, which is an internal-to-the-compiler
327 ;; note that we don't try to report.
328 ;; * NIL, in which case there's nothing to see here, move along.
329 (when (eq (info :type :kind x) :defined)
330 (format s "~&It names a type specifier."))
331 (let ((symbol-named-class (find-class x nil)))
332 (when symbol-named-class
333 (format s "~&It names a class ~A." symbol-named-class)
334 (describe symbol-named-class s)))