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