Make sure quantifiers don't cons
[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   (let ((info (slot-definition-info slotd)))
28     (ecase type
29       (reader (slot-info-reader info))
30       (writer (slot-info-writer info))
31       (boundp (slot-info-boundp info)))))
32
33 (defmethod (setf slot-accessor-function) (function
34                                           (slotd effective-slot-definition)
35                                           type)
36   (let ((info (slot-definition-info slotd)))
37     (ecase type
38       (reader (setf (slot-info-reader info) function))
39       (writer (setf (slot-info-writer info) function))
40       (boundp (setf (slot-info-boundp info) function)))))
41
42 (defconstant +slotd-reader-function-std-p+ 1)
43 (defconstant +slotd-writer-function-std-p+ 2)
44 (defconstant +slotd-boundp-function-std-p+ 4)
45 (defconstant +slotd-all-function-std-p+ 7)
46
47 (defmethod slot-accessor-std-p ((slotd effective-slot-definition) type)
48   (let ((flags (slot-value slotd 'accessor-flags)))
49     (declare (type fixnum flags))
50     (if (eq type 'all)
51         (eql +slotd-all-function-std-p+ flags)
52         (let ((mask (ecase type
53                       (reader +slotd-reader-function-std-p+)
54                       (writer +slotd-writer-function-std-p+)
55                       (boundp +slotd-boundp-function-std-p+))))
56           (declare (type fixnum mask))
57           (not (zerop (the fixnum (logand mask flags))))))))
58
59 (defmethod (setf slot-accessor-std-p) (value
60                                        (slotd effective-slot-definition)
61                                        type)
62   (let ((mask (ecase type
63                 (reader +slotd-reader-function-std-p+)
64                 (writer +slotd-writer-function-std-p+)
65                 (boundp +slotd-boundp-function-std-p+)))
66         (flags (slot-value slotd 'accessor-flags)))
67     (declare (type fixnum mask flags))
68     (setf (slot-value slotd 'accessor-flags)
69           (if value
70               (the fixnum (logior mask flags))
71               (the fixnum (logand (the fixnum (lognot mask)) flags)))))
72   value)
73
74 (defmethod initialize-internal-slot-functions
75     ((slotd effective-slot-definition))
76   (let* ((name (slot-value slotd 'name))
77          (class (slot-value slotd '%class)))
78     (dolist (type '(reader writer boundp))
79       (let* ((gf-name (ecase type
80                               (reader 'slot-value-using-class)
81                               (writer '(setf slot-value-using-class))
82                               (boundp 'slot-boundp-using-class)))
83              (gf (gdefinition gf-name)))
84         ;; KLUDGE: this logic is cut'n'pasted from
85         ;; GET-ACCESSOR-METHOD-FUNCTION, which (for STD-CLASSes) is
86         ;; only called later, because it does things that can't be
87         ;; computed this early in class finalization; however, we need
88         ;; this bit as early as possible.  -- CSR, 2009-11-05
89         (setf (slot-accessor-std-p slotd type)
90               (let* ((std-method (standard-svuc-method type))
91                      (str-method (structure-svuc-method type))
92                      (types1 `((eql ,class) (class-eq ,class) (eql ,slotd)))
93                      (types (if (eq type 'writer) `(t ,@types1) types1))
94                      (methods (compute-applicable-methods-using-types gf types)))
95                 (null (cdr methods))))
96         (setf (slot-accessor-function slotd type)
97               (lambda (&rest args)
98                 (declare (dynamic-extent args))
99                 ;; FIXME: a tiny amount of wasted SLOT-ACCESSOR-STD-P
100                 ;; work here (see KLUDGE comment above).
101                 (let ((fun (compute-slot-accessor-info slotd type gf)))
102                   (apply fun args))))))))
103
104 (defmethod finalize-internal-slot-functions ((slotd effective-slot-definition))
105   (dolist (type '(reader writer boundp))
106     (let* ((gf-name (ecase type
107                       (reader 'slot-value-using-class)
108                       (writer '(setf slot-value-using-class))
109                       (boundp 'slot-boundp-using-class)))
110            (gf (gdefinition gf-name)))
111       (compute-slot-accessor-info slotd type gf))))
112
113 ;;; CMUCL (Gerd PCL 2003-04-25) comment:
114 ;;;
115 ;;; Compute an effective method for SLOT-VALUE-USING-CLASS, (SETF
116 ;;; SLOT-VALUE-USING-CLASS) or SLOT-BOUNDP-USING-CLASS for reading/
117 ;;; writing/testing effective slot SLOTD.
118 ;;;
119 ;;; TYPE is one of the symbols READER, WRITER or BOUNDP, depending on
120 ;;; GF.  Store the effective method in the effective slot definition
121 ;;; object itself; these GFs have special dispatch functions calling
122 ;;; effective methods directly retrieved from effective slot
123 ;;; definition objects, as an optimization.
124 ;;;
125 ;;; FIXME: Change the function name to COMPUTE-SVUC-SLOTD-FUNCTION,
126 ;;; or some such.
127 (defmethod compute-slot-accessor-info ((slotd effective-slot-definition)
128                                        type gf)
129   (let* ((name (slot-value slotd 'name))
130          (class (slot-value slotd '%class)))
131     (multiple-value-bind (function std-p)
132         (if (eq **boot-state** 'complete)
133             (get-accessor-method-function gf type class slotd)
134             (get-optimized-std-accessor-method-function class slotd type))
135       (setf (slot-accessor-std-p slotd type) std-p)
136       (setf (slot-accessor-function slotd type) function))))
137
138 (defmethod slot-definition-allocation ((slotd structure-slot-definition))
139   :instance)
140 \f
141 ;;;; various class accessors that are a little more complicated than can be
142 ;;;; done with automatically generated reader methods
143
144 (defmethod class-prototype :before (class)
145   (unless (class-finalized-p class)
146     (error "~@<~S is not finalized.~:@>" class)))
147
148 ;;; KLUDGE: For some reason factoring the common body into a function
149 ;;; breaks PCL bootstrapping, so just generate it with a macrolet for
150 ;;; all.
151 (macrolet ((def (class)
152              `(defmethod class-prototype ((class ,class))
153                 (with-slots (prototype) class
154                   (or prototype
155                       (setf prototype (allocate-instance class)))))))
156   (def std-class)
157   (def condition-class)
158   (def structure-class))
159
160 (defmethod class-direct-default-initargs ((class slot-class))
161   (plist-value class 'direct-default-initargs))
162
163 (defmethod class-default-initargs ((class slot-class))
164   (plist-value class 'default-initargs))
165
166 (defmethod class-slot-cells ((class std-class))
167   (plist-value class 'class-slot-cells))
168 (defmethod (setf class-slot-cells) (new-value (class std-class))
169   (setf (plist-value class 'class-slot-cells) new-value))
170 \f
171 ;;;; class accessors that are even a little bit more complicated than those
172 ;;;; above. These have a protocol for updating them, we must implement that
173 ;;;; protocol.
174
175 ;;; Maintaining the direct subclasses backpointers. The update methods are
176 ;;; here, the values are read by an automatically generated reader method.
177 (defmethod add-direct-subclass ((class class) (subclass class))
178   (with-slots (direct-subclasses) class
179     (pushnew subclass direct-subclasses :test #'eq)
180     subclass))
181 (defmethod remove-direct-subclass ((class class) (subclass class))
182   (with-slots (direct-subclasses) class
183     (setq direct-subclasses (remove subclass direct-subclasses))
184     subclass))
185
186 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
187 ;;;
188 ;;; There are four generic functions involved, each has one method for the
189 ;;; class case and another method for the damned EQL specializers. All of
190 ;;; these are specified methods and appear in their specified place in the
191 ;;; class graph.
192 ;;;
193 ;;;   ADD-DIRECT-METHOD
194 ;;;   REMOVE-DIRECT-METHOD
195 ;;;   SPECIALIZER-DIRECT-METHODS
196 ;;;   SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
197 ;;;
198 ;;; In each case, we maintain one value which is a cons. The car is the list
199 ;;; methods. The cdr is a list of the generic functions. The cdr is always
200 ;;; computed lazily.
201
202 ;;; This needs to be used recursively, in case a non-trivial user
203 ;;; defined ADD/REMOVE-DIRECT-METHOD method ends up calling another
204 ;;; function using the same lock.
205 (defvar *specializer-lock* (sb-thread:make-mutex :name "Specializer lock"))
206
207 (defmethod add-direct-method :around ((specializer specializer) method)
208   ;; All the actions done under this lock are done in an order
209   ;; that is safe to unwind at any point.
210   (sb-thread::with-recursive-system-lock (*specializer-lock*)
211     (call-next-method)))
212
213 (defmethod remove-direct-method :around ((specializer specializer) method)
214   ;; All the actions done under this lock are done in an order
215   ;; that is safe to unwind at any point.
216   (sb-thread::with-recursive-system-lock (*specializer-lock*)
217     (call-next-method)))
218
219 (defmethod add-direct-method ((specializer class) (method method))
220   (let ((cell (slot-value specializer 'direct-methods)))
221     ;; We need to first smash the CDR, because a parallel read may
222     ;; be in progress, and because if an interrupt catches us we
223     ;; need to have a consistent state.
224     (setf (cdr cell) ()
225           (car cell) (adjoin method (car cell) :test #'eq)))
226   method)
227
228 (defmethod remove-direct-method ((specializer class) (method method))
229   (let ((cell (slot-value specializer 'direct-methods)))
230     ;; We need to first smash the CDR, because a parallel read may
231     ;; be in progress, and because if an interrupt catches us we
232     ;; need to have a consistent state.
233     (setf (cdr cell) ()
234           (car cell) (remove method (car cell))))
235   method)
236
237 (defmethod specializer-direct-methods ((specializer class))
238   (with-slots (direct-methods) specializer
239     (car direct-methods)))
240
241 (defmethod specializer-direct-generic-functions ((specializer class))
242   (let ((cell (slot-value specializer 'direct-methods)))
243     ;; If an ADD/REMOVE-METHOD is in progress, no matter: either
244     ;; we behave as if we got just first or just after -- it's just
245     ;; for update that we need to lock.
246     (or (cdr cell)
247         (sb-thread:with-mutex (*specializer-lock*)
248           (setf (cdr cell)
249                 (let (collect)
250                   (dolist (m (car cell))
251                     ;; the old PCL code used COLLECTING-ONCE which used
252                     ;; #'EQ to check for newness
253                     (pushnew (method-generic-function m) collect :test #'eq))
254                   (nreverse collect)))))))
255 \f
256 ;;; This hash table is used to store the direct methods and direct generic
257 ;;; functions of EQL specializers. Each value in the table is the cons.
258 ;;;
259 ;;; These tables are shared between threads, so they need to be synchronized.
260 (defvar *eql-specializer-methods* (make-hash-table :test 'eql :synchronized t))
261 (defvar *class-eq-specializer-methods* (make-hash-table :test 'eq :synchronized t))
262
263 (defmethod specializer-method-table ((specializer eql-specializer))
264   *eql-specializer-methods*)
265
266 (defmethod specializer-method-table ((specializer class-eq-specializer))
267   *class-eq-specializer-methods*)
268
269 (defmethod add-direct-method ((specializer specializer-with-object)
270                               (method method))
271   (let* ((object (specializer-object specializer))
272          (table (specializer-method-table specializer))
273          (entry (gethash object table)))
274     (unless entry
275       (setf entry
276             (setf (gethash object table) (cons nil nil))))
277     ;; We need to first smash the CDR, because a parallel read may
278     ;; be in progress, and because if an interrupt catches us we
279     ;; need to have a consistent state.
280     (setf (cdr entry) ()
281           (car entry) (adjoin method (car entry) :test #'eq))
282     method))
283
284 (defmethod remove-direct-method ((specializer specializer-with-object)
285                                  (method method))
286   (let* ((object (specializer-object specializer))
287          (entry (gethash object (specializer-method-table specializer))))
288     (when entry
289       ;; We need to first smash the CDR, because a parallel read may
290       ;; be in progress, and because if an interrupt catches us we
291       ;; need to have a consistent state.
292       (setf (cdr entry) ()
293             (car entry) (remove method (car entry))))
294     method))
295
296 (defmethod specializer-direct-methods ((specializer specializer-with-object))
297   (car (gethash (specializer-object specializer)
298                 (specializer-method-table specializer))))
299
300 (defmethod specializer-direct-generic-functions ((specializer
301                                                   specializer-with-object))
302   (let* ((object (specializer-object specializer))
303          (entry (gethash object (specializer-method-table specializer))))
304     (when entry
305       (or (cdr entry)
306           (sb-thread:with-mutex (*specializer-lock*)
307             (setf (cdr entry)
308                   (let (collect)
309                     (dolist (m (car entry))
310                       (pushnew (method-generic-function m) collect :test #'eq))
311                     (nreverse collect))))))))
312
313 (defun map-specializers (function)
314   (map-all-classes (lambda (class)
315                      (funcall function (class-eq-specializer class))
316                      (funcall function class)))
317   (maphash (lambda (object methods)
318              (declare (ignore methods))
319              (intern-eql-specializer object))
320            *eql-specializer-methods*)
321   (maphash (lambda (object specl)
322              (declare (ignore object))
323              (funcall function specl))
324            *eql-specializer-table*)
325   nil)
326
327 (defun map-all-generic-functions (function)
328   (let ((all-generic-functions (make-hash-table :test 'eq)))
329     (map-specializers (lambda (specl)
330                         (dolist (gf (specializer-direct-generic-functions
331                                      specl))
332                           (unless (gethash gf all-generic-functions)
333                             (setf (gethash gf all-generic-functions) t)
334                             (funcall function gf))))))
335   nil)
336
337 (defmethod shared-initialize :after ((specl class-eq-specializer)
338                                      slot-names
339                                      &key)
340   (declare (ignore slot-names))
341   (setf (slot-value specl '%type) `(class-eq ,(specializer-class specl))))
342
343 (defmethod shared-initialize :after ((specl eql-specializer) slot-names &key)
344   (declare (ignore slot-names))
345   (setf (slot-value specl '%type)
346         `(eql ,(specializer-object specl)))
347   (setf (info :type :translator specl)
348         (constantly (make-member-type :members (list (specializer-object specl))))))
349
350 (defun real-load-defclass (name metaclass-name supers slots other
351                            readers writers slot-names source-location safe-p)
352   (with-single-package-locked-error (:symbol name "defining ~S as a class")
353     (%compiler-defclass name readers writers slot-names)
354     (let ((res (apply #'ensure-class name :metaclass metaclass-name
355                       :direct-superclasses supers
356                       :direct-slots slots
357                       :definition-source source-location
358                       'safe-p safe-p
359                       other)))
360       res)))
361
362 (setf (gdefinition 'load-defclass) #'real-load-defclass)
363
364 (defun ensure-class (name &rest args)
365   (with-world-lock ()
366     (apply #'ensure-class-using-class
367            (let ((class (find-class name nil)))
368              (when (and class (eq name (class-name class)))
369                ;; NAME is the proper name of CLASS, so redefine it
370                class))
371            name
372            args)))
373
374 (defmethod ensure-class-using-class ((class null) name &rest args &key)
375   (with-world-lock ()
376     (multiple-value-bind (meta initargs)
377         (frob-ensure-class-args args)
378       (setf class (apply #'make-instance meta :name name initargs))
379       (without-package-locks
380         (setf (find-class name) class))))
381   ;; After boot (SETF FIND-CLASS) does this.
382   (unless (eq **boot-state** 'complete)
383     (%set-class-type-translation class name))
384   class)
385
386 (defmethod ensure-class-using-class ((class pcl-class) name &rest args &key)
387   (with-world-lock ()
388     (multiple-value-bind (meta initargs)
389         (frob-ensure-class-args args)
390       (unless (eq (class-of class) meta)
391         (apply #'change-class class meta initargs))
392       (apply #'reinitialize-instance class initargs)
393       (without-package-locks
394         (setf (find-class name) class))))
395   ;; After boot (SETF FIND-CLASS) does this.
396   (unless (eq **boot-state** 'complete)
397     (%set-class-type-translation class name))
398   class)
399
400 (defun frob-ensure-class-args (args)
401   (let (metaclass metaclassp reversed-plist)
402     (flet ((frob-superclass (s)
403              (cond
404                ((classp s) s)
405                ((legal-class-name-p s)
406                 (or (find-class s nil)
407                     (ensure-class s :metaclass 'forward-referenced-class)))
408                (t (error "Not a class or a legal class name: ~S." s)))))
409       (doplist (key val) args
410         (cond ((eq key :metaclass)
411                (unless metaclassp
412                  (setf metaclass val metaclassp key)))
413               (t
414                (when (eq key :direct-superclasses)
415                  (setf val (mapcar #'frob-superclass val)))
416                (setf reversed-plist (list* val key reversed-plist)))))
417       (values (cond (metaclassp
418                      (if (classp metaclass)
419                          metaclass
420                          (find-class metaclass)))
421                     (t *the-class-standard-class*))
422               (nreverse reversed-plist)))))
423
424 ;;; This is used to call initfunctions of :allocation :class slots.
425 (defun call-initfun (fun slotd safe)
426   (declare (function fun))
427   (let ((value (funcall fun)))
428     (when safe
429       (let ((type (slot-definition-type slotd)))
430         (unless (or (eq t type)
431                     (typep value type))
432           (error 'type-error :expected-type type :datum value))))
433     value))
434 \f
435 (defmethod shared-initialize :after
436     ((class std-class) slot-names &key
437      (direct-superclasses nil direct-superclasses-p)
438      (direct-slots nil direct-slots-p)
439      (direct-default-initargs nil direct-default-initargs-p)
440      definition-source)
441   (cond (direct-superclasses-p
442          (setq direct-superclasses
443                (or direct-superclasses
444                    (list (if (funcallable-standard-class-p class)
445                              *the-class-funcallable-standard-object*
446                              *the-class-standard-object*))))
447          (dolist (superclass direct-superclasses)
448            (unless (validate-superclass class superclass)
449              (error "~@<The class ~S was specified as a ~
450                      super-class of the class ~S, ~
451                      but the meta-classes ~S and ~S are incompatible.  ~
452                      Define a method for ~S to avoid this error.~@:>"
453                     superclass class (class-of superclass) (class-of class)
454                     'validate-superclass)))
455          (setf (slot-value class 'direct-superclasses) direct-superclasses))
456         (t
457          (setq direct-superclasses (slot-value class 'direct-superclasses))))
458   (setq direct-slots
459         (if direct-slots-p
460             (setf (slot-value class 'direct-slots)
461                   (mapcar (lambda (pl) (make-direct-slotd class pl))
462                           direct-slots))
463             (slot-value class 'direct-slots)))
464   (if direct-default-initargs-p
465       (setf (plist-value class 'direct-default-initargs)
466             direct-default-initargs)
467       (setq direct-default-initargs
468             (plist-value class 'direct-default-initargs)))
469   (setf (plist-value class 'class-slot-cells)
470         (let ((old-class-slot-cells (plist-value class 'class-slot-cells))
471               (safe (safe-p class))
472               (collect '()))
473           (dolist (dslotd direct-slots)
474             (when (eq :class (slot-definition-allocation dslotd))
475               ;; see CLHS 4.3.6
476               (let* ((name (slot-definition-name dslotd))
477                      (old (assoc name old-class-slot-cells)))
478                 (if (or (not old)
479                         (eq t slot-names)
480                         (member name slot-names :test #'eq))
481                     (let* ((initfunction (slot-definition-initfunction dslotd))
482                            (value
483                             (if initfunction
484                                 (call-initfun initfunction dslotd safe)
485                                 +slot-unbound+)))
486                       (push (cons name value) collect))
487                     (push old collect)))))
488           (nreverse collect)))
489   (add-direct-subclasses class direct-superclasses)
490   (if (class-finalized-p class)
491       ;; required by AMOP, "Reinitialization of Class Metaobjects"
492       (finalize-inheritance class)
493       (update-class class nil))
494   (add-slot-accessors class direct-slots definition-source)
495   (make-preliminary-layout class))
496
497 (defmethod shared-initialize :after ((class forward-referenced-class)
498                                      slot-names &key &allow-other-keys)
499   (declare (ignore slot-names))
500   (make-preliminary-layout class))
501
502 (defvar *allow-forward-referenced-classes-in-cpl-p* nil)
503
504 ;;; Give CLASS a preliminary layout if it doesn't have one already, to
505 ;;; make it known to the type system.
506 (defun make-preliminary-layout (class)
507   (flet ((compute-preliminary-cpl (root)
508            (let ((*allow-forward-referenced-classes-in-cpl-p* t))
509              (compute-class-precedence-list root))))
510     (with-world-lock ()
511       (without-package-locks
512         (unless (class-finalized-p class)
513           (let ((name (class-name class)))
514             ;; KLUDGE: This is fairly horrible.  We need to make a
515             ;; full-fledged CLASSOID here, not just tell the compiler that
516             ;; some class is forthcoming, because there are legitimate
517             ;; questions one can ask of the type system, implemented in
518             ;; terms of CLASSOIDs, involving forward-referenced classes. So.
519             (let ((layout (make-wrapper 0 class)))
520               (setf (slot-value class 'wrapper) layout)
521               (let ((cpl (compute-preliminary-cpl class)))
522                 (setf (layout-inherits layout)
523                       (order-layout-inherits
524                        (map 'simple-vector #'class-wrapper
525                             (reverse (rest cpl))))))
526               (register-layout layout :invalidate t)
527               (%set-class-type-translation class (layout-classoid layout)))))
528         (mapc #'make-preliminary-layout (class-direct-subclasses class))))))
529
530
531 (defmethod shared-initialize :before ((class class) slot-names &key name)
532   (declare (ignore slot-names name))
533   ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
534   ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
535   (setf (slot-value class '%type) `(class ,class))
536   (setf (slot-value class 'class-eq-specializer)
537         (make-instance 'class-eq-specializer :class class)))
538
539 (defmethod reinitialize-instance :before ((class slot-class) &key direct-superclasses)
540   (dolist (old-super (set-difference (class-direct-superclasses class) direct-superclasses))
541     (remove-direct-subclass old-super class))
542   (remove-slot-accessors class (class-direct-slots class)))
543
544 (defmethod reinitialize-instance :after ((class slot-class)
545                                          &rest initargs
546                                          &key)
547   (map-dependents class
548                   (lambda (dependent)
549                     (apply #'update-dependent class dependent initargs))))
550
551 (defmethod reinitialize-instance :after ((class condition-class) &key)
552   (let* ((name (class-name class))
553          (classoid (find-classoid name))
554          (slots (condition-classoid-slots classoid)))
555     ;; to balance the REMOVE-SLOT-ACCESSORS call in
556     ;; REINITIALIZE-INSTANCE :BEFORE (SLOT-CLASS).
557     (dolist (slot slots)
558       (let ((slot-name (condition-slot-name slot)))
559         (dolist (reader (condition-slot-readers slot))
560           ;; FIXME: see comment in SHARED-INITIALIZE :AFTER
561           ;; (CONDITION-CLASS T), below.  -- CSR, 2005-11-18
562           (sb-kernel::install-condition-slot-reader reader name slot-name))
563         (dolist (writer (condition-slot-writers slot))
564           (sb-kernel::install-condition-slot-writer writer name slot-name))))))
565
566 (defmethod shared-initialize :after ((class condition-class) slot-names
567                                      &key direct-slots direct-superclasses)
568   (declare (ignore slot-names))
569   (let ((classoid (find-classoid (slot-value class 'name))))
570     (with-slots (wrapper
571                  %class-precedence-list cpl-available-p finalized-p
572                  prototype (direct-supers direct-superclasses)
573                  plist)
574         class
575       (setf (slot-value class 'direct-slots)
576             (mapcar (lambda (pl) (make-direct-slotd class pl))
577                     direct-slots)
578             finalized-p t
579             (classoid-pcl-class classoid) class
580             direct-supers direct-superclasses
581             wrapper (classoid-layout classoid)
582             %class-precedence-list (compute-class-precedence-list class)
583             cpl-available-p t
584             (getf plist 'direct-default-initargs)
585             (sb-kernel::condition-classoid-direct-default-initargs classoid))
586       (add-direct-subclasses class direct-superclasses)
587       (let ((slots (compute-slots class)))
588         (setf (slot-value class 'slots) slots)
589         (setf (layout-slot-table wrapper) (make-slot-table class slots)))))
590   ;; Comment from Gerd's PCL, 2003-05-15:
591   ;;
592   ;; We don't ADD-SLOT-ACCESSORS here because we don't want to
593   ;; override condition accessors with generic functions.  We do this
594   ;; differently.
595   ;;
596   ;; ??? What does the above comment mean and why is it a good idea?
597   ;; CMUCL (which still as of 2005-11-18 uses this code and has this
598   ;; comment) loses slot information in its condition classes:
599   ;; DIRECT-SLOTS is always NIL.  We have the right information, so we
600   ;; remove slot accessors but never put them back.  I've added a
601   ;; REINITIALIZE-INSTANCE :AFTER (CONDITION-CLASS) method, but what
602   ;; was meant to happen?  -- CSR, 2005-11-18
603   )
604
605 (defmethod direct-slot-definition-class ((class condition-class)
606                                          &rest initargs)
607   (declare (ignore initargs))
608   (find-class 'condition-direct-slot-definition))
609
610 (defmethod effective-slot-definition-class ((class condition-class)
611                                             &rest initargs)
612   (declare (ignore initargs))
613   (find-class 'condition-effective-slot-definition))
614
615 (defmethod finalize-inheritance ((class condition-class))
616   (aver (slot-value class 'finalized-p))
617   nil)
618
619 (defmethod compute-effective-slot-definition
620     ((class condition-class) slot-name dslotds)
621   (let* ((slotd (call-next-method))
622          (info (slot-definition-info slotd)))
623     (setf (slot-info-reader info)
624           (lambda (x)
625             (handler-case (condition-reader-function x slot-name)
626               ;; FIXME: FIND-SLOT-DEFAULT throws an error if the slot
627               ;; is unbound; maybe it should be a CELL-ERROR of some
628               ;; sort?
629               (error () (values (slot-unbound class x slot-name))))))
630     (setf (slot-info-writer info)
631           (lambda (v x)
632             (condition-writer-function x v slot-name)))
633     (setf (slot-info-boundp info)
634           (lambda (x)
635             (multiple-value-bind (v c)
636                 (ignore-errors (condition-reader-function x slot-name))
637               (declare (ignore v))
638               (null c))))
639     slotd))
640
641 (defmethod compute-slots ((class condition-class))
642   (mapcan (lambda (superclass)
643             (mapcar (lambda (dslotd)
644                       (compute-effective-slot-definition
645                        class (slot-definition-name dslotd) (list dslotd)))
646                     (class-direct-slots superclass)))
647           (reverse (slot-value class '%class-precedence-list))))
648
649 (defmethod compute-slots :around ((class condition-class))
650   (let ((eslotds (call-next-method)))
651     (mapc #'finalize-internal-slot-functions eslotds)
652     eslotds))
653
654 (defmethod shared-initialize :after
655     ((slotd structure-slot-definition) slot-names &key
656      (allocation :instance) allocation-class)
657   (declare (ignore slot-names allocation-class))
658   (unless (eq allocation :instance)
659     (error "Structure slots must have :INSTANCE allocation.")))
660
661 (defun make-structure-class-defstruct-form (name direct-slots include)
662   (let* ((conc-name (format-symbol *package* "~S structure class " name))
663          (constructor (format-symbol *package* "~Aconstructor" conc-name))
664          (included-name (class-name include))
665          (included-slots
666           (when include
667             (mapcar #'dsd-name (dd-slots (find-defstruct-description included-name)))))
668          (old-slots nil)
669          (new-slots nil)
670          (reader-names nil)
671          (writer-names nil))
672     (dolist (slotd (reverse direct-slots))
673       (let* ((slot-name (slot-definition-name slotd))
674              (initform (slot-definition-initform slotd))
675              (type (slot-definition-type slotd))
676              (desc `(,slot-name ,initform :type ,type)))
677         (push `(slot-accessor ,name ,slot-name reader)
678               reader-names)
679         (push `(slot-accessor ,name ,slot-name writer)
680               writer-names)
681         (if (member slot-name included-slots :test #'eq)
682             (push desc old-slots)
683             (push desc new-slots))))
684     (let* ((defstruct `(defstruct (,name
685                                     ,@(when include
686                                             `((:include ,included-name
687                                                         ,@old-slots)))
688                                     (:constructor ,constructor ())
689                                     (:predicate nil)
690                                     (:conc-name ,conc-name)
691                                     (:copier nil))
692                          ,@new-slots))
693            (readers-init
694             (mapcar (lambda (slotd reader-name)
695                       (let ((accessor
696                              (slot-definition-defstruct-accessor-symbol
697                               slotd)))
698                         `(defun ,reader-name (obj)
699                            (declare (type ,name obj))
700                            (,accessor obj))))
701                     direct-slots reader-names))
702            (writers-init
703             (mapcar (lambda (slotd writer-name)
704                       (let ((accessor
705                              (slot-definition-defstruct-accessor-symbol
706                               slotd)))
707                         `(defun ,writer-name (nv obj)
708                            (declare (type ,name obj))
709                            (setf (,accessor obj) nv))))
710                     direct-slots writer-names))
711            (defstruct-form
712             `(progn
713                ,defstruct
714                ,@readers-init ,@writers-init
715                (cons nil nil))))
716       (values defstruct-form constructor reader-names writer-names))))
717
718 (defun make-defstruct-allocation-function (name)
719   ;; FIXME: Why don't we go class->layout->info == dd
720   (let ((dd (find-defstruct-description name)))
721     (ecase (dd-type dd)
722       (structure
723        (%make-structure-instance-allocator dd nil))
724       (funcallable-structure
725        (%make-funcallable-structure-instance-allocator dd nil)))))
726
727 (defmethod shared-initialize :after
728     ((class structure-class) slot-names &key
729      (direct-superclasses nil direct-superclasses-p)
730      (direct-slots nil direct-slots-p)
731      direct-default-initargs
732      definition-source)
733   (declare (ignore slot-names direct-default-initargs))
734   (if direct-superclasses-p
735       (setf (slot-value class 'direct-superclasses)
736             (or direct-superclasses
737                 (setq direct-superclasses
738                       (and (not (eq (slot-value class 'name) 'structure-object))
739                            (list *the-class-structure-object*)))))
740       (setq direct-superclasses (slot-value class 'direct-superclasses)))
741   (let* ((name (slot-value class 'name))
742          (from-defclass-p (slot-value class 'from-defclass-p))
743          (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
744     (if direct-slots-p
745         (setf (slot-value class 'direct-slots)
746               (setq direct-slots
747                     (mapcar (lambda (pl)
748                               (when defstruct-p
749                                 (let* ((slot-name (getf pl :name))
750                                        (accessor
751                                         (format-symbol *package*
752                                                        "~S structure class ~A"
753                                                        name slot-name)))
754                                   (setq pl (list* :defstruct-accessor-symbol
755                                                   accessor pl))))
756                               (make-direct-slotd class pl))
757                             direct-slots)))
758         (setq direct-slots (slot-value class 'direct-slots)))
759     (if defstruct-p
760         (let ((include (car (slot-value class 'direct-superclasses))))
761           (multiple-value-bind (defstruct-form constructor reader-names writer-names)
762               (make-structure-class-defstruct-form name direct-slots include)
763             (unless (structure-type-p name) (eval defstruct-form))
764             (mapc (lambda (dslotd reader-name writer-name)
765                     (let* ((reader (gdefinition reader-name))
766                            (writer (when (fboundp writer-name)
767                                      (gdefinition writer-name))))
768                       (setf (slot-value dslotd 'internal-reader-function)
769                             reader)
770                       (setf (slot-value dslotd 'internal-writer-function)
771                             writer)))
772                   direct-slots reader-names writer-names)
773             (setf (slot-value class 'defstruct-form) defstruct-form)
774             (setf (slot-value class 'defstruct-constructor) constructor)))
775         (setf (slot-value class 'defstruct-constructor)
776               ;; KLUDGE: not class; in fixup.lisp, can't access slots
777               ;; outside methods yet.
778               (make-defstruct-allocation-function name)))
779     (add-direct-subclasses class direct-superclasses)
780     (setf (slot-value class '%class-precedence-list)
781           (compute-class-precedence-list class))
782     (setf (slot-value class 'cpl-available-p) t)
783     (let ((slots (compute-slots class)))
784       (setf (slot-value class 'slots) slots)
785       (let* ((lclass (find-classoid (slot-value class 'name)))
786              (layout (classoid-layout lclass)))
787         (setf (classoid-pcl-class lclass) class)
788         (setf (slot-value class 'wrapper) layout)
789         (setf (layout-slot-table layout) (make-slot-table class slots))))
790     (setf (slot-value class 'finalized-p) t)
791     (add-slot-accessors class direct-slots definition-source)))
792
793 (defmethod direct-slot-definition-class ((class structure-class) &rest initargs)
794   (declare (ignore initargs))
795   (find-class 'structure-direct-slot-definition))
796
797 (defmethod finalize-inheritance ((class structure-class))
798   nil) ; always finalized
799 \f
800 (defun add-slot-accessors (class dslotds &optional source-location)
801   (fix-slot-accessors class dslotds 'add source-location))
802
803 (defun remove-slot-accessors (class dslotds)
804   (fix-slot-accessors class dslotds 'remove))
805
806 (defun fix-slot-accessors (class dslotds add/remove &optional source-location)
807   (flet ((fix (gfspec name r/w doc)
808            (let ((gf (cond ((eq add/remove 'add)
809                             (or (find-generic-function gfspec nil)
810                                 (ensure-generic-function
811                                  gfspec :lambda-list (case r/w
812                                                        (r '(object))
813                                                        (w '(new-value object))))))
814                            (t
815                             (find-generic-function gfspec nil)))))
816              (when gf
817                (case r/w
818                  (r (if (eq add/remove 'add)
819                         (add-reader-method class gf name doc source-location)
820                         (remove-reader-method class gf)))
821                  (w (if (eq add/remove 'add)
822                         (add-writer-method class gf name doc source-location)
823                         (remove-writer-method class gf))))))))
824     (dolist (dslotd dslotds)
825       (let ((slot-name (slot-definition-name dslotd))
826             (slot-doc (%slot-definition-documentation dslotd)))
827         (dolist (r (slot-definition-readers dslotd))
828           (fix r slot-name 'r slot-doc))
829         (dolist (w (slot-definition-writers dslotd))
830           (fix w slot-name 'w slot-doc))))))
831 \f
832 (defun add-direct-subclasses (class supers)
833   (dolist (super supers)
834     (unless (memq class (class-direct-subclasses class))
835       (add-direct-subclass super class))))
836
837 (defmethod finalize-inheritance ((class std-class))
838   (update-class class t))
839
840 (defmethod finalize-inheritance ((class forward-referenced-class))
841   ;; FIXME: should we not be thinking a bit about what kinds of error
842   ;; we're throwing?  Maybe we need a clos-error type to mix in?  Or
843   ;; possibly a forward-referenced-class-error, though that's
844   ;; difficult given e.g. class precedence list calculations...
845   (error
846    "~@<FINALIZE-INHERITANCE was called on a forward referenced class:~
847        ~2I~_~S~:>"
848    class))
849
850 \f
851 (defun class-has-a-forward-referenced-superclass-p (class)
852   (or (when (forward-referenced-class-p class)
853         class)
854       (some #'class-has-a-forward-referenced-superclass-p
855             (class-direct-superclasses class))))
856
857 ;;; This is called by :after shared-initialize whenever a class is initialized
858 ;;; or reinitialized. The class may or may not be finalized.
859 (defun update-class (class finalizep)
860   (without-package-locks
861     (with-world-lock ()
862       (when (or finalizep (class-finalized-p class))
863         (%update-cpl class (compute-class-precedence-list class))
864         ;; This invocation of UPDATE-SLOTS, in practice, finalizes the
865         ;; class.
866         (%update-slots class (compute-slots class))
867         (update-gfs-of-class class)
868         (setf (plist-value class 'default-initargs) (compute-default-initargs class))
869         (update-ctors 'finalize-inheritance :class class))
870       (dolist (sub (class-direct-subclasses class))
871         (update-class sub nil)))))
872
873 (define-condition cpl-protocol-violation (reference-condition error)
874   ((class :initarg :class :reader cpl-protocol-violation-class)
875    (cpl :initarg :cpl :reader cpl-protocol-violation-cpl))
876   (:default-initargs :references (list '(:sbcl :node "Metaobject Protocol")))
877   (:report
878    (lambda (c s)
879      (format s "~@<Protocol violation: the ~S class ~S ~
880                 ~:[has~;does not have~] the class ~S in its ~
881                 class precedence list: ~S.~@:>"
882              (class-name (class-of (cpl-protocol-violation-class c)))
883              (cpl-protocol-violation-class c)
884              (eq (class-of (cpl-protocol-violation-class c))
885                  *the-class-funcallable-standard-class*)
886              (find-class 'function)
887              (cpl-protocol-violation-cpl c)))))
888
889 (defun class-has-a-cpl-protocol-violation-p (class)
890   (labels ((find-in-superclasses (class classes)
891              (cond
892                ((null classes) nil)
893                ((eql class (car classes)) t)
894                (t (find-in-superclasses class (append (class-direct-superclasses (car classes)) (cdr classes)))))))
895     (let ((metaclass (class-of class)))
896       (cond
897         ((eql metaclass *the-class-standard-class*)
898          (find-in-superclasses (find-class 'function) (list class)))
899         ((eql metaclass *the-class-funcallable-standard-class*)
900          (not (find-in-superclasses (find-class 'function) (list class))))))))
901
902 (defun %update-cpl (class cpl)
903   (when (eq (class-of class) *the-class-standard-class*)
904     (when (find (find-class 'function) cpl)
905       (error 'cpl-protocol-violation :class class :cpl cpl)))
906   (when (eq (class-of class) *the-class-funcallable-standard-class*)
907     (unless (find (find-class 'function) cpl)
908       (error 'cpl-protocol-violation :class class :cpl cpl)))
909   (if (class-finalized-p class)
910       (unless (and (equal (class-precedence-list class) cpl)
911                    (dolist (c cpl t)
912                      (when (position :class (class-direct-slots c)
913                                      :key #'slot-definition-allocation)
914                        (return nil))))
915         ;; comment from the old CMU CL sources:
916         ;;   Need to have the cpl setup before %update-lisp-class-layout
917         ;;   is called on CMU CL.
918         (setf (slot-value class '%class-precedence-list) cpl)
919         (setf (slot-value class 'cpl-available-p) t)
920         (%force-cache-flushes class))
921       (progn
922         (setf (slot-value class '%class-precedence-list) cpl)
923         (setf (slot-value class 'cpl-available-p) t)))
924   (update-class-can-precede-p cpl))
925
926 (defun update-class-can-precede-p (cpl)
927   (when cpl
928     (let ((first (car cpl)))
929       (dolist (c (cdr cpl))
930         (pushnew c (slot-value first 'can-precede-list) :test #'eq)))
931     (update-class-can-precede-p (cdr cpl))))
932
933 (defun class-can-precede-p (class1 class2)
934   (member class2 (class-can-precede-list class1) :test #'eq))
935
936 ;;; This is called from %UPDATE-SLOTS to check if slot layouts are compatible.
937 ;;;
938 ;;; In addition to slot locations (implicit in the ordering of the slots), we
939 ;;; must check classes: SLOT-INFO structures from old slotds may have been
940 ;;; cached in permutation vectors, but new slotds have had new ones allocated
941 ;;; to them. This is non-problematic for standard slotds, because we know the
942 ;;; structure is compatible, but if a slot definition class changes, this can
943 ;;; change the way SLOT-VALUE-USING-CLASS should dispatch.
944 ;;;
945 ;;; Also, if the slot has a non-standard allocation, we need to check that it
946 ;;; doesn't change.
947 (defun slot-layouts-compatible-p
948     (oslotds new-instance-slotds new-class-slotds new-custom-slotds)
949   (multiple-value-bind (old-instance-slotds old-class-slotds old-custom-slotds)
950       (classify-slotds oslotds)
951     (and
952      ;; Instance slots: name, type, and class.
953      (dolist (o old-instance-slotds (not new-instance-slotds))
954        (let ((n (pop new-instance-slotds)))
955          (unless (and n
956                       (eq (slot-definition-name o) (slot-definition-name n))
957                       (eq (slot-definition-type o) (slot-definition-type n))
958                       (eq (class-of o) (class-of n)))
959            (return nil))))
960      ;; Class slots: name and class. (FIXME: class slots not typechecked?)
961      (dolist (o old-class-slotds (not new-class-slotds))
962        (let ((n (pop new-class-slotds)))
963          (unless (and n
964                       (eq (slot-definition-name o) (slot-definition-name n))
965                       (eq (class-of n) (class-of o)))
966            (return nil))))
967      ;; Custom slots: check name, type, allocation, and class. (FIXME: should we just punt?)
968      (dolist (o old-custom-slotds (not new-custom-slotds))
969        (let ((n (pop new-custom-slotds)))
970          (unless (and n
971                       (eq (slot-definition-name o) (slot-definition-name n))
972                       (eq (slot-definition-type o) (slot-definition-type n))
973                       (eq (slot-definition-allocation o) (slot-definition-allocation n))
974                       (eq (class-of o) (class-of n)))
975            (return nil)))))))
976
977 (defun style-warn-about-duplicate-slots (class)
978   (do* ((slots (slot-value class 'slots) (cdr slots))
979         (dupes nil))
980        ((null slots)
981         (when dupes
982           (style-warn
983            "~@<slot names with the same SYMBOL-NAME but ~
984                   different SYMBOL-PACKAGE (possible package problem) ~
985                   for class ~S:~4I~@:_~<~@{~/sb-impl::print-symbol-with-prefix/~^~:@_~}~:>~@:>"
986            class dupes)))
987     (let* ((slot-name (slot-definition-name (car slots)))
988            (oslots (and (not (eq (symbol-package slot-name)
989                                  *pcl-package*))
990                         (remove-if
991                          (lambda (slot-name-2)
992                            (or (eq (symbol-package slot-name-2)
993                                    *pcl-package*)
994                                (string/= slot-name slot-name-2)))
995                          (cdr slots)
996                          :key #'slot-definition-name))))
997       (when oslots
998         (pushnew (cons slot-name
999                        (mapcar #'slot-definition-name oslots))
1000                  dupes
1001                  :test #'string= :key #'car)))))
1002
1003 (defun %update-slots (class eslotds)
1004   (multiple-value-bind (instance-slots class-slots custom-slots)
1005       (classify-slotds eslotds)
1006     (let* ((nslots (length instance-slots))
1007            (owrapper (when (class-finalized-p class) (class-wrapper class)))
1008            (nwrapper
1009              (cond ((null owrapper)
1010                     (make-wrapper nslots class))
1011                    ((slot-layouts-compatible-p (wrapper-slots owrapper)
1012                                                instance-slots class-slots custom-slots)
1013                     owrapper)
1014                    (t
1015                     ;; This will initialize the new wrapper to have the
1016                     ;; same state as the old wrapper. We will then have
1017                     ;; to change that. This may seem like wasted work
1018                     ;; (and it is), but the spec requires that we call
1019                     ;; MAKE-INSTANCES-OBSOLETE.
1020                     (make-instances-obsolete class)
1021                     (class-wrapper class)))))
1022       (%update-lisp-class-layout class nwrapper)
1023       (setf (slot-value class 'slots) eslotds
1024             (wrapper-slots nwrapper) eslotds
1025             (wrapper-slot-table nwrapper) (make-slot-table class eslotds)
1026             (wrapper-length nwrapper) nslots
1027             (slot-value class 'wrapper) nwrapper)
1028       (style-warn-about-duplicate-slots class)
1029       (setf (slot-value class 'finalized-p) t)
1030       (unless (eq owrapper nwrapper)
1031         (maybe-update-standard-slot-locations class)))))
1032
1033 (defun update-gf-dfun (class gf)
1034   (let ((*new-class* class)
1035         (arg-info (gf-arg-info gf)))
1036     (cond
1037       ((special-case-for-compute-discriminating-function-p gf))
1038       ((gf-precompute-dfun-and-emf-p arg-info)
1039        (multiple-value-bind (dfun cache info) (make-final-dfun-internal gf)
1040          (update-dfun gf dfun cache info))))))
1041
1042 (defun update-gfs-of-class (class)
1043   (when (and (class-finalized-p class)
1044              (let ((cpl (class-precedence-list class)))
1045                (or (member *the-class-slot-class* cpl :test #'eq)
1046                    (member *the-class-standard-effective-slot-definition*
1047                            cpl :test #'eq))))
1048     (let ((gf-table (make-hash-table :test 'eq)))
1049       (labels ((collect-gfs (class)
1050                  (dolist (gf (specializer-direct-generic-functions class))
1051                    (setf (gethash gf gf-table) t))
1052                  (mapc #'collect-gfs (class-direct-superclasses class))))
1053         (collect-gfs class)
1054         (maphash (lambda (gf ignore)
1055                    (declare (ignore ignore))
1056                    (update-gf-dfun class gf))
1057                  gf-table)))))
1058 \f
1059 (defmethod compute-default-initargs ((class slot-class))
1060   (let ((initargs (loop for c in (class-precedence-list class)
1061                         append (class-direct-default-initargs c))))
1062     (delete-duplicates initargs :test #'eq :key #'car :from-end t)))
1063 \f
1064 ;;;; protocols for constructing direct and effective slot definitions
1065
1066 (defmethod direct-slot-definition-class ((class std-class) &rest initargs)
1067   (declare (ignore initargs))
1068   (find-class 'standard-direct-slot-definition))
1069
1070 (defun make-direct-slotd (class initargs)
1071   (apply #'make-instance
1072          (apply #'direct-slot-definition-class class initargs)
1073          :class class
1074          initargs))
1075
1076 ;;; I (CSR) am not sure, but I believe that the particular order of
1077 ;;; slots is quite important: it is ideal to attempt to have a
1078 ;;; constant slot location for the same notional slots as much as
1079 ;;; possible, so that clever discriminating functions (ONE-INDEX et
1080 ;;; al.) have a chance of working.  The below at least walks through
1081 ;;; the slots predictably, but maybe it would be good to compute some
1082 ;;; kind of optimal slot layout by looking at locations of slots in
1083 ;;; superclasses?
1084 (defun std-compute-slots (class)
1085   ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
1086   ;; for each different slot name we find in our superclasses. Each
1087   ;; call receives the class and a list of the dslotds with that name.
1088   ;; The list is in most-specific-first order.
1089   (let ((name-dslotds-alist ()))
1090     (dolist (c (reverse (class-precedence-list class)))
1091       (dolist (slot (class-direct-slots c))
1092         (let* ((name (slot-definition-name slot))
1093                (entry (assq name name-dslotds-alist)))
1094           (if entry
1095               (push slot (cdr entry))
1096               (push (list name slot) name-dslotds-alist)))))
1097     (mapcar (lambda (direct)
1098               (compute-effective-slot-definition class
1099                                                  (car direct)
1100                                                  (cdr direct)))
1101             (nreverse name-dslotds-alist))))
1102
1103 (defmethod compute-slots ((class standard-class))
1104   (std-compute-slots class))
1105 (defmethod compute-slots ((class funcallable-standard-class))
1106   (std-compute-slots class))
1107
1108 (defun std-compute-slots-around (class eslotds)
1109   (let ((location -1)
1110         (safe (safe-p class)))
1111     (dolist (eslotd eslotds eslotds)
1112       (setf (slot-definition-location eslotd)
1113             (case (slot-definition-allocation eslotd)
1114               (:instance
1115                (incf location))
1116               (:class
1117                (let* ((name (slot-definition-name eslotd))
1118                       (from-class
1119                        (or
1120                         (slot-definition-allocation-class eslotd)
1121                         ;; we get here if the user adds an extra slot
1122                         ;; himself...
1123                         (setf (slot-definition-allocation-class eslotd)
1124                               class)))
1125                       ;; which raises the question of what we should
1126                       ;; do if we find that said user has added a slot
1127                       ;; with the same name as another slot...
1128                       (cell (or (assq name (class-slot-cells from-class))
1129                                 (let ((c (cons name +slot-unbound+)))
1130                                   (push c (class-slot-cells from-class))
1131                                   c))))
1132                  (aver (consp cell))
1133                  (if (eq +slot-unbound+ (cdr cell))
1134                      ;; We may have inherited an initfunction FIXME: Is this
1135                      ;; really right? Is the initialization in
1136                      ;; SHARED-INITIALIZE (STD-CLASS) not enough?
1137                      (let ((initfun (slot-definition-initfunction eslotd)))
1138                        (if initfun
1139                            (rplacd cell (call-initfun initfun eslotd safe))
1140                            cell))
1141                      cell)))))
1142       (unless (slot-definition-class eslotd)
1143         (setf (slot-definition-class eslotd) class))
1144       (initialize-internal-slot-functions eslotd))))
1145
1146 (defmethod compute-slots :around ((class standard-class))
1147   (let ((eslotds (call-next-method)))
1148     (std-compute-slots-around class eslotds)))
1149 (defmethod compute-slots :around ((class funcallable-standard-class))
1150   (let ((eslotds (call-next-method)))
1151     (std-compute-slots-around class eslotds)))
1152
1153 (defmethod compute-slots ((class structure-class))
1154   (mapcan (lambda (superclass)
1155             (mapcar (lambda (dslotd)
1156                       (compute-effective-slot-definition
1157                        class
1158                        (slot-definition-name dslotd)
1159                        (list dslotd)))
1160                     (class-direct-slots superclass)))
1161           (reverse (slot-value class '%class-precedence-list))))
1162
1163 (defmethod compute-slots :around ((class structure-class))
1164   (let ((eslotds (call-next-method)))
1165     (mapc #'finalize-internal-slot-functions eslotds)
1166     eslotds))
1167
1168 (defmethod compute-effective-slot-definition ((class slot-class) name dslotds)
1169   (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
1170          (class (apply #'effective-slot-definition-class class initargs))
1171          (slotd (apply #'make-instance class initargs)))
1172     slotd))
1173
1174 (defmethod effective-slot-definition-class ((class std-class) &rest initargs)
1175   (declare (ignore initargs))
1176   (find-class 'standard-effective-slot-definition))
1177
1178 (defmethod effective-slot-definition-class ((class structure-class) &rest initargs)
1179   (declare (ignore initargs))
1180   (find-class 'structure-effective-slot-definition))
1181
1182 (defmethod compute-effective-slot-definition-initargs
1183     ((class slot-class) direct-slotds)
1184   (let* ((name nil)
1185          (initfunction nil)
1186          (initform nil)
1187          (initargs nil)
1188          (allocation nil)
1189          (allocation-class nil)
1190          (type t)
1191          (documentation nil)
1192          (documentationp nil)
1193          (namep  nil)
1194          (initp  nil)
1195          (allocp nil))
1196
1197     (dolist (slotd direct-slotds)
1198       (when slotd
1199         (unless namep
1200           (setq name (slot-definition-name slotd)
1201                 namep t))
1202         (unless initp
1203           (awhen (slot-definition-initfunction slotd)
1204             (setq initform (slot-definition-initform slotd)
1205                   initfunction it
1206                   initp t)))
1207         (unless documentationp
1208           (awhen (%slot-definition-documentation slotd)
1209             (setq documentation it
1210                   documentationp t)))
1211         (unless allocp
1212           (setq allocation (slot-definition-allocation slotd)
1213                 allocation-class (slot-definition-class slotd)
1214                 allocp t))
1215         (setq initargs (append (slot-definition-initargs slotd) initargs))
1216         (let ((slotd-type (slot-definition-type slotd)))
1217           (setq type (cond
1218                        ((eq type t) slotd-type)
1219                        ;; This pairwise type intersection is perhaps a
1220                        ;; little inefficient and inelegant, but it's
1221                        ;; unlikely to lie on the critical path.  Shout
1222                        ;; if I'm wrong.  -- CSR, 2005-11-24
1223                        (t (type-specifier
1224                            (specifier-type `(and ,type ,slotd-type)))))))))
1225     (list :name name
1226           :initform initform
1227           :initfunction initfunction
1228           :initargs initargs
1229           :allocation allocation
1230           :allocation-class allocation-class
1231           :type type
1232           :class class
1233           :documentation documentation)))
1234
1235 (defmethod compute-effective-slot-definition-initargs :around
1236     ((class structure-class) direct-slotds)
1237   (let* ((slotd (car direct-slotds))
1238          (accessor (slot-definition-defstruct-accessor-symbol slotd)))
1239     (list* :defstruct-accessor-symbol accessor
1240            :internal-reader-function
1241            (slot-definition-internal-reader-function slotd)
1242            :internal-writer-function
1243            (slot-definition-internal-writer-function slotd)
1244            (call-next-method))))
1245 \f
1246 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
1247 ;;;       to make the method object. They have to use make-a-method which
1248 ;;;       is a specially bootstrapped mechanism for making standard methods.
1249 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
1250   (declare (ignore direct-slot initargs))
1251   (find-class 'standard-reader-method))
1252
1253 (defmethod add-reader-method ((class slot-class) generic-function slot-name slot-documentation source-location)
1254   (add-method generic-function
1255               (make-a-method 'standard-reader-method
1256                              ()
1257                              (list (or (class-name class) 'object))
1258                              (list class)
1259                              (make-reader-method-function class slot-name)
1260                              (or slot-documentation "automatically generated reader method")
1261                              :slot-name slot-name
1262                              :object-class class
1263                              :method-class-function #'reader-method-class
1264                              :definition-source source-location)))
1265
1266 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
1267   (declare (ignore direct-slot initargs))
1268   (find-class 'standard-writer-method))
1269
1270 (defmethod add-writer-method ((class slot-class) generic-function slot-name slot-documentation source-location)
1271   (add-method generic-function
1272               (make-a-method 'standard-writer-method
1273                              ()
1274                              (list 'new-value (or (class-name class) 'object))
1275                              (list *the-class-t* class)
1276                              (make-writer-method-function class slot-name)
1277                              (or slot-documentation "automatically generated writer method")
1278                              :slot-name slot-name
1279                              :object-class class
1280                              :method-class-function #'writer-method-class
1281                              :definition-source source-location)))
1282
1283 (defmethod add-boundp-method ((class slot-class) generic-function slot-name slot-documentation source-location)
1284   (add-method generic-function
1285               (make-a-method (constantly (find-class 'standard-boundp-method))
1286                              class
1287                              ()
1288                              (list (or (class-name class) 'object))
1289                              (list class)
1290                              (make-boundp-method-function class slot-name)
1291                              (or slot-documentation "automatically generated boundp method")
1292                              :slot-name slot-name
1293                              :definition-source source-location)))
1294
1295 (defmethod remove-reader-method ((class slot-class) generic-function)
1296   (let ((method (get-method generic-function () (list class) nil)))
1297     (when method (remove-method generic-function method))))
1298
1299 (defmethod remove-writer-method ((class slot-class) generic-function)
1300   (let ((method
1301           (get-method generic-function () (list *the-class-t* class) nil)))
1302     (when method (remove-method generic-function method))))
1303
1304 (defmethod remove-boundp-method ((class slot-class) generic-function)
1305   (let ((method (get-method generic-function () (list class) nil)))
1306     (when method (remove-method generic-function method))))
1307 \f
1308 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITER-METHOD-FUNCTION
1309 ;;; function are NOT part of the standard protocol. They are however
1310 ;;; useful; PCL makes use of them internally and documents them for
1311 ;;; PCL users.  (FIXME: but SBCL certainly doesn't)
1312 ;;;
1313 ;;; *** This needs work to make type testing by the writer functions which
1314 ;;; *** do type testing faster. The idea would be to have one constructor
1315 ;;; *** for each possible type test.
1316 ;;;
1317 ;;; *** There is a subtle bug here which is going to have to be fixed.
1318 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1319 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1320 ;;; *** defined for this metaclass a chance to run.
1321
1322 (defmethod make-reader-method-function ((class slot-class) slot-name)
1323   (make-std-reader-method-function class slot-name))
1324
1325 (defmethod make-writer-method-function ((class slot-class) slot-name)
1326   (make-std-writer-method-function class slot-name))
1327
1328 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1329   (make-std-boundp-method-function class slot-name))
1330 \f
1331 (defmethod compatible-meta-class-change-p (class proto-new-class)
1332   (eq (class-of class) (class-of proto-new-class)))
1333
1334 (defmethod validate-superclass ((class class) (superclass class))
1335   (or (eq superclass *the-class-t*)
1336       (eq (class-of class) (class-of superclass))
1337       (and (eq (class-of superclass) *the-class-standard-class*)
1338            (eq (class-of class) *the-class-funcallable-standard-class*))
1339       (and (eq (class-of superclass) *the-class-funcallable-standard-class*)
1340            (eq (class-of class) *the-class-standard-class*))))
1341 \f
1342 ;;; What this does depends on which of the four possible values of
1343 ;;; LAYOUT-INVALID the PCL wrapper has; the simplest case is when it
1344 ;;; is (:FLUSH <wrapper>) or (:OBSOLETE <wrapper>), when there is
1345 ;;; nothing to do, as the new wrapper has already been created.  If
1346 ;;; LAYOUT-INVALID returns NIL, then we invalidate it (setting it to
1347 ;;; (:FLUSH <wrapper>); UPDATE-SLOTS later gets to choose whether or
1348 ;;; not to "upgrade" this to (:OBSOLETE <wrapper>).
1349 ;;;
1350 ;;; This leaves the case where LAYOUT-INVALID returns T, which happens
1351 ;;; when REGISTER-LAYOUT has invalidated a superclass of CLASS (which
1352 ;;; invalidated all the subclasses in SB-KERNEL land).  Again, here we
1353 ;;; must flush the caches and allow UPDATE-SLOTS to decide whether to
1354 ;;; obsolete the wrapper.
1355 ;;;
1356 ;;; FIXME: either here or in INVALID-WRAPPER-P looks like a good place
1357 ;;; for (AVER (NOT (EQ (LAYOUT-INVALID OWRAPPER)
1358 ;;;                    :UNINITIALIZED)))
1359 ;;;
1360 ;;; Thanks to Gerd Moellmann for the explanation.  -- CSR, 2002-10-29
1361 (defun %force-cache-flushes (class)
1362   (let* ((owrapper (class-wrapper class)))
1363     ;; We only need to do something if the wrapper is still valid. If
1364     ;; the wrapper isn't valid, state will be FLUSH or OBSOLETE, and
1365     ;; both of those will already be doing what we want. In
1366     ;; particular, we must be sure we never change an OBSOLETE into a
1367     ;; FLUSH since OBSOLETE means do what FLUSH does and then some.
1368     (when (or (not (invalid-wrapper-p owrapper))
1369               ;; KLUDGE: despite the observations above, this remains
1370               ;; a violation of locality or what might be considered
1371               ;; good style.  There has to be a better way!  -- CSR,
1372               ;; 2002-10-29
1373               (eq (layout-invalid owrapper) t))
1374       (let ((nwrapper (make-wrapper (layout-length owrapper)
1375                                     class)))
1376         (setf (wrapper-slots nwrapper)
1377               (wrapper-slots owrapper))
1378         (setf (wrapper-slot-table nwrapper)
1379               (wrapper-slot-table owrapper))
1380         (%update-lisp-class-layout class nwrapper)
1381         (setf (slot-value class 'wrapper) nwrapper)
1382         ;; Use :OBSOLETE instead of :FLUSH if any superclass has
1383         ;; been obsoleted.
1384         (if (find-if (lambda (x)
1385                        (and (consp x) (eq :obsolete (car x))))
1386                      (layout-inherits owrapper)
1387                      :key #'layout-invalid)
1388             (%invalidate-wrapper owrapper :obsolete nwrapper)
1389             (%invalidate-wrapper owrapper :flush nwrapper))))))
1390 \f
1391 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1392 ;;; the next access to the instance (as defined in 88-002R) to trap
1393 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1394 (defmethod make-instances-obsolete ((class std-class))
1395   (with-world-lock ()
1396     (let* ((owrapper (class-wrapper class))
1397            (nwrapper (make-wrapper (layout-length owrapper)
1398                                    class)))
1399       (unless (class-finalized-p class)
1400         (if (class-has-a-forward-referenced-superclass-p class)
1401             (return-from make-instances-obsolete class)
1402             (%update-cpl class (compute-class-precedence-list class))))
1403       (setf (wrapper-slots nwrapper)
1404             (wrapper-slots owrapper))
1405       (setf (wrapper-slot-table nwrapper)
1406             (wrapper-slot-table owrapper))
1407       (%update-lisp-class-layout class nwrapper)
1408       (setf (slot-value class 'wrapper) nwrapper)
1409       (%invalidate-wrapper owrapper :obsolete nwrapper)
1410       class)))
1411
1412 (defmethod make-instances-obsolete ((class symbol))
1413   (make-instances-obsolete (find-class class))
1414   ;; ANSI wants the class name when called with a symbol.
1415   class)
1416
1417 ;;; OBSOLETE-INSTANCE-TRAP is the internal trap that is called when we
1418 ;;; see an obsolete instance. The times when it is called are:
1419 ;;;   - when the instance is involved in method lookup
1420 ;;;   - when attempting to access a slot of an instance
1421 ;;;
1422 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1423 ;;; instance access macros.
1424 ;;;
1425 ;;; Of course these times when it is called are an internal
1426 ;;; implementation detail of PCL and are not part of the documented
1427 ;;; description of when the obsolete instance update happens. The
1428 ;;; documented description is as it appears in 88-002R.
1429 ;;;
1430 ;;; This has to return the new wrapper, so it counts on all the
1431 ;;; methods on obsolete-instance-trap-internal to return the new
1432 ;;; wrapper. It also does a little internal error checking to make
1433 ;;; sure that the traps are only happening when they should, and that
1434 ;;; the trap methods are computing appropriate new wrappers.
1435
1436 ;;; OBSOLETE-INSTANCE-TRAP might be called on structure instances
1437 ;;; after a structure is redefined. In most cases,
1438 ;;; OBSOLETE-INSTANCE-TRAP will not be able to fix the old instance,
1439 ;;; so it must signal an error. The hard part of this is that the
1440 ;;; error system and debugger might cause OBSOLETE-INSTANCE-TRAP to be
1441 ;;; called again, so in that case, we have to return some reasonable
1442 ;;; wrapper, instead.
1443
1444 (defvar *in-obsolete-instance-trap* nil)
1445 (defvar *the-wrapper-of-structure-object*
1446   (class-wrapper (find-class 'structure-object)))
1447
1448 (define-condition obsolete-structure (error)
1449   ((datum :reader obsolete-structure-datum :initarg :datum))
1450   (:report
1451    (lambda (condition stream)
1452      ;; Don't try to print the structure, since it probably won't work.
1453      (format stream
1454              "~@<obsolete structure error for a structure of type ~2I~_~S~:>"
1455              (type-of (obsolete-structure-datum condition))))))
1456
1457 (defun %obsolete-instance-trap (owrapper nwrapper instance)
1458   (if (not (layout-for-std-class-p owrapper))
1459       (if *in-obsolete-instance-trap*
1460           *the-wrapper-of-structure-object*
1461           (let ((*in-obsolete-instance-trap* t))
1462             (error 'obsolete-structure :datum instance)))
1463       (let* ((class (wrapper-class* nwrapper))
1464              (copy (allocate-instance class)) ;??? allocate-instance ???
1465              (oslots (get-slots instance))
1466              (nslots (get-slots copy))
1467              (added ())
1468              (discarded ())
1469              (plist ())
1470              (safe (safe-p class)))
1471
1472         ;; local  --> local     transfer value, check type
1473         ;; local  --> shared    discard value, discard slot
1474         ;; local  -->  --       discard slot
1475         ;; local  --> custom    XXX
1476
1477         ;; shared --> local     transfer value, check type
1478         ;; shared --> shared    -- (cf SHARED-INITIALIZE :AFTER STD-CLASS)
1479         ;; shared -->  --       discard value
1480         ;; shared --> custom    XXX
1481
1482         ;;  --    --> local     add slot
1483         ;;  --    --> shared    --
1484         ;;  --    --> custom    XXX
1485
1486         (multiple-value-bind (new-instance-slots new-class-slots new-custom-slots)
1487             (classify-slotds (wrapper-slots nwrapper))
1488           (declare (ignore new-class-slots))
1489           (multiple-value-bind (old-instance-slots old-class-slots old-custom-slots)
1490               (classify-slotds (wrapper-slots owrapper))
1491
1492             (let ((layout (mapcar (lambda (slotd)
1493                                     ;; Get the names only once.
1494                                     (cons (slot-definition-name slotd) slotd))
1495                                   new-instance-slots)))
1496
1497               (flet ((set-value (value cell)
1498                        (let ((name (car cell))
1499                              (slotd (cdr cell)))
1500                          (when (and safe (neq value +slot-unbound+))
1501                            (let ((type (slot-definition-type slotd)))
1502                              (assert
1503                               (typep value type) (value)
1504                               "~@<Error updating obsolete instance. Current value in slot ~
1505                                ~S of an instance of ~S is ~S, which does not match the new ~
1506                                slot type ~S.~:@>"
1507                               name class value type)))
1508                          (setf (clos-slots-ref nslots (slot-definition-location slotd)) value
1509                                ;; Prune from the list now that it's been dealt with.
1510                                layout (remove cell layout)))))
1511
1512                 ;; Go through all the old local slots.
1513                 (dolist (old old-instance-slots)
1514                   (let* ((name (slot-definition-name old))
1515                          (value (clos-slots-ref oslots (slot-definition-location old))))
1516                     (unless (eq value +slot-unbound+)
1517                       (let ((new (assq name layout)))
1518                         (cond (new
1519                                (set-value value new))
1520                               (t
1521                                (push name discarded)
1522                                (setf (getf plist name) value)))))))
1523
1524                 ;; Go through all the old shared slots.
1525                 (dolist (old old-class-slots)
1526                   (let* ((cell (slot-definition-location old))
1527                          (name (car cell))
1528                          (new (assq name layout)))
1529                     (when new
1530                       (set-value (cdr cell) new))))
1531
1532                 ;; Go through all custom slots to find added ones. CLHS
1533                 ;; doesn't specify what to do about them, and neither does
1534                 ;; AMOP. We do want them to get initialized, though, so we
1535                 ;; list them in ADDED for the benefit of SHARED-INITIALIZE.
1536                 (dolist (new new-custom-slots)
1537                   (let* ((name (slot-definition-name new))
1538                          (old (find name old-custom-slots :key #'slot-definition-name)))
1539                     (unless old
1540                       (push name added))))
1541
1542                 ;; Go through all the remaining new local slots to compute the added slots.
1543                 (dolist (cell layout)
1544                   (push (car cell) added))))))
1545
1546         (%swap-wrappers-and-slots instance copy)
1547
1548         (update-instance-for-redefined-class instance
1549                                              added
1550                                              discarded
1551                                              plist)
1552         nwrapper)))
1553 \f
1554 (defun %change-class (instance new-class initargs)
1555   (let* ((old-class (class-of instance))
1556          (copy (allocate-instance new-class))
1557          (new-wrapper (get-wrapper copy))
1558          (old-wrapper (class-wrapper old-class))
1559          (old-slots (get-slots instance))
1560          (new-slots (get-slots copy))
1561          (safe (safe-p new-class)))
1562     (multiple-value-bind (new-instance-slots new-class-slots)
1563         (classify-slotds (wrapper-slots new-wrapper))
1564       (multiple-value-bind (old-instance-slots old-class-slots)
1565           (classify-slotds (wrapper-slots old-wrapper))
1566
1567         (flet ((set-value (value slotd)
1568                  (when safe
1569                    (assert (typep value (slot-definition-type slotd)) (value)
1570                            "~@<Error changing class. Current value in slot ~S ~
1571                             of an instance of ~S is ~S, which does not match the new ~
1572                             slot type ~S in class ~S.~:@>"
1573                            (slot-definition-name slotd) old-class value
1574                            (slot-definition-type slotd) new-class))
1575                  (setf (clos-slots-ref new-slots (slot-definition-location slotd)) value)))
1576
1577           ;; "The values of local slots specified by both the class CTO and
1578           ;; CFROM are retained. If such a local slot was unbound, it
1579           ;; remains unbound."
1580           (dolist (new new-instance-slots)
1581             (let* ((name (slot-definition-name new))
1582                    (old (find name old-instance-slots :key #'slot-definition-name)))
1583               (when old
1584                 (set-value (clos-slots-ref old-slots (slot-definition-location old))
1585                            new))))
1586
1587           ;; "The values of slots specified as shared in the class CFROM and
1588           ;; as local in the class CTO are retained."
1589           (dolist (old old-class-slots)
1590             (let* ((slot-and-val (slot-definition-location old))
1591                    (new (find (car slot-and-val) new-instance-slots
1592                               :key #'slot-definition-name)))
1593               (when new
1594                 (set-value (cdr slot-and-val) new)))))))
1595
1596     ;; Make the copy point to the old instance's storage, and make the
1597     ;; old instance point to the new storage.
1598     (%swap-wrappers-and-slots instance copy)
1599
1600     (apply #'update-instance-for-different-class copy instance initargs)
1601
1602     instance))
1603
1604 (defmethod change-class ((instance standard-object) (new-class standard-class)
1605                          &rest initargs)
1606   (with-world-lock ()
1607     (unless (class-finalized-p new-class)
1608       (finalize-inheritance new-class))
1609     (let ((cpl (class-precedence-list new-class)))
1610       (dolist (class cpl)
1611         (macrolet
1612             ((frob (class-name)
1613                `(when (eq class (find-class ',class-name))
1614                   (error 'metaobject-initialization-violation
1615                          :format-control "~@<Cannot ~S objects into ~S metaobjects.~@:>"
1616                          :format-arguments (list 'change-class ',class-name)
1617                          :references (list '(:amop :initialization ,class-name))))))
1618           (frob class)
1619           (frob generic-function)
1620           (frob method)
1621           (frob slot-definition))))
1622     (%change-class instance new-class initargs)))
1623
1624 (defmethod change-class ((instance forward-referenced-class)
1625                          (new-class standard-class) &rest initargs)
1626   (with-world-lock ()
1627     (let ((cpl (class-precedence-list new-class)))
1628       (dolist (class cpl
1629                (error 'metaobject-initialization-violation
1630                       :format-control
1631                       "~@<Cannot ~S ~S objects into non-~S objects.~@:>"
1632                       :format-arguments
1633                       (list 'change-class 'forward-referenced-class 'class)
1634                       :references
1635                       (list '(:amop :generic-function ensure-class-using-class)
1636                             '(:amop :initialization class))))
1637         (when (eq class (find-class 'class))
1638           (return nil))))
1639     (%change-class instance new-class initargs)))
1640
1641 (defmethod change-class ((instance funcallable-standard-object)
1642                          (new-class funcallable-standard-class)
1643                          &rest initargs)
1644   (with-world-lock ()
1645     (let ((cpl (class-precedence-list new-class)))
1646       (dolist (class cpl)
1647         (macrolet
1648             ((frob (class-name)
1649                `(when (eq class (find-class ',class-name))
1650                   (error 'metaobject-initialization-violation
1651                          :format-control "~@<Cannot ~S objects into ~S metaobjects.~@:>"
1652                          :format-arguments (list 'change-class ',class-name)
1653                          :references (list '(:amop :initialization ,class-name))))))
1654           (frob class)
1655           (frob generic-function)
1656           (frob method)
1657           (frob slot-definition))))
1658     (%change-class instance new-class initargs)))
1659
1660 (defmethod change-class ((instance standard-object)
1661                          (new-class funcallable-standard-class)
1662                          &rest initargs)
1663   (declare (ignore initargs))
1664   (error "You can't change the class of ~S to ~S~@
1665           because it isn't already an instance with metaclass ~S."
1666          instance new-class 'standard-class))
1667
1668 (defmethod change-class ((instance funcallable-standard-object)
1669                          (new-class standard-class)
1670                          &rest initargs)
1671   (declare (ignore initargs))
1672   (error "You can't change the class of ~S to ~S~@
1673           because it isn't already an instance with metaclass ~S."
1674          instance new-class 'funcallable-standard-class))
1675
1676 (defmethod change-class ((instance t) (new-class-name symbol) &rest initargs)
1677   (apply #'change-class instance (find-class new-class-name) initargs))
1678 \f
1679 ;;;; The metaclass BUILT-IN-CLASS
1680 ;;;;
1681 ;;;; This metaclass is something of a weird creature. By this point, all
1682 ;;;; instances of it which will exist have been created, and no instance
1683 ;;;; is ever created by calling MAKE-INSTANCE.
1684 ;;;;
1685 ;;;; But, there are other parts of the protocol we must follow and those
1686 ;;;; definitions appear here.
1687
1688 (macrolet ((def (name args control)
1689                `(defmethod ,name ,args
1690                  (declare (ignore initargs))
1691                  (error 'metaobject-initialization-violation
1692                   :format-control ,(format nil "~@<~A~@:>" control)
1693                   :format-arguments (list ',name)
1694                   :references (list '(:amop :initialization "Class"))))))
1695   (def initialize-instance ((class built-in-class) &rest initargs)
1696     "Cannot ~S an instance of BUILT-IN-CLASS.")
1697   (def reinitialize-instance ((class built-in-class) &rest initargs)
1698     "Cannot ~S an instance of BUILT-IN-CLASS."))
1699
1700 (macrolet ((def (name)
1701                `(defmethod ,name ((class built-in-class)) nil)))
1702   (def class-direct-slots)
1703   (def class-slots)
1704   (def class-direct-default-initargs)
1705   (def class-default-initargs))
1706
1707 (defmethod validate-superclass ((c class) (s built-in-class))
1708   (or (eq s *the-class-t*) (eq s *the-class-stream*)
1709       ;; FIXME: bad things happen if someone tries to mix in both
1710       ;; FILE-STREAM and STRING-STREAM (as they have the same
1711       ;; layout-depthoid).  Is there any way we can provide a useful
1712       ;; error message?  -- CSR, 2005-05-03
1713       (eq s *the-class-file-stream*) (eq s *the-class-string-stream*)
1714       ;; This probably shouldn't be mixed in with certain other
1715       ;; classes, too, but it seems to work both with STANDARD-OBJECT
1716       ;; and FUNCALLABLE-STANDARD-OBJECT
1717       (eq s *the-class-sequence*)))
1718 \f
1719 ;;; Some necessary methods for FORWARD-REFERENCED-CLASS
1720 (defmethod class-direct-slots ((class forward-referenced-class)) ())
1721 (defmethod class-direct-default-initargs ((class forward-referenced-class)) ())
1722 (macrolet ((def (method)
1723              `(defmethod ,method ((class forward-referenced-class))
1724                 (error "~@<~I~S was called on a forward referenced class:~2I~_~S~:>"
1725                        ',method class))))
1726   (def class-default-initargs)
1727   (def class-precedence-list)
1728   (def class-slots))
1729
1730 (defmethod validate-superclass ((c slot-class)
1731                                 (f forward-referenced-class))
1732   t)
1733 \f
1734 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1735   (pushnew dependent (plist-value metaobject 'dependents) :test #'eq))
1736
1737 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1738   (setf (plist-value metaobject 'dependents)
1739         (delete dependent (plist-value metaobject 'dependents))))
1740
1741 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1742   (dolist (dependent (plist-value metaobject 'dependents))
1743     (funcall function dependent)))
1744