1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; This software is derived from software originally released by Xerox
5 ;;;; Corporation. Copyright and release statements follow. Later modifications
6 ;;;; to the software are in the public domain and are provided with
7 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
10 ;;;; copyright information from original PCL sources:
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
15 ;;;; Use and copying of this software and preparation of derivative works based
16 ;;;; upon this software are permitted. Any distribution of this software or
17 ;;;; derivative works must comply with all applicable United States export
20 ;;;; This software is made available AS IS, and Xerox Corporation makes no
21 ;;;; warranty about the software, its performance or its conformity to any
26 (defmethod slot-accessor-function ((slotd effective-slot-definition) type)
28 (reader (slot-definition-reader-function slotd))
29 (writer (slot-definition-writer-function slotd))
30 (boundp (slot-definition-boundp-function slotd))))
32 (defmethod (setf slot-accessor-function) (function
33 (slotd effective-slot-definition)
36 (reader (setf (slot-definition-reader-function slotd) function))
37 (writer (setf (slot-definition-writer-function slotd) function))
38 (boundp (setf (slot-definition-boundp-function slotd) function))))
40 (defconstant +slotd-reader-function-std-p+ 1)
41 (defconstant +slotd-writer-function-std-p+ 2)
42 (defconstant +slotd-boundp-function-std-p+ 4)
43 (defconstant +slotd-all-function-std-p+ 7)
45 (defmethod slot-accessor-std-p ((slotd effective-slot-definition) type)
46 (let ((flags (slot-value slotd 'accessor-flags)))
47 (declare (type fixnum flags))
49 (eql +slotd-all-function-std-p+ flags)
50 (let ((mask (ecase type
51 (reader +slotd-reader-function-std-p+)
52 (writer +slotd-writer-function-std-p+)
53 (boundp +slotd-boundp-function-std-p+))))
54 (declare (type fixnum mask))
55 (not (zerop (the fixnum (logand mask flags))))))))
57 (defmethod (setf slot-accessor-std-p) (value
58 (slotd effective-slot-definition)
60 (let ((mask (ecase type
61 (reader +slotd-reader-function-std-p+)
62 (writer +slotd-writer-function-std-p+)
63 (boundp +slotd-boundp-function-std-p+)))
64 (flags (slot-value slotd 'accessor-flags)))
65 (declare (type fixnum mask flags))
66 (setf (slot-value slotd 'accessor-flags)
68 (the fixnum (logior mask flags))
69 (the fixnum (logand (the fixnum (lognot mask)) flags)))))
72 (defmethod initialize-internal-slot-functions ((slotd
73 effective-slot-definition))
74 (let* ((name (slot-value slotd 'name))
75 (class (slot-value slotd 'class)))
76 (let ((table (or (gethash name *name->class->slotd-table*)
77 (setf (gethash name *name->class->slotd-table*)
78 (make-hash-table :test 'eq :size 5)))))
79 (setf (gethash class table) slotd))
80 (dolist (type '(reader writer boundp))
81 (let* ((gf-name (ecase type
82 (reader 'slot-value-using-class)
83 (writer '(setf slot-value-using-class))
84 (boundp 'slot-boundp-using-class)))
85 (gf (gdefinition gf-name)))
86 (compute-slot-accessor-info slotd type gf)))
87 (initialize-internal-slot-gfs name)))
89 ;;; CMUCL (Gerd PCL 2003-04-25) comment:
91 ;;; Compute an effective method for SLOT-VALUE-USING-CLASS, (SETF
92 ;;; SLOT-VALUE-USING-CLASS) or SLOT-BOUNDP-USING-CLASS for reading/
93 ;;; writing/testing effective slot SLOTD.
95 ;;; TYPE is one of the symbols READER, WRITER or BOUNDP, depending on
96 ;;; GF. Store the effective method in the effective slot definition
97 ;;; object itself; these GFs have special dispatch functions calling
98 ;;; effective methods directly retrieved from effective slot
99 ;;; definition objects, as an optimization.
101 ;;; FIXME: Change the function name to COMPUTE-SVUC-SLOTD-FUNCTION,
103 (defmethod compute-slot-accessor-info ((slotd effective-slot-definition)
105 (let* ((name (slot-value slotd 'name))
106 (class (slot-value slotd 'class))
107 (old-slotd (find-slot-definition class name))
108 (old-std-p (and old-slotd (slot-accessor-std-p old-slotd 'all))))
109 (multiple-value-bind (function std-p)
110 (if (eq *boot-state* 'complete)
111 (get-accessor-method-function gf type class slotd)
112 (get-optimized-std-accessor-method-function class slotd type))
113 (setf (slot-accessor-std-p slotd type) std-p)
114 (setf (slot-accessor-function slotd type) function))
115 (when (and old-slotd (not (eq old-std-p (slot-accessor-std-p slotd 'all))))
116 (push (cons class name) *pv-table-cache-update-info*))))
118 (defmethod slot-definition-allocation ((slotd structure-slot-definition))
121 (defmethod shared-initialize :after ((object documentation-mixin)
123 &key (documentation nil documentation-p))
124 (declare (ignore slot-names))
125 (when documentation-p
126 (setf (plist-value object 'documentation) documentation)))
128 ;;; default if DOC-TYPE doesn't match one of the specified types
129 (defmethod documentation (object doc-type)
130 (warn "unsupported DOCUMENTATION: type ~S for object ~S"
135 ;;; default if DOC-TYPE doesn't match one of the specified types
136 (defmethod (setf documentation) (new-value object doc-type)
137 ;; CMU CL made this an error, but since ANSI says that even for supported
138 ;; doc types an implementation is permitted to discard docs at any time
139 ;; for any reason, this feels to me more like a warning. -- WHN 19991214
140 (warn "discarding unsupported DOCUMENTATION of type ~S for object ~S"
145 (defmethod documentation ((object documentation-mixin) doc-type)
146 (declare (ignore doc-type))
147 (plist-value object 'documentation))
149 (defmethod (setf documentation) (new-value
150 (object documentation-mixin)
152 (declare (ignore doc-type))
153 (setf (plist-value object 'documentation) new-value))
155 (defmethod documentation ((slotd standard-slot-definition) doc-type)
156 (declare (ignore doc-type))
157 (slot-value slotd 'documentation))
159 (defmethod (setf documentation) (new-value
160 (slotd standard-slot-definition)
162 (declare (ignore doc-type))
163 (setf (slot-value slotd 'documentation) new-value))
165 ;;;; various class accessors that are a little more complicated than can be
166 ;;;; done with automatically generated reader methods
168 (defmethod class-prototype ((class std-class))
169 (with-slots (prototype) class
170 (or prototype (setq prototype (allocate-instance class)))))
172 (defmethod class-prototype ((class structure-class))
173 (with-slots (prototype wrapper defstruct-constructor) class
176 (if defstruct-constructor
177 (allocate-instance class)
178 (allocate-standard-instance wrapper))))))
180 (defmethod class-direct-default-initargs ((class slot-class))
181 (plist-value class 'direct-default-initargs))
183 (defmethod class-default-initargs ((class slot-class))
184 (plist-value class 'default-initargs))
186 (defmethod class-slot-cells ((class std-class))
187 (plist-value class 'class-slot-cells))
189 ;;;; class accessors that are even a little bit more complicated than those
190 ;;;; above. These have a protocol for updating them, we must implement that
193 ;;; Maintaining the direct subclasses backpointers. The update methods are
194 ;;; here, the values are read by an automatically generated reader method.
195 (defmethod add-direct-subclass ((class class) (subclass class))
196 (with-slots (direct-subclasses) class
197 (pushnew subclass direct-subclasses)
199 (defmethod remove-direct-subclass ((class class) (subclass class))
200 (with-slots (direct-subclasses) class
201 (setq direct-subclasses (remove subclass direct-subclasses))
204 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
206 ;;; There are four generic functions involved, each has one method for the
207 ;;; class case and another method for the damned EQL specializers. All of
208 ;;; these are specified methods and appear in their specified place in the
211 ;;; ADD-DIRECT-METHOD
212 ;;; REMOVE-DIRECT-METHOD
213 ;;; SPECIALIZER-DIRECT-METHODS
214 ;;; SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
216 ;;; In each case, we maintain one value which is a cons. The car is the list
217 ;;; methods. The cdr is a list of the generic functions. The cdr is always
219 (defmethod add-direct-method ((specializer class) (method method))
220 (with-slots (direct-methods) specializer
221 (setf (car direct-methods) (adjoin method (car direct-methods)) ;PUSH
222 (cdr direct-methods) ()))
224 (defmethod remove-direct-method ((specializer class) (method method))
225 (with-slots (direct-methods) specializer
226 (setf (car direct-methods) (remove method (car direct-methods))
227 (cdr direct-methods) ()))
230 (defmethod specializer-direct-methods ((specializer class))
231 (with-slots (direct-methods) specializer
232 (car direct-methods)))
234 (defmethod specializer-direct-generic-functions ((specializer class))
235 (with-slots (direct-methods) specializer
236 (or (cdr direct-methods)
237 (setf (cdr direct-methods)
239 (dolist (m (car direct-methods))
240 ;; the old PCL code used COLLECTING-ONCE which used
241 ;; #'EQ to check for newness
242 (pushnew (method-generic-function m) collect :test #'eq))
243 (nreverse collect))))))
245 ;;; This hash table is used to store the direct methods and direct generic
246 ;;; functions of EQL specializers. Each value in the table is the cons.
247 (defvar *eql-specializer-methods* (make-hash-table :test 'eql))
248 (defvar *class-eq-specializer-methods* (make-hash-table :test 'eq))
250 (defmethod specializer-method-table ((specializer eql-specializer))
251 *eql-specializer-methods*)
253 (defmethod specializer-method-table ((specializer class-eq-specializer))
254 *class-eq-specializer-methods*)
256 (defmethod add-direct-method ((specializer specializer-with-object)
258 (let* ((object (specializer-object specializer))
259 (table (specializer-method-table specializer))
260 (entry (gethash object table)))
263 (setf (gethash object table)
265 (setf (car entry) (adjoin method (car entry))
269 (defmethod remove-direct-method ((specializer specializer-with-object)
271 (let* ((object (specializer-object specializer))
272 (entry (gethash object (specializer-method-table specializer))))
274 (setf (car entry) (remove method (car entry))
278 (defmethod specializer-direct-methods ((specializer specializer-with-object))
279 (car (gethash (specializer-object specializer)
280 (specializer-method-table specializer))))
282 (defmethod specializer-direct-generic-functions ((specializer
283 specializer-with-object))
284 (let* ((object (specializer-object specializer))
285 (entry (gethash object (specializer-method-table specializer))))
290 (dolist (m (car entry))
291 (pushnew (method-generic-function m) collect :test #'eq))
292 (nreverse collect)))))))
294 (defun map-specializers (function)
295 (map-all-classes (lambda (class)
296 (funcall function (class-eq-specializer class))
297 (funcall function class)))
298 (maphash (lambda (object methods)
299 (declare (ignore methods))
300 (intern-eql-specializer object))
301 *eql-specializer-methods*)
302 (maphash (lambda (object specl)
303 (declare (ignore object))
304 (funcall function specl))
305 *eql-specializer-table*)
308 (defun map-all-generic-functions (function)
309 (let ((all-generic-functions (make-hash-table :test 'eq)))
310 (map-specializers (lambda (specl)
311 (dolist (gf (specializer-direct-generic-functions
313 (unless (gethash gf all-generic-functions)
314 (setf (gethash gf all-generic-functions) t)
315 (funcall function gf))))))
318 (defmethod shared-initialize :after ((specl class-eq-specializer)
321 (declare (ignore slot-names))
322 (setf (slot-value specl 'type) `(class-eq ,(specializer-class specl))))
324 (defmethod shared-initialize :after ((specl eql-specializer) slot-names &key)
325 (declare (ignore slot-names))
326 (setf (slot-value specl 'type) `(eql ,(specializer-object specl))))
328 (defun real-load-defclass (name metaclass-name supers slots other)
329 (let ((res (apply #'ensure-class name :metaclass metaclass-name
330 :direct-superclasses supers
332 :definition-source `((defclass ,name)
337 (setf (gdefinition 'load-defclass) #'real-load-defclass)
339 (defun ensure-class (name &rest args)
340 (apply #'ensure-class-using-class
341 (let ((class (find-class name nil)))
342 (when (and class (eq name (class-name class)))
343 ;; NAME is the proper name of CLASS, so redefine it
348 (defmethod ensure-class-using-class ((class null) name &rest args &key)
349 (multiple-value-bind (meta initargs)
350 (ensure-class-values class args)
351 (set-class-type-translation (class-prototype meta) name)
352 (setf class (apply #'make-instance meta :name name initargs)
353 (find-class name) class)
354 (set-class-type-translation class name)
357 (defmethod ensure-class-using-class ((class pcl-class) name &rest args &key)
358 (multiple-value-bind (meta initargs)
359 (ensure-class-values class args)
360 (unless (eq (class-of class) meta)
361 (apply #'change-class class meta initargs))
362 (apply #'reinitialize-instance class initargs)
363 (setf (find-class name) class)
364 (set-class-type-translation class name)
367 (defmethod class-predicate-name ((class t))
372 ((not (legal-class-name-p s))
373 (error "~S is not a class or a legal class name." s))
375 (or (find-class s nil)
376 (make-instance 'forward-referenced-class
379 (defun ensure-class-values (class args)
380 (let* ((initargs (copy-list args))
381 (unsupplied (list 1))
382 (supplied-meta (getf initargs :metaclass unsupplied))
383 (supplied-supers (getf initargs :direct-superclasses unsupplied))
384 (supplied-slots (getf initargs :direct-slots unsupplied))
386 (cond ((neq supplied-meta unsupplied)
387 (find-class supplied-meta))
389 (forward-referenced-class-p class))
390 *the-class-standard-class*)
393 ;; KLUDGE: It seemed to me initially that there ought to be a way
394 ;; of collecting all the erroneous problems in one go, rather than
395 ;; this way of solving the problem of signalling the errors that
396 ;; we are required to, which stops at the first bogus input.
397 ;; However, after playing around a little, I couldn't find that
398 ;; way, so I've left it as is, but if someone does come up with a
399 ;; better way... -- CSR, 2002-09-08
400 (do ((direct-slots (getf initargs :direct-slots) (cdr direct-slots)))
401 ((endp direct-slots) nil)
402 (destructuring-bind (slot &rest more) direct-slots
403 (let ((slot-name (getf slot :name)))
404 (when (some (lambda (s) (eq slot-name (getf s :name))) more)
405 ;; FIXME: It's quite possible that we ought to define an
406 ;; SB-INT:PROGRAM-ERROR function to signal these and other
407 ;; errors throughout the codebase that are required to be
408 ;; of type PROGRAM-ERROR.
409 (error 'simple-program-error
410 :format-control "~@<There is more than one direct slot ~
412 :format-arguments (list slot-name)))
413 (do ((stuff slot (cddr stuff)))
415 (destructuring-bind (option value &rest more) stuff
417 ((and (member option '(:allocation :type
418 :initform :documentation))
420 (getf more option unsupplied))))
421 (error 'simple-program-error
422 :format-control "~@<Duplicate slot option ~S for ~
424 :format-arguments (list option slot-name)))
425 ((and (eq option :readers)
426 (notevery #'symbolp value))
427 (error 'simple-program-error
428 :format-control "~@<Slot reader names for slot ~
429 named ~S must be symbols.~:>"
430 :format-arguments (list slot-name)))
431 ((and (eq option :initargs)
432 (notevery #'symbolp value))
433 (error 'simple-program-error
434 :format-control "~@<Slot initarg names for slot ~
435 named ~S must be symbols.~:>"
436 :format-arguments (list slot-name)))))))))
437 (loop for (initarg . more) on (getf initargs :direct-default-initargs)
438 for name = (car initarg)
439 when (some (lambda (a) (eq (car a) name)) more)
440 do (error 'simple-program-error
441 :format-control "~@<Duplicate initialization argument ~
442 name ~S in :DEFAULT-INITARGS.~:>"
443 :format-arguments (list name class)))
445 (default-initargs 0))
446 (do ((args initargs (cddr args)))
450 (when (> (incf metaclass) 1)
451 (error 'simple-program-error
452 :format-control "~@<More than one :METACLASS ~
453 option specified.~:>")))
454 (:direct-default-initargs
455 (when (> (incf default-initargs) 1)
456 (error 'simple-program-error
457 :format-control "~@<More than one :DEFAULT-INITARGS ~
458 option specified.~:>"))))))
459 (remf initargs :metaclass)
460 (loop (unless (remf initargs :direct-superclasses) (return)))
461 (loop (unless (remf initargs :direct-slots) (return)))
465 (when (neq supplied-supers unsupplied)
466 (list :direct-superclasses (mapcar #'fix-super supplied-supers)))
467 (when (neq supplied-slots unsupplied)
468 (list :direct-slots supplied-slots))
471 (defmethod shared-initialize :after
474 &key (direct-superclasses nil direct-superclasses-p)
475 (direct-slots nil direct-slots-p)
476 (direct-default-initargs nil direct-default-initargs-p)
477 (predicate-name nil predicate-name-p))
478 (declare (ignore slot-names))
479 (cond (direct-superclasses-p
480 (setq direct-superclasses
481 (or direct-superclasses
482 (list (if (funcallable-standard-class-p class)
483 *the-class-funcallable-standard-object*
484 *the-class-standard-object*))))
485 (dolist (superclass direct-superclasses)
486 (unless (validate-superclass class superclass)
487 (error "The class ~S was specified as a~%
488 super-class of the class ~S;~%~
489 but the meta-classes ~S and~%~S are incompatible.~@
490 Define a method for ~S to avoid this error."
491 superclass class (class-of superclass) (class-of class)
492 'validate-superclass)))
493 (setf (slot-value class 'direct-superclasses) direct-superclasses))
495 (setq direct-superclasses (slot-value class 'direct-superclasses))))
498 (setf (slot-value class 'direct-slots)
499 (mapcar (lambda (pl) (make-direct-slotd class pl))
501 (slot-value class 'direct-slots)))
502 (if direct-default-initargs-p
503 (setf (plist-value class 'direct-default-initargs)
504 direct-default-initargs)
505 (setq direct-default-initargs
506 (plist-value class 'direct-default-initargs)))
507 (setf (plist-value class 'class-slot-cells)
508 ;; The below initializes shared slots from direct initforms,
509 ;; but one might inherit initforms from superclasses
510 ;; (cf. UPDATE-SHARED-SLOT-VALUES).
512 (dolist (dslotd direct-slots)
513 (when (eq :class (slot-definition-allocation dslotd))
514 (let ((initfunction (slot-definition-initfunction dslotd)))
515 (push (cons (slot-definition-name dslotd)
517 (funcall initfunction)
521 (setq predicate-name (if predicate-name-p
522 (setf (slot-value class 'predicate-name)
523 (car predicate-name))
524 (or (slot-value class 'predicate-name)
525 (setf (slot-value class 'predicate-name)
526 (make-class-predicate-name (class-name
528 (add-direct-subclasses class direct-superclasses)
529 (make-class-predicate class predicate-name)
530 (update-class class nil)
531 (do* ((slots (slot-value class 'slots) (cdr slots))
533 ((null slots) (when dupes
535 ;; FIXME: the indentation request ("~4I")
536 ;; below appears not to do anything. Finding
537 ;; out why would be nice. -- CSR, 2003-04-24
538 "~@<slot names with the same SYMBOL-NAME but ~
539 different SYMBOL-PACKAGE (possible package problem) ~
540 for class ~S:~@:_~4I~<~@{~S~^~:@_~}~:>~@:>"
543 (let* ((slot (car slots))
544 (oslots (remove (slot-definition-name slot) (cdr slots)
545 :test-not #'string= :key #'slot-definition-name)))
547 (pushnew (cons (slot-definition-name slot)
548 (mapcar #'slot-definition-name oslots))
550 :test #'string= :key #'car))))
551 (add-slot-accessors class direct-slots)
552 (make-preliminary-layout class))
554 (defmethod shared-initialize :after ((class forward-referenced-class)
555 slot-names &key &allow-other-keys)
556 (declare (ignore slot-names))
557 (make-preliminary-layout class))
559 (defvar *allow-forward-referenced-classes-in-cpl-p* nil)
561 ;;; Give CLASS a preliminary layout if it doesn't have one already, to
562 ;;; make it known to the type system.
563 (defun make-preliminary-layout (class)
564 (flet ((compute-preliminary-cpl (root)
565 (let ((*allow-forward-referenced-classes-in-cpl-p* t))
566 (compute-class-precedence-list root))))
567 (unless (class-finalized-p class)
568 (let ((name (class-name class)))
569 (setf (find-class name) class)
570 ;; KLUDGE: This is fairly horrible. We need to make a
571 ;; full-fledged CLASSOID here, not just tell the compiler that
572 ;; some class is forthcoming, because there are legitimate
573 ;; questions one can ask of the type system, implemented in
574 ;; terms of CLASSOIDs, involving forward-referenced classes. So.
575 (when (and (eq *boot-state* 'complete)
576 (null (find-classoid name nil)))
577 (setf (find-classoid name)
578 (make-standard-classoid :name name)))
579 (set-class-type-translation class name)
580 (let ((layout (make-wrapper 0 class))
581 (classoid (find-classoid name)))
582 (setf (layout-classoid layout) classoid)
583 (setf (classoid-pcl-class classoid) class)
584 (setf (slot-value class 'wrapper) layout)
585 (let ((cpl (compute-preliminary-cpl class)))
586 (setf (layout-inherits layout)
587 (order-layout-inherits
588 (map 'simple-vector #'class-wrapper
589 (reverse (rest cpl))))))
590 (register-layout layout :invalidate t)
591 (setf (classoid-layout classoid) layout)
592 (mapc #'make-preliminary-layout (class-direct-subclasses class)))))))
595 (defmethod shared-initialize :before ((class class) slot-names &key name)
596 (declare (ignore slot-names name))
597 ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
598 ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
599 (setf (slot-value class 'type) `(class ,class))
600 (setf (slot-value class 'class-eq-specializer)
601 (make-instance 'class-eq-specializer :class class)))
603 (defmethod reinitialize-instance :before ((class slot-class) &key)
604 (remove-direct-subclasses class (class-direct-superclasses class))
605 (remove-slot-accessors class (class-direct-slots class)))
607 (defmethod reinitialize-instance :after ((class slot-class)
610 (map-dependents class
612 (apply #'update-dependent class dependent initargs))))
614 (defmethod shared-initialize :after ((class condition-class) slot-names
615 &key direct-slots direct-superclasses)
616 (declare (ignore slot-names))
617 (let ((classoid (find-classoid (class-name class))))
618 (with-slots (wrapper class-precedence-list prototype predicate-name
619 (direct-supers direct-superclasses))
621 (setf (slot-value class 'direct-slots)
622 (mapcar (lambda (pl) (make-direct-slotd class pl))
624 (setf (slot-value class 'finalized-p) t)
625 (setf (classoid-pcl-class classoid) class)
626 (setq direct-supers direct-superclasses)
627 (setq wrapper (classoid-layout classoid))
628 (setq class-precedence-list (compute-class-precedence-list class))
629 (setq prototype (make-condition (class-name class)))
630 (add-direct-subclasses class direct-superclasses)
631 (setq predicate-name (make-class-predicate-name (class-name class)))
632 (make-class-predicate class predicate-name)
633 (setf (slot-value class 'slots) (compute-slots class))))
634 ;; Comment from Gerd's PCL, 2003-05-15:
636 ;; We don't ADD-SLOT-ACCESSORS here because we don't want to
637 ;; override condition accessors with generic functions. We do this
639 (update-pv-table-cache-info class))
641 (defmethod direct-slot-definition-class ((class condition-class)
643 (declare (ignore initargs))
644 (find-class 'condition-direct-slot-definition))
646 (defmethod effective-slot-definition-class ((class condition-class)
648 (declare (ignore initargs))
649 (find-class 'condition-effective-slot-definition))
651 (defmethod finalize-inheritance ((class condition-class))
652 (aver (slot-value class 'finalized-p))
655 (defmethod compute-effective-slot-definition
656 ((class condition-class) slot-name dslotds)
657 (let ((slotd (call-next-method)))
658 (setf (slot-definition-reader-function slotd)
660 (handler-case (condition-reader-function x slot-name)
661 ;; FIXME: FIND-SLOT-DEFAULT throws an error if the slot
662 ;; is unbound; maybe it should be a CELL-ERROR of some
664 (error () (values (slot-unbound class x slot-name))))))
665 (setf (slot-definition-writer-function slotd)
667 (condition-writer-function x v slot-name)))
668 (setf (slot-definition-boundp-function slotd)
670 (multiple-value-bind (v c)
671 (ignore-errors (condition-reader-function x slot-name))
676 (defmethod compute-slots ((class condition-class))
677 (mapcan (lambda (superclass)
678 (mapcar (lambda (dslotd)
679 (compute-effective-slot-definition
680 class (slot-definition-name dslotd) (list dslotd)))
681 (class-direct-slots superclass)))
682 (reverse (slot-value class 'class-precedence-list))))
684 (defmethod compute-slots :around ((class condition-class))
685 (let ((eslotds (call-next-method)))
686 (mapc #'initialize-internal-slot-functions eslotds)
689 (defmethod shared-initialize :after
690 ((slotd structure-slot-definition) slot-names &key
691 (allocation :instance) allocation-class)
692 (declare (ignore slot-names allocation-class))
693 (unless (eq allocation :instance)
694 (error "Structure slots must have :INSTANCE allocation.")))
696 (defun make-structure-class-defstruct-form (name direct-slots include)
697 (let* ((conc-name (intern (format nil "~S structure class " name)))
698 (constructor (intern (format nil "~Aconstructor" conc-name)))
699 (defstruct `(defstruct (,name
701 `((:include ,(class-name include))))
703 (:conc-name ,conc-name)
704 (:constructor ,constructor ())
706 ,@(mapcar (lambda (slot)
707 `(,(slot-definition-name slot)
710 (reader-names (mapcar (lambda (slotd)
711 (list 'slot-accessor name
712 (slot-definition-name slotd)
715 (writer-names (mapcar (lambda (slotd)
716 (list 'slot-accessor name
717 (slot-definition-name slotd)
721 (mapcar (lambda (slotd reader-name)
723 (slot-definition-defstruct-accessor-symbol
725 `(defun ,reader-name (obj)
726 (declare (type ,name obj))
728 direct-slots reader-names))
730 (mapcar (lambda (slotd writer-name)
732 (slot-definition-defstruct-accessor-symbol
734 `(defun ,writer-name (nv obj)
735 (declare (type ,name obj))
736 (setf (,accessor obj) nv))))
737 direct-slots writer-names))
741 ,@readers-init ,@writers-init
743 (values defstruct-form constructor reader-names writer-names)))
745 (defun make-defstruct-allocation-function (class)
746 (let ((dd (get-structure-dd (class-name class))))
748 (let ((instance (%make-instance (dd-length dd)))
749 (raw-index (dd-raw-index dd)))
750 (setf (%instance-layout instance)
751 (sb-kernel::compiler-layout-or-lose (dd-name dd)))
753 (setf (%instance-ref instance raw-index)
754 (make-array (dd-raw-length dd)
755 :element-type '(unsigned-byte 32))))
758 (defmethod shared-initialize :after
759 ((class structure-class)
761 &key (direct-superclasses nil direct-superclasses-p)
762 (direct-slots nil direct-slots-p)
763 direct-default-initargs
764 (predicate-name nil predicate-name-p))
765 (declare (ignore slot-names direct-default-initargs))
766 (if direct-superclasses-p
767 (setf (slot-value class 'direct-superclasses)
768 (or direct-superclasses
769 (setq direct-superclasses
770 (and (not (eq (class-name class) 'structure-object))
771 (list *the-class-structure-object*)))))
772 (setq direct-superclasses (slot-value class 'direct-superclasses)))
773 (let* ((name (class-name class))
774 (from-defclass-p (slot-value class 'from-defclass-p))
775 (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
777 (setf (slot-value class 'direct-slots)
781 (let* ((slot-name (getf pl :name))
784 "~S structure class ~A"
786 (accessor (intern acc-name)))
787 (setq pl (list* :defstruct-accessor-symbol
789 (make-direct-slotd class pl))
791 (setq direct-slots (slot-value class 'direct-slots)))
793 (let ((include (car (slot-value class 'direct-superclasses))))
794 (multiple-value-bind (defstruct-form constructor reader-names writer-names)
795 (make-structure-class-defstruct-form name direct-slots include)
796 (unless (structure-type-p name) (eval defstruct-form))
797 (mapc (lambda (dslotd reader-name writer-name)
798 (let* ((reader (gdefinition reader-name))
799 (writer (when (gboundp writer-name)
800 (gdefinition writer-name))))
801 (setf (slot-value dslotd 'internal-reader-function)
803 (setf (slot-value dslotd 'internal-writer-function)
805 direct-slots reader-names writer-names)
806 (setf (slot-value class 'defstruct-form) defstruct-form)
807 (setf (slot-value class 'defstruct-constructor) constructor)))
808 (setf (slot-value class 'defstruct-constructor)
809 (make-defstruct-allocation-function class)))
810 (add-direct-subclasses class direct-superclasses)
811 (setf (slot-value class 'class-precedence-list)
812 (compute-class-precedence-list class))
813 (setf (slot-value class 'slots) (compute-slots class))
814 (let ((lclass (find-classoid (class-name class))))
815 (setf (classoid-pcl-class lclass) class)
816 (setf (slot-value class 'wrapper) (classoid-layout lclass)))
817 (setf (slot-value class 'finalized-p) t)
818 (update-pv-table-cache-info class)
819 (setq predicate-name (if predicate-name-p
820 (setf (slot-value class 'predicate-name)
821 (car predicate-name))
822 (or (slot-value class 'predicate-name)
823 (setf (slot-value class 'predicate-name)
824 (make-class-predicate-name
825 (class-name class))))))
826 (make-class-predicate class predicate-name)
827 (add-slot-accessors class direct-slots)))
829 (defmethod direct-slot-definition-class ((class structure-class) &rest initargs)
830 (declare (ignore initargs))
831 (find-class 'structure-direct-slot-definition))
833 (defmethod finalize-inheritance ((class structure-class))
834 nil) ; always finalized
836 (defun add-slot-accessors (class dslotds)
837 (fix-slot-accessors class dslotds 'add))
839 (defun remove-slot-accessors (class dslotds)
840 (fix-slot-accessors class dslotds 'remove))
842 (defun fix-slot-accessors (class dslotds add/remove)
843 (flet ((fix (gfspec name r/w)
844 (let ((gf (ensure-generic-function gfspec)))
846 (r (if (eq add/remove 'add)
847 (add-reader-method class gf name)
848 (remove-reader-method class gf)))
849 (w (if (eq add/remove 'add)
850 (add-writer-method class gf name)
851 (remove-writer-method class gf)))))))
852 (dolist (dslotd dslotds)
853 (let ((slot-name (slot-definition-name dslotd)))
854 (dolist (r (slot-definition-readers dslotd)) (fix r slot-name 'r))
855 (dolist (w (slot-definition-writers dslotd)) (fix w slot-name 'w))))))
857 (defun add-direct-subclasses (class supers)
858 (dolist (super supers)
859 (unless (memq class (class-direct-subclasses class))
860 (add-direct-subclass super class))))
862 (defun remove-direct-subclasses (class supers)
863 (let ((old (class-direct-superclasses class)))
864 (dolist (o (set-difference old supers))
865 (remove-direct-subclass o class))))
867 (defmethod finalize-inheritance ((class std-class))
868 (update-class class t))
870 (defmethod finalize-inheritance ((class forward-referenced-class))
871 ;; FIXME: should we not be thinking a bit about what kinds of error
872 ;; we're throwing? Maybe we need a clos-error type to mix in? Or
873 ;; possibly a forward-referenced-class-error, though that's
874 ;; difficult given e.g. class precedence list calculations...
876 "~@<FINALIZE-INHERITANCE was called on a forward referenced class:~
881 (defun class-has-a-forward-referenced-superclass-p (class)
882 (or (forward-referenced-class-p class)
883 (some #'class-has-a-forward-referenced-superclass-p
884 (class-direct-superclasses class))))
886 ;;; This is called by :after shared-initialize whenever a class is initialized
887 ;;; or reinitialized. The class may or may not be finalized.
888 (defun update-class (class finalizep)
889 ;; Comment from Gerd Moellmann:
891 ;; Note that we can't simply delay the finalization when CLASS has
892 ;; no forward referenced superclasses because that causes bootstrap
894 (when (and (not finalizep)
895 (not (class-finalized-p class))
896 (not (class-has-a-forward-referenced-superclass-p class)))
897 (finalize-inheritance class)
898 (return-from update-class))
899 (when (or finalizep (class-finalized-p class)
900 (not (class-has-a-forward-referenced-superclass-p class)))
901 (setf (find-class (class-name class)) class)
902 (update-cpl class (compute-class-precedence-list class))
903 ;; This invocation of UPDATE-SLOTS, in practice, finalizes the
904 ;; class. The hoops above are to ensure that FINALIZE-INHERITANCE
905 ;; is called at finalization, so that MOP programmers can hook
906 ;; into the system as described in "Class Finalization Protocol"
907 ;; (section 5.5.2 of AMOP).
908 (update-slots class (compute-slots class))
909 (update-gfs-of-class class)
910 (update-inits class (compute-default-initargs class))
911 (update-shared-slot-values class)
912 (update-ctors 'finalize-inheritance :class class))
914 (dolist (sub (class-direct-subclasses class)) (update-class sub nil))))
916 (defun update-shared-slot-values (class)
917 (dolist (slot (class-slots class))
918 (when (eq (slot-definition-allocation slot) :class)
919 (let ((cell (assq (slot-definition-name slot) (class-slot-cells class))))
921 (let ((initfn (slot-definition-initfunction slot)))
923 (setf (cdr cell) (funcall initfn)))))))))
925 (defun update-cpl (class cpl)
926 (if (class-finalized-p class)
927 (unless (and (equal (class-precedence-list class) cpl)
929 (when (position :class (class-direct-slots c)
930 :key #'slot-definition-allocation)
932 ;; comment from the old CMU CL sources:
933 ;; Need to have the cpl setup before update-lisp-class-layout
934 ;; is called on CMU CL.
935 (setf (slot-value class 'class-precedence-list) cpl)
936 (force-cache-flushes class))
937 (setf (slot-value class 'class-precedence-list) cpl))
938 (update-class-can-precede-p cpl))
940 (defun update-class-can-precede-p (cpl)
942 (let ((first (car cpl)))
943 (dolist (c (cdr cpl))
944 (pushnew c (slot-value first 'can-precede-list))))
945 (update-class-can-precede-p (cdr cpl))))
947 (defun class-can-precede-p (class1 class2)
948 (member class2 (class-can-precede-list class1)))
950 (defun update-slots (class eslotds)
951 (let ((instance-slots ())
953 (dolist (eslotd eslotds)
954 (let ((alloc (slot-definition-allocation eslotd)))
956 (:instance (push eslotd instance-slots))
957 (:class (push eslotd class-slots)))))
959 ;; If there is a change in the shape of the instances then the
960 ;; old class is now obsolete.
961 (let* ((nlayout (mapcar #'slot-definition-name
962 (sort instance-slots #'<
963 :key #'slot-definition-location)))
964 (nslots (length nlayout))
965 (nwrapper-class-slots (compute-class-slots class-slots))
966 (owrapper (when (class-finalized-p class)
967 (class-wrapper class)))
968 (olayout (when owrapper
969 (wrapper-instance-slots-layout owrapper)))
970 (owrapper-class-slots (and owrapper (wrapper-class-slots owrapper)))
972 (cond ((null owrapper)
973 (make-wrapper nslots class))
974 ((and (equal nlayout olayout)
976 (loop for o in owrapper-class-slots
977 for n in nwrapper-class-slots
978 do (unless (eq (car o) (car n)) (return t)))))
981 ;; This will initialize the new wrapper to have the
982 ;; same state as the old wrapper. We will then have
983 ;; to change that. This may seem like wasted work
984 ;; (and it is), but the spec requires that we call
985 ;; MAKE-INSTANCES-OBSOLETE.
986 (make-instances-obsolete class)
987 (class-wrapper class)))))
989 (with-slots (wrapper slots) class
990 (update-lisp-class-layout class nwrapper)
992 (wrapper-instance-slots-layout nwrapper) nlayout
993 (wrapper-class-slots nwrapper) nwrapper-class-slots
994 (wrapper-no-of-instance-slots nwrapper) nslots
996 (setf (slot-value class 'finalized-p) t)
997 (unless (eq owrapper nwrapper)
998 (update-pv-table-cache-info class)
999 (maybe-update-standard-class-locations class)))))
1001 (defun compute-class-slots (eslotds)
1003 (dolist (eslotd eslotds)
1004 (push (assoc (slot-definition-name eslotd)
1005 (class-slot-cells (slot-definition-class eslotd)))
1007 (nreverse collect)))
1009 (defun update-gfs-of-class (class)
1010 (when (and (class-finalized-p class)
1011 (let ((cpl (class-precedence-list class)))
1012 (or (member *the-class-slot-class* cpl)
1013 (member *the-class-standard-effective-slot-definition*
1015 (let ((gf-table (make-hash-table :test 'eq)))
1016 (labels ((collect-gfs (class)
1017 (dolist (gf (specializer-direct-generic-functions class))
1018 (setf (gethash gf gf-table) t))
1019 (mapc #'collect-gfs (class-direct-superclasses class))))
1021 (maphash (lambda (gf ignore)
1022 (declare (ignore ignore))
1023 (update-gf-dfun class gf))
1026 (defun update-inits (class inits)
1027 (setf (plist-value class 'default-initargs) inits))
1029 (defmethod compute-default-initargs ((class slot-class))
1030 (let ((initargs (loop for c in (class-precedence-list class)
1031 append (class-direct-default-initargs c))))
1032 (delete-duplicates initargs :test #'eq :key #'car :from-end t)))
1034 ;;;; protocols for constructing direct and effective slot definitions
1036 (defmethod direct-slot-definition-class ((class std-class) &rest initargs)
1037 (declare (ignore initargs))
1038 (find-class 'standard-direct-slot-definition))
1040 (defun make-direct-slotd (class initargs)
1041 (let ((initargs (list* :class class initargs)))
1042 (apply #'make-instance
1043 (apply #'direct-slot-definition-class class initargs)
1046 (defmethod compute-slots ((class std-class))
1047 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
1048 ;; for each different slot name we find in our superclasses. Each
1049 ;; call receives the class and a list of the dslotds with that name.
1050 ;; The list is in most-specific-first order.
1051 (let ((name-dslotds-alist ()))
1052 (dolist (c (class-precedence-list class))
1053 (dolist (slot (class-direct-slots c))
1054 (let* ((name (slot-definition-name slot))
1055 (entry (assq name name-dslotds-alist)))
1057 (push slot (cdr entry))
1058 (push (list name slot) name-dslotds-alist)))))
1059 (mapcar (lambda (direct)
1060 (compute-effective-slot-definition class
1062 (nreverse (cdr direct))))
1063 name-dslotds-alist)))
1065 (defmethod compute-slots ((class standard-class))
1068 (defmethod compute-slots :around ((class standard-class))
1069 (let ((eslotds (call-next-method))
1071 (dolist (eslotd eslotds eslotds)
1072 (setf (slot-definition-location eslotd)
1073 (ecase (slot-definition-allocation eslotd)
1077 (let* ((name (slot-definition-name eslotd))
1078 (from-class (slot-definition-allocation-class eslotd))
1079 (cell (assq name (class-slot-cells from-class))))
1082 (initialize-internal-slot-functions eslotd))))
1084 (defmethod compute-slots ((class funcallable-standard-class))
1087 (defmethod compute-slots :around ((class funcallable-standard-class))
1088 (labels ((instance-slot-names (slotds)
1090 (dolist (slotd slotds (nreverse collect))
1091 (when (eq (slot-definition-allocation slotd) :instance)
1092 (push (slot-definition-name slotd) collect)))))
1093 ;; This sorts slots so that slots of classes later in the CPL
1094 ;; come before slots of other classes. This is crucial for
1095 ;; funcallable instances because it ensures that the slots of
1096 ;; FUNCALLABLE-STANDARD-OBJECT, which includes the slots of
1097 ;; KERNEL:FUNCALLABLE-INSTANCE, come first, which in turn
1098 ;; makes it possible to treat FUNCALLABLE-STANDARD-OBJECT as
1099 ;; a funcallable instance.
1100 (compute-layout (eslotds)
1102 (names (instance-slot-names eslotds)))
1104 (reverse (class-precedence-list class))
1105 (nreverse (nconc names first)))
1106 (dolist (ss (class-slots class))
1107 (let ((name (slot-definition-name ss)))
1108 (when (member name names)
1110 (setq names (delete name names)))))))))
1111 (let ((all-slotds (call-next-method))
1114 (dolist (slotd all-slotds)
1115 (ecase (slot-definition-allocation slotd)
1116 (:instance (push slotd instance-slots))
1117 (:class (push slotd class-slots))))
1118 (let ((layout (compute-layout instance-slots)))
1119 (dolist (slotd instance-slots)
1120 (setf (slot-definition-location slotd)
1121 (position (slot-definition-name slotd) layout))
1122 (initialize-internal-slot-functions slotd)))
1123 (dolist (slotd class-slots)
1124 (let ((name (slot-definition-name slotd))
1125 (from-class (slot-definition-allocation-class slotd)))
1126 (setf (slot-definition-location slotd)
1127 (assoc name (class-slot-cells from-class)))
1128 (aver (consp (slot-definition-location slotd)))
1129 (initialize-internal-slot-functions slotd)))
1132 (defmethod compute-slots ((class structure-class))
1133 (mapcan (lambda (superclass)
1134 (mapcar (lambda (dslotd)
1135 (compute-effective-slot-definition
1137 (slot-definition-name dslotd)
1139 (class-direct-slots superclass)))
1140 (reverse (slot-value class 'class-precedence-list))))
1142 (defmethod compute-slots :around ((class structure-class))
1143 (let ((eslotds (call-next-method)))
1144 (mapc #'initialize-internal-slot-functions eslotds)
1147 (defmethod compute-effective-slot-definition ((class slot-class) name dslotds)
1148 (declare (ignore name))
1149 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
1150 (class (apply #'effective-slot-definition-class class initargs)))
1151 (apply #'make-instance class initargs)))
1153 (defmethod effective-slot-definition-class ((class std-class) &rest initargs)
1154 (declare (ignore initargs))
1155 (find-class 'standard-effective-slot-definition))
1157 (defmethod effective-slot-definition-class ((class structure-class) &rest initargs)
1158 (declare (ignore initargs))
1159 (find-class 'structure-effective-slot-definition))
1161 (defmethod compute-effective-slot-definition-initargs
1162 ((class slot-class) direct-slotds)
1168 (allocation-class nil)
1174 (dolist (slotd direct-slotds)
1177 (setq name (slot-definition-name slotd)
1180 (when (slot-definition-initfunction slotd)
1181 (setq initform (slot-definition-initform slotd)
1182 initfunction (slot-definition-initfunction slotd)
1185 (setq allocation (slot-definition-allocation slotd)
1186 allocation-class (slot-definition-class slotd)
1188 (setq initargs (append (slot-definition-initargs slotd) initargs))
1189 (let ((slotd-type (slot-definition-type slotd)))
1190 (setq type (cond ((eq type t) slotd-type)
1191 ((*subtypep type slotd-type) type)
1192 (t `(and ,type ,slotd-type)))))))
1195 :initfunction initfunction
1197 :allocation allocation
1198 :allocation-class allocation-class
1202 (defmethod compute-effective-slot-definition-initargs :around
1203 ((class structure-class) direct-slotds)
1204 (let ((slotd (car direct-slotds)))
1205 (list* :defstruct-accessor-symbol
1206 (slot-definition-defstruct-accessor-symbol slotd)
1207 :internal-reader-function
1208 (slot-definition-internal-reader-function slotd)
1209 :internal-writer-function
1210 (slot-definition-internal-writer-function slotd)
1211 (call-next-method))))
1213 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
1214 ;;; to make the method object. They have to use make-a-method which
1215 ;;; is a specially bootstrapped mechanism for making standard methods.
1216 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
1217 (declare (ignore direct-slot initargs))
1218 (find-class 'standard-reader-method))
1220 (defmethod add-reader-method ((class slot-class) generic-function slot-name)
1221 (add-method generic-function
1222 (make-a-method 'standard-reader-method
1224 (list (or (class-name class) 'object))
1226 (make-reader-method-function class slot-name)
1227 "automatically generated reader method"
1230 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
1231 (declare (ignore direct-slot initargs))
1232 (find-class 'standard-writer-method))
1234 (defmethod add-writer-method ((class slot-class) generic-function slot-name)
1235 (add-method generic-function
1236 (make-a-method 'standard-writer-method
1238 (list 'new-value (or (class-name class) 'object))
1239 (list *the-class-t* class)
1240 (make-writer-method-function class slot-name)
1241 "automatically generated writer method"
1244 (defmethod add-boundp-method ((class slot-class) generic-function slot-name)
1245 (add-method generic-function
1246 (make-a-method 'standard-boundp-method
1248 (list (or (class-name class) 'object))
1250 (make-boundp-method-function class slot-name)
1251 "automatically generated boundp method"
1254 (defmethod remove-reader-method ((class slot-class) generic-function)
1255 (let ((method (get-method generic-function () (list class) nil)))
1256 (when method (remove-method generic-function method))))
1258 (defmethod remove-writer-method ((class slot-class) generic-function)
1260 (get-method generic-function () (list *the-class-t* class) nil)))
1261 (when method (remove-method generic-function method))))
1263 (defmethod remove-boundp-method ((class slot-class) generic-function)
1264 (let ((method (get-method generic-function () (list class) nil)))
1265 (when method (remove-method generic-function method))))
1267 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITE-METHOD function are NOT
1268 ;;; part of the standard protocol. They are however useful, PCL makes
1269 ;;; use of them internally and documents them for PCL users.
1271 ;;; *** This needs work to make type testing by the writer functions which
1272 ;;; *** do type testing faster. The idea would be to have one constructor
1273 ;;; *** for each possible type test.
1275 ;;; *** There is a subtle bug here which is going to have to be fixed.
1276 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1277 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1278 ;;; *** defined for this metaclass a chance to run.
1280 (defmethod make-reader-method-function ((class slot-class) slot-name)
1281 (make-std-reader-method-function (class-name class) slot-name))
1283 (defmethod make-writer-method-function ((class slot-class) slot-name)
1284 (make-std-writer-method-function (class-name class) slot-name))
1286 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1287 (make-std-boundp-method-function (class-name class) slot-name))
1289 (defmethod compatible-meta-class-change-p (class proto-new-class)
1290 (eq (class-of class) (class-of proto-new-class)))
1292 (defmethod validate-superclass ((class class) (new-super class))
1293 (or (eq new-super *the-class-t*)
1294 (eq (class-of class) (class-of new-super))))
1296 (defmethod validate-superclass ((class standard-class) (new-super std-class))
1297 (let ((new-super-meta-class (class-of new-super)))
1298 (or (eq new-super-meta-class *the-class-std-class*)
1299 (eq (class-of class) new-super-meta-class))))
1301 ;;; What this does depends on which of the four possible values of
1302 ;;; LAYOUT-INVALID the PCL wrapper has; the simplest case is when it
1303 ;;; is (:FLUSH <wrapper>) or (:OBSOLETE <wrapper>), when there is
1304 ;;; nothing to do, as the new wrapper has already been created. If
1305 ;;; LAYOUT-INVALID returns NIL, then we invalidate it (setting it to
1306 ;;; (:FLUSH <wrapper>); UPDATE-SLOTS later gets to choose whether or
1307 ;;; not to "upgrade" this to (:OBSOLETE <wrapper>).
1309 ;;; This leaves the case where LAYOUT-INVALID returns T, which happens
1310 ;;; when REGISTER-LAYOUT has invalidated a superclass of CLASS (which
1311 ;;; invalidated all the subclasses in SB-KERNEL land). Again, here we
1312 ;;; must flush the caches and allow UPDATE-SLOTS to decide whether to
1313 ;;; obsolete the wrapper.
1315 ;;; FIXME: either here or in INVALID-WRAPPER-P looks like a good place
1316 ;;; for (AVER (NOT (EQ (LAYOUT-INVALID OWRAPPER)
1317 ;;; :UNINITIALIZED)))
1319 ;;; Thanks to Gerd Moellmann for the explanation. -- CSR, 2002-10-29
1320 (defun force-cache-flushes (class)
1321 (let* ((owrapper (class-wrapper class)))
1322 ;; We only need to do something if the wrapper is still valid. If
1323 ;; the wrapper isn't valid, state will be FLUSH or OBSOLETE, and
1324 ;; both of those will already be doing what we want. In
1325 ;; particular, we must be sure we never change an OBSOLETE into a
1326 ;; FLUSH since OBSOLETE means do what FLUSH does and then some.
1327 (when (or (not (invalid-wrapper-p owrapper))
1328 ;; KLUDGE: despite the observations above, this remains
1329 ;; a violation of locality or what might be considered
1330 ;; good style. There has to be a better way! -- CSR,
1332 (eq (layout-invalid owrapper) t))
1333 (let ((nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1335 (setf (wrapper-instance-slots-layout nwrapper)
1336 (wrapper-instance-slots-layout owrapper))
1337 (setf (wrapper-class-slots nwrapper)
1338 (wrapper-class-slots owrapper))
1340 (update-lisp-class-layout class nwrapper)
1341 (setf (slot-value class 'wrapper) nwrapper)
1342 (invalidate-wrapper owrapper :flush nwrapper))))))
1344 (defun flush-cache-trap (owrapper nwrapper instance)
1345 (declare (ignore owrapper))
1346 (set-wrapper instance nwrapper))
1348 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1349 ;;; the next access to the instance (as defined in 88-002R) to trap
1350 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1351 (defmethod make-instances-obsolete ((class std-class))
1352 (let* ((owrapper (class-wrapper class))
1353 (nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1355 (setf (wrapper-instance-slots-layout nwrapper)
1356 (wrapper-instance-slots-layout owrapper))
1357 (setf (wrapper-class-slots nwrapper)
1358 (wrapper-class-slots owrapper))
1360 (update-lisp-class-layout class nwrapper)
1361 (setf (slot-value class 'wrapper) nwrapper)
1362 (invalidate-wrapper owrapper :obsolete nwrapper)
1365 (defmethod make-instances-obsolete ((class symbol))
1366 (make-instances-obsolete (find-class class)))
1368 ;;; OBSOLETE-INSTANCE-TRAP is the internal trap that is called when we
1369 ;;; see an obsolete instance. The times when it is called are:
1370 ;;; - when the instance is involved in method lookup
1371 ;;; - when attempting to access a slot of an instance
1373 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1374 ;;; instance access macros.
1376 ;;; Of course these times when it is called are an internal
1377 ;;; implementation detail of PCL and are not part of the documented
1378 ;;; description of when the obsolete instance update happens. The
1379 ;;; documented description is as it appears in 88-002R.
1381 ;;; This has to return the new wrapper, so it counts on all the
1382 ;;; methods on obsolete-instance-trap-internal to return the new
1383 ;;; wrapper. It also does a little internal error checking to make
1384 ;;; sure that the traps are only happening when they should, and that
1385 ;;; the trap methods are computing appropriate new wrappers.
1387 ;;; OBSOLETE-INSTANCE-TRAP might be called on structure instances
1388 ;;; after a structure is redefined. In most cases,
1389 ;;; OBSOLETE-INSTANCE-TRAP will not be able to fix the old instance,
1390 ;;; so it must signal an error. The hard part of this is that the
1391 ;;; error system and debugger might cause OBSOLETE-INSTANCE-TRAP to be
1392 ;;; called again, so in that case, we have to return some reasonable
1393 ;;; wrapper, instead.
1395 (defvar *in-obsolete-instance-trap* nil)
1396 (defvar *the-wrapper-of-structure-object*
1397 (class-wrapper (find-class 'structure-object)))
1399 (define-condition obsolete-structure (error)
1400 ((datum :reader obsolete-structure-datum :initarg :datum))
1402 (lambda (condition stream)
1403 ;; Don't try to print the structure, since it probably won't work.
1405 "~@<obsolete structure error for a structure of type ~2I~_~S~:>"
1406 (type-of (obsolete-structure-datum condition))))))
1408 (defun obsolete-instance-trap (owrapper nwrapper instance)
1409 (if (not (pcl-instance-p instance))
1410 (if *in-obsolete-instance-trap*
1411 *the-wrapper-of-structure-object*
1412 (let ((*in-obsolete-instance-trap* t))
1413 (error 'obsolete-structure :datum instance)))
1414 (let* ((class (wrapper-class* nwrapper))
1415 (copy (allocate-instance class)) ;??? allocate-instance ???
1416 (olayout (wrapper-instance-slots-layout owrapper))
1417 (nlayout (wrapper-instance-slots-layout nwrapper))
1418 (oslots (get-slots instance))
1419 (nslots (get-slots copy))
1420 (oclass-slots (wrapper-class-slots owrapper))
1424 ;; local --> local transfer
1425 ;; local --> shared discard
1426 ;; local --> -- discard
1427 ;; shared --> local transfer
1428 ;; shared --> shared discard
1429 ;; shared --> -- discard
1433 ;; Go through all the old local slots.
1435 (dolist (name olayout)
1436 (let ((npos (posq name nlayout)))
1438 (setf (clos-slots-ref nslots npos)
1439 (clos-slots-ref oslots opos))
1441 (push name discarded)
1442 (unless (eq (clos-slots-ref oslots opos) +slot-unbound+)
1443 (setf (getf plist name) (clos-slots-ref oslots opos))))))
1446 ;; Go through all the old shared slots.
1447 (dolist (oclass-slot-and-val oclass-slots)
1448 (let ((name (car oclass-slot-and-val))
1449 (val (cdr oclass-slot-and-val)))
1450 (let ((npos (posq name nlayout)))
1452 (setf (clos-slots-ref nslots npos) (cdr oclass-slot-and-val))
1453 (progn (push name discarded)
1454 (unless (eq val +slot-unbound+)
1455 (setf (getf plist name) val)))))))
1457 ;; Go through all the new local slots to compute the added slots.
1458 (dolist (nlocal nlayout)
1459 (unless (or (memq nlocal olayout)
1460 (assq nlocal oclass-slots))
1461 (push nlocal added)))
1463 (swap-wrappers-and-slots instance copy)
1465 (update-instance-for-redefined-class instance
1471 (defun change-class-internal (instance new-class initargs)
1472 (let* ((old-class (class-of instance))
1473 (copy (allocate-instance new-class))
1474 (new-wrapper (get-wrapper copy))
1475 (old-wrapper (class-wrapper old-class))
1476 (old-layout (wrapper-instance-slots-layout old-wrapper))
1477 (new-layout (wrapper-instance-slots-layout new-wrapper))
1478 (old-slots (get-slots instance))
1479 (new-slots (get-slots copy))
1480 (old-class-slots (wrapper-class-slots old-wrapper)))
1482 ;; "The values of local slots specified by both the class CTO and
1483 ;; CFROM are retained. If such a local slot was unbound, it
1484 ;; remains unbound."
1485 (let ((new-position 0))
1486 (dolist (new-slot new-layout)
1487 (let ((old-position (posq new-slot old-layout)))
1489 (setf (clos-slots-ref new-slots new-position)
1490 (clos-slots-ref old-slots old-position))))
1491 (incf new-position)))
1493 ;; "The values of slots specified as shared in the class CFROM and
1494 ;; as local in the class CTO are retained."
1495 (dolist (slot-and-val old-class-slots)
1496 (let ((position (posq (car slot-and-val) new-layout)))
1498 (setf (clos-slots-ref new-slots position) (cdr slot-and-val)))))
1500 ;; Make the copy point to the old instance's storage, and make the
1501 ;; old instance point to the new storage.
1502 (swap-wrappers-and-slots instance copy)
1504 (apply #'update-instance-for-different-class copy instance initargs)
1507 (defmethod change-class ((instance standard-object)
1508 (new-class standard-class)
1510 (change-class-internal instance new-class initargs))
1512 (defmethod change-class ((instance funcallable-standard-object)
1513 (new-class funcallable-standard-class)
1515 (change-class-internal instance new-class initargs))
1517 (defmethod change-class ((instance standard-object)
1518 (new-class funcallable-standard-class)
1520 (declare (ignore initargs))
1521 (error "You can't change the class of ~S to ~S~@
1522 because it isn't already an instance with metaclass ~S."
1523 instance new-class 'standard-class))
1525 (defmethod change-class ((instance funcallable-standard-object)
1526 (new-class standard-class)
1528 (declare (ignore initargs))
1529 (error "You can't change the class of ~S to ~S~@
1530 because it isn't already an instance with metaclass ~S."
1531 instance new-class 'funcallable-standard-class))
1533 (defmethod change-class ((instance t) (new-class-name symbol) &rest initargs)
1534 (apply #'change-class instance (find-class new-class-name) initargs))
1536 ;;;; The metaclass BUILT-IN-CLASS
1538 ;;;; This metaclass is something of a weird creature. By this point, all
1539 ;;;; instances of it which will exist have been created, and no instance
1540 ;;;; is ever created by calling MAKE-INSTANCE.
1542 ;;;; But, there are other parts of the protocol we must follow and those
1543 ;;;; definitions appear here.
1545 (defmethod shared-initialize :before
1546 ((class built-in-class) slot-names &rest initargs)
1547 (declare (ignore slot-names initargs))
1548 (error "attempt to initialize or reinitialize a built in class"))
1550 (defmethod class-direct-slots ((class built-in-class)) ())
1551 (defmethod class-slots ((class built-in-class)) ())
1552 (defmethod class-direct-default-initargs ((class built-in-class)) ())
1553 (defmethod class-default-initargs ((class built-in-class)) ())
1555 (defmethod validate-superclass ((c class) (s built-in-class))
1556 (or (eq s *the-class-t*)
1557 (eq s *the-class-stream*)))
1559 ;;; Some necessary methods for FORWARD-REFERENCED-CLASS
1560 (defmethod class-direct-slots ((class forward-referenced-class)) ())
1561 (defmethod class-direct-default-initargs ((class forward-referenced-class)) ())
1562 (macrolet ((def (method)
1563 `(defmethod ,method ((class forward-referenced-class))
1564 (error "~@<~I~S was called on a forward referenced class:~2I~_~S~:>"
1566 (def class-default-initargs)
1567 (def class-precedence-list)
1570 (defmethod validate-superclass ((c slot-class)
1571 (f forward-referenced-class))
1574 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1575 (pushnew dependent (plist-value metaobject 'dependents)))
1577 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1578 (setf (plist-value metaobject 'dependents)
1579 (delete dependent (plist-value metaobject 'dependents))))
1581 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1582 (dolist (dependent (plist-value metaobject 'dependents))
1583 (funcall function dependent)))