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