Improve correctness and consistency of 'eval-when' usage.
[bpt/guile.git] / module / oop / goops / describe.scm
CommitLineData
14f1d9fe
MD
1;;; installed-scm-file
2
4ff2133a
LC
3;;;; Copyright (C) 1998, 1999, 2001, 2006, 2008, 2009 Free Software Foundation, Inc.
4;;;; Copyright (C) 1993-1998 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
5;;;;
73be1d9e
MV
6;;;; This library is free software; you can redistribute it and/or
7;;;; modify it under the terms of the GNU Lesser General Public
8;;;; License as published by the Free Software Foundation; either
53befeb7 9;;;; version 3 of the License, or (at your option) any later version.
4ff2133a 10;;;;
73be1d9e 11;;;; This library is distributed in the hope that it will be useful,
14f1d9fe 12;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
13;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14;;;; Lesser General Public License for more details.
4ff2133a 15;;;;
73be1d9e
MV
16;;;; You should have received a copy of the GNU Lesser General Public
17;;;; License along with this library; if not, write to the Free Software
92205699 18;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4ff2133a 19;;;;
14f1d9fe
MD
20\f
21
14f1d9fe 22;;;;
4ff2133a
LC
23;;;; This file was based upon describe.stklos from the STk distribution
24;;;; version 4.0.1 by Erick Gallesio <eg@unice.fr>.
14f1d9fe
MD
25;;;;
26
27(define-module (oop goops describe)
28 :use-module (oop goops)
29 :use-module (ice-9 session)
1a179b03
MD
30 :use-module (ice-9 format)
31 :export (describe)) ; Export the describe generic function
14f1d9fe
MD
32
33;;;
34;;; describe for simple objects
35;;;
71d540f7 36(define-method (describe (x <top>))
14f1d9fe
MD
37 (format #t "~s is " x)
38 (cond
39 ((integer? x) (format #t "an integer"))
40 ((real? x) (format #t "a real"))
41 ((complex? x) (format #t "a complex number"))
42 ((null? x) (format #t "an empty list"))
43 ((boolean? x) (format #t "a boolean value (~s)" (if x 'true 'false)))
44 ((char? x) (format #t "a character, ascii value is ~s"
45 (char->integer x)))
46 ((symbol? x) (format #t "a symbol"))
47 ((list? x) (format #t "a list"))
48 ((pair? x) (if (pair? (cdr x))
49 (format #t "an improper list")
50 (format #t "a pair")))
51 ((string? x) (if (eqv? x "")
52 (format #t "an empty string")
53 (format #t "a string of length ~s" (string-length x))))
54 ((vector? x) (if (eqv? x '#())
55 (format #t "an empty vector")
56 (format #t "a vector of length ~s" (vector-length x))))
57 ((eof-object? x) (format #t "the end-of-file object"))
58 (else (format #t "an unknown object (~s)" x)))
59 (format #t ".~%")
60 *unspecified*)
61
71d540f7 62(define-method (describe (x <procedure>))
14f1d9fe
MD
63 (let ((name (procedure-name x)))
64 (if name
65 (format #t "`~s'" name)
66 (display x))
67 (display " is ")
68 (display (if name #\a "an anonymous"))
314b8716 69 (display " procedure")
14f1d9fe
MD
70 (display " with ")
71 (arity x)))
72
73;;;
74;;; describe for GOOPS instances
75;;;
76(define (safe-class-name class)
77 (if (slot-bound? class 'name)
78 (class-name class)
79 class))
80
71d540f7 81(define-method (describe (x <object>))
14f1d9fe
MD
82 (format #t "~S is an instance of class ~A~%"
83 x (safe-class-name (class-of x)))
84
85 ;; print all the instance slots
86 (format #t "Slots are: ~%")
87 (for-each (lambda (slot)
88 (let ((name (slot-definition-name slot)))
89 (format #t " ~S = ~A~%"
90 name
91 (if (slot-bound? x name)
92 (format #f "~S" (slot-ref x name))
93 "#<unbound>"))))
94 (class-slots (class-of x)))
95 *unspecified*)
96
97;;;
98;;; Describe for classes
99;;;
71d540f7 100(define-method (describe (x <class>))
14f1d9fe
MD
101 (format #t "~S is a class. It's an instance of ~A~%"
102 (safe-class-name x) (safe-class-name (class-of x)))
103
104 ;; Super classes
105 (format #t "Superclasses are:~%")
106 (for-each (lambda (class) (format #t " ~A~%" (safe-class-name class)))
107 (class-direct-supers x))
108
109 ;; Direct slots
110 (let ((slots (class-direct-slots x)))
111 (if (null? slots)
112 (format #t "(No direct slot)~%")
113 (begin
114 (format #t "Directs slots are:~%")
115 (for-each (lambda (s)
116 (format #t " ~A~%" (slot-definition-name s)))
117 slots))))
118
119
120 ;; Direct subclasses
121 (let ((classes (class-direct-subclasses x)))
122 (if (null? classes)
123 (format #t "(No direct subclass)~%")
124 (begin
125 (format #t "Directs subclasses are:~%")
126 (for-each (lambda (s)
127 (format #t " ~A~%" (safe-class-name s)))
128 classes))))
129
130 ;; CPL
131 (format #t "Class Precedence List is:~%")
132 (for-each (lambda (s) (format #t " ~A~%" (safe-class-name s)))
133 (class-precedence-list x))
134
135 ;; Direct Methods
136 (let ((methods (class-direct-methods x)))
137 (if (null? methods)
138 (format #t "(No direct method)~%")
139 (begin
140 (format #t "Class direct methods are:~%")
141 (for-each describe methods))))
142
143; (format #t "~%Field Initializers ~% ")
144; (write (slot-ref x 'initializers)) (newline)
145
146; (format #t "~%Getters and Setters~% ")
147; (write (slot-ref x 'getters-n-setters)) (newline)
148)
149
150;;;
151;;; Describe for generic functions
152;;;
71d540f7 153(define-method (describe (x <generic>))
14f1d9fe
MD
154 (let ((name (generic-function-name x))
155 (methods (generic-function-methods x)))
156 ;; Title
157 (format #t "~S is a generic function. It's an instance of ~A.~%"
158 name (safe-class-name (class-of x)))
159 ;; Methods
160 (if (null? methods)
161 (format #t "(No method defined for ~S)~%" name)
162 (begin
163 (format #t "Methods defined for ~S~%" name)
164 (for-each (lambda (x) (describe x #t)) methods)))))
165
166;;;
167;;; Describe for methods
168;;;
71d540f7 169(define-method (describe (x <method>) . omit-generic)
14f1d9fe
MD
170 (letrec ((print-args (lambda (args)
171 ;; take care of dotted arg lists
172 (cond ((null? args) (newline))
173 ((pair? args)
174 (display #\space)
175 (display (safe-class-name (car args)))
176 (print-args (cdr args)))
177 (else
178 (display #\space)
179 (display (safe-class-name args))
180 (newline))))))
181
182 ;; Title
183 (format #t " Method ~A~%" x)
184
185 ;; Associated generic
186 (if (null? omit-generic)
187 (let ((gf (method-generic-function x)))
188 (if gf
189 (format #t "\t Generic: ~A~%" (generic-function-name gf))
190 (format #t "\t(No generic)~%"))))
191
192 ;; GF specializers
193 (format #t "\tSpecializers:")
194 (print-args (method-specializers x))))
195
bbb6fc4f 196(provide 'describe)