/[cmucl]/src/pcl/std-class.lisp
ViewVC logotype

Contents of /src/pcl/std-class.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.54 - (show annotations)
Fri Apr 25 17:43:50 2003 UTC (10 years, 11 months ago) by gerd
Branch: MAIN
Changes since 1.53: +18 -16 lines
	MOP bug fix for problem with (SETF SLOT-VALUE-USING-CLASS)
	reported by Kevin Rosenberg <kevin@rosenberg.net> on cmucl-imp,
	Tue, 22 Apr 2003 13:28:23 -0600.  There's a test case in
	the thread on cmucl-imp.

	* src/pcl/methods.lisp (update-slot-value-gf-info):
	Moved here from slots-boot.lisp.
	(get-accessor-method-function): Don't pass nil wrappers
	to get-secondary-dispatch-function.  See comment there.
	(update-gf-dfun): Remove unused code.

	* src/pcl/slots-boot.lisp (get-accessor-from-svuc-method-function):
	Move to methods.lisp, where it's used.

	* src/pcl/std-class.lisp (compute-slot-accessor-info): Add
	comment.

	Code cleanup.

	* src/pcl/std-class.lisp (compute-default-initargs): Don't use a
	recursive local function for something that can be done with loop
	in 2 lines.
1 ;;;-*-Mode:LISP; Package:PCL; 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/std-class.lisp,v 1.54 2003/04/25 17:43:50 gerd Exp $")
30
31 (in-package :pcl)
32
33 (defmethod slot-accessor-function ((slotd effective-slot-definition) type)
34 (ecase type
35 (reader (slot-definition-reader-function slotd))
36 (writer (slot-definition-writer-function slotd))
37 (boundp (slot-definition-boundp-function slotd))))
38
39 (defmethod (setf slot-accessor-function) (function
40 (slotd effective-slot-definition) type)
41 (ecase type
42 (reader (setf (slot-definition-reader-function slotd) function))
43 (writer (setf (slot-definition-writer-function slotd) function))
44 (boundp (setf (slot-definition-boundp-function slotd) function))))
45
46 (defconstant *slotd-reader-function-std-p* 1)
47 (defconstant *slotd-writer-function-std-p* 2)
48 (defconstant *slotd-boundp-function-std-p* 4)
49 (defconstant *slotd-all-function-std-p* 7)
50
51 (defmethod slot-accessor-std-p ((slotd effective-slot-definition) type)
52 (let ((flags (slot-value slotd 'accessor-flags)))
53 (declare (type fixnum flags))
54 (if (eq type 'all)
55 (eql *slotd-all-function-std-p* flags)
56 (let ((mask (ecase type
57 (reader *slotd-reader-function-std-p*)
58 (writer *slotd-writer-function-std-p*)
59 (boundp *slotd-boundp-function-std-p*))))
60 (declare (type fixnum mask))
61 (not (zerop (the fixnum (logand mask flags))))))))
62
63 (defmethod (setf slot-accessor-std-p) (value (slotd effective-slot-definition) type)
64 (let ((mask (ecase type
65 (reader *slotd-reader-function-std-p*)
66 (writer *slotd-writer-function-std-p*)
67 (boundp *slotd-boundp-function-std-p*)))
68 (flags (slot-value slotd 'accessor-flags)))
69 (declare (type fixnum mask flags))
70 (setf (slot-value slotd 'accessor-flags)
71 (if value
72 (the fixnum (logior mask flags))
73 (the fixnum (logand (the fixnum (lognot mask)) flags)))))
74 value)
75
76 (defmethod initialize-internal-slot-functions ((slotd effective-slot-definition))
77 (let* ((name (slot-value slotd 'name))
78 (class (slot-value slotd 'class)))
79 (let ((table (or (gethash name *name->class->slotd-table*)
80 (setf (gethash name *name->class->slotd-table*)
81 (make-hash-table :test 'eq :size 5)))))
82 (setf (gethash class table) slotd))
83 (dolist (type '(reader writer boundp))
84 (let* ((gf-name (ecase type
85 (reader 'slot-value-using-class)
86 (writer '(setf slot-value-using-class))
87 (boundp 'slot-boundp-using-class)))
88 (gf (gdefinition gf-name)))
89 (compute-slot-accessor-info slotd type gf)))
90 (initialize-internal-slot-gfs name)))
91
92 ;;;
93 ;;; Compute an effective method for SLOT-VALUE-USING-CLASS, (SETF
94 ;;; SLOT-VALUE-USING-CLASS) or SLOT-BOUNDP-USING-CLASS for reading/
95 ;;; writing/testing effective slot SLOTD.
96 ;;;
97 ;;; TYPE is one of the symbols READER, WRITER or BOUNDP, depending on
98 ;;; GF. Store the effective method in the effective slot definition
99 ;;; object itself; these GFs have special dispatch functions calling
100 ;;; effective methods directly retrieved from effective slot
101 ;;; definition objects, as an optimization.
102 ;;;
103 ;;; FIXME: Change the function name to COMPUTE-SVUC-SLOTD-FUNCTION,
104 ;;; or some such.
105 ;;;
106 (defmethod compute-slot-accessor-info ((slotd effective-slot-definition)
107 type gf)
108 (let* ((name (slot-value slotd 'name))
109 (class (slot-value slotd 'class))
110 (old-slotd (find-slot-definition class name))
111 (old-std-p (and old-slotd (slot-accessor-std-p old-slotd 'all))))
112 (multiple-value-bind (function std-p)
113 (if (eq *boot-state* 'complete)
114 (get-accessor-method-function gf type class slotd)
115 (get-optimized-std-accessor-method-function class slotd type))
116 (setf (slot-accessor-std-p slotd type) std-p)
117 (setf (slot-accessor-function slotd type) function))
118 (when (and old-slotd
119 (not (eq old-std-p (slot-accessor-std-p slotd 'all))))
120 (record-pv-update-info slotd))))
121
122 (defmethod slot-definition-allocation ((slotd structure-slot-definition))
123 :instance)
124
125
126
127 (defmethod shared-initialize :after ((object documentation-mixin)
128 slot-names
129 &key (documentation nil documentation-p))
130 (declare (ignore slot-names))
131 (when documentation-p
132 (setf (documentation object nil) documentation)))
133
134 (defmethod documentation (object doc-type)
135 (declare (ignore object doc-type))
136 nil)
137
138 (defmethod (setf documentation) (new-value object doc-type)
139 (declare (ignore new-value doc-type))
140 (error "~@<Can't change the documentation of ~S.~@:>" object))
141
142 (defmethod documentation ((object documentation-mixin) doc-type)
143 (declare (ignore doc-type))
144 (plist-value object 'documentation))
145
146 (defmethod (setf documentation) (new-value (object documentation-mixin) doc-type)
147 (declare (ignore doc-type))
148 (setf (plist-value object 'documentation) new-value))
149
150 (defmethod documentation ((slotd standard-slot-definition) doc-type)
151 (declare (ignore doc-type))
152 (slot-value slotd 'documentation))
153
154 (defmethod (setf documentation) (new-value (slotd standard-slot-definition) doc-type)
155 (declare (ignore doc-type))
156 (setf (slot-value slotd 'documentation) new-value))
157
158 (defmethod (setf documentation) (doc (gf generic-function) type)
159 (declare (ignore type))
160 (setf (ext:info function documentation (generic-function-name gf))
161 doc))
162
163
164 ;;;
165 ;;; Various class accessors that are a little more complicated than can be
166 ;;; done with automatically generated reader methods.
167 ;;;
168 (defmethod class-prototype ((class std-class))
169 (with-slots (prototype) class
170 (or prototype (setq prototype (allocate-instance class)))))
171
172 (defmethod class-prototype ((class structure-class))
173 (with-slots (prototype wrapper defstruct-constructor) class
174 (or prototype
175 (setq prototype
176 (if defstruct-constructor
177 (allocate-instance class)
178 (allocate-standard-instance wrapper))))))
179
180 (defmethod class-direct-default-initargs ((class slot-class))
181 (plist-value class 'direct-default-initargs))
182
183 (defmethod class-default-initargs ((class slot-class))
184 (plist-value class 'default-initargs))
185
186 (defmethod class-constructors ((class slot-class))
187 (plist-value class 'constructors))
188
189 (defmethod class-slot-cells ((class std-class))
190 (plist-value class 'class-slot-cells))
191
192
193 ;;;
194 ;;; Class accessors that are even a little bit more complicated than those
195 ;;; above. These have a protocol for updating them, we must implement that
196 ;;; protocol.
197 ;;;
198
199 ;;;
200 ;;; Maintaining the direct subclasses backpointers. The update methods are
201 ;;; here, the values are read by an automatically generated reader method.
202 ;;;
203 (defmethod add-direct-subclass ((class class) (subclass class))
204 (check-seals class 'add-direct-subclass)
205 (with-slots (direct-subclasses) class
206 (pushnew subclass direct-subclasses)
207 subclass))
208
209 (defmethod remove-direct-subclass ((class class) (subclass class))
210 (check-seals class 'remove-direct-subclass)
211 (with-slots (direct-subclasses) class
212 (setq direct-subclasses (remove subclass direct-subclasses))
213 subclass))
214
215 ;;;
216 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
217 ;;;
218 ;;; There are four generic functions involved, each has one method for the
219 ;;; class case and another method for the damned EQL specializers. All of
220 ;;; these are specified methods and appear in their specified place in the
221 ;;; class graph.
222 ;;;
223 ;;; ADD-DIRECT-METHOD
224 ;;; REMOVE-DIRECT-METHOD
225 ;;; SPECIALIZER-DIRECT-METHODS
226 ;;; SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
227 ;;;
228 ;;; In each case, we maintain one value which is a cons. The car is the list
229 ;;; methods. The cdr is a list of the generic functions. The cdr is always
230 ;;; computed lazily.
231 ;;;
232
233 (defmethod add-direct-method ((specializer class) (method method))
234 (with-slots (direct-methods) specializer
235 (setf (car direct-methods) (adjoin method (car direct-methods)) ;PUSH
236 (cdr direct-methods) ()))
237 method)
238
239 (defmethod remove-direct-method ((specializer class) (method method))
240 (with-slots (direct-methods) specializer
241 (setf (car direct-methods) (remove method (car direct-methods))
242 (cdr direct-methods) ()))
243 (remove-inline-access-method specializer method)
244 method)
245
246 (defmethod specializer-direct-methods ((specializer class))
247 (with-slots (direct-methods) specializer
248 (car direct-methods)))
249
250 (defmethod specializer-direct-generic-functions ((specializer class))
251 (with-slots (direct-methods) specializer
252 (or (cdr direct-methods)
253 (setf (cdr direct-methods)
254 (loop for method in (car direct-methods)
255 for generic-function = (method-generic-function method)
256 unless (member generic-function collected :test #'eq)
257 collect generic-function into collected
258 finally (return collected))))))
259
260
261 ;;;
262 ;;; This hash table is used to store the direct methods and direct generic
263 ;;; functions of EQL specializers. Each value in the table is the cons.
264 ;;;
265 (defvar *eql-specializer-methods* (make-hash-table :test 'eql))
266 (defvar *class-eq-specializer-methods* (make-hash-table :test 'eq))
267
268 (defmethod specializer-method-table ((specializer eql-specializer))
269 *eql-specializer-methods*)
270
271 (defmethod specializer-method-table ((specializer class-eq-specializer))
272 *class-eq-specializer-methods*)
273
274 (defmethod add-direct-method ((specializer specializer-with-object) (method method))
275 (let* ((object (specializer-object specializer))
276 (table (specializer-method-table specializer))
277 (entry (gethash object table)))
278 (unless entry
279 (setq entry
280 (setf (gethash object table)
281 (cons nil nil))))
282 (setf (car entry) (adjoin method (car entry))
283 (cdr entry) ())
284 method))
285
286 (defmethod remove-direct-method ((specializer specializer-with-object) (method method))
287 (let* ((object (specializer-object specializer))
288 (entry (gethash object (specializer-method-table specializer))))
289 (when entry
290 (setf (car entry) (remove method (car entry))
291 (cdr entry) ()))
292 method))
293
294 (defmethod specializer-direct-methods ((specializer specializer-with-object))
295 (car (gethash (specializer-object specializer)
296 (specializer-method-table specializer))))
297
298 (defmethod specializer-direct-generic-functions ((specializer specializer-with-object))
299 (let* ((object (specializer-object specializer))
300 (entry (gethash object (specializer-method-table specializer))))
301 (when entry
302 (or (cdr entry)
303 (setf (cdr entry)
304 (loop for method in (car entry)
305 for generic-function = (method-generic-function method)
306 unless (member generic-function collected :test #'eq)
307 collect generic-function into collected
308 finally (return collected)))))))
309
310 (defun map-specializers (function)
311 (map-all-classes (lambda (class)
312 (funcall function (class-eq-specializer class))
313 (funcall function class)))
314 (maphash (lambda (object methods)
315 (declare (ignore methods))
316 (intern-eql-specializer object))
317 *eql-specializer-methods*)
318 (maphash (lambda (object specl)
319 (declare (ignore object))
320 (funcall function specl))
321 *eql-specializer-table*)
322 nil)
323
324 (defun map-all-classes (function &optional (root t))
325 (let ((braid-p (memq *boot-state* '(braid complete)))
326 (root (if (symbolp root) (find-class root) root)))
327 (labels ((map-class (class)
328 (mapc #'map-class
329 (if braid-p
330 (class-direct-subclasses class)
331 (early-class-direct-subclasses class)))
332 (funcall function class)))
333 (map-class root))))
334
335 (defun map-all-generic-functions (function)
336 (let ((all-generic-functions (make-hash-table :test 'eq)))
337 (map-specializers (lambda (specl)
338 (dolist (gf (specializer-direct-generic-functions specl))
339 (unless (gethash gf all-generic-functions)
340 (setf (gethash gf all-generic-functions) t)
341 (funcall function gf))))))
342 nil)
343
344 (defmethod shared-initialize :after ((specl class-eq-specializer) slot-names &key)
345 (declare (ignore slot-names))
346 (setf (slot-value specl 'type) `(class-eq ,(specializer-class specl))))
347
348 (defmethod shared-initialize :after ((specl eql-specializer) slot-names &key)
349 (declare (ignore slot-names))
350 (setf (slot-value specl 'type) `(eql ,(specializer-object specl))))
351
352
353
354 (defun real-load-defclass (name metaclass-name supers slots other)
355 (apply #'ensure-class name :metaclass metaclass-name
356 :direct-superclasses supers
357 :direct-slots slots
358 :definition-source `((defclass ,name) ,*load-pathname*)
359 other))
360
361 (setf (gdefinition 'load-defclass) #'real-load-defclass)
362
363 (defun ensure-class (name &rest all)
364 (apply #'ensure-class-using-class (find-class name nil) name all))
365
366 (defmethod ensure-class-using-class ((class null) name &rest args &key)
367 (multiple-value-bind (meta initargs)
368 (ensure-class-values class args)
369 (inform-type-system-about-class (class-prototype meta) name)
370 (setf class (apply #'make-instance meta :name name initargs)
371 (find-class name) class)
372 (inform-type-system-about-class class name)
373 class))
374
375 (defmethod ensure-class-using-class ((class pcl-class) name &rest args &key)
376 (multiple-value-bind (meta initargs)
377 (ensure-class-values class args)
378 (unless (eq (class-of class) meta)
379 (apply #'change-class class meta initargs))
380 (apply #'reinitialize-instance class initargs)
381 (setf (find-class name) class)
382 (inform-type-system-about-class class name)
383 class))
384
385 (defmethod class-predicate-name ((class t))
386 'function-returning-nil)
387
388 (defun ensure-class-values (class args)
389 (let* ((initargs (copy-list args))
390 (unsupplied (list 1))
391 (supplied-meta (getf initargs :metaclass unsupplied))
392 (supplied-supers (getf initargs :direct-superclasses unsupplied))
393 (meta (cond ((neq supplied-meta unsupplied)
394 (find-class supplied-meta))
395 ((or (null class)
396 (forward-referenced-class-p class))
397 *the-class-standard-class*)
398 (t
399 (class-of class)))))
400 (flet ((fix-super (super)
401 (cond ((classp super)
402 super)
403 ((legal-class-name-p super)
404 (or (find-class super nil)
405 (make-instance 'forward-referenced-class
406 :name super)))
407 (t
408 (simple-program-error
409 "~@<~S is not a class or a legal class name.~@:>"
410 super)))))
411 ;;
412 ;; CLHS: signal PROGRAM-ERROR, if
413 ;; (a) there are any duplicate slot names
414 ;; (b) any of the slot options :ALLOCATION, :INITFORM, :TYPE, or
415 ;; :DOCUMENTATION appears more than one in a single slot description.
416 (loop for (slot . more) on (getf initargs :direct-slots)
417 for slot-name = (getf slot :name)
418 if (some (lambda (s) (eq slot-name (getf s :name))) more) do
419 (simple-program-error
420 "~@<More than one direct slot with name ~S.~@:>"
421 slot-name)
422 else do
423 (loop for (option value . more) on slot by #'cddr
424 if (and (member option '(:allocation :type :initform
425 :documentation))
426 (not (eq unsupplied
427 (getf more option unsupplied)))) do
428 (simple-program-error
429 "~@<Duplicate slot option ~S for slot ~S.~@:>"
430 option slot-name)
431 else if (and (eq option :readers)
432 (notevery #'symbolp value)) do
433 (simple-program-error
434 "~@<Slot ~S: slot reader names must be symbols.~@:>"
435 slot-name)
436 else if (and (eq option :initargs)
437 (notevery #'symbolp value)) do
438 (simple-program-error
439 "~@<Slot ~S: initarg names must be symbols.~@:>"
440 slot-name)))
441 ;;
442 ;; CLHS: signal PROGRAM-ERROR, if an initialization argument name
443 ;; appears more than once in :DEFAULT-INITARGS class option.
444 (loop for (initarg . more) on (getf initargs :direct-default-initargs)
445 for name = (car initarg)
446 when (some (lambda (a) (eq (car a) name)) more) do
447 (simple-program-error
448 "~@<Duplicate initialization argument ~
449 name ~S in ~s of class ~A.~@:>"
450 name :default-initargs class))
451 ;;
452 (loop for (arg value) on initargs by #'cddr
453 count (eq arg :metaclass) into metaclass
454 count (eq arg :direct-default-initargs) into default-initargs
455 when (or (> metaclass 1) (> default-initargs 1)) do
456 (simple-program-error
457 "~@<Class ~S: More than one ~S specified~@:>"
458 class (if (eq arg :direct-default-initargs)
459 :default-initargs arg)))
460 (remf initargs :metaclass)
461 (remf initargs :direct-superclasses)
462 ;;
463 (values meta
464 (nconc (when (neq supplied-supers unsupplied)
465 (list :direct-superclasses
466 (mapcar #'fix-super supplied-supers)))
467 initargs)))))
468
469
470 ;;;
471 ;;;
472 ;;;
473 (defmethod shared-initialize :after
474 ((class std-class)
475 slot-names
476 &key (direct-superclasses nil direct-superclasses-p)
477 (direct-slots nil direct-slots-p)
478 (direct-default-initargs nil direct-default-initargs-p)
479 (predicate-name nil predicate-name-p))
480 (declare (ignore slot-names))
481 (cond ((or direct-superclasses-p
482 (null (slot-value class 'direct-superclasses)))
483 (setq direct-superclasses
484 (or direct-superclasses
485 (list (if (funcallable-standard-class-p class)
486 *the-class-funcallable-standard-object*
487 *the-class-standard-object*))))
488 (dolist (superclass direct-superclasses)
489 (unless (validate-superclass class superclass)
490 (error "~@<The class ~S was specified as a ~
491 super-class of the class ~S, ~
492 but the meta-classes ~S and ~S are incompatible. ~
493 Define a method for ~S to avoid this error.~@:>"
494 superclass class (class-of superclass) (class-of class)
495 'validate-superclass)))
496 (setf (slot-value class 'direct-superclasses) direct-superclasses))
497 (t
498 (setq direct-superclasses (slot-value class 'direct-superclasses))))
499 (setq direct-slots
500 (if direct-slots-p
501 (setf (slot-value class 'direct-slots)
502 (mapcar (lambda (pl) (make-direct-slotd class pl))
503 direct-slots))
504 (slot-value class 'direct-slots)))
505 (if direct-default-initargs-p
506 (setf (plist-value class 'direct-default-initargs)
507 direct-default-initargs)
508 (setq direct-default-initargs
509 (plist-value class 'direct-default-initargs)))
510 (setf (plist-value class 'class-slot-cells)
511 (let ((collected ()))
512 (dolist (dslotd direct-slots (nreverse collected))
513 (when (eq (slot-definition-allocation dslotd) :class)
514 (let ((initfunction (slot-definition-initfunction dslotd)))
515 (push (cons (slot-definition-name dslotd)
516 (if initfunction
517 (funcall initfunction)
518 +slot-unbound+))
519 collected))))))
520 (setq predicate-name (if predicate-name-p
521 (setf (slot-value class 'predicate-name)
522 (car predicate-name))
523 (or (slot-value class 'predicate-name)
524 (setf (slot-value class 'predicate-name)
525 (make-class-predicate-name (class-name class))))))
526 (add-direct-subclasses class direct-superclasses)
527 (make-class-predicate class predicate-name)
528 (update-class class nil)
529 (add-slot-accessors class direct-slots)
530 (make-preliminary-layout class))
531
532 (defmethod shared-initialize :after ((class forward-referenced-class)
533 slot-names &key &allow-other-keys)
534 (declare (ignore slot-names))
535 (make-preliminary-layout class))
536
537 ;;;
538 ;;; Give CLASS a preliminary layout, if it doesn't have a layout
539 ;;; already. This is done to make CLASS known to the type system
540 ;;; before the class is finalized, and is a consequence of the class
541 ;;; schizophrenia we are suffering from.
542 ;;;
543 (defvar *allow-forward-referenced-classes-in-cpl-p* nil)
544
545 (defun make-preliminary-layout (class)
546 (flet ((compute-preliminary-cpl (root)
547 (let ((*allow-forward-referenced-classes-in-cpl-p* t))
548 (compute-class-precedence-list root))))
549 (unless (class-finalized-p class)
550 (let ((name (class-name class)))
551 (setf (find-class name) class)
552 (inform-type-system-about-class class name)
553 (let ((layout (make-wrapper 0 class))
554 (kernel-class (kernel::find-class name)))
555 (setf (kernel:layout-class layout) kernel-class)
556 (setf (kernel:%class-pcl-class kernel-class) class)
557 (setf (slot-value class 'wrapper) layout)
558 (let ((cpl (compute-preliminary-cpl class)))
559 (setf (kernel:layout-inherits layout)
560 (kernel:order-layout-inherits
561 (map 'simple-vector #'class-wrapper
562 (reverse (rest cpl))))))
563 (kernel:register-layout layout :invalidate t)
564 (setf (kernel:%class-layout kernel-class) layout)
565 (mapc #'make-preliminary-layout (class-direct-subclasses class)))))))
566
567 (defmethod shared-initialize :before ((class class) slot-names &key name)
568 (declare (ignore slot-names name))
569 (setf (slot-value class 'type) `(class ,class))
570 (setf (slot-value class 'class-eq-specializer)
571 (make-instance 'class-eq-specializer :class class)))
572
573 (defmethod reinitialize-instance :before ((class slot-class)
574 &key direct-superclasses)
575 (remove-direct-subclasses class direct-superclasses)
576 (remove-slot-accessors class (class-direct-slots class)))
577
578 (defmethod reinitialize-instance :after ((class slot-class)
579 &rest initargs
580 &key)
581 (map-dependents class
582 (lambda (dependent)
583 (apply #'update-dependent class dependent initargs))))
584
585 (defmethod shared-initialize :after
586 ((class structure-class)
587 slot-names
588 &key (direct-superclasses nil direct-superclasses-p)
589 (direct-slots nil direct-slots-p)
590 direct-default-initargs
591 (predicate-name nil predicate-name-p))
592 (declare (ignore slot-names direct-default-initargs))
593 (if direct-superclasses-p
594 (setf (slot-value class 'direct-superclasses)
595 (or direct-superclasses
596 (setq direct-superclasses
597 (and (not (eq (class-name class) 'structure-object))
598 (list *the-class-structure-object*)))))
599 (setq direct-superclasses (slot-value class 'direct-superclasses)))
600 (let* ((name (class-name class))
601 (from-defclass-p (slot-value class 'from-defclass-p))
602 (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
603 (if direct-slots-p
604 (setf (slot-value class 'direct-slots)
605 (setq direct-slots
606 (mapcar (lambda (pl)
607 (when defstruct-p
608 (let* ((slot-name (getf pl :name))
609 (accessor
610 (symbolicate
611 *package*
612 (if (symbol-package name)
613 (package-name (symbol-package name))
614 "")
615 "::" name " structure class " slot-name)))
616 (setq pl (list* :defstruct-accessor-symbol accessor
617 pl))))
618 (make-direct-slotd class pl))
619 direct-slots)))
620 (setq direct-slots (slot-value class 'direct-slots)))
621 (when defstruct-p
622 (let* ((include (car (slot-value class 'direct-superclasses)))
623 (conc-name (symbolicate *package*
624 (if (symbol-package name)
625 (package-name (symbol-package name))
626 "")
627 "::" name " structure class "))
628 ;;
629 ;; It's not possible to use a generalized name for the
630 ;; constructor function. It shouldn't matter though, I think,
631 ;; like for the slot names above, because this stuff is not
632 ;; supposed to be used by users directly.
633 (constructor
634 (symbolicate *package* conc-name " constructor"))
635 (defstruct `(defstruct (,name
636 ,@(when include
637 `((:include ,(class-name include))))
638 (:predicate nil)
639 (:conc-name ,conc-name)
640 (:constructor ,constructor ()))
641 ;;
642 ;; Use a temporary unbound marker that lets
643 ;; SHARED-INITIALIZE recognize if a before-method
644 ;; has written to a slot.
645 ,@(mapcar (lambda (slot)
646 `(,(slot-definition-name slot)
647 '.unbound.))
648 direct-slots)))
649 (reader-names (mapcar (lambda (slotd)
650 (list 'slot-accessor name
651 (slot-definition-name slotd)
652 'reader))
653 direct-slots))
654 (writer-names (mapcar (lambda (slotd)
655 (list 'slot-accessor name
656 (slot-definition-name slotd)
657 'writer))
658 direct-slots))
659 (readers-init
660 (mapcar (lambda (slotd reader-name)
661 (let ((accessor
662 (slot-definition-defstruct-accessor-symbol slotd)))
663 `(defun ,reader-name (obj)
664 (declare (type ,name obj))
665 (,accessor obj))))
666 direct-slots reader-names))
667 (writers-init
668 (mapcar (lambda (slotd writer-name)
669 (let ((accessor
670 (slot-definition-defstruct-accessor-symbol slotd)))
671 `(defun ,writer-name (nv obj)
672 (declare (type ,name obj))
673 (setf (,accessor obj) nv))))
674 direct-slots writer-names))
675 (defstruct-form
676 `(progn
677 ,defstruct
678 ,@readers-init ,@writers-init)))
679 (unless (structure-type-p name) (eval defstruct-form))
680 (mapc (lambda (dslotd reader-name writer-name)
681 (let* ((reader (when (fboundp reader-name)
682 (gdefinition reader-name)))
683 (writer (when (fboundp writer-name)
684 (gdefinition writer-name))))
685 (setf (slot-value dslotd 'internal-reader-function) reader)
686 (setf (slot-value dslotd 'internal-writer-function) writer)))
687 direct-slots reader-names writer-names)
688 (setf (slot-value class 'defstruct-form) defstruct-form)
689 (setf (slot-value class 'defstruct-constructor) constructor))))
690 (add-direct-subclasses class direct-superclasses)
691 (setf (slot-value class 'class-precedence-list)
692 (compute-class-precedence-list class))
693 (setf (slot-value class 'slots) (compute-slots class))
694 (let ((lclass (kernel::find-class (class-name class))))
695 (setf (kernel:%class-pcl-class lclass) class)
696 (setf (slot-value class 'wrapper) (kernel:%class-layout lclass)))
697 (setf (slot-value class 'finalized-p) t)
698 (update-pv-table-cache-info class)
699 (setq predicate-name (if predicate-name-p
700 (setf (slot-value class 'predicate-name)
701 (car predicate-name))
702 (or (slot-value class 'predicate-name)
703 (setf (slot-value class 'predicate-name)
704 (make-class-predicate-name (class-name class))))))
705 (make-class-predicate class predicate-name)
706 (add-slot-accessors class direct-slots))
707
708 (defmethod direct-slot-definition-class ((class structure-class) initargs)
709 (declare (ignore initargs))
710 (find-class 'structure-direct-slot-definition))
711
712 (defmethod finalize-inheritance ((class structure-class))
713 nil) ; always finalized
714
715 (defun add-slot-accessors (class dslotds)
716 (fix-slot-accessors class dslotds 'add))
717
718 (defun remove-slot-accessors (class dslotds)
719 (fix-slot-accessors class dslotds 'remove))
720
721 (defun fix-slot-accessors (class dslotds add/remove)
722 (flet ((fix (gfspec name r/w)
723 (let ((gf (ensure-generic-function gfspec)))
724 (case r/w
725 (r (if (eq add/remove 'add)
726 (add-reader-method class gf name)
727 (remove-reader-method class gf)))
728 (w (if (eq add/remove 'add)
729 (add-writer-method class gf name)
730 (remove-writer-method class gf)))))))
731 (dolist (dslotd dslotds)
732 (let ((slot-name (slot-definition-name dslotd)))
733 (dolist (r (slot-definition-readers dslotd))
734 (fix r slot-name 'r))
735 (dolist (w (slot-definition-writers dslotd))
736 (fix w slot-name 'w))))))
737
738
739 (defun add-direct-subclasses (class new)
740 (dolist (n new)
741 (unless (memq class (class-direct-subclasses class))
742 (add-direct-subclass n class))))
743
744 (defun remove-direct-subclasses (class new)
745 (let ((old (class-direct-superclasses class)))
746 (dolist (o (set-difference old new))
747 (remove-direct-subclass o class))))
748
749
750 ;;;
751 ;;;
752 ;;;
753 (defmethod finalize-inheritance ((class std-class))
754 (update-class class t))
755
756 (defmethod finalize-inheritance ((class forward-referenced-class))
757 (simple-program-error
758 "~@<Forward-referenced classes cannot be finalized: ~A.~@:>"
759 class))
760
761
762 ;;;
763 ;;; Called by :after shared-initialize whenever a class is initialized or
764 ;;; reinitialized. The class may or may not be finalized.
765 ;;;
766 (defun update-class (class finalizep)
767 ;;
768 ;; Calling UPDATE-SLOTS below sets the class wrapper of CLASS, which
769 ;; makes the class finalized. When UPDATE-CLASS isn't called from
770 ;; FINALIZE-INHERITANCE, make sure that this finalization invokes
771 ;; FINALIZE-INHERITANCE as per AMOP. Note that we can't simply
772 ;; delay the finalization when CLASS has no forward referenced
773 ;; superclasses because that causes bootstrap problems.
774 (when (and (not (or finalizep (class-finalized-p class)))
775 (not (class-has-a-forward-referenced-superclass-p class)))
776 (finalize-inheritance class)
777 (return-from update-class))
778 ;;
779 (when (or finalizep
780 (class-finalized-p class)
781 (not (class-has-a-forward-referenced-superclass-p class)))
782 (setf (find-class (class-name class)) class)
783 (update-cpl class (compute-class-precedence-list class))
784 (update-slots class (compute-slots class))
785 (update-gfs-of-class class)
786 (update-inits class (compute-default-initargs class))
787 (update-ctors 'finalize-inheritance :class class))
788 ;;
789 (unless finalizep
790 (dolist (sub (class-direct-subclasses class))
791 (update-class sub nil))))
792
793 (defun update-cpl (class cpl)
794 (if (class-finalized-p class)
795 (unless (equal (class-precedence-list class) cpl)
796 ;; Need to have the cpl setup before update-lisp-class-layout
797 ;; is called on CMUCL.
798 (setf (slot-value class 'class-precedence-list) cpl)
799 (force-cache-flushes class))
800 (setf (slot-value class 'class-precedence-list) cpl))
801 (update-class-can-precede-p cpl))
802
803 (defun update-class-can-precede-p (cpl)
804 (loop for (class . rest) on cpl do
805 (with-slots (can-precede-list) class
806 (setq can-precede-list
807 (union rest can-precede-list :test #'eq)))))
808
809 (defun class-can-precede-p (class1 class2)
810 (member class2 (class-can-precede-list class1)))
811
812 (defun update-slots (class eslotds)
813 (ext:collect ((instance-slots) (class-slots))
814 (dolist (eslotd eslotds)
815 (ecase (slot-definition-allocation eslotd)
816 (:instance (instance-slots eslotd))
817 (:class (class-slots eslotd))))
818 ;;
819 ;; If there is a change in the shape of the instances then the
820 ;; old class is now obsolete.
821 (let* ((nlayout (mapcar #'slot-definition-name
822 (sort (instance-slots) #'<
823 :key #'slot-definition-location)))
824 (nslots (length nlayout))
825 (nwrapper-class-slots (compute-class-slots (class-slots)))
826 (owrapper (when (class-finalized-p class)
827 (class-wrapper class)))
828 (olayout (when owrapper
829 (wrapper-instance-slots-layout owrapper)))
830 (nwrapper
831 (cond ((null owrapper)
832 (make-wrapper nslots class))
833 ;;
834 ;; We cannot reuse the old wrapper easily when it
835 ;; has class slot cells, even if these cells are
836 ;; EQUAL to the ones used in the new wrapper. The
837 ;; class slot cells of OWRAPPER may be referenced
838 ;; from caches, and if we don't change the wrapper,
839 ;; the caches won't notice that something has
840 ;; changed. We could do something here manually,
841 ;; but I don't think it's worth it.
842 ((and (equal nlayout olayout)
843 (null (wrapper-class-slots owrapper)))
844 owrapper)
845 (t
846 ;;
847 ;; This will initialize the new wrapper to have the same
848 ;; state as the old wrapper. We will then have to change
849 ;; that. This may seem like wasted work (it is), but the
850 ;; spec requires that we call make-instances-obsolete.
851 (make-instances-obsolete class)
852 (class-wrapper class)))))
853
854 (with-slots (wrapper slots finalized-p) class
855 (update-lisp-class-layout class nwrapper)
856 (setf slots eslotds
857 (wrapper-instance-slots-layout nwrapper) nlayout
858 (wrapper-class-slots nwrapper) nwrapper-class-slots
859 (wrapper-no-of-instance-slots nwrapper) nslots
860 wrapper nwrapper
861 finalized-p t))
862
863 (unless (eq owrapper nwrapper)
864 (update-inline-access class)
865 (update-pv-table-cache-info class)
866 (maybe-update-standard-class-locations class)))))
867
868 (defun compute-class-slots (eslotds)
869 (loop for eslotd in eslotds
870 for name = (slot-definition-name eslotd)
871 and class = (slot-definition-class eslotd)
872 collect (assoc name (class-slot-cells class))))
873
874 (defun update-gfs-of-class (class)
875 (when (and (class-finalized-p class)
876 (let ((cpl (class-precedence-list class)))
877 (or (member *the-class-slot-class* cpl)
878 (member *the-class-standard-effective-slot-definition* cpl))))
879 (let ((gf-table (make-hash-table :test 'eq)))
880 (labels ((collect-gfs (class)
881 (dolist (gf (specializer-direct-generic-functions class))
882 (setf (gethash gf gf-table) t))
883 (mapc #'collect-gfs (class-direct-superclasses class))))
884 (collect-gfs class)
885 (maphash (lambda (gf ignore)
886 (declare (ignore ignore))
887 (update-gf-dfun class gf))
888 gf-table)))))
889
890 (defun update-inits (class inits)
891 (setf (plist-value class 'default-initargs) inits))
892
893
894 ;;;
895 ;;;
896 ;;;
897 (defmethod compute-default-initargs ((class slot-class))
898 (let ((initargs (loop for c in (class-precedence-list class)
899 append (class-direct-default-initargs c))))
900 (delete-duplicates initargs :test #'eq :key #'car :from-end t)))
901
902 ;;;
903 ;;; Protocols for constructing direct and effective slot definitions.
904 ;;;
905 ;;;
906 ;;;
907 ;;;
908 (defmethod direct-slot-definition-class ((class std-class) initargs)
909 (declare (ignore initargs))
910 (find-class 'standard-direct-slot-definition))
911
912 (defun make-direct-slotd (class initargs)
913 (let ((initargs (list* :class class initargs)))
914 (apply #'make-instance (direct-slot-definition-class class initargs)
915 initargs)))
916
917 ;;;
918 ;;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
919 ;;; for each different slot name we find in our superclasses. Each
920 ;;; call receives the class and a list of the dslotds with that name.
921 ;;; The list is in most-specific-first order.
922 ;;;
923 (defmethod compute-slots ((class std-class))
924 (loop with names/slots = ()
925 for c in (class-precedence-list class) do
926 (loop for slot in (class-direct-slots c)
927 as name = (slot-definition-name slot)
928 as entry = (assq name names/slots) do
929 (if entry
930 (push slot (cdr entry))
931 (push (list name slot) names/slots)))
932 finally
933 (return
934 (loop for (name . slots) in names/slots collect
935 (compute-effective-slot-definition
936 class name (nreverse slots))))))
937
938 ;;;
939 ;;; These are the specified AMOP methods.
940 ;;;
941
942 (defmethod compute-slots ((class standard-class))
943 (call-next-method))
944
945 (defmethod compute-slots :around ((class standard-class))
946 (loop with slotds = (call-next-method) and location = -1
947 for slot in slotds do
948 (setf (slot-definition-location slot)
949 (ecase (slot-definition-allocation slot)
950 (:instance
951 (incf location))
952 (:class
953 (let* ((name (slot-definition-name slot))
954 (from-class (slot-definition-allocation-class slot))
955 (cell (assq name (class-slot-cells from-class))))
956 (assert (consp cell))
957 cell))))
958 (initialize-internal-slot-functions slot)
959 finally
960 (return slotds)))
961
962 (defmethod compute-slots ((class funcallable-standard-class))
963 (call-next-method))
964
965 (defmethod compute-slots :around ((class funcallable-standard-class))
966 (labels (;;
967 ;; Return a list of the names of instance slots in SLOTDS.
968 (instance-slot-names (slotds)
969 (loop for e in slotds
970 when (eq (slot-definition-allocation e) :instance)
971 collect (slot-definition-name e)))
972 ;;
973 ;; This sorts slots so that slots of classes later in the CPL
974 ;; come before slots of other classes. This is crucial for
975 ;; funcallable instances because it ensures that the slots of
976 ;; FUNCALLABLE-STANDARD-OBJECT, which includes the slots of
977 ;; KERNEL:FUNCALLABLE-INSTANCE, come first, which in turn
978 ;; makes it possible to treat FUNCALLABLE-STANDARD-OBJECT as
979 ;; a funcallable instance.
980 (compute-layout (eslotds)
981 (loop with first = ()
982 with names = (instance-slot-names eslotds)
983 for class in (reverse (class-precedence-list class)) do
984 (loop for ss in (class-slots class)
985 as name = (slot-definition-name ss)
986 when (member name names) do
987 (push name first)
988 (setq names (delete name names)))
989 finally (return (nreverse (nconc names first))))))
990 ;;
991 (let ((all-slotds (call-next-method))
992 (instance-slots ())
993 (class-slots ()))
994 (loop for slotd in all-slotds do
995 (ecase (slot-definition-allocation slotd)
996 (:instance (push slotd instance-slots))
997 (:class (push slotd class-slots))))
998 (loop with layout = (compute-layout instance-slots)
999 for slotd in instance-slots do
1000 (setf (slot-definition-location slotd)
1001 (position (slot-definition-name slotd) layout))
1002 (initialize-internal-slot-functions slotd))
1003 (loop for slotd in class-slots
1004 as name = (slot-definition-name slotd)
1005 as from-class = (slot-definition-allocation-class slotd) do
1006 (setf (slot-definition-location slotd)
1007 (assoc name (class-slot-cells from-class)))
1008 (assert (consp (slot-definition-location slotd)))
1009 (initialize-internal-slot-functions slotd))
1010 all-slotds)))
1011
1012 (defmethod compute-slots ((class structure-class))
1013 (mapcan (lambda (superclass)
1014 (mapcar (lambda (dslotd)
1015 (compute-effective-slot-definition
1016 class (slot-definition-name dslotd) (list dslotd)))
1017 (class-direct-slots superclass)))
1018 (reverse (slot-value class 'class-precedence-list))))
1019
1020 (defmethod compute-slots :around ((class structure-class))
1021 (let ((eslotds (call-next-method)))
1022 (mapc #'initialize-internal-slot-functions eslotds)
1023 eslotds))
1024
1025 (defmethod compute-effective-slot-definition
1026 ((class slot-class) slot-name dslotds)
1027 (declare (ignore slot-name))
1028 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
1029 (class (effective-slot-definition-class class initargs)))
1030 (apply #'make-instance class initargs)))
1031
1032 (defmethod effective-slot-definition-class ((class std-class) initargs)
1033 (declare (ignore initargs))
1034 (find-class 'standard-effective-slot-definition))
1035
1036 (defmethod effective-slot-definition-class ((class structure-class) initargs)
1037 (declare (ignore initargs))
1038 (find-class 'structure-effective-slot-definition))
1039
1040 (defmethod compute-effective-slot-definition-initargs
1041 ((class slot-class) direct-slotds)
1042 (let* ((name nil)
1043 (initfunction nil)
1044 (initform nil)
1045 (initargs nil)
1046 (allocation nil)
1047 (allocation-class nil)
1048 (type t)
1049 (namep nil)
1050 (initp nil)
1051 (allocp nil))
1052
1053 (dolist (slotd direct-slotds)
1054 (when slotd
1055 (unless namep
1056 (setq name (slot-definition-name slotd)
1057 namep t))
1058 (unless initp
1059 (when (slot-definition-initfunction slotd)
1060 (setq initform (slot-definition-initform slotd)
1061 initfunction (slot-definition-initfunction slotd)
1062 initp t)))
1063 (unless allocp
1064 (setq allocation (slot-definition-allocation slotd)
1065 allocation-class (slot-definition-class slotd)
1066 allocp t))
1067 (setq initargs (append (slot-definition-initargs slotd) initargs))
1068 (let ((slotd-type (slot-definition-type slotd)))
1069 (setq type (cond ((eq type t) slotd-type)
1070 ((*subtypep type slotd-type) type)
1071 (t `(and ,type ,slotd-type)))))))
1072 (list :name name
1073 :initform initform
1074 :initfunction initfunction
1075 :initargs initargs
1076 :allocation allocation
1077 :allocation-class allocation-class
1078 :type type
1079 :class class)))
1080
1081 (defmethod compute-effective-slot-definition-initargs :around
1082 ((class structure-class) direct-slotds)
1083 (let ((slotd (car direct-slotds)))
1084 (list* :defstruct-accessor-symbol (slot-definition-defstruct-accessor-symbol slotd)
1085 :internal-reader-function (slot-definition-internal-reader-function slotd)
1086 :internal-writer-function (slot-definition-internal-writer-function slotd)
1087 (call-next-method))))
1088
1089 ;;;
1090 ;;; NOTE: For bootstrapping considerations, these can't use make-instance
1091 ;;; to make the method object. They have to use make-a-method which
1092 ;;; is a specially bootstrapped mechanism for making standard methods.
1093 ;;;
1094 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
1095 (declare (ignore direct-slot initargs))
1096 (find-class 'standard-reader-method))
1097
1098 (defmethod add-reader-method ((class slot-class) generic-function slot-name)
1099 (add-method generic-function
1100 (make-a-method 'standard-reader-method
1101 ()
1102 (list (or (class-name class) 'object))
1103 (list class)
1104 (make-reader-method-function class slot-name)
1105 "automatically generated reader method"
1106 slot-name)))
1107
1108 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
1109 (declare (ignore direct-slot initargs))
1110 (find-class 'standard-writer-method))
1111
1112 (defmethod add-writer-method ((class slot-class) generic-function slot-name)
1113 (add-method generic-function
1114 (make-a-method 'standard-writer-method
1115 ()
1116 (list 'new-value (or (class-name class) 'object))
1117 (list *the-class-t* class)
1118 (make-writer-method-function class slot-name)
1119 "automatically generated writer method"
1120 slot-name)))
1121
1122 (defmethod add-boundp-method ((class slot-class) generic-function slot-name)
1123 (add-method generic-function
1124 (make-a-method 'standard-boundp-method
1125 ()
1126 (list (or (class-name class) 'object))
1127 (list class)
1128 (make-boundp-method-function class slot-name)
1129 "automatically generated boundp method"
1130 slot-name)))
1131
1132 (defmethod remove-reader-method ((class slot-class) generic-function)
1133 (let ((method (get-method generic-function () (list class) nil)))
1134 (when method
1135 (remove-method generic-function method))))
1136
1137 (defmethod remove-writer-method ((class slot-class) generic-function)
1138 (let ((method (get-method generic-function ()
1139 (list *the-class-t* class) nil)))
1140 (when method
1141 (remove-method generic-function method))))
1142
1143 (defmethod remove-boundp-method ((class slot-class) generic-function)
1144 (let ((method (get-method generic-function () (list class) nil)))
1145 (when method
1146 (remove-method generic-function method))))
1147
1148
1149 ;;;
1150 ;;; make-reader-method-function and make-write-method function are NOT part of
1151 ;;; the standard protocol. They are however useful, PCL makes uses makes use
1152 ;;; of them internally and documents them for PCL users.
1153 ;;;
1154 ;;; *** This needs work to make type testing by the writer functions which
1155 ;;; *** do type testing faster. The idea would be to have one constructor
1156 ;;; *** for each possible type test. In order to do this it would be nice
1157 ;;; *** to have help from inform-type-system-about-class and friends.
1158 ;;;
1159 ;;; *** There is a subtle bug here which is going to have to be fixed.
1160 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1161 ;;; *** have to give the optimize-slot-value method the user might have
1162 ;;; *** defined for this metclass a chance to run.
1163 ;;;
1164 (defmethod make-reader-method-function ((class slot-class) slot-name)
1165 (make-std-reader-method-function (class-name class) slot-name))
1166
1167 (defmethod make-writer-method-function ((class slot-class) slot-name)
1168 (make-std-writer-method-function (class-name class) slot-name))
1169
1170 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1171 (make-std-boundp-method-function (class-name class) slot-name))
1172
1173
1174 ;;;; inform-type-system-about-class
1175 ;;;
1176 ;;; This is NOT part of the standard protocol. It is an internal mechanism
1177 ;;; which PCL uses to *try* and tell the type system about class definitions.
1178 ;;; In a more fully integrated implementation of CLOS, the type system would
1179 ;;; know about class objects and class names in a more fundamental way and
1180 ;;; the mechanism used to inform the type system about new classes would be
1181 ;;; different.
1182 ;;;
1183 (defmethod inform-type-system-about-class ((class std-class) name)
1184 ;; Maybe add skeleton lisp:standard-class to avoid undefined-function
1185 ;; compiler warnings. Not otherwise needed in this implementation.
1186 (inform-type-system-about-std-class name)
1187 (set-class-translation class name))
1188
1189 (defmethod inform-type-system-about-class ((class funcallable-standard-class)
1190 name)
1191 (set-class-translation class name))
1192
1193 (defmethod inform-type-system-about-class ((class structure-class) name)
1194 (set-class-translation class name))
1195
1196 (defmethod inform-type-system-about-class ((class condition-class) name)
1197 (set-class-translation class name))
1198
1199 (defmethod inform-type-system-about-class ((class forward-referenced-class)
1200 name)
1201 (inform-type-system-about-std-class name)
1202 (set-class-translation class name))
1203
1204
1205 (defmethod compatible-meta-class-change-p (class proto-new-class)
1206 (eq (class-of class) (class-of proto-new-class)))
1207
1208 (defmethod validate-superclass ((class class) (new-super class))
1209 (or (eq new-super *the-class-t*)
1210 (eq (class-of class) (class-of new-super))))
1211
1212 (defmethod validate-superclass ((class standard-class) (new-super std-class))
1213 (let ((new-super-meta-class (class-of new-super)))
1214 (or (eq new-super-meta-class *the-class-std-class*)
1215 (eq (class-of class) new-super-meta-class))))
1216
1217
1218 ;;;
1219 ;;; Force the flushing of caches by creating a new wrapper for CLASS,
1220 ;;; if necessary.
1221 ;;;
1222 ;;; If the LAYOUT-INVALID slot of CLASS's wrapper is
1223 ;;;
1224 ;;; -- (:FLUSH <new wrapper>) or (:OBSOLETE <new wrapper>), there's
1225 ;;; nothing to do. The new wrapper has already been created.
1226 ;;;
1227 ;;; -- :INVALID, then it has been set to that value by a previous
1228 ;;; call to REGISTER-LAYOUT for a superclass S of CLASS; S's
1229 ;;; wrapper has been invalidated together with its subclasses. In
1230 ;;; this case, CLASS's caches must obviously be flushed, too, like
1231 ;;; S's. So, make a new wrapper for CLASS, and translate kernel's
1232 ;;; :INVALID to PCL (:FLUSH <new wrapper>). UPDATE-SLOTS can later
1233 ;;; decide if it wants to make this (:OBSOLETE ...).
1234 ;;;
1235 ;;; -- NIL, then the wrapper is still valid, in which case we do
1236 ;;; the same as for :INVALID, but for the obviously slightly
1237 ;;; different reason.
1238 ;;;
1239 (defun force-cache-flushes (class)
1240 (let* ((owrapper (class-wrapper class)))
1241 (when (or (not (invalid-wrapper-p owrapper))
1242 (eq :invalid (kernel:layout-invalid owrapper)))
1243 (let ((nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1244 class)))
1245 (setf (wrapper-instance-slots-layout nwrapper)
1246 (wrapper-instance-slots-layout owrapper))
1247 (setf (wrapper-class-slots nwrapper)
1248 (wrapper-class-slots owrapper))
1249 (with-pcl-lock
1250 (update-lisp-class-layout class nwrapper)
1251 (setf (slot-value class 'wrapper) nwrapper)
1252 (invalidate-wrapper owrapper :flush nwrapper))))))
1253
1254 (defun flush-cache-trap (owrapper nwrapper instance)
1255 (declare (ignore owrapper))
1256 (cond ((std-instance-p instance)
1257 (setf (std-instance-wrapper instance) nwrapper))
1258 ((fsc-instance-p instance)
1259 (setf (fsc-instance-wrapper instance) nwrapper))
1260 (t
1261 (internal-error "Internal error."))))
1262
1263
1264 ;;;
1265 ;;; make-instances-obsolete can be called by user code. It will cause the
1266 ;;; next access to the instance (as defined in 88-002R) to trap through the
1267 ;;; update-instance-for-redefined-class mechanism.
1268 ;;;
1269 (defmethod make-instances-obsolete ((class std-class))
1270 (let* ((owrapper (class-wrapper class))
1271 (nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1272 class)))
1273 (setf (wrapper-instance-slots-layout nwrapper)
1274 (wrapper-instance-slots-layout owrapper))
1275 (setf (wrapper-class-slots nwrapper)
1276 (wrapper-class-slots owrapper))
1277 (with-pcl-lock
1278 (update-lisp-class-layout class nwrapper)
1279 (setf (slot-value class 'wrapper) nwrapper)
1280 (invalidate-wrapper owrapper :obsolete nwrapper)
1281 class)))
1282
1283 (defmethod make-instances-obsolete ((class symbol))
1284 (make-instances-obsolete (find-class class)))
1285
1286
1287 ;;;
1288 ;;; obsolete-instance-trap is the internal trap that is called when we see
1289 ;;; an obsolete instance. The times when it is called are:
1290 ;;; - when the instance is involved in method lookup
1291 ;;; - when attempting to access a slot of an instance
1292 ;;;
1293 ;;; It is not called by class-of, wrapper-of, or any of the low-level instance
1294 ;;; access macros.
1295 ;;;
1296 ;;; Of course these times when it is called are an internal implementation
1297 ;;; detail of PCL and are not part of the documented description of when the
1298 ;;; obsolete instance update happens. The documented description is as it
1299 ;;; appears in 88-002R.
1300 ;;;
1301 ;;; This has to return the new wrapper, so it counts on all the methods on
1302 ;;; obsolete-instance-trap-internal to return the new wrapper. It also does
1303 ;;; a little internal error checking to make sure that the traps are only
1304 ;;; happening when they should, and that the trap methods are computing
1305 ;;; apropriate new wrappers.
1306 ;;;
1307
1308 ;;; obsolete-instance-trap might be called on structure instances
1309 ;;; after a structure is redefined. In most cases, obsolete-instance-trap
1310 ;;; will not be able to fix the old instance, so it must signal an
1311 ;;; error. The hard part of this is that the error system and debugger
1312 ;;; might cause obsolete-instance-trap to be called again, so in that
1313 ;;; case, we have to return some reasonable wrapper, instead.
1314
1315 (defvar *in-obsolete-instance-trap* nil)
1316
1317 (defvar *the-wrapper-of-structure-object*
1318 (class-wrapper (find-class 'structure-object)))
1319
1320 (define-condition obsolete-structure (error)
1321 ((datum :reader obsolete-structure-datum :initarg :datum))
1322 (:report
1323 (lambda (condition stream)
1324 ;; Don't try to print the structure, since it probably
1325 ;; won't work.
1326 (format stream "~@<Obsolete structure error in ~S ~
1327 for a structure of type ~S.~@:>"
1328 (conditions::condition-function-name condition)
1329 (type-of (obsolete-structure-datum condition))))))
1330
1331 (defun obsolete-instance-trap (owrapper nwrapper instance)
1332 (if (not (pcl-instance-p instance))
1333 (if *in-obsolete-instance-trap*
1334 *the-wrapper-of-structure-object*
1335 (let ((*in-obsolete-instance-trap* t))
1336 (error 'obsolete-structure :datum instance)))
1337 (let* ((class (wrapper-class* nwrapper))
1338 (copy (allocate-instance class)) ;??? allocate-instance ???
1339 (olayout (wrapper-instance-slots-layout owrapper))
1340 (nlayout (wrapper-instance-slots-layout nwrapper))
1341 (oslots (get-slots instance))
1342 (nslots (get-slots copy))
1343 (oclass-slots (wrapper-class-slots owrapper))
1344 (added ())
1345 (discarded ())
1346 (plist ()))
1347 ;; local --> local transfer
1348 ;; local --> shared discard
1349 ;; local --> -- discard
1350 ;; shared --> local transfer
1351 ;; shared --> shared discard
1352 ;; shared --> -- discard
1353 ;; -- --> local add
1354 ;; -- --> shared --
1355 ;;
1356 ;; Go through all the old local slots.
1357 ;;
1358 (loop for name in olayout and opos from 0
1359 as npos = (posq name nlayout)
1360 if npos do
1361 (setf (slot-ref nslots npos)
1362 (slot-ref oslots opos))
1363 else do
1364 (push name discarded)
1365 (unless (eq (slot-ref oslots opos) +slot-unbound+)
1366 (setf (getf plist name)
1367 (slot-ref oslots opos))))
1368 ;;
1369 ;; Go through all the old shared slots.
1370 ;;
1371 (loop for (name . val) in oclass-slots
1372 for npos = (posq name nlayout)
1373 if npos do
1374 (setf (slot-ref nslots npos) val)
1375 else do
1376 (push name discarded)
1377 (unless (eq val +slot-unbound+)
1378 (setf (getf plist name) val)))
1379 ;;
1380 ;; Go through all the new local slots to compute the added slots.
1381 ;;
1382 (dolist (nlocal nlayout)
1383 (unless (or (memq nlocal olayout)
1384 (assq nlocal oclass-slots))
1385 (push nlocal added)))
1386
1387 (swap-wrappers-and-slots instance copy)
1388
1389 (update-instance-for-redefined-class instance
1390 added
1391 discarded
1392 plist)
1393 nwrapper)))
1394
1395
1396 ;;;
1397 ;;;
1398 ;;;
1399 (defun change-class-internal (instance new-class initargs)
1400 (let* ((old-class (class-of instance))
1401 (copy (allocate-instance new-class))
1402 (new-wrapper (get-wrapper copy))
1403 (old-wrapper (class-wrapper old-class))
1404 (old-layout (wrapper-instance-slots-layout old-wrapper))
1405 (new-layout (wrapper-instance-slots-layout new-wrapper))
1406 (old-slots (get-slots instance))
1407 (new-slots (get-slots copy))
1408 (old-class-slots (wrapper-class-slots old-wrapper)))
1409
1410 ;;
1411 ;; "The values of local slots specified by both the class Cto and
1412 ;; Cfrom are retained. If such a local slot was unbound, it remains
1413 ;; unbound."
1414 ;;
1415 (loop for new-slot in new-layout and new-position from 0
1416 for old-position = (posq new-slot old-layout)
1417 when old-position do
1418 (setf (slot-ref new-slots new-position)
1419 (slot-ref old-slots old-position)))
1420 ;;
1421 ;; "The values of slots specified as shared in the class Cfrom and
1422 ;; as local in the class Cto are retained."
1423 ;;
1424 (loop for (name . val) in old-class-slots
1425 for new-position = (posq name new-layout)
1426 when new-position do
1427 (setf (slot-ref new-slots new-position) val))
1428
1429 ;; Make the copy point to the old instance's storage, and make the
1430 ;; old instance point to the new storage.
1431 (swap-wrappers-and-slots instance copy)
1432
1433 (apply #'update-instance-for-different-class copy instance initargs)
1434 instance))
1435
1436 (defmethod change-class ((instance standard-object)
1437 (new-class standard-class)
1438 &rest initargs)
1439 (change-class-internal instance new-class initargs))
1440
1441 (defmethod change-class ((instance funcallable-standard-object)
1442 (new-class funcallable-standard-class)
1443 &rest initargs)
1444 (change-class-internal instance new-class initargs))
1445
1446 (defmethod change-class ((instance standard-object)
1447 (new-class funcallable-standard-class)
1448 &rest initargs)
1449 (declare (ignore initargs))
1450 (error "~@<Can't change the class of ~S to ~S ~
1451 because it isn't already an instance with metaclass ~S.~@:>"
1452 instance new-class 'standard-class))
1453
1454 (defmethod change-class ((instance funcallable-standard-object)
1455 (new-class standard-class)
1456 &rest initargs)
1457 (declare (ignore initargs))
1458 (error "~@<Can't change the class of ~S to ~S ~
1459 because it isn't already an instance with metaclass ~S.~@:>"
1460 instance new-class 'funcallable-standard-class))
1461
1462 (defmethod change-class ((instance t) (new-class-name symbol) &rest initargs)
1463 (apply #'change-class instance (find-class new-class-name) initargs))
1464
1465
1466
1467 ;;;
1468 ;;; The metaclass BUILT-IN-CLASS
1469 ;;;
1470 ;;; This metaclass is something of a weird creature. By this point, all
1471 ;;; instances of it which will exist have been created, and no instance
1472 ;;; is ever created by calling MAKE-INSTANCE.
1473 ;;;
1474 ;;; But, there are other parts of the protcol we must follow and those
1475 ;;; definitions appear here.
1476 ;;;
1477 (defmethod shared-initialize :before
1478 ((class built-in-class) slot-names &rest initargs)
1479 (declare (ignore slot-names initargs))
1480 (error "Attempt to initialize or reinitialize a built-in class."))
1481
1482 (defmethod class-direct-slots ((class built-in-class)) ())
1483 (defmethod class-slots ((class built-in-class)) ())
1484 (defmethod class-direct-default-initargs ((class built-in-class)) ())
1485 (defmethod class-default-initargs ((class built-in-class)) ())
1486
1487 (defmethod validate-superclass ((c class) (s built-in-class))
1488 (or (eq s *the-class-t*)
1489 (eq s *the-class-stream*)))
1490
1491
1492
1493 ;;;
1494 ;;;
1495 ;;;
1496
1497 (macrolet ((frob (method)
1498 `(defmethod ,method ((class forward-referenced-class))
1499 (declare (ignore class))
1500 ())))
1501 (frob class-direct-slots)
1502 (frob class-direct-default-initargs))
1503
1504 (macrolet ((frob (method)
1505 `(defmethod ,method ((class forward-referenced-class))
1506 (error "~@<~S called for forward referenced class ~S.~@:>"
1507 ',method class))))
1508 (frob class-default-initargs)
1509 (frob class-precedence-list)
1510 (frob class-slots))
1511
1512 (defmethod validate-superclass ((c slot-class)
1513 (f forward-referenced-class))
1514 t)
1515
1516
1517 ;;;
1518 ;;;
1519 ;;;
1520
1521 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1522 (pushnew dependent (plist-value metaobject 'dependents)))
1523
1524 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1525 (setf (plist-value metaobject 'dependents)
1526 (delete dependent (plist-value metaobject 'dependents))))
1527
1528 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1529 (dolist (dependent (plist-value metaobject 'dependents))
1530 (funcall function dependent)))
1531
1532
1533 ;;;
1534 ;;; Conditions
1535 ;;;
1536 (defmethod class-direct-slots ((class condition-class)) ())
1537 (defmethod class-slots ((class condition-class)) ())
1538 (defmethod class-default-initargs ((class condition-class)) ())
1539 (defmethod class-direct-default-initargs ((class condition-class)) ())
1540
1541 (defmethod shared-initialize :after ((class condition-class) slot-names
1542 &key direct-superclasses)
1543 (declare (ignore slot-names))
1544 (let ((kernel-class (kernel::find-class (class-name class))))
1545 (with-slots (wrapper class-precedence-list prototype predicate-name
1546 (direct-supers direct-superclasses))
1547 class
1548 (setf (slot-value class 'finalized-p) t)
1549 (setf (kernel:%class-pcl-class kernel-class) class)
1550 (setq direct-supers direct-superclasses)
1551 (setq wrapper (kernel:%class-layout kernel-class))
1552 (setq class-precedence-list (compute-class-precedence-list class))
1553 (setq prototype (make-condition (class-name class)))
1554 (add-direct-subclasses class direct-superclasses)
1555 (setq predicate-name (make-class-predicate-name (class-name class)))
1556 (make-class-predicate class predicate-name))))
1557

  ViewVC Help
Powered by ViewVC 1.1.5