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")
15 (defvar *describe-indentation-step* 3
17 "the number of spaces that sets off each line of a recursive description")
19 (declaim (ftype (function (t stream)) describe-object))
20 (defgeneric describe-object ((x t) stream))
22 (defun describe (x &optional (stream-designator *standard-output*))
24 "Print a description of the object X."
25 (let ((stream (out-synonym-of stream-designator)))
26 (pprint-logical-block (stream nil)
28 (describe-object x stream)
32 ;;;; miscellaneous DESCRIBE-OBJECT methods
34 (defmethod describe-object ((x t) s)
35 (format s "~@<~S ~_is a ~S.~:>" x (type-of x)))
37 (defmethod describe-object ((x cons) s)
39 (when (and (legal-function-name-p x)
41 (format s "Its FDEFINITION is ~S.~@:_" (fdefinition x))
42 ;; TO DO: should check for SETF documentation.
43 ;; TO DO: should make it clear whether the definition is a
44 ;; DEFUN (SETF FOO) or DEFSETF FOO or what.
47 (defmethod describe-object ((x array) s)
48 (let ((rank (array-rank x)))
50 (format s "~S ~_is " x)
51 (write-string (if (%array-displaced-p x) "a displaced" "an") s)
52 (format s " array of rank ~S." rank)
53 (format s "~@:_Its dimensions are ~S." (array-dimensions x)))
56 "~@:_~S is a ~:[~;displaced ~]vector of length ~S." x
57 (and (array-header-p x) (%array-displaced-p x)) (length x))
58 (when (array-has-fill-pointer-p x)
59 (format s "~@:_It has a fill pointer, currently ~S."
61 (let ((array-element-type (array-element-type x)))
62 (unless (eq array-element-type t)
64 "~@:_Its element type is specialized to ~S."
65 array-element-type))))
67 (defmethod describe-object ((x hash-table) s)
68 (declare (type stream s))
69 (format s "~@<~S ~_is an ~S hash table.~:>" x (hash-table-test x))
70 (format s "~_Its SIZE is ~S." (hash-table-size x))
72 "~@:_~@<Its REHASH-SIZE is ~S. ~_Its REHASH-THRESHOLD is ~S.~:>"
73 (hash-table-rehash-size x)
74 (hash-table-rehash-threshold x))
75 (let ((count (hash-table-count x)))
76 (format s "~@:_It holds ~S key/value pair~:P~:[: ~2I~_~;.~]"
79 (declare (type index n))
82 (write-char #\space s))
84 (when (and *print-length* (> n *print-length*))
87 (format s "~:_(~@<~S ~:_~S~:>)" k v)))))
89 (defmethod describe-object ((condition condition) s)
90 (sb-kernel:describe-condition condition s))
92 ;;;; DESCRIBE-OBJECT methods for symbols and functions, including all
93 ;;;; sorts of messy stuff about documentation, type information,
94 ;;;; packaging, function implementation, etc..
96 ;;; Print the specified kind of documentation about the given NAME. If
97 ;;; NAME is null, or not a valid name, then don't print anything.
98 (declaim (ftype (function (t stream t t) (values)) %describe-doc))
99 (defun %describe-doc (name s kind kind-doc)
100 (when (and name (typep name '(or symbol cons)))
101 (let ((doc (fdocumentation name kind)))
103 (format s "~_~@(~A documentation:~)~@:_ ~A"
104 (or kind-doc kind) doc))))
107 ;;; Describe various stuff about the functional semantics attached to
108 ;;; the specified NAME, if NAME is the kind of thing you can look
109 ;;; up as a name. (In the case of anonymous closures and other
110 ;;; things, it might not be.) TYPE-SPEC is the function type specifier
111 ;;; extracted from the definition, or NIL if none.
112 (declaim (ftype (function (t stream t)) %describe-function-name))
113 (defun %describe-function-name (name s type-spec)
114 (when (and name (typep name '(or symbol cons)))
115 (multiple-value-bind (type where)
116 (if (or (symbolp name) (and (listp name) (eq (car name) 'setf)))
117 (values (type-specifier (info :function :type name))
118 (info :function :where-from name))
119 (values type-spec :defined))
121 (format s "~@:_Its ~(~A~) argument types are:~@:_ ~S"
123 (format s "~@:_Its result type is:~@:_ ~S" (third type))))
124 (let ((inlinep (info :function :inlinep name)))
127 "~@:_It is currently declared ~(~A~);~
128 ~:[no~;~] expansion is available."
129 inlinep (info :function :inline-expansion name))))))
131 ;;; Interpreted function describing; handles both closure and
132 ;;; non-closure functions. Instead of printing the compiled-from info,
133 ;;; we print the definition.
134 (defun %describe-function-interpreted (x s kind name)
135 (declare (type stream s))
136 (multiple-value-bind (exp closure-p dname)
137 (sb-eval:interpreted-function-lambda-expression x)
138 (let ((args (sb-eval:interpreted-function-arglist x)))
139 (format s "~@:_~@(~@[~A ~]arguments:~@:_~)" kind)
141 (format s " ~<~S~:>" args)
142 (write-string " There are no arguments." s)))
143 (let ((name (or name dname)))
144 (%describe-doc name s 'function kind)
145 (unless (eq kind :macro)
146 (%describe-function-name
149 (type-specifier (sb-eval:interpreted-function-type x)))))
151 (format s "~@:_Its closure environment is:~%")
152 (pprint-logical-block (s nil)
153 (pprint-indent :current 2)
154 (let ((closure (sb-eval:interpreted-function-closure x)))
155 (dotimes (i (length closure))
156 (format s "~@:_~S: ~S" i (svref closure i))))))
157 (format s "~@:_Its definition is:~@:_ ~S" exp)))
159 ;;; Print information from the debug-info about where CODE-OBJ was
161 (defun %describe-compiled-from (code-obj s)
162 (declare (type stream s))
163 (let ((info (sb-kernel:%code-debug-info code-obj)))
165 (let ((sources (sb-c::debug-info-source info)))
167 (format s "~@:_On ~A it was compiled from:"
168 ;; FIXME: The FORMAT-UNIVERSAL-TIME calls in the system
169 ;; should become more consistent, probably not using
170 ;; any nondefault options.
171 (format-universal-time nil
172 (sb-c::debug-source-compiled
174 :style :abbreviated))
175 (dolist (source sources)
176 (let ((name (sb-c::debug-source-name source)))
177 (ecase (sb-c::debug-source-from source)
179 (format s "~@:_~A~@:_ Created: " (namestring name))
180 (sb-int:format-universal-time s (sb-c::debug-source-created
182 (:lisp (format s "~@:_~S" name))))))))))
184 ;;; Describe a compiled function. The closure case calls us to print
186 (defun %describe-function-compiled (x s kind name)
187 (declare (type stream s))
188 ;; FIXME: The lowercaseness of %FUNCTION-ARGLIST results, and the
189 ;; non-sentenceness of the "Arguments" label, makes awkward output.
190 ;; Better would be "Its arguments are: ~S" (with uppercase argument
191 ;; names) when arguments are known, and otherwise "There is no
192 ;; information available about its arguments." or "It has no
193 ;; arguments." (And why is %FUNCTION-ARGLIST a string instead of a
194 ;; list of symbols anyway?)
195 (let ((args (%function-arglist x)))
196 (format s "~@:_~@(~@[~A ~]arguments:~@:_~)" kind)
198 (format s " There is no argument information available."))
200 (write-string " There are no arguments." s))
203 (pprint-logical-block (s nil)
204 (pprint-indent :current 2)
205 (write-string args s)))))
206 (let ((name (or name (%function-name x))))
207 (%describe-doc name s 'function kind)
208 (unless (eq kind :macro)
209 (%describe-function-name name s (%function-type x))))
210 (%describe-compiled-from (sb-kernel:function-code-header x) s))
212 (defun %describe-function-byte-compiled (x s kind name)
213 (declare (type stream s))
214 (let ((name (or name (sb-c::byte-function-name x))))
215 (%describe-doc name s 'function kind)
216 (unless (eq kind :macro)
217 (%describe-function-name name s 'function)))
218 (%describe-compiled-from (sb-c::byte-function-component x) s))
220 ;;; Describe a function with the specified kind and name. The latter
221 ;;; arguments provide some information about where the function came
222 ;;; from. Kind NIL means not from a name.
223 (defun %describe-function (x s &optional (kind nil) name)
224 (declare (type function x))
225 (declare (type stream s))
226 (declare (type (member :macro :function nil) kind))
229 (:macro (format s "Macro-function: ~S" x))
230 (:function (format s "Function: ~S" x))
231 ((nil) (format s "~S is a function." x)))
233 (#.sb-vm:closure-header-type
234 (%describe-function-compiled (%closure-function x) s kind name)
235 (format s "~@:_Its closure environment is:")
236 (pprint-logical-block (s nil)
237 (pprint-indent :current 8)
238 (dotimes (i (- (get-closure-length x) (1- sb-vm:closure-info-offset)))
239 (format s "~@:_~S: ~S" i (%closure-index-ref x i)))))
240 ((#.sb-vm:function-header-type #.sb-vm:closure-function-header-type)
241 (%describe-function-compiled x s kind name))
242 (#.sb-vm:funcallable-instance-header-type
244 (sb-kernel:byte-function
245 (%describe-function-byte-compiled x s kind name))
246 (sb-kernel:byte-closure
247 (%describe-function-byte-compiled (byte-closure-function x)
249 (format s "~@:_Its closure environment is:")
250 (pprint-logical-block (s nil)
251 (pprint-indent :current 8)
252 (let ((data (byte-closure-data x)))
253 (dotimes (i (length data))
254 (format s "~@:_~S: ~S" i (svref data i))))))
255 (sb-eval:interpreted-function
256 (%describe-function-interpreted x s kind name))
257 (standard-generic-function
258 ;; There should be a special method for this case; we'll
260 (describe-object x s))
262 (format s "~@:_It is an unknown type of funcallable instance."))))
264 (format s "~@:_It is an unknown type of function."))))
266 (defmethod describe-object ((x function) s)
267 (%describe-function x s))
269 (defmethod describe-object ((x symbol) s)
270 (declare (type stream s))
272 ;; Describe the packaging.
273 (let ((package (symbol-package x)))
275 (multiple-value-bind (symbol status)
276 (find-symbol (symbol-name x) package)
277 (declare (ignore symbol))
278 (format s "~S is ~_an ~(~A~) symbol ~_in ~S."
279 x status (symbol-package x)))
280 (format s "~S is ~_an uninterned symbol." x)))
281 ;; TO DO: We could grovel over all packages looking for and
282 ;; reporting other phenomena, e.g. IMPORT and SHADOW, or
283 ;; availability in some package even after (SYMBOL-PACKAGE X) has
286 ;; Describe the value cell.
287 (let* ((kind (info :variable :kind x))
289 (:special "special variable")
290 (:constant "constant")
291 (:global "undefined variable")
295 (let ((info (info :variable :alien-info x)))
296 (format s "~@:_~@<It is an alien at #X~8,'0X of type ~3I~:_~S.~:>~@:_"
297 (sap-int (eval (sb-alien::heap-alien-info-sap-form info)))
298 (sb-alien-internals:unparse-alien-type
299 (sb-alien::heap-alien-info-type info)))
300 (format s "~@<Its current value is ~3I~:_~S.~:>"
303 (format s "~@:_It is a ~A; its ~_value is ~S." wot (symbol-value x)))
304 ((not (eq kind :global))
305 (format s "~@:_It is a ~A; no current value." wot)))
307 (when (eq (info :variable :where-from x) :declared)
308 (format s "~@:_Its declared type ~_is ~S."
309 (type-specifier (info :variable :type x))))
311 (%describe-doc x s 'variable kind))
313 ;; Print out properties.
314 (format s "~@[~@:_Its SYMBOL-PLIST is ~@<~2I~_~S~:>.~]" (symbol-plist x))
316 ;; Describe the function cell.
317 (cond ((macro-function x)
318 (%describe-function (macro-function x) s :macro x))
319 ((special-operator-p x)
320 (%describe-doc x s 'function "Special form"))
322 (%describe-function (fdefinition x) s :function x)))
324 ;; TO DO: Print out other stuff from the INFO database:
325 ;; * Does it name a type or class?
326 ;; * Is it a structure accessor? (This is important since those are
327 ;; magical in some ways, e.g. blasting the structure if you
330 ;; Print other documentation.
331 (%describe-doc x s 'structure "Structure")
332 (%describe-doc x s 'type "Type")
333 (%describe-doc x s 'setf "Setf macro")
334 (dolist (assoc (info :random-documentation :stuff x))
336 "~@:_Documentation on the ~(~A~):~@:_~A"