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 (defmethod compute-slot-accessor-info ((slotd effective-slot-definition)
91 (let* ((name (slot-value slotd 'name))
92 (class (slot-value slotd 'class))
93 (old-slotd (find-slot-definition class name))
94 (old-std-p (and old-slotd (slot-accessor-std-p old-slotd 'all))))
95 (multiple-value-bind (function std-p)
96 (if (eq *boot-state* 'complete)
97 (get-accessor-method-function gf type class slotd)
98 (get-optimized-std-accessor-method-function class slotd type))
99 (setf (slot-accessor-std-p slotd type) std-p)
100 (setf (slot-accessor-function slotd type) function))
101 (when (and old-slotd (not (eq old-std-p (slot-accessor-std-p slotd 'all))))
102 (push (cons class name) *pv-table-cache-update-info*))))
104 (defmethod slot-definition-allocation ((slotd structure-slot-definition))
107 (defmethod shared-initialize :after ((object documentation-mixin)
109 &key (documentation nil documentation-p))
110 (declare (ignore slot-names))
111 (when documentation-p
112 (setf (plist-value object 'documentation) documentation)))
114 ;;; default if DOC-TYPE doesn't match one of the specified types
115 (defmethod documentation (object doc-type)
116 (warn "unsupported DOCUMENTATION: type ~S for object ~S"
121 ;;; default if DOC-TYPE doesn't match one of the specified types
122 (defmethod (setf documentation) (new-value object doc-type)
123 ;; CMU CL made this an error, but since ANSI says that even for supported
124 ;; doc types an implementation is permitted to discard docs at any time
125 ;; for any reason, this feels to me more like a warning. -- WHN 19991214
126 (warn "discarding unsupported DOCUMENTATION of type ~S for object ~S"
131 (defmethod documentation ((object documentation-mixin) doc-type)
132 (declare (ignore doc-type))
133 (plist-value object 'documentation))
135 (defmethod (setf documentation) (new-value
136 (object documentation-mixin)
138 (declare (ignore doc-type))
139 (setf (plist-value object 'documentation) new-value))
141 (defmethod documentation ((slotd standard-slot-definition) doc-type)
142 (declare (ignore doc-type))
143 (slot-value slotd 'documentation))
145 (defmethod (setf documentation) (new-value
146 (slotd standard-slot-definition)
148 (declare (ignore doc-type))
149 (setf (slot-value slotd 'documentation) new-value))
151 ;;;; various class accessors that are a little more complicated than can be
152 ;;;; done with automatically generated reader methods
154 (defmethod class-prototype ((class std-class))
155 (with-slots (prototype) class
156 (or prototype (setq prototype (allocate-instance class)))))
158 (defmethod class-prototype ((class structure-class))
159 (with-slots (prototype wrapper defstruct-constructor) class
162 (if defstruct-constructor
163 (allocate-instance class)
164 (allocate-standard-instance wrapper))))))
166 (defmethod class-direct-default-initargs ((class slot-class))
167 (plist-value class 'direct-default-initargs))
169 (defmethod class-default-initargs ((class slot-class))
170 (plist-value class 'default-initargs))
172 (defmethod class-slot-cells ((class std-class))
173 (plist-value class 'class-slot-cells))
175 ;;;; class accessors that are even a little bit more complicated than those
176 ;;;; above. These have a protocol for updating them, we must implement that
179 ;;; Maintaining the direct subclasses backpointers. The update methods are
180 ;;; here, the values are read by an automatically generated reader method.
181 (defmethod add-direct-subclass ((class class) (subclass class))
182 (with-slots (direct-subclasses) class
183 (pushnew subclass direct-subclasses)
185 (defmethod remove-direct-subclass ((class class) (subclass class))
186 (with-slots (direct-subclasses) class
187 (setq direct-subclasses (remove subclass direct-subclasses))
190 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
192 ;;; There are four generic functions involved, each has one method for the
193 ;;; class case and another method for the damned EQL specializers. All of
194 ;;; these are specified methods and appear in their specified place in the
197 ;;; ADD-DIRECT-METHOD
198 ;;; REMOVE-DIRECT-METHOD
199 ;;; SPECIALIZER-DIRECT-METHODS
200 ;;; SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
202 ;;; In each case, we maintain one value which is a cons. The car is the list
203 ;;; methods. The cdr is a list of the generic functions. The cdr is always
205 (defmethod add-direct-method ((specializer class) (method method))
206 (with-slots (direct-methods) specializer
207 (setf (car direct-methods) (adjoin method (car direct-methods)) ;PUSH
208 (cdr direct-methods) ()))
210 (defmethod remove-direct-method ((specializer class) (method method))
211 (with-slots (direct-methods) specializer
212 (setf (car direct-methods) (remove method (car direct-methods))
213 (cdr direct-methods) ()))
216 (defmethod specializer-direct-methods ((specializer class))
217 (with-slots (direct-methods) specializer
218 (car direct-methods)))
220 (defmethod specializer-direct-generic-functions ((specializer class))
221 (with-slots (direct-methods) specializer
222 (or (cdr direct-methods)
223 (setf (cdr direct-methods)
225 (dolist (m (car direct-methods))
226 ;; the old PCL code used COLLECTING-ONCE which used
227 ;; #'EQ to check for newness
228 (pushnew (method-generic-function m) collect :test #'eq))
229 (nreverse collect))))))
231 ;;; This hash table is used to store the direct methods and direct generic
232 ;;; functions of EQL specializers. Each value in the table is the cons.
233 (defvar *eql-specializer-methods* (make-hash-table :test 'eql))
234 (defvar *class-eq-specializer-methods* (make-hash-table :test 'eq))
236 (defmethod specializer-method-table ((specializer eql-specializer))
237 *eql-specializer-methods*)
239 (defmethod specializer-method-table ((specializer class-eq-specializer))
240 *class-eq-specializer-methods*)
242 (defmethod add-direct-method ((specializer specializer-with-object)
244 (let* ((object (specializer-object specializer))
245 (table (specializer-method-table specializer))
246 (entry (gethash object table)))
249 (setf (gethash object table)
251 (setf (car entry) (adjoin method (car entry))
255 (defmethod remove-direct-method ((specializer specializer-with-object)
257 (let* ((object (specializer-object specializer))
258 (entry (gethash object (specializer-method-table specializer))))
260 (setf (car entry) (remove method (car entry))
264 (defmethod specializer-direct-methods ((specializer specializer-with-object))
265 (car (gethash (specializer-object specializer)
266 (specializer-method-table specializer))))
268 (defmethod specializer-direct-generic-functions ((specializer
269 specializer-with-object))
270 (let* ((object (specializer-object specializer))
271 (entry (gethash object (specializer-method-table specializer))))
276 (dolist (m (car entry))
277 (pushnew (method-generic-function m) collect :test #'eq))
278 (nreverse collect)))))))
280 (defun map-specializers (function)
281 (map-all-classes (lambda (class)
282 (funcall function (class-eq-specializer class))
283 (funcall function class)))
284 (maphash (lambda (object methods)
285 (declare (ignore methods))
286 (intern-eql-specializer object))
287 *eql-specializer-methods*)
288 (maphash (lambda (object specl)
289 (declare (ignore object))
290 (funcall function specl))
291 *eql-specializer-table*)
294 (defun map-all-generic-functions (function)
295 (let ((all-generic-functions (make-hash-table :test 'eq)))
296 (map-specializers (lambda (specl)
297 (dolist (gf (specializer-direct-generic-functions
299 (unless (gethash gf all-generic-functions)
300 (setf (gethash gf all-generic-functions) t)
301 (funcall function gf))))))
304 (defmethod shared-initialize :after ((specl class-eq-specializer)
307 (declare (ignore slot-names))
308 (setf (slot-value specl 'type) `(class-eq ,(specializer-class specl))))
310 (defmethod shared-initialize :after ((specl eql-specializer) slot-names &key)
311 (declare (ignore slot-names))
312 (setf (slot-value specl 'type) `(eql ,(specializer-object specl))))
314 (defun real-load-defclass (name metaclass-name supers slots other)
315 (let ((res (apply #'ensure-class name :metaclass metaclass-name
316 :direct-superclasses supers
318 :definition-source `((defclass ,name)
323 (setf (gdefinition 'load-defclass) #'real-load-defclass)
325 (defun ensure-class (name &rest all)
326 (apply #'ensure-class-using-class (find-class name nil) name all))
328 (defmethod ensure-class-using-class ((class null) name &rest args &key)
329 (multiple-value-bind (meta initargs)
330 (ensure-class-values class args)
331 (set-class-type-translation (class-prototype meta) name)
332 (setf class (apply #'make-instance meta :name name initargs)
333 (find-class name) class)
334 (set-class-type-translation class name)
337 (defmethod ensure-class-using-class ((class pcl-class) name &rest args &key)
338 (multiple-value-bind (meta initargs)
339 (ensure-class-values class args)
340 (unless (eq (class-of class) meta)
341 (apply #'change-class class meta initargs))
342 (apply #'reinitialize-instance class initargs)
343 (setf (find-class name) class)
344 (set-class-type-translation class name)
347 (defmethod class-predicate-name ((class t))
352 ((not (legal-class-name-p s))
353 (error "~S is not a class or a legal class name." s))
355 (or (find-class s nil)
356 (make-instance 'forward-referenced-class
359 (defun ensure-class-values (class args)
360 (let* ((initargs (copy-list args))
361 (unsupplied (list 1))
362 (supplied-meta (getf initargs :metaclass unsupplied))
363 (supplied-supers (getf initargs :direct-superclasses unsupplied))
364 (supplied-slots (getf initargs :direct-slots unsupplied))
366 (cond ((neq supplied-meta unsupplied)
367 (find-class supplied-meta))
369 (forward-referenced-class-p class))
370 *the-class-standard-class*)
373 ;; KLUDGE: It seemed to me initially that there ought to be a way
374 ;; of collecting all the erroneous problems in one go, rather than
375 ;; this way of solving the problem of signalling the errors that
376 ;; we are required to, which stops at the first bogus input.
377 ;; However, after playing around a little, I couldn't find that
378 ;; way, so I've left it as is, but if someone does come up with a
379 ;; better way... -- CSR, 2002-09-08
380 (do ((direct-slots (getf initargs :direct-slots) (cdr direct-slots)))
381 ((endp direct-slots) nil)
382 (destructuring-bind (slot &rest more) direct-slots
383 (let ((slot-name (getf slot :name)))
384 (when (some (lambda (s) (eq slot-name (getf s :name))) more)
385 ;; FIXME: It's quite possible that we ought to define an
386 ;; SB-INT:PROGRAM-ERROR function to signal these and other
387 ;; errors throughout the codebase that are required to be
388 ;; of type PROGRAM-ERROR.
389 (error 'simple-program-error
390 :format-control "~@<There is more than one direct slot ~
392 :format-arguments (list slot-name)))
393 (do ((stuff slot (cddr stuff)))
395 (destructuring-bind (option value &rest more) stuff
397 ((and (member option '(:allocation :type
398 :initform :documentation))
400 (getf more option unsupplied))))
401 (error 'simple-program-error
402 :format-control "~@<Duplicate slot option ~S for ~
404 :format-arguments (list option slot-name)))
405 ((and (eq option :readers)
406 (notevery #'symbolp value))
407 (error 'simple-program-error
408 :format-control "~@<Slot reader names for slot ~
409 named ~S must be symbols.~:>"
410 :format-arguments (list slot-name)))
411 ((and (eq option :initargs)
412 (notevery #'symbolp value))
413 (error 'simple-program-error
414 :format-control "~@<Slot initarg names for slot ~
415 named ~S must be symbols.~:>"
416 :format-arguments (list slot-name)))))))))
417 (loop for (initarg . more) on (getf initargs :direct-default-initargs)
418 for name = (car initarg)
419 when (some (lambda (a) (eq (car a) name)) more)
420 do (error 'simple-program-error
421 :format-control "~@<Duplicate initialization argument ~
422 name ~S in :DEFAULT-INITARGS.~:>"
423 :format-arguments (list name class)))
425 (default-initargs 0))
426 (do ((args initargs (cddr args)))
430 (when (> (incf metaclass) 1)
431 (error 'simple-program-error
432 :format-control "~@<More than one :METACLASS ~
433 option specified.~:>")))
434 (:direct-default-initargs
435 (when (> (incf default-initargs) 1)
436 (error 'simple-program-error
437 :format-control "~@<More than one :DEFAULT-INITARGS ~
438 option specified.~:>"))))))
439 (remf initargs :metaclass)
440 (loop (unless (remf initargs :direct-superclasses) (return)))
441 (loop (unless (remf initargs :direct-slots) (return)))
445 (when (neq supplied-supers unsupplied)
446 (list :direct-superclasses (mapcar #'fix-super supplied-supers)))
447 (when (neq supplied-slots unsupplied)
448 (list :direct-slots supplied-slots))
451 (defmethod shared-initialize :after
454 &key (direct-superclasses nil direct-superclasses-p)
455 (direct-slots nil direct-slots-p)
456 (direct-default-initargs nil direct-default-initargs-p)
457 (predicate-name nil predicate-name-p))
458 (declare (ignore slot-names))
459 (cond (direct-superclasses-p
460 (setq direct-superclasses
461 (or direct-superclasses
462 (list (if (funcallable-standard-class-p class)
463 *the-class-funcallable-standard-object*
464 *the-class-standard-object*))))
465 (dolist (superclass direct-superclasses)
466 (unless (validate-superclass class superclass)
467 (error "The class ~S was specified as a~%
468 super-class of the class ~S;~%~
469 but the meta-classes ~S and~%~S are incompatible.~@
470 Define a method for ~S to avoid this error."
471 superclass class (class-of superclass) (class-of class)
472 'validate-superclass)))
473 (setf (slot-value class 'direct-superclasses) direct-superclasses))
475 (setq direct-superclasses (slot-value class 'direct-superclasses))))
478 (setf (slot-value class 'direct-slots)
479 (mapcar (lambda (pl) (make-direct-slotd class pl))
481 (slot-value class 'direct-slots)))
482 (if direct-default-initargs-p
483 (setf (plist-value class 'direct-default-initargs)
484 direct-default-initargs)
485 (setq direct-default-initargs
486 (plist-value class 'direct-default-initargs)))
487 (setf (plist-value class 'class-slot-cells)
489 (dolist (dslotd direct-slots)
490 (when (eq :class (slot-definition-allocation dslotd))
491 (let ((initfunction (slot-definition-initfunction dslotd)))
492 (push (cons (slot-definition-name dslotd)
494 (funcall initfunction)
498 (setq predicate-name (if predicate-name-p
499 (setf (slot-value class 'predicate-name)
500 (car predicate-name))
501 (or (slot-value class 'predicate-name)
502 (setf (slot-value class 'predicate-name)
503 (make-class-predicate-name (class-name
505 (add-direct-subclasses class direct-superclasses)
506 (make-class-predicate class predicate-name)
507 (update-class class nil)
508 (do* ((slots (slot-value class 'slots) (cdr slots))
510 ((null slots) (when dupes
512 ;; FIXME: the indentation request ("~4I")
513 ;; below appears not to do anything. Finding
514 ;; out why would be nice. -- CSR, 2003-04-24
515 "~@<slot names with the same SYMBOL-NAME but ~
516 different SYMBOL-PACKAGE (possible package problem) ~
517 for class ~S:~@:_~4I~<~@{~S~^~:@_~}~:>~@:>"
520 (let* ((slot (car slots))
521 (oslots (remove (slot-definition-name slot) (cdr slots)
522 :test-not #'string= :key #'slot-definition-name)))
524 (pushnew (cons (slot-definition-name slot)
525 (mapcar #'slot-definition-name oslots))
527 :test #'string= :key #'car))))
528 (add-slot-accessors class direct-slots)
529 (make-preliminary-layout class))
531 (defmethod shared-initialize :after ((class forward-referenced-class)
532 slot-names &key &allow-other-keys)
533 (declare (ignore slot-names))
534 (make-preliminary-layout class))
536 (defvar *allow-forward-referenced-classes-in-cpl-p* nil)
538 ;;; Give CLASS a preliminary layout if it doesn't have one already, to
539 ;;; make it known to the type system.
540 (defun make-preliminary-layout (class)
541 (flet ((compute-preliminary-cpl (root)
542 (let ((*allow-forward-referenced-classes-in-cpl-p* t))
543 (compute-class-precedence-list root))))
544 (unless (class-finalized-p class)
545 (let ((name (class-name class)))
546 (setf (find-class name) class)
547 ;; KLUDGE: This is fairly horrible. We need to make a
548 ;; full-fledged CLASSOID here, not just tell the compiler that
549 ;; some class is forthcoming, because there are legitimate
550 ;; questions one can ask of the type system, implemented in
551 ;; terms of CLASSOIDs, involving forward-referenced classes. So.
552 (when (and (eq *boot-state* 'complete)
553 (null (find-classoid name nil)))
554 (setf (find-classoid name)
555 (make-standard-classoid :name name)))
556 (set-class-type-translation class name)
557 (let ((layout (make-wrapper 0 class))
558 (classoid (find-classoid name)))
559 (setf (layout-classoid layout) classoid)
560 (setf (classoid-pcl-class classoid) class)
561 (setf (slot-value class 'wrapper) layout)
562 (let ((cpl (compute-preliminary-cpl class)))
563 (setf (layout-inherits layout)
564 (order-layout-inherits
565 (map 'simple-vector #'class-wrapper
566 (reverse (rest cpl))))))
567 (register-layout layout :invalidate t)
568 (setf (classoid-layout classoid) layout)
569 (mapc #'make-preliminary-layout (class-direct-subclasses class)))))))
572 (defmethod shared-initialize :before ((class class) slot-names &key name)
573 (declare (ignore slot-names name))
574 ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
575 ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
576 (setf (slot-value class 'type) `(class ,class))
577 (setf (slot-value class 'class-eq-specializer)
578 (make-instance 'class-eq-specializer :class class)))
580 (defmethod reinitialize-instance :before ((class slot-class) &key)
581 (remove-direct-subclasses class (class-direct-superclasses class))
582 (remove-slot-accessors class (class-direct-slots class)))
584 (defmethod reinitialize-instance :after ((class slot-class)
587 (map-dependents class
589 (apply #'update-dependent class dependent initargs))))
591 (defmethod shared-initialize :after ((class condition-class) slot-names
592 &key direct-superclasses)
593 (declare (ignore slot-names))
594 (let ((classoid (find-classoid (class-name class))))
595 (with-slots (wrapper class-precedence-list prototype predicate-name
596 (direct-supers direct-superclasses))
598 (setf (slot-value class 'finalized-p) t)
599 (setf (classoid-pcl-class classoid) class)
600 (setq direct-supers direct-superclasses)
601 (setq wrapper (classoid-layout classoid))
602 (setq class-precedence-list (compute-class-precedence-list class))
603 (setq prototype (make-condition (class-name class)))
604 (add-direct-subclasses class direct-superclasses)
605 (setq predicate-name (make-class-predicate-name (class-name class)))
606 (make-class-predicate class predicate-name))))
608 (defmethod shared-initialize :after
609 ((slotd structure-slot-definition) slot-names &key
610 (allocation :instance) allocation-class)
611 (declare (ignore slot-names allocation-class))
612 (unless (eq allocation :instance)
613 (error "Structure slots must have :INSTANCE allocation.")))
615 (defun make-structure-class-defstruct-form (name direct-slots include)
616 (let* ((conc-name (intern (format nil "~S structure class " name)))
617 (constructor (intern (format nil "~Aconstructor" conc-name)))
618 (defstruct `(defstruct (,name
620 `((:include ,(class-name include))))
622 (:conc-name ,conc-name)
623 (:constructor ,constructor ())
625 ,@(mapcar (lambda (slot)
626 `(,(slot-definition-name slot)
629 (reader-names (mapcar (lambda (slotd)
630 (list 'slot-accessor name
631 (slot-definition-name slotd)
634 (writer-names (mapcar (lambda (slotd)
635 (list 'slot-accessor name
636 (slot-definition-name slotd)
640 (mapcar (lambda (slotd reader-name)
642 (slot-definition-defstruct-accessor-symbol
644 `(defun ,reader-name (obj)
645 (declare (type ,name obj))
647 direct-slots reader-names))
649 (mapcar (lambda (slotd writer-name)
651 (slot-definition-defstruct-accessor-symbol
653 `(defun ,writer-name (nv obj)
654 (declare (type ,name obj))
655 (setf (,accessor obj) nv))))
656 direct-slots writer-names))
660 ,@readers-init ,@writers-init
662 (values defstruct-form constructor reader-names writer-names)))
664 (defmethod shared-initialize :after
665 ((class structure-class)
667 &key (direct-superclasses nil direct-superclasses-p)
668 (direct-slots nil direct-slots-p)
669 direct-default-initargs
670 (predicate-name nil predicate-name-p))
671 (declare (ignore slot-names direct-default-initargs))
672 (if direct-superclasses-p
673 (setf (slot-value class 'direct-superclasses)
674 (or direct-superclasses
675 (setq direct-superclasses
676 (and (not (eq (class-name class) 'structure-object))
677 (list *the-class-structure-object*)))))
678 (setq direct-superclasses (slot-value class 'direct-superclasses)))
679 (let* ((name (class-name class))
680 (from-defclass-p (slot-value class 'from-defclass-p))
681 (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
683 (setf (slot-value class 'direct-slots)
687 (let* ((slot-name (getf pl :name))
690 "~S structure class ~A"
692 (accessor (intern acc-name)))
693 (setq pl (list* :defstruct-accessor-symbol
695 (make-direct-slotd class pl))
697 (setq direct-slots (slot-value class 'direct-slots)))
699 (let ((include (car (slot-value class 'direct-superclasses))))
700 (multiple-value-bind (defstruct-form constructor reader-names writer-names)
701 (make-structure-class-defstruct-form name direct-slots include)
702 (unless (structure-type-p name) (eval defstruct-form))
703 (mapc (lambda (dslotd reader-name writer-name)
704 (let* ((reader (gdefinition reader-name))
705 (writer (when (gboundp writer-name)
706 (gdefinition writer-name))))
707 (setf (slot-value dslotd 'internal-reader-function)
709 (setf (slot-value dslotd 'internal-writer-function)
711 direct-slots reader-names writer-names)
712 (setf (slot-value class 'defstruct-form) defstruct-form)
713 (setf (slot-value class 'defstruct-constructor) constructor))))
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 'slots) (compute-slots class))
718 (let ((lclass (find-classoid (class-name class))))
719 (setf (classoid-pcl-class lclass) class)
720 (setf (slot-value class 'wrapper) (classoid-layout lclass)))
721 (setf (slot-value class 'finalized-p) t)
722 (update-pv-table-cache-info class)
723 (setq predicate-name (if predicate-name-p
724 (setf (slot-value class 'predicate-name)
725 (car predicate-name))
726 (or (slot-value class 'predicate-name)
727 (setf (slot-value class 'predicate-name)
728 (make-class-predicate-name
729 (class-name class))))))
730 (make-class-predicate class predicate-name)
731 (add-slot-accessors class direct-slots)))
733 (defmethod direct-slot-definition-class ((class structure-class) initargs)
734 (declare (ignore initargs))
735 (find-class 'structure-direct-slot-definition))
737 (defmethod finalize-inheritance ((class structure-class))
738 nil) ; always finalized
740 (defun add-slot-accessors (class dslotds)
741 (fix-slot-accessors class dslotds 'add))
743 (defun remove-slot-accessors (class dslotds)
744 (fix-slot-accessors class dslotds 'remove))
746 (defun fix-slot-accessors (class dslotds add/remove)
747 (flet ((fix (gfspec name r/w)
748 (let ((gf (ensure-generic-function gfspec)))
750 (r (if (eq add/remove 'add)
751 (add-reader-method class gf name)
752 (remove-reader-method class gf)))
753 (w (if (eq add/remove 'add)
754 (add-writer-method class gf name)
755 (remove-writer-method class gf)))))))
756 (dolist (dslotd dslotds)
757 (let ((slot-name (slot-definition-name dslotd)))
758 (dolist (r (slot-definition-readers dslotd)) (fix r slot-name 'r))
759 (dolist (w (slot-definition-writers dslotd)) (fix w slot-name 'w))))))
761 (defun add-direct-subclasses (class supers)
762 (dolist (super supers)
763 (unless (memq class (class-direct-subclasses class))
764 (add-direct-subclass super class))))
766 (defun remove-direct-subclasses (class supers)
767 (let ((old (class-direct-superclasses class)))
768 (dolist (o (set-difference old supers))
769 (remove-direct-subclass o class))))
771 (defmethod finalize-inheritance ((class std-class))
772 (update-class class t))
774 (defmethod finalize-inheritance ((class forward-referenced-class))
775 ;; FIXME: should we not be thinking a bit about what kinds of error
776 ;; we're throwing? Maybe we need a clos-error type to mix in? Or
777 ;; possibly a forward-referenced-class-error, though that's
778 ;; difficult given e.g. class precedence list calculations...
780 "~@<FINALIZE-INHERITANCE was called on a forward referenced class:~
785 (defun class-has-a-forward-referenced-superclass-p (class)
786 (or (forward-referenced-class-p class)
787 (some #'class-has-a-forward-referenced-superclass-p
788 (class-direct-superclasses class))))
790 ;;; This is called by :after shared-initialize whenever a class is initialized
791 ;;; or reinitialized. The class may or may not be finalized.
792 (defun update-class (class finalizep)
793 ;; Comment from Gerd Moellmann:
795 ;; Note that we can't simply delay the finalization when CLASS has
796 ;; no forward referenced superclasses because that causes bootstrap
798 (when (and (not finalizep)
799 (not (class-finalized-p class))
800 (not (class-has-a-forward-referenced-superclass-p class)))
801 (finalize-inheritance class)
802 (return-from update-class))
803 (when (or finalizep (class-finalized-p class)
804 (not (class-has-a-forward-referenced-superclass-p class)))
805 (setf (find-class (class-name class)) class)
806 (update-cpl class (compute-class-precedence-list class))
807 ;; This invocation of UPDATE-SLOTS, in practice, finalizes the
808 ;; class. The hoops above are to ensure that FINALIZE-INHERITANCE
809 ;; is called at finalization, so that MOP programmers can hook
810 ;; into the system as described in "Class Finalization Protocol"
811 ;; (section 5.5.2 of AMOP).
812 (update-slots class (compute-slots class))
813 (update-gfs-of-class class)
814 (update-inits class (compute-default-initargs class))
815 (update-ctors 'finalize-inheritance :class class))
817 (dolist (sub (class-direct-subclasses class)) (update-class sub nil))))
819 (defun update-cpl (class cpl)
820 (if (class-finalized-p class)
821 (unless (equal (class-precedence-list class) cpl)
822 ;; comment from the old CMU CL sources:
823 ;; Need to have the cpl setup before update-lisp-class-layout
824 ;; is called on CMU CL.
825 (setf (slot-value class 'class-precedence-list) cpl)
826 (force-cache-flushes class))
827 (setf (slot-value class 'class-precedence-list) cpl))
828 (update-class-can-precede-p cpl))
830 (defun update-class-can-precede-p (cpl)
832 (let ((first (car cpl)))
833 (dolist (c (cdr cpl))
834 (pushnew c (slot-value first 'can-precede-list))))
835 (update-class-can-precede-p (cdr cpl))))
837 (defun class-can-precede-p (class1 class2)
838 (member class2 (class-can-precede-list class1)))
840 (defun update-slots (class eslotds)
841 (let ((instance-slots ())
843 (dolist (eslotd eslotds)
844 (let ((alloc (slot-definition-allocation eslotd)))
846 (:instance (push eslotd instance-slots))
847 (:class (push eslotd class-slots)))))
849 ;; If there is a change in the shape of the instances then the
850 ;; old class is now obsolete.
851 (let* ((nlayout (mapcar #'slot-definition-name
852 (sort instance-slots #'<
853 :key #'slot-definition-location)))
854 (nslots (length nlayout))
855 (nwrapper-class-slots (compute-class-slots class-slots))
856 (owrapper (when (class-finalized-p class)
857 (class-wrapper class)))
858 (olayout (when owrapper
859 (wrapper-instance-slots-layout owrapper)))
860 (owrapper-class-slots (and owrapper (wrapper-class-slots owrapper)))
862 (cond ((null owrapper)
863 (make-wrapper nslots class))
864 ((and (equal nlayout olayout)
866 (loop for o in owrapper-class-slots
867 for n in nwrapper-class-slots
868 do (unless (eq (car o) (car n)) (return t)))))
871 ;; This will initialize the new wrapper to have the
872 ;; same state as the old wrapper. We will then have
873 ;; to change that. This may seem like wasted work
874 ;; (and it is), but the spec requires that we call
875 ;; MAKE-INSTANCES-OBSOLETE.
876 (make-instances-obsolete class)
877 (class-wrapper class)))))
879 (with-slots (wrapper slots) class
880 (update-lisp-class-layout class nwrapper)
882 (wrapper-instance-slots-layout nwrapper) nlayout
883 (wrapper-class-slots nwrapper) nwrapper-class-slots
884 (wrapper-no-of-instance-slots nwrapper) nslots
886 (setf (slot-value class 'finalized-p) t)
887 (unless (eq owrapper nwrapper)
888 (update-pv-table-cache-info class)))))
890 (defun compute-class-slots (eslotds)
892 (dolist (eslotd eslotds)
893 (push (assoc (slot-definition-name eslotd)
894 (class-slot-cells (slot-definition-class eslotd)))
898 (defun update-gfs-of-class (class)
899 (when (and (class-finalized-p class)
900 (let ((cpl (class-precedence-list class)))
901 (or (member *the-class-slot-class* cpl)
902 (member *the-class-standard-effective-slot-definition*
904 (let ((gf-table (make-hash-table :test 'eq)))
905 (labels ((collect-gfs (class)
906 (dolist (gf (specializer-direct-generic-functions class))
907 (setf (gethash gf gf-table) t))
908 (mapc #'collect-gfs (class-direct-superclasses class))))
910 (maphash (lambda (gf ignore)
911 (declare (ignore ignore))
912 (update-gf-dfun class gf))
915 (defun update-inits (class inits)
916 (setf (plist-value class 'default-initargs) inits))
918 (defmethod compute-default-initargs ((class slot-class))
919 (let ((cpl (class-precedence-list class))
920 (direct (class-direct-default-initargs class)))
921 (labels ((walk (tail)
924 (let ((c (pop tail)))
925 (append (if (eq c class)
927 (class-direct-default-initargs c))
929 (let ((initargs (walk cpl)))
930 (delete-duplicates initargs :test #'eq :key #'car :from-end t)))))
932 ;;;; protocols for constructing direct and effective slot definitions
934 (defmethod direct-slot-definition-class ((class std-class) initargs)
935 (declare (ignore initargs))
936 (find-class 'standard-direct-slot-definition))
938 (defun make-direct-slotd (class initargs)
939 (let ((initargs (list* :class class initargs)))
940 (apply #'make-instance
941 (direct-slot-definition-class class initargs)
944 (defmethod compute-slots ((class std-class))
945 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
946 ;; for each different slot name we find in our superclasses. Each
947 ;; call receives the class and a list of the dslotds with that name.
948 ;; The list is in most-specific-first order.
949 (let ((name-dslotds-alist ()))
950 (dolist (c (class-precedence-list class))
951 (dolist (slot (class-direct-slots c))
952 (let* ((name (slot-definition-name slot))
953 (entry (assq name name-dslotds-alist)))
955 (push slot (cdr entry))
956 (push (list name slot) name-dslotds-alist)))))
957 (mapcar (lambda (direct)
958 (compute-effective-slot-definition class
960 (nreverse (cdr direct))))
961 name-dslotds-alist)))
963 (defmethod compute-slots ((class standard-class))
966 (defmethod compute-slots :around ((class standard-class))
967 (let ((eslotds (call-next-method))
969 (dolist (eslotd eslotds eslotds)
970 (setf (slot-definition-location eslotd)
971 (ecase (slot-definition-allocation eslotd)
975 (let* ((name (slot-definition-name eslotd))
976 (from-class (slot-definition-allocation-class eslotd))
977 (cell (assq name (class-slot-cells from-class))))
980 (initialize-internal-slot-functions eslotd))))
982 (defmethod compute-slots ((class funcallable-standard-class))
985 (defmethod compute-slots :around ((class funcallable-standard-class))
986 (labels ((instance-slot-names (slotds)
988 (dolist (slotd slotds (nreverse collect))
989 (when (eq (slot-definition-allocation slotd) :instance)
990 (push (slot-definition-name slotd) collect)))))
991 ;; This sorts slots so that slots of classes later in the CPL
992 ;; come before slots of other classes. This is crucial for
993 ;; funcallable instances because it ensures that the slots of
994 ;; FUNCALLABLE-STANDARD-OBJECT, which includes the slots of
995 ;; KERNEL:FUNCALLABLE-INSTANCE, come first, which in turn
996 ;; makes it possible to treat FUNCALLABLE-STANDARD-OBJECT as
997 ;; a funcallable instance.
998 (compute-layout (eslotds)
1000 (names (instance-slot-names eslotds)))
1002 (reverse (class-precedence-list class))
1003 (nreverse (nconc names first)))
1004 (dolist (ss (class-slots class))
1005 (let ((name (slot-definition-name ss)))
1006 (when (member name names)
1008 (setq names (delete name names)))))))))
1009 (let ((all-slotds (call-next-method))
1012 (dolist (slotd all-slotds)
1013 (ecase (slot-definition-allocation slotd)
1014 (:instance (push slotd instance-slots))
1015 (:class (push slotd class-slots))))
1016 (let ((layout (compute-layout instance-slots)))
1017 (dolist (slotd instance-slots)
1018 (setf (slot-definition-location slotd)
1019 (position (slot-definition-name slotd) layout))
1020 (initialize-internal-slot-functions slotd)))
1021 (dolist (slotd class-slots)
1022 (let ((name (slot-definition-name slotd))
1023 (from-class (slot-definition-allocation-class slotd)))
1024 (setf (slot-definition-location slotd)
1025 (assoc name (class-slot-cells from-class)))
1026 (aver (consp (slot-definition-location slotd)))
1027 (initialize-internal-slot-functions slotd)))
1030 (defmethod compute-slots ((class structure-class))
1031 (mapcan (lambda (superclass)
1032 (mapcar (lambda (dslotd)
1033 (compute-effective-slot-definition
1035 (slot-definition-name dslotd)
1037 (class-direct-slots superclass)))
1038 (reverse (slot-value class 'class-precedence-list))))
1040 (defmethod compute-slots :around ((class structure-class))
1041 (let ((eslotds (call-next-method)))
1042 (mapc #'initialize-internal-slot-functions eslotds)
1045 (defmethod compute-effective-slot-definition ((class slot-class) name dslotds)
1046 (declare (ignore name))
1047 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
1048 (class (effective-slot-definition-class class initargs)))
1049 (apply #'make-instance class initargs)))
1051 (defmethod effective-slot-definition-class ((class std-class) initargs)
1052 (declare (ignore initargs))
1053 (find-class 'standard-effective-slot-definition))
1055 (defmethod effective-slot-definition-class ((class structure-class) initargs)
1056 (declare (ignore initargs))
1057 (find-class 'structure-effective-slot-definition))
1059 (defmethod compute-effective-slot-definition-initargs
1060 ((class slot-class) direct-slotds)
1066 (allocation-class nil)
1072 (dolist (slotd direct-slotds)
1075 (setq name (slot-definition-name slotd)
1078 (when (slot-definition-initfunction slotd)
1079 (setq initform (slot-definition-initform slotd)
1080 initfunction (slot-definition-initfunction slotd)
1083 (setq allocation (slot-definition-allocation slotd)
1084 allocation-class (slot-definition-class slotd)
1086 (setq initargs (append (slot-definition-initargs slotd) initargs))
1087 (let ((slotd-type (slot-definition-type slotd)))
1088 (setq type (cond ((eq type t) slotd-type)
1089 ((*subtypep type slotd-type) type)
1090 (t `(and ,type ,slotd-type)))))))
1093 :initfunction initfunction
1095 :allocation allocation
1096 :allocation-class allocation-class
1100 (defmethod compute-effective-slot-definition-initargs :around
1101 ((class structure-class) direct-slotds)
1102 (let ((slotd (car direct-slotds)))
1103 (list* :defstruct-accessor-symbol
1104 (slot-definition-defstruct-accessor-symbol slotd)
1105 :internal-reader-function
1106 (slot-definition-internal-reader-function slotd)
1107 :internal-writer-function
1108 (slot-definition-internal-writer-function slotd)
1109 (call-next-method))))
1111 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
1112 ;;; to make the method object. They have to use make-a-method which
1113 ;;; is a specially bootstrapped mechanism for making standard methods.
1114 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
1115 (declare (ignore direct-slot initargs))
1116 (find-class 'standard-reader-method))
1118 (defmethod add-reader-method ((class slot-class) generic-function slot-name)
1119 (add-method generic-function
1120 (make-a-method 'standard-reader-method
1122 (list (or (class-name class) 'object))
1124 (make-reader-method-function class slot-name)
1125 "automatically generated reader method"
1128 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
1129 (declare (ignore direct-slot initargs))
1130 (find-class 'standard-writer-method))
1132 (defmethod add-writer-method ((class slot-class) generic-function slot-name)
1133 (add-method generic-function
1134 (make-a-method 'standard-writer-method
1136 (list 'new-value (or (class-name class) 'object))
1137 (list *the-class-t* class)
1138 (make-writer-method-function class slot-name)
1139 "automatically generated writer method"
1142 (defmethod add-boundp-method ((class slot-class) generic-function slot-name)
1143 (add-method generic-function
1144 (make-a-method 'standard-boundp-method
1146 (list (or (class-name class) 'object))
1148 (make-boundp-method-function class slot-name)
1149 "automatically generated boundp method"
1152 (defmethod remove-reader-method ((class slot-class) generic-function)
1153 (let ((method (get-method generic-function () (list class) nil)))
1154 (when method (remove-method generic-function method))))
1156 (defmethod remove-writer-method ((class slot-class) generic-function)
1158 (get-method generic-function () (list *the-class-t* class) nil)))
1159 (when method (remove-method generic-function method))))
1161 (defmethod remove-boundp-method ((class slot-class) generic-function)
1162 (let ((method (get-method generic-function () (list class) nil)))
1163 (when method (remove-method generic-function method))))
1165 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITE-METHOD function are NOT
1166 ;;; part of the standard protocol. They are however useful, PCL makes
1167 ;;; use of them internally and documents them for PCL users.
1169 ;;; *** This needs work to make type testing by the writer functions which
1170 ;;; *** do type testing faster. The idea would be to have one constructor
1171 ;;; *** for each possible type test.
1173 ;;; *** There is a subtle bug here which is going to have to be fixed.
1174 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1175 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1176 ;;; *** defined for this metaclass a chance to run.
1178 (defmethod make-reader-method-function ((class slot-class) slot-name)
1179 (make-std-reader-method-function (class-name class) slot-name))
1181 (defmethod make-writer-method-function ((class slot-class) slot-name)
1182 (make-std-writer-method-function (class-name class) slot-name))
1184 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1185 (make-std-boundp-method-function (class-name class) slot-name))
1187 (defmethod compatible-meta-class-change-p (class proto-new-class)
1188 (eq (class-of class) (class-of proto-new-class)))
1190 (defmethod validate-superclass ((class class) (new-super class))
1191 (or (eq new-super *the-class-t*)
1192 (eq (class-of class) (class-of new-super))))
1194 (defmethod validate-superclass ((class standard-class) (new-super std-class))
1195 (let ((new-super-meta-class (class-of new-super)))
1196 (or (eq new-super-meta-class *the-class-std-class*)
1197 (eq (class-of class) new-super-meta-class))))
1199 ;;; What this does depends on which of the four possible values of
1200 ;;; LAYOUT-INVALID the PCL wrapper has; the simplest case is when it
1201 ;;; is (:FLUSH <wrapper>) or (:OBSOLETE <wrapper>), when there is
1202 ;;; nothing to do, as the new wrapper has already been created. If
1203 ;;; LAYOUT-INVALID returns NIL, then we invalidate it (setting it to
1204 ;;; (:FLUSH <wrapper>); UPDATE-SLOTS later gets to choose whether or
1205 ;;; not to "upgrade" this to (:OBSOLETE <wrapper>).
1207 ;;; This leaves the case where LAYOUT-INVALID returns T, which happens
1208 ;;; when REGISTER-LAYOUT has invalidated a superclass of CLASS (which
1209 ;;; invalidated all the subclasses in SB-KERNEL land). Again, here we
1210 ;;; must flush the caches and allow UPDATE-SLOTS to decide whether to
1211 ;;; obsolete the wrapper.
1213 ;;; FIXME: either here or in INVALID-WRAPPER-P looks like a good place
1214 ;;; for (AVER (NOT (EQ (LAYOUT-INVALID OWRAPPER)
1215 ;;; :UNINITIALIZED)))
1217 ;;; Thanks to Gerd Moellmann for the explanation. -- CSR, 2002-10-29
1218 (defun force-cache-flushes (class)
1219 (let* ((owrapper (class-wrapper class)))
1220 ;; We only need to do something if the wrapper is still valid. If
1221 ;; the wrapper isn't valid, state will be FLUSH or OBSOLETE, and
1222 ;; both of those will already be doing what we want. In
1223 ;; particular, we must be sure we never change an OBSOLETE into a
1224 ;; FLUSH since OBSOLETE means do what FLUSH does and then some.
1225 (when (or (not (invalid-wrapper-p owrapper))
1226 ;; KLUDGE: despite the observations above, this remains
1227 ;; a violation of locality or what might be considered
1228 ;; good style. There has to be a better way! -- CSR,
1230 (eq (layout-invalid owrapper) t))
1231 (let ((nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1233 (setf (wrapper-instance-slots-layout nwrapper)
1234 (wrapper-instance-slots-layout owrapper))
1235 (setf (wrapper-class-slots nwrapper)
1236 (wrapper-class-slots owrapper))
1238 (update-lisp-class-layout class nwrapper)
1239 (setf (slot-value class 'wrapper) nwrapper)
1240 (invalidate-wrapper owrapper :flush nwrapper))))))
1242 (defun flush-cache-trap (owrapper nwrapper instance)
1243 (declare (ignore owrapper))
1244 (set-wrapper instance nwrapper))
1246 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1247 ;;; the next access to the instance (as defined in 88-002R) to trap
1248 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1249 (defmethod make-instances-obsolete ((class std-class))
1250 (let* ((owrapper (class-wrapper class))
1251 (nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1253 (setf (wrapper-instance-slots-layout nwrapper)
1254 (wrapper-instance-slots-layout owrapper))
1255 (setf (wrapper-class-slots nwrapper)
1256 (wrapper-class-slots owrapper))
1258 (update-lisp-class-layout class nwrapper)
1259 (setf (slot-value class 'wrapper) nwrapper)
1260 (invalidate-wrapper owrapper :obsolete nwrapper)
1263 (defmethod make-instances-obsolete ((class symbol))
1264 (make-instances-obsolete (find-class class)))
1266 ;;; OBSOLETE-INSTANCE-TRAP is the internal trap that is called when we
1267 ;;; see an obsolete instance. The times when it is called are:
1268 ;;; - when the instance is involved in method lookup
1269 ;;; - when attempting to access a slot of an instance
1271 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1272 ;;; instance access macros.
1274 ;;; Of course these times when it is called are an internal
1275 ;;; implementation detail of PCL and are not part of the documented
1276 ;;; description of when the obsolete instance update happens. The
1277 ;;; documented description is as it appears in 88-002R.
1279 ;;; This has to return the new wrapper, so it counts on all the
1280 ;;; methods on obsolete-instance-trap-internal to return the new
1281 ;;; wrapper. It also does a little internal error checking to make
1282 ;;; sure that the traps are only happening when they should, and that
1283 ;;; the trap methods are computing appropriate new wrappers.
1285 ;;; OBSOLETE-INSTANCE-TRAP might be called on structure instances
1286 ;;; after a structure is redefined. In most cases,
1287 ;;; OBSOLETE-INSTANCE-TRAP will not be able to fix the old instance,
1288 ;;; so it must signal an error. The hard part of this is that the
1289 ;;; error system and debugger might cause OBSOLETE-INSTANCE-TRAP to be
1290 ;;; called again, so in that case, we have to return some reasonable
1291 ;;; wrapper, instead.
1293 (defvar *in-obsolete-instance-trap* nil)
1294 (defvar *the-wrapper-of-structure-object*
1295 (class-wrapper (find-class 'structure-object)))
1297 (define-condition obsolete-structure (error)
1298 ((datum :reader obsolete-structure-datum :initarg :datum))
1300 (lambda (condition stream)
1301 ;; Don't try to print the structure, since it probably won't work.
1303 "~@<obsolete structure error for a structure of type ~2I~_~S~:>"
1304 (type-of (obsolete-structure-datum condition))))))
1306 (defun obsolete-instance-trap (owrapper nwrapper instance)
1307 (if (not (pcl-instance-p instance))
1308 (if *in-obsolete-instance-trap*
1309 *the-wrapper-of-structure-object*
1310 (let ((*in-obsolete-instance-trap* t))
1311 (error 'obsolete-structure :datum instance)))
1312 (let* ((class (wrapper-class* nwrapper))
1313 (copy (allocate-instance class)) ;??? allocate-instance ???
1314 (olayout (wrapper-instance-slots-layout owrapper))
1315 (nlayout (wrapper-instance-slots-layout nwrapper))
1316 (oslots (get-slots instance))
1317 (nslots (get-slots copy))
1318 (oclass-slots (wrapper-class-slots owrapper))
1322 ;; local --> local transfer
1323 ;; local --> shared discard
1324 ;; local --> -- discard
1325 ;; shared --> local transfer
1326 ;; shared --> shared discard
1327 ;; shared --> -- discard
1331 ;; Go through all the old local slots.
1333 (dolist (name olayout)
1334 (let ((npos (posq name nlayout)))
1336 (setf (clos-slots-ref nslots npos)
1337 (clos-slots-ref oslots opos))
1339 (push name discarded)
1340 (unless (eq (clos-slots-ref oslots opos) +slot-unbound+)
1341 (setf (getf plist name) (clos-slots-ref oslots opos))))))
1344 ;; Go through all the old shared slots.
1345 (dolist (oclass-slot-and-val oclass-slots)
1346 (let ((name (car oclass-slot-and-val))
1347 (val (cdr oclass-slot-and-val)))
1348 (let ((npos (posq name nlayout)))
1350 (setf (clos-slots-ref nslots npos) (cdr oclass-slot-and-val))
1351 (progn (push name discarded)
1352 (unless (eq val +slot-unbound+)
1353 (setf (getf plist name) val)))))))
1355 ;; Go through all the new local slots to compute the added slots.
1356 (dolist (nlocal nlayout)
1357 (unless (or (memq nlocal olayout)
1358 (assq nlocal oclass-slots))
1359 (push nlocal added)))
1361 (swap-wrappers-and-slots instance copy)
1363 (update-instance-for-redefined-class instance
1369 (defun change-class-internal (instance new-class initargs)
1370 (let* ((old-class (class-of instance))
1371 (copy (allocate-instance new-class))
1372 (new-wrapper (get-wrapper copy))
1373 (old-wrapper (class-wrapper old-class))
1374 (old-layout (wrapper-instance-slots-layout old-wrapper))
1375 (new-layout (wrapper-instance-slots-layout new-wrapper))
1376 (old-slots (get-slots instance))
1377 (new-slots (get-slots copy))
1378 (old-class-slots (wrapper-class-slots old-wrapper)))
1380 ;; "The values of local slots specified by both the class CTO and
1381 ;; CFROM are retained. If such a local slot was unbound, it
1382 ;; remains unbound."
1383 (let ((new-position 0))
1384 (dolist (new-slot new-layout)
1385 (let ((old-position (posq new-slot old-layout)))
1387 (setf (clos-slots-ref new-slots new-position)
1388 (clos-slots-ref old-slots old-position))))
1389 (incf new-position)))
1391 ;; "The values of slots specified as shared in the class CFROM and
1392 ;; as local in the class CTO are retained."
1393 (dolist (slot-and-val old-class-slots)
1394 (let ((position (posq (car slot-and-val) new-layout)))
1396 (setf (clos-slots-ref new-slots position) (cdr slot-and-val)))))
1398 ;; Make the copy point to the old instance's storage, and make the
1399 ;; old instance point to the new storage.
1400 (swap-wrappers-and-slots instance copy)
1402 (apply #'update-instance-for-different-class copy instance initargs)
1405 (defmethod change-class ((instance standard-object)
1406 (new-class standard-class)
1408 (change-class-internal instance new-class initargs))
1410 (defmethod change-class ((instance funcallable-standard-object)
1411 (new-class funcallable-standard-class)
1413 (change-class-internal instance new-class initargs))
1415 (defmethod change-class ((instance standard-object)
1416 (new-class funcallable-standard-class)
1418 (declare (ignore initargs))
1419 (error "You can't change the class of ~S to ~S~@
1420 because it isn't already an instance with metaclass ~S."
1421 instance new-class 'standard-class))
1423 (defmethod change-class ((instance funcallable-standard-object)
1424 (new-class standard-class)
1426 (declare (ignore initargs))
1427 (error "You can't change the class of ~S to ~S~@
1428 because it isn't already an instance with metaclass ~S."
1429 instance new-class 'funcallable-standard-class))
1431 (defmethod change-class ((instance t) (new-class-name symbol) &rest initargs)
1432 (apply #'change-class instance (find-class new-class-name) initargs))
1434 ;;;; The metaclass BUILT-IN-CLASS
1436 ;;;; This metaclass is something of a weird creature. By this point, all
1437 ;;;; instances of it which will exist have been created, and no instance
1438 ;;;; is ever created by calling MAKE-INSTANCE.
1440 ;;;; But, there are other parts of the protocol we must follow and those
1441 ;;;; definitions appear here.
1443 (defmethod shared-initialize :before
1444 ((class built-in-class) slot-names &rest initargs)
1445 (declare (ignore slot-names initargs))
1446 (error "attempt to initialize or reinitialize a built in class"))
1448 (defmethod class-direct-slots ((class built-in-class)) ())
1449 (defmethod class-slots ((class built-in-class)) ())
1450 (defmethod class-direct-default-initargs ((class built-in-class)) ())
1451 (defmethod class-default-initargs ((class built-in-class)) ())
1453 (defmethod validate-superclass ((c class) (s built-in-class))
1454 (or (eq s *the-class-t*)
1455 (eq s *the-class-stream*)))
1457 ;;; Some necessary methods for FORWARD-REFERENCED-CLASS
1458 (defmethod class-direct-slots ((class forward-referenced-class)) ())
1459 (defmethod class-direct-default-initargs ((class forward-referenced-class)) ())
1460 (macrolet ((def (method)
1461 `(defmethod ,method ((class forward-referenced-class))
1462 (error "~@<~I~S was called on a forward referenced class:~2I~_~S~:>"
1464 (def class-default-initargs)
1465 (def class-precedence-list)
1468 (defmethod validate-superclass ((c slot-class)
1469 (f forward-referenced-class))
1472 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1473 (pushnew dependent (plist-value metaobject 'dependents)))
1475 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1476 (setf (plist-value metaobject 'dependents)
1477 (delete dependent (plist-value metaobject 'dependents))))
1479 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1480 (dolist (dependent (plist-value metaobject 'dependents))
1481 (funcall function dependent)))