0.8.4.17:
[sbcl.git] / src / code / describe.lisp
1 ;;;; most of the DESCRIBE system -- that part which isn't derived
2 ;;;; from PCL code
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
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.
12
13 (in-package "SB-IMPL") ;(SB-IMPL, not SB!IMPL, since we're built in warm load.)
14 \f
15 (declaim (ftype (function (t stream)) describe-object))
16 (defgeneric describe-object (x stream))
17
18 (defun describe (x &optional (stream-designator *standard-output*))
19   #+sb-doc
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)
25     ;;     ...
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
30     ;;   DESCRIBE-OBJECT. 
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.
41     )
42   (values))
43 \f
44 ;;;; miscellaneous DESCRIBE-OBJECT methods
45
46 (defmethod describe-object ((x t) s)
47   (format s "~&~@<~S ~_is a ~S.~:>~%" x (type-of x)))
48
49 (defmethod describe-object ((x cons) s)
50   (call-next-method)
51   (when (and (legal-fun-name-p x)
52              (fboundp 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.
58     ))
59
60 (defmethod describe-object ((x array) s)
61   (fresh-line s)
62   (pprint-logical-block (s nil)
63     (cond
64      ((= 1 (array-rank x))
65       (format s "~S is a vector with ~D elements."
66               x (car (array-dimensions x)))
67       (when (array-has-fill-pointer-p x)
68         (format s "~@:_It has a fill pointer value of ~S."
69                 (fill-pointer x))))
70      (t
71       (format s "~S is an array of dimension ~:S."
72               x (array-dimensions x))))
73     (let ((array-element-type (array-element-type x)))
74       (unless (eq array-element-type t)
75         (format s
76                 "~@:_Its element type is specialized to ~S."
77                 array-element-type)))
78     (if (and (array-header-p x) (%array-displaced-p x))
79         (format s "~@:_The array is displaced with offset ~S."
80                 (%array-displacement x))))
81   (terpri s))
82
83 (defmethod describe-object ((x hash-table) s)
84   (declare (type stream s))
85   (format s "~&~@<~S ~_is an ~S hash table.~:>" x (hash-table-test x))
86   (format s "~&Its SIZE is ~S." (hash-table-size x))
87   (format s
88           "~&~@<Its REHASH-SIZE is ~S. ~_Its REHASH-THRESHOLD is ~S.~:>"
89           (hash-table-rehash-size x)
90           (hash-table-rehash-threshold x))
91   (fresh-line s)
92   (pprint-logical-block (s nil)
93     (let ((count (hash-table-count x)))
94       (format s "It holds ~S key/value pair~:P~:[: ~2I~_~;.~]"
95               count (zerop count))
96       (let ((n 0))
97         (declare (type index n))
98         (dohash (k v x)
99           (unless (zerop n)
100             (write-char #\space s))
101           (incf n)
102           (when (and *print-length* (> n *print-length*))
103             (format s "~:_...")
104             (return))
105           (format s "~:_(~@<~S ~:_~S~:>)" k v)))))
106   (terpri s))
107
108 (defmethod describe-object ((condition condition) s)
109   (sb-kernel:describe-condition condition s))
110 \f
111 ;;;; DESCRIBE-OBJECT methods for symbols and functions, including all
112 ;;;; sorts of messy stuff about documentation, type information,
113 ;;;; packaging, function implementation, etc...
114
115 ;;; Print the specified kind of documentation about the given NAME. If
116 ;;; NAME is null, or not a valid name, then don't print anything.
117 (declaim (ftype (function (t stream t t) (values)) %describe-doc))
118 (defun %describe-doc (name s kind kind-doc)
119   (when (and name (typep name '(or symbol cons)))
120     (let ((doc (fdocumentation name kind)))
121       (when doc
122         (format s "~&~@(~A documentation:~)~%  ~A"
123                 (or kind-doc kind) doc))))
124   (values))
125
126 ;;; Describe various stuff about the functional semantics attached to
127 ;;; the specified NAME, if NAME is the kind of thing you can look
128 ;;; up as a name. (In the case of anonymous closures and other
129 ;;; things, it might not be.) TYPE-SPEC is the function type specifier
130 ;;; extracted from the definition, or NIL if none.
131 (declaim (ftype (function (t stream t)) %describe-fun-name))
132 (defun %describe-fun-name (name s type-spec) 
133   (when (and name (typep name '(or symbol cons)))
134     (multiple-value-bind (type where)
135         (if (legal-fun-name-p name)
136             (values (type-specifier (info :function :type name))
137                     (info :function :where-from name))
138             (values type-spec :defined))
139       (when (consp type)
140         (format s "~&Its ~(~A~) argument types are:~%  ~S"
141                 where (second type))
142         (format s "~&Its result type is:~%  ~S" (third type))))
143     (let ((inlinep (info :function :inlinep name)))
144       (when inlinep
145         (format s
146                 "~&It is currently declared ~(~A~);~
147                  ~:[no~;~] expansion is available."
148                 inlinep (info :function :inline-expansion-designator name))))))
149
150 ;;; Print information from the debug-info about where CODE-OBJ was
151 ;;; compiled from.
152 (defun %describe-compiled-from (code-obj s)
153   (declare (type stream s))
154   (let ((info (sb-kernel:%code-debug-info code-obj)))
155     (when info
156       (let ((sources (sb-c::debug-info-source info)))
157         (when sources
158           (format s "~&On ~A it was compiled from:"
159                   ;; FIXME: The FORMAT-UNIVERSAL-TIME calls in the system
160                   ;; should become more consistent, probably not using
161                   ;; any nondefault options.
162                   (format-universal-time nil
163                                          (sb-c::debug-source-compiled
164                                           (first sources))
165                                          :style :abbreviated))
166           (dolist (source sources)
167             (let ((name (sb-c::debug-source-name source)))
168               (ecase (sb-c::debug-source-from source)
169                 (:file
170                  (format s "~&~A~@:_  Created: " (namestring name))
171                  (format-universal-time s (sb-c::debug-source-created
172                                            source)))
173                 (:lisp (format s "~&~S" name))))))))))
174
175 ;;; Describe a compiled function. The closure case calls us to print
176 ;;; the guts.
177 (defun %describe-fun-compiled (x s kind name)
178   (declare (type stream s))
179   (let ((args (%simple-fun-arglist x)))
180     (cond ((not args)
181            (write-string "  There are no arguments." s))
182           (t
183            (format s "~&~@(The ~@[~A's ~]arguments are:~@:_~)" kind)
184            (write-string "  " s)
185             (let ((*print-pretty* t)
186                   (*print-escape* t)
187                   (*print-base* 10)
188                   (*print-radix* nil))
189               (pprint-logical-block (s nil)
190                  (pprint-indent :current 2)
191                  (format s "~A" args))))))
192   (let ((name (or name (%simple-fun-name x))))
193     (%describe-doc name s 'function kind)
194     (unless (eq kind :macro)
195       (%describe-fun-name name s (%simple-fun-type x))))
196   (%describe-compiled-from (sb-kernel:fun-code-header x) s))
197
198 ;;; Describe a function object. KIND and NAME provide some information
199 ;;; about where the function came from.
200 (defun %describe-fun (x s &optional (kind :function) (name nil))
201   (declare (type function x))
202   (declare (type stream s))
203   (declare (type (member :macro :function) kind))
204   (fresh-line s)
205   (pprint-logical-block (s nil)
206     (ecase kind
207       (:macro (format s "Macro-function: ~S" x))
208       (:function (if name
209                      (format s "Function: ~S" x)
210                      (format s "~S is a function." x))))
211     (format s "~@:_~@<Its associated name (as in ~S) is ~2I~_~S.~:>"
212             'function-lambda-expression
213             (%fun-name x))
214     (case (widetag-of x)
215       (#.sb-vm:closure-header-widetag
216        (%describe-fun-compiled (%closure-fun x) s kind name)
217        (format s "~@:_Its closure environment is:")
218        (pprint-logical-block (s nil)
219          (pprint-indent :current 8)
220          (dotimes (i (- (get-closure-length x) (1- sb-vm:closure-info-offset)))
221            (format s "~@:_~S: ~S" i (%closure-index-ref x i)))))
222       (#.sb-vm:simple-fun-header-widetag
223        (%describe-fun-compiled x s kind name))
224       (#.sb-vm:funcallable-instance-header-widetag
225        ;; Only STANDARD-GENERIC-FUNCTION would be handled here, but
226        ;; since it has its own DESCRIBE-OBJECT method, it should've been
227        ;; picked off before getting here. So hopefully we never get here.
228        (format s "~@:_It is an unknown type of funcallable instance."))
229       (t
230        (format s "~@:_It is an unknown type of function."))))
231   (terpri s))
232
233 (defmethod describe-object ((x function) s)
234   (%describe-fun x s :function))
235
236 (defgeneric describe-symbol-fdefinition (function stream &key name))
237
238 (defmethod describe-symbol-fdefinition ((fun function) stream &key name)
239   (%describe-fun fun stream :function name))
240
241 (defmethod describe-symbol-fdefinition ((fun standard-generic-function) stream
242                                         &key name)
243   (declare (ignore name))
244   ;; Just delegate.
245   (describe-object fun stream))
246
247 (defmethod describe-object ((x symbol) s)
248   (declare (type stream s))
249
250   ;; Describe the packaging.
251   (let ((package (symbol-package x)))
252     (if package
253         (multiple-value-bind (symbol status)
254             (find-symbol (symbol-name x) package)
255           (declare (ignore symbol))
256           (format s "~&~@<~S is ~_an ~(~A~) symbol ~_in ~S.~:>"
257                   x status (symbol-package x)))
258         (format s "~&~@<~S is ~_an uninterned symbol.~:>" x)))
259   ;; TO DO: We could grovel over all packages looking for and
260   ;; reporting other phenomena, e.g. IMPORT and SHADOW, or
261   ;; availability in some package even after (SYMBOL-PACKAGE X) has
262   ;; been set to NIL.
263
264   ;; Describe the value cell.
265   (let* ((kind (info :variable :kind x))
266          (wot (ecase kind
267                 (:special "special variable")
268                 (:macro "symbol macro")
269                 (:constant "constant")
270                 (:global "undefined variable")
271                 (:alien nil))))
272     (pprint-logical-block (s nil)
273       (cond
274        ((eq kind :alien)
275         (let ((info (info :variable :alien-info x)))
276           (format s "~&~@<It is an alien at #X~8,'0X of type ~3I~:_~S.~:>"
277                   (sap-int (eval (sb-alien::heap-alien-info-sap-form info)))
278                   (sb-alien-internals:unparse-alien-type
279                    (sb-alien::heap-alien-info-type info)))
280           (format s "~&~@<Its current value is ~3I~:_~S.~:>"
281                   (eval x))))
282        ((eq kind :macro)
283         (let ((expansion (info :variable :macro-expansion x)))
284           (format s "~&It is a ~A with expansion ~S." wot expansion)))
285        ((boundp x)
286         (format s "~&~@<It is a ~A; its ~_value is ~S.~:>"
287                 wot (symbol-value x)))
288        ((not (eq kind :global))
289         (format s "~&~@<It is a ~A; no current value.~:>" wot)))
290
291       (when (eq (info :variable :where-from x) :declared)
292         (format s "~&~@<Its declared type ~_is ~S.~:>"
293                 (type-specifier (info :variable :type x)))))
294
295     (%describe-doc x s 'variable kind))
296
297   ;; Print out properties.
298   (format s "~@[~&Its SYMBOL-PLIST is ~@<~2I~_~S~:>.~]" (symbol-plist x))
299
300   ;; Describe the function cell.
301   (cond ((macro-function x)
302          (%describe-fun (macro-function x) s :macro x))
303         ((special-operator-p x)
304          (%describe-doc x s :function "Special form"))
305         ((fboundp x)
306          (describe-symbol-fdefinition (fdefinition x) s :name x)))
307
308   ;; Print other documentation.
309   (%describe-doc x s 'structure "Structure")
310   (%describe-doc x s 'type "Type")
311   (%describe-doc x s 'setf "Setf macro")
312   (dolist (assoc (info :random-documentation :stuff x))
313     (format s
314             "~&~@<Documentation on the ~(~A~):~@:_~A~:>"
315             (car assoc)
316             (cdr assoc)))
317   
318   ;; Mention the associated type information, if any.
319   ;;
320   ;; As of sbcl-0.7.2, (INFO :TYPE :KIND X) might be
321   ;;   * :PRIMITIVE, which is handled by the FIND-CLASS case.
322   ;;   * :DEFINED, which is handled specially.
323   ;;   * :INSTANCE, which is handled by the FIND-CLASS case.
324   ;;   * :FORTHCOMING-DEFCLASS-TYPE, which is an internal-to-the-compiler
325   ;;     note that we don't try to report.
326   ;;   * NIL, in which case there's nothing to see here, move along.
327   (when (eq (info :type :kind x) :defined)
328     (format s "~&It names a type specifier."))
329   (let ((symbol-named-class (find-class x nil)))
330     (when symbol-named-class
331       (format s "~&It names a class ~A." symbol-named-class)
332       (describe symbol-named-class s)))
333
334   (terpri s))