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 (dolist (type '(reader writer boundp))
77 (let* ((gf-name (ecase type
78 (reader 'slot-value-using-class)
79 (writer '(setf slot-value-using-class))
80 (boundp 'slot-boundp-using-class)))
81 (gf (gdefinition gf-name)))
82 (compute-slot-accessor-info slotd type gf)))))
84 ;;; CMUCL (Gerd PCL 2003-04-25) comment:
86 ;;; Compute an effective method for SLOT-VALUE-USING-CLASS, (SETF
87 ;;; SLOT-VALUE-USING-CLASS) or SLOT-BOUNDP-USING-CLASS for reading/
88 ;;; writing/testing effective slot SLOTD.
90 ;;; TYPE is one of the symbols READER, WRITER or BOUNDP, depending on
91 ;;; GF. Store the effective method in the effective slot definition
92 ;;; object itself; these GFs have special dispatch functions calling
93 ;;; effective methods directly retrieved from effective slot
94 ;;; definition objects, as an optimization.
96 ;;; FIXME: Change the function name to COMPUTE-SVUC-SLOTD-FUNCTION,
98 (defmethod compute-slot-accessor-info ((slotd effective-slot-definition)
100 (let* ((name (slot-value slotd 'name))
101 (class (slot-value slotd '%class))
102 (old-slotd (find-slot-definition class name))
103 (old-std-p (and old-slotd (slot-accessor-std-p old-slotd 'all))))
104 (multiple-value-bind (function std-p)
105 (if (eq *boot-state* 'complete)
106 (get-accessor-method-function gf type class slotd)
107 (get-optimized-std-accessor-method-function class slotd type))
108 (setf (slot-accessor-std-p slotd type) std-p)
109 (setf (slot-accessor-function slotd type) function))
110 (when (and old-slotd (not (eq old-std-p (slot-accessor-std-p slotd 'all))))
111 (push (cons class name) *pv-table-cache-update-info*))))
113 (defmethod slot-definition-allocation ((slotd structure-slot-definition))
116 ;;;; various class accessors that are a little more complicated than can be
117 ;;;; done with automatically generated reader methods
119 (defmethod class-prototype :before (class)
120 (unless (class-finalized-p class)
121 (error "~@<~S is not finalized.~:@>" class)))
123 ;;; KLUDGE: For some reason factoring the common body into a function
124 ;;; breaks PCL bootstrapping, so just generate it with a macrolet for
126 (macrolet ((def (class)
127 `(defmethod class-prototype ((class ,class))
128 (with-slots (prototype) class
130 (setf prototype (allocate-instance class)))))))
132 (def condition-class)
133 (def structure-class))
135 (defmethod class-direct-default-initargs ((class slot-class))
136 (plist-value class 'direct-default-initargs))
138 (defmethod class-default-initargs ((class slot-class))
139 (plist-value class 'default-initargs))
141 (defmethod class-slot-cells ((class std-class))
142 (plist-value class 'class-slot-cells))
143 (defmethod (setf class-slot-cells) (new-value (class std-class))
144 (setf (plist-value class 'class-slot-cells) new-value))
146 ;;;; class accessors that are even a little bit more complicated than those
147 ;;;; above. These have a protocol for updating them, we must implement that
150 ;;; Maintaining the direct subclasses backpointers. The update methods are
151 ;;; here, the values are read by an automatically generated reader method.
152 (defmethod add-direct-subclass ((class class) (subclass class))
153 (with-slots (direct-subclasses) class
154 (pushnew subclass direct-subclasses)
156 (defmethod remove-direct-subclass ((class class) (subclass class))
157 (with-slots (direct-subclasses) class
158 (setq direct-subclasses (remove subclass direct-subclasses))
161 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
163 ;;; There are four generic functions involved, each has one method for the
164 ;;; class case and another method for the damned EQL specializers. All of
165 ;;; these are specified methods and appear in their specified place in the
168 ;;; ADD-DIRECT-METHOD
169 ;;; REMOVE-DIRECT-METHOD
170 ;;; SPECIALIZER-DIRECT-METHODS
171 ;;; SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
173 ;;; In each case, we maintain one value which is a cons. The car is the list
174 ;;; methods. The cdr is a list of the generic functions. The cdr is always
176 (defmethod add-direct-method ((specializer class) (method method))
177 (with-slots (direct-methods) specializer
178 (setf (car direct-methods) (adjoin method (car direct-methods)) ;PUSH
179 (cdr direct-methods) ()))
181 (defmethod remove-direct-method ((specializer class) (method method))
182 (with-slots (direct-methods) specializer
183 (setf (car direct-methods) (remove method (car direct-methods))
184 (cdr direct-methods) ()))
187 (defmethod specializer-direct-methods ((specializer class))
188 (with-slots (direct-methods) specializer
189 (car direct-methods)))
191 (defmethod specializer-direct-generic-functions ((specializer class))
192 (with-slots (direct-methods) specializer
193 (or (cdr direct-methods)
194 (setf (cdr direct-methods)
196 (dolist (m (car direct-methods))
197 ;; the old PCL code used COLLECTING-ONCE which used
198 ;; #'EQ to check for newness
199 (pushnew (method-generic-function m) collect :test #'eq))
200 (nreverse collect))))))
202 ;;; This hash table is used to store the direct methods and direct generic
203 ;;; functions of EQL specializers. Each value in the table is the cons.
204 (defvar *eql-specializer-methods* (make-hash-table :test 'eql))
205 (defvar *class-eq-specializer-methods* (make-hash-table :test 'eq))
207 (defmethod specializer-method-table ((specializer eql-specializer))
208 *eql-specializer-methods*)
210 (defmethod specializer-method-table ((specializer class-eq-specializer))
211 *class-eq-specializer-methods*)
213 (defmethod add-direct-method ((specializer specializer-with-object)
215 (let* ((object (specializer-object specializer))
216 (table (specializer-method-table specializer))
217 (entry (gethash object table)))
220 (setf (gethash object table)
222 (setf (car entry) (adjoin method (car entry))
226 (defmethod remove-direct-method ((specializer specializer-with-object)
228 (let* ((object (specializer-object specializer))
229 (entry (gethash object (specializer-method-table specializer))))
231 (setf (car entry) (remove method (car entry))
235 (defmethod specializer-direct-methods ((specializer specializer-with-object))
236 (car (gethash (specializer-object specializer)
237 (specializer-method-table specializer))))
239 (defmethod specializer-direct-generic-functions ((specializer
240 specializer-with-object))
241 (let* ((object (specializer-object specializer))
242 (entry (gethash object (specializer-method-table specializer))))
247 (dolist (m (car entry))
248 (pushnew (method-generic-function m) collect :test #'eq))
249 (nreverse collect)))))))
251 (defun map-specializers (function)
252 (map-all-classes (lambda (class)
253 (funcall function (class-eq-specializer class))
254 (funcall function class)))
255 (maphash (lambda (object methods)
256 (declare (ignore methods))
257 (intern-eql-specializer object))
258 *eql-specializer-methods*)
259 (maphash (lambda (object specl)
260 (declare (ignore object))
261 (funcall function specl))
262 *eql-specializer-table*)
265 (defun map-all-generic-functions (function)
266 (let ((all-generic-functions (make-hash-table :test 'eq)))
267 (map-specializers (lambda (specl)
268 (dolist (gf (specializer-direct-generic-functions
270 (unless (gethash gf all-generic-functions)
271 (setf (gethash gf all-generic-functions) t)
272 (funcall function gf))))))
275 (defmethod shared-initialize :after ((specl class-eq-specializer)
278 (declare (ignore slot-names))
279 (setf (slot-value specl '%type) `(class-eq ,(specializer-class specl))))
281 (defmethod shared-initialize :after ((specl eql-specializer) slot-names &key)
282 (declare (ignore slot-names))
283 (setf (slot-value specl '%type)
284 `(eql ,(specializer-object specl)))
285 (setf (info :type :translator specl)
286 (constantly (make-member-type :members (list (specializer-object specl))))))
288 (defun real-load-defclass (name metaclass-name supers slots other
289 readers writers slot-names source-location)
290 (with-single-package-locked-error (:symbol name "defining ~S as a class")
291 (%compiler-defclass name readers writers slot-names)
292 (let ((res (apply #'ensure-class name :metaclass metaclass-name
293 :direct-superclasses supers
295 :definition-source source-location
299 (setf (gdefinition 'load-defclass) #'real-load-defclass)
301 (defun ensure-class (name &rest args)
302 (apply #'ensure-class-using-class
303 (let ((class (find-class name nil)))
304 (when (and class (eq name (class-name class)))
305 ;; NAME is the proper name of CLASS, so redefine it
310 (defmethod ensure-class-using-class ((class null) name &rest args &key)
311 (multiple-value-bind (meta initargs)
312 (ensure-class-values class args)
314 (set-class-type-translation (class-prototype meta) name)
315 (setf class (apply #'make-instance meta :name name initargs))
316 (without-package-locks
317 (setf (find-class name) class))
318 (set-class-type-translation class name)
321 (defmethod ensure-class-using-class ((class pcl-class) name &rest args &key)
322 (multiple-value-bind (meta initargs)
323 (ensure-class-values class args)
324 (unless (eq (class-of class) meta)
325 (apply #'change-class class meta initargs))
326 (apply #'reinitialize-instance class initargs)
327 (without-package-locks
328 (setf (find-class name) class))
329 (set-class-type-translation class name)
334 ((not (legal-class-name-p s))
335 (error "~S is not a class or a legal class name." s))
337 (or (find-class s nil)
338 (ensure-class s :metaclass 'forward-referenced-class)))))
340 (defun ensure-class-values (class initargs)
341 (let (metaclass metaclassp reversed-plist)
342 (doplist (key val) initargs
343 (cond ((eq key :metaclass)
347 (when (eq key :direct-superclasses)
348 (setf val (mapcar #'fix-super val)))
349 (setf reversed-plist (list* val key reversed-plist)))))
350 (values (cond (metaclassp
351 (if (classp metaclass)
353 (find-class metaclass)))
354 ((or (null class) (forward-referenced-class-p class))
355 *the-class-standard-class*)
358 (nreverse reversed-plist))))
361 (defmethod shared-initialize :after
362 ((class std-class) slot-names &key
363 (direct-superclasses nil direct-superclasses-p)
364 (direct-slots nil direct-slots-p)
365 (direct-default-initargs nil direct-default-initargs-p))
366 (cond (direct-superclasses-p
367 (setq direct-superclasses
368 (or direct-superclasses
369 (list (if (funcallable-standard-class-p class)
370 *the-class-funcallable-standard-object*
371 *the-class-standard-object*))))
372 (dolist (superclass direct-superclasses)
373 (unless (validate-superclass class superclass)
374 (error "~@<The class ~S was specified as a ~
375 super-class of the class ~S, ~
376 but the meta-classes ~S and ~S are incompatible. ~
377 Define a method for ~S to avoid this error.~@:>"
378 superclass class (class-of superclass) (class-of class)
379 'validate-superclass)))
380 (setf (slot-value class 'direct-superclasses) direct-superclasses))
382 (setq direct-superclasses (slot-value class 'direct-superclasses))))
385 (setf (slot-value class 'direct-slots)
386 (mapcar (lambda (pl) (make-direct-slotd class pl))
388 (slot-value class 'direct-slots)))
389 (if direct-default-initargs-p
390 (setf (plist-value class 'direct-default-initargs)
391 direct-default-initargs)
392 (setq direct-default-initargs
393 (plist-value class 'direct-default-initargs)))
394 (setf (plist-value class 'class-slot-cells)
395 (let ((old-class-slot-cells (plist-value class 'class-slot-cells))
397 (dolist (dslotd direct-slots)
398 (when (eq :class (slot-definition-allocation dslotd))
400 (let* ((name (slot-definition-name dslotd))
401 (old (assoc name old-class-slot-cells)))
404 (member name slot-names))
405 (let* ((initfunction (slot-definition-initfunction dslotd))
406 (value (if initfunction
407 (funcall initfunction)
409 (push (cons name value) collect))
410 (push old collect)))))
412 (add-direct-subclasses class direct-superclasses)
413 (update-class class nil)
414 (do* ((slots (slot-value class 'slots) (cdr slots))
416 ((null slots) (when dupes
418 ;; FIXME: the indentation request ("~4I")
419 ;; below appears not to do anything. Finding
420 ;; out why would be nice. -- CSR, 2003-04-24
421 "~@<slot names with the same SYMBOL-NAME but ~
422 different SYMBOL-PACKAGE (possible package problem) ~
423 for class ~S:~@:_~4I~<~@{~S~^~:@_~}~:>~@:>"
426 (let* ((slot (car slots))
427 (oslots (remove (slot-definition-name slot) (cdr slots)
428 :test #'string/= :key #'slot-definition-name)))
430 (pushnew (cons (slot-definition-name slot)
431 (mapcar #'slot-definition-name oslots))
433 :test #'string= :key #'car))))
434 (add-slot-accessors class direct-slots)
435 (make-preliminary-layout class))
437 (defmethod shared-initialize :after ((class forward-referenced-class)
438 slot-names &key &allow-other-keys)
439 (declare (ignore slot-names))
440 (make-preliminary-layout class))
442 (defvar *allow-forward-referenced-classes-in-cpl-p* nil)
444 ;;; Give CLASS a preliminary layout if it doesn't have one already, to
445 ;;; make it known to the type system.
446 (defun make-preliminary-layout (class)
447 (flet ((compute-preliminary-cpl (root)
448 (let ((*allow-forward-referenced-classes-in-cpl-p* t))
449 (compute-class-precedence-list root))))
450 (without-package-locks
451 (unless (class-finalized-p class)
452 (let ((name (class-name class)))
453 ;; KLUDGE: This is fairly horrible. We need to make a
454 ;; full-fledged CLASSOID here, not just tell the compiler that
455 ;; some class is forthcoming, because there are legitimate
456 ;; questions one can ask of the type system, implemented in
457 ;; terms of CLASSOIDs, involving forward-referenced classes. So.
458 (let ((classoid (or (let ((layout (slot-value class 'wrapper)))
459 (when layout (layout-classoid layout)))
461 (find-classoid name nil)
462 (make-standard-classoid
463 :name (if (symbolp name) name nil))))
464 (layout (make-wrapper 0 class)))
465 (setf (layout-classoid layout) classoid)
466 (setf (classoid-pcl-class classoid) class)
467 (setf (slot-value class 'wrapper) layout)
468 (let ((cpl (compute-preliminary-cpl class)))
469 (setf (layout-inherits layout)
470 (order-layout-inherits
471 (map 'simple-vector #'class-wrapper
472 (reverse (rest cpl))))))
473 (register-layout layout :invalidate t)
474 (setf (classoid-layout classoid) layout))))
475 (mapc #'make-preliminary-layout (class-direct-subclasses class)))))
478 (defmethod shared-initialize :before ((class class) slot-names &key name)
479 (declare (ignore slot-names name))
480 ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
481 ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
482 (setf (slot-value class '%type) `(class ,class))
483 (setf (slot-value class 'class-eq-specializer)
484 (make-instance 'class-eq-specializer :class class)))
486 (defmethod reinitialize-instance :before ((class slot-class) &key direct-superclasses)
487 (dolist (old-super (set-difference (class-direct-superclasses class) direct-superclasses))
488 (remove-direct-subclass old-super class))
489 (remove-slot-accessors class (class-direct-slots class)))
491 (defmethod reinitialize-instance :after ((class slot-class)
494 (map-dependents class
496 (apply #'update-dependent class dependent initargs))))
498 (defmethod reinitialize-instance :after ((class condition-class) &key)
499 (let* ((name (class-name class))
500 (classoid (find-classoid name))
501 (slots (condition-classoid-slots classoid)))
502 ;; to balance the REMOVE-SLOT-ACCESSORS call in
503 ;; REINITIALIZE-INSTANCE :BEFORE (SLOT-CLASS).
505 (let ((slot-name (condition-slot-name slot)))
506 (dolist (reader (condition-slot-readers slot))
507 ;; FIXME: see comment in SHARED-INITIALIZE :AFTER
508 ;; (CONDITION-CLASS T), below. -- CSR, 2005-11-18
509 (sb-kernel::install-condition-slot-reader reader name slot-name))
510 (dolist (writer (condition-slot-writers slot))
511 (sb-kernel::install-condition-slot-writer writer name slot-name))))))
513 (defmethod shared-initialize :after ((class condition-class) slot-names
514 &key direct-slots direct-superclasses)
515 (declare (ignore slot-names))
516 (let ((classoid (find-classoid (class-name class))))
517 (with-slots (wrapper %class-precedence-list cpl-available-p
518 prototype (direct-supers direct-superclasses))
520 (setf (slot-value class 'direct-slots)
521 (mapcar (lambda (pl) (make-direct-slotd class pl))
523 (setf (slot-value class 'finalized-p) t)
524 (setf (classoid-pcl-class classoid) class)
525 (setq direct-supers direct-superclasses)
526 (setq wrapper (classoid-layout classoid))
527 (setq %class-precedence-list (compute-class-precedence-list class))
528 (setq cpl-available-p t)
529 (add-direct-subclasses class direct-superclasses)
530 (setf (slot-value class 'slots) (compute-slots class))))
531 ;; Comment from Gerd's PCL, 2003-05-15:
533 ;; We don't ADD-SLOT-ACCESSORS here because we don't want to
534 ;; override condition accessors with generic functions. We do this
537 ;; ??? What does the above comment mean and why is it a good idea?
538 ;; CMUCL (which still as of 2005-11-18 uses this code and has this
539 ;; comment) loses slot information in its condition classes:
540 ;; DIRECT-SLOTS is always NIL. We have the right information, so we
541 ;; remove slot accessors but never put them back. I've added a
542 ;; REINITIALIZE-INSTANCE :AFTER (CONDITION-CLASS) method, but what
543 ;; was meant to happen? -- CSR, 2005-11-18
544 (update-pv-table-cache-info class))
546 (defmethod direct-slot-definition-class ((class condition-class)
548 (declare (ignore initargs))
549 (find-class 'condition-direct-slot-definition))
551 (defmethod effective-slot-definition-class ((class condition-class)
553 (declare (ignore initargs))
554 (find-class 'condition-effective-slot-definition))
556 (defmethod finalize-inheritance ((class condition-class))
557 (aver (slot-value class 'finalized-p))
560 (defmethod compute-effective-slot-definition
561 ((class condition-class) slot-name dslotds)
562 (let ((slotd (call-next-method)))
563 (setf (slot-definition-reader-function slotd)
565 (handler-case (condition-reader-function x slot-name)
566 ;; FIXME: FIND-SLOT-DEFAULT throws an error if the slot
567 ;; is unbound; maybe it should be a CELL-ERROR of some
569 (error () (values (slot-unbound class x slot-name))))))
570 (setf (slot-definition-writer-function slotd)
572 (condition-writer-function x v slot-name)))
573 (setf (slot-definition-boundp-function slotd)
575 (multiple-value-bind (v c)
576 (ignore-errors (condition-reader-function x slot-name))
581 (defmethod compute-slots ((class condition-class))
582 (mapcan (lambda (superclass)
583 (mapcar (lambda (dslotd)
584 (compute-effective-slot-definition
585 class (slot-definition-name dslotd) (list dslotd)))
586 (class-direct-slots superclass)))
587 (reverse (slot-value class '%class-precedence-list))))
589 (defmethod compute-slots :around ((class condition-class))
590 (let ((eslotds (call-next-method)))
591 (mapc #'initialize-internal-slot-functions eslotds)
594 (defmethod shared-initialize :after
595 ((slotd structure-slot-definition) slot-names &key
596 (allocation :instance) allocation-class)
597 (declare (ignore slot-names allocation-class))
598 (unless (eq allocation :instance)
599 (error "Structure slots must have :INSTANCE allocation.")))
601 (defun make-structure-class-defstruct-form (name direct-slots include)
602 (let* ((conc-name (format-symbol *package* "~S structure class " name))
603 (constructor (format-symbol *package* "~Aconstructor" conc-name))
604 (defstruct `(defstruct (,name
606 `((:include ,(class-name include))))
608 (:conc-name ,conc-name)
609 (:constructor ,constructor ())
611 ,@(mapcar (lambda (slot)
612 `(,(slot-definition-name slot)
615 (reader-names (mapcar (lambda (slotd)
616 (list 'slot-accessor name
617 (slot-definition-name slotd)
620 (writer-names (mapcar (lambda (slotd)
621 (list 'slot-accessor name
622 (slot-definition-name slotd)
626 (mapcar (lambda (slotd reader-name)
628 (slot-definition-defstruct-accessor-symbol
630 `(defun ,reader-name (obj)
631 (declare (type ,name obj))
633 direct-slots reader-names))
635 (mapcar (lambda (slotd writer-name)
637 (slot-definition-defstruct-accessor-symbol
639 `(defun ,writer-name (nv obj)
640 (declare (type ,name obj))
641 (setf (,accessor obj) nv))))
642 direct-slots writer-names))
646 ,@readers-init ,@writers-init
648 (values defstruct-form constructor reader-names writer-names)))
650 (defun make-defstruct-allocation-function (class)
651 (let ((dd (get-structure-dd (class-name class))))
653 (sb-kernel::%make-instance-with-layout
654 (sb-kernel::compiler-layout-or-lose (dd-name dd))))))
656 (defmethod shared-initialize :after
657 ((class structure-class) slot-names &key
658 (direct-superclasses nil direct-superclasses-p)
659 (direct-slots nil direct-slots-p)
660 direct-default-initargs)
661 (declare (ignore slot-names direct-default-initargs))
662 (if direct-superclasses-p
663 (setf (slot-value class 'direct-superclasses)
664 (or direct-superclasses
665 (setq direct-superclasses
666 (and (not (eq (class-name class) 'structure-object))
667 (list *the-class-structure-object*)))))
668 (setq direct-superclasses (slot-value class 'direct-superclasses)))
669 (let* ((name (class-name class))
670 (from-defclass-p (slot-value class 'from-defclass-p))
671 (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
673 (setf (slot-value class 'direct-slots)
677 (let* ((slot-name (getf pl :name))
679 (format-symbol *package*
680 "~S structure class ~A"
682 (setq pl (list* :defstruct-accessor-symbol
684 (make-direct-slotd class pl))
686 (setq direct-slots (slot-value class 'direct-slots)))
688 (let ((include (car (slot-value class 'direct-superclasses))))
689 (multiple-value-bind (defstruct-form constructor reader-names writer-names)
690 (make-structure-class-defstruct-form name direct-slots include)
691 (unless (structure-type-p name) (eval defstruct-form))
692 (mapc (lambda (dslotd reader-name writer-name)
693 (let* ((reader (gdefinition reader-name))
694 (writer (when (fboundp writer-name)
695 (gdefinition writer-name))))
696 (setf (slot-value dslotd 'internal-reader-function)
698 (setf (slot-value dslotd 'internal-writer-function)
700 direct-slots reader-names writer-names)
701 (setf (slot-value class 'defstruct-form) defstruct-form)
702 (setf (slot-value class 'defstruct-constructor) constructor)))
703 (setf (slot-value class 'defstruct-constructor)
704 (make-defstruct-allocation-function class)))
705 (add-direct-subclasses class direct-superclasses)
706 (setf (slot-value class '%class-precedence-list)
707 (compute-class-precedence-list class))
708 (setf (slot-value class 'cpl-available-p) t)
709 (setf (slot-value class 'slots) (compute-slots class))
710 (let ((lclass (find-classoid (class-name class))))
711 (setf (classoid-pcl-class lclass) class)
712 (setf (slot-value class 'wrapper) (classoid-layout lclass)))
713 (setf (slot-value class 'finalized-p) t)
714 (update-pv-table-cache-info class)
715 (add-slot-accessors class direct-slots)))
717 (defmethod direct-slot-definition-class ((class structure-class) &rest initargs)
718 (declare (ignore initargs))
719 (find-class 'structure-direct-slot-definition))
721 (defmethod finalize-inheritance ((class structure-class))
722 nil) ; always finalized
724 (defun add-slot-accessors (class dslotds)
725 (fix-slot-accessors class dslotds 'add))
727 (defun remove-slot-accessors (class dslotds)
728 (fix-slot-accessors class dslotds 'remove))
730 (defun fix-slot-accessors (class dslotds add/remove)
731 (flet ((fix (gfspec name r/w)
732 (let ((gf (cond ((eq add/remove 'add)
734 (without-package-locks
735 (ensure-generic-function gfspec))
736 (ensure-generic-function
737 gfspec :lambda-list (case r/w
739 (w '(new-value object))))))
740 ((generic-function-p (and (fboundp gfspec)
741 (fdefinition gfspec)))
742 (without-package-locks
743 (ensure-generic-function gfspec))))))
746 (r (if (eq add/remove 'add)
747 (add-reader-method class gf name)
748 (remove-reader-method class gf)))
749 (w (if (eq add/remove 'add)
750 (add-writer-method class gf name)
751 (remove-writer-method class gf))))))))
752 (dolist (dslotd dslotds)
753 (let ((slot-name (slot-definition-name dslotd)))
754 (dolist (r (slot-definition-readers dslotd))
755 (fix r slot-name 'r))
756 (dolist (w (slot-definition-writers dslotd))
757 (fix w slot-name 'w))))))
759 (defun add-direct-subclasses (class supers)
760 (dolist (super supers)
761 (unless (memq class (class-direct-subclasses class))
762 (add-direct-subclass super class))))
764 (defmethod finalize-inheritance ((class std-class))
765 (update-class class t))
767 (defmethod finalize-inheritance ((class forward-referenced-class))
768 ;; FIXME: should we not be thinking a bit about what kinds of error
769 ;; we're throwing? Maybe we need a clos-error type to mix in? Or
770 ;; possibly a forward-referenced-class-error, though that's
771 ;; difficult given e.g. class precedence list calculations...
773 "~@<FINALIZE-INHERITANCE was called on a forward referenced class:~
778 (defun class-has-a-forward-referenced-superclass-p (class)
779 (or (forward-referenced-class-p class)
780 (some #'class-has-a-forward-referenced-superclass-p
781 (class-direct-superclasses class))))
783 ;;; This is called by :after shared-initialize whenever a class is initialized
784 ;;; or reinitialized. The class may or may not be finalized.
785 (defun update-class (class finalizep)
786 (without-package-locks
787 (when (or finalizep (class-finalized-p class))
788 (update-cpl class (compute-class-precedence-list class))
789 ;; This invocation of UPDATE-SLOTS, in practice, finalizes the
791 (update-slots class (compute-slots class))
792 (update-gfs-of-class class)
793 (update-initargs class (compute-default-initargs class))
794 (update-ctors 'finalize-inheritance :class class))
795 (dolist (sub (class-direct-subclasses class))
796 (update-class sub nil))))
798 (define-condition cpl-protocol-violation (reference-condition error)
799 ((class :initarg :class :reader cpl-protocol-violation-class)
800 (cpl :initarg :cpl :reader cpl-protocol-violation-cpl))
801 (:default-initargs :references (list '(:sbcl :node "Metaobject Protocol")))
804 (format s "~@<Protocol violation: the ~S class ~S ~
805 ~:[has~;does not have~] the class ~S in its ~
806 class precedence list: ~S.~@:>"
807 (class-name (class-of (cpl-protocol-violation-class c)))
808 (cpl-protocol-violation-class c)
809 (eq (class-of (cpl-protocol-violation-class c))
810 *the-class-funcallable-standard-class*)
811 (find-class 'function)
812 (cpl-protocol-violation-cpl c)))))
814 (defun update-cpl (class cpl)
815 (when (eq (class-of class) *the-class-standard-class*)
816 (when (find (find-class 'function) cpl)
817 (error 'cpl-protocol-violation :class class :cpl cpl)))
818 (when (eq (class-of class) *the-class-funcallable-standard-class*)
819 (unless (find (find-class 'function) cpl)
820 (error 'cpl-protocol-violation :class class :cpl cpl)))
821 (if (class-finalized-p class)
822 (unless (and (equal (class-precedence-list class) cpl)
824 (when (position :class (class-direct-slots c)
825 :key #'slot-definition-allocation)
827 ;; comment from the old CMU CL sources:
828 ;; Need to have the cpl setup before update-lisp-class-layout
829 ;; is called on CMU CL.
830 (setf (slot-value class '%class-precedence-list) cpl)
831 (setf (slot-value class 'cpl-available-p) t)
832 (force-cache-flushes class))
834 (setf (slot-value class '%class-precedence-list) cpl)
835 (setf (slot-value class 'cpl-available-p) t)))
836 (update-class-can-precede-p cpl))
838 (defun update-class-can-precede-p (cpl)
840 (let ((first (car cpl)))
841 (dolist (c (cdr cpl))
842 (pushnew c (slot-value first 'can-precede-list))))
843 (update-class-can-precede-p (cdr cpl))))
845 (defun class-can-precede-p (class1 class2)
846 (member class2 (class-can-precede-list class1)))
848 (defun update-slots (class eslotds)
849 (let ((instance-slots ())
851 (dolist (eslotd eslotds)
852 (let ((alloc (slot-definition-allocation eslotd)))
854 (:instance (push eslotd instance-slots))
855 (:class (push eslotd class-slots)))))
857 ;; If there is a change in the shape of the instances then the
858 ;; old class is now obsolete.
859 (let* ((nlayout (mapcar #'slot-definition-name
860 (sort instance-slots #'<
861 :key #'slot-definition-location)))
862 (nslots (length nlayout))
863 (nwrapper-class-slots (compute-class-slots class-slots))
864 (owrapper (when (class-finalized-p class)
865 (class-wrapper class)))
866 (olayout (when owrapper
867 (wrapper-instance-slots-layout owrapper)))
868 (owrapper-class-slots (and owrapper (wrapper-class-slots owrapper)))
870 (cond ((null owrapper)
871 (make-wrapper nslots class))
872 ((and (equal nlayout olayout)
874 (loop for o in owrapper-class-slots
875 for n in nwrapper-class-slots
876 do (unless (eq (car o) (car n)) (return t)))))
879 ;; This will initialize the new wrapper to have the
880 ;; same state as the old wrapper. We will then have
881 ;; to change that. This may seem like wasted work
882 ;; (and it is), but the spec requires that we call
883 ;; MAKE-INSTANCES-OBSOLETE.
884 (make-instances-obsolete class)
885 (class-wrapper class)))))
887 (with-slots (wrapper slots) class
888 (update-lisp-class-layout class nwrapper)
890 (wrapper-instance-slots-layout nwrapper) nlayout
891 (wrapper-class-slots nwrapper) nwrapper-class-slots
892 (wrapper-no-of-instance-slots nwrapper) nslots
894 (setf (slot-value class 'finalized-p) t)
895 (unless (eq owrapper nwrapper)
896 (update-pv-table-cache-info class)
897 (maybe-update-standard-class-locations class)))))
899 (defun compute-class-slots (eslotds)
901 (dolist (eslotd eslotds (nreverse collect))
902 (let ((cell (assoc (slot-definition-name eslotd)
904 (slot-definition-allocation-class eslotd)))))
906 (push cell collect)))))
908 (defun update-gfs-of-class (class)
909 (when (and (class-finalized-p class)
910 (let ((cpl (class-precedence-list class)))
911 (or (member *the-class-slot-class* cpl)
912 (member *the-class-standard-effective-slot-definition*
914 (let ((gf-table (make-hash-table :test 'eq)))
915 (labels ((collect-gfs (class)
916 (dolist (gf (specializer-direct-generic-functions class))
917 (setf (gethash gf gf-table) t))
918 (mapc #'collect-gfs (class-direct-superclasses class))))
920 (maphash (lambda (gf ignore)
921 (declare (ignore ignore))
922 (update-gf-dfun class gf))
925 (defun update-initargs (class inits)
926 (setf (plist-value class 'default-initargs) inits))
928 (defmethod compute-default-initargs ((class slot-class))
929 (let ((initargs (loop for c in (class-precedence-list class)
930 append (class-direct-default-initargs c))))
931 (delete-duplicates initargs :test #'eq :key #'car :from-end t)))
933 ;;;; protocols for constructing direct and effective slot definitions
935 (defmethod direct-slot-definition-class ((class std-class) &rest initargs)
936 (declare (ignore initargs))
937 (find-class 'standard-direct-slot-definition))
939 (defun make-direct-slotd (class initargs)
940 (apply #'make-instance
941 (apply #'direct-slot-definition-class class initargs)
945 ;;; I (CSR) am not sure, but I believe that the particular order of
946 ;;; slots is quite important: it is ideal to attempt to have a
947 ;;; constant slot location for the same notional slots as much as
948 ;;; possible, so that clever discriminating functions (ONE-INDEX et
949 ;;; al.) have a chance of working. The below at least walks through
950 ;;; the slots predictably, but maybe it would be good to compute some
951 ;;; kind of optimal slot layout by looking at locations of slots in
953 (defun std-compute-slots (class)
954 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
955 ;; for each different slot name we find in our superclasses. Each
956 ;; call receives the class and a list of the dslotds with that name.
957 ;; The list is in most-specific-first order.
958 (let ((name-dslotds-alist ()))
959 (dolist (c (reverse (class-precedence-list class)))
960 (dolist (slot (class-direct-slots c))
961 (let* ((name (slot-definition-name slot))
962 (entry (assq name name-dslotds-alist)))
964 (push slot (cdr entry))
965 (push (list name slot) name-dslotds-alist)))))
966 (mapcar (lambda (direct)
967 (compute-effective-slot-definition class
970 (nreverse name-dslotds-alist))))
972 (defmethod compute-slots ((class standard-class))
973 (std-compute-slots class))
974 (defmethod compute-slots ((class funcallable-standard-class))
975 (std-compute-slots class))
977 (defun std-compute-slots-around (class eslotds)
979 (dolist (eslotd eslotds eslotds)
980 (setf (slot-definition-location eslotd)
981 (case (slot-definition-allocation eslotd)
985 (let* ((name (slot-definition-name eslotd))
988 (slot-definition-allocation-class eslotd)
989 ;; we get here if the user adds an extra slot
991 (setf (slot-definition-allocation-class eslotd)
993 ;; which raises the question of what we should
994 ;; do if we find that said user has added a slot
995 ;; with the same name as another slot...
996 (cell (or (assq name (class-slot-cells from-class))
997 (let ((c (cons name +slot-unbound+)))
998 (push c (class-slot-cells from-class))
1001 (if (eq +slot-unbound+ (cdr cell))
1002 ;; We may have inherited an initfunction
1003 (let ((initfun (slot-definition-initfunction eslotd)))
1005 (rplacd cell (funcall initfun))
1008 (unless (slot-definition-class eslotd)
1009 (setf (slot-definition-class eslotd) class))
1010 (initialize-internal-slot-functions eslotd))))
1012 (defmethod compute-slots :around ((class standard-class))
1013 (let ((eslotds (call-next-method)))
1014 (std-compute-slots-around class eslotds)))
1015 (defmethod compute-slots :around ((class funcallable-standard-class))
1016 (let ((eslotds (call-next-method)))
1017 (std-compute-slots-around class eslotds)))
1019 (defmethod compute-slots ((class structure-class))
1020 (mapcan (lambda (superclass)
1021 (mapcar (lambda (dslotd)
1022 (compute-effective-slot-definition
1024 (slot-definition-name dslotd)
1026 (class-direct-slots superclass)))
1027 (reverse (slot-value class '%class-precedence-list))))
1029 (defmethod compute-slots :around ((class structure-class))
1030 (let ((eslotds (call-next-method)))
1031 (mapc #'initialize-internal-slot-functions eslotds)
1034 (defmethod compute-effective-slot-definition ((class slot-class) name dslotds)
1035 (declare (ignore name))
1036 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
1037 (class (apply #'effective-slot-definition-class class initargs)))
1038 (apply #'make-instance class initargs)))
1040 (defmethod effective-slot-definition-class ((class std-class) &rest initargs)
1041 (declare (ignore initargs))
1042 (find-class 'standard-effective-slot-definition))
1044 (defmethod effective-slot-definition-class ((class structure-class) &rest initargs)
1045 (declare (ignore initargs))
1046 (find-class 'structure-effective-slot-definition))
1048 (defmethod compute-effective-slot-definition-initargs
1049 ((class slot-class) direct-slotds)
1055 (allocation-class nil)
1058 (documentationp nil)
1063 (dolist (slotd direct-slotds)
1066 (setq name (slot-definition-name slotd)
1069 (when (slot-definition-initfunction slotd)
1070 (setq initform (slot-definition-initform slotd)
1071 initfunction (slot-definition-initfunction slotd)
1073 (unless documentationp
1074 (when (%slot-definition-documentation slotd)
1075 (setq documentation (%slot-definition-documentation slotd)
1078 (setq allocation (slot-definition-allocation slotd)
1079 allocation-class (slot-definition-class slotd)
1081 (setq initargs (append (slot-definition-initargs slotd) initargs))
1082 (let ((slotd-type (slot-definition-type slotd)))
1084 ((eq type t) slotd-type)
1085 ;; This pairwise type intersection is perhaps a
1086 ;; little inefficient and inelegant, but it's
1087 ;; unlikely to lie on the critical path. Shout
1088 ;; if I'm wrong. -- CSR, 2005-11-24
1090 (specifier-type `(and ,type ,slotd-type)))))))))
1093 :initfunction initfunction
1095 :allocation allocation
1096 :allocation-class allocation-class
1099 :documentation documentation)))
1101 (defmethod compute-effective-slot-definition-initargs :around
1102 ((class structure-class) direct-slotds)
1103 (let ((slotd (car direct-slotds)))
1104 (list* :defstruct-accessor-symbol
1105 (slot-definition-defstruct-accessor-symbol slotd)
1106 :internal-reader-function
1107 (slot-definition-internal-reader-function slotd)
1108 :internal-writer-function
1109 (slot-definition-internal-writer-function slotd)
1110 (call-next-method))))
1112 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
1113 ;;; to make the method object. They have to use make-a-method which
1114 ;;; is a specially bootstrapped mechanism for making standard methods.
1115 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
1116 (declare (ignore direct-slot initargs))
1117 (find-class 'standard-reader-method))
1119 (defmethod add-reader-method ((class slot-class) generic-function slot-name)
1120 (add-method generic-function
1121 (make-a-method 'standard-reader-method
1123 (list (or (class-name class) 'object))
1125 (make-reader-method-function class slot-name)
1126 "automatically generated reader method"
1129 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
1130 (declare (ignore direct-slot initargs))
1131 (find-class 'standard-writer-method))
1133 (defmethod add-writer-method ((class slot-class) generic-function slot-name)
1134 (add-method generic-function
1135 (make-a-method 'standard-writer-method
1137 (list 'new-value (or (class-name class) 'object))
1138 (list *the-class-t* class)
1139 (make-writer-method-function class slot-name)
1140 "automatically generated writer method"
1143 (defmethod add-boundp-method ((class slot-class) generic-function slot-name)
1144 (add-method generic-function
1145 (make-a-method 'standard-boundp-method
1147 (list (or (class-name class) 'object))
1149 (make-boundp-method-function class slot-name)
1150 "automatically generated boundp method"
1153 (defmethod remove-reader-method ((class slot-class) generic-function)
1154 (let ((method (get-method generic-function () (list class) nil)))
1155 (when method (remove-method generic-function method))))
1157 (defmethod remove-writer-method ((class slot-class) generic-function)
1159 (get-method generic-function () (list *the-class-t* class) nil)))
1160 (when method (remove-method generic-function method))))
1162 (defmethod remove-boundp-method ((class slot-class) generic-function)
1163 (let ((method (get-method generic-function () (list class) nil)))
1164 (when method (remove-method generic-function method))))
1166 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITE-METHOD function are NOT
1167 ;;; part of the standard protocol. They are however useful, PCL makes
1168 ;;; use of them internally and documents them for PCL users.
1170 ;;; *** This needs work to make type testing by the writer functions which
1171 ;;; *** do type testing faster. The idea would be to have one constructor
1172 ;;; *** for each possible type test.
1174 ;;; *** There is a subtle bug here which is going to have to be fixed.
1175 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1176 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1177 ;;; *** defined for this metaclass a chance to run.
1179 (defmethod make-reader-method-function ((class slot-class) slot-name)
1180 (make-std-reader-method-function (class-name class) slot-name))
1182 (defmethod make-writer-method-function ((class slot-class) slot-name)
1183 (make-std-writer-method-function (class-name class) slot-name))
1185 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1186 (make-std-boundp-method-function (class-name class) slot-name))
1188 (defmethod compatible-meta-class-change-p (class proto-new-class)
1189 (eq (class-of class) (class-of proto-new-class)))
1191 (defmethod validate-superclass ((class class) (superclass class))
1192 (or (eq superclass *the-class-t*)
1193 (eq (class-of class) (class-of superclass))
1194 (and (eq (class-of superclass) *the-class-standard-class*)
1195 (eq (class-of class) *the-class-funcallable-standard-class*))
1196 (and (eq (class-of superclass) *the-class-funcallable-standard-class*)
1197 (eq (class-of class) *the-class-standard-class*))))
1199 ;;; What this does depends on which of the four possible values of
1200 ;;; LAYOUT-INVALID the PCL wrapper has; the simplest case is when it
1201 ;;; is (:FLUSH <wrapper>) or (:OBSOLETE <wrapper>), when there is
1202 ;;; nothing to do, as the new wrapper has already been created. If
1203 ;;; LAYOUT-INVALID returns NIL, then we invalidate it (setting it to
1204 ;;; (:FLUSH <wrapper>); UPDATE-SLOTS later gets to choose whether or
1205 ;;; not to "upgrade" this to (:OBSOLETE <wrapper>).
1207 ;;; This leaves the case where LAYOUT-INVALID returns T, which happens
1208 ;;; when REGISTER-LAYOUT has invalidated a superclass of CLASS (which
1209 ;;; invalidated all the subclasses in SB-KERNEL land). Again, here we
1210 ;;; must flush the caches and allow UPDATE-SLOTS to decide whether to
1211 ;;; obsolete the wrapper.
1213 ;;; FIXME: either here or in INVALID-WRAPPER-P looks like a good place
1214 ;;; for (AVER (NOT (EQ (LAYOUT-INVALID OWRAPPER)
1215 ;;; :UNINITIALIZED)))
1217 ;;; Thanks to Gerd Moellmann for the explanation. -- CSR, 2002-10-29
1218 (defun force-cache-flushes (class)
1219 (let* ((owrapper (class-wrapper class)))
1220 ;; We only need to do something if the wrapper is still valid. If
1221 ;; the wrapper isn't valid, state will be FLUSH or OBSOLETE, and
1222 ;; both of those will already be doing what we want. In
1223 ;; particular, we must be sure we never change an OBSOLETE into a
1224 ;; FLUSH since OBSOLETE means do what FLUSH does and then some.
1225 (when (or (not (invalid-wrapper-p owrapper))
1226 ;; KLUDGE: despite the observations above, this remains
1227 ;; a violation of locality or what might be considered
1228 ;; good style. There has to be a better way! -- CSR,
1230 (eq (layout-invalid owrapper) t))
1231 (let ((nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1233 (setf (wrapper-instance-slots-layout nwrapper)
1234 (wrapper-instance-slots-layout owrapper))
1235 (setf (wrapper-class-slots nwrapper)
1236 (wrapper-class-slots owrapper))
1238 (update-lisp-class-layout class nwrapper)
1239 (setf (slot-value class 'wrapper) nwrapper)
1240 ;; Use :OBSOLETE instead of :FLUSH if any superclass has
1242 (if (find-if (lambda (x)
1243 (and (consp x) (eq :obsolete (car x))))
1244 (layout-inherits owrapper)
1245 :key #'layout-invalid)
1246 (invalidate-wrapper owrapper :obsolete nwrapper)
1247 (invalidate-wrapper owrapper :flush nwrapper)))))))
1249 (defun flush-cache-trap (owrapper nwrapper instance)
1250 (declare (ignore owrapper))
1251 (set-wrapper instance nwrapper))
1253 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1254 ;;; the next access to the instance (as defined in 88-002R) to trap
1255 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1256 (defmethod make-instances-obsolete ((class std-class))
1257 (let* ((owrapper (class-wrapper class))
1258 (nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1260 (unless (class-finalized-p class)
1261 (if (class-has-a-forward-referenced-superclass-p class)
1262 (return-from make-instances-obsolete class)
1263 (update-cpl class (compute-class-precedence-list class))))
1264 (setf (wrapper-instance-slots-layout nwrapper)
1265 (wrapper-instance-slots-layout owrapper))
1266 (setf (wrapper-class-slots nwrapper)
1267 (wrapper-class-slots owrapper))
1269 (update-lisp-class-layout class nwrapper)
1270 (setf (slot-value class 'wrapper) nwrapper)
1271 (invalidate-wrapper owrapper :obsolete nwrapper)
1274 (defmethod make-instances-obsolete ((class symbol))
1275 (make-instances-obsolete (find-class class))
1276 ;; ANSI wants the class name when called with a symbol.
1279 ;;; OBSOLETE-INSTANCE-TRAP is the internal trap that is called when we
1280 ;;; see an obsolete instance. The times when it is called are:
1281 ;;; - when the instance is involved in method lookup
1282 ;;; - when attempting to access a slot of an instance
1284 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1285 ;;; instance access macros.
1287 ;;; Of course these times when it is called are an internal
1288 ;;; implementation detail of PCL and are not part of the documented
1289 ;;; description of when the obsolete instance update happens. The
1290 ;;; documented description is as it appears in 88-002R.
1292 ;;; This has to return the new wrapper, so it counts on all the
1293 ;;; methods on obsolete-instance-trap-internal to return the new
1294 ;;; wrapper. It also does a little internal error checking to make
1295 ;;; sure that the traps are only happening when they should, and that
1296 ;;; the trap methods are computing appropriate new wrappers.
1298 ;;; OBSOLETE-INSTANCE-TRAP might be called on structure instances
1299 ;;; after a structure is redefined. In most cases,
1300 ;;; OBSOLETE-INSTANCE-TRAP will not be able to fix the old instance,
1301 ;;; so it must signal an error. The hard part of this is that the
1302 ;;; error system and debugger might cause OBSOLETE-INSTANCE-TRAP to be
1303 ;;; called again, so in that case, we have to return some reasonable
1304 ;;; wrapper, instead.
1306 (defvar *in-obsolete-instance-trap* nil)
1307 (defvar *the-wrapper-of-structure-object*
1308 (class-wrapper (find-class 'structure-object)))
1310 (define-condition obsolete-structure (error)
1311 ((datum :reader obsolete-structure-datum :initarg :datum))
1313 (lambda (condition stream)
1314 ;; Don't try to print the structure, since it probably won't work.
1316 "~@<obsolete structure error for a structure of type ~2I~_~S~:>"
1317 (type-of (obsolete-structure-datum condition))))))
1319 (defun obsolete-instance-trap (owrapper nwrapper instance)
1320 (if (not (pcl-instance-p instance))
1321 (if *in-obsolete-instance-trap*
1322 *the-wrapper-of-structure-object*
1323 (let ((*in-obsolete-instance-trap* t))
1324 (error 'obsolete-structure :datum instance)))
1325 (let* ((class (wrapper-class* nwrapper))
1326 (copy (allocate-instance class)) ;??? allocate-instance ???
1327 (olayout (wrapper-instance-slots-layout owrapper))
1328 (nlayout (wrapper-instance-slots-layout nwrapper))
1329 (oslots (get-slots instance))
1330 (nslots (get-slots copy))
1331 (oclass-slots (wrapper-class-slots owrapper))
1336 ;; local --> local transfer value
1337 ;; local --> shared discard value, discard slot
1338 ;; local --> -- discard slot
1339 ;; shared --> local transfer value
1340 ;; shared --> shared -- (cf SHARED-INITIALIZE :AFTER STD-CLASS)
1341 ;; shared --> -- discard value
1342 ;; -- --> local add slot
1345 ;; Go through all the old local slots.
1347 (dolist (name olayout)
1348 (let ((npos (posq name nlayout)))
1350 (setf (clos-slots-ref nslots npos)
1351 (clos-slots-ref oslots opos))
1353 (push name discarded)
1354 (unless (eq (clos-slots-ref oslots opos) +slot-unbound+)
1355 (setf (getf plist name) (clos-slots-ref oslots opos))))))
1358 ;; Go through all the old shared slots.
1359 (dolist (oclass-slot-and-val oclass-slots)
1360 (let ((name (car oclass-slot-and-val))
1361 (val (cdr oclass-slot-and-val)))
1362 (let ((npos (posq name nlayout)))
1364 (setf (clos-slots-ref nslots npos) val)))))
1366 ;; Go through all the new local slots to compute the added slots.
1367 (dolist (nlocal nlayout)
1368 (unless (or (memq nlocal olayout)
1369 (assq nlocal oclass-slots))
1370 (push nlocal added)))
1372 (swap-wrappers-and-slots instance copy)
1374 (update-instance-for-redefined-class instance
1380 (defun change-class-internal (instance new-class initargs)
1381 (let* ((old-class (class-of instance))
1382 (copy (allocate-instance new-class))
1383 (new-wrapper (get-wrapper copy))
1384 (old-wrapper (class-wrapper old-class))
1385 (old-layout (wrapper-instance-slots-layout old-wrapper))
1386 (new-layout (wrapper-instance-slots-layout new-wrapper))
1387 (old-slots (get-slots instance))
1388 (new-slots (get-slots copy))
1389 (old-class-slots (wrapper-class-slots old-wrapper)))
1391 ;; "The values of local slots specified by both the class CTO and
1392 ;; CFROM are retained. If such a local slot was unbound, it
1393 ;; remains unbound."
1394 (let ((new-position 0))
1395 (dolist (new-slot new-layout)
1396 (let ((old-position (posq new-slot old-layout)))
1398 (setf (clos-slots-ref new-slots new-position)
1399 (clos-slots-ref old-slots old-position))))
1400 (incf new-position)))
1402 ;; "The values of slots specified as shared in the class CFROM and
1403 ;; as local in the class CTO are retained."
1404 (dolist (slot-and-val old-class-slots)
1405 (let ((position (posq (car slot-and-val) new-layout)))
1407 (setf (clos-slots-ref new-slots position) (cdr slot-and-val)))))
1409 ;; Make the copy point to the old instance's storage, and make the
1410 ;; old instance point to the new storage.
1411 (swap-wrappers-and-slots instance copy)
1413 (apply #'update-instance-for-different-class copy instance initargs)
1416 (defmethod change-class ((instance standard-object) (new-class standard-class)
1418 (unless (class-finalized-p new-class)
1419 (finalize-inheritance new-class))
1420 (let ((cpl (class-precedence-list new-class)))
1424 `(when (eq class (find-class ',class-name))
1425 (error 'metaobject-initialization-violation
1426 :format-control "~@<Cannot ~S objects into ~S metaobjects.~@:>"
1427 :format-arguments (list 'change-class ',class-name)
1428 :references (list '(:amop :initialization ,class-name))))))
1430 (frob generic-function)
1432 (frob slot-definition))))
1433 (change-class-internal instance new-class initargs))
1435 (defmethod change-class ((instance forward-referenced-class)
1436 (new-class standard-class) &rest initargs)
1437 (let ((cpl (class-precedence-list new-class)))
1439 (error 'metaobject-initialization-violation
1441 "~@<Cannot ~S ~S objects into non-~S objects.~@:>"
1443 (list 'change-class 'forward-referenced-class 'class)
1445 (list '(:amop :generic-function ensure-class-using-class)
1446 '(:amop :initialization class))))
1447 (when (eq class (find-class 'class))
1449 (change-class-internal instance new-class initargs))
1451 (defmethod change-class ((instance funcallable-standard-object)
1452 (new-class funcallable-standard-class)
1454 (let ((cpl (class-precedence-list new-class)))
1458 `(when (eq class (find-class ',class-name))
1459 (error 'metaobject-initialization-violation
1460 :format-control "~@<Cannot ~S objects into ~S metaobjects.~@:>"
1461 :format-arguments (list 'change-class ',class-name)
1462 :references (list '(:amop :initialization ,class-name))))))
1464 (frob generic-function)
1466 (frob slot-definition))))
1467 (change-class-internal instance new-class initargs))
1469 (defmethod change-class ((instance standard-object)
1470 (new-class funcallable-standard-class)
1472 (declare (ignore initargs))
1473 (error "You can't change the class of ~S to ~S~@
1474 because it isn't already an instance with metaclass ~S."
1475 instance new-class 'standard-class))
1477 (defmethod change-class ((instance funcallable-standard-object)
1478 (new-class standard-class)
1480 (declare (ignore initargs))
1481 (error "You can't change the class of ~S to ~S~@
1482 because it isn't already an instance with metaclass ~S."
1483 instance new-class 'funcallable-standard-class))
1485 (defmethod change-class ((instance t) (new-class-name symbol) &rest initargs)
1486 (apply #'change-class instance (find-class new-class-name) initargs))
1488 ;;;; The metaclass BUILT-IN-CLASS
1490 ;;;; This metaclass is something of a weird creature. By this point, all
1491 ;;;; instances of it which will exist have been created, and no instance
1492 ;;;; is ever created by calling MAKE-INSTANCE.
1494 ;;;; But, there are other parts of the protocol we must follow and those
1495 ;;;; definitions appear here.
1497 (macrolet ((def (name args control)
1498 `(defmethod ,name ,args
1499 (declare (ignore initargs))
1500 (error 'metaobject-initialization-violation
1501 :format-control ,(format nil "~@<~A~@:>" control)
1502 :format-arguments (list ',name)
1503 :references (list '(:amop :initialization "Class"))))))
1504 (def initialize-instance ((class built-in-class) &rest initargs)
1505 "Cannot ~S an instance of BUILT-IN-CLASS.")
1506 (def reinitialize-instance ((class built-in-class) &rest initargs)
1507 "Cannot ~S an instance of BUILT-IN-CLASS."))
1509 (macrolet ((def (name)
1510 `(defmethod ,name ((class built-in-class)) nil)))
1511 (def class-direct-slots)
1513 (def class-direct-default-initargs)
1514 (def class-default-initargs))
1516 (defmethod validate-superclass ((c class) (s built-in-class))
1517 (or (eq s *the-class-t*) (eq s *the-class-stream*)
1518 ;; FIXME: bad things happen if someone tries to mix in both
1519 ;; FILE-STREAM and STRING-STREAM (as they have the same
1520 ;; layout-depthoid). Is there any way we can provide a useful
1521 ;; error message? -- CSR, 2005-05-03
1522 (eq s *the-class-file-stream*) (eq s *the-class-string-stream*)))
1524 ;;; Some necessary methods for FORWARD-REFERENCED-CLASS
1525 (defmethod class-direct-slots ((class forward-referenced-class)) ())
1526 (defmethod class-direct-default-initargs ((class forward-referenced-class)) ())
1527 (macrolet ((def (method)
1528 `(defmethod ,method ((class forward-referenced-class))
1529 (error "~@<~I~S was called on a forward referenced class:~2I~_~S~:>"
1531 (def class-default-initargs)
1532 (def class-precedence-list)
1535 (defmethod validate-superclass ((c slot-class)
1536 (f forward-referenced-class))
1539 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1540 (pushnew dependent (plist-value metaobject 'dependents)))
1542 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1543 (setf (plist-value metaobject 'dependents)
1544 (delete dependent (plist-value metaobject 'dependents))))
1546 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1547 (dolist (dependent (plist-value metaobject 'dependents))
1548 (funcall function dependent)))