/[cmucl]/src/pcl/env.lisp
ViewVC logotype

Contents of /src/pcl/env.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (show annotations)
Sun May 30 23:13:58 1999 UTC (14 years, 10 months ago) by pw
Branch: MAIN
Changes since 1.11: +34 -63 lines
Remove all #+ and #- conditionals from the source code. What is left
is essentially Common Lisp except for explicit references to things
in CMUCL specific packages.
1 ;;;-*-Mode:LISP; Package:(PCL (LISP WALKER)); Base:10; Syntax:Common-lisp -*-
2 ;;;
3 ;;; *************************************************************************
4 ;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
5 ;;; All rights reserved.
6 ;;;
7 ;;; Use and copying of this software and preparation of derivative works
8 ;;; based upon this software are permitted. Any distribution of this
9 ;;; software or derivative works must comply with all applicable United
10 ;;; States export control laws.
11 ;;;
12 ;;; This software is made available AS IS, and Xerox Corporation makes no
13 ;;; warranty about the software, its performance or its conformity to any
14 ;;; specification.
15 ;;;
16 ;;; Any person obtaining a copy of this software is requested to send their
17 ;;; name and post office or electronic mail address to:
18 ;;; CommonLoops Coordinator
19 ;;; Xerox PARC
20 ;;; 3333 Coyote Hill Rd.
21 ;;; Palo Alto, CA 94304
22 ;;; (or send Arpanet mail to CommonLoops-Coordinator.pa@Xerox.arpa)
23 ;;;
24 ;;; Suggestions, comments and requests for improvements are also welcome.
25 ;;; *************************************************************************
26 ;;;
27
28 (ext:file-comment
29 "$Header: /tiger/var/lib/cvsroots/cmucl/src/pcl/env.lisp,v 1.12 1999/05/30 23:13:58 pw Exp $")
30 ;;;
31 ;;; Basic environmental stuff.
32 ;;;
33
34 (in-package :pcl)
35
36 ;;;
37 ;;;
38 ;;;
39
40 (defgeneric describe-object (object stream))
41
42 (defmethod describe-object (object stream)
43 (describe object stream))
44
45 (defmethod describe-object ((object slot-object) stream)
46 (let* ((class (class-of object))
47 (slotds (slots-to-inspect class object))
48 (max-slot-name-length 0)
49 (instance-slotds ())
50 (class-slotds ())
51 (other-slotds ()))
52 (flet ((adjust-slot-name-length (name)
53 (setq max-slot-name-length
54 (max max-slot-name-length
55 (length (the string (symbol-name name))))))
56 (describe-slot (name value &optional (allocation () alloc-p))
57 (if alloc-p
58 (format stream
59 "~% ~A ~S ~VT ~S"
60 name allocation (+ max-slot-name-length 7) value)
61 (format stream
62 "~% ~A~VT ~S"
63 name max-slot-name-length value))))
64 ;; Figure out a good width for the slot-name column.
65 (dolist (slotd slotds)
66 (adjust-slot-name-length (slot-definition-name slotd))
67 (case (slot-definition-allocation slotd)
68 (:instance (push slotd instance-slotds))
69 (:class (push slotd class-slotds))
70 (otherwise (push slotd other-slotds))))
71 (setq max-slot-name-length (min (+ max-slot-name-length 3) 30))
72 (format stream "~%~S is an instance of class ~S:" object class)
73
74 (when instance-slotds
75 (format stream "~% The following slots have :INSTANCE allocation:")
76 (dolist (slotd (nreverse instance-slotds))
77 (describe-slot (slot-definition-name slotd)
78 (slot-value-or-default object (slot-definition-name slotd)))))
79
80 (when class-slotds
81 (format stream "~% The following slots have :CLASS allocation:")
82 (dolist (slotd (nreverse class-slotds))
83 (describe-slot (slot-definition-name slotd)
84 (slot-value-or-default object (slot-definition-name slotd)))))
85
86 (when other-slotds
87 (format stream "~% The following slots have allocation as shown:")
88 (dolist (slotd (nreverse other-slotds))
89 (describe-slot (slot-definition-name slotd)
90 (slot-value-or-default object (slot-definition-name slotd))
91 (slot-definition-allocation slotd))))
92 (values))))
93
94 (defmethod slots-to-inspect ((class slot-class) (object slot-object))
95 (class-slots class))
96
97 (defvar *describe-metaobjects-as-objects-p* nil)
98
99 (defmethod describe-object ((fun standard-generic-function) stream)
100 (format stream "~A is a generic function.~%" fun)
101 (format stream "Its arguments are:~% ~S~%"
102 (generic-function-pretty-arglist fun))
103 (format stream "Its methods are:")
104 (dolist (meth (generic-function-methods fun))
105 (format stream "~2% ~{~S ~}~:S =>~%"
106 (method-qualifiers meth)
107 (unparse-specializers meth))
108 (describe-object (or (method-fast-function meth)
109 (method-function meth))
110 stream))
111 (when *describe-metaobjects-as-objects-p*
112 (call-next-method)))
113
114 ;;;
115 ;;;
116 ;;;
117 (defmethod describe-object ((class class) stream)
118 (flet ((pretty-class (c) (or (class-name c) c)))
119 (macrolet ((ft (string &rest args) `(format stream ,string ,@args)))
120 (ft "~&~S is a class, it is an instance of ~S.~%"
121 class (pretty-class (class-of class)))
122 (let ((name (class-name class)))
123 (if name
124 (if (eq class (find-class name nil))
125 (ft "Its proper name is ~S.~%" name)
126 (ft "Its name is ~S, but this is not a proper name.~%" name))
127 (ft "It has no name (the name is NIL).~%")))
128 (ft "The direct superclasses are: ~:S, and the direct~%~
129 subclasses are: ~:S. The class precedence list is:~%~S~%~
130 There are ~D methods specialized for this class."
131 (mapcar #'pretty-class (class-direct-superclasses class))
132 (mapcar #'pretty-class (class-direct-subclasses class))
133 (mapcar #'pretty-class (class-precedence-list class))
134 (length (specializer-direct-methods class)))))
135 (when *describe-metaobjects-as-objects-p*
136 (call-next-method)))
137
138 (defun describe-package (object stream)
139 (unless (packagep object) (setq object (find-package object)))
140 (format stream "~&~S is a ~S.~%" object (type-of object))
141 (let ((nick (package-nicknames object)))
142 (when nick
143 (format stream "You can also call it~@[ ~{~S~^, ~} or~] ~S.~%"
144 (butlast nick) (first (last nick)))))
145 (let* ((internal (lisp::package-internal-symbols object))
146 (internal-count (- (lisp::package-hashtable-size internal)
147 (lisp::package-hashtable-free internal)))
148 (external (lisp::package-external-symbols object))
149 (external-count (- (lisp::package-hashtable-size external)
150 (lisp::package-hashtable-free external))))
151 (format stream "It has ~D internal and ~D external symbols (~D total).~%"
152 internal-count external-count (+ internal-count external-count)))
153 (let ((used (package-use-list object)))
154 (when used
155 (format stream "It uses the packages ~{~S~^, ~}.~%"
156 (mapcar #'package-name used))))
157 (let ((users (package-used-by-list object)))
158 (when users
159 (format stream "It is used by the packages ~{~S~^, ~}.~%"
160 (mapcar #'package-name users)))))
161
162 (defmethod describe-object ((object package) stream)
163 (describe-package object stream))
164
165 (defmethod describe-object ((object hash-table) stream)
166 (format stream "~&~S is an ~a hash table."
167 object
168 (lisp::hash-table-test object))
169 (format stream "~&Its size is ~d buckets."
170 (lisp::hash-table-size object))
171 (format stream "~&Its rehash-size is ~d."
172 (lisp::hash-table-rehash-size object))
173 (format stream "~&Its rehash-threshold is ~d."
174 (hash-table-rehash-threshold object))
175 (format stream "~&It currently holds ~d entries."
176 (lisp::hash-table-number-entries object)))
177
178
179
180 ;;;
181 ;;; trace-method and untrace-method accept method specs as arguments. A
182 ;;; method-spec should be a list like:
183 ;;; (<generic-function-spec> qualifiers* (specializers*))
184 ;;; where <generic-function-spec> should be either a symbol or a list
185 ;;; of (SETF <symbol>).
186 ;;;
187 ;;; For example, to trace the method defined by:
188 ;;;
189 ;;; (defmethod foo ((x spaceship)) 'ss)
190 ;;;
191 ;;; You should say:
192 ;;;
193 ;;; (trace-method '(foo (spaceship)))
194 ;;;
195 ;;; You can also provide a method object in the place of the method
196 ;;; spec, in which case that method object will be traced.
197 ;;;
198 ;;; For untrace-method, if an argument is given, that method is untraced.
199 ;;; If no argument is given, all traced methods are untraced.
200 ;;;
201 (defclass traced-method (method)
202 ((method :initarg :method)
203 (function :initarg :function
204 :reader method-function)
205 (generic-function :initform nil
206 :accessor method-generic-function)))
207
208 (defmethod method-lambda-list ((m traced-method))
209 (with-slots (method) m (method-lambda-list method)))
210
211 (defmethod method-specializers ((m traced-method))
212 (with-slots (method) m (method-specializers method)))
213
214 (defmethod method-qualifiers ((m traced-method))
215 (with-slots (method) m (method-qualifiers method)))
216
217 (defmethod accessor-method-slot-name ((m traced-method))
218 (with-slots (method) m (accessor-method-slot-name method)))
219
220 (defvar *traced-methods* ())
221
222 (defun trace-method (spec &rest options)
223 #+copy-&rest-arg (setq options (copy-list options))
224 (multiple-value-bind (gf omethod name)
225 (parse-method-or-spec spec)
226 (let* ((tfunction (trace-method-internal (method-function omethod)
227 name
228 options))
229 (tmethod (make-instance 'traced-method
230 :method omethod
231 :function tfunction)))
232 (remove-method gf omethod)
233 (add-method gf tmethod)
234 (pushnew tmethod *traced-methods*)
235 tmethod)))
236
237 (defun untrace-method (&optional spec)
238 (flet ((untrace-1 (m)
239 (let ((gf (method-generic-function m)))
240 (when gf
241 (remove-method gf m)
242 (add-method gf (slot-value m 'method))
243 (setq *traced-methods* (remove m *traced-methods*))))))
244 (if (not (null spec))
245 (multiple-value-bind (gf method)
246 (parse-method-or-spec spec)
247 (declare (ignore gf))
248 (if (memq method *traced-methods*)
249 (untrace-1 method)
250 (error "~S is not a traced method?" method)))
251 (dolist (m *traced-methods*) (untrace-1 m)))))
252
253 (defun trace-method-internal (ofunction name options)
254 (eval `(untrace ,name))
255 (setf (symbol-function name) ofunction)
256 (eval `(trace ,name ,@options))
257 (symbol-function name))
258
259
260
261
262 ;(defun compile-method (spec)
263 ; (multiple-value-bind (gf method name)
264 ; (parse-method-or-spec spec)
265 ; (declare (ignore gf))
266 ; (compile name (method-function method))
267 ; (setf (method-function method) (symbol-function name))))
268
269 (defmacro undefmethod (&rest args)
270 (declare (arglist name {method-qualifier}* specializers))
271 `(undefmethod-1 ',args))
272
273 (defun undefmethod-1 (args)
274 (multiple-value-bind (gf method)
275 (parse-method-or-spec args)
276 (when (and gf method)
277 (remove-method gf method)
278 method)))
279
280
281 (pushnew :pcl *features*)
282 (pushnew :portable-commonloops *features*)
283 (pushnew :pcl-structures *features*)
284
285 (when (find-package "OLD-PCL")
286 (setf (symbol-function (find-symbol "PRINT-OBJECT" :old-pcl))
287 (symbol-function 'pcl::print-object)))
288
289
290 ;;;; MAKE-LOAD-FORM
291
292 (export '(cl::make-load-form cl::make-load-form-saving-slots) "CL")
293
294 (defgeneric make-load-form (object &optional environment))
295
296 (defmethod make-load-form ((object structure-object) &optional environment)
297 (declare (ignore environment))
298 (kernel:make-structure-load-form object))
299
300 (defmethod make-load-form ((object wrapper) &optional env)
301 (declare (ignore env))
302 (let ((pname (kernel:class-proper-name (kernel:layout-class object))))
303 (unless pname
304 (error "Can't dump wrapper for anonymous class:~% ~S"
305 (kernel:layout-class object)))
306 `(kernel:class-layout (lisp:find-class ',pname))))
307
308 (defun make-load-form-saving-slots (object &key slot-names environment)
309 (declare (ignore environment))
310 (when slot-names
311 (warn ":SLOT-NAMES MAKE-LOAD-FORM option not implemented, dumping all ~
312 slots:~% ~S"
313 object))
314 :just-dump-it-normally)
315
316
317 ;;; The following are hacks to deal with CMU CL having two different CLASS
318 ;;; classes.
319 ;;;
320 (defun coerce-to-pcl-class (class)
321 (if (typep class 'lisp:class)
322 (or (kernel:class-pcl-class class)
323 (find-structure-class (lisp:class-name class)))
324 class))
325
326 (defmethod make-instance ((class lisp:class) &rest stuff)
327 (apply #'make-instance (coerce-to-pcl-class class) stuff))
328 (defmethod change-class (instance (class lisp:class))
329 (apply #'change-class instance (coerce-to-pcl-class class)))
330
331 (macrolet ((frob (&rest names)
332 `(progn
333 ,@(mapcar #'(lambda (name)
334 `(defmethod ,name ((class lisp:class))
335 (funcall #',name
336 (coerce-to-pcl-class class))))
337 names))))
338 (frob
339 class-direct-slots
340 class-prototype
341 class-precedence-list
342 class-direct-default-initargs
343 class-direct-superclasses
344 compute-class-precedence-list
345 class-default-initargs class-finalized-p
346 class-direct-subclasses class-slots
347 make-instances-obsolete))

  ViewVC Help
Powered by ViewVC 1.1.5