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 ((class std-class))
125 (with-slots (prototype) class
126 (or prototype (setq prototype (allocate-instance class)))))
128 (defmethod class-prototype ((class structure-class))
129 (with-slots (prototype wrapper defstruct-constructor) class
132 (if defstruct-constructor
133 (allocate-instance class)
134 (allocate-standard-instance wrapper))))))
136 (defmethod class-direct-default-initargs ((class slot-class))
137 (plist-value class 'direct-default-initargs))
139 (defmethod class-default-initargs ((class slot-class))
140 (plist-value class 'default-initargs))
142 (defmethod class-slot-cells ((class std-class))
143 (plist-value class 'class-slot-cells))
144 (defmethod (setf class-slot-cells) (new-value (class std-class))
145 (setf (plist-value class 'class-slot-cells) new-value))
147 ;;;; class accessors that are even a little bit more complicated than those
148 ;;;; above. These have a protocol for updating them, we must implement that
151 ;;; Maintaining the direct subclasses backpointers. The update methods are
152 ;;; here, the values are read by an automatically generated reader method.
153 (defmethod add-direct-subclass ((class class) (subclass class))
154 (with-slots (direct-subclasses) class
155 (pushnew subclass direct-subclasses)
157 (defmethod remove-direct-subclass ((class class) (subclass class))
158 (with-slots (direct-subclasses) class
159 (setq direct-subclasses (remove subclass direct-subclasses))
162 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
164 ;;; There are four generic functions involved, each has one method for the
165 ;;; class case and another method for the damned EQL specializers. All of
166 ;;; these are specified methods and appear in their specified place in the
169 ;;; ADD-DIRECT-METHOD
170 ;;; REMOVE-DIRECT-METHOD
171 ;;; SPECIALIZER-DIRECT-METHODS
172 ;;; SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
174 ;;; In each case, we maintain one value which is a cons. The car is the list
175 ;;; methods. The cdr is a list of the generic functions. The cdr is always
177 (defmethod add-direct-method ((specializer class) (method method))
178 (with-slots (direct-methods) specializer
179 (setf (car direct-methods) (adjoin method (car direct-methods)) ;PUSH
180 (cdr direct-methods) ()))
182 (defmethod remove-direct-method ((specializer class) (method method))
183 (with-slots (direct-methods) specializer
184 (setf (car direct-methods) (remove method (car direct-methods))
185 (cdr direct-methods) ()))
188 (defmethod specializer-direct-methods ((specializer class))
189 (with-slots (direct-methods) specializer
190 (car direct-methods)))
192 (defmethod specializer-direct-generic-functions ((specializer class))
193 (with-slots (direct-methods) specializer
194 (or (cdr direct-methods)
195 (setf (cdr direct-methods)
197 (dolist (m (car direct-methods))
198 ;; the old PCL code used COLLECTING-ONCE which used
199 ;; #'EQ to check for newness
200 (pushnew (method-generic-function m) collect :test #'eq))
201 (nreverse collect))))))
203 ;;; This hash table is used to store the direct methods and direct generic
204 ;;; functions of EQL specializers. Each value in the table is the cons.
205 (defvar *eql-specializer-methods* (make-hash-table :test 'eql))
206 (defvar *class-eq-specializer-methods* (make-hash-table :test 'eq))
208 (defmethod specializer-method-table ((specializer eql-specializer))
209 *eql-specializer-methods*)
211 (defmethod specializer-method-table ((specializer class-eq-specializer))
212 *class-eq-specializer-methods*)
214 (defmethod add-direct-method ((specializer specializer-with-object)
216 (let* ((object (specializer-object specializer))
217 (table (specializer-method-table specializer))
218 (entry (gethash object table)))
221 (setf (gethash object table)
223 (setf (car entry) (adjoin method (car entry))
227 (defmethod remove-direct-method ((specializer specializer-with-object)
229 (let* ((object (specializer-object specializer))
230 (entry (gethash object (specializer-method-table specializer))))
232 (setf (car entry) (remove method (car entry))
236 (defmethod specializer-direct-methods ((specializer specializer-with-object))
237 (car (gethash (specializer-object specializer)
238 (specializer-method-table specializer))))
240 (defmethod specializer-direct-generic-functions ((specializer
241 specializer-with-object))
242 (let* ((object (specializer-object specializer))
243 (entry (gethash object (specializer-method-table specializer))))
248 (dolist (m (car entry))
249 (pushnew (method-generic-function m) collect :test #'eq))
250 (nreverse collect)))))))
252 (defun map-specializers (function)
253 (map-all-classes (lambda (class)
254 (funcall function (class-eq-specializer class))
255 (funcall function class)))
256 (maphash (lambda (object methods)
257 (declare (ignore methods))
258 (intern-eql-specializer object))
259 *eql-specializer-methods*)
260 (maphash (lambda (object specl)
261 (declare (ignore object))
262 (funcall function specl))
263 *eql-specializer-table*)
266 (defun map-all-generic-functions (function)
267 (let ((all-generic-functions (make-hash-table :test 'eq)))
268 (map-specializers (lambda (specl)
269 (dolist (gf (specializer-direct-generic-functions
271 (unless (gethash gf all-generic-functions)
272 (setf (gethash gf all-generic-functions) t)
273 (funcall function gf))))))
276 (defmethod shared-initialize :after ((specl class-eq-specializer)
279 (declare (ignore slot-names))
280 (setf (slot-value specl 'type) `(class-eq ,(specializer-class specl))))
282 (defmethod shared-initialize :after ((specl eql-specializer) slot-names &key)
283 (declare (ignore slot-names))
284 (setf (slot-value specl 'type)
285 `(eql ,(specializer-object specl)))
286 (setf (info :type :translator specl)
287 (constantly (make-member-type :members (list (specializer-object specl))))))
290 (defun real-load-defclass (name metaclass-name supers slots other)
291 (let ((res (apply #'ensure-class name :metaclass metaclass-name
292 :direct-superclasses supers
294 :definition-source `((defclass ,name)
299 (setf (gdefinition 'load-defclass) #'real-load-defclass)
301 (defun ensure-class (name &rest args)
302 (apply #'ensure-class-using-class
303 (let ((class (find-class name nil)))
304 (when (and class (eq name (class-name class)))
305 ;; NAME is the proper name of CLASS, so redefine it
310 (defmethod ensure-class-using-class ((class null) name &rest args &key)
311 (without-package-locks
312 (multiple-value-bind (meta initargs)
313 (ensure-class-values class args)
314 (set-class-type-translation (class-prototype meta) name)
315 (setf class (apply #'make-instance meta :name name initargs)
316 (find-class name) class)
317 (set-class-type-translation class name)
320 (defmethod ensure-class-using-class ((class pcl-class) name &rest args &key)
321 (without-package-locks
322 (multiple-value-bind (meta initargs)
323 (ensure-class-values class args)
324 (unless (eq (class-of class) meta)
325 (apply #'change-class class meta initargs))
326 (apply #'reinitialize-instance class initargs)
327 (setf (find-class name) class)
328 (set-class-type-translation class name)
331 (defmethod class-predicate-name ((class t))
336 ((not (legal-class-name-p s))
337 (error "~S is not a class or a legal class name." s))
339 (or (find-class s nil)
340 (make-instance 'forward-referenced-class
343 (defun ensure-class-values (class args)
344 (let* ((initargs (copy-list args))
345 (unsupplied (list 1))
346 (supplied-meta (getf initargs :metaclass unsupplied))
347 (supplied-supers (getf initargs :direct-superclasses unsupplied))
348 (supplied-slots (getf initargs :direct-slots unsupplied))
350 (cond ((neq supplied-meta unsupplied)
351 (find-class supplied-meta))
353 (forward-referenced-class-p class))
354 *the-class-standard-class*)
357 ;; KLUDGE: It seemed to me initially that there ought to be a way
358 ;; of collecting all the erroneous problems in one go, rather than
359 ;; this way of solving the problem of signalling the errors that
360 ;; we are required to, which stops at the first bogus input.
361 ;; However, after playing around a little, I couldn't find that
362 ;; way, so I've left it as is, but if someone does come up with a
363 ;; better way... -- CSR, 2002-09-08
364 (do ((direct-slots (getf initargs :direct-slots) (cdr direct-slots)))
365 ((endp direct-slots) nil)
366 (destructuring-bind (slot &rest more) direct-slots
367 (let ((slot-name (getf slot :name)))
368 (when (some (lambda (s) (eq slot-name (getf s :name))) more)
369 ;; FIXME: It's quite possible that we ought to define an
370 ;; SB-INT:PROGRAM-ERROR function to signal these and other
371 ;; errors throughout the codebase that are required to be
372 ;; of type PROGRAM-ERROR.
373 (error 'simple-program-error
374 :format-control "~@<There is more than one direct slot ~
376 :format-arguments (list slot-name)))
377 (do ((stuff slot (cddr stuff)))
379 (destructuring-bind (option value &rest more) stuff
381 ((and (member option '(:allocation :type
382 :initform :documentation))
384 (getf more option unsupplied))))
385 (error 'simple-program-error
386 :format-control "~@<Duplicate slot option ~S for ~
388 :format-arguments (list option slot-name)))
389 ((and (eq option :readers)
390 (notevery #'symbolp value))
391 (error 'simple-program-error
392 :format-control "~@<Slot reader names for slot ~
393 named ~S must be symbols.~:>"
394 :format-arguments (list slot-name)))
395 ((and (eq option :initargs)
396 (notevery #'symbolp value))
397 (error 'simple-program-error
398 :format-control "~@<Slot initarg names for slot ~
399 named ~S must be symbols.~:>"
400 :format-arguments (list slot-name)))))))))
401 (loop for (initarg . more) on (getf initargs :direct-default-initargs)
402 for name = (car initarg)
403 when (some (lambda (a) (eq (car a) name)) more)
404 do (error 'simple-program-error
405 :format-control "~@<Duplicate initialization argument ~
406 name ~S in :DEFAULT-INITARGS.~:>"
407 :format-arguments (list name class)))
409 (default-initargs 0))
410 (do ((args initargs (cddr args)))
414 (when (> (incf metaclass) 1)
415 (error 'simple-program-error
416 :format-control "~@<More than one :METACLASS ~
417 option specified.~:>")))
418 (:direct-default-initargs
419 (when (> (incf default-initargs) 1)
420 (error 'simple-program-error
421 :format-control "~@<More than one :DEFAULT-INITARGS ~
422 option specified.~:>"))))))
423 (remf initargs :metaclass)
424 (loop (unless (remf initargs :direct-superclasses) (return)))
425 (loop (unless (remf initargs :direct-slots) (return)))
429 (when (neq supplied-supers unsupplied)
430 (list :direct-superclasses (mapcar #'fix-super supplied-supers)))
431 (when (neq supplied-slots unsupplied)
432 (list :direct-slots supplied-slots))
435 (defmethod shared-initialize :after
438 &key (direct-superclasses nil direct-superclasses-p)
439 (direct-slots nil direct-slots-p)
440 (direct-default-initargs nil direct-default-initargs-p)
441 (predicate-name nil predicate-name-p))
442 (cond (direct-superclasses-p
443 (setq direct-superclasses
444 (or direct-superclasses
445 (list (if (funcallable-standard-class-p class)
446 *the-class-funcallable-standard-object*
447 *the-class-standard-object*))))
448 (dolist (superclass direct-superclasses)
449 (unless (validate-superclass class superclass)
450 (error "The class ~S was specified as a~%
451 super-class of the class ~S;~%~
452 but the meta-classes ~S and~%~S are incompatible.~@
453 Define a method for ~S to avoid this error."
454 superclass class (class-of superclass) (class-of class)
455 'validate-superclass)))
456 (setf (slot-value class 'direct-superclasses) direct-superclasses))
458 (setq direct-superclasses (slot-value class 'direct-superclasses))))
461 (setf (slot-value class 'direct-slots)
462 (mapcar (lambda (pl) (make-direct-slotd class pl))
464 (slot-value class 'direct-slots)))
465 (if direct-default-initargs-p
466 (setf (plist-value class 'direct-default-initargs)
467 direct-default-initargs)
468 (setq direct-default-initargs
469 (plist-value class 'direct-default-initargs)))
470 (setf (plist-value class 'class-slot-cells)
471 (let ((old-class-slot-cells (plist-value class 'class-slot-cells))
473 (dolist (dslotd direct-slots)
474 (when (eq :class (slot-definition-allocation dslotd))
476 (let* ((name (slot-definition-name dslotd))
477 (old (assoc name old-class-slot-cells)))
480 (member name slot-names))
481 (let* ((initfunction (slot-definition-initfunction dslotd))
482 (value (if initfunction
483 (funcall initfunction)
485 (push (cons name value) collect))
486 (push old collect)))))
488 (setq predicate-name (if predicate-name-p
489 (setf (slot-value class 'predicate-name)
490 (car predicate-name))
491 (or (slot-value class 'predicate-name)
492 (setf (slot-value class 'predicate-name)
493 (make-class-predicate-name (class-name
495 (add-direct-subclasses class direct-superclasses)
496 (make-class-predicate class predicate-name)
497 (update-class class nil)
498 (do* ((slots (slot-value class 'slots) (cdr slots))
500 ((null slots) (when dupes
502 ;; FIXME: the indentation request ("~4I")
503 ;; below appears not to do anything. Finding
504 ;; out why would be nice. -- CSR, 2003-04-24
505 "~@<slot names with the same SYMBOL-NAME but ~
506 different SYMBOL-PACKAGE (possible package problem) ~
507 for class ~S:~@:_~4I~<~@{~S~^~:@_~}~:>~@:>"
510 (let* ((slot (car slots))
511 (oslots (remove (slot-definition-name slot) (cdr slots)
512 :test #'string/= :key #'slot-definition-name)))
514 (pushnew (cons (slot-definition-name slot)
515 (mapcar #'slot-definition-name oslots))
517 :test #'string= :key #'car))))
518 (add-slot-accessors class direct-slots)
519 (make-preliminary-layout class))
521 (defmethod shared-initialize :after ((class forward-referenced-class)
522 slot-names &key &allow-other-keys)
523 (declare (ignore slot-names))
524 (make-preliminary-layout class))
526 (defvar *allow-forward-referenced-classes-in-cpl-p* nil)
528 ;;; Give CLASS a preliminary layout if it doesn't have one already, to
529 ;;; make it known to the type system.
530 (defun make-preliminary-layout (class)
531 (flet ((compute-preliminary-cpl (root)
532 (let ((*allow-forward-referenced-classes-in-cpl-p* t))
533 (compute-class-precedence-list root))))
534 (without-package-locks
535 (unless (class-finalized-p class)
536 (let ((name (class-name class)))
537 (setf (find-class name) class)
538 ;; KLUDGE: This is fairly horrible. We need to make a
539 ;; full-fledged CLASSOID here, not just tell the compiler that
540 ;; some class is forthcoming, because there are legitimate
541 ;; questions one can ask of the type system, implemented in
542 ;; terms of CLASSOIDs, involving forward-referenced classes. So.
543 (when (and (eq *boot-state* 'complete)
544 (null (find-classoid name nil)))
545 (setf (find-classoid name)
546 (make-standard-classoid :name name)))
547 (set-class-type-translation class name)
548 (let ((layout (make-wrapper 0 class))
549 (classoid (find-classoid name)))
550 (setf (layout-classoid layout) classoid)
551 (setf (classoid-pcl-class classoid) class)
552 (setf (slot-value class 'wrapper) layout)
553 (let ((cpl (compute-preliminary-cpl class)))
554 (setf (layout-inherits layout)
555 (order-layout-inherits
556 (map 'simple-vector #'class-wrapper
557 (reverse (rest cpl))))))
558 (register-layout layout :invalidate t)
559 (setf (classoid-layout classoid) layout)
560 (mapc #'make-preliminary-layout (class-direct-subclasses class))))))))
563 (defmethod shared-initialize :before ((class class) slot-names &key name)
564 (declare (ignore slot-names name))
565 ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
566 ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
567 (setf (slot-value class 'type) `(class ,class))
568 (setf (slot-value class 'class-eq-specializer)
569 (make-instance 'class-eq-specializer :class class)))
571 (defmethod reinitialize-instance :before ((class slot-class) &key)
572 (remove-direct-subclasses class (class-direct-superclasses class))
573 (remove-slot-accessors class (class-direct-slots class)))
575 (defmethod reinitialize-instance :after ((class slot-class)
578 (map-dependents class
580 (apply #'update-dependent class dependent initargs))))
582 (defmethod shared-initialize :after ((class condition-class) slot-names
583 &key direct-slots direct-superclasses)
584 (declare (ignore slot-names))
585 (let ((classoid (find-classoid (class-name class))))
586 (with-slots (wrapper class-precedence-list prototype predicate-name
587 (direct-supers direct-superclasses))
589 (setf (slot-value class 'direct-slots)
590 (mapcar (lambda (pl) (make-direct-slotd class pl))
592 (setf (slot-value class 'finalized-p) t)
593 (setf (classoid-pcl-class classoid) class)
594 (setq direct-supers direct-superclasses)
595 (setq wrapper (classoid-layout classoid))
596 (setq class-precedence-list (compute-class-precedence-list class))
597 (setq prototype (make-condition (class-name class)))
598 (add-direct-subclasses class direct-superclasses)
599 (setq predicate-name (make-class-predicate-name (class-name class)))
600 (make-class-predicate class predicate-name)
601 (setf (slot-value class 'slots) (compute-slots class))))
602 ;; Comment from Gerd's PCL, 2003-05-15:
604 ;; We don't ADD-SLOT-ACCESSORS here because we don't want to
605 ;; override condition accessors with generic functions. We do this
607 (update-pv-table-cache-info class))
609 (defmethod direct-slot-definition-class ((class condition-class)
611 (declare (ignore initargs))
612 (find-class 'condition-direct-slot-definition))
614 (defmethod effective-slot-definition-class ((class condition-class)
616 (declare (ignore initargs))
617 (find-class 'condition-effective-slot-definition))
619 (defmethod finalize-inheritance ((class condition-class))
620 (aver (slot-value class 'finalized-p))
623 (defmethod compute-effective-slot-definition
624 ((class condition-class) slot-name dslotds)
625 (let ((slotd (call-next-method)))
626 (setf (slot-definition-reader-function slotd)
628 (handler-case (condition-reader-function x slot-name)
629 ;; FIXME: FIND-SLOT-DEFAULT throws an error if the slot
630 ;; is unbound; maybe it should be a CELL-ERROR of some
632 (error () (values (slot-unbound class x slot-name))))))
633 (setf (slot-definition-writer-function slotd)
635 (condition-writer-function x v slot-name)))
636 (setf (slot-definition-boundp-function slotd)
638 (multiple-value-bind (v c)
639 (ignore-errors (condition-reader-function x slot-name))
644 (defmethod compute-slots ((class condition-class))
645 (mapcan (lambda (superclass)
646 (mapcar (lambda (dslotd)
647 (compute-effective-slot-definition
648 class (slot-definition-name dslotd) (list dslotd)))
649 (class-direct-slots superclass)))
650 (reverse (slot-value class 'class-precedence-list))))
652 (defmethod compute-slots :around ((class condition-class))
653 (let ((eslotds (call-next-method)))
654 (mapc #'initialize-internal-slot-functions eslotds)
657 (defmethod shared-initialize :after
658 ((slotd structure-slot-definition) slot-names &key
659 (allocation :instance) allocation-class)
660 (declare (ignore slot-names allocation-class))
661 (unless (eq allocation :instance)
662 (error "Structure slots must have :INSTANCE allocation.")))
664 (defun make-structure-class-defstruct-form (name direct-slots include)
665 (let* ((conc-name (format-symbol *package* "~S structure class " name))
666 (constructor (format-symbol *package* "~Aconstructor" conc-name))
667 (defstruct `(defstruct (,name
669 `((:include ,(class-name include))))
671 (:conc-name ,conc-name)
672 (:constructor ,constructor ())
674 ,@(mapcar (lambda (slot)
675 `(,(slot-definition-name slot)
678 (reader-names (mapcar (lambda (slotd)
679 (list 'slot-accessor name
680 (slot-definition-name slotd)
683 (writer-names (mapcar (lambda (slotd)
684 (list 'slot-accessor name
685 (slot-definition-name slotd)
689 (mapcar (lambda (slotd reader-name)
691 (slot-definition-defstruct-accessor-symbol
693 `(defun ,reader-name (obj)
694 (declare (type ,name obj))
696 direct-slots reader-names))
698 (mapcar (lambda (slotd writer-name)
700 (slot-definition-defstruct-accessor-symbol
702 `(defun ,writer-name (nv obj)
703 (declare (type ,name obj))
704 (setf (,accessor obj) nv))))
705 direct-slots writer-names))
709 ,@readers-init ,@writers-init
711 (values defstruct-form constructor reader-names writer-names)))
713 (defun make-defstruct-allocation-function (class)
714 (let ((dd (get-structure-dd (class-name class))))
716 (let ((instance (%make-instance (dd-length dd)))
717 (raw-index (dd-raw-index dd)))
718 (setf (%instance-layout instance)
719 (sb-kernel::compiler-layout-or-lose (dd-name dd)))
721 (setf (%instance-ref instance raw-index)
722 (make-array (dd-raw-length dd)
723 :element-type '(unsigned-byte 32))))
726 (defmethod shared-initialize :after
727 ((class structure-class)
729 &key (direct-superclasses nil direct-superclasses-p)
730 (direct-slots nil direct-slots-p)
731 direct-default-initargs
732 (predicate-name nil predicate-name-p))
733 (declare (ignore slot-names direct-default-initargs))
734 (if direct-superclasses-p
735 (setf (slot-value class 'direct-superclasses)
736 (or direct-superclasses
737 (setq direct-superclasses
738 (and (not (eq (class-name class) 'structure-object))
739 (list *the-class-structure-object*)))))
740 (setq direct-superclasses (slot-value class 'direct-superclasses)))
741 (let* ((name (class-name class))
742 (from-defclass-p (slot-value class 'from-defclass-p))
743 (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
745 (setf (slot-value class 'direct-slots)
749 (let* ((slot-name (getf pl :name))
751 (format-symbol *package*
752 "~S structure class ~A"
754 (setq pl (list* :defstruct-accessor-symbol
756 (make-direct-slotd class pl))
758 (setq direct-slots (slot-value class 'direct-slots)))
760 (let ((include (car (slot-value class 'direct-superclasses))))
761 (multiple-value-bind (defstruct-form constructor reader-names writer-names)
762 (make-structure-class-defstruct-form name direct-slots include)
763 (unless (structure-type-p name) (eval defstruct-form))
764 (mapc (lambda (dslotd reader-name writer-name)
765 (let* ((reader (gdefinition reader-name))
766 (writer (when (gboundp writer-name)
767 (gdefinition writer-name))))
768 (setf (slot-value dslotd 'internal-reader-function)
770 (setf (slot-value dslotd 'internal-writer-function)
772 direct-slots reader-names writer-names)
773 (setf (slot-value class 'defstruct-form) defstruct-form)
774 (setf (slot-value class 'defstruct-constructor) constructor)))
775 (setf (slot-value class 'defstruct-constructor)
776 (make-defstruct-allocation-function class)))
777 (add-direct-subclasses class direct-superclasses)
778 (setf (slot-value class 'class-precedence-list)
779 (compute-class-precedence-list class))
780 (setf (slot-value class 'slots) (compute-slots class))
781 (let ((lclass (find-classoid (class-name class))))
782 (setf (classoid-pcl-class lclass) class)
783 (setf (slot-value class 'wrapper) (classoid-layout lclass)))
784 (setf (slot-value class 'finalized-p) t)
785 (update-pv-table-cache-info class)
786 (setq predicate-name (if predicate-name-p
787 (setf (slot-value class 'predicate-name)
788 (car predicate-name))
789 (or (slot-value class 'predicate-name)
790 (setf (slot-value class 'predicate-name)
791 (make-class-predicate-name
792 (class-name class))))))
793 (make-class-predicate class predicate-name)
794 (add-slot-accessors class direct-slots)))
796 (defmethod direct-slot-definition-class ((class structure-class) &rest initargs)
797 (declare (ignore initargs))
798 (find-class 'structure-direct-slot-definition))
800 (defmethod finalize-inheritance ((class structure-class))
801 nil) ; always finalized
803 (defun add-slot-accessors (class dslotds)
804 (fix-slot-accessors class dslotds 'add))
806 (defun remove-slot-accessors (class dslotds)
807 (fix-slot-accessors class dslotds 'remove))
809 (defun fix-slot-accessors (class dslotds add/remove)
810 ;; We disable package locks here, since defining a class can trigger
811 ;; the update of the accessors of another class -- which might lead
812 ;; to package lock violations if we didn't.
813 (without-package-locks
814 (flet ((fix (gfspec name r/w)
815 (let* ((ll (case r/w (r '(object)) (w '(new-value object))))
816 (gf (if (fboundp gfspec)
817 (ensure-generic-function gfspec)
818 (ensure-generic-function gfspec :lambda-list ll))))
820 (r (if (eq add/remove 'add)
821 (add-reader-method class gf name)
822 (remove-reader-method class gf)))
823 (w (if (eq add/remove 'add)
824 (add-writer-method class gf name)
825 (remove-writer-method class gf)))))))
826 (dolist (dslotd dslotds)
827 (let ((slot-name (slot-definition-name dslotd)))
828 (dolist (r (slot-definition-readers dslotd))
829 (fix r slot-name 'r))
830 (dolist (w (slot-definition-writers dslotd))
831 (fix w slot-name 'w)))))))
833 (defun add-direct-subclasses (class supers)
834 (dolist (super supers)
835 (unless (memq class (class-direct-subclasses class))
836 (add-direct-subclass super class))))
838 (defun remove-direct-subclasses (class supers)
839 (let ((old (class-direct-superclasses class)))
840 (dolist (o (set-difference old supers))
841 (remove-direct-subclass o class))))
843 (defmethod finalize-inheritance ((class std-class))
844 (update-class class t))
846 (defmethod finalize-inheritance ((class forward-referenced-class))
847 ;; FIXME: should we not be thinking a bit about what kinds of error
848 ;; we're throwing? Maybe we need a clos-error type to mix in? Or
849 ;; possibly a forward-referenced-class-error, though that's
850 ;; difficult given e.g. class precedence list calculations...
852 "~@<FINALIZE-INHERITANCE was called on a forward referenced class:~
857 (defun class-has-a-forward-referenced-superclass-p (class)
858 (or (forward-referenced-class-p class)
859 (some #'class-has-a-forward-referenced-superclass-p
860 (class-direct-superclasses class))))
862 ;;; This is called by :after shared-initialize whenever a class is initialized
863 ;;; or reinitialized. The class may or may not be finalized.
864 (defun update-class (class finalizep)
865 ;; Comment from Gerd Moellmann:
867 ;; Note that we can't simply delay the finalization when CLASS has
868 ;; no forward referenced superclasses because that causes bootstrap
870 (without-package-locks
871 (when (and (not finalizep)
872 (not (class-finalized-p class))
873 (not (class-has-a-forward-referenced-superclass-p class)))
874 (finalize-inheritance class)
875 (return-from update-class))
876 (when (or finalizep (class-finalized-p class)
877 (not (class-has-a-forward-referenced-superclass-p class)))
878 (setf (find-class (class-name class)) class)
879 (update-cpl class (compute-class-precedence-list class))
880 ;; This invocation of UPDATE-SLOTS, in practice, finalizes the
881 ;; class. The hoops above are to ensure that FINALIZE-INHERITANCE
882 ;; is called at finalization, so that MOP programmers can hook
883 ;; into the system as described in "Class Finalization Protocol"
884 ;; (section 5.5.2 of AMOP).
885 (update-slots class (compute-slots class))
886 (update-gfs-of-class class)
887 (update-initargs class (compute-default-initargs class))
888 (update-ctors 'finalize-inheritance :class class))
890 (dolist (sub (class-direct-subclasses class)) (update-class sub nil)))))
892 (defun update-cpl (class cpl)
893 (if (class-finalized-p class)
894 (unless (and (equal (class-precedence-list class) cpl)
896 (when (position :class (class-direct-slots c)
897 :key #'slot-definition-allocation)
899 ;; comment from the old CMU CL sources:
900 ;; Need to have the cpl setup before update-lisp-class-layout
901 ;; is called on CMU CL.
902 (setf (slot-value class 'class-precedence-list) cpl)
903 (force-cache-flushes class))
904 (setf (slot-value class 'class-precedence-list) cpl))
905 (update-class-can-precede-p cpl))
907 (defun update-class-can-precede-p (cpl)
909 (let ((first (car cpl)))
910 (dolist (c (cdr cpl))
911 (pushnew c (slot-value first 'can-precede-list))))
912 (update-class-can-precede-p (cdr cpl))))
914 (defun class-can-precede-p (class1 class2)
915 (member class2 (class-can-precede-list class1)))
917 (defun update-slots (class eslotds)
918 (let ((instance-slots ())
920 (dolist (eslotd eslotds)
921 (let ((alloc (slot-definition-allocation eslotd)))
923 (:instance (push eslotd instance-slots))
924 (:class (push eslotd class-slots)))))
926 ;; If there is a change in the shape of the instances then the
927 ;; old class is now obsolete.
928 (let* ((nlayout (mapcar #'slot-definition-name
929 (sort instance-slots #'<
930 :key #'slot-definition-location)))
931 (nslots (length nlayout))
932 (nwrapper-class-slots (compute-class-slots class-slots))
933 (owrapper (when (class-finalized-p class)
934 (class-wrapper class)))
935 (olayout (when owrapper
936 (wrapper-instance-slots-layout owrapper)))
937 (owrapper-class-slots (and owrapper (wrapper-class-slots owrapper)))
939 (cond ((null owrapper)
940 (make-wrapper nslots class))
941 ((and (equal nlayout olayout)
943 (loop for o in owrapper-class-slots
944 for n in nwrapper-class-slots
945 do (unless (eq (car o) (car n)) (return t)))))
948 ;; This will initialize the new wrapper to have the
949 ;; same state as the old wrapper. We will then have
950 ;; to change that. This may seem like wasted work
951 ;; (and it is), but the spec requires that we call
952 ;; MAKE-INSTANCES-OBSOLETE.
953 (make-instances-obsolete class)
954 (class-wrapper class)))))
956 (with-slots (wrapper slots) class
957 (update-lisp-class-layout class nwrapper)
959 (wrapper-instance-slots-layout nwrapper) nlayout
960 (wrapper-class-slots nwrapper) nwrapper-class-slots
961 (wrapper-no-of-instance-slots nwrapper) nslots
963 (setf (slot-value class 'finalized-p) t)
964 (unless (eq owrapper nwrapper)
965 (update-pv-table-cache-info class)
966 (maybe-update-standard-class-locations class)))))
968 (defun compute-class-slots (eslotds)
970 (dolist (eslotd eslotds)
971 (push (assoc (slot-definition-name eslotd)
972 (class-slot-cells (slot-definition-class eslotd)))
976 (defun update-gfs-of-class (class)
977 (when (and (class-finalized-p class)
978 (let ((cpl (class-precedence-list class)))
979 (or (member *the-class-slot-class* cpl)
980 (member *the-class-standard-effective-slot-definition*
982 (let ((gf-table (make-hash-table :test 'eq)))
983 (labels ((collect-gfs (class)
984 (dolist (gf (specializer-direct-generic-functions class))
985 (setf (gethash gf gf-table) t))
986 (mapc #'collect-gfs (class-direct-superclasses class))))
988 (maphash (lambda (gf ignore)
989 (declare (ignore ignore))
990 (update-gf-dfun class gf))
993 (defun update-initargs (class inits)
994 (setf (plist-value class 'default-initargs) inits))
996 (defmethod compute-default-initargs ((class slot-class))
997 (let ((initargs (loop for c in (class-precedence-list class)
998 append (class-direct-default-initargs c))))
999 (delete-duplicates initargs :test #'eq :key #'car :from-end t)))
1001 ;;;; protocols for constructing direct and effective slot definitions
1003 (defmethod direct-slot-definition-class ((class std-class) &rest initargs)
1004 (declare (ignore initargs))
1005 (find-class 'standard-direct-slot-definition))
1007 (defun make-direct-slotd (class initargs)
1008 (let ((initargs (list* :class class initargs)))
1009 (apply #'make-instance
1010 (apply #'direct-slot-definition-class class initargs)
1013 (defmethod compute-slots ((class std-class))
1014 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
1015 ;; for each different slot name we find in our superclasses. Each
1016 ;; call receives the class and a list of the dslotds with that name.
1017 ;; The list is in most-specific-first order.
1018 (let ((name-dslotds-alist ()))
1019 (dolist (c (class-precedence-list class))
1020 (dolist (slot (class-direct-slots c))
1021 (let* ((name (slot-definition-name slot))
1022 (entry (assq name name-dslotds-alist)))
1024 (push slot (cdr entry))
1025 (push (list name slot) name-dslotds-alist)))))
1026 (mapcar (lambda (direct)
1027 (compute-effective-slot-definition class
1029 (nreverse (cdr direct))))
1030 name-dslotds-alist)))
1032 (defmethod compute-slots ((class standard-class))
1035 (defmethod compute-slots :around ((class standard-class))
1036 (let ((eslotds (call-next-method))
1038 (dolist (eslotd eslotds eslotds)
1039 (setf (slot-definition-location eslotd)
1040 (ecase (slot-definition-allocation eslotd)
1044 (let* ((name (slot-definition-name eslotd))
1047 (slot-definition-allocation-class eslotd)
1048 ;; we get here if the user adds an extra slot
1050 (setf (slot-definition-allocation-class eslotd)
1052 ;; which raises the question of what we should
1053 ;; do if we find that said user has added a slot
1054 ;; with the same name as another slot...
1055 (cell (or (assq name (class-slot-cells from-class))
1056 (setf (class-slot-cells from-class)
1057 (cons (cons name +slot-unbound+)
1058 (class-slot-cells from-class))))))
1060 (if (eq +slot-unbound+ (cdr cell))
1061 ;; We may have inherited an initfunction
1062 (let ((initfun (slot-definition-initfunction eslotd)))
1064 (rplacd cell (funcall initfun))
1067 (unless (slot-definition-class eslotd)
1068 (setf (slot-definition-class eslotd) class))
1069 (initialize-internal-slot-functions eslotd))))
1071 (defmethod compute-slots ((class funcallable-standard-class))
1074 (defmethod compute-slots :around ((class funcallable-standard-class))
1075 (labels ((instance-slot-names (slotds)
1077 (dolist (slotd slotds (nreverse collect))
1078 (when (eq (slot-definition-allocation slotd) :instance)
1079 (push (slot-definition-name slotd) collect)))))
1080 ;; This sorts slots so that slots of classes later in the CPL
1081 ;; come before slots of other classes. This is crucial for
1082 ;; funcallable instances because it ensures that the slots of
1083 ;; FUNCALLABLE-STANDARD-OBJECT, which includes the slots of
1084 ;; KERNEL:FUNCALLABLE-INSTANCE, come first, which in turn
1085 ;; makes it possible to treat FUNCALLABLE-STANDARD-OBJECT as
1086 ;; a funcallable instance.
1087 (compute-layout (eslotds)
1089 (names (instance-slot-names eslotds)))
1091 (reverse (class-precedence-list class))
1092 (nreverse (nconc names first)))
1093 (dolist (ss (class-slots class))
1094 (let ((name (slot-definition-name ss)))
1095 (when (member name names)
1097 (setq names (delete name names)))))))))
1098 (let ((all-slotds (call-next-method))
1101 (dolist (slotd all-slotds)
1102 (ecase (slot-definition-allocation slotd)
1103 (:instance (push slotd instance-slots))
1104 (:class (push slotd class-slots))))
1105 (let ((layout (compute-layout instance-slots)))
1106 (dolist (slotd instance-slots)
1107 (setf (slot-definition-location slotd)
1108 (position (slot-definition-name slotd) layout))
1109 (initialize-internal-slot-functions slotd)))
1110 (dolist (slotd class-slots)
1111 (let ((name (slot-definition-name slotd))
1112 (from-class (slot-definition-allocation-class slotd)))
1113 (setf (slot-definition-location slotd)
1114 (assoc name (class-slot-cells from-class)))
1115 (aver (consp (slot-definition-location slotd)))
1116 (initialize-internal-slot-functions slotd)))
1119 (defmethod compute-slots ((class structure-class))
1120 (mapcan (lambda (superclass)
1121 (mapcar (lambda (dslotd)
1122 (compute-effective-slot-definition
1124 (slot-definition-name dslotd)
1126 (class-direct-slots superclass)))
1127 (reverse (slot-value class 'class-precedence-list))))
1129 (defmethod compute-slots :around ((class structure-class))
1130 (let ((eslotds (call-next-method)))
1131 (mapc #'initialize-internal-slot-functions eslotds)
1134 (defmethod compute-effective-slot-definition ((class slot-class) name dslotds)
1135 (declare (ignore name))
1136 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
1137 (class (apply #'effective-slot-definition-class class initargs)))
1138 (apply #'make-instance class initargs)))
1140 (defmethod effective-slot-definition-class ((class std-class) &rest initargs)
1141 (declare (ignore initargs))
1142 (find-class 'standard-effective-slot-definition))
1144 (defmethod effective-slot-definition-class ((class structure-class) &rest initargs)
1145 (declare (ignore initargs))
1146 (find-class 'structure-effective-slot-definition))
1148 (defmethod compute-effective-slot-definition-initargs
1149 ((class slot-class) direct-slotds)
1155 (allocation-class nil)
1161 (dolist (slotd direct-slotds)
1164 (setq name (slot-definition-name slotd)
1167 (when (slot-definition-initfunction slotd)
1168 (setq initform (slot-definition-initform slotd)
1169 initfunction (slot-definition-initfunction slotd)
1172 (setq allocation (slot-definition-allocation slotd)
1173 allocation-class (slot-definition-class slotd)
1175 (setq initargs (append (slot-definition-initargs slotd) initargs))
1176 (let ((slotd-type (slot-definition-type slotd)))
1177 (setq type (cond ((eq type t) slotd-type)
1178 ((*subtypep type slotd-type) type)
1179 (t `(and ,type ,slotd-type)))))))
1182 :initfunction initfunction
1184 :allocation allocation
1185 :allocation-class allocation-class
1189 (defmethod compute-effective-slot-definition-initargs :around
1190 ((class structure-class) direct-slotds)
1191 (let ((slotd (car direct-slotds)))
1192 (list* :defstruct-accessor-symbol
1193 (slot-definition-defstruct-accessor-symbol slotd)
1194 :internal-reader-function
1195 (slot-definition-internal-reader-function slotd)
1196 :internal-writer-function
1197 (slot-definition-internal-writer-function slotd)
1198 (call-next-method))))
1200 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
1201 ;;; to make the method object. They have to use make-a-method which
1202 ;;; is a specially bootstrapped mechanism for making standard methods.
1203 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
1204 (declare (ignore direct-slot initargs))
1205 (find-class 'standard-reader-method))
1207 (defmethod add-reader-method ((class slot-class) generic-function slot-name)
1208 (add-method generic-function
1209 (make-a-method 'standard-reader-method
1211 (list (or (class-name class) 'object))
1213 (make-reader-method-function class slot-name)
1214 "automatically generated reader method"
1217 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
1218 (declare (ignore direct-slot initargs))
1219 (find-class 'standard-writer-method))
1221 (defmethod add-writer-method ((class slot-class) generic-function slot-name)
1222 (add-method generic-function
1223 (make-a-method 'standard-writer-method
1225 (list 'new-value (or (class-name class) 'object))
1226 (list *the-class-t* class)
1227 (make-writer-method-function class slot-name)
1228 "automatically generated writer method"
1231 (defmethod add-boundp-method ((class slot-class) generic-function slot-name)
1232 (add-method generic-function
1233 (make-a-method 'standard-boundp-method
1235 (list (or (class-name class) 'object))
1237 (make-boundp-method-function class slot-name)
1238 "automatically generated boundp method"
1241 (defmethod remove-reader-method ((class slot-class) generic-function)
1242 (let ((method (get-method generic-function () (list class) nil)))
1243 (when method (remove-method generic-function method))))
1245 (defmethod remove-writer-method ((class slot-class) generic-function)
1247 (get-method generic-function () (list *the-class-t* class) nil)))
1248 (when method (remove-method generic-function method))))
1250 (defmethod remove-boundp-method ((class slot-class) generic-function)
1251 (let ((method (get-method generic-function () (list class) nil)))
1252 (when method (remove-method generic-function method))))
1254 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITE-METHOD function are NOT
1255 ;;; part of the standard protocol. They are however useful, PCL makes
1256 ;;; use of them internally and documents them for PCL users.
1258 ;;; *** This needs work to make type testing by the writer functions which
1259 ;;; *** do type testing faster. The idea would be to have one constructor
1260 ;;; *** for each possible type test.
1262 ;;; *** There is a subtle bug here which is going to have to be fixed.
1263 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1264 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1265 ;;; *** defined for this metaclass a chance to run.
1267 (defmethod make-reader-method-function ((class slot-class) slot-name)
1268 (make-std-reader-method-function (class-name class) slot-name))
1270 (defmethod make-writer-method-function ((class slot-class) slot-name)
1271 (make-std-writer-method-function (class-name class) slot-name))
1273 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1274 (make-std-boundp-method-function (class-name class) slot-name))
1276 (defmethod compatible-meta-class-change-p (class proto-new-class)
1277 (eq (class-of class) (class-of proto-new-class)))
1279 (defmethod validate-superclass ((class class) (new-super class))
1280 (or (eq new-super *the-class-t*)
1281 (eq (class-of class) (class-of new-super))))
1283 (defmethod validate-superclass ((class standard-class) (new-super std-class))
1284 (let ((new-super-meta-class (class-of new-super)))
1285 (or (eq new-super-meta-class *the-class-std-class*)
1286 (eq (class-of class) new-super-meta-class))))
1288 ;;; What this does depends on which of the four possible values of
1289 ;;; LAYOUT-INVALID the PCL wrapper has; the simplest case is when it
1290 ;;; is (:FLUSH <wrapper>) or (:OBSOLETE <wrapper>), when there is
1291 ;;; nothing to do, as the new wrapper has already been created. If
1292 ;;; LAYOUT-INVALID returns NIL, then we invalidate it (setting it to
1293 ;;; (:FLUSH <wrapper>); UPDATE-SLOTS later gets to choose whether or
1294 ;;; not to "upgrade" this to (:OBSOLETE <wrapper>).
1296 ;;; This leaves the case where LAYOUT-INVALID returns T, which happens
1297 ;;; when REGISTER-LAYOUT has invalidated a superclass of CLASS (which
1298 ;;; invalidated all the subclasses in SB-KERNEL land). Again, here we
1299 ;;; must flush the caches and allow UPDATE-SLOTS to decide whether to
1300 ;;; obsolete the wrapper.
1302 ;;; FIXME: either here or in INVALID-WRAPPER-P looks like a good place
1303 ;;; for (AVER (NOT (EQ (LAYOUT-INVALID OWRAPPER)
1304 ;;; :UNINITIALIZED)))
1306 ;;; Thanks to Gerd Moellmann for the explanation. -- CSR, 2002-10-29
1307 (defun force-cache-flushes (class)
1308 (let* ((owrapper (class-wrapper class)))
1309 ;; We only need to do something if the wrapper is still valid. If
1310 ;; the wrapper isn't valid, state will be FLUSH or OBSOLETE, and
1311 ;; both of those will already be doing what we want. In
1312 ;; particular, we must be sure we never change an OBSOLETE into a
1313 ;; FLUSH since OBSOLETE means do what FLUSH does and then some.
1314 (when (or (not (invalid-wrapper-p owrapper))
1315 ;; KLUDGE: despite the observations above, this remains
1316 ;; a violation of locality or what might be considered
1317 ;; good style. There has to be a better way! -- CSR,
1319 (eq (layout-invalid owrapper) t))
1320 (let ((nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1322 (setf (wrapper-instance-slots-layout nwrapper)
1323 (wrapper-instance-slots-layout owrapper))
1324 (setf (wrapper-class-slots nwrapper)
1325 (wrapper-class-slots owrapper))
1327 (update-lisp-class-layout class nwrapper)
1328 (setf (slot-value class 'wrapper) nwrapper)
1329 ;; Use :OBSOLETE instead of :FLUSH if any superclass has
1331 (if (find-if (lambda (x)
1332 (and (consp x) (eq :obsolete (car x))))
1333 (layout-inherits owrapper)
1334 :key #'layout-invalid)
1335 (invalidate-wrapper owrapper :obsolete nwrapper)
1336 (invalidate-wrapper owrapper :flush nwrapper)))))))
1338 (defun flush-cache-trap (owrapper nwrapper instance)
1339 (declare (ignore owrapper))
1340 (set-wrapper instance nwrapper))
1342 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1343 ;;; the next access to the instance (as defined in 88-002R) to trap
1344 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1345 (defmethod make-instances-obsolete ((class std-class))
1346 (let* ((owrapper (class-wrapper class))
1347 (nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1349 (setf (wrapper-instance-slots-layout nwrapper)
1350 (wrapper-instance-slots-layout owrapper))
1351 (setf (wrapper-class-slots nwrapper)
1352 (wrapper-class-slots owrapper))
1354 (update-lisp-class-layout class nwrapper)
1355 (setf (slot-value class 'wrapper) nwrapper)
1356 (invalidate-wrapper owrapper :obsolete nwrapper)
1359 (defmethod make-instances-obsolete ((class symbol))
1360 (make-instances-obsolete (find-class class))
1361 ;; ANSI wants the class name when called with a symbol.
1364 ;;; OBSOLETE-INSTANCE-TRAP is the internal trap that is called when we
1365 ;;; see an obsolete instance. The times when it is called are:
1366 ;;; - when the instance is involved in method lookup
1367 ;;; - when attempting to access a slot of an instance
1369 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1370 ;;; instance access macros.
1372 ;;; Of course these times when it is called are an internal
1373 ;;; implementation detail of PCL and are not part of the documented
1374 ;;; description of when the obsolete instance update happens. The
1375 ;;; documented description is as it appears in 88-002R.
1377 ;;; This has to return the new wrapper, so it counts on all the
1378 ;;; methods on obsolete-instance-trap-internal to return the new
1379 ;;; wrapper. It also does a little internal error checking to make
1380 ;;; sure that the traps are only happening when they should, and that
1381 ;;; the trap methods are computing appropriate new wrappers.
1383 ;;; OBSOLETE-INSTANCE-TRAP might be called on structure instances
1384 ;;; after a structure is redefined. In most cases,
1385 ;;; OBSOLETE-INSTANCE-TRAP will not be able to fix the old instance,
1386 ;;; so it must signal an error. The hard part of this is that the
1387 ;;; error system and debugger might cause OBSOLETE-INSTANCE-TRAP to be
1388 ;;; called again, so in that case, we have to return some reasonable
1389 ;;; wrapper, instead.
1391 (defvar *in-obsolete-instance-trap* nil)
1392 (defvar *the-wrapper-of-structure-object*
1393 (class-wrapper (find-class 'structure-object)))
1395 (define-condition obsolete-structure (error)
1396 ((datum :reader obsolete-structure-datum :initarg :datum))
1398 (lambda (condition stream)
1399 ;; Don't try to print the structure, since it probably won't work.
1401 "~@<obsolete structure error for a structure of type ~2I~_~S~:>"
1402 (type-of (obsolete-structure-datum condition))))))
1404 (defun obsolete-instance-trap (owrapper nwrapper instance)
1405 (if (not (pcl-instance-p instance))
1406 (if *in-obsolete-instance-trap*
1407 *the-wrapper-of-structure-object*
1408 (let ((*in-obsolete-instance-trap* t))
1409 (error 'obsolete-structure :datum instance)))
1410 (let* ((class (wrapper-class* nwrapper))
1411 (copy (allocate-instance class)) ;??? allocate-instance ???
1412 (olayout (wrapper-instance-slots-layout owrapper))
1413 (nlayout (wrapper-instance-slots-layout nwrapper))
1414 (oslots (get-slots instance))
1415 (nslots (get-slots copy))
1416 (oclass-slots (wrapper-class-slots owrapper))
1421 ;; local --> local transfer value
1422 ;; local --> shared discard value, discard slot
1423 ;; local --> -- discard slot
1424 ;; shared --> local transfer value
1425 ;; shared --> shared -- (cf SHARED-INITIALIZE :AFTER STD-CLASS)
1426 ;; shared --> -- discard value
1427 ;; -- --> local add slot
1430 ;; Collect class slots from inherited wrappers. Needed for
1431 ;; shared -> local transfers of inherited slots.
1432 (let ((inherited (layout-inherits owrapper)))
1433 (loop for i from (1- (length inherited)) downto 0
1434 for layout = (aref inherited i)
1435 when (typep layout 'wrapper)
1436 do (dolist (slot (wrapper-class-slots layout))
1437 (pushnew slot oclass-slots :key #'car))))
1439 ;; Go through all the old local slots.
1441 (dolist (name olayout)
1442 (let ((npos (posq name nlayout)))
1444 (setf (clos-slots-ref nslots npos)
1445 (clos-slots-ref oslots opos))
1447 (push name discarded)
1448 (unless (eq (clos-slots-ref oslots opos) +slot-unbound+)
1449 (setf (getf plist name) (clos-slots-ref oslots opos))))))
1452 ;; Go through all the old shared slots.
1453 (dolist (oclass-slot-and-val oclass-slots)
1454 (let ((name (car oclass-slot-and-val))
1455 (val (cdr oclass-slot-and-val)))
1456 (let ((npos (posq name nlayout)))
1458 (setf (clos-slots-ref nslots npos) val)))))
1460 ;; Go through all the new local slots to compute the added slots.
1461 (dolist (nlocal nlayout)
1462 (unless (or (memq nlocal olayout)
1463 (assq nlocal oclass-slots))
1464 (push nlocal added)))
1466 (swap-wrappers-and-slots instance copy)
1468 (update-instance-for-redefined-class instance
1474 (defun change-class-internal (instance new-class initargs)
1475 (let* ((old-class (class-of instance))
1476 (copy (allocate-instance new-class))
1477 (new-wrapper (get-wrapper copy))
1478 (old-wrapper (class-wrapper old-class))
1479 (old-layout (wrapper-instance-slots-layout old-wrapper))
1480 (new-layout (wrapper-instance-slots-layout new-wrapper))
1481 (old-slots (get-slots instance))
1482 (new-slots (get-slots copy))
1483 (old-class-slots (wrapper-class-slots old-wrapper)))
1485 ;; "The values of local slots specified by both the class CTO and
1486 ;; CFROM are retained. If such a local slot was unbound, it
1487 ;; remains unbound."
1488 (let ((new-position 0))
1489 (dolist (new-slot new-layout)
1490 (let ((old-position (posq new-slot old-layout)))
1492 (setf (clos-slots-ref new-slots new-position)
1493 (clos-slots-ref old-slots old-position))))
1494 (incf new-position)))
1496 ;; "The values of slots specified as shared in the class CFROM and
1497 ;; as local in the class CTO are retained."
1498 (dolist (slot-and-val old-class-slots)
1499 (let ((position (posq (car slot-and-val) new-layout)))
1501 (setf (clos-slots-ref new-slots position) (cdr slot-and-val)))))
1503 ;; Make the copy point to the old instance's storage, and make the
1504 ;; old instance point to the new storage.
1505 (swap-wrappers-and-slots instance copy)
1507 (apply #'update-instance-for-different-class copy instance initargs)
1510 (defmethod change-class ((instance standard-object)
1511 (new-class standard-class)
1513 (change-class-internal instance new-class initargs))
1515 (defmethod change-class ((instance funcallable-standard-object)
1516 (new-class funcallable-standard-class)
1518 (change-class-internal instance new-class initargs))
1520 (defmethod change-class ((instance standard-object)
1521 (new-class funcallable-standard-class)
1523 (declare (ignore initargs))
1524 (error "You can't change the class of ~S to ~S~@
1525 because it isn't already an instance with metaclass ~S."
1526 instance new-class 'standard-class))
1528 (defmethod change-class ((instance funcallable-standard-object)
1529 (new-class standard-class)
1531 (declare (ignore initargs))
1532 (error "You can't change the class of ~S to ~S~@
1533 because it isn't already an instance with metaclass ~S."
1534 instance new-class 'funcallable-standard-class))
1536 (defmethod change-class ((instance t) (new-class-name symbol) &rest initargs)
1537 (apply #'change-class instance (find-class new-class-name) initargs))
1539 ;;;; The metaclass BUILT-IN-CLASS
1541 ;;;; This metaclass is something of a weird creature. By this point, all
1542 ;;;; instances of it which will exist have been created, and no instance
1543 ;;;; is ever created by calling MAKE-INSTANCE.
1545 ;;;; But, there are other parts of the protocol we must follow and those
1546 ;;;; definitions appear here.
1548 (defmethod shared-initialize :before
1549 ((class built-in-class) slot-names &rest initargs)
1550 (declare (ignore slot-names initargs))
1551 (error "attempt to initialize or reinitialize a built in class"))
1553 (defmethod class-direct-slots ((class built-in-class)) ())
1554 (defmethod class-slots ((class built-in-class)) ())
1555 (defmethod class-direct-default-initargs ((class built-in-class)) ())
1556 (defmethod class-default-initargs ((class built-in-class)) ())
1558 (defmethod validate-superclass ((c class) (s built-in-class))
1559 (or (eq s *the-class-t*)
1560 (eq s *the-class-stream*)))
1562 ;;; Some necessary methods for FORWARD-REFERENCED-CLASS
1563 (defmethod class-direct-slots ((class forward-referenced-class)) ())
1564 (defmethod class-direct-default-initargs ((class forward-referenced-class)) ())
1565 (macrolet ((def (method)
1566 `(defmethod ,method ((class forward-referenced-class))
1567 (error "~@<~I~S was called on a forward referenced class:~2I~_~S~:>"
1569 (def class-default-initargs)
1570 (def class-precedence-list)
1573 (defmethod validate-superclass ((c slot-class)
1574 (f forward-referenced-class))
1577 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1578 (pushnew dependent (plist-value metaobject 'dependents)))
1580 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1581 (setf (plist-value metaobject 'dependents)
1582 (delete dependent (plist-value metaobject 'dependents))))
1584 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1585 (dolist (dependent (plist-value metaobject 'dependents))
1586 (funcall function dependent)))