0.6.11.35:
[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 \f
15 (defvar *describe-indentation-step* 3
16   #+sb-doc
17   "the number of spaces that sets off each line of a recursive description")
18
19 (declaim (ftype (function (t stream)) describe-object))
20 (defgeneric describe-object ((x t) stream))
21
22 (defun describe (x &optional (stream-designator *standard-output*))
23   #+sb-doc
24   "Print a description of the object X."
25   (let ((stream (out-synonym-of stream-designator)))
26     (pprint-logical-block (stream nil)
27       (fresh-line stream)
28       (describe-object x stream)
29       (fresh-line stream)))
30   (values))
31 \f
32 ;;;; miscellaneous DESCRIBE-OBJECT methods
33
34 (defmethod describe-object ((x t) s)
35   (format s "~@<~S ~_is a ~S.~:>" x (type-of x)))
36
37 (defmethod describe-object ((x cons) s)
38   (call-next-method)
39   (when (and (legal-function-name-p x)
40              (fboundp 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.
45     ))
46
47 (defmethod describe-object ((x array) s)
48   (let ((rank (array-rank x)))
49     (cond ((> rank 1)
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)))
54           (t
55            (format s
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."
60                      (fill-pointer x))))))
61   (let ((array-element-type (array-element-type x)))
62     (unless (eq array-element-type t)
63       (format s
64               "~@:_Its element type is specialized to ~S."
65               array-element-type))))
66
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))
71   (format s
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~_~;.~]"
77             count (zerop count))
78     (let ((n 0))
79       (declare (type index n))
80       (dohash (k v x)
81         (unless (zerop n)
82           (write-char #\space s))
83         (incf n)
84         (when (and *print-length* (> n *print-length*))
85           (format s "~:_...")
86           (return))
87         (format s "~:_(~@<~S ~:_~S~:>)" k v)))))
88
89 (defmethod describe-object ((condition condition) s)
90   (sb-kernel:describe-condition condition s))
91 \f
92 ;;;; DESCRIBE-OBJECT methods for symbols and functions, including all
93 ;;;; sorts of messy stuff about documentation, type information,
94 ;;;; packaging, function implementation, etc..
95
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)))
102       (when doc
103         (format s "~_~@(~A documentation:~)~@:_  ~A"
104                 (or kind-doc kind) doc))))
105   (values))
106
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))
120       (when (consp type)
121         (format s "~@:_Its ~(~A~) argument types are:~@:_  ~S"
122                 where (second type))
123         (format s "~@:_Its result type is:~@:_  ~S" (third type))))
124     (let ((inlinep (info :function :inlinep name)))
125       (when inlinep
126         (format s
127                 "~@:_It is currently declared ~(~A~);~
128                  ~:[no~;~] expansion is available."
129                 inlinep (info :function :inline-expansion name))))))
130
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)
140       (if args
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
147          name
148          s
149          (type-specifier (sb-eval:interpreted-function-type x)))))
150     (when closure-p
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)))
158
159 ;;; Print information from the debug-info about where CODE-OBJ was
160 ;;; compiled from.
161 (defun %describe-compiled-from (code-obj s)
162   (declare (type stream s))
163   (let ((info (sb-kernel:%code-debug-info code-obj)))
164     (when info
165       (let ((sources (sb-c::debug-info-source info)))
166         (when sources
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
173                                           (first sources))
174                                          :style :abbreviated))
175           (dolist (source sources)
176             (let ((name (sb-c::debug-source-name source)))
177               (ecase (sb-c::debug-source-from source)
178                 (:file
179                  (format s "~@:_~A~@:_  Created: " (namestring name))
180                  (sb-int:format-universal-time s (sb-c::debug-source-created
181                                                   source)))
182                 (:lisp (format s "~@:_~S" name))))))))))
183
184 ;;; Describe a compiled function. The closure case calls us to print
185 ;;; the guts.
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)
197     (cond ((not args)
198            (format s "  There is no argument information available."))
199           ((string= args "()")
200            (write-string "  There are no arguments." s))
201           (t
202            (write-string "  " 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))
211
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))
219
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))
227   (fresh-line s)
228   (ecase 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)))
232   (case (get-type 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
243      (typecase x
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)
248                                           s kind name)
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
259         ;; delegate to that.
260         (describe-object x s))
261        (t
262         (format s "~@:_It is an unknown type of funcallable instance."))))
263     (t
264      (format s "~@:_It is an unknown type of function."))))
265
266 (defmethod describe-object ((x function) s)
267   (%describe-function x s))
268   
269 (defmethod describe-object ((x symbol) s)
270   (declare (type stream s))
271
272   ;; Describe the packaging.
273   (let ((package (symbol-package x)))
274     (if package
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
284   ;; been set to NIL.
285
286   ;; Describe the value cell.
287   (let* ((kind (info :variable :kind x))
288          (wot (ecase kind
289                 (:special "special variable")
290                 (:constant "constant")
291                 (:global "undefined variable")
292                 (:alien nil))))
293     (cond
294      ((eq kind :alien)
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.~:>"
301                 (eval x))))
302      ((boundp x)
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)))
306
307     (when (eq (info :variable :where-from x) :declared)
308       (format s "~@:_Its declared type ~_is ~S."
309               (type-specifier (info :variable :type x))))
310
311     (%describe-doc x s 'variable kind))
312
313   ;; Print out properties.
314   (format s "~@[~@:_Its SYMBOL-PLIST is ~@<~2I~_~S~:>.~]" (symbol-plist x))
315
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"))
321         ((fboundp x)
322          (%describe-function (fdefinition x) s :function x)))
323
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 
328   ;;     redefine them.)
329
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))
335     (format s
336             "~@:_Documentation on the ~(~A~):~@:_~A"
337             (car assoc)
338             (cdr assoc))))