0.8.0.27:
[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     (let ((rank (array-rank x)))
64       (cond ((= rank 1)
65              (format s
66                      "~S is a ~:[~;displaced ~]vector of length ~S." x
67                      (and (array-header-p x)
68                           (%array-displaced-p x)
69                           ) (length x))
70              (when (array-has-fill-pointer-p x)
71                (format s "~@:_It has a fill pointer, currently ~S."
72                        (fill-pointer x))))
73             (t
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)
80         (format s
81                 "~@:_Its element type is specialized to ~S."
82                 array-element-type))))
83   (terpri s))
84
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))
89   (format s
90           "~&~@<Its REHASH-SIZE is ~S. ~_Its REHASH-THRESHOLD is ~S.~:>"
91           (hash-table-rehash-size x)
92           (hash-table-rehash-threshold x))
93   (fresh-line)
94   (pprint-logical-block (s nil)
95     (let ((count (hash-table-count x)))
96       (format s "It holds ~S key/value pair~:P~:[: ~2I~_~;.~]"
97               count (zerop count))
98       (let ((n 0))
99         (declare (type index n))
100         (dohash (k v x)
101           (unless (zerop n)
102             (write-char #\space s))
103           (incf n)
104           (when (and *print-length* (> n *print-length*))
105             (format s "~:_...")
106             (return))
107           (format s "~:_(~@<~S ~:_~S~:>)" k v)))))
108   (terpri s))
109
110 (defmethod describe-object ((condition condition) s)
111   (sb-kernel:describe-condition condition s))
112 \f
113 ;;;; DESCRIBE-OBJECT methods for symbols and functions, including all
114 ;;;; sorts of messy stuff about documentation, type information,
115 ;;;; packaging, function implementation, etc...
116
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)))
123       (when doc
124         (format s "~&~@(~A documentation:~)~%  ~A"
125                 (or kind-doc kind) doc))))
126   (values))
127
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))
141       (when (consp type)
142         (format s "~&Its ~(~A~) argument types are:~%  ~S"
143                 where (second type))
144         (format s "~&Its result type is:~%  ~S" (third type))))
145     (let ((inlinep (info :function :inlinep name)))
146       (when inlinep
147         (format s
148                 "~&It is currently declared ~(~A~);~
149                  ~:[no~;~] expansion is available."
150                 inlinep (info :function :inline-expansion-designator name))))))
151
152 ;;; Print information from the debug-info about where CODE-OBJ was
153 ;;; compiled from.
154 (defun %describe-compiled-from (code-obj s)
155   (declare (type stream s))
156   (let ((info (sb-kernel:%code-debug-info code-obj)))
157     (when info
158       (let ((sources (sb-c::debug-info-source info)))
159         (when sources
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
166                                           (first sources))
167                                          :style :abbreviated))
168           (dolist (source sources)
169             (let ((name (sb-c::debug-source-name source)))
170               (ecase (sb-c::debug-source-from source)
171                 (:file
172                  (format s "~&~A~@:_  Created: " (namestring name))
173                  (format-universal-time s (sb-c::debug-source-created
174                                            source)))
175                 (:lisp (format s "~&~S" name))))))))))
176
177 ;;; Describe a compiled function. The closure case calls us to print
178 ;;; the guts.
179 (defun %describe-fun-compiled (x s kind name)
180   (declare (type stream s))
181   (let ((args (%simple-fun-arglist x)))
182     (cond ((not args)
183            (write-string "  There are no arguments." s))
184           (t
185            (format s "~&~@(The ~@[~A's ~]arguments are:~@:_~)" kind)
186            (write-string "  " s)
187             (let ((*print-pretty* t)
188                   (*print-escape* t)
189                   (*print-base* 10)
190                   (*print-radix* nil))
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))
199
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))
206   (fresh-line s)
207   (pprint-logical-block (s nil)
208     (ecase kind
209       (:macro (format s "Macro-function: ~S" x))
210       (:function (if name
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
215             (%fun-name x))
216     (case (widetag-of x)
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."))
231       (t
232        (format s "~@:_It is an unknown type of function."))))
233   (terpri s))
234
235 (defmethod describe-object ((x function) s)
236   (%describe-fun x s :function))
237
238 (defgeneric describe-symbol-fdefinition (function stream &key name))
239
240 (defmethod describe-symbol-fdefinition ((fun function) stream &key name)
241   (%describe-fun fun stream :function name))
242
243 (defmethod describe-symbol-fdefinition ((fun standard-generic-function) stream
244                                         &key name)
245   (declare (ignore name))
246   ;; Just delegate.
247   (describe-object fun stream))
248
249 (defmethod describe-object ((x symbol) s)
250   (declare (type stream s))
251
252   ;; Describe the packaging.
253   (let ((package (symbol-package x)))
254     (if package
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
264   ;; been set to NIL.
265
266   ;; Describe the value cell.
267   (let* ((kind (info :variable :kind x))
268          (wot (ecase kind
269                 (:special "special variable")
270                 (:macro "symbol macro")
271                 (:constant "constant")
272                 (:global "undefined variable")
273                 (:alien nil))))
274     (pprint-logical-block (s nil)
275       (cond
276        ((eq kind :alien)
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.~:>"
283                   (eval x))))
284        ((eq kind :macro)
285         (let ((expansion (info :variable :macro-expansion x)))
286           (format s "~&It is a ~A with expansion ~S." wot expansion)))
287        ((boundp x)
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)))
292
293       (when (eq (info :variable :where-from x) :declared)
294         (format s "~&~@<Its declared type ~_is ~S.~:>"
295                 (type-specifier (info :variable :type x)))))
296
297     (%describe-doc x s 'variable kind))
298
299   ;; Print out properties.
300   (format s "~@[~&Its SYMBOL-PLIST is ~@<~2I~_~S~:>.~]" (symbol-plist x))
301
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"))
307         ((fboundp x)
308          (describe-symbol-fdefinition (fdefinition x) s :name x)))
309
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))
315     (format s
316             "~&~@<Documentation on the ~(~A~):~@:_~A~:>"
317             (car assoc)
318             (cdr assoc)))
319   
320   ;; Mention the associated type information, if any.
321   ;;
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-classoid x nil)))
332     (when symbol-named-class
333       (format s "~&It names a class ~A." symbol-named-class)
334       (describe symbol-named-class s)))
335
336   (terpri s))