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