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-finalized-p ((class pcl-class))
155 (with-slots (wrapper) class
156 (not (null wrapper))))
158 (defmethod class-prototype ((class std-class))
159 (with-slots (prototype) class
160 (or prototype (setq prototype (allocate-instance class)))))
162 (defmethod class-prototype ((class structure-class))
163 (with-slots (prototype wrapper defstruct-constructor) class
166 (if defstruct-constructor
167 (allocate-instance class)
168 (allocate-standard-instance wrapper))))))
170 (defmethod class-direct-default-initargs ((class slot-class))
171 (plist-value class 'direct-default-initargs))
173 (defmethod class-default-initargs ((class slot-class))
174 (plist-value class 'default-initargs))
176 (defmethod class-constructors ((class slot-class))
177 (plist-value class 'constructors))
179 (defmethod class-slot-cells ((class std-class))
180 (plist-value class 'class-slot-cells))
182 ;;;; class accessors that are even a little bit more complicated than those
183 ;;;; above. These have a protocol for updating them, we must implement that
186 ;;; Maintaining the direct subclasses backpointers. The update methods are
187 ;;; here, the values are read by an automatically generated reader method.
188 (defmethod add-direct-subclass ((class class) (subclass class))
189 (with-slots (direct-subclasses) class
190 (pushnew subclass direct-subclasses)
192 (defmethod remove-direct-subclass ((class class) (subclass class))
193 (with-slots (direct-subclasses) class
194 (setq direct-subclasses (remove subclass direct-subclasses))
197 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
199 ;;; There are four generic functions involved, each has one method for the
200 ;;; class case and another method for the damned EQL specializers. All of
201 ;;; these are specified methods and appear in their specified place in the
204 ;;; ADD-DIRECT-METHOD
205 ;;; REMOVE-DIRECT-METHOD
206 ;;; SPECIALIZER-DIRECT-METHODS
207 ;;; SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
209 ;;; In each case, we maintain one value which is a cons. The car is the list
210 ;;; methods. The cdr is a list of the generic functions. The cdr is always
212 (defmethod add-direct-method ((specializer class) (method method))
213 (with-slots (direct-methods) specializer
214 (setf (car direct-methods) (adjoin method (car direct-methods)) ;PUSH
215 (cdr direct-methods) ()))
217 (defmethod remove-direct-method ((specializer class) (method method))
218 (with-slots (direct-methods) specializer
219 (setf (car direct-methods) (remove method (car direct-methods))
220 (cdr direct-methods) ()))
223 (defmethod specializer-direct-methods ((specializer class))
224 (with-slots (direct-methods) specializer
225 (car direct-methods)))
227 (defmethod specializer-direct-generic-functions ((specializer class))
228 (with-slots (direct-methods) specializer
229 (or (cdr direct-methods)
230 (setf (cdr direct-methods)
231 (gathering1 (collecting-once)
232 (dolist (m (car direct-methods))
233 (gather1 (method-generic-function m))))))))
235 ;;; This hash table is used to store the direct methods and direct generic
236 ;;; functions of EQL specializers. Each value in the table is the cons.
237 (defvar *eql-specializer-methods* (make-hash-table :test 'eql))
238 (defvar *class-eq-specializer-methods* (make-hash-table :test 'eq))
240 (defmethod specializer-method-table ((specializer eql-specializer))
241 *eql-specializer-methods*)
243 (defmethod specializer-method-table ((specializer class-eq-specializer))
244 *class-eq-specializer-methods*)
246 (defmethod add-direct-method ((specializer specializer-with-object)
248 (let* ((object (specializer-object specializer))
249 (table (specializer-method-table specializer))
250 (entry (gethash object table)))
253 (setf (gethash object table)
255 (setf (car entry) (adjoin method (car entry))
259 (defmethod remove-direct-method ((specializer specializer-with-object)
261 (let* ((object (specializer-object specializer))
262 (entry (gethash object (specializer-method-table specializer))))
264 (setf (car entry) (remove method (car entry))
268 (defmethod specializer-direct-methods ((specializer specializer-with-object))
269 (car (gethash (specializer-object specializer)
270 (specializer-method-table specializer))))
272 (defmethod specializer-direct-generic-functions ((specializer
273 specializer-with-object))
274 (let* ((object (specializer-object specializer))
275 (entry (gethash object (specializer-method-table specializer))))
279 (gathering1 (collecting-once)
280 (dolist (m (car entry))
281 (gather1 (method-generic-function m)))))))))
283 (defun map-specializers (function)
284 (map-all-classes #'(lambda (class)
285 (funcall function (class-eq-specializer class))
286 (funcall function class)))
287 (maphash #'(lambda (object methods)
288 (declare (ignore methods))
289 (intern-eql-specializer object))
290 *eql-specializer-methods*)
291 (maphash #'(lambda (object specl)
292 (declare (ignore object))
293 (funcall function specl))
294 *eql-specializer-table*)
297 (defun map-all-generic-functions (function)
298 (let ((all-generic-functions (make-hash-table :test 'eq)))
299 (map-specializers #'(lambda (specl)
300 (dolist (gf (specializer-direct-generic-functions
302 (unless (gethash gf all-generic-functions)
303 (setf (gethash gf all-generic-functions) t)
304 (funcall function gf))))))
307 (defmethod shared-initialize :after ((specl class-eq-specializer)
310 (declare (ignore slot-names))
311 (setf (slot-value specl 'type) `(class-eq ,(specializer-class specl))))
313 (defmethod shared-initialize :after ((specl eql-specializer) slot-names &key)
314 (declare (ignore slot-names))
315 (setf (slot-value specl 'type) `(eql ,(specializer-object specl))))
317 (defun real-load-defclass (name metaclass-name supers slots other)
318 (let ((res (apply #'ensure-class name :metaclass metaclass-name
319 :direct-superclasses supers
321 :definition-source `((defclass ,name)
324 ;; Defclass of a class with a forward-referenced superclass does not
325 ;; have a wrapper. RES is the incomplete PCL class. The Lisp class
326 ;; does not yet exist. Maybe should return NIL in that case as RES
327 ;; is not useful to the user?
328 (and (class-wrapper res) (sb-kernel:layout-class (class-wrapper res)))))
330 (setf (gdefinition 'load-defclass) #'real-load-defclass)
332 (defun ensure-class (name &rest all)
333 (apply #'ensure-class-using-class name (find-class name nil) all))
335 (defmethod ensure-class-using-class (name (class null) &rest args &key)
336 (multiple-value-bind (meta initargs)
337 (ensure-class-values class args)
338 (inform-type-system-about-class (class-prototype meta) name);***
339 (setf class (apply #'make-instance meta :name name initargs)
340 (find-class name) class)
341 (inform-type-system-about-class class name) ;***
344 (defmethod ensure-class-using-class (name (class pcl-class) &rest args &key)
345 (multiple-value-bind (meta initargs)
346 (ensure-class-values class args)
347 (unless (eq (class-of class) meta) (change-class class meta))
348 (apply #'reinitialize-instance class initargs)
349 (setf (find-class name) class)
350 (inform-type-system-about-class class name) ;***
353 (defmethod class-predicate-name ((class t))
356 (defun ensure-class-values (class args)
357 (let* ((initargs (copy-list args))
358 (unsupplied (list 1))
359 (supplied-meta (getf initargs :metaclass unsupplied))
360 (supplied-supers (getf initargs :direct-superclasses unsupplied))
361 (supplied-slots (getf initargs :direct-slots unsupplied))
363 (cond ((neq supplied-meta unsupplied)
364 (find-class supplied-meta))
366 (forward-referenced-class-p class))
367 *the-class-standard-class*)
370 (flet ((fix-super (s)
372 ((not (legal-class-name-p s))
373 (error "~S is not a class or a legal class name." s))
375 (or (find-class s nil)
377 (make-instance 'forward-referenced-class
379 (loop (unless (remf initargs :metaclass) (return)))
380 (loop (unless (remf initargs :direct-superclasses) (return)))
381 (loop (unless (remf initargs :direct-slots) (return)))
383 (list* :direct-superclasses
384 (and (neq supplied-supers unsupplied)
385 (mapcar #'fix-super supplied-supers))
387 (and (neq supplied-slots unsupplied) supplied-slots)
390 #|| ; since it doesn't do anything
391 (defmethod shared-initialize :before ((class std-class)
393 &key direct-superclasses)
394 (declare (ignore slot-names))
395 ;; *** error checking
399 (defmethod shared-initialize :after
402 &key (direct-superclasses nil direct-superclasses-p)
403 (direct-slots nil direct-slots-p)
404 (direct-default-initargs nil direct-default-initargs-p)
405 (predicate-name nil predicate-name-p))
406 (declare (ignore slot-names))
407 (cond (direct-superclasses-p
408 (setq direct-superclasses
409 (or direct-superclasses
410 (list (if (funcallable-standard-class-p class)
411 *the-class-funcallable-standard-object*
412 *the-class-standard-object*))))
413 (dolist (superclass direct-superclasses)
414 (unless (validate-superclass class superclass)
415 (error "The class ~S was specified as a~%
416 super-class of the class ~S;~%~
417 but the meta-classes ~S and~%~S are incompatible.~@
418 Define a method for ~S to avoid this error."
419 superclass class (class-of superclass) (class-of class)
420 'validate-superclass)))
421 (setf (slot-value class 'direct-superclasses) direct-superclasses))
423 (setq direct-superclasses (slot-value class 'direct-superclasses))))
426 (setf (slot-value class 'direct-slots)
427 (mapcar (lambda (pl) (make-direct-slotd class pl))
429 (slot-value class 'direct-slots)))
430 (if direct-default-initargs-p
431 (setf (plist-value class 'direct-default-initargs)
432 direct-default-initargs)
433 (setq direct-default-initargs
434 (plist-value class 'direct-default-initargs)))
435 (setf (plist-value class 'class-slot-cells)
436 (gathering1 (collecting)
437 (dolist (dslotd direct-slots)
438 (when (eq (slot-definition-allocation dslotd) class)
439 (let ((initfunction (slot-definition-initfunction dslotd)))
440 (gather1 (cons (slot-definition-name dslotd)
442 (funcall initfunction)
443 +slot-unbound+))))))))
444 (setq predicate-name (if predicate-name-p
445 (setf (slot-value class 'predicate-name)
446 (car predicate-name))
447 (or (slot-value class 'predicate-name)
448 (setf (slot-value class 'predicate-name)
449 (make-class-predicate-name (class-name
451 (add-direct-subclasses class direct-superclasses)
452 (update-class class nil)
453 (make-class-predicate class predicate-name)
454 (add-slot-accessors class direct-slots))
456 (defmethod shared-initialize :before ((class class) slot-names &key name)
457 (declare (ignore slot-names name))
458 ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
459 ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
460 (setf (slot-value class 'type) `(class ,class))
461 (setf (slot-value class 'class-eq-specializer)
462 (make-instance 'class-eq-specializer :class class)))
464 (defmethod reinitialize-instance :before ((class slot-class) &key)
465 (remove-direct-subclasses class (class-direct-superclasses class))
466 (remove-slot-accessors class (class-direct-slots class)))
468 (defmethod reinitialize-instance :after ((class slot-class)
471 (map-dependents class
472 #'(lambda (dependent)
473 (apply #'update-dependent class dependent initargs))))
475 (defmethod shared-initialize :after
476 ((class structure-class)
478 &key (direct-superclasses nil direct-superclasses-p)
479 (direct-slots nil direct-slots-p)
480 direct-default-initargs
481 (predicate-name nil predicate-name-p))
482 (declare (ignore slot-names direct-default-initargs))
483 (if direct-superclasses-p
484 (setf (slot-value class 'direct-superclasses)
485 (or direct-superclasses
486 (setq direct-superclasses
487 (and (not (eq (class-name class) 'structure-object))
488 (list *the-class-structure-object*)))))
489 (setq direct-superclasses (slot-value class 'direct-superclasses)))
490 (let* ((name (class-name class))
491 (from-defclass-p (slot-value class 'from-defclass-p))
492 (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
494 (setf (slot-value class 'direct-slots)
496 (mapcar #'(lambda (pl)
498 (let* ((slot-name (getf pl :name))
501 "~S structure class ~A"
503 (accessor (intern acc-name)))
504 (setq pl (list* :defstruct-accessor-symbol
506 (make-direct-slotd class pl))
508 (setq direct-slots (slot-value class 'direct-slots)))
510 (let* ((include (car (slot-value class 'direct-superclasses)))
511 (conc-name (intern (format nil "~S structure class " name)))
512 (constructor (intern (format nil "~A constructor" conc-name)))
513 (defstruct `(defstruct (,name
515 `((:include ,(class-name include))))
516 (:print-function print-std-instance)
518 (:conc-name ,conc-name)
519 (:constructor ,constructor ())
521 ,@(mapcar (lambda (slot)
522 `(,(slot-definition-name slot)
525 (reader-names (mapcar (lambda (slotd)
529 (slot-definition-name
532 (writer-names (mapcar (lambda (slotd)
536 (slot-definition-name
540 (mapcar (lambda (slotd reader-name)
542 (slot-definition-defstruct-accessor-symbol
544 `(defun ,reader-name (obj)
545 (declare (type ,name obj))
547 direct-slots reader-names))
549 (mapcar (lambda (slotd writer-name)
551 (slot-definition-defstruct-accessor-symbol
553 `(defun ,writer-name (nv obj)
554 (declare (type ,name obj))
555 (setf (,accessor obj) nv))))
556 direct-slots writer-names))
560 ,@readers-init ,@writers-init
562 (unless (structure-type-p name) (eval defstruct-form))
563 (mapc #'(lambda (dslotd reader-name writer-name)
564 (let* ((reader (gdefinition reader-name))
565 (writer (when (gboundp writer-name)
566 (gdefinition writer-name))))
567 (setf (slot-value dslotd 'internal-reader-function)
569 (setf (slot-value dslotd 'internal-writer-function)
571 direct-slots reader-names writer-names)
572 (setf (slot-value class 'defstruct-form) defstruct-form)
573 (setf (slot-value class 'defstruct-constructor) constructor))))
574 (add-direct-subclasses class direct-superclasses)
575 (setf (slot-value class 'class-precedence-list)
576 (compute-class-precedence-list class))
577 (setf (slot-value class 'slots) (compute-slots class))
578 (let ((lclass (cl:find-class (class-name class))))
579 (setf (sb-kernel:class-pcl-class lclass) class)
580 (setf (slot-value class 'wrapper) (sb-kernel:class-layout lclass)))
581 (update-pv-table-cache-info class)
582 (setq predicate-name (if predicate-name-p
583 (setf (slot-value class 'predicate-name)
584 (car predicate-name))
585 (or (slot-value class 'predicate-name)
586 (setf (slot-value class 'predicate-name)
587 (make-class-predicate-name
588 (class-name class))))))
589 (make-class-predicate class predicate-name)
590 (add-slot-accessors class direct-slots))
592 (defmethod direct-slot-definition-class ((class structure-class) initargs)
593 (declare (ignore initargs))
594 (find-class 'structure-direct-slot-definition))
596 (defmethod finalize-inheritance ((class structure-class))
597 nil) ; always finalized
599 (defun add-slot-accessors (class dslotds)
600 (fix-slot-accessors class dslotds 'add))
602 (defun remove-slot-accessors (class dslotds)
603 (fix-slot-accessors class dslotds 'remove))
605 (defun fix-slot-accessors (class dslotds add/remove)
606 (flet ((fix (gfspec name r/w)
607 (let ((gf (ensure-generic-function gfspec)))
609 (r (if (eq add/remove 'add)
610 (add-reader-method class gf name)
611 (remove-reader-method class gf)))
612 (w (if (eq add/remove 'add)
613 (add-writer-method class gf name)
614 (remove-writer-method class gf)))))))
615 (dolist (dslotd dslotds)
616 (let ((slot-name (slot-definition-name dslotd)))
617 (dolist (r (slot-definition-readers dslotd)) (fix r slot-name 'r))
618 (dolist (w (slot-definition-writers dslotd)) (fix w slot-name 'w))))))
620 (defun add-direct-subclasses (class new)
622 (unless (memq class (class-direct-subclasses class))
623 (add-direct-subclass n class))))
625 (defun remove-direct-subclasses (class new)
626 (let ((old (class-direct-superclasses class)))
627 (dolist (o (set-difference old new))
628 (remove-direct-subclass o class))))
630 (defmethod finalize-inheritance ((class std-class))
631 (update-class class t))
633 (defun class-has-a-forward-referenced-superclass-p (class)
634 (or (forward-referenced-class-p class)
635 (some #'class-has-a-forward-referenced-superclass-p
636 (class-direct-superclasses class))))
638 ;;; This is called by :after shared-initialize whenever a class is initialized
639 ;;; or reinitialized. The class may or may not be finalized.
640 (defun update-class (class finalizep)
641 (when (or finalizep (class-finalized-p class)
642 (not (class-has-a-forward-referenced-superclass-p class)))
643 (update-cpl class (compute-class-precedence-list class))
644 (update-slots class (compute-slots class))
645 (update-gfs-of-class class)
646 (update-inits class (compute-default-initargs class))
647 (update-make-instance-function-table class))
649 (dolist (sub (class-direct-subclasses class)) (update-class sub nil))))
651 (defun update-cpl (class cpl)
652 (if (class-finalized-p class)
653 (unless (equal (class-precedence-list class) cpl)
654 ;; comment from the old CMU CL sources:
655 ;; Need to have the cpl setup before update-lisp-class-layout
656 ;; is called on CMU CL.
657 (setf (slot-value class 'class-precedence-list) cpl)
658 (force-cache-flushes class))
659 (setf (slot-value class 'class-precedence-list) cpl))
660 (update-class-can-precede-p cpl))
662 (defun update-class-can-precede-p (cpl)
664 (let ((first (car cpl)))
665 (dolist (c (cdr cpl))
666 (pushnew c (slot-value first 'can-precede-list))))
667 (update-class-can-precede-p (cdr cpl))))
669 (defun class-can-precede-p (class1 class2)
670 (member class2 (class-can-precede-list class1)))
672 (defun update-slots (class eslotds)
673 (let ((instance-slots ())
675 (dolist (eslotd eslotds)
676 (let ((alloc (slot-definition-allocation eslotd)))
677 (cond ((eq alloc :instance) (push eslotd instance-slots))
678 ((classp alloc) (push eslotd class-slots)))))
680 ;; If there is a change in the shape of the instances then the
681 ;; old class is now obsolete.
682 (let* ((nlayout (mapcar #'slot-definition-name
683 (sort instance-slots #'<
684 :key #'slot-definition-location)))
685 (nslots (length nlayout))
686 (nwrapper-class-slots (compute-class-slots class-slots))
687 (owrapper (class-wrapper class))
688 (olayout (and owrapper (wrapper-instance-slots-layout owrapper)))
689 (owrapper-class-slots (and owrapper (wrapper-class-slots owrapper)))
691 (cond ((null owrapper)
692 (make-wrapper nslots class))
693 ((and (equal nlayout olayout)
695 (iterate ((o (list-elements owrapper-class-slots))
696 (n (list-elements nwrapper-class-slots)))
697 (unless (eq (car o) (car n)) (return t)))))
700 ;; This will initialize the new wrapper to have the same
701 ;; state as the old wrapper. We will then have to change
702 ;; that. This may seem like wasted work (it is), but the
703 ;; spec requires that we call make-instances-obsolete.
704 (make-instances-obsolete class)
705 (class-wrapper class)))))
707 (with-slots (wrapper slots) class
708 (update-lisp-class-layout class nwrapper)
710 (wrapper-instance-slots-layout nwrapper) nlayout
711 (wrapper-class-slots nwrapper) nwrapper-class-slots
712 (wrapper-no-of-instance-slots nwrapper) nslots
715 (unless (eq owrapper nwrapper)
716 (update-pv-table-cache-info class)))))
718 (defun compute-class-slots (eslotds)
719 (gathering1 (collecting)
720 (dolist (eslotd eslotds)
722 (assoc (slot-definition-name eslotd)
723 (class-slot-cells (slot-definition-allocation eslotd)))))))
725 (defun compute-layout (cpl instance-eslotds)
727 (gathering1 (collecting)
728 (dolist (eslotd instance-eslotds)
729 (when (eq (slot-definition-allocation eslotd) :instance)
730 (gather1 (slot-definition-name eslotd))))))
732 (labels ((rwalk (tail)
735 (dolist (ss (class-slots (car tail)))
736 (let ((n (slot-definition-name ss)))
737 (when (member n names)
738 (setq order (cons n order)
739 names (remove n names))))))))
740 (rwalk (if (slot-boundp (car cpl) 'slots)
743 (reverse (append names order)))))
745 (defun update-gfs-of-class (class)
746 (when (and (class-finalized-p class)
747 (let ((cpl (class-precedence-list class)))
748 (or (member *the-class-slot-class* cpl)
749 (member *the-class-standard-effective-slot-definition*
751 (let ((gf-table (make-hash-table :test 'eq)))
752 (labels ((collect-gfs (class)
753 (dolist (gf (specializer-direct-generic-functions class))
754 (setf (gethash gf gf-table) t))
755 (mapc #'collect-gfs (class-direct-superclasses class))))
757 (maphash #'(lambda (gf ignore)
758 (declare (ignore ignore))
759 (update-gf-dfun class gf))
762 (defun update-inits (class inits)
763 (setf (plist-value class 'default-initargs) inits))
765 (defmethod compute-default-initargs ((class slot-class))
766 (let ((cpl (class-precedence-list class))
767 (direct (class-direct-default-initargs class)))
768 (labels ((walk (tail)
771 (let ((c (pop tail)))
772 (append (if (eq c class)
774 (class-direct-default-initargs c))
776 (let ((initargs (walk cpl)))
777 (delete-duplicates initargs :test #'eq :key #'car :from-end t)))))
779 ;;;; protocols for constructing direct and effective slot definitions
781 (defmethod direct-slot-definition-class ((class std-class) initargs)
782 (declare (ignore initargs))
783 (find-class 'standard-direct-slot-definition))
785 (defun make-direct-slotd (class initargs)
786 (let ((initargs (list* :class class initargs)))
787 (apply #'make-instance
788 (direct-slot-definition-class class initargs)
791 (defmethod compute-slots ((class std-class))
792 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
793 ;; for each different slot name we find in our superclasses. Each
794 ;; call receives the class and a list of the dslotds with that name.
795 ;; The list is in most-specific-first order.
796 (let ((name-dslotds-alist ()))
797 (dolist (c (class-precedence-list class))
798 (let ((dslotds (class-direct-slots c)))
800 (let* ((name (slot-definition-name d))
801 (entry (assq name name-dslotds-alist)))
804 (push (list name d) name-dslotds-alist))))))
805 (mapcar #'(lambda (direct)
806 (compute-effective-slot-definition class
807 (nreverse (cdr direct))))
808 name-dslotds-alist)))
810 (defmethod compute-slots :around ((class std-class))
811 (let ((eslotds (call-next-method))
812 (cpl (class-precedence-list class))
815 (dolist (eslotd eslotds)
816 (let ((alloc (slot-definition-allocation eslotd)))
817 (cond ((eq alloc :instance) (push eslotd instance-slots))
818 ((classp alloc) (push eslotd class-slots)))))
819 (let ((nlayout (compute-layout cpl instance-slots)))
820 (dolist (eslotd instance-slots)
821 (setf (slot-definition-location eslotd)
822 (position (slot-definition-name eslotd) nlayout))))
823 (dolist (eslotd class-slots)
824 (setf (slot-definition-location eslotd)
825 (assoc (slot-definition-name eslotd)
826 (class-slot-cells (slot-definition-allocation eslotd)))))
827 (mapc #'initialize-internal-slot-functions eslotds)
830 (defmethod compute-slots ((class structure-class))
831 (mapcan #'(lambda (superclass)
832 (mapcar #'(lambda (dslotd)
833 (compute-effective-slot-definition class
835 (class-direct-slots superclass)))
836 (reverse (slot-value class 'class-precedence-list))))
838 (defmethod compute-slots :around ((class structure-class))
839 (let ((eslotds (call-next-method)))
840 (mapc #'initialize-internal-slot-functions eslotds)
843 (defmethod compute-effective-slot-definition ((class slot-class) dslotds)
844 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
845 (class (effective-slot-definition-class class initargs)))
846 (apply #'make-instance class initargs)))
848 (defmethod effective-slot-definition-class ((class std-class) initargs)
849 (declare (ignore initargs))
850 (find-class 'standard-effective-slot-definition))
852 (defmethod effective-slot-definition-class ((class structure-class) initargs)
853 (declare (ignore initargs))
854 (find-class 'structure-effective-slot-definition))
856 (defmethod compute-effective-slot-definition-initargs
857 ((class slot-class) direct-slotds)
868 (dolist (slotd direct-slotds)
871 (setq name (slot-definition-name slotd)
874 (when (slot-definition-initfunction slotd)
875 (setq initform (slot-definition-initform slotd)
876 initfunction (slot-definition-initfunction slotd)
879 (setq allocation (slot-definition-allocation slotd)
881 (setq initargs (append (slot-definition-initargs slotd) initargs))
882 (let ((slotd-type (slot-definition-type slotd)))
883 (setq type (cond ((eq type t) slotd-type)
884 ((*subtypep type slotd-type) type)
885 (t `(and ,type ,slotd-type)))))))
888 :initfunction initfunction
890 :allocation allocation
894 (defmethod compute-effective-slot-definition-initargs :around
895 ((class structure-class) direct-slotds)
896 (let ((slotd (car direct-slotds)))
897 (list* :defstruct-accessor-symbol
898 (slot-definition-defstruct-accessor-symbol slotd)
899 :internal-reader-function
900 (slot-definition-internal-reader-function slotd)
901 :internal-writer-function
902 (slot-definition-internal-writer-function slotd)
903 (call-next-method))))
905 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
906 ;;; to make the method object. They have to use make-a-method which
907 ;;; is a specially bootstrapped mechanism for making standard methods.
908 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
909 (declare (ignore direct-slot initargs))
910 (find-class 'standard-reader-method))
912 (defmethod add-reader-method ((class slot-class) generic-function slot-name)
913 (add-method generic-function
914 (make-a-method 'standard-reader-method
916 (list (or (class-name class) 'object))
918 (make-reader-method-function class slot-name)
919 "automatically generated reader method"
922 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
923 (declare (ignore direct-slot initargs))
924 (find-class 'standard-writer-method))
926 (defmethod add-writer-method ((class slot-class) generic-function slot-name)
927 (add-method generic-function
928 (make-a-method 'standard-writer-method
930 (list 'new-value (or (class-name class) 'object))
931 (list *the-class-t* class)
932 (make-writer-method-function class slot-name)
933 "automatically generated writer method"
936 (defmethod add-boundp-method ((class slot-class) generic-function slot-name)
937 (add-method generic-function
938 (make-a-method 'standard-boundp-method
940 (list (or (class-name class) 'object))
942 (make-boundp-method-function class slot-name)
943 "automatically generated boundp method"
946 (defmethod remove-reader-method ((class slot-class) generic-function)
947 (let ((method (get-method generic-function () (list class) nil)))
948 (when method (remove-method generic-function method))))
950 (defmethod remove-writer-method ((class slot-class) generic-function)
952 (get-method generic-function () (list *the-class-t* class) nil)))
953 (when method (remove-method generic-function method))))
955 (defmethod remove-boundp-method ((class slot-class) generic-function)
956 (let ((method (get-method generic-function () (list class) nil)))
957 (when method (remove-method generic-function method))))
959 ;;; make-reader-method-function and make-write-method function are NOT part of
960 ;;; the standard protocol. They are however useful, PCL makes uses makes use
961 ;;; of them internally and documents them for PCL users.
963 ;;; *** This needs work to make type testing by the writer functions which
964 ;;; *** do type testing faster. The idea would be to have one constructor
965 ;;; *** for each possible type test. In order to do this it would be nice
966 ;;; *** to have help from inform-type-system-about-class and friends.
968 ;;; *** There is a subtle bug here which is going to have to be fixed.
969 ;;; *** Namely, the simplistic use of the template has to be fixed. We
970 ;;; *** have to give the optimize-slot-value method the user might have
971 ;;; *** defined for this metclass a chance to run.
973 (defmethod make-reader-method-function ((class slot-class) slot-name)
974 (make-std-reader-method-function (class-name class) slot-name))
976 (defmethod make-writer-method-function ((class slot-class) slot-name)
977 (make-std-writer-method-function (class-name class) slot-name))
979 (defmethod make-boundp-method-function ((class slot-class) slot-name)
980 (make-std-boundp-method-function (class-name class) slot-name))
982 ;;;; inform-type-system-about-class
983 ;;;; make-type-predicate
985 ;;; These are NOT part of the standard protocol. They are internal
986 ;;; mechanism which PCL uses to *try* and tell the type system about
987 ;;; class definitions. In a more fully integrated implementation of
988 ;;; CLOS, the type system would know about class objects and class
989 ;;; names in a more fundamental way and the mechanism used to inform
990 ;;; the type system about new classes would be different.
991 (defmethod inform-type-system-about-class ((class std-class) name)
992 (inform-type-system-about-std-class name))
994 (defmethod compatible-meta-class-change-p (class proto-new-class)
995 (eq (class-of class) (class-of proto-new-class)))
997 (defmethod validate-superclass ((class class) (new-super class))
998 (or (eq new-super *the-class-t*)
999 (eq (class-of class) (class-of new-super))))
1001 (defmethod validate-superclass ((class standard-class) (new-super std-class))
1002 (let ((new-super-meta-class (class-of new-super)))
1003 (or (eq new-super-meta-class *the-class-std-class*)
1004 (eq (class-of class) new-super-meta-class))))
1006 (defun force-cache-flushes (class)
1007 (let* ((owrapper (class-wrapper class))
1008 (state (wrapper-state owrapper)))
1009 ;; We only need to do something if the state is still T. If the
1010 ;; state isn't T, it will be FLUSH or OBSOLETE, and both of those
1011 ;; will already be doing what we want. In particular, we must be
1012 ;; sure we never change an OBSOLETE into a FLUSH since OBSOLETE
1013 ;; means do what FLUSH does and then some.
1014 (when (eq state t) ; FIXME: should be done through INVALID-WRAPPER-P
1015 (let ((nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1017 (setf (wrapper-instance-slots-layout nwrapper)
1018 (wrapper-instance-slots-layout owrapper))
1019 (setf (wrapper-class-slots nwrapper)
1020 (wrapper-class-slots owrapper))
1021 (sb-sys:without-interrupts
1022 (update-lisp-class-layout class nwrapper)
1023 (setf (slot-value class 'wrapper) nwrapper)
1024 (invalidate-wrapper owrapper ':flush nwrapper))))))
1026 (defun flush-cache-trap (owrapper nwrapper instance)
1027 (declare (ignore owrapper))
1028 (set-wrapper instance nwrapper))
1030 ;;; make-instances-obsolete can be called by user code. It will cause the
1031 ;;; next access to the instance (as defined in 88-002R) to trap through the
1032 ;;; update-instance-for-redefined-class mechanism.
1033 (defmethod make-instances-obsolete ((class std-class))
1034 (let* ((owrapper (class-wrapper class))
1035 (nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1037 (setf (wrapper-instance-slots-layout nwrapper)
1038 (wrapper-instance-slots-layout owrapper))
1039 (setf (wrapper-class-slots nwrapper)
1040 (wrapper-class-slots owrapper))
1041 (sb-sys:without-interrupts
1042 (update-lisp-class-layout class nwrapper)
1043 (setf (slot-value class 'wrapper) nwrapper)
1044 (invalidate-wrapper owrapper ':obsolete nwrapper)
1047 (defmethod make-instances-obsolete ((class symbol))
1048 (make-instances-obsolete (find-class class)))
1050 ;;; obsolete-instance-trap is the internal trap that is called when we see
1051 ;;; an obsolete instance. The times when it is called are:
1052 ;;; - when the instance is involved in method lookup
1053 ;;; - when attempting to access a slot of an instance
1055 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1056 ;;; instance access macros.
1058 ;;; Of course these times when it is called are an internal
1059 ;;; implementation detail of PCL and are not part of the documented
1060 ;;; description of when the obsolete instance update happens. The
1061 ;;; documented description is as it appears in 88-002R.
1063 ;;; This has to return the new wrapper, so it counts on all the
1064 ;;; methods on obsolete-instance-trap-internal to return the new
1065 ;;; wrapper. It also does a little internal error checking to make
1066 ;;; sure that the traps are only happening when they should, and that
1067 ;;; the trap methods are computing appropriate new wrappers.
1069 ;;; obsolete-instance-trap might be called on structure instances
1070 ;;; after a structure is redefined. In most cases, obsolete-instance-trap
1071 ;;; will not be able to fix the old instance, so it must signal an
1072 ;;; error. The hard part of this is that the error system and debugger
1073 ;;; might cause obsolete-instance-trap to be called again, so in that
1074 ;;; case, we have to return some reasonable wrapper, instead.
1076 (defvar *in-obsolete-instance-trap* nil)
1077 (defvar *the-wrapper-of-structure-object*
1078 (class-wrapper (find-class 'structure-object)))
1080 (define-condition obsolete-structure (error)
1081 ((datum :reader obsolete-structure-datum :initarg :datum))
1083 (lambda (condition stream)
1084 ;; Don't try to print the structure, since it probably won't work.
1086 "obsolete structure error in ~S:~@
1087 for a structure of type: ~S"
1088 (sb-kernel::condition-function-name condition)
1089 (type-of (obsolete-structure-datum condition))))))
1091 (defun obsolete-instance-trap (owrapper nwrapper instance)
1092 (if (not (pcl-instance-p instance))
1093 (if *in-obsolete-instance-trap*
1094 *the-wrapper-of-structure-object*
1095 (let ((*in-obsolete-instance-trap* t))
1096 (error 'obsolete-structure :datum instance)))
1097 (let* ((class (wrapper-class* nwrapper))
1098 (copy (allocate-instance class)) ;??? allocate-instance ???
1099 (olayout (wrapper-instance-slots-layout owrapper))
1100 (nlayout (wrapper-instance-slots-layout nwrapper))
1101 (oslots (get-slots instance))
1102 (nslots (get-slots copy))
1103 (oclass-slots (wrapper-class-slots owrapper))
1107 ;; local --> local transfer
1108 ;; local --> shared discard
1109 ;; local --> -- discard
1110 ;; shared --> local transfer
1111 ;; shared --> shared discard
1112 ;; shared --> -- discard
1116 ;; Go through all the old local slots.
1117 (iterate ((name (list-elements olayout))
1118 (opos (interval :from 0)))
1119 (let ((npos (posq name nlayout)))
1121 (setf (clos-slots-ref nslots npos)
1122 (clos-slots-ref oslots opos))
1124 (push name discarded)
1125 (unless (eq (clos-slots-ref oslots opos) +slot-unbound+)
1126 (setf (getf plist name) (clos-slots-ref oslots opos)))))))
1128 ;; Go through all the old shared slots.
1129 (iterate ((oclass-slot-and-val (list-elements oclass-slots)))
1130 (let ((name (car oclass-slot-and-val))
1131 (val (cdr oclass-slot-and-val)))
1132 (let ((npos (posq name nlayout)))
1134 (setf (clos-slots-ref nslots npos) (cdr oclass-slot-and-val))
1135 (progn (push name discarded)
1136 (unless (eq val +slot-unbound+)
1137 (setf (getf plist name) val)))))))
1139 ;; Go through all the new local slots to compute the added slots.
1140 (dolist (nlocal nlayout)
1141 (unless (or (memq nlocal olayout)
1142 (assq nlocal oclass-slots))
1143 (push nlocal added)))
1145 (swap-wrappers-and-slots instance copy)
1147 (update-instance-for-redefined-class instance
1153 (defmacro copy-instance-internal (instance)
1155 (let* ((class (class-of instance))
1156 (copy (allocate-instance class)))
1157 (if (std-instance-p ,instance)
1158 (setf (std-instance-slots ,instance)
1159 (std-instance-slots ,instance))
1160 (setf (fsc-instance-slots ,instance)
1161 (fsc-instance-slots ,instance)))
1164 (defun change-class-internal (instance new-class)
1165 (let* ((old-class (class-of instance))
1166 (copy (allocate-instance new-class))
1167 (new-wrapper (get-wrapper copy))
1168 (old-wrapper (class-wrapper old-class))
1169 (old-layout (wrapper-instance-slots-layout old-wrapper))
1170 (new-layout (wrapper-instance-slots-layout new-wrapper))
1171 (old-slots (get-slots instance))
1172 (new-slots (get-slots copy))
1173 (old-class-slots (wrapper-class-slots old-wrapper)))
1175 ;; "The values of local slots specified by both the class CTO and
1176 ;; CFROM are retained. If such a local slot was unbound, it remains
1178 (iterate ((new-slot (list-elements new-layout))
1179 (new-position (interval :from 0)))
1180 (let ((old-position (posq new-slot old-layout)))
1182 (setf (clos-slots-ref new-slots new-position)
1183 (clos-slots-ref old-slots old-position)))))
1185 ;; "The values of slots specified as shared in the class CFROM and
1186 ;; as local in the class CTO are retained."
1187 (iterate ((slot-and-val (list-elements old-class-slots)))
1188 (let ((position (posq (car slot-and-val) new-layout)))
1190 (setf (clos-slots-ref new-slots position) (cdr slot-and-val)))))
1192 ;; Make the copy point to the old instance's storage, and make the
1193 ;; old instance point to the new storage.
1194 (swap-wrappers-and-slots instance copy)
1196 (update-instance-for-different-class copy instance)
1199 (defmethod change-class ((instance standard-object)
1200 (new-class standard-class))
1201 (change-class-internal instance new-class))
1203 (defmethod change-class ((instance funcallable-standard-object)
1204 (new-class funcallable-standard-class))
1205 (change-class-internal instance new-class))
1207 (defmethod change-class ((instance standard-object)
1208 (new-class funcallable-standard-class))
1209 (error "You can't change the class of ~S to ~S~@
1210 because it isn't already an instance with metaclass ~S."
1211 instance new-class 'standard-class))
1213 (defmethod change-class ((instance funcallable-standard-object)
1214 (new-class standard-class))
1215 (error "You can't change the class of ~S to ~S~@
1216 because it isn't already an instance with metaclass ~S."
1217 instance new-class 'funcallable-standard-class))
1219 (defmethod change-class ((instance t) (new-class-name symbol))
1220 (change-class instance (find-class new-class-name)))
1222 ;;;; The metaclass BUILT-IN-CLASS
1224 ;;;; This metaclass is something of a weird creature. By this point, all
1225 ;;;; instances of it which will exist have been created, and no instance
1226 ;;;; is ever created by calling MAKE-INSTANCE.
1228 ;;;; But, there are other parts of the protocol we must follow and those
1229 ;;;; definitions appear here.
1231 (defmethod shared-initialize :before
1232 ((class built-in-class) slot-names &rest initargs)
1233 (declare (ignore slot-names initargs))
1234 (error "attempt to initialize or reinitialize a built in class"))
1236 (defmethod class-direct-slots ((class built-in-class)) ())
1237 (defmethod class-slots ((class built-in-class)) ())
1238 (defmethod class-direct-default-initargs ((class built-in-class)) ())
1239 (defmethod class-default-initargs ((class built-in-class)) ())
1241 (defmethod validate-superclass ((c class) (s built-in-class))
1242 (or (eq s *the-class-t*)
1243 (eq s *the-class-stream*)))
1245 (defmethod validate-superclass ((c slot-class)
1246 (f forward-referenced-class))
1249 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1250 (pushnew dependent (plist-value metaobject 'dependents)))
1252 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1253 (setf (plist-value metaobject 'dependents)
1254 (delete dependent (plist-value metaobject 'dependents))))
1256 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1257 (dolist (dependent (plist-value metaobject 'dependents))
1258 (funcall function dependent)))