0.6.8.17:
[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-conditions::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 (symbol 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. Type-Spec is the function type specifier
108 ;;; extracted from the definition, or NIL if none.
109 (declaim (ftype (function ((or symbol cons) stream t)) %describe-function-name))
110 (defun %describe-function-name (name s type-spec) 
111   (multiple-value-bind (type where)
112       (if (or (symbolp name) (and (listp name) (eq (car name) 'setf)))
113           (values (type-specifier (info :function :type name))
114                   (info :function :where-from name))
115           (values type-spec :defined))
116     (when (consp type)
117       (format s "~@:_Its ~(~A~) argument types are:~@:_  ~S"
118               where (second type))
119       (format s "~@:_Its result type is:~@:_  ~S" (third type))))
120   (let ((inlinep (info :function :inlinep name)))
121     (when inlinep
122       (format s "~@:_It is currently declared ~(~A~);~
123                  ~:[no~;~] expansion is available."
124               inlinep (info :function :inline-expansion name)))))
125
126 ;;; Interpreted function describing; handles both closure and
127 ;;; non-closure functions. Instead of printing the compiled-from info,
128 ;;; we print the definition.
129 (defun %describe-function-interpreted (x s kind name)
130   (declare (type stream s))
131   (multiple-value-bind (exp closure-p dname)
132       (sb-eval:interpreted-function-lambda-expression x)
133     (let ((args (sb-eval:interpreted-function-arglist x)))
134       (format s "~@:_~@(~@[~A ~]arguments:~@:_~)" kind)
135       (if args
136           (format s "  ~<~S~:>" args)
137           (write-string "  There are no arguments." s)))
138     (let ((name (or name dname)))
139       (%describe-doc name s 'function kind)
140       (unless (eq kind :macro)
141         (%describe-function-name
142          name
143          s
144          (type-specifier (sb-eval:interpreted-function-type x)))))
145     (when closure-p
146       (format s "~@:_Its closure environment is:")
147       (pprint-logical-block (s nil)
148         (pprint-indent :current 2)
149         (let ((clos (sb-eval:interpreted-function-closure x)))
150           (dotimes (i (length clos))
151             (format s "~@:_~S: ~S" i (svref clos i))))))
152     (format s "~@:_Its definition is:~@:_  ~S" exp)))
153
154 ;;; Print information from the debug-info about where CODE-OBJ was
155 ;;; compiled from.
156 (defun %describe-compiled-from (code-obj s)
157   (declare (type stream s))
158   (let ((info (sb-kernel:%code-debug-info code-obj)))
159     (when info
160       (let ((sources (sb-c::debug-info-source info)))
161         (when sources
162           (format s "~@:_On ~A it was compiled from:"
163                   ;; FIXME: The FORMAT-UNIVERSAL-TIME calls in the system
164                   ;; should become more consistent, probably not using
165                   ;; any nondefault options.
166                   (format-universal-time nil
167                                          (sb-c::debug-source-compiled
168                                           (first sources))
169                                          :style :abbreviated))
170           (dolist (source sources)
171             (let ((name (sb-c::debug-source-name source)))
172               (ecase (sb-c::debug-source-from source)
173                 (:file
174                  (format s "~@:_~A~@:_  Created: " (namestring name))
175                  (sb-int:format-universal-time t (sb-c::debug-source-created
176                                                   source)))
177                 (:lisp (format s "~@:_~S" name))))))))))
178
179 ;;; Describe a compiled function. The closure case calls us to print
180 ;;; the guts.
181 (defun %describe-function-compiled (x s kind name)
182   (declare (type stream s))
183   ;; FIXME: The lowercaseness of %FUNCTION-ARGLIST results, and the
184   ;; non-sentenceness of the "Arguments" label, makes awkward output.
185   ;; Better would be "Its arguments are: ~S" (with uppercase argument
186   ;; names) when arguments are known, and otherwise "There is no
187   ;; information available about its arguments." or "It has no
188   ;; arguments." (And why is %FUNCTION-ARGLIST a string instead of a
189   ;; list of symbols anyway?)
190   (let ((args (%function-arglist x)))
191     (format s "~@:_~@(~@[~A ~]arguments:~@:_~)" kind)
192     (cond ((not args)
193            (format s "  There is no argument information available."))
194           ((string= args "()")
195            (write-string "  There are no arguments." s))
196           (t
197            (write-string "  " s)
198            (pprint-logical-block (s nil)
199              (pprint-indent :current 2)
200              (write-string args s)))))
201   (let ((name (or name (%function-name x))))
202     (%describe-doc name s 'function kind)
203     (unless (eq kind :macro)
204       (%describe-function-name name s (%function-type x))))
205   (%describe-compiled-from (sb-kernel:function-code-header x) s))
206
207 (defun %describe-function-byte-compiled (x s kind name)
208   (declare (type stream s))
209   (let ((name (or name (sb-c::byte-function-name x))))
210     (%describe-doc name s 'function kind)
211     (unless (eq kind :macro)
212       (%describe-function-name name s 'function)))
213   (%describe-compiled-from (sb-c::byte-function-component x) s))
214
215 ;;; Describe a function with the specified kind and name. The latter
216 ;;; arguments provide some information about where the function came
217 ;;; from. Kind NIL means not from a name.
218 (defun %describe-function (x s &optional (kind nil) name)
219   (declare (type function x))
220   (declare (type stream s))
221   (declare (type (member :macro :function nil) kind))
222   (fresh-line s)
223   (ecase kind
224     (:macro (format s "Macro-function: ~S" x))
225     (:function (format s "Function: ~S" x))
226     ((nil) (format s "~S is a function." x)))
227   (case (get-type x)
228     (#.sb-vm:closure-header-type
229      (%describe-function-compiled (%closure-function x) s kind name)
230      (format s "~@:_Its closure environment is:")
231      (pprint-logical-block (s nil)
232        (pprint-indent :current 8)
233        (dotimes (i (- (get-closure-length x) (1- sb-vm:closure-info-offset)))
234          (format s "~@:_~S: ~S" i (%closure-index-ref x i)))))
235     ((#.sb-vm:function-header-type #.sb-vm:closure-function-header-type)
236      (%describe-function-compiled x s kind name))
237     (#.sb-vm:funcallable-instance-header-type
238      (typecase x
239        (sb-kernel:byte-function
240         (%describe-function-byte-compiled x s kind name))
241        (sb-kernel:byte-closure
242         (%describe-function-byte-compiled (byte-closure-function x)
243                                           s kind name)
244         (format s "~@:_Its closure environment is:")
245         (pprint-logical-block (s nil)
246           (pprint-indent :current 8)
247           (let ((data (byte-closure-data x)))
248             (dotimes (i (length data))
249               (format s "~@:_~S: ~S" i (svref data i))))))
250        (sb-eval:interpreted-function
251         (%describe-function-interpreted x s kind name))
252        (standard-generic-function
253         ;; There should be a special method for this case; we'll
254         ;; delegate to that.
255         (describe-object x s))
256        (t
257         (format s "~@:_It is an unknown type of funcallable instance."))))
258     (t
259      (format s "~@:_It is an unknown type of function."))))
260
261 (defmethod describe-object ((x function) s)
262   (%describe-function x s))
263   
264 (defmethod describe-object ((x symbol) s)
265   (declare (type stream s))
266
267   ;; Describe the packaging.
268   (let ((package (symbol-package x)))
269     (if package
270         (multiple-value-bind (symbol status)
271             (find-symbol (symbol-name x) package)
272           (declare (ignore symbol))
273           (format s "~S is an ~(~A~) symbol in ~S."
274                   x status (symbol-package x)))
275         (format s "~S is an uninterned symbol." x)))
276   ;; TO DO: We could grovel over all packages looking for and
277   ;; reporting other phenomena, e.g. IMPORT and SHADOW, or
278   ;; availability in some package even after (SYMBOL-PACKAGE X) has
279   ;; been set to NIL.
280
281   ;; Describe the value cell.
282   (let* ((kind (info :variable :kind x))
283          (wot (ecase kind
284                 (:special "special variable")
285                 (:constant "constant")
286                 (:global "undefined variable")
287                 (:alien nil))))
288     (cond
289      ((eq kind :alien)
290       (let ((info (info :variable :alien-info x)))
291         (format s "~@:_~@<It is an alien at #X~8,'0X of type ~3I~:_~S.~:>~@:_"
292                 (sap-int (eval (sb-alien::heap-alien-info-sap-form info)))
293                 (sb-alien-internals:unparse-alien-type
294                  (sb-alien::heap-alien-info-type info)))
295         (format s "~@<Its current value is ~3I~:_~S.~:>"
296                 (eval x))))
297      ((boundp x)
298       (format s "~@:_It is a ~A; its value is ~S." wot (symbol-value x)))
299      ((not (eq kind :global))
300       (format s "~@:_It is a ~A; no current value." wot)))
301
302     (when (eq (info :variable :where-from x) :declared)
303       (format s "~@:_Its declared type is ~S."
304               (type-specifier (info :variable :type x))))
305
306     (%describe-doc x s 'variable kind))
307
308   ;; Print out properties.
309   (format s "~@[~@:_Its SYMBOL-PLIST is ~@<~2I~_~S~:>.~]" (symbol-plist x))
310
311   ;; Describe the function cell.
312   (cond ((macro-function x)
313          (%describe-function (macro-function x) s :macro x))
314         ((special-operator-p x)
315          (%describe-doc x s 'function "Special form"))
316         ((fboundp x)
317          (%describe-function (fdefinition x) s :function x)))
318
319   ;; TO DO: Print out other stuff from the INFO database:
320   ;;   * Does it name a type or class?
321   ;;   * Is it a structure accessor? (important since those are 
322   ;;     magical in some ways, e.g. blasting the structure if you 
323   ;;     redefine them)
324
325   ;; Print other documentation.
326   (%describe-doc x s 'structure "Structure")
327   (%describe-doc x s 'type "Type")
328   (%describe-doc x s 'setf "Setf macro")
329   (dolist (assoc (info :random-documentation :stuff x))
330     (format s
331             "~@:_Documentation on the ~(~A~):~@:_~A"
332             (car assoc)
333             (cdr assoc))))