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 (add-slot-accessors class direct-slots)
509 (make-preliminary-layout class))
511 (defmethod shared-initialize :after ((class forward-referenced-class)
512 slot-names &key &allow-other-keys)
513 (declare (ignore slot-names))
514 (make-preliminary-layout class))
516 (defvar *allow-forward-referenced-classes-in-cpl-p* nil)
518 ;;; Give CLASS a preliminary layout if it doesn't have one already, to
519 ;;; make it known to the type system.
520 (defun make-preliminary-layout (class)
521 (flet ((compute-preliminary-cpl (root)
522 (let ((*allow-forward-referenced-classes-in-cpl-p* t))
523 (compute-class-precedence-list root))))
524 (unless (class-finalized-p class)
525 (let ((name (class-name class)))
526 (setf (find-class name) class)
527 ;; KLUDGE: This is fairly horrible. We need to make a
528 ;; full-fledged CLASSOID here, not just tell the compiler that
529 ;; some class is forthcoming, because there are legitimate
530 ;; questions one can ask of the type system, implemented in
531 ;; terms of CLASSOIDs, involving forward-referenced classes. So.
532 (when (and (eq *boot-state* 'complete)
533 (null (find-classoid name nil)))
534 (setf (find-classoid name)
535 (make-standard-classoid :name name)))
536 (set-class-type-translation class name)
537 (let ((layout (make-wrapper 0 class))
538 (classoid (find-classoid name)))
539 (setf (layout-classoid layout) classoid)
540 (setf (classoid-pcl-class classoid) class)
541 (setf (slot-value class 'wrapper) layout)
542 (let ((cpl (compute-preliminary-cpl class)))
543 (setf (layout-inherits layout)
544 (order-layout-inherits
545 (map 'simple-vector #'class-wrapper
546 (reverse (rest cpl))))))
547 (register-layout layout :invalidate t)
548 (setf (classoid-layout classoid) layout)
549 (mapc #'make-preliminary-layout (class-direct-subclasses class)))))))
552 (defmethod shared-initialize :before ((class class) slot-names &key name)
553 (declare (ignore slot-names name))
554 ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
555 ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
556 (setf (slot-value class 'type) `(class ,class))
557 (setf (slot-value class 'class-eq-specializer)
558 (make-instance 'class-eq-specializer :class class)))
560 (defmethod reinitialize-instance :before ((class slot-class) &key)
561 (remove-direct-subclasses class (class-direct-superclasses class))
562 (remove-slot-accessors class (class-direct-slots class)))
564 (defmethod reinitialize-instance :after ((class slot-class)
567 (map-dependents class
569 (apply #'update-dependent class dependent initargs))))
571 (defmethod shared-initialize :after ((class condition-class) slot-names
572 &key direct-superclasses)
573 (declare (ignore slot-names))
574 (let ((classoid (find-classoid (class-name class))))
575 (with-slots (wrapper class-precedence-list prototype predicate-name
576 (direct-supers direct-superclasses))
578 (setf (slot-value class 'finalized-p) t)
579 (setf (classoid-pcl-class classoid) class)
580 (setq direct-supers direct-superclasses)
581 (setq wrapper (classoid-layout classoid))
582 (setq class-precedence-list (compute-class-precedence-list class))
583 (setq prototype (make-condition (class-name class)))
584 (add-direct-subclasses class direct-superclasses)
585 (setq predicate-name (make-class-predicate-name (class-name class)))
586 (make-class-predicate class predicate-name))))
588 (defmethod shared-initialize :after
589 ((slotd structure-slot-definition) slot-names &key
590 (allocation :instance) allocation-class)
591 (declare (ignore slot-names allocation-class))
592 (unless (eq allocation :instance)
593 (error "Structure slots must have :INSTANCE allocation.")))
595 (defun make-structure-class-defstruct-form (name direct-slots include)
596 (let* ((conc-name (intern (format nil "~S structure class " name)))
597 (constructor (intern (format nil "~Aconstructor" conc-name)))
598 (defstruct `(defstruct (,name
600 `((:include ,(class-name include))))
602 (:conc-name ,conc-name)
603 (:constructor ,constructor ())
605 ,@(mapcar (lambda (slot)
606 `(,(slot-definition-name slot)
609 (reader-names (mapcar (lambda (slotd)
610 (list 'slot-accessor name
611 (slot-definition-name slotd)
614 (writer-names (mapcar (lambda (slotd)
615 (list 'slot-accessor name
616 (slot-definition-name slotd)
620 (mapcar (lambda (slotd reader-name)
622 (slot-definition-defstruct-accessor-symbol
624 `(defun ,reader-name (obj)
625 (declare (type ,name obj))
627 direct-slots reader-names))
629 (mapcar (lambda (slotd writer-name)
631 (slot-definition-defstruct-accessor-symbol
633 `(defun ,writer-name (nv obj)
634 (declare (type ,name obj))
635 (setf (,accessor obj) nv))))
636 direct-slots writer-names))
640 ,@readers-init ,@writers-init
642 (values defstruct-form constructor reader-names writer-names)))
644 (defmethod shared-initialize :after
645 ((class structure-class)
647 &key (direct-superclasses nil direct-superclasses-p)
648 (direct-slots nil direct-slots-p)
649 direct-default-initargs
650 (predicate-name nil predicate-name-p))
651 (declare (ignore slot-names direct-default-initargs))
652 (if direct-superclasses-p
653 (setf (slot-value class 'direct-superclasses)
654 (or direct-superclasses
655 (setq direct-superclasses
656 (and (not (eq (class-name class) 'structure-object))
657 (list *the-class-structure-object*)))))
658 (setq direct-superclasses (slot-value class 'direct-superclasses)))
659 (let* ((name (class-name class))
660 (from-defclass-p (slot-value class 'from-defclass-p))
661 (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
663 (setf (slot-value class 'direct-slots)
667 (let* ((slot-name (getf pl :name))
670 "~S structure class ~A"
672 (accessor (intern acc-name)))
673 (setq pl (list* :defstruct-accessor-symbol
675 (make-direct-slotd class pl))
677 (setq direct-slots (slot-value class 'direct-slots)))
679 (let ((include (car (slot-value class 'direct-superclasses))))
680 (multiple-value-bind (defstruct-form constructor reader-names writer-names)
681 (make-structure-class-defstruct-form name direct-slots include)
682 (unless (structure-type-p name) (eval defstruct-form))
683 (mapc (lambda (dslotd reader-name writer-name)
684 (let* ((reader (gdefinition reader-name))
685 (writer (when (gboundp writer-name)
686 (gdefinition writer-name))))
687 (setf (slot-value dslotd 'internal-reader-function)
689 (setf (slot-value dslotd 'internal-writer-function)
691 direct-slots reader-names writer-names)
692 (setf (slot-value class 'defstruct-form) defstruct-form)
693 (setf (slot-value class 'defstruct-constructor) constructor))))
694 (add-direct-subclasses class direct-superclasses)
695 (setf (slot-value class 'class-precedence-list)
696 (compute-class-precedence-list class))
697 (setf (slot-value class 'slots) (compute-slots class))
698 (let ((lclass (find-classoid (class-name class))))
699 (setf (classoid-pcl-class lclass) class)
700 (setf (slot-value class 'wrapper) (classoid-layout lclass)))
701 (setf (slot-value class 'finalized-p) t)
702 (update-pv-table-cache-info class)
703 (setq predicate-name (if predicate-name-p
704 (setf (slot-value class 'predicate-name)
705 (car predicate-name))
706 (or (slot-value class 'predicate-name)
707 (setf (slot-value class 'predicate-name)
708 (make-class-predicate-name
709 (class-name class))))))
710 (make-class-predicate class predicate-name)
711 (add-slot-accessors class direct-slots)))
713 (defmethod direct-slot-definition-class ((class structure-class) initargs)
714 (declare (ignore initargs))
715 (find-class 'structure-direct-slot-definition))
717 (defmethod finalize-inheritance ((class structure-class))
718 nil) ; always finalized
720 (defun add-slot-accessors (class dslotds)
721 (fix-slot-accessors class dslotds 'add))
723 (defun remove-slot-accessors (class dslotds)
724 (fix-slot-accessors class dslotds 'remove))
726 (defun fix-slot-accessors (class dslotds add/remove)
727 (flet ((fix (gfspec name r/w)
728 (let ((gf (ensure-generic-function gfspec)))
730 (r (if (eq add/remove 'add)
731 (add-reader-method class gf name)
732 (remove-reader-method class gf)))
733 (w (if (eq add/remove 'add)
734 (add-writer-method class gf name)
735 (remove-writer-method class gf)))))))
736 (dolist (dslotd dslotds)
737 (let ((slot-name (slot-definition-name dslotd)))
738 (dolist (r (slot-definition-readers dslotd)) (fix r slot-name 'r))
739 (dolist (w (slot-definition-writers dslotd)) (fix w slot-name 'w))))))
741 (defun add-direct-subclasses (class supers)
742 (dolist (super supers)
743 (unless (memq class (class-direct-subclasses class))
744 (add-direct-subclass super class))))
746 (defun remove-direct-subclasses (class supers)
747 (let ((old (class-direct-superclasses class)))
748 (dolist (o (set-difference old supers))
749 (remove-direct-subclass o class))))
751 (defmethod finalize-inheritance ((class std-class))
752 (update-class class t))
754 (defmethod finalize-inheritance ((class forward-referenced-class))
755 ;; FIXME: should we not be thinking a bit about what kinds of error
756 ;; we're throwing? Maybe we need a clos-error type to mix in? Or
757 ;; possibly a forward-referenced-class-error, though that's
758 ;; difficult given e.g. class precedence list calculations...
760 "~@<FINALIZE-INHERITANCE was called on a forward referenced class:~
765 (defun class-has-a-forward-referenced-superclass-p (class)
766 (or (forward-referenced-class-p class)
767 (some #'class-has-a-forward-referenced-superclass-p
768 (class-direct-superclasses class))))
770 ;;; This is called by :after shared-initialize whenever a class is initialized
771 ;;; or reinitialized. The class may or may not be finalized.
772 (defun update-class (class finalizep)
773 ;; Comment from Gerd Moellmann:
775 ;; Note that we can't simply delay the finalization when CLASS has
776 ;; no forward referenced superclasses because that causes bootstrap
778 (when (and (not finalizep)
779 (not (class-finalized-p class))
780 (not (class-has-a-forward-referenced-superclass-p class)))
781 (finalize-inheritance class)
782 (return-from update-class))
783 (when (or finalizep (class-finalized-p class)
784 (not (class-has-a-forward-referenced-superclass-p class)))
785 (setf (find-class (class-name class)) class)
786 (update-cpl class (compute-class-precedence-list class))
787 ;; This invocation of UPDATE-SLOTS, in practice, finalizes the
788 ;; class. The hoops above are to ensure that FINALIZE-INHERITANCE
789 ;; is called at finalization, so that MOP programmers can hook
790 ;; into the system as described in "Class Finalization Protocol"
791 ;; (section 5.5.2 of AMOP).
792 (update-slots class (compute-slots class))
793 (update-gfs-of-class class)
794 (update-inits class (compute-default-initargs class))
795 (update-ctors 'finalize-inheritance :class class))
797 (dolist (sub (class-direct-subclasses class)) (update-class sub nil))))
799 (defun update-cpl (class cpl)
800 (if (class-finalized-p class)
801 (unless (equal (class-precedence-list class) cpl)
802 ;; comment from the old CMU CL sources:
803 ;; Need to have the cpl setup before update-lisp-class-layout
804 ;; is called on CMU CL.
805 (setf (slot-value class 'class-precedence-list) cpl)
806 (force-cache-flushes class))
807 (setf (slot-value class 'class-precedence-list) cpl))
808 (update-class-can-precede-p cpl))
810 (defun update-class-can-precede-p (cpl)
812 (let ((first (car cpl)))
813 (dolist (c (cdr cpl))
814 (pushnew c (slot-value first 'can-precede-list))))
815 (update-class-can-precede-p (cdr cpl))))
817 (defun class-can-precede-p (class1 class2)
818 (member class2 (class-can-precede-list class1)))
820 (defun update-slots (class eslotds)
821 (let ((instance-slots ())
823 (dolist (eslotd eslotds)
824 (let ((alloc (slot-definition-allocation eslotd)))
826 (:instance (push eslotd instance-slots))
827 (:class (push eslotd class-slots)))))
829 ;; If there is a change in the shape of the instances then the
830 ;; old class is now obsolete.
831 (let* ((nlayout (mapcar #'slot-definition-name
832 (sort instance-slots #'<
833 :key #'slot-definition-location)))
834 (nslots (length nlayout))
835 (nwrapper-class-slots (compute-class-slots class-slots))
836 (owrapper (when (class-finalized-p class)
837 (class-wrapper class)))
838 (olayout (when owrapper
839 (wrapper-instance-slots-layout owrapper)))
840 (owrapper-class-slots (and owrapper (wrapper-class-slots owrapper)))
842 (cond ((null owrapper)
843 (make-wrapper nslots class))
844 ((and (equal nlayout olayout)
846 (loop for o in owrapper-class-slots
847 for n in nwrapper-class-slots
848 do (unless (eq (car o) (car n)) (return t)))))
851 ;; This will initialize the new wrapper to have the
852 ;; same state as the old wrapper. We will then have
853 ;; to change that. This may seem like wasted work
854 ;; (and it is), but the spec requires that we call
855 ;; MAKE-INSTANCES-OBSOLETE.
856 (make-instances-obsolete class)
857 (class-wrapper class)))))
859 (with-slots (wrapper slots) class
860 (update-lisp-class-layout class nwrapper)
862 (wrapper-instance-slots-layout nwrapper) nlayout
863 (wrapper-class-slots nwrapper) nwrapper-class-slots
864 (wrapper-no-of-instance-slots nwrapper) nslots
866 (setf (slot-value class 'finalized-p) t)
867 (unless (eq owrapper nwrapper)
868 (update-pv-table-cache-info class)))))
870 (defun compute-class-slots (eslotds)
872 (dolist (eslotd eslotds)
873 (push (assoc (slot-definition-name eslotd)
874 (class-slot-cells (slot-definition-class eslotd)))
878 (defun update-gfs-of-class (class)
879 (when (and (class-finalized-p class)
880 (let ((cpl (class-precedence-list class)))
881 (or (member *the-class-slot-class* cpl)
882 (member *the-class-standard-effective-slot-definition*
884 (let ((gf-table (make-hash-table :test 'eq)))
885 (labels ((collect-gfs (class)
886 (dolist (gf (specializer-direct-generic-functions class))
887 (setf (gethash gf gf-table) t))
888 (mapc #'collect-gfs (class-direct-superclasses class))))
890 (maphash (lambda (gf ignore)
891 (declare (ignore ignore))
892 (update-gf-dfun class gf))
895 (defun update-inits (class inits)
896 (setf (plist-value class 'default-initargs) inits))
898 (defmethod compute-default-initargs ((class slot-class))
899 (let ((cpl (class-precedence-list class))
900 (direct (class-direct-default-initargs class)))
901 (labels ((walk (tail)
904 (let ((c (pop tail)))
905 (append (if (eq c class)
907 (class-direct-default-initargs c))
909 (let ((initargs (walk cpl)))
910 (delete-duplicates initargs :test #'eq :key #'car :from-end t)))))
912 ;;;; protocols for constructing direct and effective slot definitions
914 (defmethod direct-slot-definition-class ((class std-class) initargs)
915 (declare (ignore initargs))
916 (find-class 'standard-direct-slot-definition))
918 (defun make-direct-slotd (class initargs)
919 (let ((initargs (list* :class class initargs)))
920 (apply #'make-instance
921 (direct-slot-definition-class class initargs)
924 (defmethod compute-slots ((class std-class))
925 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
926 ;; for each different slot name we find in our superclasses. Each
927 ;; call receives the class and a list of the dslotds with that name.
928 ;; The list is in most-specific-first order.
929 (let ((name-dslotds-alist ()))
930 (dolist (c (class-precedence-list class))
931 (dolist (slot (class-direct-slots c))
932 (let* ((name (slot-definition-name slot))
933 (entry (assq name name-dslotds-alist)))
935 (push slot (cdr entry))
936 (push (list name slot) name-dslotds-alist)))))
937 (mapcar (lambda (direct)
938 (compute-effective-slot-definition class
940 (nreverse (cdr direct))))
941 name-dslotds-alist)))
943 (defmethod compute-slots ((class standard-class))
946 (defmethod compute-slots :around ((class standard-class))
947 (let ((eslotds (call-next-method))
949 (dolist (eslotd eslotds eslotds)
950 (setf (slot-definition-location eslotd)
951 (ecase (slot-definition-allocation eslotd)
955 (let* ((name (slot-definition-name eslotd))
956 (from-class (slot-definition-allocation-class eslotd))
957 (cell (assq name (class-slot-cells from-class))))
960 (initialize-internal-slot-functions eslotd))))
962 (defmethod compute-slots ((class funcallable-standard-class))
965 (defmethod compute-slots :around ((class funcallable-standard-class))
966 (labels ((instance-slot-names (slotds)
968 (dolist (slotd slotds (nreverse collect))
969 (when (eq (slot-definition-allocation slotd) :instance)
970 (push (slot-definition-name slotd) collect)))))
971 ;; This sorts slots so that slots of classes later in the CPL
972 ;; come before slots of other classes. This is crucial for
973 ;; funcallable instances because it ensures that the slots of
974 ;; FUNCALLABLE-STANDARD-OBJECT, which includes the slots of
975 ;; KERNEL:FUNCALLABLE-INSTANCE, come first, which in turn
976 ;; makes it possible to treat FUNCALLABLE-STANDARD-OBJECT as
977 ;; a funcallable instance.
978 (compute-layout (eslotds)
980 (names (instance-slot-names eslotds)))
982 (reverse (class-precedence-list class))
983 (nreverse (nconc names first)))
984 (dolist (ss (class-slots class))
985 (let ((name (slot-definition-name ss)))
986 (when (member name names)
988 (setq names (delete name names)))))))))
989 (let ((all-slotds (call-next-method))
992 (dolist (slotd all-slotds)
993 (ecase (slot-definition-allocation slotd)
994 (:instance (push slotd instance-slots))
995 (:class (push slotd class-slots))))
996 (let ((layout (compute-layout instance-slots)))
997 (dolist (slotd instance-slots)
998 (setf (slot-definition-location slotd)
999 (position (slot-definition-name slotd) layout))
1000 (initialize-internal-slot-functions slotd)))
1001 (dolist (slotd class-slots)
1002 (let ((name (slot-definition-name slotd))
1003 (from-class (slot-definition-allocation-class slotd)))
1004 (setf (slot-definition-location slotd)
1005 (assoc name (class-slot-cells from-class)))
1006 (aver (consp (slot-definition-location slotd)))
1007 (initialize-internal-slot-functions slotd)))
1010 (defmethod compute-slots ((class structure-class))
1011 (mapcan (lambda (superclass)
1012 (mapcar (lambda (dslotd)
1013 (compute-effective-slot-definition
1015 (slot-definition-name dslotd)
1017 (class-direct-slots superclass)))
1018 (reverse (slot-value class 'class-precedence-list))))
1020 (defmethod compute-slots :around ((class structure-class))
1021 (let ((eslotds (call-next-method)))
1022 (mapc #'initialize-internal-slot-functions eslotds)
1025 (defmethod compute-effective-slot-definition ((class slot-class) name dslotds)
1026 (declare (ignore name))
1027 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
1028 (class (effective-slot-definition-class class initargs)))
1029 (apply #'make-instance class initargs)))
1031 (defmethod effective-slot-definition-class ((class std-class) initargs)
1032 (declare (ignore initargs))
1033 (find-class 'standard-effective-slot-definition))
1035 (defmethod effective-slot-definition-class ((class structure-class) initargs)
1036 (declare (ignore initargs))
1037 (find-class 'structure-effective-slot-definition))
1039 (defmethod compute-effective-slot-definition-initargs
1040 ((class slot-class) direct-slotds)
1046 (allocation-class nil)
1052 (dolist (slotd direct-slotds)
1055 (setq name (slot-definition-name slotd)
1058 (when (slot-definition-initfunction slotd)
1059 (setq initform (slot-definition-initform slotd)
1060 initfunction (slot-definition-initfunction slotd)
1063 (setq allocation (slot-definition-allocation slotd)
1064 allocation-class (slot-definition-class slotd)
1066 (setq initargs (append (slot-definition-initargs slotd) initargs))
1067 (let ((slotd-type (slot-definition-type slotd)))
1068 (setq type (cond ((eq type t) slotd-type)
1069 ((*subtypep type slotd-type) type)
1070 (t `(and ,type ,slotd-type)))))))
1073 :initfunction initfunction
1075 :allocation allocation
1076 :allocation-class allocation-class
1080 (defmethod compute-effective-slot-definition-initargs :around
1081 ((class structure-class) direct-slotds)
1082 (let ((slotd (car direct-slotds)))
1083 (list* :defstruct-accessor-symbol
1084 (slot-definition-defstruct-accessor-symbol slotd)
1085 :internal-reader-function
1086 (slot-definition-internal-reader-function slotd)
1087 :internal-writer-function
1088 (slot-definition-internal-writer-function slotd)
1089 (call-next-method))))
1091 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
1092 ;;; to make the method object. They have to use make-a-method which
1093 ;;; is a specially bootstrapped mechanism for making standard methods.
1094 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
1095 (declare (ignore direct-slot initargs))
1096 (find-class 'standard-reader-method))
1098 (defmethod add-reader-method ((class slot-class) generic-function slot-name)
1099 (add-method generic-function
1100 (make-a-method 'standard-reader-method
1102 (list (or (class-name class) 'object))
1104 (make-reader-method-function class slot-name)
1105 "automatically generated reader method"
1108 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
1109 (declare (ignore direct-slot initargs))
1110 (find-class 'standard-writer-method))
1112 (defmethod add-writer-method ((class slot-class) generic-function slot-name)
1113 (add-method generic-function
1114 (make-a-method 'standard-writer-method
1116 (list 'new-value (or (class-name class) 'object))
1117 (list *the-class-t* class)
1118 (make-writer-method-function class slot-name)
1119 "automatically generated writer method"
1122 (defmethod add-boundp-method ((class slot-class) generic-function slot-name)
1123 (add-method generic-function
1124 (make-a-method 'standard-boundp-method
1126 (list (or (class-name class) 'object))
1128 (make-boundp-method-function class slot-name)
1129 "automatically generated boundp method"
1132 (defmethod remove-reader-method ((class slot-class) generic-function)
1133 (let ((method (get-method generic-function () (list class) nil)))
1134 (when method (remove-method generic-function method))))
1136 (defmethod remove-writer-method ((class slot-class) generic-function)
1138 (get-method generic-function () (list *the-class-t* class) nil)))
1139 (when method (remove-method generic-function method))))
1141 (defmethod remove-boundp-method ((class slot-class) generic-function)
1142 (let ((method (get-method generic-function () (list class) nil)))
1143 (when method (remove-method generic-function method))))
1145 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITE-METHOD function are NOT
1146 ;;; part of the standard protocol. They are however useful, PCL makes
1147 ;;; use of them internally and documents them for PCL users.
1149 ;;; *** This needs work to make type testing by the writer functions which
1150 ;;; *** do type testing faster. The idea would be to have one constructor
1151 ;;; *** for each possible type test.
1153 ;;; *** There is a subtle bug here which is going to have to be fixed.
1154 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1155 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1156 ;;; *** defined for this metaclass a chance to run.
1158 (defmethod make-reader-method-function ((class slot-class) slot-name)
1159 (make-std-reader-method-function (class-name class) slot-name))
1161 (defmethod make-writer-method-function ((class slot-class) slot-name)
1162 (make-std-writer-method-function (class-name class) slot-name))
1164 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1165 (make-std-boundp-method-function (class-name class) slot-name))
1167 (defmethod compatible-meta-class-change-p (class proto-new-class)
1168 (eq (class-of class) (class-of proto-new-class)))
1170 (defmethod validate-superclass ((class class) (new-super class))
1171 (or (eq new-super *the-class-t*)
1172 (eq (class-of class) (class-of new-super))))
1174 (defmethod validate-superclass ((class standard-class) (new-super std-class))
1175 (let ((new-super-meta-class (class-of new-super)))
1176 (or (eq new-super-meta-class *the-class-std-class*)
1177 (eq (class-of class) new-super-meta-class))))
1179 ;;; What this does depends on which of the four possible values of
1180 ;;; LAYOUT-INVALID the PCL wrapper has; the simplest case is when it
1181 ;;; is (:FLUSH <wrapper>) or (:OBSOLETE <wrapper>), when there is
1182 ;;; nothing to do, as the new wrapper has already been created. If
1183 ;;; LAYOUT-INVALID returns NIL, then we invalidate it (setting it to
1184 ;;; (:FLUSH <wrapper>); UPDATE-SLOTS later gets to choose whether or
1185 ;;; not to "upgrade" this to (:OBSOLETE <wrapper>).
1187 ;;; This leaves the case where LAYOUT-INVALID returns T, which happens
1188 ;;; when REGISTER-LAYOUT has invalidated a superclass of CLASS (which
1189 ;;; invalidated all the subclasses in SB-KERNEL land). Again, here we
1190 ;;; must flush the caches and allow UPDATE-SLOTS to decide whether to
1191 ;;; obsolete the wrapper.
1193 ;;; FIXME: either here or in INVALID-WRAPPER-P looks like a good place
1194 ;;; for (AVER (NOT (EQ (LAYOUT-INVALID OWRAPPER)
1195 ;;; :UNINITIALIZED)))
1197 ;;; Thanks to Gerd Moellmann for the explanation. -- CSR, 2002-10-29
1198 (defun force-cache-flushes (class)
1199 (let* ((owrapper (class-wrapper class)))
1200 ;; We only need to do something if the wrapper is still valid. If
1201 ;; the wrapper isn't valid, state will be FLUSH or OBSOLETE, and
1202 ;; both of those will already be doing what we want. In
1203 ;; particular, we must be sure we never change an OBSOLETE into a
1204 ;; FLUSH since OBSOLETE means do what FLUSH does and then some.
1205 (when (or (not (invalid-wrapper-p owrapper))
1206 ;; KLUDGE: despite the observations above, this remains
1207 ;; a violation of locality or what might be considered
1208 ;; good style. There has to be a better way! -- CSR,
1210 (eq (layout-invalid owrapper) t))
1211 (let ((nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1213 (setf (wrapper-instance-slots-layout nwrapper)
1214 (wrapper-instance-slots-layout owrapper))
1215 (setf (wrapper-class-slots nwrapper)
1216 (wrapper-class-slots owrapper))
1218 (update-lisp-class-layout class nwrapper)
1219 (setf (slot-value class 'wrapper) nwrapper)
1220 (invalidate-wrapper owrapper :flush nwrapper))))))
1222 (defun flush-cache-trap (owrapper nwrapper instance)
1223 (declare (ignore owrapper))
1224 (set-wrapper instance nwrapper))
1226 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1227 ;;; the next access to the instance (as defined in 88-002R) to trap
1228 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1229 (defmethod make-instances-obsolete ((class std-class))
1230 (let* ((owrapper (class-wrapper class))
1231 (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 :obsolete nwrapper)
1243 (defmethod make-instances-obsolete ((class symbol))
1244 (make-instances-obsolete (find-class class)))
1246 ;;; OBSOLETE-INSTANCE-TRAP is the internal trap that is called when we
1247 ;;; see an obsolete instance. The times when it is called are:
1248 ;;; - when the instance is involved in method lookup
1249 ;;; - when attempting to access a slot of an instance
1251 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1252 ;;; instance access macros.
1254 ;;; Of course these times when it is called are an internal
1255 ;;; implementation detail of PCL and are not part of the documented
1256 ;;; description of when the obsolete instance update happens. The
1257 ;;; documented description is as it appears in 88-002R.
1259 ;;; This has to return the new wrapper, so it counts on all the
1260 ;;; methods on obsolete-instance-trap-internal to return the new
1261 ;;; wrapper. It also does a little internal error checking to make
1262 ;;; sure that the traps are only happening when they should, and that
1263 ;;; the trap methods are computing appropriate new wrappers.
1265 ;;; OBSOLETE-INSTANCE-TRAP might be called on structure instances
1266 ;;; after a structure is redefined. In most cases,
1267 ;;; OBSOLETE-INSTANCE-TRAP will not be able to fix the old instance,
1268 ;;; so it must signal an error. The hard part of this is that the
1269 ;;; error system and debugger might cause OBSOLETE-INSTANCE-TRAP to be
1270 ;;; called again, so in that case, we have to return some reasonable
1271 ;;; wrapper, instead.
1273 (defvar *in-obsolete-instance-trap* nil)
1274 (defvar *the-wrapper-of-structure-object*
1275 (class-wrapper (find-class 'structure-object)))
1277 (define-condition obsolete-structure (error)
1278 ((datum :reader obsolete-structure-datum :initarg :datum))
1280 (lambda (condition stream)
1281 ;; Don't try to print the structure, since it probably won't work.
1283 "~@<obsolete structure error for a structure of type ~2I~_~S~:>"
1284 (type-of (obsolete-structure-datum condition))))))
1286 (defun obsolete-instance-trap (owrapper nwrapper instance)
1287 (if (not (pcl-instance-p instance))
1288 (if *in-obsolete-instance-trap*
1289 *the-wrapper-of-structure-object*
1290 (let ((*in-obsolete-instance-trap* t))
1291 (error 'obsolete-structure :datum instance)))
1292 (let* ((class (wrapper-class* nwrapper))
1293 (copy (allocate-instance class)) ;??? allocate-instance ???
1294 (olayout (wrapper-instance-slots-layout owrapper))
1295 (nlayout (wrapper-instance-slots-layout nwrapper))
1296 (oslots (get-slots instance))
1297 (nslots (get-slots copy))
1298 (oclass-slots (wrapper-class-slots owrapper))
1302 ;; local --> local transfer
1303 ;; local --> shared discard
1304 ;; local --> -- discard
1305 ;; shared --> local transfer
1306 ;; shared --> shared discard
1307 ;; shared --> -- discard
1311 ;; Go through all the old local slots.
1313 (dolist (name olayout)
1314 (let ((npos (posq name nlayout)))
1316 (setf (clos-slots-ref nslots npos)
1317 (clos-slots-ref oslots opos))
1319 (push name discarded)
1320 (unless (eq (clos-slots-ref oslots opos) +slot-unbound+)
1321 (setf (getf plist name) (clos-slots-ref oslots opos))))))
1324 ;; Go through all the old shared slots.
1325 (dolist (oclass-slot-and-val oclass-slots)
1326 (let ((name (car oclass-slot-and-val))
1327 (val (cdr oclass-slot-and-val)))
1328 (let ((npos (posq name nlayout)))
1330 (setf (clos-slots-ref nslots npos) (cdr oclass-slot-and-val))
1331 (progn (push name discarded)
1332 (unless (eq val +slot-unbound+)
1333 (setf (getf plist name) val)))))))
1335 ;; Go through all the new local slots to compute the added slots.
1336 (dolist (nlocal nlayout)
1337 (unless (or (memq nlocal olayout)
1338 (assq nlocal oclass-slots))
1339 (push nlocal added)))
1341 (swap-wrappers-and-slots instance copy)
1343 (update-instance-for-redefined-class instance
1349 (defun change-class-internal (instance new-class initargs)
1350 (let* ((old-class (class-of instance))
1351 (copy (allocate-instance new-class))
1352 (new-wrapper (get-wrapper copy))
1353 (old-wrapper (class-wrapper old-class))
1354 (old-layout (wrapper-instance-slots-layout old-wrapper))
1355 (new-layout (wrapper-instance-slots-layout new-wrapper))
1356 (old-slots (get-slots instance))
1357 (new-slots (get-slots copy))
1358 (old-class-slots (wrapper-class-slots old-wrapper)))
1360 ;; "The values of local slots specified by both the class CTO and
1361 ;; CFROM are retained. If such a local slot was unbound, it
1362 ;; remains unbound."
1363 (let ((new-position 0))
1364 (dolist (new-slot new-layout)
1365 (let ((old-position (posq new-slot old-layout)))
1367 (setf (clos-slots-ref new-slots new-position)
1368 (clos-slots-ref old-slots old-position))))
1369 (incf new-position)))
1371 ;; "The values of slots specified as shared in the class CFROM and
1372 ;; as local in the class CTO are retained."
1373 (dolist (slot-and-val old-class-slots)
1374 (let ((position (posq (car slot-and-val) new-layout)))
1376 (setf (clos-slots-ref new-slots position) (cdr slot-and-val)))))
1378 ;; Make the copy point to the old instance's storage, and make the
1379 ;; old instance point to the new storage.
1380 (swap-wrappers-and-slots instance copy)
1382 (apply #'update-instance-for-different-class copy instance initargs)
1385 (defmethod change-class ((instance standard-object)
1386 (new-class standard-class)
1388 (change-class-internal instance new-class initargs))
1390 (defmethod change-class ((instance funcallable-standard-object)
1391 (new-class funcallable-standard-class)
1393 (change-class-internal instance new-class initargs))
1395 (defmethod change-class ((instance standard-object)
1396 (new-class funcallable-standard-class)
1398 (declare (ignore initargs))
1399 (error "You can't change the class of ~S to ~S~@
1400 because it isn't already an instance with metaclass ~S."
1401 instance new-class 'standard-class))
1403 (defmethod change-class ((instance funcallable-standard-object)
1404 (new-class standard-class)
1406 (declare (ignore initargs))
1407 (error "You can't change the class of ~S to ~S~@
1408 because it isn't already an instance with metaclass ~S."
1409 instance new-class 'funcallable-standard-class))
1411 (defmethod change-class ((instance t) (new-class-name symbol) &rest initargs)
1412 (apply #'change-class instance (find-class new-class-name) initargs))
1414 ;;;; The metaclass BUILT-IN-CLASS
1416 ;;;; This metaclass is something of a weird creature. By this point, all
1417 ;;;; instances of it which will exist have been created, and no instance
1418 ;;;; is ever created by calling MAKE-INSTANCE.
1420 ;;;; But, there are other parts of the protocol we must follow and those
1421 ;;;; definitions appear here.
1423 (defmethod shared-initialize :before
1424 ((class built-in-class) slot-names &rest initargs)
1425 (declare (ignore slot-names initargs))
1426 (error "attempt to initialize or reinitialize a built in class"))
1428 (defmethod class-direct-slots ((class built-in-class)) ())
1429 (defmethod class-slots ((class built-in-class)) ())
1430 (defmethod class-direct-default-initargs ((class built-in-class)) ())
1431 (defmethod class-default-initargs ((class built-in-class)) ())
1433 (defmethod validate-superclass ((c class) (s built-in-class))
1434 (or (eq s *the-class-t*)
1435 (eq s *the-class-stream*)))
1437 ;;; Some necessary methods for FORWARD-REFERENCED-CLASS
1438 (defmethod class-direct-slots ((class forward-referenced-class)) ())
1439 (defmethod class-direct-default-initargs ((class forward-referenced-class)) ())
1440 (macrolet ((def (method)
1441 `(defmethod ,method ((class forward-referenced-class))
1442 (error "~@<~I~S was called on a forward referenced class:~2I~_~S~:>"
1444 (def class-default-initargs)
1445 (def class-precedence-list)
1448 (defmethod validate-superclass ((c slot-class)
1449 (f forward-referenced-class))
1452 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1453 (pushnew dependent (plist-value metaobject 'dependents)))
1455 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1456 (setf (plist-value metaobject 'dependents)
1457 (delete dependent (plist-value metaobject 'dependents))))
1459 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1460 (dolist (dependent (plist-value metaobject 'dependents))
1461 (funcall function dependent)))