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-slot-cells ((class std-class))
177 (plist-value class 'class-slot-cells))
179 ;;;; class accessors that are even a little bit more complicated than those
180 ;;;; above. These have a protocol for updating them, we must implement that
183 ;;; Maintaining the direct subclasses backpointers. The update methods are
184 ;;; here, the values are read by an automatically generated reader method.
185 (defmethod add-direct-subclass ((class class) (subclass class))
186 (with-slots (direct-subclasses) class
187 (pushnew subclass direct-subclasses)
189 (defmethod remove-direct-subclass ((class class) (subclass class))
190 (with-slots (direct-subclasses) class
191 (setq direct-subclasses (remove subclass direct-subclasses))
194 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
196 ;;; There are four generic functions involved, each has one method for the
197 ;;; class case and another method for the damned EQL specializers. All of
198 ;;; these are specified methods and appear in their specified place in the
201 ;;; ADD-DIRECT-METHOD
202 ;;; REMOVE-DIRECT-METHOD
203 ;;; SPECIALIZER-DIRECT-METHODS
204 ;;; SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
206 ;;; In each case, we maintain one value which is a cons. The car is the list
207 ;;; methods. The cdr is a list of the generic functions. The cdr is always
209 (defmethod add-direct-method ((specializer class) (method method))
210 (with-slots (direct-methods) specializer
211 (setf (car direct-methods) (adjoin method (car direct-methods)) ;PUSH
212 (cdr direct-methods) ()))
214 (defmethod remove-direct-method ((specializer class) (method method))
215 (with-slots (direct-methods) specializer
216 (setf (car direct-methods) (remove method (car direct-methods))
217 (cdr direct-methods) ()))
220 (defmethod specializer-direct-methods ((specializer class))
221 (with-slots (direct-methods) specializer
222 (car direct-methods)))
224 (defmethod specializer-direct-generic-functions ((specializer class))
225 (with-slots (direct-methods) specializer
226 (or (cdr direct-methods)
227 (setf (cdr direct-methods)
229 (dolist (m (car direct-methods))
230 ;; the old PCL code used COLLECTING-ONCE which used
231 ;; #'EQ to check for newness
232 (pushnew (method-generic-function m) collect :test #'eq))
233 (nreverse collect))))))
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))))
280 (dolist (m (car entry))
281 (pushnew (method-generic-function m) collect :test #'eq))
282 (nreverse collect)))))))
284 (defun map-specializers (function)
285 (map-all-classes (lambda (class)
286 (funcall function (class-eq-specializer class))
287 (funcall function class)))
288 (maphash (lambda (object methods)
289 (declare (ignore methods))
290 (intern-eql-specializer object))
291 *eql-specializer-methods*)
292 (maphash (lambda (object specl)
293 (declare (ignore object))
294 (funcall function specl))
295 *eql-specializer-table*)
298 (defun map-all-generic-functions (function)
299 (let ((all-generic-functions (make-hash-table :test 'eq)))
300 (map-specializers (lambda (specl)
301 (dolist (gf (specializer-direct-generic-functions
303 (unless (gethash gf all-generic-functions)
304 (setf (gethash gf all-generic-functions) t)
305 (funcall function gf))))))
308 (defmethod shared-initialize :after ((specl class-eq-specializer)
311 (declare (ignore slot-names))
312 (setf (slot-value specl 'type) `(class-eq ,(specializer-class specl))))
314 (defmethod shared-initialize :after ((specl eql-specializer) slot-names &key)
315 (declare (ignore slot-names))
316 (setf (slot-value specl 'type) `(eql ,(specializer-object specl))))
318 (defun real-load-defclass (name metaclass-name supers slots other)
319 (let ((res (apply #'ensure-class name :metaclass metaclass-name
320 :direct-superclasses supers
322 :definition-source `((defclass ,name)
325 ;; Defclass of a class with a forward-referenced superclass does not
326 ;; have a wrapper. RES is the incomplete PCL class. The Lisp class
327 ;; does not yet exist. Maybe should return NIL in that case as RES
328 ;; is not useful to the user?
329 (and (class-wrapper res) (sb-kernel:layout-class (class-wrapper res)))))
331 (setf (gdefinition 'load-defclass) #'real-load-defclass)
333 (defun ensure-class (name &rest all)
334 (apply #'ensure-class-using-class name (find-class name nil) all))
336 (defmethod ensure-class-using-class (name (class null) &rest args &key)
337 (multiple-value-bind (meta initargs)
338 (ensure-class-values class args)
339 (setf class (apply #'make-instance meta :name name initargs)
340 (find-class name) class)
343 (defmethod ensure-class-using-class (name (class pcl-class) &rest args &key)
344 (multiple-value-bind (meta initargs)
345 (ensure-class-values class args)
346 (unless (eq (class-of class) meta) (change-class class meta))
347 (apply #'reinitialize-instance class initargs)
348 (setf (find-class name) class)
351 (defmethod class-predicate-name ((class t))
356 ((not (legal-class-name-p s))
357 (error "~S is not a class or a legal class name." s))
359 (or (find-class s nil)
361 (make-instance 'forward-referenced-class
364 (defun ensure-class-values (class args)
365 (let* ((initargs (copy-list args))
366 (unsupplied (list 1))
367 (supplied-meta (getf initargs :metaclass unsupplied))
368 (supplied-supers (getf initargs :direct-superclasses unsupplied))
369 (supplied-slots (getf initargs :direct-slots unsupplied))
371 (cond ((neq supplied-meta unsupplied)
372 (find-class supplied-meta))
374 (forward-referenced-class-p class))
375 *the-class-standard-class*)
378 ;; KLUDGE: It seemed to me initially that there ought to be a way
379 ;; of collecting all the erroneous problems in one go, rather than
380 ;; this way of solving the problem of signalling the errors that
381 ;; we are required to, which stops at the first bogus input.
382 ;; However, after playing around a little, I couldn't find that
383 ;; way, so I've left it as is, but if someone does come up with a
384 ;; better way... -- CSR, 2002-09-08
385 (loop for (slot . more) on (getf initargs :direct-slots)
386 for slot-name = (getf slot :name)
387 if (some (lambda (s) (eq slot-name (getf s :name))) more)
388 ;; FIXME: It's quite possible that we ought to define an
389 ;; SB-INT:PROGRAM-ERROR function to signal these and other
390 ;; errors throughout the code base that are required to be
391 ;; of type PROGRAM-ERROR.
392 do (error 'simple-program-error
393 :format-control "More than one direct slot with name ~S."
394 :format-arguments (list slot-name))
396 do (loop for (option value . more) on slot by #'cddr
397 when (and (member option
399 :initform :documentation))
401 (getf more option unsupplied))))
402 do (error 'simple-program-error
403 :format-control "Duplicate slot option ~S for slot ~S."
404 :format-arguments (list option slot-name))))
405 (loop for (initarg . more) on (getf initargs :direct-default-initargs)
406 for name = (car initarg)
407 when (some (lambda (a) (eq (car a) name)) more)
408 do (error 'simple-program-error
409 :format-control "Duplicate initialization argument ~
410 name ~S in :default-initargs of class ~A."
411 :format-arguments (list name class)))
412 (loop (unless (remf initargs :metaclass) (return)))
413 (loop (unless (remf initargs :direct-superclasses) (return)))
414 (loop (unless (remf initargs :direct-slots) (return)))
416 (list* :direct-superclasses
417 (and (neq supplied-supers unsupplied)
418 (mapcar #'fix-super supplied-supers))
420 (and (neq supplied-slots unsupplied) supplied-slots)
424 (defmethod shared-initialize :after
427 &key (direct-superclasses nil direct-superclasses-p)
428 (direct-slots nil direct-slots-p)
429 (direct-default-initargs nil direct-default-initargs-p)
430 (predicate-name nil predicate-name-p))
431 (declare (ignore slot-names))
432 (cond (direct-superclasses-p
433 (setq direct-superclasses
434 (or direct-superclasses
435 (list (if (funcallable-standard-class-p class)
436 *the-class-funcallable-standard-object*
437 *the-class-standard-object*))))
438 (dolist (superclass direct-superclasses)
439 (unless (validate-superclass class superclass)
440 (error "The class ~S was specified as a~%
441 super-class of the class ~S;~%~
442 but the meta-classes ~S and~%~S are incompatible.~@
443 Define a method for ~S to avoid this error."
444 superclass class (class-of superclass) (class-of class)
445 'validate-superclass)))
446 (setf (slot-value class 'direct-superclasses) direct-superclasses))
448 (setq direct-superclasses (slot-value class 'direct-superclasses))))
451 (setf (slot-value class 'direct-slots)
452 (mapcar (lambda (pl) (make-direct-slotd class pl))
454 (slot-value class 'direct-slots)))
455 (if direct-default-initargs-p
456 (setf (plist-value class 'direct-default-initargs)
457 direct-default-initargs)
458 (setq direct-default-initargs
459 (plist-value class 'direct-default-initargs)))
460 (setf (plist-value class 'class-slot-cells)
462 (dolist (dslotd direct-slots)
463 (when (eq (slot-definition-allocation dslotd) class)
464 (let ((initfunction (slot-definition-initfunction dslotd)))
465 (push (cons (slot-definition-name dslotd)
467 (funcall initfunction)
471 (setq predicate-name (if predicate-name-p
472 (setf (slot-value class 'predicate-name)
473 (car predicate-name))
474 (or (slot-value class 'predicate-name)
475 (setf (slot-value class 'predicate-name)
476 (make-class-predicate-name (class-name
478 (add-direct-subclasses class direct-superclasses)
479 (update-class class nil)
480 (make-class-predicate class predicate-name)
481 (add-slot-accessors class direct-slots))
483 (defmethod shared-initialize :before ((class class) slot-names &key name)
484 (declare (ignore slot-names name))
485 ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
486 ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
487 (setf (slot-value class 'type) `(class ,class))
488 (setf (slot-value class 'class-eq-specializer)
489 (make-instance 'class-eq-specializer :class class)))
491 (defmethod reinitialize-instance :before ((class slot-class) &key)
492 (remove-direct-subclasses class (class-direct-superclasses class))
493 (remove-slot-accessors class (class-direct-slots class)))
495 (defmethod reinitialize-instance :after ((class slot-class)
498 (map-dependents class
500 (apply #'update-dependent class dependent initargs))))
502 (defmethod shared-initialize :after ((slotd standard-slot-definition)
504 (declare (ignore slot-names))
505 (with-slots (allocation class)
507 (setq allocation (if (eq allocation :class) class allocation))))
509 (defmethod shared-initialize :after ((slotd structure-slot-definition)
511 &key (allocation :instance))
512 (declare (ignore slot-names))
513 (unless (eq allocation :instance)
514 (error "Structure slots must have :INSTANCE allocation.")))
516 (defun make-structure-class-defstruct-form (name direct-slots include)
517 (let* ((conc-name (intern (format nil "~S structure class " name)))
518 (constructor (intern (format nil "~Aconstructor" conc-name)))
519 (defstruct `(defstruct (,name
521 `((:include ,(class-name include))))
523 (:conc-name ,conc-name)
524 (:constructor ,constructor ())
526 ,@(mapcar (lambda (slot)
527 `(,(slot-definition-name slot)
530 (reader-names (mapcar (lambda (slotd)
534 (slot-definition-name
537 (writer-names (mapcar (lambda (slotd)
541 (slot-definition-name
545 (mapcar (lambda (slotd reader-name)
547 (slot-definition-defstruct-accessor-symbol
549 `(defun ,reader-name (obj)
550 (declare (type ,name obj))
552 direct-slots reader-names))
554 (mapcar (lambda (slotd writer-name)
556 (slot-definition-defstruct-accessor-symbol
558 `(defun ,writer-name (nv obj)
559 (declare (type ,name obj))
560 (setf (,accessor obj) nv))))
561 direct-slots writer-names))
565 ,@readers-init ,@writers-init
567 (values defstruct-form constructor reader-names writer-names)))
569 (defmethod shared-initialize :after
570 ((class structure-class)
572 &key (direct-superclasses nil direct-superclasses-p)
573 (direct-slots nil direct-slots-p)
574 direct-default-initargs
575 (predicate-name nil predicate-name-p))
576 (declare (ignore slot-names direct-default-initargs))
577 (if direct-superclasses-p
578 (setf (slot-value class 'direct-superclasses)
579 (or direct-superclasses
580 (setq direct-superclasses
581 (and (not (eq (class-name class) 'structure-object))
582 (list *the-class-structure-object*)))))
583 (setq direct-superclasses (slot-value class 'direct-superclasses)))
584 (let* ((name (class-name class))
585 (from-defclass-p (slot-value class 'from-defclass-p))
586 (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
588 (setf (slot-value class 'direct-slots)
592 (let* ((slot-name (getf pl :name))
595 "~S structure class ~A"
597 (accessor (intern acc-name)))
598 (setq pl (list* :defstruct-accessor-symbol
600 (make-direct-slotd class pl))
602 (setq direct-slots (slot-value class 'direct-slots)))
604 (let ((include (car (slot-value class 'direct-superclasses))))
605 (multiple-value-bind (defstruct-form constructor reader-names writer-names)
606 (make-structure-class-defstruct-form name direct-slots include)
607 (unless (structure-type-p name) (eval defstruct-form))
608 (mapc (lambda (dslotd reader-name writer-name)
609 (let* ((reader (gdefinition reader-name))
610 (writer (when (gboundp writer-name)
611 (gdefinition writer-name))))
612 (setf (slot-value dslotd 'internal-reader-function)
614 (setf (slot-value dslotd 'internal-writer-function)
616 direct-slots reader-names writer-names)
617 (setf (slot-value class 'defstruct-form) defstruct-form)
618 (setf (slot-value class 'defstruct-constructor) constructor))))
619 (add-direct-subclasses class direct-superclasses)
620 (setf (slot-value class 'class-precedence-list)
621 (compute-class-precedence-list class))
622 (setf (slot-value class 'slots) (compute-slots class))
623 (let ((lclass (cl:find-class (class-name class))))
624 (setf (sb-kernel:class-pcl-class lclass) class)
625 (setf (slot-value class 'wrapper) (sb-kernel:class-layout lclass)))
626 (update-pv-table-cache-info class)
627 (setq predicate-name (if predicate-name-p
628 (setf (slot-value class 'predicate-name)
629 (car predicate-name))
630 (or (slot-value class 'predicate-name)
631 (setf (slot-value class 'predicate-name)
632 (make-class-predicate-name
633 (class-name class))))))
634 (make-class-predicate class predicate-name)
635 (add-slot-accessors class direct-slots)))
637 (defmethod direct-slot-definition-class ((class structure-class) initargs)
638 (declare (ignore initargs))
639 (find-class 'structure-direct-slot-definition))
641 (defmethod finalize-inheritance ((class structure-class))
642 nil) ; always finalized
644 (defun add-slot-accessors (class dslotds)
645 (fix-slot-accessors class dslotds 'add))
647 (defun remove-slot-accessors (class dslotds)
648 (fix-slot-accessors class dslotds 'remove))
650 (defun fix-slot-accessors (class dslotds add/remove)
651 (flet ((fix (gfspec name r/w)
652 (let ((gf (ensure-generic-function gfspec)))
654 (r (if (eq add/remove 'add)
655 (add-reader-method class gf name)
656 (remove-reader-method class gf)))
657 (w (if (eq add/remove 'add)
658 (add-writer-method class gf name)
659 (remove-writer-method class gf)))))))
660 (dolist (dslotd dslotds)
661 (let ((slot-name (slot-definition-name dslotd)))
662 (dolist (r (slot-definition-readers dslotd)) (fix r slot-name 'r))
663 (dolist (w (slot-definition-writers dslotd)) (fix w slot-name 'w))))))
665 (defun add-direct-subclasses (class supers)
666 (dolist (super supers)
667 (unless (memq class (class-direct-subclasses class))
668 (add-direct-subclass super class))))
670 (defun remove-direct-subclasses (class supers)
671 (let ((old (class-direct-superclasses class)))
672 (dolist (o (set-difference old supers))
673 (remove-direct-subclass o class))))
675 (defmethod finalize-inheritance ((class std-class))
676 (update-class class t))
678 (defun class-has-a-forward-referenced-superclass-p (class)
679 (or (forward-referenced-class-p class)
680 (some #'class-has-a-forward-referenced-superclass-p
681 (class-direct-superclasses class))))
683 ;;; This is called by :after shared-initialize whenever a class is initialized
684 ;;; or reinitialized. The class may or may not be finalized.
685 (defun update-class (class finalizep)
686 (when (or finalizep (class-finalized-p class)
687 (not (class-has-a-forward-referenced-superclass-p class)))
688 (update-cpl class (compute-class-precedence-list class))
689 (update-slots class (compute-slots class))
690 (update-gfs-of-class class)
691 (update-inits class (compute-default-initargs class))
692 (update-make-instance-function-table class))
694 (dolist (sub (class-direct-subclasses class)) (update-class sub nil))))
696 (defun update-cpl (class cpl)
697 (if (class-finalized-p class)
698 (unless (equal (class-precedence-list class) cpl)
699 ;; comment from the old CMU CL sources:
700 ;; Need to have the cpl setup before update-lisp-class-layout
701 ;; is called on CMU CL.
702 (setf (slot-value class 'class-precedence-list) cpl)
703 (force-cache-flushes class))
704 (setf (slot-value class 'class-precedence-list) cpl))
705 (update-class-can-precede-p cpl))
707 (defun update-class-can-precede-p (cpl)
709 (let ((first (car cpl)))
710 (dolist (c (cdr cpl))
711 (pushnew c (slot-value first 'can-precede-list))))
712 (update-class-can-precede-p (cdr cpl))))
714 (defun class-can-precede-p (class1 class2)
715 (member class2 (class-can-precede-list class1)))
717 (defun update-slots (class eslotds)
718 (let ((instance-slots ())
720 (dolist (eslotd eslotds)
721 (let ((alloc (slot-definition-allocation eslotd)))
722 (cond ((eq alloc :instance) (push eslotd instance-slots))
723 ((classp alloc) (push eslotd class-slots)))))
725 ;; If there is a change in the shape of the instances then the
726 ;; old class is now obsolete.
727 (let* ((nlayout (mapcar #'slot-definition-name
728 (sort instance-slots #'<
729 :key #'slot-definition-location)))
730 (nslots (length nlayout))
731 (nwrapper-class-slots (compute-class-slots class-slots))
732 (owrapper (class-wrapper class))
733 (olayout (and owrapper (wrapper-instance-slots-layout owrapper)))
734 (owrapper-class-slots (and owrapper (wrapper-class-slots owrapper)))
736 (cond ((null owrapper)
737 (make-wrapper nslots class))
738 ((and (equal nlayout olayout)
740 (loop for o in owrapper-class-slots
741 for n in nwrapper-class-slots
742 do (unless (eq (car o) (car n)) (return t)))))
745 ;; This will initialize the new wrapper to have the
746 ;; same state as the old wrapper. We will then have
747 ;; to change that. This may seem like wasted work
748 ;; (and it is), but the spec requires that we call
749 ;; MAKE-INSTANCES-OBSOLETE.
750 (make-instances-obsolete class)
751 (class-wrapper class)))))
753 (with-slots (wrapper slots) class
754 (update-lisp-class-layout class nwrapper)
756 (wrapper-instance-slots-layout nwrapper) nlayout
757 (wrapper-class-slots nwrapper) nwrapper-class-slots
758 (wrapper-no-of-instance-slots nwrapper) nslots
761 (unless (eq owrapper nwrapper)
762 (update-pv-table-cache-info class)))))
764 (defun compute-class-slots (eslotds)
766 (dolist (eslotd eslotds)
767 (push (assoc (slot-definition-name eslotd)
768 (class-slot-cells (slot-definition-allocation eslotd)))
772 (defun compute-layout (cpl instance-eslotds)
775 (dolist (eslotd instance-eslotds)
776 (when (eq (slot-definition-allocation eslotd) :instance)
777 (push (slot-definition-name eslotd) collect)))
780 (labels ((rwalk (tail)
783 (dolist (ss (class-slots (car tail)))
784 (let ((n (slot-definition-name ss)))
785 (when (member n names)
786 (setq order (cons n order)
787 names (remove n names))))))))
788 (rwalk (if (slot-boundp (car cpl) 'slots)
791 (reverse (append names order)))))
793 (defun update-gfs-of-class (class)
794 (when (and (class-finalized-p class)
795 (let ((cpl (class-precedence-list class)))
796 (or (member *the-class-slot-class* cpl)
797 (member *the-class-standard-effective-slot-definition*
799 (let ((gf-table (make-hash-table :test 'eq)))
800 (labels ((collect-gfs (class)
801 (dolist (gf (specializer-direct-generic-functions class))
802 (setf (gethash gf gf-table) t))
803 (mapc #'collect-gfs (class-direct-superclasses class))))
805 (maphash (lambda (gf ignore)
806 (declare (ignore ignore))
807 (update-gf-dfun class gf))
810 (defun update-inits (class inits)
811 (setf (plist-value class 'default-initargs) inits))
813 (defmethod compute-default-initargs ((class slot-class))
814 (let ((cpl (class-precedence-list class))
815 (direct (class-direct-default-initargs class)))
816 (labels ((walk (tail)
819 (let ((c (pop tail)))
820 (append (if (eq c class)
822 (class-direct-default-initargs c))
824 (let ((initargs (walk cpl)))
825 (delete-duplicates initargs :test #'eq :key #'car :from-end t)))))
827 ;;;; protocols for constructing direct and effective slot definitions
829 (defmethod direct-slot-definition-class ((class std-class) initargs)
830 (declare (ignore initargs))
831 (find-class 'standard-direct-slot-definition))
833 (defun make-direct-slotd (class initargs)
834 (let ((initargs (list* :class class initargs)))
835 (apply #'make-instance
836 (direct-slot-definition-class class initargs)
839 (defmethod compute-slots ((class std-class))
840 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
841 ;; for each different slot name we find in our superclasses. Each
842 ;; call receives the class and a list of the dslotds with that name.
843 ;; The list is in most-specific-first order.
844 (let ((name-dslotds-alist ()))
845 (dolist (c (class-precedence-list class))
846 (let ((dslotds (class-direct-slots c)))
848 (let* ((name (slot-definition-name d))
849 (entry (assq name name-dslotds-alist)))
852 (push (list name d) name-dslotds-alist))))))
853 (mapcar (lambda (direct)
854 (compute-effective-slot-definition class
855 (nreverse (cdr direct))))
856 name-dslotds-alist)))
858 (defmethod compute-slots :around ((class std-class))
859 (let ((eslotds (call-next-method))
860 (cpl (class-precedence-list class))
863 (dolist (eslotd eslotds)
864 (let ((alloc (slot-definition-allocation eslotd)))
865 (cond ((eq alloc :instance) (push eslotd instance-slots))
866 ((classp alloc) (push eslotd class-slots)))))
867 (let ((nlayout (compute-layout cpl instance-slots)))
868 (dolist (eslotd instance-slots)
869 (setf (slot-definition-location eslotd)
870 (position (slot-definition-name eslotd) nlayout))))
871 (dolist (eslotd class-slots)
872 (setf (slot-definition-location eslotd)
873 (assoc (slot-definition-name eslotd)
874 (class-slot-cells (slot-definition-allocation eslotd)))))
875 (mapc #'initialize-internal-slot-functions eslotds)
878 (defmethod compute-slots ((class structure-class))
879 (mapcan (lambda (superclass)
880 (mapcar (lambda (dslotd)
881 (compute-effective-slot-definition class
883 (class-direct-slots superclass)))
884 (reverse (slot-value class 'class-precedence-list))))
886 (defmethod compute-slots :around ((class structure-class))
887 (let ((eslotds (call-next-method)))
888 (mapc #'initialize-internal-slot-functions eslotds)
891 (defmethod compute-effective-slot-definition ((class slot-class) dslotds)
892 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
893 (class (effective-slot-definition-class class initargs)))
894 (apply #'make-instance class initargs)))
896 (defmethod effective-slot-definition-class ((class std-class) initargs)
897 (declare (ignore initargs))
898 (find-class 'standard-effective-slot-definition))
900 (defmethod effective-slot-definition-class ((class structure-class) initargs)
901 (declare (ignore initargs))
902 (find-class 'structure-effective-slot-definition))
904 (defmethod compute-effective-slot-definition-initargs
905 ((class slot-class) direct-slotds)
916 (dolist (slotd direct-slotds)
919 (setq name (slot-definition-name slotd)
922 (when (slot-definition-initfunction slotd)
923 (setq initform (slot-definition-initform slotd)
924 initfunction (slot-definition-initfunction slotd)
927 (setq allocation (slot-definition-allocation slotd)
929 (setq initargs (append (slot-definition-initargs slotd) initargs))
930 (let ((slotd-type (slot-definition-type slotd)))
931 (setq type (cond ((eq type t) slotd-type)
932 ((*subtypep type slotd-type) type)
933 (t `(and ,type ,slotd-type)))))))
936 :initfunction initfunction
938 :allocation allocation
942 (defmethod compute-effective-slot-definition-initargs :around
943 ((class structure-class) direct-slotds)
944 (let ((slotd (car direct-slotds)))
945 (list* :defstruct-accessor-symbol
946 (slot-definition-defstruct-accessor-symbol slotd)
947 :internal-reader-function
948 (slot-definition-internal-reader-function slotd)
949 :internal-writer-function
950 (slot-definition-internal-writer-function slotd)
951 (call-next-method))))
953 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
954 ;;; to make the method object. They have to use make-a-method which
955 ;;; is a specially bootstrapped mechanism for making standard methods.
956 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
957 (declare (ignore direct-slot initargs))
958 (find-class 'standard-reader-method))
960 (defmethod add-reader-method ((class slot-class) generic-function slot-name)
961 (add-method generic-function
962 (make-a-method 'standard-reader-method
964 (list (or (class-name class) 'object))
966 (make-reader-method-function class slot-name)
967 "automatically generated reader method"
970 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
971 (declare (ignore direct-slot initargs))
972 (find-class 'standard-writer-method))
974 (defmethod add-writer-method ((class slot-class) generic-function slot-name)
975 (add-method generic-function
976 (make-a-method 'standard-writer-method
978 (list 'new-value (or (class-name class) 'object))
979 (list *the-class-t* class)
980 (make-writer-method-function class slot-name)
981 "automatically generated writer method"
984 (defmethod add-boundp-method ((class slot-class) generic-function slot-name)
985 (add-method generic-function
986 (make-a-method 'standard-boundp-method
988 (list (or (class-name class) 'object))
990 (make-boundp-method-function class slot-name)
991 "automatically generated boundp method"
994 (defmethod remove-reader-method ((class slot-class) generic-function)
995 (let ((method (get-method generic-function () (list class) nil)))
996 (when method (remove-method generic-function method))))
998 (defmethod remove-writer-method ((class slot-class) generic-function)
1000 (get-method generic-function () (list *the-class-t* class) nil)))
1001 (when method (remove-method generic-function method))))
1003 (defmethod remove-boundp-method ((class slot-class) generic-function)
1004 (let ((method (get-method generic-function () (list class) nil)))
1005 (when method (remove-method generic-function method))))
1007 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITE-METHOD function are NOT
1008 ;;; part of the standard protocol. They are however useful, PCL makes
1009 ;;; use of them internally and documents them for PCL users.
1011 ;;; *** This needs work to make type testing by the writer functions which
1012 ;;; *** do type testing faster. The idea would be to have one constructor
1013 ;;; *** for each possible type test.
1015 ;;; *** There is a subtle bug here which is going to have to be fixed.
1016 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1017 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1018 ;;; *** defined for this metaclass a chance to run.
1020 (defmethod make-reader-method-function ((class slot-class) slot-name)
1021 (make-std-reader-method-function (class-name class) slot-name))
1023 (defmethod make-writer-method-function ((class slot-class) slot-name)
1024 (make-std-writer-method-function (class-name class) slot-name))
1026 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1027 (make-std-boundp-method-function (class-name class) slot-name))
1029 (defmethod compatible-meta-class-change-p (class proto-new-class)
1030 (eq (class-of class) (class-of proto-new-class)))
1032 (defmethod validate-superclass ((class class) (new-super class))
1033 (or (eq new-super *the-class-t*)
1034 (eq (class-of class) (class-of new-super))))
1036 (defmethod validate-superclass ((class standard-class) (new-super std-class))
1037 (let ((new-super-meta-class (class-of new-super)))
1038 (or (eq new-super-meta-class *the-class-std-class*)
1039 (eq (class-of class) new-super-meta-class))))
1041 (defun force-cache-flushes (class)
1042 (let* ((owrapper (class-wrapper class)))
1043 ;; We only need to do something if the state is still T. If the
1044 ;; state isn't T, it will be FLUSH or OBSOLETE, and both of those
1045 ;; will already be doing what we want. In particular, we must be
1046 ;; sure we never change an OBSOLETE into a FLUSH since OBSOLETE
1047 ;; means do what FLUSH does and then some.
1048 (unless (invalid-wrapper-p owrapper)
1049 (let ((nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1051 (setf (wrapper-instance-slots-layout nwrapper)
1052 (wrapper-instance-slots-layout owrapper))
1053 (setf (wrapper-class-slots nwrapper)
1054 (wrapper-class-slots owrapper))
1055 (sb-sys:without-interrupts
1056 (update-lisp-class-layout class nwrapper)
1057 (setf (slot-value class 'wrapper) nwrapper)
1058 (invalidate-wrapper owrapper :flush nwrapper))))))
1060 (defun flush-cache-trap (owrapper nwrapper instance)
1061 (declare (ignore owrapper))
1062 (set-wrapper instance nwrapper))
1064 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1065 ;;; the next access to the instance (as defined in 88-002R) to trap
1066 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1067 (defmethod make-instances-obsolete ((class std-class))
1068 (let* ((owrapper (class-wrapper class))
1069 (nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1071 (setf (wrapper-instance-slots-layout nwrapper)
1072 (wrapper-instance-slots-layout owrapper))
1073 (setf (wrapper-class-slots nwrapper)
1074 (wrapper-class-slots owrapper))
1075 (sb-sys:without-interrupts
1076 (update-lisp-class-layout class nwrapper)
1077 (setf (slot-value class 'wrapper) nwrapper)
1078 (invalidate-wrapper owrapper :obsolete nwrapper)
1081 (defmethod make-instances-obsolete ((class symbol))
1082 (make-instances-obsolete (find-class class)))
1084 ;;; OBSOLETE-INSTANCE-TRAP is the internal trap that is called when we
1085 ;;; see an obsolete instance. The times when it is called are:
1086 ;;; - when the instance is involved in method lookup
1087 ;;; - when attempting to access a slot of an instance
1089 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1090 ;;; instance access macros.
1092 ;;; Of course these times when it is called are an internal
1093 ;;; implementation detail of PCL and are not part of the documented
1094 ;;; description of when the obsolete instance update happens. The
1095 ;;; documented description is as it appears in 88-002R.
1097 ;;; This has to return the new wrapper, so it counts on all the
1098 ;;; methods on obsolete-instance-trap-internal to return the new
1099 ;;; wrapper. It also does a little internal error checking to make
1100 ;;; sure that the traps are only happening when they should, and that
1101 ;;; the trap methods are computing appropriate new wrappers.
1103 ;;; OBSOLETE-INSTANCE-TRAP might be called on structure instances
1104 ;;; after a structure is redefined. In most cases,
1105 ;;; OBSOLETE-INSTANCE-TRAP will not be able to fix the old instance,
1106 ;;; so it must signal an error. The hard part of this is that the
1107 ;;; error system and debugger might cause OBSOLETE-INSTANCE-TRAP to be
1108 ;;; called again, so in that case, we have to return some reasonable
1109 ;;; wrapper, instead.
1111 (defvar *in-obsolete-instance-trap* nil)
1112 (defvar *the-wrapper-of-structure-object*
1113 (class-wrapper (find-class 'structure-object)))
1115 (define-condition obsolete-structure (error)
1116 ((datum :reader obsolete-structure-datum :initarg :datum))
1118 (lambda (condition stream)
1119 ;; Don't try to print the structure, since it probably won't work.
1121 "~@<obsolete structure error for a structure of type ~2I~_~S~:>"
1122 (type-of (obsolete-structure-datum condition))))))
1124 (defun obsolete-instance-trap (owrapper nwrapper instance)
1125 (if (not (pcl-instance-p instance))
1126 (if *in-obsolete-instance-trap*
1127 *the-wrapper-of-structure-object*
1128 (let ((*in-obsolete-instance-trap* t))
1129 (error 'obsolete-structure :datum instance)))
1130 (let* ((class (wrapper-class* nwrapper))
1131 (copy (allocate-instance class)) ;??? allocate-instance ???
1132 (olayout (wrapper-instance-slots-layout owrapper))
1133 (nlayout (wrapper-instance-slots-layout nwrapper))
1134 (oslots (get-slots instance))
1135 (nslots (get-slots copy))
1136 (oclass-slots (wrapper-class-slots owrapper))
1140 ;; local --> local transfer
1141 ;; local --> shared discard
1142 ;; local --> -- discard
1143 ;; shared --> local transfer
1144 ;; shared --> shared discard
1145 ;; shared --> -- discard
1149 ;; Go through all the old local slots.
1151 (dolist (name olayout)
1152 (let ((npos (posq name nlayout)))
1154 (setf (clos-slots-ref nslots npos)
1155 (clos-slots-ref oslots opos))
1157 (push name discarded)
1158 (unless (eq (clos-slots-ref oslots opos) +slot-unbound+)
1159 (setf (getf plist name) (clos-slots-ref oslots opos))))))
1162 ;; Go through all the old shared slots.
1163 (dolist (oclass-slot-and-val oclass-slots)
1164 (let ((name (car oclass-slot-and-val))
1165 (val (cdr oclass-slot-and-val)))
1166 (let ((npos (posq name nlayout)))
1168 (setf (clos-slots-ref nslots npos) (cdr oclass-slot-and-val))
1169 (progn (push name discarded)
1170 (unless (eq val +slot-unbound+)
1171 (setf (getf plist name) val)))))))
1173 ;; Go through all the new local slots to compute the added slots.
1174 (dolist (nlocal nlayout)
1175 (unless (or (memq nlocal olayout)
1176 (assq nlocal oclass-slots))
1177 (push nlocal added)))
1179 (swap-wrappers-and-slots instance copy)
1181 (update-instance-for-redefined-class instance
1187 (defun change-class-internal (instance new-class initargs)
1188 (let* ((old-class (class-of instance))
1189 (copy (allocate-instance new-class))
1190 (new-wrapper (get-wrapper copy))
1191 (old-wrapper (class-wrapper old-class))
1192 (old-layout (wrapper-instance-slots-layout old-wrapper))
1193 (new-layout (wrapper-instance-slots-layout new-wrapper))
1194 (old-slots (get-slots instance))
1195 (new-slots (get-slots copy))
1196 (old-class-slots (wrapper-class-slots old-wrapper)))
1198 ;; "The values of local slots specified by both the class CTO and
1199 ;; CFROM are retained. If such a local slot was unbound, it
1200 ;; remains unbound."
1201 (let ((new-position 0))
1202 (dolist (new-slot new-layout)
1203 (let ((old-position (posq new-slot old-layout)))
1205 (setf (clos-slots-ref new-slots new-position)
1206 (clos-slots-ref old-slots old-position))))
1207 (incf new-position)))
1209 ;; "The values of slots specified as shared in the class CFROM and
1210 ;; as local in the class CTO are retained."
1211 (dolist (slot-and-val old-class-slots)
1212 (let ((position (posq (car slot-and-val) new-layout)))
1214 (setf (clos-slots-ref new-slots position) (cdr slot-and-val)))))
1216 ;; Make the copy point to the old instance's storage, and make the
1217 ;; old instance point to the new storage.
1218 (swap-wrappers-and-slots instance copy)
1220 (apply #'update-instance-for-different-class copy instance initargs)
1223 (defmethod change-class ((instance standard-object)
1224 (new-class standard-class)
1226 (change-class-internal instance new-class initargs))
1228 (defmethod change-class ((instance funcallable-standard-object)
1229 (new-class funcallable-standard-class)
1231 (change-class-internal instance new-class initargs))
1233 (defmethod change-class ((instance standard-object)
1234 (new-class funcallable-standard-class)
1236 (declare (ignore initargs))
1237 (error "You can't change the class of ~S to ~S~@
1238 because it isn't already an instance with metaclass ~S."
1239 instance new-class 'standard-class))
1241 (defmethod change-class ((instance funcallable-standard-object)
1242 (new-class standard-class)
1244 (declare (ignore initargs))
1245 (error "You can't change the class of ~S to ~S~@
1246 because it isn't already an instance with metaclass ~S."
1247 instance new-class 'funcallable-standard-class))
1249 (defmethod change-class ((instance t) (new-class-name symbol) &rest initargs)
1250 (apply #'change-class instance (find-class new-class-name) initargs))
1252 ;;;; The metaclass BUILT-IN-CLASS
1254 ;;;; This metaclass is something of a weird creature. By this point, all
1255 ;;;; instances of it which will exist have been created, and no instance
1256 ;;;; is ever created by calling MAKE-INSTANCE.
1258 ;;;; But, there are other parts of the protocol we must follow and those
1259 ;;;; definitions appear here.
1261 (defmethod shared-initialize :before
1262 ((class built-in-class) slot-names &rest initargs)
1263 (declare (ignore slot-names initargs))
1264 (error "attempt to initialize or reinitialize a built in class"))
1266 (defmethod class-direct-slots ((class built-in-class)) ())
1267 (defmethod class-slots ((class built-in-class)) ())
1268 (defmethod class-direct-default-initargs ((class built-in-class)) ())
1269 (defmethod class-default-initargs ((class built-in-class)) ())
1271 (defmethod validate-superclass ((c class) (s built-in-class))
1272 (or (eq s *the-class-t*)
1273 (eq s *the-class-stream*)))
1275 (defmethod validate-superclass ((c slot-class)
1276 (f forward-referenced-class))
1279 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1280 (pushnew dependent (plist-value metaobject 'dependents)))
1282 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1283 (setf (plist-value metaobject 'dependents)
1284 (delete dependent (plist-value metaobject 'dependents))))
1286 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1287 (dolist (dependent (plist-value metaobject 'dependents))
1288 (funcall function dependent)))