0.6.11.45:
[sbcl.git] / src / code / describe.lisp
1 ;;;; most of the DESCRIBE mechanism -- 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")
14
15 (declaim #.*optimize-byte-compilation*)
16
17 \f
18 (defvar *describe-indentation-step* 3
19   #+sb-doc
20   "the number of spaces that sets off each line of a recursive description")
21
22 (declaim (ftype (function (t stream)) describe-object))
23 (defgeneric describe-object ((x t) stream))
24
25 (defun describe (x &optional (stream-designator *standard-output*))
26   #+sb-doc
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)
31       (fresh-line stream)
32       (describe-object x stream)
33       (fresh-line stream)))
34   (values))
35 \f
36 ;;;; miscellaneous DESCRIBE-OBJECT methods
37
38 (defmethod describe-object ((x t) s)
39   (format s "~@<~S ~_is a ~S.~:>" x (type-of x)))
40
41 (defmethod describe-object ((x cons) s)
42   (call-next-method)
43   (when (and (legal-function-name-p x)
44              (fboundp 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.
49     ))
50
51 (defmethod describe-object ((x array) s)
52   (let ((rank (array-rank x)))
53     (cond ((> rank 1)
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)))
58           (t
59            (format s
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."
64                      (fill-pointer x))))))
65   (let ((array-element-type (array-element-type x)))
66     (unless (eq array-element-type t)
67       (format s
68               "~@:_Its element type is specialized to ~S."
69               array-element-type))))
70
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))
75   (format s
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~_~;.~]"
81             count (zerop count))
82     (let ((n 0))
83       (declare (type index n))
84       (dohash (k v x)
85         (unless (zerop n)
86           (write-char #\space s))
87         (incf n)
88         (when (and *print-length* (> n *print-length*))
89           (format s "~:_...")
90           (return))
91         (format s "~:_(~@<~S ~:_~S~:>)" k v)))))
92
93 (defmethod describe-object ((condition condition) s)
94   (sb-kernel:describe-condition condition s))
95 \f
96 ;;;; DESCRIBE-OBJECT methods for symbols and functions, including all
97 ;;;; sorts of messy stuff about documentation, type information,
98 ;;;; packaging, function implementation, etc..
99
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)))
106       (when doc
107         (format s "~_~@(~A documentation:~)~@:_  ~A"
108                 (or kind-doc kind) doc))))
109   (values))
110
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))
124       (when (consp type)
125         (format s "~@:_Its ~(~A~) argument types are:~@:_  ~S"
126                 where (second type))
127         (format s "~@:_Its result type is:~@:_  ~S" (third type))))
128     (let ((inlinep (info :function :inlinep name)))
129       (when inlinep
130         (format s
131                 "~@:_It is currently declared ~(~A~);~
132                  ~:[no~;~] expansion is available."
133                 inlinep (info :function :inline-expansion name))))))
134
135 ;;; Interpreted function describing; handles both closure and
136 ;;; non-closure functions. Instead of printing the compiled-from info,
137 ;;; we print the definition.
138 (defun %describe-function-interpreted (x s kind name)
139   (declare (type stream s))
140   (multiple-value-bind (exp closure-p dname)
141       (sb-eval:interpreted-function-lambda-expression x)
142     (let ((args (sb-eval:interpreted-function-arglist x)))
143       (format s "~@:_~@(~@[~A ~]arguments:~@:_~)" kind)
144       (if args
145           (format s "  ~<~S~:>" args)
146           (write-string "  There are no arguments." s)))
147     (let ((name (or name dname)))
148       (%describe-doc name s 'function kind)
149       (unless (eq kind :macro)
150         (%describe-function-name
151          name
152          s
153          (type-specifier (sb-eval:interpreted-function-type x)))))
154     (when closure-p
155       (format s "~@:_Its closure environment is:~%")
156       (pprint-logical-block (s nil)
157         (pprint-indent :current 2)
158         (let ((closure (sb-eval:interpreted-function-closure x)))
159           (dotimes (i (length closure))
160             (format s "~@:_~S: ~S" i (svref closure i))))))
161     (format s "~@:_Its definition is:~@:_  ~S" exp)))
162
163 ;;; Print information from the debug-info about where CODE-OBJ was
164 ;;; compiled from.
165 (defun %describe-compiled-from (code-obj s)
166   (declare (type stream s))
167   (let ((info (sb-kernel:%code-debug-info code-obj)))
168     (when info
169       (let ((sources (sb-c::debug-info-source info)))
170         (when sources
171           (format s "~@:_On ~A it was compiled from:"
172                   ;; FIXME: The FORMAT-UNIVERSAL-TIME calls in the system
173                   ;; should become more consistent, probably not using
174                   ;; any nondefault options.
175                   (format-universal-time nil
176                                          (sb-c::debug-source-compiled
177                                           (first sources))
178                                          :style :abbreviated))
179           (dolist (source sources)
180             (let ((name (sb-c::debug-source-name source)))
181               (ecase (sb-c::debug-source-from source)
182                 (:file
183                  (format s "~@:_~A~@:_  Created: " (namestring name))
184                  (format-universal-time s (sb-c::debug-source-created
185                                            source)))
186                 (:lisp (format s "~@:_~S" name))))))))))
187
188 ;;; Describe a compiled function. The closure case calls us to print
189 ;;; the guts.
190 (defun %describe-function-compiled (x s kind name)
191   (declare (type stream s))
192   ;; FIXME: The lowercaseness of %FUNCTION-ARGLIST results, and the
193   ;; non-sentenceness of the "Arguments" label, makes awkward output.
194   ;; Better would be "Its arguments are: ~S" (with uppercase argument
195   ;; names) when arguments are known, and otherwise "There is no
196   ;; information available about its arguments." or "It has no
197   ;; arguments." (And why is %FUNCTION-ARGLIST a string instead of a
198   ;; list of symbols anyway?)
199   (let ((args (%function-arglist x)))
200     (format s "~@:_~@(~@[~A ~]arguments:~@:_~)" kind)
201     (cond ((not args)
202            (format s "  There is no argument information available."))
203           ((string= args "()")
204            (write-string "  There are no arguments." s))
205           (t
206            (write-string "  " s)
207            (pprint-logical-block (s nil)
208              (pprint-indent :current 2)
209              (write-string args s)))))
210   (let ((name (or name (%function-name x))))
211     (%describe-doc name s 'function kind)
212     (unless (eq kind :macro)
213       (%describe-function-name name s (%function-type x))))
214   (%describe-compiled-from (sb-kernel:function-code-header x) s))
215
216 (defun %describe-function-byte-compiled (x s kind name)
217   (declare (type stream s))
218   (let ((name (or name (sb-c::byte-function-name x))))
219     (%describe-doc name s 'function kind)
220     (unless (eq kind :macro)
221       (%describe-function-name name s 'function)))
222   (%describe-compiled-from (sb-c::byte-function-component x) s))
223
224 ;;; Describe a function with the specified kind and name. The latter
225 ;;; arguments provide some information about where the function came
226 ;;; from. Kind NIL means not from a name.
227 (defun %describe-function (x s &optional (kind nil) name)
228   (declare (type function x))
229   (declare (type stream s))
230   (declare (type (member :macro :function nil) kind))
231   (fresh-line s)
232   (ecase kind
233     (:macro (format s "Macro-function: ~S" x))
234     (:function (format s "Function: ~S" x))
235     ((nil) (format s "~S is a function." x)))
236   (case (get-type x)
237     (#.sb-vm:closure-header-type
238      (%describe-function-compiled (%closure-function x) s kind name)
239      (format s "~@:_Its closure environment is:")
240      (pprint-logical-block (s nil)
241        (pprint-indent :current 8)
242        (dotimes (i (- (get-closure-length x) (1- sb-vm:closure-info-offset)))
243          (format s "~@:_~S: ~S" i (%closure-index-ref x i)))))
244     ((#.sb-vm:function-header-type #.sb-vm:closure-function-header-type)
245      (%describe-function-compiled x s kind name))
246     (#.sb-vm:funcallable-instance-header-type
247      (typecase x
248        (sb-kernel:byte-function
249         (%describe-function-byte-compiled x s kind name))
250        (sb-kernel:byte-closure
251         (%describe-function-byte-compiled (byte-closure-function x)
252                                           s kind name)
253         (format s "~@:_Its closure environment is:")
254         (pprint-logical-block (s nil)
255           (pprint-indent :current 8)
256           (let ((data (byte-closure-data x)))
257             (dotimes (i (length data))
258               (format s "~@:_~S: ~S" i (svref data i))))))
259        (sb-eval:interpreted-function
260         (%describe-function-interpreted x s kind name))
261        (standard-generic-function
262         ;; There should be a special method for this case; we'll
263         ;; delegate to that.
264         (describe-object x s))
265        (t
266         (format s "~@:_It is an unknown type of funcallable instance."))))
267     (t
268      (format s "~@:_It is an unknown type of function."))))
269
270 (defmethod describe-object ((x function) s)
271   (%describe-function x s))
272   
273 (defmethod describe-object ((x symbol) s)
274   (declare (type stream s))
275
276   ;; Describe the packaging.
277   (let ((package (symbol-package x)))
278     (if package
279         (multiple-value-bind (symbol status)
280             (find-symbol (symbol-name x) package)
281           (declare (ignore symbol))
282           (format s "~S is ~_an ~(~A~) symbol ~_in ~S."
283                   x status (symbol-package x)))
284         (format s "~S is ~_an uninterned symbol." x)))
285   ;; TO DO: We could grovel over all packages looking for and
286   ;; reporting other phenomena, e.g. IMPORT and SHADOW, or
287   ;; availability in some package even after (SYMBOL-PACKAGE X) has
288   ;; been set to NIL.
289
290   ;; Describe the value cell.
291   (let* ((kind (info :variable :kind x))
292          (wot (ecase kind
293                 (:special "special variable")
294                 (:constant "constant")
295                 (:global "undefined variable")
296                 (:alien nil))))
297     (cond
298      ((eq kind :alien)
299       (let ((info (info :variable :alien-info x)))
300         (format s "~@:_~@<It is an alien at #X~8,'0X of type ~3I~:_~S.~:>~@:_"
301                 (sap-int (eval (sb-alien::heap-alien-info-sap-form info)))
302                 (sb-alien-internals:unparse-alien-type
303                  (sb-alien::heap-alien-info-type info)))
304         (format s "~@<Its current value is ~3I~:_~S.~:>"
305                 (eval x))))
306      ((boundp x)
307       (format s "~@:_It is a ~A; its ~_value is ~S." wot (symbol-value x)))
308      ((not (eq kind :global))
309       (format s "~@:_It is a ~A; no current value." wot)))
310
311     (when (eq (info :variable :where-from x) :declared)
312       (format s "~@:_Its declared type ~_is ~S."
313               (type-specifier (info :variable :type x))))
314
315     (%describe-doc x s 'variable kind))
316
317   ;; Print out properties.
318   (format s "~@[~@:_Its SYMBOL-PLIST is ~@<~2I~_~S~:>.~]" (symbol-plist x))
319
320   ;; Describe the function cell.
321   (cond ((macro-function x)
322          (%describe-function (macro-function x) s :macro x))
323         ((special-operator-p x)
324          (%describe-doc x s 'function "Special form"))
325         ((fboundp x)
326          (%describe-function (fdefinition x) s :function x)))
327
328   ;; TO DO: Print out other stuff from the INFO database:
329   ;;   * Does it name a type or class?
330   ;;   * Is it a structure accessor? (This is important since those are 
331   ;;     magical in some ways, e.g. blasting the structure if you 
332   ;;     redefine them.)
333
334   ;; Print other documentation.
335   (%describe-doc x s 'structure "Structure")
336   (%describe-doc x s 'type "Type")
337   (%describe-doc x s 'setf "Setf macro")
338   (dolist (assoc (info :random-documentation :stuff x))
339     (format s
340             "~@:_Documentation on the ~(~A~):~@:_~A"
341             (car assoc)
342             (cdr assoc))))