0.pre7.88:
[sbcl.git] / src / pcl / describe.lisp
1 ;;;; that part of the DESCRIBE mechanism which is based on code from
2 ;;;; PCL
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 software originally released by Xerox
8 ;;;; Corporation. Copyright and release statements follow. Later modifications
9 ;;;; to the software are in the public domain and are provided with
10 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
11 ;;;; information.
12
13 ;;;; copyright information from original PCL sources:
14 ;;;;
15 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
16 ;;;; All rights reserved.
17 ;;;;
18 ;;;; Use and copying of this software and preparation of derivative works based
19 ;;;; upon this software are permitted. Any distribution of this software or
20 ;;;; derivative works must comply with all applicable United States export
21 ;;;; control laws.
22 ;;;;
23 ;;;; This software is made available AS IS, and Xerox Corporation makes no
24 ;;;; warranty about the software, its performance or its conformity to any
25 ;;;; specification.
26
27 (in-package "SB-PCL")
28
29 (defmethod slots-to-inspect ((class slot-class) (object slot-object))
30   (class-slots class))
31
32 (defmethod describe-object ((object slot-object) stream)
33
34   (let* ((class (class-of object))
35          (slotds (slots-to-inspect class object))
36          (max-slot-name-length 0)
37          (instance-slotds ())
38          (class-slotds ())
39          (other-slotds ()))
40
41     (flet ((adjust-slot-name-length (name)
42              (setq max-slot-name-length
43                    (max max-slot-name-length
44                         (length (the string (symbol-name name))))))
45            (describe-slot (name value &optional (allocation () alloc-p))
46              (if alloc-p
47                  (format stream
48                          "~% ~A ~S ~VT  ~S"
49                          name allocation (+ max-slot-name-length 7) value)
50                  (format stream
51                          "~% ~A~VT  ~S"
52                          name max-slot-name-length value))))
53
54       ;; Figure out a good width for the slot-name column.
55       (dolist (slotd slotds)
56         (adjust-slot-name-length (slot-definition-name slotd))
57         (case (slot-definition-allocation slotd)
58           (:instance (push slotd instance-slotds))
59           (:class  (push slotd class-slotds))
60           (otherwise (push slotd other-slotds))))
61       (setq max-slot-name-length  (min (+ max-slot-name-length 3) 30))
62       (format stream "~%~@<~S ~_is an instance of class ~S.~:>" object class)
63
64       ;; Now that we know the width, we can print.
65       (when instance-slotds
66         (format stream "~%The following slots have :INSTANCE allocation:")
67         (dolist (slotd (nreverse instance-slotds))
68           (describe-slot
69            (slot-definition-name slotd)
70            (slot-value-or-default object
71                                   (slot-definition-name slotd)))))
72       (when class-slotds
73         (format stream "~%The following slots have :CLASS allocation:")
74         (dolist (slotd (nreverse class-slotds))
75           (describe-slot
76            (slot-definition-name slotd)
77            (slot-value-or-default object
78                                   (slot-definition-name slotd)))))
79       (when other-slotds
80         (format stream "~%The following slots have allocation as shown:")
81         (dolist (slotd (nreverse other-slotds))
82           (describe-slot
83            (slot-definition-name slotd)
84            (slot-value-or-default object
85                                   (slot-definition-name slotd))
86            (slot-definition-allocation slotd)))))))
87
88 (defvar *describe-metaobjects-as-objects-p* nil)
89
90 (defmethod describe-object ((fun standard-generic-function) stream)
91   (format stream "~A is a generic function.~%" fun)
92   (format stream "Its arguments are:~%  ~S~%"
93           (generic-function-pretty-arglist fun))
94   (format stream "Its methods are:")
95   (dolist (method (generic-function-methods fun))
96     (format stream "~2%    ~{~S ~}~:S =>~%"
97             (method-qualifiers method)
98             (unparse-specializers method))
99     (describe-object (or (method-fast-function method)
100                          (method-function method))
101                      stream))
102   (when *describe-metaobjects-as-objects-p*
103     (call-next-method)))
104
105 (defmethod describe-object ((class class) stream)
106   (flet ((pretty-class (c) (or (class-name c) c)))
107     (macrolet ((ft (string &rest args) `(format stream ,string ,@args)))
108       (ft "~&~S is a class, it is an instance of ~S.~%"
109           class (pretty-class (class-of class)))
110       (let ((name (class-name class)))
111         (if name
112             (if (eq class (find-class name nil))
113                 (ft "Its proper name is ~S.~%" name)
114                 (ft "Its name is ~S, but this is not a proper name.~%" name))
115             (ft "It has no name (the name is NIL).~%")))
116       (ft "The direct superclasses are: ~:S, and the direct~%~
117            subclasses are: ~:S. The class precedence list is:~%~S~%~
118            There are ~W methods specialized for this class."
119           (mapcar #'pretty-class (class-direct-superclasses class))
120           (mapcar #'pretty-class (class-direct-subclasses class))
121           (mapcar #'pretty-class (class-precedence-list class))
122           (length (specializer-direct-methods class)))))
123   (when *describe-metaobjects-as-objects-p*
124     (call-next-method)))
125
126 (defmethod describe-object ((package package) stream)
127   (pprint-logical-block (stream nil)
128     (format stream "~&~S is a ~S." package (type-of package))
129     (format stream
130             "~@[~&It has nicknames ~{~:_~S~^ ~}~]"
131             (package-nicknames package))
132     (let* ((internal (package-internal-symbols package))
133            (internal-count (- (package-hashtable-size internal)
134                               (package-hashtable-free internal)))
135            (external (package-external-symbols package))
136            (external-count (- (package-hashtable-size external)
137                               (package-hashtable-free external))))
138       (format stream
139               "~&It has ~S internal and ~S external symbols."
140               internal-count external-count))
141     (format stream
142             "~@[~&It uses ~{~:_~S~^ ~}~]"
143             (package-use-list package))
144     (format stream
145             "~@[~&It is used by ~{~:_~S~^ ~}~]"
146             (package-used-by-list package))))