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 ;;; CMUCL (Gerd PCL 2003-04-25) comment:
91 ;;; Compute an effective method for SLOT-VALUE-USING-CLASS, (SETF
92 ;;; SLOT-VALUE-USING-CLASS) or SLOT-BOUNDP-USING-CLASS for reading/
93 ;;; writing/testing effective slot SLOTD.
95 ;;; TYPE is one of the symbols READER, WRITER or BOUNDP, depending on
96 ;;; GF. Store the effective method in the effective slot definition
97 ;;; object itself; these GFs have special dispatch functions calling
98 ;;; effective methods directly retrieved from effective slot
99 ;;; definition objects, as an optimization.
101 ;;; FIXME: Change the function name to COMPUTE-SVUC-SLOTD-FUNCTION,
103 (defmethod compute-slot-accessor-info ((slotd effective-slot-definition)
105 (let* ((name (slot-value slotd 'name))
106 (class (slot-value slotd 'class))
107 (old-slotd (find-slot-definition class name))
108 (old-std-p (and old-slotd (slot-accessor-std-p old-slotd 'all))))
109 (multiple-value-bind (function std-p)
110 (if (eq *boot-state* 'complete)
111 (get-accessor-method-function gf type class slotd)
112 (get-optimized-std-accessor-method-function class slotd type))
113 (setf (slot-accessor-std-p slotd type) std-p)
114 (setf (slot-accessor-function slotd type) function))
115 (when (and old-slotd (not (eq old-std-p (slot-accessor-std-p slotd 'all))))
116 (push (cons class name) *pv-table-cache-update-info*))))
118 (defmethod slot-definition-allocation ((slotd structure-slot-definition))
121 (defmethod shared-initialize :after ((object documentation-mixin)
123 &key (documentation nil documentation-p))
124 (declare (ignore slot-names))
125 (when documentation-p
126 (setf (plist-value object 'documentation) documentation)))
128 ;;; default if DOC-TYPE doesn't match one of the specified types
129 (defmethod documentation (object doc-type)
130 (warn "unsupported DOCUMENTATION: type ~S for object ~S"
135 ;;; default if DOC-TYPE doesn't match one of the specified types
136 (defmethod (setf documentation) (new-value object doc-type)
137 ;; CMU CL made this an error, but since ANSI says that even for supported
138 ;; doc types an implementation is permitted to discard docs at any time
139 ;; for any reason, this feels to me more like a warning. -- WHN 19991214
140 (warn "discarding unsupported DOCUMENTATION of type ~S for object ~S"
145 (defmethod documentation ((object documentation-mixin) doc-type)
146 (declare (ignore doc-type))
147 (plist-value object 'documentation))
149 (defmethod (setf documentation) (new-value
150 (object documentation-mixin)
152 (declare (ignore doc-type))
153 (setf (plist-value object 'documentation) new-value))
155 (defmethod documentation ((slotd standard-slot-definition) doc-type)
156 (declare (ignore doc-type))
157 (slot-value slotd 'documentation))
159 (defmethod (setf documentation) (new-value
160 (slotd standard-slot-definition)
162 (declare (ignore doc-type))
163 (setf (slot-value slotd 'documentation) new-value))
165 ;;;; various class accessors that are a little more complicated than can be
166 ;;;; done with automatically generated reader methods
168 (defmethod class-prototype ((class std-class))
169 (with-slots (prototype) class
170 (or prototype (setq prototype (allocate-instance class)))))
172 (defmethod class-prototype ((class structure-class))
173 (with-slots (prototype wrapper defstruct-constructor) class
176 (if defstruct-constructor
177 (allocate-instance class)
178 (allocate-standard-instance wrapper))))))
180 (defmethod class-direct-default-initargs ((class slot-class))
181 (plist-value class 'direct-default-initargs))
183 (defmethod class-default-initargs ((class slot-class))
184 (plist-value class 'default-initargs))
186 (defmethod class-slot-cells ((class std-class))
187 (plist-value class 'class-slot-cells))
189 ;;;; class accessors that are even a little bit more complicated than those
190 ;;;; above. These have a protocol for updating them, we must implement that
193 ;;; Maintaining the direct subclasses backpointers. The update methods are
194 ;;; here, the values are read by an automatically generated reader method.
195 (defmethod add-direct-subclass ((class class) (subclass class))
196 (with-slots (direct-subclasses) class
197 (pushnew subclass direct-subclasses)
199 (defmethod remove-direct-subclass ((class class) (subclass class))
200 (with-slots (direct-subclasses) class
201 (setq direct-subclasses (remove subclass direct-subclasses))
204 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
206 ;;; There are four generic functions involved, each has one method for the
207 ;;; class case and another method for the damned EQL specializers. All of
208 ;;; these are specified methods and appear in their specified place in the
211 ;;; ADD-DIRECT-METHOD
212 ;;; REMOVE-DIRECT-METHOD
213 ;;; SPECIALIZER-DIRECT-METHODS
214 ;;; SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
216 ;;; In each case, we maintain one value which is a cons. The car is the list
217 ;;; methods. The cdr is a list of the generic functions. The cdr is always
219 (defmethod add-direct-method ((specializer class) (method method))
220 (with-slots (direct-methods) specializer
221 (setf (car direct-methods) (adjoin method (car direct-methods)) ;PUSH
222 (cdr direct-methods) ()))
224 (defmethod remove-direct-method ((specializer class) (method method))
225 (with-slots (direct-methods) specializer
226 (setf (car direct-methods) (remove method (car direct-methods))
227 (cdr direct-methods) ()))
230 (defmethod specializer-direct-methods ((specializer class))
231 (with-slots (direct-methods) specializer
232 (car direct-methods)))
234 (defmethod specializer-direct-generic-functions ((specializer class))
235 (with-slots (direct-methods) specializer
236 (or (cdr direct-methods)
237 (setf (cdr direct-methods)
239 (dolist (m (car direct-methods))
240 ;; the old PCL code used COLLECTING-ONCE which used
241 ;; #'EQ to check for newness
242 (pushnew (method-generic-function m) collect :test #'eq))
243 (nreverse collect))))))
245 ;;; This hash table is used to store the direct methods and direct generic
246 ;;; functions of EQL specializers. Each value in the table is the cons.
247 (defvar *eql-specializer-methods* (make-hash-table :test 'eql))
248 (defvar *class-eq-specializer-methods* (make-hash-table :test 'eq))
250 (defmethod specializer-method-table ((specializer eql-specializer))
251 *eql-specializer-methods*)
253 (defmethod specializer-method-table ((specializer class-eq-specializer))
254 *class-eq-specializer-methods*)
256 (defmethod add-direct-method ((specializer specializer-with-object)
258 (let* ((object (specializer-object specializer))
259 (table (specializer-method-table specializer))
260 (entry (gethash object table)))
263 (setf (gethash object table)
265 (setf (car entry) (adjoin method (car entry))
269 (defmethod remove-direct-method ((specializer specializer-with-object)
271 (let* ((object (specializer-object specializer))
272 (entry (gethash object (specializer-method-table specializer))))
274 (setf (car entry) (remove method (car entry))
278 (defmethod specializer-direct-methods ((specializer specializer-with-object))
279 (car (gethash (specializer-object specializer)
280 (specializer-method-table specializer))))
282 (defmethod specializer-direct-generic-functions ((specializer
283 specializer-with-object))
284 (let* ((object (specializer-object specializer))
285 (entry (gethash object (specializer-method-table specializer))))
290 (dolist (m (car entry))
291 (pushnew (method-generic-function m) collect :test #'eq))
292 (nreverse collect)))))))
294 (defun map-specializers (function)
295 (map-all-classes (lambda (class)
296 (funcall function (class-eq-specializer class))
297 (funcall function class)))
298 (maphash (lambda (object methods)
299 (declare (ignore methods))
300 (intern-eql-specializer object))
301 *eql-specializer-methods*)
302 (maphash (lambda (object specl)
303 (declare (ignore object))
304 (funcall function specl))
305 *eql-specializer-table*)
308 (defun map-all-generic-functions (function)
309 (let ((all-generic-functions (make-hash-table :test 'eq)))
310 (map-specializers (lambda (specl)
311 (dolist (gf (specializer-direct-generic-functions
313 (unless (gethash gf all-generic-functions)
314 (setf (gethash gf all-generic-functions) t)
315 (funcall function gf))))))
318 (defmethod shared-initialize :after ((specl class-eq-specializer)
321 (declare (ignore slot-names))
322 (setf (slot-value specl 'type) `(class-eq ,(specializer-class specl))))
324 (defmethod shared-initialize :after ((specl eql-specializer) slot-names &key)
325 (declare (ignore slot-names))
326 (setf (slot-value specl 'type) `(eql ,(specializer-object specl))))
328 (defun real-load-defclass (name metaclass-name supers slots other)
329 (let ((res (apply #'ensure-class name :metaclass metaclass-name
330 :direct-superclasses supers
332 :definition-source `((defclass ,name)
337 (setf (gdefinition 'load-defclass) #'real-load-defclass)
339 (defun ensure-class (name &rest all)
340 (apply #'ensure-class-using-class (find-class name nil) name all))
342 (defmethod ensure-class-using-class ((class null) name &rest args &key)
343 (multiple-value-bind (meta initargs)
344 (ensure-class-values class args)
345 (set-class-type-translation (class-prototype meta) name)
346 (setf class (apply #'make-instance meta :name name initargs)
347 (find-class name) class)
348 (set-class-type-translation class name)
351 (defmethod ensure-class-using-class ((class pcl-class) name &rest args &key)
352 (multiple-value-bind (meta initargs)
353 (ensure-class-values class args)
354 (unless (eq (class-of class) meta)
355 (apply #'change-class class meta initargs))
356 (apply #'reinitialize-instance class initargs)
357 (setf (find-class name) class)
358 (set-class-type-translation class name)
361 (defmethod class-predicate-name ((class t))
366 ((not (legal-class-name-p s))
367 (error "~S is not a class or a legal class name." s))
369 (or (find-class s nil)
370 (make-instance 'forward-referenced-class
373 (defun ensure-class-values (class args)
374 (let* ((initargs (copy-list args))
375 (unsupplied (list 1))
376 (supplied-meta (getf initargs :metaclass unsupplied))
377 (supplied-supers (getf initargs :direct-superclasses unsupplied))
378 (supplied-slots (getf initargs :direct-slots unsupplied))
380 (cond ((neq supplied-meta unsupplied)
381 (find-class supplied-meta))
383 (forward-referenced-class-p class))
384 *the-class-standard-class*)
387 ;; KLUDGE: It seemed to me initially that there ought to be a way
388 ;; of collecting all the erroneous problems in one go, rather than
389 ;; this way of solving the problem of signalling the errors that
390 ;; we are required to, which stops at the first bogus input.
391 ;; However, after playing around a little, I couldn't find that
392 ;; way, so I've left it as is, but if someone does come up with a
393 ;; better way... -- CSR, 2002-09-08
394 (do ((direct-slots (getf initargs :direct-slots) (cdr direct-slots)))
395 ((endp direct-slots) nil)
396 (destructuring-bind (slot &rest more) direct-slots
397 (let ((slot-name (getf slot :name)))
398 (when (some (lambda (s) (eq slot-name (getf s :name))) more)
399 ;; FIXME: It's quite possible that we ought to define an
400 ;; SB-INT:PROGRAM-ERROR function to signal these and other
401 ;; errors throughout the codebase that are required to be
402 ;; of type PROGRAM-ERROR.
403 (error 'simple-program-error
404 :format-control "~@<There is more than one direct slot ~
406 :format-arguments (list slot-name)))
407 (do ((stuff slot (cddr stuff)))
409 (destructuring-bind (option value &rest more) stuff
411 ((and (member option '(:allocation :type
412 :initform :documentation))
414 (getf more option unsupplied))))
415 (error 'simple-program-error
416 :format-control "~@<Duplicate slot option ~S for ~
418 :format-arguments (list option slot-name)))
419 ((and (eq option :readers)
420 (notevery #'symbolp value))
421 (error 'simple-program-error
422 :format-control "~@<Slot reader names for slot ~
423 named ~S must be symbols.~:>"
424 :format-arguments (list slot-name)))
425 ((and (eq option :initargs)
426 (notevery #'symbolp value))
427 (error 'simple-program-error
428 :format-control "~@<Slot initarg names for slot ~
429 named ~S must be symbols.~:>"
430 :format-arguments (list slot-name)))))))))
431 (loop for (initarg . more) on (getf initargs :direct-default-initargs)
432 for name = (car initarg)
433 when (some (lambda (a) (eq (car a) name)) more)
434 do (error 'simple-program-error
435 :format-control "~@<Duplicate initialization argument ~
436 name ~S in :DEFAULT-INITARGS.~:>"
437 :format-arguments (list name class)))
439 (default-initargs 0))
440 (do ((args initargs (cddr args)))
444 (when (> (incf metaclass) 1)
445 (error 'simple-program-error
446 :format-control "~@<More than one :METACLASS ~
447 option specified.~:>")))
448 (:direct-default-initargs
449 (when (> (incf default-initargs) 1)
450 (error 'simple-program-error
451 :format-control "~@<More than one :DEFAULT-INITARGS ~
452 option specified.~:>"))))))
453 (remf initargs :metaclass)
454 (loop (unless (remf initargs :direct-superclasses) (return)))
455 (loop (unless (remf initargs :direct-slots) (return)))
459 (when (neq supplied-supers unsupplied)
460 (list :direct-superclasses (mapcar #'fix-super supplied-supers)))
461 (when (neq supplied-slots unsupplied)
462 (list :direct-slots supplied-slots))
465 (defmethod shared-initialize :after
468 &key (direct-superclasses nil direct-superclasses-p)
469 (direct-slots nil direct-slots-p)
470 (direct-default-initargs nil direct-default-initargs-p)
471 (predicate-name nil predicate-name-p))
472 (declare (ignore slot-names))
473 (cond (direct-superclasses-p
474 (setq direct-superclasses
475 (or direct-superclasses
476 (list (if (funcallable-standard-class-p class)
477 *the-class-funcallable-standard-object*
478 *the-class-standard-object*))))
479 (dolist (superclass direct-superclasses)
480 (unless (validate-superclass class superclass)
481 (error "The class ~S was specified as a~%
482 super-class of the class ~S;~%~
483 but the meta-classes ~S and~%~S are incompatible.~@
484 Define a method for ~S to avoid this error."
485 superclass class (class-of superclass) (class-of class)
486 'validate-superclass)))
487 (setf (slot-value class 'direct-superclasses) direct-superclasses))
489 (setq direct-superclasses (slot-value class 'direct-superclasses))))
492 (setf (slot-value class 'direct-slots)
493 (mapcar (lambda (pl) (make-direct-slotd class pl))
495 (slot-value class 'direct-slots)))
496 (if direct-default-initargs-p
497 (setf (plist-value class 'direct-default-initargs)
498 direct-default-initargs)
499 (setq direct-default-initargs
500 (plist-value class 'direct-default-initargs)))
501 (setf (plist-value class 'class-slot-cells)
503 (dolist (dslotd direct-slots)
504 (when (eq :class (slot-definition-allocation dslotd))
505 (let ((initfunction (slot-definition-initfunction dslotd)))
506 (push (cons (slot-definition-name dslotd)
508 (funcall initfunction)
512 (setq predicate-name (if predicate-name-p
513 (setf (slot-value class 'predicate-name)
514 (car predicate-name))
515 (or (slot-value class 'predicate-name)
516 (setf (slot-value class 'predicate-name)
517 (make-class-predicate-name (class-name
519 (add-direct-subclasses class direct-superclasses)
520 (make-class-predicate class predicate-name)
521 (update-class class nil)
522 (do* ((slots (slot-value class 'slots) (cdr slots))
524 ((null slots) (when dupes
526 ;; FIXME: the indentation request ("~4I")
527 ;; below appears not to do anything. Finding
528 ;; out why would be nice. -- CSR, 2003-04-24
529 "~@<slot names with the same SYMBOL-NAME but ~
530 different SYMBOL-PACKAGE (possible package problem) ~
531 for class ~S:~@:_~4I~<~@{~S~^~:@_~}~:>~@:>"
534 (let* ((slot (car slots))
535 (oslots (remove (slot-definition-name slot) (cdr slots)
536 :test-not #'string= :key #'slot-definition-name)))
538 (pushnew (cons (slot-definition-name slot)
539 (mapcar #'slot-definition-name oslots))
541 :test #'string= :key #'car))))
542 (add-slot-accessors class direct-slots)
543 (make-preliminary-layout class))
545 (defmethod shared-initialize :after ((class forward-referenced-class)
546 slot-names &key &allow-other-keys)
547 (declare (ignore slot-names))
548 (make-preliminary-layout class))
550 (defvar *allow-forward-referenced-classes-in-cpl-p* nil)
552 ;;; Give CLASS a preliminary layout if it doesn't have one already, to
553 ;;; make it known to the type system.
554 (defun make-preliminary-layout (class)
555 (flet ((compute-preliminary-cpl (root)
556 (let ((*allow-forward-referenced-classes-in-cpl-p* t))
557 (compute-class-precedence-list root))))
558 (unless (class-finalized-p class)
559 (let ((name (class-name class)))
560 (setf (find-class name) class)
561 ;; KLUDGE: This is fairly horrible. We need to make a
562 ;; full-fledged CLASSOID here, not just tell the compiler that
563 ;; some class is forthcoming, because there are legitimate
564 ;; questions one can ask of the type system, implemented in
565 ;; terms of CLASSOIDs, involving forward-referenced classes. So.
566 (when (and (eq *boot-state* 'complete)
567 (null (find-classoid name nil)))
568 (setf (find-classoid name)
569 (make-standard-classoid :name name)))
570 (set-class-type-translation class name)
571 (let ((layout (make-wrapper 0 class))
572 (classoid (find-classoid name)))
573 (setf (layout-classoid layout) classoid)
574 (setf (classoid-pcl-class classoid) class)
575 (setf (slot-value class 'wrapper) layout)
576 (let ((cpl (compute-preliminary-cpl class)))
577 (setf (layout-inherits layout)
578 (order-layout-inherits
579 (map 'simple-vector #'class-wrapper
580 (reverse (rest cpl))))))
581 (register-layout layout :invalidate t)
582 (setf (classoid-layout classoid) layout)
583 (mapc #'make-preliminary-layout (class-direct-subclasses class)))))))
586 (defmethod shared-initialize :before ((class class) slot-names &key name)
587 (declare (ignore slot-names name))
588 ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
589 ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
590 (setf (slot-value class 'type) `(class ,class))
591 (setf (slot-value class 'class-eq-specializer)
592 (make-instance 'class-eq-specializer :class class)))
594 (defmethod reinitialize-instance :before ((class slot-class) &key)
595 (remove-direct-subclasses class (class-direct-superclasses class))
596 (remove-slot-accessors class (class-direct-slots class)))
598 (defmethod reinitialize-instance :after ((class slot-class)
601 (map-dependents class
603 (apply #'update-dependent class dependent initargs))))
605 (defmethod shared-initialize :after ((class condition-class) slot-names
606 &key direct-superclasses)
607 (declare (ignore slot-names))
608 (let ((classoid (find-classoid (class-name class))))
609 (with-slots (wrapper class-precedence-list prototype predicate-name
610 (direct-supers direct-superclasses))
612 (setf (slot-value class 'finalized-p) t)
613 (setf (classoid-pcl-class classoid) class)
614 (setq direct-supers direct-superclasses)
615 (setq wrapper (classoid-layout classoid))
616 (setq class-precedence-list (compute-class-precedence-list class))
617 (setq prototype (make-condition (class-name class)))
618 (add-direct-subclasses class direct-superclasses)
619 (setq predicate-name (make-class-predicate-name (class-name class)))
620 (make-class-predicate class predicate-name))))
622 (defmethod shared-initialize :after
623 ((slotd structure-slot-definition) slot-names &key
624 (allocation :instance) allocation-class)
625 (declare (ignore slot-names allocation-class))
626 (unless (eq allocation :instance)
627 (error "Structure slots must have :INSTANCE allocation.")))
629 (defun make-structure-class-defstruct-form (name direct-slots include)
630 (let* ((conc-name (intern (format nil "~S structure class " name)))
631 (constructor (intern (format nil "~Aconstructor" conc-name)))
632 (defstruct `(defstruct (,name
634 `((:include ,(class-name include))))
636 (:conc-name ,conc-name)
637 (:constructor ,constructor ())
639 ,@(mapcar (lambda (slot)
640 `(,(slot-definition-name slot)
643 (reader-names (mapcar (lambda (slotd)
644 (list 'slot-accessor name
645 (slot-definition-name slotd)
648 (writer-names (mapcar (lambda (slotd)
649 (list 'slot-accessor name
650 (slot-definition-name slotd)
654 (mapcar (lambda (slotd reader-name)
656 (slot-definition-defstruct-accessor-symbol
658 `(defun ,reader-name (obj)
659 (declare (type ,name obj))
661 direct-slots reader-names))
663 (mapcar (lambda (slotd writer-name)
665 (slot-definition-defstruct-accessor-symbol
667 `(defun ,writer-name (nv obj)
668 (declare (type ,name obj))
669 (setf (,accessor obj) nv))))
670 direct-slots writer-names))
674 ,@readers-init ,@writers-init
676 (values defstruct-form constructor reader-names writer-names)))
678 (defmethod shared-initialize :after
679 ((class structure-class)
681 &key (direct-superclasses nil direct-superclasses-p)
682 (direct-slots nil direct-slots-p)
683 direct-default-initargs
684 (predicate-name nil predicate-name-p))
685 (declare (ignore slot-names direct-default-initargs))
686 (if direct-superclasses-p
687 (setf (slot-value class 'direct-superclasses)
688 (or direct-superclasses
689 (setq direct-superclasses
690 (and (not (eq (class-name class) 'structure-object))
691 (list *the-class-structure-object*)))))
692 (setq direct-superclasses (slot-value class 'direct-superclasses)))
693 (let* ((name (class-name class))
694 (from-defclass-p (slot-value class 'from-defclass-p))
695 (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
697 (setf (slot-value class 'direct-slots)
701 (let* ((slot-name (getf pl :name))
704 "~S structure class ~A"
706 (accessor (intern acc-name)))
707 (setq pl (list* :defstruct-accessor-symbol
709 (make-direct-slotd class pl))
711 (setq direct-slots (slot-value class 'direct-slots)))
713 (let ((include (car (slot-value class 'direct-superclasses))))
714 (multiple-value-bind (defstruct-form constructor reader-names writer-names)
715 (make-structure-class-defstruct-form name direct-slots include)
716 (unless (structure-type-p name) (eval defstruct-form))
717 (mapc (lambda (dslotd reader-name writer-name)
718 (let* ((reader (gdefinition reader-name))
719 (writer (when (gboundp writer-name)
720 (gdefinition writer-name))))
721 (setf (slot-value dslotd 'internal-reader-function)
723 (setf (slot-value dslotd 'internal-writer-function)
725 direct-slots reader-names writer-names)
726 (setf (slot-value class 'defstruct-form) defstruct-form)
727 (setf (slot-value class 'defstruct-constructor) constructor))))
728 (add-direct-subclasses class direct-superclasses)
729 (setf (slot-value class 'class-precedence-list)
730 (compute-class-precedence-list class))
731 (setf (slot-value class 'slots) (compute-slots class))
732 (let ((lclass (find-classoid (class-name class))))
733 (setf (classoid-pcl-class lclass) class)
734 (setf (slot-value class 'wrapper) (classoid-layout lclass)))
735 (setf (slot-value class 'finalized-p) t)
736 (update-pv-table-cache-info class)
737 (setq predicate-name (if predicate-name-p
738 (setf (slot-value class 'predicate-name)
739 (car predicate-name))
740 (or (slot-value class 'predicate-name)
741 (setf (slot-value class 'predicate-name)
742 (make-class-predicate-name
743 (class-name class))))))
744 (make-class-predicate class predicate-name)
745 (add-slot-accessors class direct-slots)))
747 (defmethod direct-slot-definition-class ((class structure-class) initargs)
748 (declare (ignore initargs))
749 (find-class 'structure-direct-slot-definition))
751 (defmethod finalize-inheritance ((class structure-class))
752 nil) ; always finalized
754 (defun add-slot-accessors (class dslotds)
755 (fix-slot-accessors class dslotds 'add))
757 (defun remove-slot-accessors (class dslotds)
758 (fix-slot-accessors class dslotds 'remove))
760 (defun fix-slot-accessors (class dslotds add/remove)
761 (flet ((fix (gfspec name r/w)
762 (let ((gf (ensure-generic-function gfspec)))
764 (r (if (eq add/remove 'add)
765 (add-reader-method class gf name)
766 (remove-reader-method class gf)))
767 (w (if (eq add/remove 'add)
768 (add-writer-method class gf name)
769 (remove-writer-method class gf)))))))
770 (dolist (dslotd dslotds)
771 (let ((slot-name (slot-definition-name dslotd)))
772 (dolist (r (slot-definition-readers dslotd)) (fix r slot-name 'r))
773 (dolist (w (slot-definition-writers dslotd)) (fix w slot-name 'w))))))
775 (defun add-direct-subclasses (class supers)
776 (dolist (super supers)
777 (unless (memq class (class-direct-subclasses class))
778 (add-direct-subclass super class))))
780 (defun remove-direct-subclasses (class supers)
781 (let ((old (class-direct-superclasses class)))
782 (dolist (o (set-difference old supers))
783 (remove-direct-subclass o class))))
785 (defmethod finalize-inheritance ((class std-class))
786 (update-class class t))
788 (defmethod finalize-inheritance ((class forward-referenced-class))
789 ;; FIXME: should we not be thinking a bit about what kinds of error
790 ;; we're throwing? Maybe we need a clos-error type to mix in? Or
791 ;; possibly a forward-referenced-class-error, though that's
792 ;; difficult given e.g. class precedence list calculations...
794 "~@<FINALIZE-INHERITANCE was called on a forward referenced class:~
799 (defun class-has-a-forward-referenced-superclass-p (class)
800 (or (forward-referenced-class-p class)
801 (some #'class-has-a-forward-referenced-superclass-p
802 (class-direct-superclasses class))))
804 ;;; This is called by :after shared-initialize whenever a class is initialized
805 ;;; or reinitialized. The class may or may not be finalized.
806 (defun update-class (class finalizep)
807 ;; Comment from Gerd Moellmann:
809 ;; Note that we can't simply delay the finalization when CLASS has
810 ;; no forward referenced superclasses because that causes bootstrap
812 (when (and (not finalizep)
813 (not (class-finalized-p class))
814 (not (class-has-a-forward-referenced-superclass-p class)))
815 (finalize-inheritance class)
816 (return-from update-class))
817 (when (or finalizep (class-finalized-p class)
818 (not (class-has-a-forward-referenced-superclass-p class)))
819 (setf (find-class (class-name class)) class)
820 (update-cpl class (compute-class-precedence-list class))
821 ;; This invocation of UPDATE-SLOTS, in practice, finalizes the
822 ;; class. The hoops above are to ensure that FINALIZE-INHERITANCE
823 ;; is called at finalization, so that MOP programmers can hook
824 ;; into the system as described in "Class Finalization Protocol"
825 ;; (section 5.5.2 of AMOP).
826 (update-slots class (compute-slots class))
827 (update-gfs-of-class class)
828 (update-inits class (compute-default-initargs class))
829 (update-ctors 'finalize-inheritance :class class))
831 (dolist (sub (class-direct-subclasses class)) (update-class sub nil))))
833 (defun update-cpl (class cpl)
834 (if (class-finalized-p class)
835 (unless (equal (class-precedence-list class) cpl)
836 ;; comment from the old CMU CL sources:
837 ;; Need to have the cpl setup before update-lisp-class-layout
838 ;; is called on CMU CL.
839 (setf (slot-value class 'class-precedence-list) cpl)
840 (force-cache-flushes class))
841 (setf (slot-value class 'class-precedence-list) cpl))
842 (update-class-can-precede-p cpl))
844 (defun update-class-can-precede-p (cpl)
846 (let ((first (car cpl)))
847 (dolist (c (cdr cpl))
848 (pushnew c (slot-value first 'can-precede-list))))
849 (update-class-can-precede-p (cdr cpl))))
851 (defun class-can-precede-p (class1 class2)
852 (member class2 (class-can-precede-list class1)))
854 (defun update-slots (class eslotds)
855 (let ((instance-slots ())
857 (dolist (eslotd eslotds)
858 (let ((alloc (slot-definition-allocation eslotd)))
860 (:instance (push eslotd instance-slots))
861 (:class (push eslotd class-slots)))))
863 ;; If there is a change in the shape of the instances then the
864 ;; old class is now obsolete.
865 (let* ((nlayout (mapcar #'slot-definition-name
866 (sort instance-slots #'<
867 :key #'slot-definition-location)))
868 (nslots (length nlayout))
869 (nwrapper-class-slots (compute-class-slots class-slots))
870 (owrapper (when (class-finalized-p class)
871 (class-wrapper class)))
872 (olayout (when owrapper
873 (wrapper-instance-slots-layout owrapper)))
874 (owrapper-class-slots (and owrapper (wrapper-class-slots owrapper)))
876 (cond ((null owrapper)
877 (make-wrapper nslots class))
878 ((and (equal nlayout olayout)
880 (loop for o in owrapper-class-slots
881 for n in nwrapper-class-slots
882 do (unless (eq (car o) (car n)) (return t)))))
885 ;; This will initialize the new wrapper to have the
886 ;; same state as the old wrapper. We will then have
887 ;; to change that. This may seem like wasted work
888 ;; (and it is), but the spec requires that we call
889 ;; MAKE-INSTANCES-OBSOLETE.
890 (make-instances-obsolete class)
891 (class-wrapper class)))))
893 (with-slots (wrapper slots) class
894 (update-lisp-class-layout class nwrapper)
896 (wrapper-instance-slots-layout nwrapper) nlayout
897 (wrapper-class-slots nwrapper) nwrapper-class-slots
898 (wrapper-no-of-instance-slots nwrapper) nslots
900 (setf (slot-value class 'finalized-p) t)
901 (unless (eq owrapper nwrapper)
902 (update-pv-table-cache-info class)))))
904 (defun compute-class-slots (eslotds)
906 (dolist (eslotd eslotds)
907 (push (assoc (slot-definition-name eslotd)
908 (class-slot-cells (slot-definition-class eslotd)))
912 (defun update-gfs-of-class (class)
913 (when (and (class-finalized-p class)
914 (let ((cpl (class-precedence-list class)))
915 (or (member *the-class-slot-class* cpl)
916 (member *the-class-standard-effective-slot-definition*
918 (let ((gf-table (make-hash-table :test 'eq)))
919 (labels ((collect-gfs (class)
920 (dolist (gf (specializer-direct-generic-functions class))
921 (setf (gethash gf gf-table) t))
922 (mapc #'collect-gfs (class-direct-superclasses class))))
924 (maphash (lambda (gf ignore)
925 (declare (ignore ignore))
926 (update-gf-dfun class gf))
929 (defun update-inits (class inits)
930 (setf (plist-value class 'default-initargs) inits))
932 (defmethod compute-default-initargs ((class slot-class))
933 (let ((initargs (loop for c in (class-precedence-list class)
934 append (class-direct-default-initargs c))))
935 (delete-duplicates initargs :test #'eq :key #'car :from-end t)))
937 ;;;; protocols for constructing direct and effective slot definitions
939 (defmethod direct-slot-definition-class ((class std-class) initargs)
940 (declare (ignore initargs))
941 (find-class 'standard-direct-slot-definition))
943 (defun make-direct-slotd (class initargs)
944 (let ((initargs (list* :class class initargs)))
945 (apply #'make-instance
946 (direct-slot-definition-class class initargs)
949 (defmethod compute-slots ((class std-class))
950 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
951 ;; for each different slot name we find in our superclasses. Each
952 ;; call receives the class and a list of the dslotds with that name.
953 ;; The list is in most-specific-first order.
954 (let ((name-dslotds-alist ()))
955 (dolist (c (class-precedence-list class))
956 (dolist (slot (class-direct-slots c))
957 (let* ((name (slot-definition-name slot))
958 (entry (assq name name-dslotds-alist)))
960 (push slot (cdr entry))
961 (push (list name slot) name-dslotds-alist)))))
962 (mapcar (lambda (direct)
963 (compute-effective-slot-definition class
965 (nreverse (cdr direct))))
966 name-dslotds-alist)))
968 (defmethod compute-slots ((class standard-class))
971 (defmethod compute-slots :around ((class standard-class))
972 (let ((eslotds (call-next-method))
974 (dolist (eslotd eslotds eslotds)
975 (setf (slot-definition-location eslotd)
976 (ecase (slot-definition-allocation eslotd)
980 (let* ((name (slot-definition-name eslotd))
981 (from-class (slot-definition-allocation-class eslotd))
982 (cell (assq name (class-slot-cells from-class))))
985 (initialize-internal-slot-functions eslotd))))
987 (defmethod compute-slots ((class funcallable-standard-class))
990 (defmethod compute-slots :around ((class funcallable-standard-class))
991 (labels ((instance-slot-names (slotds)
993 (dolist (slotd slotds (nreverse collect))
994 (when (eq (slot-definition-allocation slotd) :instance)
995 (push (slot-definition-name slotd) collect)))))
996 ;; This sorts slots so that slots of classes later in the CPL
997 ;; come before slots of other classes. This is crucial for
998 ;; funcallable instances because it ensures that the slots of
999 ;; FUNCALLABLE-STANDARD-OBJECT, which includes the slots of
1000 ;; KERNEL:FUNCALLABLE-INSTANCE, come first, which in turn
1001 ;; makes it possible to treat FUNCALLABLE-STANDARD-OBJECT as
1002 ;; a funcallable instance.
1003 (compute-layout (eslotds)
1005 (names (instance-slot-names eslotds)))
1007 (reverse (class-precedence-list class))
1008 (nreverse (nconc names first)))
1009 (dolist (ss (class-slots class))
1010 (let ((name (slot-definition-name ss)))
1011 (when (member name names)
1013 (setq names (delete name names)))))))))
1014 (let ((all-slotds (call-next-method))
1017 (dolist (slotd all-slotds)
1018 (ecase (slot-definition-allocation slotd)
1019 (:instance (push slotd instance-slots))
1020 (:class (push slotd class-slots))))
1021 (let ((layout (compute-layout instance-slots)))
1022 (dolist (slotd instance-slots)
1023 (setf (slot-definition-location slotd)
1024 (position (slot-definition-name slotd) layout))
1025 (initialize-internal-slot-functions slotd)))
1026 (dolist (slotd class-slots)
1027 (let ((name (slot-definition-name slotd))
1028 (from-class (slot-definition-allocation-class slotd)))
1029 (setf (slot-definition-location slotd)
1030 (assoc name (class-slot-cells from-class)))
1031 (aver (consp (slot-definition-location slotd)))
1032 (initialize-internal-slot-functions slotd)))
1035 (defmethod compute-slots ((class structure-class))
1036 (mapcan (lambda (superclass)
1037 (mapcar (lambda (dslotd)
1038 (compute-effective-slot-definition
1040 (slot-definition-name dslotd)
1042 (class-direct-slots superclass)))
1043 (reverse (slot-value class 'class-precedence-list))))
1045 (defmethod compute-slots :around ((class structure-class))
1046 (let ((eslotds (call-next-method)))
1047 (mapc #'initialize-internal-slot-functions eslotds)
1050 (defmethod compute-effective-slot-definition ((class slot-class) name dslotds)
1051 (declare (ignore name))
1052 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
1053 (class (effective-slot-definition-class class initargs)))
1054 (apply #'make-instance class initargs)))
1056 (defmethod effective-slot-definition-class ((class std-class) initargs)
1057 (declare (ignore initargs))
1058 (find-class 'standard-effective-slot-definition))
1060 (defmethod effective-slot-definition-class ((class structure-class) initargs)
1061 (declare (ignore initargs))
1062 (find-class 'structure-effective-slot-definition))
1064 (defmethod compute-effective-slot-definition-initargs
1065 ((class slot-class) direct-slotds)
1071 (allocation-class nil)
1077 (dolist (slotd direct-slotds)
1080 (setq name (slot-definition-name slotd)
1083 (when (slot-definition-initfunction slotd)
1084 (setq initform (slot-definition-initform slotd)
1085 initfunction (slot-definition-initfunction slotd)
1088 (setq allocation (slot-definition-allocation slotd)
1089 allocation-class (slot-definition-class slotd)
1091 (setq initargs (append (slot-definition-initargs slotd) initargs))
1092 (let ((slotd-type (slot-definition-type slotd)))
1093 (setq type (cond ((eq type t) slotd-type)
1094 ((*subtypep type slotd-type) type)
1095 (t `(and ,type ,slotd-type)))))))
1098 :initfunction initfunction
1100 :allocation allocation
1101 :allocation-class allocation-class
1105 (defmethod compute-effective-slot-definition-initargs :around
1106 ((class structure-class) direct-slotds)
1107 (let ((slotd (car direct-slotds)))
1108 (list* :defstruct-accessor-symbol
1109 (slot-definition-defstruct-accessor-symbol slotd)
1110 :internal-reader-function
1111 (slot-definition-internal-reader-function slotd)
1112 :internal-writer-function
1113 (slot-definition-internal-writer-function slotd)
1114 (call-next-method))))
1116 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
1117 ;;; to make the method object. They have to use make-a-method which
1118 ;;; is a specially bootstrapped mechanism for making standard methods.
1119 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
1120 (declare (ignore direct-slot initargs))
1121 (find-class 'standard-reader-method))
1123 (defmethod add-reader-method ((class slot-class) generic-function slot-name)
1124 (add-method generic-function
1125 (make-a-method 'standard-reader-method
1127 (list (or (class-name class) 'object))
1129 (make-reader-method-function class slot-name)
1130 "automatically generated reader method"
1133 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
1134 (declare (ignore direct-slot initargs))
1135 (find-class 'standard-writer-method))
1137 (defmethod add-writer-method ((class slot-class) generic-function slot-name)
1138 (add-method generic-function
1139 (make-a-method 'standard-writer-method
1141 (list 'new-value (or (class-name class) 'object))
1142 (list *the-class-t* class)
1143 (make-writer-method-function class slot-name)
1144 "automatically generated writer method"
1147 (defmethod add-boundp-method ((class slot-class) generic-function slot-name)
1148 (add-method generic-function
1149 (make-a-method 'standard-boundp-method
1151 (list (or (class-name class) 'object))
1153 (make-boundp-method-function class slot-name)
1154 "automatically generated boundp method"
1157 (defmethod remove-reader-method ((class slot-class) generic-function)
1158 (let ((method (get-method generic-function () (list class) nil)))
1159 (when method (remove-method generic-function method))))
1161 (defmethod remove-writer-method ((class slot-class) generic-function)
1163 (get-method generic-function () (list *the-class-t* class) nil)))
1164 (when method (remove-method generic-function method))))
1166 (defmethod remove-boundp-method ((class slot-class) generic-function)
1167 (let ((method (get-method generic-function () (list class) nil)))
1168 (when method (remove-method generic-function method))))
1170 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITE-METHOD function are NOT
1171 ;;; part of the standard protocol. They are however useful, PCL makes
1172 ;;; use of them internally and documents them for PCL users.
1174 ;;; *** This needs work to make type testing by the writer functions which
1175 ;;; *** do type testing faster. The idea would be to have one constructor
1176 ;;; *** for each possible type test.
1178 ;;; *** There is a subtle bug here which is going to have to be fixed.
1179 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1180 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1181 ;;; *** defined for this metaclass a chance to run.
1183 (defmethod make-reader-method-function ((class slot-class) slot-name)
1184 (make-std-reader-method-function (class-name class) slot-name))
1186 (defmethod make-writer-method-function ((class slot-class) slot-name)
1187 (make-std-writer-method-function (class-name class) slot-name))
1189 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1190 (make-std-boundp-method-function (class-name class) slot-name))
1192 (defmethod compatible-meta-class-change-p (class proto-new-class)
1193 (eq (class-of class) (class-of proto-new-class)))
1195 (defmethod validate-superclass ((class class) (new-super class))
1196 (or (eq new-super *the-class-t*)
1197 (eq (class-of class) (class-of new-super))))
1199 (defmethod validate-superclass ((class standard-class) (new-super std-class))
1200 (let ((new-super-meta-class (class-of new-super)))
1201 (or (eq new-super-meta-class *the-class-std-class*)
1202 (eq (class-of class) new-super-meta-class))))
1204 ;;; What this does depends on which of the four possible values of
1205 ;;; LAYOUT-INVALID the PCL wrapper has; the simplest case is when it
1206 ;;; is (:FLUSH <wrapper>) or (:OBSOLETE <wrapper>), when there is
1207 ;;; nothing to do, as the new wrapper has already been created. If
1208 ;;; LAYOUT-INVALID returns NIL, then we invalidate it (setting it to
1209 ;;; (:FLUSH <wrapper>); UPDATE-SLOTS later gets to choose whether or
1210 ;;; not to "upgrade" this to (:OBSOLETE <wrapper>).
1212 ;;; This leaves the case where LAYOUT-INVALID returns T, which happens
1213 ;;; when REGISTER-LAYOUT has invalidated a superclass of CLASS (which
1214 ;;; invalidated all the subclasses in SB-KERNEL land). Again, here we
1215 ;;; must flush the caches and allow UPDATE-SLOTS to decide whether to
1216 ;;; obsolete the wrapper.
1218 ;;; FIXME: either here or in INVALID-WRAPPER-P looks like a good place
1219 ;;; for (AVER (NOT (EQ (LAYOUT-INVALID OWRAPPER)
1220 ;;; :UNINITIALIZED)))
1222 ;;; Thanks to Gerd Moellmann for the explanation. -- CSR, 2002-10-29
1223 (defun force-cache-flushes (class)
1224 (let* ((owrapper (class-wrapper class)))
1225 ;; We only need to do something if the wrapper is still valid. If
1226 ;; the wrapper isn't valid, state will be FLUSH or OBSOLETE, and
1227 ;; both of those will already be doing what we want. In
1228 ;; particular, we must be sure we never change an OBSOLETE into a
1229 ;; FLUSH since OBSOLETE means do what FLUSH does and then some.
1230 (when (or (not (invalid-wrapper-p owrapper))
1231 ;; KLUDGE: despite the observations above, this remains
1232 ;; a violation of locality or what might be considered
1233 ;; good style. There has to be a better way! -- CSR,
1235 (eq (layout-invalid owrapper) t))
1236 (let ((nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1238 (setf (wrapper-instance-slots-layout nwrapper)
1239 (wrapper-instance-slots-layout owrapper))
1240 (setf (wrapper-class-slots nwrapper)
1241 (wrapper-class-slots owrapper))
1243 (update-lisp-class-layout class nwrapper)
1244 (setf (slot-value class 'wrapper) nwrapper)
1245 (invalidate-wrapper owrapper :flush nwrapper))))))
1247 (defun flush-cache-trap (owrapper nwrapper instance)
1248 (declare (ignore owrapper))
1249 (set-wrapper instance nwrapper))
1251 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1252 ;;; the next access to the instance (as defined in 88-002R) to trap
1253 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1254 (defmethod make-instances-obsolete ((class std-class))
1255 (let* ((owrapper (class-wrapper class))
1256 (nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1258 (setf (wrapper-instance-slots-layout nwrapper)
1259 (wrapper-instance-slots-layout owrapper))
1260 (setf (wrapper-class-slots nwrapper)
1261 (wrapper-class-slots owrapper))
1263 (update-lisp-class-layout class nwrapper)
1264 (setf (slot-value class 'wrapper) nwrapper)
1265 (invalidate-wrapper owrapper :obsolete nwrapper)
1268 (defmethod make-instances-obsolete ((class symbol))
1269 (make-instances-obsolete (find-class class)))
1271 ;;; OBSOLETE-INSTANCE-TRAP is the internal trap that is called when we
1272 ;;; see an obsolete instance. The times when it is called are:
1273 ;;; - when the instance is involved in method lookup
1274 ;;; - when attempting to access a slot of an instance
1276 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1277 ;;; instance access macros.
1279 ;;; Of course these times when it is called are an internal
1280 ;;; implementation detail of PCL and are not part of the documented
1281 ;;; description of when the obsolete instance update happens. The
1282 ;;; documented description is as it appears in 88-002R.
1284 ;;; This has to return the new wrapper, so it counts on all the
1285 ;;; methods on obsolete-instance-trap-internal to return the new
1286 ;;; wrapper. It also does a little internal error checking to make
1287 ;;; sure that the traps are only happening when they should, and that
1288 ;;; the trap methods are computing appropriate new wrappers.
1290 ;;; OBSOLETE-INSTANCE-TRAP might be called on structure instances
1291 ;;; after a structure is redefined. In most cases,
1292 ;;; OBSOLETE-INSTANCE-TRAP will not be able to fix the old instance,
1293 ;;; so it must signal an error. The hard part of this is that the
1294 ;;; error system and debugger might cause OBSOLETE-INSTANCE-TRAP to be
1295 ;;; called again, so in that case, we have to return some reasonable
1296 ;;; wrapper, instead.
1298 (defvar *in-obsolete-instance-trap* nil)
1299 (defvar *the-wrapper-of-structure-object*
1300 (class-wrapper (find-class 'structure-object)))
1302 (define-condition obsolete-structure (error)
1303 ((datum :reader obsolete-structure-datum :initarg :datum))
1305 (lambda (condition stream)
1306 ;; Don't try to print the structure, since it probably won't work.
1308 "~@<obsolete structure error for a structure of type ~2I~_~S~:>"
1309 (type-of (obsolete-structure-datum condition))))))
1311 (defun obsolete-instance-trap (owrapper nwrapper instance)
1312 (if (not (pcl-instance-p instance))
1313 (if *in-obsolete-instance-trap*
1314 *the-wrapper-of-structure-object*
1315 (let ((*in-obsolete-instance-trap* t))
1316 (error 'obsolete-structure :datum instance)))
1317 (let* ((class (wrapper-class* nwrapper))
1318 (copy (allocate-instance class)) ;??? allocate-instance ???
1319 (olayout (wrapper-instance-slots-layout owrapper))
1320 (nlayout (wrapper-instance-slots-layout nwrapper))
1321 (oslots (get-slots instance))
1322 (nslots (get-slots copy))
1323 (oclass-slots (wrapper-class-slots owrapper))
1327 ;; local --> local transfer
1328 ;; local --> shared discard
1329 ;; local --> -- discard
1330 ;; shared --> local transfer
1331 ;; shared --> shared discard
1332 ;; shared --> -- discard
1336 ;; Go through all the old local slots.
1338 (dolist (name olayout)
1339 (let ((npos (posq name nlayout)))
1341 (setf (clos-slots-ref nslots npos)
1342 (clos-slots-ref oslots opos))
1344 (push name discarded)
1345 (unless (eq (clos-slots-ref oslots opos) +slot-unbound+)
1346 (setf (getf plist name) (clos-slots-ref oslots opos))))))
1349 ;; Go through all the old shared slots.
1350 (dolist (oclass-slot-and-val oclass-slots)
1351 (let ((name (car oclass-slot-and-val))
1352 (val (cdr oclass-slot-and-val)))
1353 (let ((npos (posq name nlayout)))
1355 (setf (clos-slots-ref nslots npos) (cdr oclass-slot-and-val))
1356 (progn (push name discarded)
1357 (unless (eq val +slot-unbound+)
1358 (setf (getf plist name) val)))))))
1360 ;; Go through all the new local slots to compute the added slots.
1361 (dolist (nlocal nlayout)
1362 (unless (or (memq nlocal olayout)
1363 (assq nlocal oclass-slots))
1364 (push nlocal added)))
1366 (swap-wrappers-and-slots instance copy)
1368 (update-instance-for-redefined-class instance
1374 (defun change-class-internal (instance new-class initargs)
1375 (let* ((old-class (class-of instance))
1376 (copy (allocate-instance new-class))
1377 (new-wrapper (get-wrapper copy))
1378 (old-wrapper (class-wrapper old-class))
1379 (old-layout (wrapper-instance-slots-layout old-wrapper))
1380 (new-layout (wrapper-instance-slots-layout new-wrapper))
1381 (old-slots (get-slots instance))
1382 (new-slots (get-slots copy))
1383 (old-class-slots (wrapper-class-slots old-wrapper)))
1385 ;; "The values of local slots specified by both the class CTO and
1386 ;; CFROM are retained. If such a local slot was unbound, it
1387 ;; remains unbound."
1388 (let ((new-position 0))
1389 (dolist (new-slot new-layout)
1390 (let ((old-position (posq new-slot old-layout)))
1392 (setf (clos-slots-ref new-slots new-position)
1393 (clos-slots-ref old-slots old-position))))
1394 (incf new-position)))
1396 ;; "The values of slots specified as shared in the class CFROM and
1397 ;; as local in the class CTO are retained."
1398 (dolist (slot-and-val old-class-slots)
1399 (let ((position (posq (car slot-and-val) new-layout)))
1401 (setf (clos-slots-ref new-slots position) (cdr slot-and-val)))))
1403 ;; Make the copy point to the old instance's storage, and make the
1404 ;; old instance point to the new storage.
1405 (swap-wrappers-and-slots instance copy)
1407 (apply #'update-instance-for-different-class copy instance initargs)
1410 (defmethod change-class ((instance standard-object)
1411 (new-class standard-class)
1413 (change-class-internal instance new-class initargs))
1415 (defmethod change-class ((instance funcallable-standard-object)
1416 (new-class funcallable-standard-class)
1418 (change-class-internal instance new-class initargs))
1420 (defmethod change-class ((instance standard-object)
1421 (new-class funcallable-standard-class)
1423 (declare (ignore initargs))
1424 (error "You can't change the class of ~S to ~S~@
1425 because it isn't already an instance with metaclass ~S."
1426 instance new-class 'standard-class))
1428 (defmethod change-class ((instance funcallable-standard-object)
1429 (new-class standard-class)
1431 (declare (ignore initargs))
1432 (error "You can't change the class of ~S to ~S~@
1433 because it isn't already an instance with metaclass ~S."
1434 instance new-class 'funcallable-standard-class))
1436 (defmethod change-class ((instance t) (new-class-name symbol) &rest initargs)
1437 (apply #'change-class instance (find-class new-class-name) initargs))
1439 ;;;; The metaclass BUILT-IN-CLASS
1441 ;;;; This metaclass is something of a weird creature. By this point, all
1442 ;;;; instances of it which will exist have been created, and no instance
1443 ;;;; is ever created by calling MAKE-INSTANCE.
1445 ;;;; But, there are other parts of the protocol we must follow and those
1446 ;;;; definitions appear here.
1448 (defmethod shared-initialize :before
1449 ((class built-in-class) slot-names &rest initargs)
1450 (declare (ignore slot-names initargs))
1451 (error "attempt to initialize or reinitialize a built in class"))
1453 (defmethod class-direct-slots ((class built-in-class)) ())
1454 (defmethod class-slots ((class built-in-class)) ())
1455 (defmethod class-direct-default-initargs ((class built-in-class)) ())
1456 (defmethod class-default-initargs ((class built-in-class)) ())
1458 (defmethod validate-superclass ((c class) (s built-in-class))
1459 (or (eq s *the-class-t*)
1460 (eq s *the-class-stream*)))
1462 ;;; Some necessary methods for FORWARD-REFERENCED-CLASS
1463 (defmethod class-direct-slots ((class forward-referenced-class)) ())
1464 (defmethod class-direct-default-initargs ((class forward-referenced-class)) ())
1465 (macrolet ((def (method)
1466 `(defmethod ,method ((class forward-referenced-class))
1467 (error "~@<~I~S was called on a forward referenced class:~2I~_~S~:>"
1469 (def class-default-initargs)
1470 (def class-precedence-list)
1473 (defmethod validate-superclass ((c slot-class)
1474 (f forward-referenced-class))
1477 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1478 (pushnew dependent (plist-value metaobject 'dependents)))
1480 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1481 (setf (plist-value metaobject 'dependents)
1482 (delete dependent (plist-value metaobject 'dependents))))
1484 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1485 (dolist (dependent (plist-value metaobject 'dependents))
1486 (funcall function dependent)))