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 ;;;; various class accessors that are a little more complicated than can be
122 ;;;; done with automatically generated reader methods
124 (defmethod class-prototype :before (class)
125 (unless (class-finalized-p class)
126 (error "~S not yet finalized, cannot allocate a prototype." class)))
128 ;;; KLUDGE: For some reason factoring the common body into a function
129 ;;; breaks PCL bootstrapping, so just generate it with a macrolet for
131 (macrolet ((def (class)
132 `(defmethod class-prototype ((class ,class))
133 (with-slots (prototype) class
135 (setf prototype (allocate-instance class)))))))
137 (def condition-class)
138 (def structure-class))
140 (defmethod class-direct-default-initargs ((class slot-class))
141 (plist-value class 'direct-default-initargs))
143 (defmethod class-default-initargs ((class slot-class))
144 (plist-value class 'default-initargs))
146 (defmethod class-slot-cells ((class std-class))
147 (plist-value class 'class-slot-cells))
148 (defmethod (setf class-slot-cells) (new-value (class std-class))
149 (setf (plist-value class 'class-slot-cells) new-value))
151 ;;;; class accessors that are even a little bit more complicated than those
152 ;;;; above. These have a protocol for updating them, we must implement that
155 ;;; Maintaining the direct subclasses backpointers. The update methods are
156 ;;; here, the values are read by an automatically generated reader method.
157 (defmethod add-direct-subclass ((class class) (subclass class))
158 (with-slots (direct-subclasses) class
159 (pushnew subclass direct-subclasses)
161 (defmethod remove-direct-subclass ((class class) (subclass class))
162 (with-slots (direct-subclasses) class
163 (setq direct-subclasses (remove subclass direct-subclasses))
166 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
168 ;;; There are four generic functions involved, each has one method for the
169 ;;; class case and another method for the damned EQL specializers. All of
170 ;;; these are specified methods and appear in their specified place in the
173 ;;; ADD-DIRECT-METHOD
174 ;;; REMOVE-DIRECT-METHOD
175 ;;; SPECIALIZER-DIRECT-METHODS
176 ;;; SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
178 ;;; In each case, we maintain one value which is a cons. The car is the list
179 ;;; methods. The cdr is a list of the generic functions. The cdr is always
181 (defmethod add-direct-method ((specializer class) (method method))
182 (with-slots (direct-methods) specializer
183 (setf (car direct-methods) (adjoin method (car direct-methods)) ;PUSH
184 (cdr direct-methods) ()))
186 (defmethod remove-direct-method ((specializer class) (method method))
187 (with-slots (direct-methods) specializer
188 (setf (car direct-methods) (remove method (car direct-methods))
189 (cdr direct-methods) ()))
192 (defmethod specializer-direct-methods ((specializer class))
193 (with-slots (direct-methods) specializer
194 (car direct-methods)))
196 (defmethod specializer-direct-generic-functions ((specializer class))
197 (with-slots (direct-methods) specializer
198 (or (cdr direct-methods)
199 (setf (cdr direct-methods)
201 (dolist (m (car direct-methods))
202 ;; the old PCL code used COLLECTING-ONCE which used
203 ;; #'EQ to check for newness
204 (pushnew (method-generic-function m) collect :test #'eq))
205 (nreverse collect))))))
207 ;;; This hash table is used to store the direct methods and direct generic
208 ;;; functions of EQL specializers. Each value in the table is the cons.
209 (defvar *eql-specializer-methods* (make-hash-table :test 'eql))
210 (defvar *class-eq-specializer-methods* (make-hash-table :test 'eq))
212 (defmethod specializer-method-table ((specializer eql-specializer))
213 *eql-specializer-methods*)
215 (defmethod specializer-method-table ((specializer class-eq-specializer))
216 *class-eq-specializer-methods*)
218 (defmethod add-direct-method ((specializer specializer-with-object)
220 (let* ((object (specializer-object specializer))
221 (table (specializer-method-table specializer))
222 (entry (gethash object table)))
225 (setf (gethash object table)
227 (setf (car entry) (adjoin method (car entry))
231 (defmethod remove-direct-method ((specializer specializer-with-object)
233 (let* ((object (specializer-object specializer))
234 (entry (gethash object (specializer-method-table specializer))))
236 (setf (car entry) (remove method (car entry))
240 (defmethod specializer-direct-methods ((specializer specializer-with-object))
241 (car (gethash (specializer-object specializer)
242 (specializer-method-table specializer))))
244 (defmethod specializer-direct-generic-functions ((specializer
245 specializer-with-object))
246 (let* ((object (specializer-object specializer))
247 (entry (gethash object (specializer-method-table specializer))))
252 (dolist (m (car entry))
253 (pushnew (method-generic-function m) collect :test #'eq))
254 (nreverse collect)))))))
256 (defun map-specializers (function)
257 (map-all-classes (lambda (class)
258 (funcall function (class-eq-specializer class))
259 (funcall function class)))
260 (maphash (lambda (object methods)
261 (declare (ignore methods))
262 (intern-eql-specializer object))
263 *eql-specializer-methods*)
264 (maphash (lambda (object specl)
265 (declare (ignore object))
266 (funcall function specl))
267 *eql-specializer-table*)
270 (defun map-all-generic-functions (function)
271 (let ((all-generic-functions (make-hash-table :test 'eq)))
272 (map-specializers (lambda (specl)
273 (dolist (gf (specializer-direct-generic-functions
275 (unless (gethash gf all-generic-functions)
276 (setf (gethash gf all-generic-functions) t)
277 (funcall function gf))))))
280 (defmethod shared-initialize :after ((specl class-eq-specializer)
283 (declare (ignore slot-names))
284 (setf (slot-value specl 'type) `(class-eq ,(specializer-class specl))))
286 (defmethod shared-initialize :after ((specl eql-specializer) slot-names &key)
287 (declare (ignore slot-names))
288 (setf (slot-value specl 'type)
289 `(eql ,(specializer-object specl)))
290 (setf (info :type :translator specl)
291 (constantly (make-member-type :members (list (specializer-object specl))))))
293 (defun real-load-defclass (name metaclass-name supers slots other
294 readers writers slot-names)
295 (with-single-package-locked-error (:symbol name "defining ~S as a class")
296 (%compiler-defclass name readers writers slot-names)
297 (let ((res (apply #'ensure-class name :metaclass metaclass-name
298 :direct-superclasses supers
300 :definition-source `((defclass ,name)
305 (setf (gdefinition 'load-defclass) #'real-load-defclass)
307 (defun ensure-class (name &rest args)
308 (apply #'ensure-class-using-class
309 (let ((class (find-class name nil)))
310 (when (and class (eq name (class-name class)))
311 ;; NAME is the proper name of CLASS, so redefine it
316 (defmethod ensure-class-using-class ((class null) name &rest args &key)
317 (multiple-value-bind (meta initargs)
318 (ensure-class-values class args)
319 (set-class-type-translation (class-prototype meta) name)
320 (setf class (apply #'make-instance meta :name name initargs))
321 (without-package-locks
322 (setf (find-class name) class))
323 (set-class-type-translation class name)
326 (defmethod ensure-class-using-class ((class pcl-class) name &rest args &key)
327 (multiple-value-bind (meta initargs)
328 (ensure-class-values class args)
329 (unless (eq (class-of class) meta)
330 (apply #'change-class class meta initargs))
331 (apply #'reinitialize-instance class initargs)
332 (without-package-locks
333 (setf (find-class name) class))
334 (set-class-type-translation class name)
337 (defmethod class-predicate-name ((class t))
342 ((not (legal-class-name-p s))
343 (error "~S is not a class or a legal class name." s))
345 (or (find-class s nil)
346 (make-instance 'forward-referenced-class
349 (defun ensure-class-values (class initargs)
350 (let (metaclass metaclassp reversed-plist)
351 (doplist (key val) initargs
352 (cond ((eq key :metaclass)
356 (when (eq key :direct-superclasses)
357 (setf val (mapcar #'fix-super val)))
358 (setf reversed-plist (list* val key reversed-plist)))))
359 (values (cond (metaclassp
360 (if (classp metaclass)
362 (find-class metaclass)))
363 ((or (null class) (forward-referenced-class-p class))
364 *the-class-standard-class*)
367 (nreverse reversed-plist))))
370 (defmethod shared-initialize :after
373 &key (direct-superclasses nil direct-superclasses-p)
374 (direct-slots nil direct-slots-p)
375 (direct-default-initargs nil direct-default-initargs-p)
376 (predicate-name nil predicate-name-p))
377 (cond (direct-superclasses-p
378 (setq direct-superclasses
379 (or direct-superclasses
380 (list (if (funcallable-standard-class-p class)
381 *the-class-funcallable-standard-object*
382 *the-class-standard-object*))))
383 (dolist (superclass direct-superclasses)
384 (unless (validate-superclass class superclass)
385 (error "The class ~S was specified as a~%
386 super-class of the class ~S;~%~
387 but the meta-classes ~S and~%~S are incompatible.~@
388 Define a method for ~S to avoid this error."
389 superclass class (class-of superclass) (class-of class)
390 'validate-superclass)))
391 (setf (slot-value class 'direct-superclasses) direct-superclasses))
393 (setq direct-superclasses (slot-value class 'direct-superclasses))))
396 (setf (slot-value class 'direct-slots)
397 (mapcar (lambda (pl) (make-direct-slotd class pl))
399 (slot-value class 'direct-slots)))
400 (if direct-default-initargs-p
401 (setf (plist-value class 'direct-default-initargs)
402 direct-default-initargs)
403 (setq direct-default-initargs
404 (plist-value class 'direct-default-initargs)))
405 (setf (plist-value class 'class-slot-cells)
406 (let ((old-class-slot-cells (plist-value class 'class-slot-cells))
408 (dolist (dslotd direct-slots)
409 (when (eq :class (slot-definition-allocation dslotd))
411 (let* ((name (slot-definition-name dslotd))
412 (old (assoc name old-class-slot-cells)))
415 (member name slot-names))
416 (let* ((initfunction (slot-definition-initfunction dslotd))
417 (value (if initfunction
418 (funcall initfunction)
420 (push (cons name value) collect))
421 (push old collect)))))
423 (setq predicate-name (if predicate-name-p
424 (setf (slot-value class 'predicate-name)
425 (car predicate-name))
426 (or (slot-value class 'predicate-name)
427 (setf (slot-value class 'predicate-name)
428 (make-class-predicate-name (class-name
430 (add-direct-subclasses class direct-superclasses)
431 (make-class-predicate class predicate-name)
432 (update-class class nil)
433 (do* ((slots (slot-value class 'slots) (cdr slots))
435 ((null slots) (when dupes
437 ;; FIXME: the indentation request ("~4I")
438 ;; below appears not to do anything. Finding
439 ;; out why would be nice. -- CSR, 2003-04-24
440 "~@<slot names with the same SYMBOL-NAME but ~
441 different SYMBOL-PACKAGE (possible package problem) ~
442 for class ~S:~@:_~4I~<~@{~S~^~:@_~}~:>~@:>"
445 (let* ((slot (car slots))
446 (oslots (remove (slot-definition-name slot) (cdr slots)
447 :test #'string/= :key #'slot-definition-name)))
449 (pushnew (cons (slot-definition-name slot)
450 (mapcar #'slot-definition-name oslots))
452 :test #'string= :key #'car))))
453 (add-slot-accessors class direct-slots)
454 (make-preliminary-layout class))
456 (defmethod shared-initialize :after ((class forward-referenced-class)
457 slot-names &key &allow-other-keys)
458 (declare (ignore slot-names))
459 (make-preliminary-layout class))
461 (defvar *allow-forward-referenced-classes-in-cpl-p* nil)
463 ;;; Give CLASS a preliminary layout if it doesn't have one already, to
464 ;;; make it known to the type system.
465 (defun make-preliminary-layout (class)
466 (flet ((compute-preliminary-cpl (root)
467 (let ((*allow-forward-referenced-classes-in-cpl-p* t))
468 (compute-class-precedence-list root))))
469 (without-package-locks
470 (unless (class-finalized-p class)
471 (let ((name (class-name class)))
472 (setf (find-class name) class)
473 ;; KLUDGE: This is fairly horrible. We need to make a
474 ;; full-fledged CLASSOID here, not just tell the compiler that
475 ;; some class is forthcoming, because there are legitimate
476 ;; questions one can ask of the type system, implemented in
477 ;; terms of CLASSOIDs, involving forward-referenced classes. So.
478 (when (and (eq *boot-state* 'complete)
479 (null (find-classoid name nil)))
480 (setf (find-classoid name)
481 (make-standard-classoid :name name)))
482 (set-class-type-translation class name)
483 (let ((layout (make-wrapper 0 class))
484 (classoid (find-classoid name)))
485 (setf (layout-classoid layout) classoid)
486 (setf (classoid-pcl-class classoid) class)
487 (setf (slot-value class 'wrapper) layout)
488 (let ((cpl (compute-preliminary-cpl class)))
489 (setf (layout-inherits layout)
490 (order-layout-inherits
491 (map 'simple-vector #'class-wrapper
492 (reverse (rest cpl))))))
493 (register-layout layout :invalidate t)
494 (setf (classoid-layout classoid) layout)
495 (mapc #'make-preliminary-layout (class-direct-subclasses class))))))))
498 (defmethod shared-initialize :before ((class class) slot-names &key name)
499 (declare (ignore slot-names name))
500 ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
501 ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
502 (setf (slot-value class 'type) `(class ,class))
503 (setf (slot-value class 'class-eq-specializer)
504 (make-instance 'class-eq-specializer :class class)))
506 (defmethod reinitialize-instance :before ((class slot-class) &key direct-superclasses)
507 (dolist (old-super (set-difference (class-direct-superclasses class) direct-superclasses))
508 (remove-direct-subclass old-super class))
509 (remove-slot-accessors class (class-direct-slots class)))
511 (defmethod reinitialize-instance :after ((class slot-class)
514 (map-dependents class
516 (apply #'update-dependent class dependent initargs))))
518 (defmethod shared-initialize :after ((class condition-class) slot-names
519 &key direct-slots direct-superclasses)
520 (declare (ignore slot-names))
521 (let ((classoid (find-classoid (class-name class))))
522 (with-slots (wrapper class-precedence-list cpl-available-p
523 prototype predicate-name
524 (direct-supers direct-superclasses))
526 (setf (slot-value class 'direct-slots)
527 (mapcar (lambda (pl) (make-direct-slotd class pl))
529 (setf (slot-value class 'finalized-p) t)
530 (setf (classoid-pcl-class classoid) class)
531 (setq direct-supers direct-superclasses)
532 (setq wrapper (classoid-layout classoid))
533 (setq class-precedence-list (compute-class-precedence-list class))
534 (setq cpl-available-p t)
535 (add-direct-subclasses class direct-superclasses)
536 (setq predicate-name (make-class-predicate-name (class-name class)))
537 (make-class-predicate class predicate-name)
538 (setf (slot-value class 'slots) (compute-slots class))))
539 ;; Comment from Gerd's PCL, 2003-05-15:
541 ;; We don't ADD-SLOT-ACCESSORS here because we don't want to
542 ;; override condition accessors with generic functions. We do this
544 (update-pv-table-cache-info class))
546 (defmethod direct-slot-definition-class ((class condition-class)
548 (declare (ignore initargs))
549 (find-class 'condition-direct-slot-definition))
551 (defmethod effective-slot-definition-class ((class condition-class)
553 (declare (ignore initargs))
554 (find-class 'condition-effective-slot-definition))
556 (defmethod finalize-inheritance ((class condition-class))
557 (aver (slot-value class 'finalized-p))
560 (defmethod compute-effective-slot-definition
561 ((class condition-class) slot-name dslotds)
562 (let ((slotd (call-next-method)))
563 (setf (slot-definition-reader-function slotd)
565 (handler-case (condition-reader-function x slot-name)
566 ;; FIXME: FIND-SLOT-DEFAULT throws an error if the slot
567 ;; is unbound; maybe it should be a CELL-ERROR of some
569 (error () (values (slot-unbound class x slot-name))))))
570 (setf (slot-definition-writer-function slotd)
572 (condition-writer-function x v slot-name)))
573 (setf (slot-definition-boundp-function slotd)
575 (multiple-value-bind (v c)
576 (ignore-errors (condition-reader-function x slot-name))
581 (defmethod compute-slots ((class condition-class))
582 (mapcan (lambda (superclass)
583 (mapcar (lambda (dslotd)
584 (compute-effective-slot-definition
585 class (slot-definition-name dslotd) (list dslotd)))
586 (class-direct-slots superclass)))
587 (reverse (slot-value class 'class-precedence-list))))
589 (defmethod compute-slots :around ((class condition-class))
590 (let ((eslotds (call-next-method)))
591 (mapc #'initialize-internal-slot-functions eslotds)
594 (defmethod shared-initialize :after
595 ((slotd structure-slot-definition) slot-names &key
596 (allocation :instance) allocation-class)
597 (declare (ignore slot-names allocation-class))
598 (unless (eq allocation :instance)
599 (error "Structure slots must have :INSTANCE allocation.")))
601 (defun make-structure-class-defstruct-form (name direct-slots include)
602 (let* ((conc-name (format-symbol *package* "~S structure class " name))
603 (constructor (format-symbol *package* "~Aconstructor" conc-name))
604 (defstruct `(defstruct (,name
606 `((:include ,(class-name include))))
608 (:conc-name ,conc-name)
609 (:constructor ,constructor ())
611 ,@(mapcar (lambda (slot)
612 `(,(slot-definition-name slot)
615 (reader-names (mapcar (lambda (slotd)
616 (list 'slot-accessor name
617 (slot-definition-name slotd)
620 (writer-names (mapcar (lambda (slotd)
621 (list 'slot-accessor name
622 (slot-definition-name slotd)
626 (mapcar (lambda (slotd reader-name)
628 (slot-definition-defstruct-accessor-symbol
630 `(defun ,reader-name (obj)
631 (declare (type ,name obj))
633 direct-slots reader-names))
635 (mapcar (lambda (slotd writer-name)
637 (slot-definition-defstruct-accessor-symbol
639 `(defun ,writer-name (nv obj)
640 (declare (type ,name obj))
641 (setf (,accessor obj) nv))))
642 direct-slots writer-names))
646 ,@readers-init ,@writers-init
648 (values defstruct-form constructor reader-names writer-names)))
650 (defun make-defstruct-allocation-function (class)
651 (let ((dd (get-structure-dd (class-name class))))
653 (let ((instance (%make-instance (dd-length dd)))
654 (raw-index (dd-raw-index dd)))
655 (setf (%instance-layout instance)
656 (sb-kernel::compiler-layout-or-lose (dd-name dd)))
658 (setf (%instance-ref instance raw-index)
659 (make-array (dd-raw-length dd)
660 :element-type '(unsigned-byte 32))))
663 (defmethod shared-initialize :after
664 ((class structure-class)
666 &key (direct-superclasses nil direct-superclasses-p)
667 (direct-slots nil direct-slots-p)
668 direct-default-initargs
669 (predicate-name nil predicate-name-p))
670 (declare (ignore slot-names direct-default-initargs))
671 (if direct-superclasses-p
672 (setf (slot-value class 'direct-superclasses)
673 (or direct-superclasses
674 (setq direct-superclasses
675 (and (not (eq (class-name class) 'structure-object))
676 (list *the-class-structure-object*)))))
677 (setq direct-superclasses (slot-value class 'direct-superclasses)))
678 (let* ((name (class-name class))
679 (from-defclass-p (slot-value class 'from-defclass-p))
680 (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
682 (setf (slot-value class 'direct-slots)
686 (let* ((slot-name (getf pl :name))
688 (format-symbol *package*
689 "~S structure class ~A"
691 (setq pl (list* :defstruct-accessor-symbol
693 (make-direct-slotd class pl))
695 (setq direct-slots (slot-value class 'direct-slots)))
697 (let ((include (car (slot-value class 'direct-superclasses))))
698 (multiple-value-bind (defstruct-form constructor reader-names writer-names)
699 (make-structure-class-defstruct-form name direct-slots include)
700 (unless (structure-type-p name) (eval defstruct-form))
701 (mapc (lambda (dslotd reader-name writer-name)
702 (let* ((reader (gdefinition reader-name))
703 (writer (when (gboundp writer-name)
704 (gdefinition writer-name))))
705 (setf (slot-value dslotd 'internal-reader-function)
707 (setf (slot-value dslotd 'internal-writer-function)
709 direct-slots reader-names writer-names)
710 (setf (slot-value class 'defstruct-form) defstruct-form)
711 (setf (slot-value class 'defstruct-constructor) constructor)))
712 (setf (slot-value class 'defstruct-constructor)
713 (make-defstruct-allocation-function class)))
714 (add-direct-subclasses class direct-superclasses)
715 (setf (slot-value class 'class-precedence-list)
716 (compute-class-precedence-list class))
717 (setf (slot-value class 'cpl-available-p) t)
718 (setf (slot-value class 'slots) (compute-slots class))
719 (let ((lclass (find-classoid (class-name class))))
720 (setf (classoid-pcl-class lclass) class)
721 (setf (slot-value class 'wrapper) (classoid-layout lclass)))
722 (setf (slot-value class 'finalized-p) t)
723 (update-pv-table-cache-info class)
724 (setq predicate-name (if predicate-name-p
725 (setf (slot-value class 'predicate-name)
726 (car predicate-name))
727 (or (slot-value class 'predicate-name)
728 (setf (slot-value class 'predicate-name)
729 (make-class-predicate-name
730 (class-name class))))))
731 (make-class-predicate class predicate-name)
732 (add-slot-accessors class direct-slots)))
734 (defmethod direct-slot-definition-class ((class structure-class) &rest initargs)
735 (declare (ignore initargs))
736 (find-class 'structure-direct-slot-definition))
738 (defmethod finalize-inheritance ((class structure-class))
739 nil) ; always finalized
741 (defun add-slot-accessors (class dslotds)
742 (fix-slot-accessors class dslotds 'add))
744 (defun remove-slot-accessors (class dslotds)
745 (fix-slot-accessors class dslotds 'remove))
747 (defun fix-slot-accessors (class dslotds add/remove)
748 (flet ((fix (gfspec name r/w)
749 (let ((gf (cond ((eq add/remove 'add)
751 (without-package-locks
752 (ensure-generic-function gfspec))
753 (ensure-generic-function
754 gfspec :lambda-list (case r/w
756 (w '(new-value object))))))
757 ((generic-function-p (and (fboundp gfspec)
758 (fdefinition gfspec)))
759 (without-package-locks
760 (ensure-generic-function gfspec))))))
763 (r (if (eq add/remove 'add)
764 (add-reader-method class gf name)
765 (remove-reader-method class gf)))
766 (w (if (eq add/remove 'add)
767 (add-writer-method class gf name)
768 (remove-writer-method class gf))))))))
769 (dolist (dslotd dslotds)
770 (let ((slot-name (slot-definition-name dslotd)))
771 (dolist (r (slot-definition-readers dslotd))
772 (fix r slot-name 'r))
773 (dolist (w (slot-definition-writers dslotd))
774 (fix w slot-name 'w))))))
776 (defun add-direct-subclasses (class supers)
777 (dolist (super supers)
778 (unless (memq class (class-direct-subclasses class))
779 (add-direct-subclass super class))))
781 (defmethod finalize-inheritance ((class std-class))
782 (update-class class t))
784 (defmethod finalize-inheritance ((class forward-referenced-class))
785 ;; FIXME: should we not be thinking a bit about what kinds of error
786 ;; we're throwing? Maybe we need a clos-error type to mix in? Or
787 ;; possibly a forward-referenced-class-error, though that's
788 ;; difficult given e.g. class precedence list calculations...
790 "~@<FINALIZE-INHERITANCE was called on a forward referenced class:~
795 (defun class-has-a-forward-referenced-superclass-p (class)
796 (or (forward-referenced-class-p class)
797 (some #'class-has-a-forward-referenced-superclass-p
798 (class-direct-superclasses class))))
800 ;;; This is called by :after shared-initialize whenever a class is initialized
801 ;;; or reinitialized. The class may or may not be finalized.
802 (defun update-class (class finalizep)
803 ;; Comment from Gerd Moellmann:
805 ;; Note that we can't simply delay the finalization when CLASS has
806 ;; no forward referenced superclasses because that causes bootstrap
808 (without-package-locks
809 (when (and (not finalizep)
810 (not (class-finalized-p class))
811 (not (class-has-a-forward-referenced-superclass-p class)))
812 (finalize-inheritance class)
813 (return-from update-class))
814 (when (or finalizep (class-finalized-p class)
815 (not (class-has-a-forward-referenced-superclass-p class)))
816 (setf (find-class (class-name class)) class)
817 (update-cpl class (compute-class-precedence-list class))
818 ;; This invocation of UPDATE-SLOTS, in practice, finalizes the
819 ;; class. The hoops above are to ensure that FINALIZE-INHERITANCE
820 ;; is called at finalization, so that MOP programmers can hook
821 ;; into the system as described in "Class Finalization Protocol"
822 ;; (section 5.5.2 of AMOP).
823 (update-slots class (compute-slots class))
824 (update-gfs-of-class class)
825 (update-initargs class (compute-default-initargs class))
826 (update-ctors 'finalize-inheritance :class class))
828 (dolist (sub (class-direct-subclasses class)) (update-class sub nil)))))
830 (defun update-cpl (class cpl)
831 (if (class-finalized-p class)
832 (unless (and (equal (class-precedence-list class) cpl)
834 (when (position :class (class-direct-slots c)
835 :key #'slot-definition-allocation)
837 ;; comment from the old CMU CL sources:
838 ;; Need to have the cpl setup before update-lisp-class-layout
839 ;; is called on CMU CL.
840 (setf (slot-value class 'class-precedence-list) cpl)
841 (setf (slot-value class 'cpl-available-p) t)
842 (force-cache-flushes class))
844 (setf (slot-value class 'class-precedence-list) cpl)
845 (setf (slot-value class 'cpl-available-p) t)))
846 (update-class-can-precede-p cpl))
848 (defun update-class-can-precede-p (cpl)
850 (let ((first (car cpl)))
851 (dolist (c (cdr cpl))
852 (pushnew c (slot-value first 'can-precede-list))))
853 (update-class-can-precede-p (cdr cpl))))
855 (defun class-can-precede-p (class1 class2)
856 (member class2 (class-can-precede-list class1)))
858 (defun update-slots (class eslotds)
859 (let ((instance-slots ())
861 (dolist (eslotd eslotds)
862 (let ((alloc (slot-definition-allocation eslotd)))
864 (:instance (push eslotd instance-slots))
865 (:class (push eslotd class-slots)))))
867 ;; If there is a change in the shape of the instances then the
868 ;; old class is now obsolete.
869 (let* ((nlayout (mapcar #'slot-definition-name
870 (sort instance-slots #'<
871 :key #'slot-definition-location)))
872 (nslots (length nlayout))
873 (nwrapper-class-slots (compute-class-slots class-slots))
874 (owrapper (when (class-finalized-p class)
875 (class-wrapper class)))
876 (olayout (when owrapper
877 (wrapper-instance-slots-layout owrapper)))
878 (owrapper-class-slots (and owrapper (wrapper-class-slots owrapper)))
880 (cond ((null owrapper)
881 (make-wrapper nslots class))
882 ((and (equal nlayout olayout)
884 (loop for o in owrapper-class-slots
885 for n in nwrapper-class-slots
886 do (unless (eq (car o) (car n)) (return t)))))
889 ;; This will initialize the new wrapper to have the
890 ;; same state as the old wrapper. We will then have
891 ;; to change that. This may seem like wasted work
892 ;; (and it is), but the spec requires that we call
893 ;; MAKE-INSTANCES-OBSOLETE.
894 (make-instances-obsolete class)
895 (class-wrapper class)))))
897 (with-slots (wrapper slots) class
898 (update-lisp-class-layout class nwrapper)
900 (wrapper-instance-slots-layout nwrapper) nlayout
901 (wrapper-class-slots nwrapper) nwrapper-class-slots
902 (wrapper-no-of-instance-slots nwrapper) nslots
904 (setf (slot-value class 'finalized-p) t)
905 (unless (eq owrapper nwrapper)
906 (update-pv-table-cache-info class)
907 (maybe-update-standard-class-locations class)))))
909 (defun compute-class-slots (eslotds)
911 (dolist (eslotd eslotds)
912 (push (assoc (slot-definition-name eslotd)
913 (class-slot-cells (slot-definition-class eslotd)))
917 (defun update-gfs-of-class (class)
918 (when (and (class-finalized-p class)
919 (let ((cpl (class-precedence-list class)))
920 (or (member *the-class-slot-class* cpl)
921 (member *the-class-standard-effective-slot-definition*
923 (let ((gf-table (make-hash-table :test 'eq)))
924 (labels ((collect-gfs (class)
925 (dolist (gf (specializer-direct-generic-functions class))
926 (setf (gethash gf gf-table) t))
927 (mapc #'collect-gfs (class-direct-superclasses class))))
929 (maphash (lambda (gf ignore)
930 (declare (ignore ignore))
931 (update-gf-dfun class gf))
934 (defun update-initargs (class inits)
935 (setf (plist-value class 'default-initargs) inits))
937 (defmethod compute-default-initargs ((class slot-class))
938 (let ((initargs (loop for c in (class-precedence-list class)
939 append (class-direct-default-initargs c))))
940 (delete-duplicates initargs :test #'eq :key #'car :from-end t)))
942 ;;;; protocols for constructing direct and effective slot definitions
944 (defmethod direct-slot-definition-class ((class std-class) &rest initargs)
945 (declare (ignore initargs))
946 (find-class 'standard-direct-slot-definition))
948 (defun make-direct-slotd (class initargs)
949 (apply #'make-instance
950 (apply #'direct-slot-definition-class class initargs)
954 ;;; I (CSR) am not sure, but I believe that the particular order of
955 ;;; slots is quite important: it is ideal to attempt to have a
956 ;;; constant slot location for the same notional slots as much as
957 ;;; possible, so that clever discriminating functions (ONE-INDEX et
958 ;;; al.) have a chance of working. The below at least walks through
959 ;;; the slots predictably, but maybe it would be good to compute some
960 ;;; kind of optimal slot layout by looking at locations of slots in
962 (defmethod compute-slots ((class std-class))
963 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
964 ;; for each different slot name we find in our superclasses. Each
965 ;; call receives the class and a list of the dslotds with that name.
966 ;; The list is in most-specific-first order.
967 (let ((name-dslotds-alist ()))
968 (dolist (c (reverse (class-precedence-list class)))
969 (dolist (slot (class-direct-slots c))
970 (let* ((name (slot-definition-name slot))
971 (entry (assq name name-dslotds-alist)))
973 (push slot (cdr entry))
974 (push (list name slot) name-dslotds-alist)))))
975 (mapcar (lambda (direct)
976 (compute-effective-slot-definition class
979 (nreverse name-dslotds-alist))))
981 (defmethod compute-slots ((class standard-class))
984 (defmethod compute-slots :around ((class standard-class))
985 (let ((eslotds (call-next-method))
987 (dolist (eslotd eslotds eslotds)
988 (setf (slot-definition-location eslotd)
989 (case (slot-definition-allocation eslotd)
993 (let* ((name (slot-definition-name eslotd))
996 (slot-definition-allocation-class eslotd)
997 ;; we get here if the user adds an extra slot
999 (setf (slot-definition-allocation-class eslotd)
1001 ;; which raises the question of what we should
1002 ;; do if we find that said user has added a slot
1003 ;; with the same name as another slot...
1004 (cell (or (assq name (class-slot-cells from-class))
1005 (setf (class-slot-cells from-class)
1006 (cons (cons name +slot-unbound+)
1007 (class-slot-cells from-class))))))
1009 (if (eq +slot-unbound+ (cdr cell))
1010 ;; We may have inherited an initfunction
1011 (let ((initfun (slot-definition-initfunction eslotd)))
1013 (rplacd cell (funcall initfun))
1016 (unless (slot-definition-class eslotd)
1017 (setf (slot-definition-class eslotd) class))
1018 (initialize-internal-slot-functions eslotd))))
1020 (defmethod compute-slots ((class funcallable-standard-class))
1023 (defmethod compute-slots :around ((class funcallable-standard-class))
1024 (labels ((instance-slot-names (slotds)
1026 (dolist (slotd slotds (nreverse collect))
1027 (when (eq (slot-definition-allocation slotd) :instance)
1028 (push (slot-definition-name slotd) collect)))))
1029 ;; This sorts slots so that slots of classes later in the CPL
1030 ;; come before slots of other classes. This is crucial for
1031 ;; funcallable instances because it ensures that the slots of
1032 ;; FUNCALLABLE-STANDARD-OBJECT, which includes the slots of
1033 ;; KERNEL:FUNCALLABLE-INSTANCE, come first, which in turn
1034 ;; makes it possible to treat FUNCALLABLE-STANDARD-OBJECT as
1035 ;; a funcallable instance.
1036 (compute-layout (eslotds)
1038 (names (instance-slot-names eslotds)))
1040 (reverse (class-precedence-list class))
1041 (nreverse (nconc names first)))
1042 (dolist (ss (class-slots class))
1043 (let ((name (slot-definition-name ss)))
1044 (when (member name names)
1046 (setq names (delete name names)))))))))
1047 (let ((all-slotds (call-next-method))
1050 (dolist (slotd all-slotds)
1051 (case (slot-definition-allocation slotd)
1052 (:instance (push slotd instance-slots))
1053 (:class (push slotd class-slots))))
1054 (let ((layout (compute-layout instance-slots)))
1055 (dolist (slotd instance-slots)
1056 (setf (slot-definition-location slotd)
1057 (position (slot-definition-name slotd) layout))
1058 (initialize-internal-slot-functions slotd)))
1059 (dolist (slotd class-slots)
1060 (let ((name (slot-definition-name slotd))
1061 (from-class (slot-definition-allocation-class slotd)))
1062 (setf (slot-definition-location slotd)
1063 (assoc name (class-slot-cells from-class)))
1064 (aver (consp (slot-definition-location slotd)))
1065 (initialize-internal-slot-functions slotd)))
1068 (defmethod compute-slots ((class structure-class))
1069 (mapcan (lambda (superclass)
1070 (mapcar (lambda (dslotd)
1071 (compute-effective-slot-definition
1073 (slot-definition-name dslotd)
1075 (class-direct-slots superclass)))
1076 (reverse (slot-value class 'class-precedence-list))))
1078 (defmethod compute-slots :around ((class structure-class))
1079 (let ((eslotds (call-next-method)))
1080 (mapc #'initialize-internal-slot-functions eslotds)
1083 (defmethod compute-effective-slot-definition ((class slot-class) name dslotds)
1084 (declare (ignore name))
1085 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
1086 (class (apply #'effective-slot-definition-class class initargs)))
1087 (apply #'make-instance class initargs)))
1089 (defmethod effective-slot-definition-class ((class std-class) &rest initargs)
1090 (declare (ignore initargs))
1091 (find-class 'standard-effective-slot-definition))
1093 (defmethod effective-slot-definition-class ((class structure-class) &rest initargs)
1094 (declare (ignore initargs))
1095 (find-class 'structure-effective-slot-definition))
1097 (defmethod compute-effective-slot-definition-initargs
1098 ((class slot-class) direct-slotds)
1104 (allocation-class nil)
1110 (dolist (slotd direct-slotds)
1113 (setq name (slot-definition-name slotd)
1116 (when (slot-definition-initfunction slotd)
1117 (setq initform (slot-definition-initform slotd)
1118 initfunction (slot-definition-initfunction slotd)
1121 (setq allocation (slot-definition-allocation slotd)
1122 allocation-class (slot-definition-class slotd)
1124 (setq initargs (append (slot-definition-initargs slotd) initargs))
1125 (let ((slotd-type (slot-definition-type slotd)))
1126 (setq type (cond ((eq type t) slotd-type)
1127 ((*subtypep type slotd-type) type)
1128 (t `(and ,type ,slotd-type)))))))
1131 :initfunction initfunction
1133 :allocation allocation
1134 :allocation-class allocation-class
1138 (defmethod compute-effective-slot-definition-initargs :around
1139 ((class structure-class) direct-slotds)
1140 (let ((slotd (car direct-slotds)))
1141 (list* :defstruct-accessor-symbol
1142 (slot-definition-defstruct-accessor-symbol slotd)
1143 :internal-reader-function
1144 (slot-definition-internal-reader-function slotd)
1145 :internal-writer-function
1146 (slot-definition-internal-writer-function slotd)
1147 (call-next-method))))
1149 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
1150 ;;; to make the method object. They have to use make-a-method which
1151 ;;; is a specially bootstrapped mechanism for making standard methods.
1152 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
1153 (declare (ignore direct-slot initargs))
1154 (find-class 'standard-reader-method))
1156 (defmethod add-reader-method ((class slot-class) generic-function slot-name)
1157 (add-method generic-function
1158 (make-a-method 'standard-reader-method
1160 (list (or (class-name class) 'object))
1162 (make-reader-method-function class slot-name)
1163 "automatically generated reader method"
1166 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
1167 (declare (ignore direct-slot initargs))
1168 (find-class 'standard-writer-method))
1170 (defmethod add-writer-method ((class slot-class) generic-function slot-name)
1171 (add-method generic-function
1172 (make-a-method 'standard-writer-method
1174 (list 'new-value (or (class-name class) 'object))
1175 (list *the-class-t* class)
1176 (make-writer-method-function class slot-name)
1177 "automatically generated writer method"
1180 (defmethod add-boundp-method ((class slot-class) generic-function slot-name)
1181 (add-method generic-function
1182 (make-a-method 'standard-boundp-method
1184 (list (or (class-name class) 'object))
1186 (make-boundp-method-function class slot-name)
1187 "automatically generated boundp method"
1190 (defmethod remove-reader-method ((class slot-class) generic-function)
1191 (let ((method (get-method generic-function () (list class) nil)))
1192 (when method (remove-method generic-function method))))
1194 (defmethod remove-writer-method ((class slot-class) generic-function)
1196 (get-method generic-function () (list *the-class-t* class) nil)))
1197 (when method (remove-method generic-function method))))
1199 (defmethod remove-boundp-method ((class slot-class) generic-function)
1200 (let ((method (get-method generic-function () (list class) nil)))
1201 (when method (remove-method generic-function method))))
1203 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITE-METHOD function are NOT
1204 ;;; part of the standard protocol. They are however useful, PCL makes
1205 ;;; use of them internally and documents them for PCL users.
1207 ;;; *** This needs work to make type testing by the writer functions which
1208 ;;; *** do type testing faster. The idea would be to have one constructor
1209 ;;; *** for each possible type test.
1211 ;;; *** There is a subtle bug here which is going to have to be fixed.
1212 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1213 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1214 ;;; *** defined for this metaclass a chance to run.
1216 (defmethod make-reader-method-function ((class slot-class) slot-name)
1217 (make-std-reader-method-function (class-name class) slot-name))
1219 (defmethod make-writer-method-function ((class slot-class) slot-name)
1220 (make-std-writer-method-function (class-name class) slot-name))
1222 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1223 (make-std-boundp-method-function (class-name class) slot-name))
1225 (defmethod compatible-meta-class-change-p (class proto-new-class)
1226 (eq (class-of class) (class-of proto-new-class)))
1228 (defmethod validate-superclass ((class class) (new-super class))
1229 (or (eq new-super *the-class-t*)
1230 (eq (class-of class) (class-of new-super))))
1232 (defmethod validate-superclass ((class standard-class) (new-super std-class))
1233 (let ((new-super-meta-class (class-of new-super)))
1234 (or (eq new-super-meta-class *the-class-std-class*)
1235 (eq (class-of class) new-super-meta-class))))
1237 ;;; What this does depends on which of the four possible values of
1238 ;;; LAYOUT-INVALID the PCL wrapper has; the simplest case is when it
1239 ;;; is (:FLUSH <wrapper>) or (:OBSOLETE <wrapper>), when there is
1240 ;;; nothing to do, as the new wrapper has already been created. If
1241 ;;; LAYOUT-INVALID returns NIL, then we invalidate it (setting it to
1242 ;;; (:FLUSH <wrapper>); UPDATE-SLOTS later gets to choose whether or
1243 ;;; not to "upgrade" this to (:OBSOLETE <wrapper>).
1245 ;;; This leaves the case where LAYOUT-INVALID returns T, which happens
1246 ;;; when REGISTER-LAYOUT has invalidated a superclass of CLASS (which
1247 ;;; invalidated all the subclasses in SB-KERNEL land). Again, here we
1248 ;;; must flush the caches and allow UPDATE-SLOTS to decide whether to
1249 ;;; obsolete the wrapper.
1251 ;;; FIXME: either here or in INVALID-WRAPPER-P looks like a good place
1252 ;;; for (AVER (NOT (EQ (LAYOUT-INVALID OWRAPPER)
1253 ;;; :UNINITIALIZED)))
1255 ;;; Thanks to Gerd Moellmann for the explanation. -- CSR, 2002-10-29
1256 (defun force-cache-flushes (class)
1257 (let* ((owrapper (class-wrapper class)))
1258 ;; We only need to do something if the wrapper is still valid. If
1259 ;; the wrapper isn't valid, state will be FLUSH or OBSOLETE, and
1260 ;; both of those will already be doing what we want. In
1261 ;; particular, we must be sure we never change an OBSOLETE into a
1262 ;; FLUSH since OBSOLETE means do what FLUSH does and then some.
1263 (when (or (not (invalid-wrapper-p owrapper))
1264 ;; KLUDGE: despite the observations above, this remains
1265 ;; a violation of locality or what might be considered
1266 ;; good style. There has to be a better way! -- CSR,
1268 (eq (layout-invalid owrapper) t))
1269 (let ((nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1271 (setf (wrapper-instance-slots-layout nwrapper)
1272 (wrapper-instance-slots-layout owrapper))
1273 (setf (wrapper-class-slots nwrapper)
1274 (wrapper-class-slots owrapper))
1276 (update-lisp-class-layout class nwrapper)
1277 (setf (slot-value class 'wrapper) nwrapper)
1278 ;; Use :OBSOLETE instead of :FLUSH if any superclass has
1280 (if (find-if (lambda (x)
1281 (and (consp x) (eq :obsolete (car x))))
1282 (layout-inherits owrapper)
1283 :key #'layout-invalid)
1284 (invalidate-wrapper owrapper :obsolete nwrapper)
1285 (invalidate-wrapper owrapper :flush nwrapper)))))))
1287 (defun flush-cache-trap (owrapper nwrapper instance)
1288 (declare (ignore owrapper))
1289 (set-wrapper instance nwrapper))
1291 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1292 ;;; the next access to the instance (as defined in 88-002R) to trap
1293 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1294 (defmethod make-instances-obsolete ((class std-class))
1295 (let* ((owrapper (class-wrapper class))
1296 (nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1298 (setf (wrapper-instance-slots-layout nwrapper)
1299 (wrapper-instance-slots-layout owrapper))
1300 (setf (wrapper-class-slots nwrapper)
1301 (wrapper-class-slots owrapper))
1303 (update-lisp-class-layout class nwrapper)
1304 (setf (slot-value class 'wrapper) nwrapper)
1305 (invalidate-wrapper owrapper :obsolete nwrapper)
1308 (defmethod make-instances-obsolete ((class symbol))
1309 (make-instances-obsolete (find-class class))
1310 ;; ANSI wants the class name when called with a symbol.
1313 ;;; OBSOLETE-INSTANCE-TRAP is the internal trap that is called when we
1314 ;;; see an obsolete instance. The times when it is called are:
1315 ;;; - when the instance is involved in method lookup
1316 ;;; - when attempting to access a slot of an instance
1318 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1319 ;;; instance access macros.
1321 ;;; Of course these times when it is called are an internal
1322 ;;; implementation detail of PCL and are not part of the documented
1323 ;;; description of when the obsolete instance update happens. The
1324 ;;; documented description is as it appears in 88-002R.
1326 ;;; This has to return the new wrapper, so it counts on all the
1327 ;;; methods on obsolete-instance-trap-internal to return the new
1328 ;;; wrapper. It also does a little internal error checking to make
1329 ;;; sure that the traps are only happening when they should, and that
1330 ;;; the trap methods are computing appropriate new wrappers.
1332 ;;; OBSOLETE-INSTANCE-TRAP might be called on structure instances
1333 ;;; after a structure is redefined. In most cases,
1334 ;;; OBSOLETE-INSTANCE-TRAP will not be able to fix the old instance,
1335 ;;; so it must signal an error. The hard part of this is that the
1336 ;;; error system and debugger might cause OBSOLETE-INSTANCE-TRAP to be
1337 ;;; called again, so in that case, we have to return some reasonable
1338 ;;; wrapper, instead.
1340 (defvar *in-obsolete-instance-trap* nil)
1341 (defvar *the-wrapper-of-structure-object*
1342 (class-wrapper (find-class 'structure-object)))
1344 (define-condition obsolete-structure (error)
1345 ((datum :reader obsolete-structure-datum :initarg :datum))
1347 (lambda (condition stream)
1348 ;; Don't try to print the structure, since it probably won't work.
1350 "~@<obsolete structure error for a structure of type ~2I~_~S~:>"
1351 (type-of (obsolete-structure-datum condition))))))
1353 (defun obsolete-instance-trap (owrapper nwrapper instance)
1354 (if (not (pcl-instance-p instance))
1355 (if *in-obsolete-instance-trap*
1356 *the-wrapper-of-structure-object*
1357 (let ((*in-obsolete-instance-trap* t))
1358 (error 'obsolete-structure :datum instance)))
1359 (let* ((class (wrapper-class* nwrapper))
1360 (copy (allocate-instance class)) ;??? allocate-instance ???
1361 (olayout (wrapper-instance-slots-layout owrapper))
1362 (nlayout (wrapper-instance-slots-layout nwrapper))
1363 (oslots (get-slots instance))
1364 (nslots (get-slots copy))
1365 (oclass-slots (wrapper-class-slots owrapper))
1370 ;; local --> local transfer value
1371 ;; local --> shared discard value, discard slot
1372 ;; local --> -- discard slot
1373 ;; shared --> local transfer value
1374 ;; shared --> shared -- (cf SHARED-INITIALIZE :AFTER STD-CLASS)
1375 ;; shared --> -- discard value
1376 ;; -- --> local add slot
1379 ;; Collect class slots from inherited wrappers. Needed for
1380 ;; shared -> local transfers of inherited slots.
1381 (let ((inherited (layout-inherits owrapper)))
1382 (loop for i from (1- (length inherited)) downto 0
1383 for layout = (aref inherited i)
1384 when (typep layout 'wrapper)
1385 do (dolist (slot (wrapper-class-slots layout))
1386 (pushnew slot oclass-slots :key #'car))))
1388 ;; Go through all the old local slots.
1390 (dolist (name olayout)
1391 (let ((npos (posq name nlayout)))
1393 (setf (clos-slots-ref nslots npos)
1394 (clos-slots-ref oslots opos))
1396 (push name discarded)
1397 (unless (eq (clos-slots-ref oslots opos) +slot-unbound+)
1398 (setf (getf plist name) (clos-slots-ref oslots opos))))))
1401 ;; Go through all the old shared slots.
1402 (dolist (oclass-slot-and-val oclass-slots)
1403 (let ((name (car oclass-slot-and-val))
1404 (val (cdr oclass-slot-and-val)))
1405 (let ((npos (posq name nlayout)))
1407 (setf (clos-slots-ref nslots npos) val)))))
1409 ;; Go through all the new local slots to compute the added slots.
1410 (dolist (nlocal nlayout)
1411 (unless (or (memq nlocal olayout)
1412 (assq nlocal oclass-slots))
1413 (push nlocal added)))
1415 (swap-wrappers-and-slots instance copy)
1417 (update-instance-for-redefined-class instance
1423 (defun change-class-internal (instance new-class initargs)
1424 (let* ((old-class (class-of instance))
1425 (copy (allocate-instance new-class))
1426 (new-wrapper (get-wrapper copy))
1427 (old-wrapper (class-wrapper old-class))
1428 (old-layout (wrapper-instance-slots-layout old-wrapper))
1429 (new-layout (wrapper-instance-slots-layout new-wrapper))
1430 (old-slots (get-slots instance))
1431 (new-slots (get-slots copy))
1432 (old-class-slots (wrapper-class-slots old-wrapper)))
1434 ;; "The values of local slots specified by both the class CTO and
1435 ;; CFROM are retained. If such a local slot was unbound, it
1436 ;; remains unbound."
1437 (let ((new-position 0))
1438 (dolist (new-slot new-layout)
1439 (let ((old-position (posq new-slot old-layout)))
1441 (setf (clos-slots-ref new-slots new-position)
1442 (clos-slots-ref old-slots old-position))))
1443 (incf new-position)))
1445 ;; "The values of slots specified as shared in the class CFROM and
1446 ;; as local in the class CTO are retained."
1447 (dolist (slot-and-val old-class-slots)
1448 (let ((position (posq (car slot-and-val) new-layout)))
1450 (setf (clos-slots-ref new-slots position) (cdr slot-and-val)))))
1452 ;; Make the copy point to the old instance's storage, and make the
1453 ;; old instance point to the new storage.
1454 (swap-wrappers-and-slots instance copy)
1456 (apply #'update-instance-for-different-class copy instance initargs)
1459 (defmethod change-class ((instance standard-object)
1460 (new-class standard-class)
1462 (change-class-internal instance new-class initargs))
1464 (defmethod change-class ((instance funcallable-standard-object)
1465 (new-class funcallable-standard-class)
1467 (change-class-internal instance new-class initargs))
1469 (defmethod change-class ((instance standard-object)
1470 (new-class funcallable-standard-class)
1472 (declare (ignore initargs))
1473 (error "You can't change the class of ~S to ~S~@
1474 because it isn't already an instance with metaclass ~S."
1475 instance new-class 'standard-class))
1477 (defmethod change-class ((instance funcallable-standard-object)
1478 (new-class standard-class)
1480 (declare (ignore initargs))
1481 (error "You can't change the class of ~S to ~S~@
1482 because it isn't already an instance with metaclass ~S."
1483 instance new-class 'funcallable-standard-class))
1485 (defmethod change-class ((instance t) (new-class-name symbol) &rest initargs)
1486 (apply #'change-class instance (find-class new-class-name) initargs))
1488 ;;;; The metaclass BUILT-IN-CLASS
1490 ;;;; This metaclass is something of a weird creature. By this point, all
1491 ;;;; instances of it which will exist have been created, and no instance
1492 ;;;; is ever created by calling MAKE-INSTANCE.
1494 ;;;; But, there are other parts of the protocol we must follow and those
1495 ;;;; definitions appear here.
1497 (defmethod shared-initialize :before
1498 ((class built-in-class) slot-names &rest initargs)
1499 (declare (ignore slot-names initargs))
1500 (error "attempt to initialize or reinitialize a built in class"))
1502 (defmethod class-direct-slots ((class built-in-class)) ())
1503 (defmethod class-slots ((class built-in-class)) ())
1504 (defmethod class-direct-default-initargs ((class built-in-class)) ())
1505 (defmethod class-default-initargs ((class built-in-class)) ())
1507 (defmethod validate-superclass ((c class) (s built-in-class))
1508 (or (eq s *the-class-t*) (eq s *the-class-stream*)
1509 ;; FIXME: bad things happen if someone tries to mix in both
1510 ;; FILE-STREAM and STRING-STREAM (as they have the same
1511 ;; layout-depthoid). Is there any way we can provide a useful
1512 ;; error message? -- CSR, 2005-05-03
1513 (eq s *the-class-file-stream*) (eq s *the-class-string-stream*)))
1515 ;;; Some necessary methods for FORWARD-REFERENCED-CLASS
1516 (defmethod class-direct-slots ((class forward-referenced-class)) ())
1517 (defmethod class-direct-default-initargs ((class forward-referenced-class)) ())
1518 (macrolet ((def (method)
1519 `(defmethod ,method ((class forward-referenced-class))
1520 (error "~@<~I~S was called on a forward referenced class:~2I~_~S~:>"
1522 (def class-default-initargs)
1523 (def class-precedence-list)
1526 (defmethod validate-superclass ((c slot-class)
1527 (f forward-referenced-class))
1530 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1531 (pushnew dependent (plist-value metaobject 'dependents)))
1533 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1534 (setf (plist-value metaobject 'dependents)
1535 (delete dependent (plist-value metaobject 'dependents))))
1537 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1538 (dolist (dependent (plist-value metaobject 'dependents))
1539 (funcall function dependent)))