Initial revision
[sbcl.git] / src / pcl / std-class.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3
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
8 ;;;; information.
9
10 ;;;; copyright information from original PCL sources:
11 ;;;;
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
14 ;;;;
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
18 ;;;; control laws.
19 ;;;;
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
22 ;;;; specification.
23
24 (sb-int:file-comment
25   "$Header$")
26
27 (in-package "SB-PCL")
28 \f
29 (defmethod slot-accessor-function ((slotd effective-slot-definition) type)
30   (ecase type
31     (reader (slot-definition-reader-function slotd))
32     (writer (slot-definition-writer-function slotd))
33     (boundp (slot-definition-boundp-function slotd))))
34
35 (defmethod (setf slot-accessor-function) (function
36                                           (slotd effective-slot-definition)
37                                           type)
38   (ecase type
39     (reader (setf (slot-definition-reader-function slotd) function))
40     (writer (setf (slot-definition-writer-function slotd) function))
41     (boundp (setf (slot-definition-boundp-function slotd) function))))
42
43 (defconstant *slotd-reader-function-std-p* 1)
44 (defconstant *slotd-writer-function-std-p* 2)
45 (defconstant *slotd-boundp-function-std-p* 4)
46 (defconstant *slotd-all-function-std-p* 7)
47
48 (defmethod slot-accessor-std-p ((slotd effective-slot-definition) type)
49   (let ((flags (slot-value slotd 'accessor-flags)))
50     (declare (type fixnum flags))
51     (if (eq type 'all)
52         (eql *slotd-all-function-std-p* flags)
53         (let ((mask (ecase type
54                       (reader *slotd-reader-function-std-p*)
55                       (writer *slotd-writer-function-std-p*)
56                       (boundp *slotd-boundp-function-std-p*))))
57           (declare (type fixnum mask))
58           (not (zerop (the fixnum (logand mask flags))))))))
59
60 (defmethod (setf slot-accessor-std-p) (value
61                                        (slotd effective-slot-definition)
62                                        type)
63   (let ((mask (ecase type
64                 (reader *slotd-reader-function-std-p*)
65                 (writer *slotd-writer-function-std-p*)
66                 (boundp *slotd-boundp-function-std-p*)))
67         (flags (slot-value slotd 'accessor-flags)))
68     (declare (type fixnum mask flags))
69     (setf (slot-value slotd 'accessor-flags)
70           (if value
71               (the fixnum (logior mask flags))
72               (the fixnum (logand (the fixnum (lognot mask)) flags)))))
73   value)
74
75 (defmethod initialize-internal-slot-functions ((slotd
76                                                 effective-slot-definition))
77   (let* ((name (slot-value slotd 'name))
78          (class (slot-value slotd 'class)))
79     (let ((table (or (gethash name *name->class->slotd-table*)
80                      (setf (gethash name *name->class->slotd-table*)
81                            (make-hash-table :test 'eq :size 5)))))
82       (setf (gethash class table) slotd))
83     (dolist (type '(reader writer boundp))
84       (let* ((gf-name (ecase type
85                               (reader 'slot-value-using-class)
86                               (writer '(setf slot-value-using-class))
87                               (boundp 'slot-boundp-using-class)))
88              (gf (gdefinition gf-name)))
89         (compute-slot-accessor-info slotd type gf)))
90     (initialize-internal-slot-gfs name)))
91
92 (defmethod compute-slot-accessor-info ((slotd effective-slot-definition)
93                                        type gf)
94   (let* ((name (slot-value slotd 'name))
95          (class (slot-value slotd 'class))
96          (old-slotd (find-slot-definition class name))
97          (old-std-p (and old-slotd (slot-accessor-std-p old-slotd 'all))))
98     (multiple-value-bind (function std-p)
99         (if (eq *boot-state* 'complete)
100             (get-accessor-method-function gf type class slotd)
101             (get-optimized-std-accessor-method-function class slotd type))
102       (setf (slot-accessor-std-p slotd type) std-p)
103       (setf (slot-accessor-function slotd type) function))
104     (when (and old-slotd (not (eq old-std-p (slot-accessor-std-p slotd 'all))))
105       (push (cons class name) *pv-table-cache-update-info*))))
106
107 (defmethod slot-definition-allocation ((slotd structure-slot-definition))
108   :instance)
109 \f
110 (defmethod shared-initialize :after ((object documentation-mixin)
111                                      slot-names
112                                      &key (documentation nil documentation-p))
113   (declare (ignore slot-names))
114   (when documentation-p
115     (setf (plist-value object 'documentation) documentation)))
116
117 ;;; default if DOC-TYPE doesn't match one of the specified types
118 (defmethod documentation (object doc-type)
119   (warn "unsupported DOCUMENTATION: type ~S for object ~S"
120         doc-type
121         (type-of object))
122   nil)
123
124 ;;; default if DOC-TYPE doesn't match one of the specified types
125 (defmethod (setf documentation) (new-value object doc-type)
126   ;; CMU CL made this an error, but since ANSI says that even for supported
127   ;; doc types an implementation is permitted to discard docs at any time
128   ;; for any reason, this feels to me more like a warning. -- WHN 19991214
129   (warn "discarding unsupported DOCUMENTATION of type ~S for object ~S"
130         doc-type
131         (type-of object))
132   new-value)
133
134 (defmethod documentation ((object documentation-mixin) doc-type)
135   (declare (ignore doc-type))
136   (plist-value object 'documentation))
137
138 (defmethod (setf documentation) (new-value
139                                  (object documentation-mixin)
140                                  doc-type)
141   (declare (ignore doc-type))
142   (setf (plist-value object 'documentation) new-value))
143
144 (defmethod documentation ((slotd standard-slot-definition) doc-type)
145   (declare (ignore doc-type))
146   (slot-value slotd 'documentation))
147
148 (defmethod (setf documentation) (new-value
149                                  (slotd standard-slot-definition)
150                                  doc-type)
151   (declare (ignore doc-type))
152   (setf (slot-value slotd 'documentation) new-value))
153 \f
154 ;;;; various class accessors that are a little more complicated than can be
155 ;;;; done with automatically generated reader methods
156
157 (defmethod class-finalized-p ((class pcl-class))
158   (with-slots (wrapper) class
159     (not (null wrapper))))
160
161 (defmethod class-prototype ((class std-class))
162   (with-slots (prototype) class
163     (or prototype (setq prototype (allocate-instance class)))))
164
165 (defmethod class-prototype ((class structure-class))
166   (with-slots (prototype wrapper defstruct-constructor) class
167     (or prototype
168         (setq prototype
169               (if defstruct-constructor
170                   (allocate-instance class)
171                   (allocate-standard-instance wrapper))))))
172
173 (defmethod class-direct-default-initargs ((class slot-class))
174   (plist-value class 'direct-default-initargs))
175
176 (defmethod class-default-initargs ((class slot-class))
177   (plist-value class 'default-initargs))
178
179 (defmethod class-constructors ((class slot-class))
180   (plist-value class 'constructors))
181
182 (defmethod class-slot-cells ((class std-class))
183   (plist-value class 'class-slot-cells))
184 \f
185 ;;;; class accessors that are even a little bit more complicated than those
186 ;;;; above. These have a protocol for updating them, we must implement that
187 ;;;; protocol.
188
189 ;;; Maintaining the direct subclasses backpointers. The update methods are
190 ;;; here, the values are read by an automatically generated reader method.
191 (defmethod add-direct-subclass ((class class) (subclass class))
192   (with-slots (direct-subclasses) class
193     (pushnew subclass direct-subclasses)
194     subclass))
195 (defmethod remove-direct-subclass ((class class) (subclass class))
196   (with-slots (direct-subclasses) class
197     (setq direct-subclasses (remove subclass direct-subclasses))
198     subclass))
199
200 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
201 ;;;
202 ;;; There are four generic functions involved, each has one method for the
203 ;;; class case and another method for the damned EQL specializers. All of
204 ;;; these are specified methods and appear in their specified place in the
205 ;;; class graph.
206 ;;;
207 ;;;   ADD-DIRECT-METHOD
208 ;;;   REMOVE-DIRECT-METHOD
209 ;;;   SPECIALIZER-DIRECT-METHODS
210 ;;;   SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
211 ;;;
212 ;;; In each case, we maintain one value which is a cons. The car is the list
213 ;;; methods. The cdr is a list of the generic functions. The cdr is always
214 ;;; computed lazily.
215 (defmethod add-direct-method ((specializer class) (method method))
216   (with-slots (direct-methods) specializer
217     (setf (car direct-methods) (adjoin method (car direct-methods))     ;PUSH
218           (cdr direct-methods) ()))
219   method)
220 (defmethod remove-direct-method ((specializer class) (method method))
221   (with-slots (direct-methods) specializer
222     (setf (car direct-methods) (remove method (car direct-methods))
223           (cdr direct-methods) ()))
224   method)
225
226 (defmethod specializer-direct-methods ((specializer class))
227   (with-slots (direct-methods) specializer
228     (car direct-methods)))
229
230 (defmethod specializer-direct-generic-functions ((specializer class))
231   (with-slots (direct-methods) specializer
232     (or (cdr direct-methods)
233         (setf (cdr direct-methods)
234               (gathering1 (collecting-once)
235                 (dolist (m (car direct-methods))
236                   (gather1 (method-generic-function m))))))))
237 \f
238 ;;; This hash table is used to store the direct methods and direct generic
239 ;;; functions of EQL specializers. Each value in the table is the cons.
240 (defvar *eql-specializer-methods* (make-hash-table :test 'eql))
241 (defvar *class-eq-specializer-methods* (make-hash-table :test 'eq))
242
243 (defmethod specializer-method-table ((specializer eql-specializer))
244   *eql-specializer-methods*)
245
246 (defmethod specializer-method-table ((specializer class-eq-specializer))
247   *class-eq-specializer-methods*)
248
249 (defmethod add-direct-method ((specializer specializer-with-object) (method method))
250   (let* ((object (specializer-object specializer))
251          (table (specializer-method-table specializer))
252          (entry (gethash object table)))
253     (unless entry
254       (setq entry
255             (setf (gethash object table)
256                   (cons nil nil))))
257     (setf (car entry) (adjoin method (car entry))
258           (cdr entry) ())
259     method))
260
261 (defmethod remove-direct-method ((specializer specializer-with-object) (method method))
262   (let* ((object (specializer-object specializer))
263          (entry (gethash object (specializer-method-table specializer))))
264     (when entry
265       (setf (car entry) (remove method (car entry))
266             (cdr entry) ()))
267     method))
268
269 (defmethod specializer-direct-methods ((specializer specializer-with-object))
270   (car (gethash (specializer-object specializer)
271                 (specializer-method-table specializer))))
272
273 (defmethod specializer-direct-generic-functions ((specializer specializer-with-object))
274   (let* ((object (specializer-object specializer))
275          (entry (gethash object (specializer-method-table specializer))))
276     (when entry
277       (or (cdr entry)
278           (setf (cdr entry)
279                 (gathering1 (collecting-once)
280                   (dolist (m (car entry))
281                     (gather1 (method-generic-function m)))))))))
282
283 (defun map-specializers (function)
284   (map-all-classes #'(lambda (class)
285                        (funcall function (class-eq-specializer class))
286                        (funcall function class)))
287   (maphash #'(lambda (object methods)
288                (declare (ignore methods))
289                (intern-eql-specializer object))
290            *eql-specializer-methods*)
291   (maphash #'(lambda (object specl)
292                (declare (ignore object))
293                (funcall function specl))
294            *eql-specializer-table*)
295   nil)
296
297 (defun map-all-generic-functions (function)
298   (let ((all-generic-functions (make-hash-table :test 'eq)))
299     (map-specializers #'(lambda (specl)
300                           (dolist (gf (specializer-direct-generic-functions specl))
301                             (unless (gethash gf all-generic-functions)
302                               (setf (gethash gf all-generic-functions) t)
303                               (funcall function gf))))))
304   nil)
305
306 (defmethod shared-initialize :after ((specl class-eq-specializer) slot-names &key)
307   (declare (ignore slot-names))
308   (setf (slot-value specl 'type) `(class-eq ,(specializer-class specl))))
309
310 (defmethod shared-initialize :after ((specl eql-specializer) slot-names &key)
311   (declare (ignore slot-names))
312   (setf (slot-value specl 'type) `(eql ,(specializer-object specl))))
313 \f
314 (defun real-load-defclass (name metaclass-name supers slots other accessors)
315   (do-standard-defsetfs-for-defclass accessors)                 ;***
316   (let ((res (apply #'ensure-class name :metaclass metaclass-name
317                     :direct-superclasses supers
318                     :direct-slots slots
319                     :definition-source `((defclass ,name)
320                                          ,*load-truename*)
321                     other)))
322     ;; Defclass of a class with a forward-referenced superclass does not
323     ;; have a wrapper. RES is the incomplete PCL class. The Lisp class
324     ;; does not yet exist. Maybe should return NIL in that case as RES
325     ;; is not useful to the user?
326     (and (class-wrapper res) (sb-kernel:layout-class (class-wrapper res)))))
327
328 (setf (gdefinition 'load-defclass) #'real-load-defclass)
329
330 (defun ensure-class (name &rest all)
331   (apply #'ensure-class-using-class name (find-class name nil) all))
332
333 (defmethod ensure-class-using-class (name (class null) &rest args &key)
334   (multiple-value-bind (meta initargs)
335       (ensure-class-values class args)
336     (inform-type-system-about-class (class-prototype meta) name);***
337     (setf class (apply #'make-instance meta :name name initargs)
338           (find-class name) class)
339     (inform-type-system-about-class class name)                 ;***
340     class))
341
342 (defmethod ensure-class-using-class (name (class pcl-class) &rest args &key)
343   (multiple-value-bind (meta initargs)
344       (ensure-class-values class args)
345     (unless (eq (class-of class) meta) (change-class class meta))
346     (apply #'reinitialize-instance class initargs)
347     (setf (find-class name) class)
348     (inform-type-system-about-class class name)                 ;***
349     class))
350
351 (defmethod class-predicate-name ((class t))
352   'function-returning-nil)
353
354 (defun ensure-class-values (class args)
355   (let* ((initargs (copy-list args))
356          (unsupplied (list 1))
357          (supplied-meta   (getf initargs :metaclass unsupplied))
358          (supplied-supers (getf initargs :direct-superclasses unsupplied))
359          (supplied-slots  (getf initargs :direct-slots unsupplied))
360          (meta
361            (cond ((neq supplied-meta unsupplied)
362                   (find-class supplied-meta))
363                  ((or (null class)
364                       (forward-referenced-class-p class))
365                   *the-class-standard-class*)
366                  (t
367                   (class-of class)))))
368     (flet ((fix-super (s)
369              (cond ((classp s) s)
370                    ((not (legal-class-name-p s))
371                     (error "~S is not a class or a legal class name." s))
372                    (t
373                     (or (find-class s nil)
374                         (setf (find-class s)
375                               (make-instance 'forward-referenced-class
376                                              :name s)))))))
377       (loop (unless (remf initargs :metaclass) (return)))
378       (loop (unless (remf initargs :direct-superclasses) (return)))
379       (loop (unless (remf initargs :direct-slots) (return)))
380       (values meta
381               (list* :direct-superclasses
382                      (and (neq supplied-supers unsupplied)
383                           (mapcar #'fix-super supplied-supers))
384                      :direct-slots
385                      (and (neq supplied-slots unsupplied) supplied-slots)
386                      initargs)))))
387 \f
388 #|| ; since it doesn't do anything
389 (defmethod shared-initialize :before ((class std-class)
390                                       slot-names
391                                       &key direct-superclasses)
392   (declare (ignore slot-names))
393   ;; *** error checking
394   )
395 ||#
396
397 (defmethod shared-initialize :after
398            ((class std-class)
399             slot-names
400             &key (direct-superclasses nil direct-superclasses-p)
401                  (direct-slots nil direct-slots-p)
402                  (direct-default-initargs nil direct-default-initargs-p)
403                  (predicate-name nil predicate-name-p))
404   (declare (ignore slot-names))
405   (cond (direct-superclasses-p
406          (setq direct-superclasses
407                (or direct-superclasses
408                    (list (if (funcallable-standard-class-p class)
409                              *the-class-funcallable-standard-object*
410                              *the-class-standard-object*))))
411          (dolist (superclass direct-superclasses)
412            (unless (validate-superclass class superclass)
413              (error "The class ~S was specified as a~%
414                      super-class of the class ~S;~%~
415                      but the meta-classes ~S and~%~S are incompatible.~@
416                      Define a method for ~S to avoid this error."
417                      superclass class (class-of superclass) (class-of class)
418                      'validate-superclass)))
419          (setf (slot-value class 'direct-superclasses) direct-superclasses))
420         (t
421          (setq direct-superclasses (slot-value class 'direct-superclasses))))
422   (setq direct-slots
423         (if direct-slots-p
424             (setf (slot-value class 'direct-slots)
425                   (mapcar #'(lambda (pl) (make-direct-slotd class pl)) direct-slots))
426             (slot-value class 'direct-slots)))
427   (if direct-default-initargs-p
428       (setf (plist-value class 'direct-default-initargs) direct-default-initargs)
429       (setq direct-default-initargs (plist-value class 'direct-default-initargs)))
430   (setf (plist-value class 'class-slot-cells)
431         (gathering1 (collecting)
432           (dolist (dslotd direct-slots)
433             (when (eq (slot-definition-allocation dslotd) class)
434               (let ((initfunction (slot-definition-initfunction dslotd)))
435                 (gather1 (cons (slot-definition-name dslotd)
436                                (if initfunction
437                                    (funcall initfunction)
438                                    *slot-unbound*))))))))
439   (setq predicate-name (if predicate-name-p
440                            (setf (slot-value class 'predicate-name)
441                                  (car predicate-name))
442                            (or (slot-value class 'predicate-name)
443                                (setf (slot-value class 'predicate-name)
444                                      (make-class-predicate-name (class-name class))))))
445   (add-direct-subclasses class direct-superclasses)
446   (update-class class nil)
447   (make-class-predicate class predicate-name)
448   (add-slot-accessors class direct-slots))
449
450 (defmethod shared-initialize :before ((class class) slot-names &key name)
451   (declare (ignore slot-names name))
452   (setf (slot-value class 'type) `(class ,class))
453   (setf (slot-value class 'class-eq-specializer)
454         (make-instance 'class-eq-specializer :class class)))
455
456 (defmethod reinitialize-instance :before ((class slot-class) &key)
457   (remove-direct-subclasses class (class-direct-superclasses class))
458   (remove-slot-accessors    class (class-direct-slots class)))
459
460 (defmethod reinitialize-instance :after ((class slot-class)
461                                          &rest initargs
462                                          &key)
463   (map-dependents class
464                   #'(lambda (dependent)
465                       (apply #'update-dependent class dependent initargs))))
466
467 (defmethod shared-initialize :after
468       ((class structure-class)
469        slot-names
470        &key (direct-superclasses nil direct-superclasses-p)
471             (direct-slots nil direct-slots-p)
472             direct-default-initargs
473             (predicate-name nil predicate-name-p))
474   (declare (ignore slot-names direct-default-initargs))
475   (if direct-superclasses-p
476       (setf (slot-value class 'direct-superclasses)
477             (or direct-superclasses
478                 (setq direct-superclasses
479                       (and (not (eq (class-name class) 'structure-object))
480                            (list *the-class-structure-object*)))))
481       (setq direct-superclasses (slot-value class 'direct-superclasses)))
482   (let* ((name (class-name class))
483          (from-defclass-p (slot-value class 'from-defclass-p))
484          (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
485     (if direct-slots-p
486         (setf (slot-value class 'direct-slots)
487               (setq direct-slots
488                     (mapcar #'(lambda (pl)
489                                 (when defstruct-p
490                                   (let* ((slot-name (getf pl :name))
491                                          (acc-name (format nil "~S structure class ~A"
492                                                            name slot-name))
493                                          (accessor (intern acc-name)))
494                                     (setq pl (list* :defstruct-accessor-symbol accessor
495                                                     pl))))
496                                 (make-direct-slotd class pl))
497                             direct-slots)))
498         (setq direct-slots (slot-value class 'direct-slots)))
499     (when defstruct-p
500       (let* ((include (car (slot-value class 'direct-superclasses)))
501              (conc-name (intern (format nil "~S structure class " name)))
502              (constructor (intern (format nil "~A constructor" conc-name)))
503              (defstruct `(defstruct (,name
504                                       ,@(when include
505                                           `((:include ,(class-name include))))
506                                       (:print-function print-std-instance)
507                                       (:predicate nil)
508                                       (:conc-name ,conc-name)
509                                       (:constructor ,constructor ()))
510                            ,@(mapcar #'(lambda (slot)
511                                          `(,(slot-definition-name slot)
512                                            *slot-unbound*))
513                                      direct-slots)))
514              (reader-names (mapcar #'(lambda (slotd)
515                                        (intern (format nil "~A~A reader" conc-name
516                                                        (slot-definition-name slotd))))
517                                    direct-slots))
518              (writer-names (mapcar #'(lambda (slotd)
519                                        (intern (format nil "~A~A writer" conc-name
520                                                        (slot-definition-name slotd))))
521                                    direct-slots))
522              (readers-init
523               (mapcar #'(lambda (slotd reader-name)
524                           (let ((accessor
525                                  (slot-definition-defstruct-accessor-symbol slotd)))
526                             `(defun ,reader-name (obj)
527                                (declare (type ,name obj))
528                                (,accessor obj))))
529                       direct-slots reader-names))
530              (writers-init
531               (mapcar #'(lambda (slotd writer-name)
532                           (let ((accessor
533                                  (slot-definition-defstruct-accessor-symbol slotd)))
534                             `(defun ,writer-name (nv obj)
535                                (declare (type ,name obj))
536                                (setf (,accessor obj) nv))))
537                       direct-slots writer-names))
538              (defstruct-form
539                `(progn
540                   ,defstruct
541                   ,@readers-init ,@writers-init
542                   (declare-structure ',name nil nil))))
543         (unless (structure-type-p name) (eval defstruct-form))
544         (mapc #'(lambda (dslotd reader-name writer-name)
545                   (let* ((reader (gdefinition reader-name))
546                          (writer (when (gboundp writer-name)
547                                    (gdefinition writer-name))))
548                     (setf (slot-value dslotd 'internal-reader-function) reader)
549                     (setf (slot-value dslotd 'internal-writer-function) writer)))
550               direct-slots reader-names writer-names)
551         (setf (slot-value class 'defstruct-form) defstruct-form)
552         (setf (slot-value class 'defstruct-constructor) constructor))))
553   (add-direct-subclasses class direct-superclasses)
554   (setf (slot-value class 'class-precedence-list)
555         (compute-class-precedence-list class))
556   (setf (slot-value class 'slots) (compute-slots class))
557   (let ((lclass (cl:find-class (class-name class))))
558     (setf (sb-kernel:class-pcl-class lclass) class)
559     (setf (slot-value class 'wrapper) (sb-kernel:class-layout lclass)))
560   (update-pv-table-cache-info class)
561   (setq predicate-name (if predicate-name-p
562                            (setf (slot-value class 'predicate-name)
563                                  (car predicate-name))
564                            (or (slot-value class 'predicate-name)
565                                (setf (slot-value class 'predicate-name)
566                                      (make-class-predicate-name (class-name class))))))
567   (make-class-predicate class predicate-name)
568   (add-slot-accessors class direct-slots))
569
570 (defmethod direct-slot-definition-class ((class structure-class) initargs)
571   (declare (ignore initargs))
572   (find-class 'structure-direct-slot-definition))
573
574 (defmethod finalize-inheritance ((class structure-class))
575   nil) ; always finalized
576 \f
577 (defun add-slot-accessors (class dslotds)
578   (fix-slot-accessors class dslotds 'add))
579
580 (defun remove-slot-accessors (class dslotds)
581   (fix-slot-accessors class dslotds 'remove))
582
583 (defun fix-slot-accessors (class dslotds add/remove)
584   (flet ((fix (gfspec name r/w)
585            (let ((gf (ensure-generic-function gfspec)))
586              (case r/w
587                (r (if (eq add/remove 'add)
588                       (add-reader-method class gf name)
589                       (remove-reader-method class gf)))
590                (w (if (eq add/remove 'add)
591                       (add-writer-method class gf name)
592                       (remove-writer-method class gf)))))))
593     (dolist (dslotd dslotds)
594       (let ((slot-name (slot-definition-name dslotd)))
595         (dolist (r (slot-definition-readers dslotd)) (fix r slot-name 'r))
596         (dolist (w (slot-definition-writers dslotd)) (fix w slot-name 'w))))))
597 \f
598 (defun add-direct-subclasses (class new)
599   (dolist (n new)
600     (unless (memq class (class-direct-subclasses class))
601       (add-direct-subclass n class))))
602
603 (defun remove-direct-subclasses (class new)
604   (let ((old (class-direct-superclasses class)))
605     (dolist (o (set-difference old new))
606       (remove-direct-subclass o class))))
607 \f
608 (defmethod finalize-inheritance ((class std-class))
609   (update-class class t))
610 \f
611 (defun class-has-a-forward-referenced-superclass-p (class)
612   (or (forward-referenced-class-p class)
613       (some #'class-has-a-forward-referenced-superclass-p
614             (class-direct-superclasses class))))
615
616 ;;; This is called by :after shared-initialize whenever a class is initialized
617 ;;; or reinitialized. The class may or may not be finalized.
618 (defun update-class (class finalizep)
619   (when (or finalizep (class-finalized-p class)
620             (not (class-has-a-forward-referenced-superclass-p class)))
621     (update-cpl class (compute-class-precedence-list class))
622     (update-slots class (compute-slots class))
623     (update-gfs-of-class class)
624     (update-inits class (compute-default-initargs class))
625     (update-make-instance-function-table class))
626   (unless finalizep
627     (dolist (sub (class-direct-subclasses class)) (update-class sub nil))))
628
629 (defun update-cpl (class cpl)
630   (if (class-finalized-p class)
631       (unless (equal (class-precedence-list class) cpl)
632         ;; comment from the old CMU CL sources:
633         ;;   Need to have the cpl setup before update-lisp-class-layout
634         ;;   is called on CMU CL.
635         (setf (slot-value class 'class-precedence-list) cpl)
636         (force-cache-flushes class))
637       (setf (slot-value class 'class-precedence-list) cpl))
638   (update-class-can-precede-p cpl))
639
640 (defun update-class-can-precede-p (cpl)
641   (when cpl
642     (let ((first (car cpl)))
643       (dolist (c (cdr cpl))
644         (pushnew c (slot-value first 'can-precede-list))))
645     (update-class-can-precede-p (cdr cpl))))
646
647 (defun class-can-precede-p (class1 class2)
648   (member class2 (class-can-precede-list class1)))
649
650 (defun update-slots (class eslotds)
651   (let ((instance-slots ())
652         (class-slots    ()))
653     (dolist (eslotd eslotds)
654       (let ((alloc (slot-definition-allocation eslotd)))
655         (cond ((eq alloc :instance) (push eslotd instance-slots))
656               ((classp alloc)       (push eslotd class-slots)))))
657
658     ;; If there is a change in the shape of the instances then the
659     ;; old class is now obsolete.
660     (let* ((nlayout (mapcar #'slot-definition-name
661                             (sort instance-slots #'< :key #'slot-definition-location)))
662            (nslots (length nlayout))
663            (nwrapper-class-slots (compute-class-slots class-slots))
664            (owrapper (class-wrapper class))
665            (olayout (and owrapper (wrapper-instance-slots-layout owrapper)))
666            (owrapper-class-slots (and owrapper (wrapper-class-slots owrapper)))
667            (nwrapper
668             (cond ((null owrapper)
669                    (make-wrapper nslots class))
670                   ((and (equal nlayout olayout)
671                         (not
672                          (iterate ((o (list-elements owrapper-class-slots))
673                                    (n (list-elements nwrapper-class-slots)))
674                                   (unless (eq (car o) (car n)) (return t)))))
675                    owrapper)
676                   (t
677                    ;; This will initialize the new wrapper to have the same
678                    ;; state as the old wrapper. We will then have to change
679                    ;; that. This may seem like wasted work (it is), but the
680                    ;; spec requires that we call make-instances-obsolete.
681                    (make-instances-obsolete class)
682                    (class-wrapper class)))))
683
684       (with-slots (wrapper slots) class
685         (update-lisp-class-layout class nwrapper)
686         (setf slots eslotds
687               (wrapper-instance-slots-layout nwrapper) nlayout
688               (wrapper-class-slots nwrapper) nwrapper-class-slots
689               (wrapper-no-of-instance-slots nwrapper) nslots
690               wrapper nwrapper))
691
692       (unless (eq owrapper nwrapper)
693         (update-pv-table-cache-info class)))))
694
695 (defun compute-class-slots (eslotds)
696   (gathering1 (collecting)
697     (dolist (eslotd eslotds)
698       (gather1
699         (assoc (slot-definition-name eslotd)
700                (class-slot-cells (slot-definition-allocation eslotd)))))))
701
702 (defun compute-layout (cpl instance-eslotds)
703   (let* ((names
704            (gathering1 (collecting)
705              (dolist (eslotd instance-eslotds)
706                (when (eq (slot-definition-allocation eslotd) :instance)
707                  (gather1 (slot-definition-name eslotd))))))
708          (order ()))
709     (labels ((rwalk (tail)
710                (when tail
711                  (rwalk (cdr tail))
712                  (dolist (ss (class-slots (car tail)))
713                    (let ((n (slot-definition-name ss)))
714                      (when (member n names)
715                        (setq order (cons n order)
716                              names (remove n names))))))))
717       (rwalk (if (slot-boundp (car cpl) 'slots)
718                  cpl
719                  (cdr cpl)))
720       (reverse (append names order)))))
721
722 (defun update-gfs-of-class (class)
723   (when (and (class-finalized-p class)
724              (let ((cpl (class-precedence-list class)))
725                (or (member *the-class-slot-class* cpl)
726                    (member *the-class-standard-effective-slot-definition* cpl))))
727     (let ((gf-table (make-hash-table :test 'eq)))
728       (labels ((collect-gfs (class)
729                  (dolist (gf (specializer-direct-generic-functions class))
730                    (setf (gethash gf gf-table) t))
731                  (mapc #'collect-gfs (class-direct-superclasses class))))
732         (collect-gfs class)
733         (maphash #'(lambda (gf ignore)
734                      (declare (ignore ignore))
735                      (update-gf-dfun class gf))
736                  gf-table)))))
737
738 (defun update-inits (class inits)
739   (setf (plist-value class 'default-initargs) inits))
740 \f
741 (defmethod compute-default-initargs ((class slot-class))
742   (let ((cpl (class-precedence-list class))
743         (direct (class-direct-default-initargs class)))
744     (labels ((walk (tail)
745                (if (null tail)
746                    nil
747                    (let ((c (pop tail)))
748                      (append (if (eq c class)
749                                  direct
750                                  (class-direct-default-initargs c))
751                              (walk tail))))))
752       (let ((initargs (walk cpl)))
753         (delete-duplicates initargs :test #'eq :key #'car :from-end t)))))
754 \f
755 ;;;; protocols for constructing direct and effective slot definitions
756
757 (defmethod direct-slot-definition-class ((class std-class) initargs)
758   (declare (ignore initargs))
759   (find-class 'standard-direct-slot-definition))
760
761 (defun make-direct-slotd (class initargs)
762   (let ((initargs (list* :class class initargs)))
763     (apply #'make-instance
764            (direct-slot-definition-class class initargs)
765            initargs)))
766
767 (defmethod compute-slots ((class std-class))
768   ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
769   ;; for each different slot name we find in our superclasses. Each
770   ;; call receives the class and a list of the dslotds with that name.
771   ;; The list is in most-specific-first order.
772   (let ((name-dslotds-alist ()))
773     (dolist (c (class-precedence-list class))
774       (let ((dslotds (class-direct-slots c)))
775         (dolist (d dslotds)
776           (let* ((name (slot-definition-name d))
777                  (entry (assq name name-dslotds-alist)))
778             (if entry
779                 (push d (cdr entry))
780                 (push (list name d) name-dslotds-alist))))))
781     (mapcar #'(lambda (direct)
782                 (compute-effective-slot-definition class
783                                                    (nreverse (cdr direct))))
784             name-dslotds-alist)))
785
786 (defmethod compute-slots :around ((class std-class))
787   (let ((eslotds (call-next-method))
788         (cpl (class-precedence-list class))
789         (instance-slots ())
790         (class-slots    ()))
791     (dolist (eslotd eslotds)
792       (let ((alloc (slot-definition-allocation eslotd)))
793         (cond ((eq alloc :instance) (push eslotd instance-slots))
794               ((classp alloc)       (push eslotd class-slots)))))
795     (let ((nlayout (compute-layout cpl instance-slots)))
796       (dolist (eslotd instance-slots)
797         (setf (slot-definition-location eslotd)
798               (position (slot-definition-name eslotd) nlayout))))
799     (dolist (eslotd class-slots)
800       (setf (slot-definition-location eslotd)
801             (assoc (slot-definition-name eslotd)
802                    (class-slot-cells (slot-definition-allocation eslotd)))))
803     (mapc #'initialize-internal-slot-functions eslotds)
804     eslotds))
805
806 (defmethod compute-slots ((class structure-class))
807   (mapcan #'(lambda (superclass)
808               (mapcar #'(lambda (dslotd)
809                           (compute-effective-slot-definition class
810                                                              (list dslotd)))
811                       (class-direct-slots superclass)))
812           (reverse (slot-value class 'class-precedence-list))))
813
814 (defmethod compute-slots :around ((class structure-class))
815   (let ((eslotds (call-next-method)))
816     (mapc #'initialize-internal-slot-functions eslotds)
817     eslotds))
818
819 (defmethod compute-effective-slot-definition ((class slot-class) dslotds)
820   (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
821          (class (effective-slot-definition-class class initargs)))
822     (apply #'make-instance class initargs)))
823
824 (defmethod effective-slot-definition-class ((class std-class) initargs)
825   (declare (ignore initargs))
826   (find-class 'standard-effective-slot-definition))
827
828 (defmethod effective-slot-definition-class ((class structure-class) initargs)
829   (declare (ignore initargs))
830   (find-class 'structure-effective-slot-definition))
831
832 (defmethod compute-effective-slot-definition-initargs
833     ((class slot-class) direct-slotds)
834   (let* ((name nil)
835          (initfunction nil)
836          (initform nil)
837          (initargs nil)
838          (allocation nil)
839          (type t)
840          (namep  nil)
841          (initp  nil)
842          (allocp nil))
843
844     (dolist (slotd direct-slotds)
845       (when slotd
846         (unless namep
847           (setq name (slot-definition-name slotd)
848                 namep t))
849         (unless initp
850           (when (slot-definition-initfunction slotd)
851             (setq initform (slot-definition-initform slotd)
852                   initfunction (slot-definition-initfunction slotd)
853                   initp t)))
854         (unless allocp
855           (setq allocation (slot-definition-allocation slotd)
856                 allocp t))
857         (setq initargs (append (slot-definition-initargs slotd) initargs))
858         (let ((slotd-type (slot-definition-type slotd)))
859           (setq type (cond ((eq type 't) slotd-type)
860                            ((*subtypep type slotd-type) type)
861                            (t `(and ,type ,slotd-type)))))))
862     (list :name name
863           :initform initform
864           :initfunction initfunction
865           :initargs initargs
866           :allocation allocation
867           :type type
868           :class class)))
869
870 (defmethod compute-effective-slot-definition-initargs :around
871     ((class structure-class) direct-slotds)
872   (let ((slotd (car direct-slotds)))
873     (list* :defstruct-accessor-symbol (slot-definition-defstruct-accessor-symbol slotd)
874            :internal-reader-function (slot-definition-internal-reader-function slotd)
875            :internal-writer-function (slot-definition-internal-writer-function slotd)
876            (call-next-method))))
877 \f
878 ;;; NOTE: For bootstrapping considerations, these can't use make-instance
879 ;;;       to make the method object. They have to use make-a-method which
880 ;;;       is a specially bootstrapped mechanism for making standard methods.
881 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
882   (declare (ignore direct-slot initargs))
883   (find-class 'standard-reader-method))
884
885 (defmethod add-reader-method ((class slot-class) generic-function slot-name)
886   (add-method generic-function
887               (make-a-method 'standard-reader-method
888                              ()
889                              (list (or (class-name class) 'object))
890                              (list class)
891                              (make-reader-method-function class slot-name)
892                              "automatically generated reader method"
893                              slot-name)))
894
895 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
896   (declare (ignore direct-slot initargs))
897   (find-class 'standard-writer-method))
898
899 (defmethod add-writer-method ((class slot-class) generic-function slot-name)
900   (add-method generic-function
901               (make-a-method 'standard-writer-method
902                              ()
903                              (list 'new-value (or (class-name class) 'object))
904                              (list *the-class-t* class)
905                              (make-writer-method-function class slot-name)
906                              "automatically generated writer method"
907                              slot-name)))
908
909 (defmethod add-boundp-method ((class slot-class) generic-function slot-name)
910   (add-method generic-function
911               (make-a-method 'standard-boundp-method
912                              ()
913                              (list (or (class-name class) 'object))
914                              (list class)
915                              (make-boundp-method-function class slot-name)
916                              "automatically generated boundp method"
917                              slot-name)))
918
919 (defmethod remove-reader-method ((class slot-class) generic-function)
920   (let ((method (get-method generic-function () (list class) nil)))
921     (when method (remove-method generic-function method))))
922
923 (defmethod remove-writer-method ((class slot-class) generic-function)
924   (let ((method
925           (get-method generic-function () (list *the-class-t* class) nil)))
926     (when method (remove-method generic-function method))))
927
928 (defmethod remove-boundp-method ((class slot-class) generic-function)
929   (let ((method (get-method generic-function () (list class) nil)))
930     (when method (remove-method generic-function method))))
931 \f
932 ;;; make-reader-method-function and make-write-method function are NOT part of
933 ;;; the standard protocol. They are however useful, PCL makes uses makes use
934 ;;; of them internally and documents them for PCL users.
935 ;;;
936 ;;; *** This needs work to make type testing by the writer functions which
937 ;;; *** do type testing faster. The idea would be to have one constructor
938 ;;; *** for each possible type test. In order to do this it would be nice
939 ;;; *** to have help from inform-type-system-about-class and friends.
940 ;;;
941 ;;; *** There is a subtle bug here which is going to have to be fixed.
942 ;;; *** Namely, the simplistic use of the template has to be fixed. We
943 ;;; *** have to give the optimize-slot-value method the user might have
944 ;;; *** defined for this metclass a chance to run.
945
946 (defmethod make-reader-method-function ((class slot-class) slot-name)
947   (make-std-reader-method-function (class-name class) slot-name))
948
949 (defmethod make-writer-method-function ((class slot-class) slot-name)
950   (make-std-writer-method-function (class-name class) slot-name))
951
952 (defmethod make-boundp-method-function ((class slot-class) slot-name)
953   (make-std-boundp-method-function (class-name class) slot-name))
954 \f
955 ;;;; inform-type-system-about-class
956 ;;;; make-type-predicate
957 ;;;
958 ;;; These are NOT part of the standard protocol. They are internal mechanism
959 ;;; which PCL uses to *try* and tell the type system about class definitions.
960 ;;; In a more fully integrated implementation of CLOS, the type system would
961 ;;; know about class objects and class names in a more fundamental way and
962 ;;; the mechanism used to inform the type system about new classes would be
963 ;;; different.
964 (defmethod inform-type-system-about-class ((class std-class) name)
965   (inform-type-system-about-std-class name))
966 \f
967 (defmethod compatible-meta-class-change-p (class proto-new-class)
968   (eq (class-of class) (class-of proto-new-class)))
969
970 (defmethod validate-superclass ((class class) (new-super class))
971   (or (eq new-super *the-class-t*)
972       (eq (class-of class) (class-of new-super))))
973
974 (defmethod validate-superclass ((class standard-class) (new-super std-class))
975   (let ((new-super-meta-class (class-of new-super)))
976     (or (eq new-super-meta-class *the-class-std-class*)
977         (eq (class-of class) new-super-meta-class))))
978 \f
979 (defun force-cache-flushes (class)
980   (let* ((owrapper (class-wrapper class))
981          (state (wrapper-state owrapper)))
982     ;; We only need to do something if the state is still T. If the
983     ;; state isn't T, it will be FLUSH or OBSOLETE, and both of those
984     ;; will already be doing what we want. In particular, we must be
985     ;; sure we never change an OBSOLETE into a FLUSH since OBSOLETE
986     ;; means do what FLUSH does and then some.
987     (when (eq state 't) ; FIXME: should be done through INVALID-WRAPPER-P
988       (let ((nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
989                                     class)))
990         (setf (wrapper-instance-slots-layout nwrapper)
991               (wrapper-instance-slots-layout owrapper))
992         (setf (wrapper-class-slots nwrapper)
993               (wrapper-class-slots owrapper))
994         (without-interrupts
995           (update-lisp-class-layout class nwrapper)
996           (setf (slot-value class 'wrapper) nwrapper)
997           (invalidate-wrapper owrapper ':flush nwrapper))))))
998
999 (defun flush-cache-trap (owrapper nwrapper instance)
1000   (declare (ignore owrapper))
1001   (set-wrapper instance nwrapper))
1002 \f
1003 ;;; make-instances-obsolete can be called by user code. It will cause the
1004 ;;; next access to the instance (as defined in 88-002R) to trap through the
1005 ;;; update-instance-for-redefined-class mechanism.
1006 (defmethod make-instances-obsolete ((class std-class))
1007   (let* ((owrapper (class-wrapper class))
1008          (nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1009                                  class)))
1010       (setf (wrapper-instance-slots-layout nwrapper)
1011             (wrapper-instance-slots-layout owrapper))
1012       (setf (wrapper-class-slots nwrapper)
1013             (wrapper-class-slots owrapper))
1014       (without-interrupts
1015         (update-lisp-class-layout class nwrapper)
1016         (setf (slot-value class 'wrapper) nwrapper)
1017         (invalidate-wrapper owrapper ':obsolete nwrapper)
1018         class)))
1019
1020 (defmethod make-instances-obsolete ((class symbol))
1021   (make-instances-obsolete (find-class class)))
1022
1023 ;;; obsolete-instance-trap is the internal trap that is called when we see
1024 ;;; an obsolete instance. The times when it is called are:
1025 ;;;   - when the instance is involved in method lookup
1026 ;;;   - when attempting to access a slot of an instance
1027 ;;;
1028 ;;; It is not called by class-of, wrapper-of, or any of the low-level instance
1029 ;;; access macros.
1030 ;;;
1031 ;;; Of course these times when it is called are an internal implementation
1032 ;;; detail of PCL and are not part of the documented description of when the
1033 ;;; obsolete instance update happens. The documented description is as it
1034 ;;; appears in 88-002R.
1035 ;;;
1036 ;;; This has to return the new wrapper, so it counts on all the methods on
1037 ;;; obsolete-instance-trap-internal to return the new wrapper. It also does
1038 ;;; a little internal error checking to make sure that the traps are only
1039 ;;; happening when they should, and that the trap methods are computing
1040 ;;; appropriate new wrappers.
1041
1042 ;;; obsolete-instance-trap might be called on structure instances
1043 ;;; after a structure is redefined. In most cases, obsolete-instance-trap
1044 ;;; will not be able to fix the old instance, so it must signal an
1045 ;;; error. The hard part of this is that the error system and debugger
1046 ;;; might cause obsolete-instance-trap to be called again, so in that
1047 ;;; case, we have to return some reasonable wrapper, instead.
1048
1049 (defvar *in-obsolete-instance-trap* nil)
1050 (defvar *the-wrapper-of-structure-object*
1051   (class-wrapper (find-class 'structure-object)))
1052
1053 (define-condition obsolete-structure (error)
1054   ((datum :reader obsolete-structure-datum :initarg :datum))
1055   (:report
1056    (lambda (condition stream)
1057      ;; Don't try to print the structure, since it probably won't work.
1058      (format stream
1059              "obsolete structure error in ~S:~@
1060               for a structure of type: ~S"
1061              (sb-conditions::condition-function-name condition)
1062              (type-of (obsolete-structure-datum condition))))))
1063
1064 (defun obsolete-instance-trap (owrapper nwrapper instance)
1065   (if (not (pcl-instance-p instance))
1066       (if *in-obsolete-instance-trap*
1067           *the-wrapper-of-structure-object*
1068            (let ((*in-obsolete-instance-trap* t))
1069              (error 'obsolete-structure :datum instance)))
1070       (let* ((class (wrapper-class* nwrapper))
1071              (copy (allocate-instance class)) ;??? allocate-instance ???
1072              (olayout (wrapper-instance-slots-layout owrapper))
1073              (nlayout (wrapper-instance-slots-layout nwrapper))
1074              (oslots (get-slots instance))
1075              (nslots (get-slots copy))
1076              (oclass-slots (wrapper-class-slots owrapper))
1077              (added ())
1078              (discarded ())
1079              (plist ()))
1080         ;; local  --> local     transfer
1081         ;; local  --> shared       discard
1082         ;; local  -->  --         discard
1083         ;; shared --> local     transfer
1084         ;; shared --> shared       discard
1085         ;; shared -->  --         discard
1086         ;;  --    --> local     add
1087         ;;  --    --> shared    --
1088
1089         ;; Go through all the old local slots.
1090         (iterate ((name (list-elements olayout))
1091                   (opos (interval :from 0)))
1092           (let ((npos (posq name nlayout)))
1093             (if npos
1094                 (setf (instance-ref nslots npos) (instance-ref oslots opos))
1095                 (progn
1096                   (push name discarded)
1097                   (unless (eq (instance-ref oslots opos) *slot-unbound*)
1098                     (setf (getf plist name) (instance-ref oslots opos)))))))
1099
1100         ;; Go through all the old shared slots.
1101         (iterate ((oclass-slot-and-val (list-elements oclass-slots)))
1102           (let ((name (car oclass-slot-and-val))
1103                 (val (cdr oclass-slot-and-val)))
1104             (let ((npos (posq name nlayout)))
1105               (if npos
1106                   (setf (instance-ref nslots npos) (cdr oclass-slot-and-val))
1107                   (progn (push name discarded)
1108                          (unless (eq val *slot-unbound*)
1109                            (setf (getf plist name) val)))))))
1110
1111         ;; Go through all the new local slots to compute the added slots.
1112         (dolist (nlocal nlayout)
1113           (unless (or (memq nlocal olayout)
1114                       (assq nlocal oclass-slots))
1115             (push nlocal added)))
1116
1117         (swap-wrappers-and-slots instance copy)
1118
1119         (update-instance-for-redefined-class instance
1120                                              added
1121                                              discarded
1122                                              plist)
1123         nwrapper)))
1124 \f
1125 (defmacro copy-instance-internal (instance)
1126   `(progn
1127      (let* ((class (class-of instance))
1128             (copy (allocate-instance class)))
1129        (if (std-instance-p ,instance)
1130            (setf (std-instance-slots ,instance)
1131                  (std-instance-slots ,instance))
1132          (setf (fsc-instance-slots ,instance)
1133                (fsc-instance-slots ,instance)))
1134        copy)))
1135
1136 (defun change-class-internal (instance new-class)
1137   (let* ((old-class (class-of instance))
1138          (copy (allocate-instance new-class))
1139          (new-wrapper (get-wrapper copy))
1140          (old-wrapper (class-wrapper old-class))
1141          (old-layout (wrapper-instance-slots-layout old-wrapper))
1142          (new-layout (wrapper-instance-slots-layout new-wrapper))
1143          (old-slots (get-slots instance))
1144          (new-slots (get-slots copy))
1145          (old-class-slots (wrapper-class-slots old-wrapper)))
1146
1147     ;; "The values of local slots specified by both the class CTO and
1148     ;; CFROM are retained. If such a local slot was unbound, it remains
1149     ;; unbound."
1150     (iterate ((new-slot (list-elements new-layout))
1151               (new-position (interval :from 0)))
1152       (let ((old-position (posq new-slot old-layout)))
1153         (when old-position
1154           (setf (instance-ref new-slots new-position)
1155                 (instance-ref old-slots old-position)))))
1156
1157     ;; "The values of slots specified as shared in the class CFROM and
1158     ;; as local in the class CTO are retained."
1159     (iterate ((slot-and-val (list-elements old-class-slots)))
1160       (let ((position (posq (car slot-and-val) new-layout)))
1161         (when position
1162           (setf (instance-ref new-slots position) (cdr slot-and-val)))))
1163
1164     ;; Make the copy point to the old instance's storage, and make the
1165     ;; old instance point to the new storage.
1166     (swap-wrappers-and-slots instance copy)
1167
1168     (update-instance-for-different-class copy instance)
1169     instance))
1170
1171 (defmethod change-class ((instance standard-object)
1172                          (new-class standard-class))
1173   (change-class-internal instance new-class))
1174
1175 (defmethod change-class ((instance funcallable-standard-object)
1176                          (new-class funcallable-standard-class))
1177   (change-class-internal instance new-class))
1178
1179 (defmethod change-class ((instance standard-object)
1180                          (new-class funcallable-standard-class))
1181   (error "You can't change the class of ~S to ~S~@
1182           because it isn't already an instance with metaclass ~S."
1183          instance new-class 'standard-class))
1184
1185 (defmethod change-class ((instance funcallable-standard-object)
1186                          (new-class standard-class))
1187   (error "You can't change the class of ~S to ~S~@
1188           because it isn't already an instance with metaclass ~S."
1189          instance new-class 'funcallable-standard-class))
1190
1191 (defmethod change-class ((instance t) (new-class-name symbol))
1192   (change-class instance (find-class new-class-name)))
1193 \f
1194 ;;;; The metaclass BUILT-IN-CLASS
1195 ;;;;
1196 ;;;; This metaclass is something of a weird creature. By this point, all
1197 ;;;; instances of it which will exist have been created, and no instance
1198 ;;;; is ever created by calling MAKE-INSTANCE.
1199 ;;;;
1200 ;;;; But, there are other parts of the protocol we must follow and those
1201 ;;;; definitions appear here.
1202
1203 (defmethod shared-initialize :before
1204            ((class built-in-class) slot-names &rest initargs)
1205   (declare (ignore slot-names initargs))
1206   (error "attempt to initialize or reinitialize a built in class"))
1207
1208 (defmethod class-direct-slots       ((class built-in-class)) ())
1209 (defmethod class-slots             ((class built-in-class)) ())
1210 (defmethod class-direct-default-initargs ((class built-in-class)) ())
1211 (defmethod class-default-initargs       ((class built-in-class)) ())
1212
1213 (defmethod validate-superclass ((c class) (s built-in-class))
1214   (or (eq s *the-class-t*)
1215       (eq s *the-class-stream*)))
1216 \f
1217 (defmethod validate-superclass ((c slot-class)
1218                                 (f forward-referenced-class))
1219   't)
1220 \f
1221 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1222   (pushnew dependent (plist-value metaobject 'dependents)))
1223
1224 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1225   (setf (plist-value metaobject 'dependents)
1226         (delete dependent (plist-value metaobject 'dependents))))
1227
1228 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1229   (dolist (dependent (plist-value metaobject 'dependents))
1230     (funcall function dependent)))
1231