1 ;;;; most of the DESCRIBE mechanism -- 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 #.*optimize-byte-compilation*)
18 (defvar *describe-indentation-step* 3
20 "the number of spaces that sets off each line of a recursive description")
22 (declaim (ftype (function (t stream)) describe-object))
23 (defgeneric describe-object ((x t) stream))
25 (defun describe (x &optional (stream-designator *standard-output*))
27 "Print a description of the object X."
28 (declare #.*optimize-external-despite-byte-compilation*)
29 (let ((stream (out-synonym-of stream-designator)))
30 (pprint-logical-block (stream nil)
32 (describe-object x stream)
36 ;;;; miscellaneous DESCRIBE-OBJECT methods
38 (defmethod describe-object ((x t) s)
39 (format s "~@<~S ~_is a ~S.~:>" x (type-of x)))
41 (defmethod describe-object ((x cons) s)
43 (when (and (legal-function-name-p x)
45 (format s "Its FDEFINITION is ~S.~@:_" (fdefinition x))
46 ;; TO DO: should check for SETF documentation.
47 ;; TO DO: should make it clear whether the definition is a
48 ;; DEFUN (SETF FOO) or DEFSETF FOO or what.
51 (defmethod describe-object ((x array) s)
52 (let ((rank (array-rank x)))
54 (format s "~S ~_is " x)
55 (write-string (if (%array-displaced-p x) "a displaced" "an") s)
56 (format s " array of rank ~S." rank)
57 (format s "~@:_Its dimensions are ~S." (array-dimensions x)))
60 "~@:_~S is a ~:[~;displaced ~]vector of length ~S." x
61 (and (array-header-p x) (%array-displaced-p x)) (length x))
62 (when (array-has-fill-pointer-p x)
63 (format s "~@:_It has a fill pointer, currently ~S."
65 (let ((array-element-type (array-element-type x)))
66 (unless (eq array-element-type t)
68 "~@:_Its element type is specialized to ~S."
69 array-element-type))))
71 (defmethod describe-object ((x hash-table) s)
72 (declare (type stream s))
73 (format s "~@<~S ~_is an ~S hash table.~:>" x (hash-table-test x))
74 (format s "~_Its SIZE is ~S." (hash-table-size x))
76 "~@:_~@<Its REHASH-SIZE is ~S. ~_Its REHASH-THRESHOLD is ~S.~:>"
77 (hash-table-rehash-size x)
78 (hash-table-rehash-threshold x))
79 (let ((count (hash-table-count x)))
80 (format s "~@:_It holds ~S key/value pair~:P~:[: ~2I~_~;.~]"
83 (declare (type index n))
86 (write-char #\space s))
88 (when (and *print-length* (> n *print-length*))
91 (format s "~:_(~@<~S ~:_~S~:>)" k v)))))
93 (defmethod describe-object ((condition condition) s)
94 (sb-kernel:describe-condition condition s))
96 ;;;; DESCRIBE-OBJECT methods for symbols and functions, including all
97 ;;;; sorts of messy stuff about documentation, type information,
98 ;;;; packaging, function implementation, etc..
100 ;;; Print the specified kind of documentation about the given NAME. If
101 ;;; NAME is null, or not a valid name, then don't print anything.
102 (declaim (ftype (function (t stream t t) (values)) %describe-doc))
103 (defun %describe-doc (name s kind kind-doc)
104 (when (and name (typep name '(or symbol cons)))
105 (let ((doc (fdocumentation name kind)))
107 (format s "~_~@(~A documentation:~)~@:_ ~A"
108 (or kind-doc kind) doc))))
111 ;;; Describe various stuff about the functional semantics attached to
112 ;;; the specified NAME, if NAME is the kind of thing you can look
113 ;;; up as a name. (In the case of anonymous closures and other
114 ;;; things, it might not be.) TYPE-SPEC is the function type specifier
115 ;;; extracted from the definition, or NIL if none.
116 (declaim (ftype (function (t stream t)) %describe-function-name))
117 (defun %describe-function-name (name s type-spec)
118 (when (and name (typep name '(or symbol cons)))
119 (multiple-value-bind (type where)
120 (if (or (symbolp name) (and (listp name) (eq (car name) 'setf)))
121 (values (type-specifier (info :function :type name))
122 (info :function :where-from name))
123 (values type-spec :defined))
125 (format s "~@:_Its ~(~A~) argument types are:~@:_ ~S"
127 (format s "~@:_Its result type is:~@:_ ~S" (third type))))
128 (let ((inlinep (info :function :inlinep name)))
131 "~@:_It is currently declared ~(~A~);~
132 ~:[no~;~] expansion is available."
133 inlinep (info :function :inline-expansion name))))))
135 ;;; Print information from the debug-info about where CODE-OBJ was
137 (defun %describe-compiled-from (code-obj s)
138 (declare (type stream s))
139 (let ((info (sb-kernel:%code-debug-info code-obj)))
141 (let ((sources (sb-c::debug-info-source info)))
143 (format s "~@:_On ~A it was compiled from:"
144 ;; FIXME: The FORMAT-UNIVERSAL-TIME calls in the system
145 ;; should become more consistent, probably not using
146 ;; any nondefault options.
147 (format-universal-time nil
148 (sb-c::debug-source-compiled
150 :style :abbreviated))
151 (dolist (source sources)
152 (let ((name (sb-c::debug-source-name source)))
153 (ecase (sb-c::debug-source-from source)
155 (format s "~@:_~A~@:_ Created: " (namestring name))
156 (format-universal-time s (sb-c::debug-source-created
158 (:lisp (format s "~@:_~S" name))))))))))
160 ;;; Describe a compiled function. The closure case calls us to print
162 (defun %describe-function-compiled (x s kind name)
163 (declare (type stream s))
164 ;; FIXME: The lowercaseness of %FUNCTION-ARGLIST results, and the
165 ;; non-sentenceness of the "Arguments" label, makes awkward output.
166 ;; Better would be "Its arguments are: ~S" (with uppercase argument
167 ;; names) when arguments are known, and otherwise "There is no
168 ;; information available about its arguments." or "It has no
169 ;; arguments." (And why is %FUNCTION-ARGLIST a string instead of a
170 ;; list of symbols anyway?)
171 (let ((args (%function-arglist x)))
172 (format s "~@:_~@(~@[~A ~]arguments:~@:_~)" kind)
174 (format s " There is no argument information available."))
176 (write-string " There are no arguments." s))
179 (pprint-logical-block (s nil)
180 (pprint-indent :current 2)
181 (write-string args s)))))
182 (let ((name (or name (%function-name x))))
183 (%describe-doc name s 'function kind)
184 (unless (eq kind :macro)
185 (%describe-function-name name s (%function-type x))))
186 (%describe-compiled-from (sb-kernel:function-code-header x) s))
188 (defun %describe-function-byte-compiled (x s kind name)
189 (declare (type stream s))
190 (let ((name (or name (sb-c::byte-function-name x))))
191 (%describe-doc name s 'function kind)
192 (unless (eq kind :macro)
193 (%describe-function-name name s 'function)))
194 (%describe-compiled-from (sb-c::byte-function-component x) s))
196 ;;; Describe a function with the specified kind and name. The latter
197 ;;; arguments provide some information about where the function came
198 ;;; from. Kind NIL means not from a name.
199 (defun %describe-function (x s &optional (kind nil) name)
200 (declare (type function x))
201 (declare (type stream s))
202 (declare (type (member :macro :function nil) kind))
205 (:macro (format s "Macro-function: ~S" x))
206 (:function (format s "Function: ~S" x))
207 ((nil) (format s "~S is a function." x)))
209 (#.sb-vm:closure-header-type
210 (%describe-function-compiled (%closure-function x) s kind name)
211 (format s "~@:_Its closure environment is:")
212 (pprint-logical-block (s nil)
213 (pprint-indent :current 8)
214 (dotimes (i (- (get-closure-length x) (1- sb-vm:closure-info-offset)))
215 (format s "~@:_~S: ~S" i (%closure-index-ref x i)))))
216 ((#.sb-vm:function-header-type #.sb-vm:closure-function-header-type)
217 (%describe-function-compiled x s kind name))
218 (#.sb-vm:funcallable-instance-header-type
220 (sb-kernel:byte-function
221 (%describe-function-byte-compiled x s kind name))
222 (sb-kernel:byte-closure
223 (%describe-function-byte-compiled (byte-closure-function x)
225 (format s "~@:_Its closure environment is:")
226 (pprint-logical-block (s nil)
227 (pprint-indent :current 8)
228 (let ((data (byte-closure-data x)))
229 (dotimes (i (length data))
230 (format s "~@:_~S: ~S" i (svref data i))))))
231 (standard-generic-function
232 ;; There should be a special method for this case; we'll
234 (describe-object x s))
236 (format s "~@:_It is an unknown type of funcallable instance."))))
238 (format s "~@:_It is an unknown type of function."))))
240 (defmethod describe-object ((x function) s)
241 (%describe-function x s))
243 (defmethod describe-object ((x symbol) s)
244 (declare (type stream s))
246 ;; Describe the packaging.
247 (let ((package (symbol-package x)))
249 (multiple-value-bind (symbol status)
250 (find-symbol (symbol-name x) package)
251 (declare (ignore symbol))
252 (format s "~S is ~_an ~(~A~) symbol ~_in ~S."
253 x status (symbol-package x)))
254 (format s "~S is ~_an uninterned symbol." x)))
255 ;; TO DO: We could grovel over all packages looking for and
256 ;; reporting other phenomena, e.g. IMPORT and SHADOW, or
257 ;; availability in some package even after (SYMBOL-PACKAGE X) has
260 ;; Describe the value cell.
261 (let* ((kind (info :variable :kind x))
263 (:special "special variable")
264 (:constant "constant")
265 (:global "undefined variable")
269 (let ((info (info :variable :alien-info x)))
270 (format s "~@:_~@<It is an alien at #X~8,'0X of type ~3I~:_~S.~:>~@:_"
271 (sap-int (eval (sb-alien::heap-alien-info-sap-form info)))
272 (sb-alien-internals:unparse-alien-type
273 (sb-alien::heap-alien-info-type info)))
274 (format s "~@<Its current value is ~3I~:_~S.~:>"
277 (format s "~@:_It is a ~A; its ~_value is ~S." wot (symbol-value x)))
278 ((not (eq kind :global))
279 (format s "~@:_It is a ~A; no current value." wot)))
281 (when (eq (info :variable :where-from x) :declared)
282 (format s "~@:_Its declared type ~_is ~S."
283 (type-specifier (info :variable :type x))))
285 (%describe-doc x s 'variable kind))
287 ;; Print out properties.
288 (format s "~@[~@:_Its SYMBOL-PLIST is ~@<~2I~_~S~:>.~]" (symbol-plist x))
290 ;; Describe the function cell.
291 (cond ((macro-function x)
292 (%describe-function (macro-function x) s :macro x))
293 ((special-operator-p x)
294 (%describe-doc x s 'function "Special form"))
296 (%describe-function (fdefinition x) s :function x)))
298 ;; FIXME: Print out other stuff from the INFO database:
299 ;; * Does it name a type?
300 ;; * Is it a structure accessor? (This is important since those are
301 ;; magical in some ways, e.g. blasting the structure if you
304 ;; Print other documentation.
305 (%describe-doc x s 'structure "Structure")
306 (%describe-doc x s 'type "Type")
307 (%describe-doc x s 'setf "Setf macro")
309 (dolist (assoc (info :random-documentation :stuff x))
311 "~@:_Documentation on the ~(~A~):~@:_~A"
315 ;; Describe the associated class, if any.
316 (let ((symbol-named-class (cl:find-class x nil)))
317 (when symbol-named-class
318 (format t "~&It names a class ~A." symbol-named-class)
319 (describe symbol-named-class))))