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