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