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