0.8.19.15:
[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 (if (fboundp gfspec)
748                          (without-package-locks 
749                            (ensure-generic-function gfspec))
750                          (ensure-generic-function 
751                           gfspec :lambda-list (case r/w 
752                                                 (r '(object)) 
753                                                 (w '(new-value object)))))))
754              (case r/w
755                (r (if (eq add/remove 'add)
756                       (add-reader-method class gf name)
757                       (remove-reader-method class gf)))
758                (w (if (eq add/remove 'add)
759                       (add-writer-method class gf name)
760                       (remove-writer-method class gf)))))))
761     (dolist (dslotd dslotds)
762       (let ((slot-name (slot-definition-name dslotd)))
763         (dolist (r (slot-definition-readers dslotd)) 
764           (fix r slot-name 'r))
765         (dolist (w (slot-definition-writers dslotd)) 
766           (fix w slot-name 'w))))))
767 \f
768 (defun add-direct-subclasses (class supers)
769   (dolist (super supers)
770     (unless (memq class (class-direct-subclasses class))
771       (add-direct-subclass super class))))
772
773 (defmethod finalize-inheritance ((class std-class))
774   (update-class class t))
775
776 (defmethod finalize-inheritance ((class forward-referenced-class))
777   ;; FIXME: should we not be thinking a bit about what kinds of error
778   ;; we're throwing?  Maybe we need a clos-error type to mix in?  Or
779   ;; possibly a forward-referenced-class-error, though that's
780   ;; difficult given e.g. class precedence list calculations...
781   (error
782    "~@<FINALIZE-INHERITANCE was called on a forward referenced class:~
783        ~2I~_~S~:>"
784    class))
785
786 \f
787 (defun class-has-a-forward-referenced-superclass-p (class)
788   (or (forward-referenced-class-p class)
789       (some #'class-has-a-forward-referenced-superclass-p
790             (class-direct-superclasses class))))
791
792 ;;; This is called by :after shared-initialize whenever a class is initialized
793 ;;; or reinitialized. The class may or may not be finalized.
794 (defun update-class (class finalizep)
795   ;; Comment from Gerd Moellmann:
796   ;;
797   ;; Note that we can't simply delay the finalization when CLASS has
798   ;; no forward referenced superclasses because that causes bootstrap
799   ;; problems.
800   (without-package-locks
801    (when (and (not finalizep)
802               (not (class-finalized-p class))
803               (not (class-has-a-forward-referenced-superclass-p class)))
804      (finalize-inheritance class)
805      (return-from update-class))
806    (when (or finalizep (class-finalized-p class)
807              (not (class-has-a-forward-referenced-superclass-p class)))
808      (setf (find-class (class-name class)) class)
809      (update-cpl class (compute-class-precedence-list class))
810      ;; This invocation of UPDATE-SLOTS, in practice, finalizes the
811      ;; class.  The hoops above are to ensure that FINALIZE-INHERITANCE
812      ;; is called at finalization, so that MOP programmers can hook
813      ;; into the system as described in "Class Finalization Protocol"
814      ;; (section 5.5.2 of AMOP).
815      (update-slots class (compute-slots class))
816      (update-gfs-of-class class)
817      (update-initargs class (compute-default-initargs class))
818      (update-ctors 'finalize-inheritance :class class))
819    (unless finalizep
820      (dolist (sub (class-direct-subclasses class)) (update-class sub nil)))))
821
822 (defun update-cpl (class cpl)
823   (if (class-finalized-p class)
824       (unless (and (equal (class-precedence-list class) cpl)
825                    (dolist (c cpl t)
826                      (when (position :class (class-direct-slots c)
827                                      :key #'slot-definition-allocation)
828                        (return nil))))
829         ;; comment from the old CMU CL sources:
830         ;;   Need to have the cpl setup before update-lisp-class-layout
831         ;;   is called on CMU CL.
832         (setf (slot-value class 'class-precedence-list) cpl)
833         (setf (slot-value class 'cpl-available-p) t)
834         (force-cache-flushes class))
835       (progn
836         (setf (slot-value class 'class-precedence-list) cpl)
837         (setf (slot-value class 'cpl-available-p) t)))
838   (update-class-can-precede-p cpl))
839
840 (defun update-class-can-precede-p (cpl)
841   (when cpl
842     (let ((first (car cpl)))
843       (dolist (c (cdr cpl))
844         (pushnew c (slot-value first 'can-precede-list))))
845     (update-class-can-precede-p (cdr cpl))))
846
847 (defun class-can-precede-p (class1 class2)
848   (member class2 (class-can-precede-list class1)))
849
850 (defun update-slots (class eslotds)
851   (let ((instance-slots ())
852         (class-slots    ()))
853     (dolist (eslotd eslotds)
854       (let ((alloc (slot-definition-allocation eslotd)))
855         (case alloc
856           (:instance (push eslotd instance-slots))
857           (:class (push eslotd class-slots)))))
858
859     ;; If there is a change in the shape of the instances then the
860     ;; old class is now obsolete.
861     (let* ((nlayout (mapcar #'slot-definition-name
862                             (sort instance-slots #'<
863                                   :key #'slot-definition-location)))
864            (nslots (length nlayout))
865            (nwrapper-class-slots (compute-class-slots class-slots))
866            (owrapper (when (class-finalized-p class)
867                        (class-wrapper class)))
868            (olayout (when owrapper
869                       (wrapper-instance-slots-layout owrapper)))
870            (owrapper-class-slots (and owrapper (wrapper-class-slots owrapper)))
871            (nwrapper
872             (cond ((null owrapper)
873                    (make-wrapper nslots class))
874                   ((and (equal nlayout olayout)
875                         (not
876                          (loop for o in owrapper-class-slots
877                                for n in nwrapper-class-slots
878                                do (unless (eq (car o) (car n)) (return t)))))
879                    owrapper)
880                   (t
881                    ;; This will initialize the new wrapper to have the
882                    ;; same state as the old wrapper. We will then have
883                    ;; to change that. This may seem like wasted work
884                    ;; (and it is), but the spec requires that we call
885                    ;; MAKE-INSTANCES-OBSOLETE.
886                    (make-instances-obsolete class)
887                    (class-wrapper class)))))
888
889       (with-slots (wrapper slots) class
890         (update-lisp-class-layout class nwrapper)
891         (setf slots eslotds
892               (wrapper-instance-slots-layout nwrapper) nlayout
893               (wrapper-class-slots nwrapper) nwrapper-class-slots
894               (wrapper-no-of-instance-slots nwrapper) nslots
895               wrapper nwrapper))
896       (setf (slot-value class 'finalized-p) t)
897       (unless (eq owrapper nwrapper)
898         (update-pv-table-cache-info class)
899         (maybe-update-standard-class-locations class)))))
900
901 (defun compute-class-slots (eslotds)
902   (let (collect)
903     (dolist (eslotd eslotds)
904       (push (assoc (slot-definition-name eslotd)
905                    (class-slot-cells (slot-definition-class eslotd)))
906             collect))
907     (nreverse collect)))
908
909 (defun update-gfs-of-class (class)
910   (when (and (class-finalized-p class)
911              (let ((cpl (class-precedence-list class)))
912                (or (member *the-class-slot-class* cpl)
913                    (member *the-class-standard-effective-slot-definition*
914                            cpl))))
915     (let ((gf-table (make-hash-table :test 'eq)))
916       (labels ((collect-gfs (class)
917                  (dolist (gf (specializer-direct-generic-functions class))
918                    (setf (gethash gf gf-table) t))
919                  (mapc #'collect-gfs (class-direct-superclasses class))))
920         (collect-gfs class)
921         (maphash (lambda (gf ignore)
922                    (declare (ignore ignore))
923                    (update-gf-dfun class gf))
924                  gf-table)))))
925
926 (defun update-initargs (class inits)
927   (setf (plist-value class 'default-initargs) inits))
928 \f
929 (defmethod compute-default-initargs ((class slot-class))
930   (let ((initargs (loop for c in (class-precedence-list class)
931                         append (class-direct-default-initargs c))))
932     (delete-duplicates initargs :test #'eq :key #'car :from-end t)))
933 \f
934 ;;;; protocols for constructing direct and effective slot definitions
935
936 (defmethod direct-slot-definition-class ((class std-class) &rest initargs)
937   (declare (ignore initargs))
938   (find-class 'standard-direct-slot-definition))
939
940 (defun make-direct-slotd (class initargs)
941   (apply #'make-instance
942          (apply #'direct-slot-definition-class class initargs)
943          :class class
944          initargs))
945
946 ;;; I (CSR) am not sure, but I believe that the particular order of
947 ;;; slots is quite important: it is ideal to attempt to have a
948 ;;; constant slot location for the same notional slots as much as
949 ;;; possible, so that clever discriminating functions (ONE-INDEX et
950 ;;; al.) have a chance of working.  The below at least walks through
951 ;;; the slots predictably, but maybe it would be good to compute some
952 ;;; kind of optimal slot layout by looking at locations of slots in
953 ;;; superclasses?
954 (defmethod compute-slots ((class std-class))
955   ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
956   ;; for each different slot name we find in our superclasses. Each
957   ;; call receives the class and a list of the dslotds with that name.
958   ;; The list is in most-specific-first order.
959   (let ((name-dslotds-alist ()))
960     (dolist (c (reverse (class-precedence-list class)))
961       (dolist (slot (class-direct-slots c))
962         (let* ((name (slot-definition-name slot))
963                (entry (assq name name-dslotds-alist)))
964           (if entry
965               (push slot (cdr entry))
966               (push (list name slot) name-dslotds-alist)))))
967     (mapcar (lambda (direct)
968               (compute-effective-slot-definition class
969                                                  (car direct)
970                                                  (cdr direct)))
971             (nreverse name-dslotds-alist))))
972
973 (defmethod compute-slots ((class standard-class))
974   (call-next-method))
975
976 (defmethod compute-slots :around ((class standard-class))
977   (let ((eslotds (call-next-method))
978         (location -1))
979     (dolist (eslotd eslotds eslotds)
980       (setf (slot-definition-location eslotd)
981             (case (slot-definition-allocation eslotd)
982               (:instance
983                (incf location))
984               (:class
985                (let* ((name (slot-definition-name eslotd))
986                       (from-class 
987                        (or 
988                         (slot-definition-allocation-class eslotd)
989                         ;; we get here if the user adds an extra slot
990                         ;; himself...
991                         (setf (slot-definition-allocation-class eslotd) 
992                               class)))
993                       ;; which raises the question of what we should
994                       ;; do if we find that said user has added a slot
995                       ;; with the same name as another slot...
996                       (cell (or (assq name (class-slot-cells from-class))
997                                 (setf (class-slot-cells from-class)
998                                       (cons (cons name +slot-unbound+)
999                                             (class-slot-cells from-class))))))
1000                  (aver (consp cell))
1001                  (if (eq +slot-unbound+ (cdr cell))
1002                      ;; We may have inherited an initfunction
1003                      (let ((initfun (slot-definition-initfunction eslotd)))
1004                        (if initfun
1005                            (rplacd cell (funcall initfun))
1006                            cell))
1007                      cell)))))
1008       (unless (slot-definition-class eslotd)
1009         (setf (slot-definition-class eslotd) class))
1010       (initialize-internal-slot-functions eslotd))))
1011
1012 (defmethod compute-slots ((class funcallable-standard-class))
1013   (call-next-method))
1014
1015 (defmethod compute-slots :around ((class funcallable-standard-class))
1016   (labels ((instance-slot-names (slotds)
1017              (let (collect)
1018                (dolist (slotd slotds (nreverse collect))
1019                  (when (eq (slot-definition-allocation slotd) :instance)
1020                    (push (slot-definition-name slotd) collect)))))
1021            ;; This sorts slots so that slots of classes later in the CPL
1022            ;; come before slots of other classes.  This is crucial for
1023            ;; funcallable instances because it ensures that the slots of
1024            ;; FUNCALLABLE-STANDARD-OBJECT, which includes the slots of
1025            ;; KERNEL:FUNCALLABLE-INSTANCE, come first, which in turn
1026            ;; makes it possible to treat FUNCALLABLE-STANDARD-OBJECT as
1027            ;; a funcallable instance.
1028            (compute-layout (eslotds)
1029              (let ((first ())
1030                    (names (instance-slot-names eslotds)))
1031                (dolist (class
1032                         (reverse (class-precedence-list class))
1033                         (nreverse (nconc names first)))
1034                  (dolist (ss (class-slots class))
1035                    (let ((name (slot-definition-name ss)))
1036                      (when (member name names)
1037                        (push name first)
1038                        (setq names (delete name names)))))))))
1039     (let ((all-slotds (call-next-method))
1040           (instance-slots ())
1041           (class-slots ()))
1042       (dolist (slotd all-slotds)
1043         (case (slot-definition-allocation slotd)
1044           (:instance (push slotd instance-slots))
1045           (:class (push slotd class-slots))))
1046       (let ((layout (compute-layout instance-slots)))
1047         (dolist (slotd instance-slots)
1048           (setf (slot-definition-location slotd)
1049                 (position (slot-definition-name slotd) layout))
1050           (initialize-internal-slot-functions slotd)))
1051       (dolist (slotd class-slots)
1052         (let ((name (slot-definition-name slotd))
1053               (from-class (slot-definition-allocation-class slotd)))
1054           (setf (slot-definition-location slotd)
1055                 (assoc name (class-slot-cells from-class)))
1056           (aver (consp (slot-definition-location slotd)))
1057           (initialize-internal-slot-functions slotd)))
1058       all-slotds)))
1059
1060 (defmethod compute-slots ((class structure-class))
1061   (mapcan (lambda (superclass)
1062             (mapcar (lambda (dslotd)
1063                       (compute-effective-slot-definition
1064                        class
1065                        (slot-definition-name dslotd)
1066                        (list dslotd)))
1067                     (class-direct-slots superclass)))
1068           (reverse (slot-value class 'class-precedence-list))))
1069
1070 (defmethod compute-slots :around ((class structure-class))
1071   (let ((eslotds (call-next-method)))
1072     (mapc #'initialize-internal-slot-functions eslotds)
1073     eslotds))
1074
1075 (defmethod compute-effective-slot-definition ((class slot-class) name dslotds)
1076   (declare (ignore name))
1077   (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
1078          (class (apply #'effective-slot-definition-class class initargs)))
1079     (apply #'make-instance class initargs)))
1080
1081 (defmethod effective-slot-definition-class ((class std-class) &rest initargs)
1082   (declare (ignore initargs))
1083   (find-class 'standard-effective-slot-definition))
1084
1085 (defmethod effective-slot-definition-class ((class structure-class) &rest initargs)
1086   (declare (ignore initargs))
1087   (find-class 'structure-effective-slot-definition))
1088
1089 (defmethod compute-effective-slot-definition-initargs
1090     ((class slot-class) direct-slotds)
1091   (let* ((name nil)
1092          (initfunction nil)
1093          (initform nil)
1094          (initargs nil)
1095          (allocation nil)
1096          (allocation-class nil)
1097          (type t)
1098          (namep  nil)
1099          (initp  nil)
1100          (allocp nil))
1101
1102     (dolist (slotd direct-slotds)
1103       (when slotd
1104         (unless namep
1105           (setq name (slot-definition-name slotd)
1106                 namep t))
1107         (unless initp
1108           (when (slot-definition-initfunction slotd)
1109             (setq initform (slot-definition-initform slotd)
1110                   initfunction (slot-definition-initfunction slotd)
1111                   initp t)))
1112         (unless allocp
1113           (setq allocation (slot-definition-allocation slotd)
1114                 allocation-class (slot-definition-class slotd)
1115                 allocp t))
1116         (setq initargs (append (slot-definition-initargs slotd) initargs))
1117         (let ((slotd-type (slot-definition-type slotd)))
1118           (setq type (cond ((eq type t) slotd-type)
1119                            ((*subtypep type slotd-type) type)
1120                            (t `(and ,type ,slotd-type)))))))
1121     (list :name name
1122           :initform initform
1123           :initfunction initfunction
1124           :initargs initargs
1125           :allocation allocation
1126           :allocation-class allocation-class
1127           :type type
1128           :class class)))
1129
1130 (defmethod compute-effective-slot-definition-initargs :around
1131     ((class structure-class) direct-slotds)
1132   (let ((slotd (car direct-slotds)))
1133     (list* :defstruct-accessor-symbol
1134            (slot-definition-defstruct-accessor-symbol slotd)
1135            :internal-reader-function
1136            (slot-definition-internal-reader-function slotd)
1137            :internal-writer-function
1138            (slot-definition-internal-writer-function slotd)
1139            (call-next-method))))
1140 \f
1141 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
1142 ;;;       to make the method object. They have to use make-a-method which
1143 ;;;       is a specially bootstrapped mechanism for making standard methods.
1144 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
1145   (declare (ignore direct-slot initargs))
1146   (find-class 'standard-reader-method))
1147
1148 (defmethod add-reader-method ((class slot-class) generic-function slot-name)
1149   (add-method generic-function
1150               (make-a-method 'standard-reader-method
1151                              ()
1152                              (list (or (class-name class) 'object))
1153                              (list class)
1154                              (make-reader-method-function class slot-name)
1155                              "automatically generated reader method"
1156                              slot-name)))
1157
1158 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
1159   (declare (ignore direct-slot initargs))
1160   (find-class 'standard-writer-method))
1161
1162 (defmethod add-writer-method ((class slot-class) generic-function slot-name)
1163   (add-method generic-function
1164               (make-a-method 'standard-writer-method
1165                              ()
1166                              (list 'new-value (or (class-name class) 'object))
1167                              (list *the-class-t* class)
1168                              (make-writer-method-function class slot-name)
1169                              "automatically generated writer method"
1170                              slot-name)))
1171
1172 (defmethod add-boundp-method ((class slot-class) generic-function slot-name)
1173   (add-method generic-function
1174               (make-a-method 'standard-boundp-method
1175                              ()
1176                              (list (or (class-name class) 'object))
1177                              (list class)
1178                              (make-boundp-method-function class slot-name)
1179                              "automatically generated boundp method"
1180                              slot-name)))
1181
1182 (defmethod remove-reader-method ((class slot-class) generic-function)
1183   (let ((method (get-method generic-function () (list class) nil)))
1184     (when method (remove-method generic-function method))))
1185
1186 (defmethod remove-writer-method ((class slot-class) generic-function)
1187   (let ((method
1188           (get-method generic-function () (list *the-class-t* class) nil)))
1189     (when method (remove-method generic-function method))))
1190
1191 (defmethod remove-boundp-method ((class slot-class) generic-function)
1192   (let ((method (get-method generic-function () (list class) nil)))
1193     (when method (remove-method generic-function method))))
1194 \f
1195 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITE-METHOD function are NOT
1196 ;;; part of the standard protocol. They are however useful, PCL makes
1197 ;;; use of them internally and documents them for PCL users.
1198 ;;;
1199 ;;; *** This needs work to make type testing by the writer functions which
1200 ;;; *** do type testing faster. The idea would be to have one constructor
1201 ;;; *** for each possible type test.
1202 ;;;
1203 ;;; *** There is a subtle bug here which is going to have to be fixed.
1204 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1205 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1206 ;;; *** defined for this metaclass a chance to run.
1207
1208 (defmethod make-reader-method-function ((class slot-class) slot-name)
1209   (make-std-reader-method-function (class-name class) slot-name))
1210
1211 (defmethod make-writer-method-function ((class slot-class) slot-name)
1212   (make-std-writer-method-function (class-name class) slot-name))
1213
1214 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1215   (make-std-boundp-method-function (class-name class) slot-name))
1216 \f
1217 (defmethod compatible-meta-class-change-p (class proto-new-class)
1218   (eq (class-of class) (class-of proto-new-class)))
1219
1220 (defmethod validate-superclass ((class class) (new-super class))
1221   (or (eq new-super *the-class-t*)
1222       (eq (class-of class) (class-of new-super))))
1223
1224 (defmethod validate-superclass ((class standard-class) (new-super std-class))
1225   (let ((new-super-meta-class (class-of new-super)))
1226     (or (eq new-super-meta-class *the-class-std-class*)
1227         (eq (class-of class) new-super-meta-class))))
1228 \f
1229 ;;; What this does depends on which of the four possible values of
1230 ;;; LAYOUT-INVALID the PCL wrapper has; the simplest case is when it
1231 ;;; is (:FLUSH <wrapper>) or (:OBSOLETE <wrapper>), when there is
1232 ;;; nothing to do, as the new wrapper has already been created.  If
1233 ;;; LAYOUT-INVALID returns NIL, then we invalidate it (setting it to
1234 ;;; (:FLUSH <wrapper>); UPDATE-SLOTS later gets to choose whether or
1235 ;;; not to "upgrade" this to (:OBSOLETE <wrapper>).
1236 ;;;
1237 ;;; This leaves the case where LAYOUT-INVALID returns T, which happens
1238 ;;; when REGISTER-LAYOUT has invalidated a superclass of CLASS (which
1239 ;;; invalidated all the subclasses in SB-KERNEL land).  Again, here we
1240 ;;; must flush the caches and allow UPDATE-SLOTS to decide whether to
1241 ;;; obsolete the wrapper.
1242 ;;;
1243 ;;; FIXME: either here or in INVALID-WRAPPER-P looks like a good place
1244 ;;; for (AVER (NOT (EQ (LAYOUT-INVALID OWRAPPER)
1245 ;;;                    :UNINITIALIZED)))
1246 ;;;
1247 ;;; Thanks to Gerd Moellmann for the explanation.  -- CSR, 2002-10-29
1248 (defun force-cache-flushes (class)
1249   (let* ((owrapper (class-wrapper class)))
1250     ;; We only need to do something if the wrapper is still valid. If
1251     ;; the wrapper isn't valid, state will be FLUSH or OBSOLETE, and
1252     ;; both of those will already be doing what we want. In
1253     ;; particular, we must be sure we never change an OBSOLETE into a
1254     ;; FLUSH since OBSOLETE means do what FLUSH does and then some.
1255     (when (or (not (invalid-wrapper-p owrapper))
1256               ;; KLUDGE: despite the observations above, this remains
1257               ;; a violation of locality or what might be considered
1258               ;; good style.  There has to be a better way!  -- CSR,
1259               ;; 2002-10-29
1260               (eq (layout-invalid owrapper) t))
1261       (let ((nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1262                                     class)))
1263         (setf (wrapper-instance-slots-layout nwrapper)
1264               (wrapper-instance-slots-layout owrapper))
1265         (setf (wrapper-class-slots nwrapper)
1266               (wrapper-class-slots owrapper))
1267         (with-pcl-lock
1268           (update-lisp-class-layout class nwrapper)
1269           (setf (slot-value class 'wrapper) nwrapper)
1270           ;; Use :OBSOLETE instead of :FLUSH if any superclass has
1271           ;; been obsoleted.
1272           (if (find-if (lambda (x) 
1273                          (and (consp x) (eq :obsolete (car x))))
1274                        (layout-inherits owrapper) 
1275                        :key #'layout-invalid)
1276               (invalidate-wrapper owrapper :obsolete nwrapper)
1277               (invalidate-wrapper owrapper :flush nwrapper)))))))
1278
1279 (defun flush-cache-trap (owrapper nwrapper instance)
1280   (declare (ignore owrapper))
1281   (set-wrapper instance nwrapper))
1282 \f
1283 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1284 ;;; the next access to the instance (as defined in 88-002R) to trap
1285 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1286 (defmethod make-instances-obsolete ((class std-class))
1287   (let* ((owrapper (class-wrapper class))
1288          (nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1289                                  class)))
1290       (setf (wrapper-instance-slots-layout nwrapper)
1291             (wrapper-instance-slots-layout owrapper))
1292       (setf (wrapper-class-slots nwrapper)
1293             (wrapper-class-slots owrapper))
1294       (with-pcl-lock
1295         (update-lisp-class-layout class nwrapper)
1296         (setf (slot-value class 'wrapper) nwrapper)
1297         (invalidate-wrapper owrapper :obsolete nwrapper)
1298         class)))
1299
1300 (defmethod make-instances-obsolete ((class symbol))
1301   (make-instances-obsolete (find-class class))
1302   ;; ANSI wants the class name when called with a symbol.
1303   class)
1304
1305 ;;; OBSOLETE-INSTANCE-TRAP is the internal trap that is called when we
1306 ;;; see an obsolete instance. The times when it is called are:
1307 ;;;   - when the instance is involved in method lookup
1308 ;;;   - when attempting to access a slot of an instance
1309 ;;;
1310 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1311 ;;; instance access macros.
1312 ;;;
1313 ;;; Of course these times when it is called are an internal
1314 ;;; implementation detail of PCL and are not part of the documented
1315 ;;; description of when the obsolete instance update happens. The
1316 ;;; documented description is as it appears in 88-002R.
1317 ;;;
1318 ;;; This has to return the new wrapper, so it counts on all the
1319 ;;; methods on obsolete-instance-trap-internal to return the new
1320 ;;; wrapper. It also does a little internal error checking to make
1321 ;;; sure that the traps are only happening when they should, and that
1322 ;;; the trap methods are computing appropriate new wrappers.
1323
1324 ;;; OBSOLETE-INSTANCE-TRAP might be called on structure instances
1325 ;;; after a structure is redefined. In most cases,
1326 ;;; OBSOLETE-INSTANCE-TRAP will not be able to fix the old instance,
1327 ;;; so it must signal an error. The hard part of this is that the
1328 ;;; error system and debugger might cause OBSOLETE-INSTANCE-TRAP to be
1329 ;;; called again, so in that case, we have to return some reasonable
1330 ;;; wrapper, instead.
1331
1332 (defvar *in-obsolete-instance-trap* nil)
1333 (defvar *the-wrapper-of-structure-object*
1334   (class-wrapper (find-class 'structure-object)))
1335
1336 (define-condition obsolete-structure (error)
1337   ((datum :reader obsolete-structure-datum :initarg :datum))
1338   (:report
1339    (lambda (condition stream)
1340      ;; Don't try to print the structure, since it probably won't work.
1341      (format stream
1342              "~@<obsolete structure error for a structure of type ~2I~_~S~:>"
1343              (type-of (obsolete-structure-datum condition))))))
1344
1345 (defun obsolete-instance-trap (owrapper nwrapper instance)
1346   (if (not (pcl-instance-p instance))
1347       (if *in-obsolete-instance-trap*
1348           *the-wrapper-of-structure-object*
1349            (let ((*in-obsolete-instance-trap* t))
1350              (error 'obsolete-structure :datum instance)))
1351       (let* ((class (wrapper-class* nwrapper))
1352              (copy (allocate-instance class)) ;??? allocate-instance ???
1353              (olayout (wrapper-instance-slots-layout owrapper))
1354              (nlayout (wrapper-instance-slots-layout nwrapper))
1355              (oslots (get-slots instance))
1356              (nslots (get-slots copy))
1357              (oclass-slots (wrapper-class-slots owrapper))
1358              (added ())
1359              (discarded ())
1360              (plist ()))
1361
1362         ;; local  --> local     transfer value
1363         ;; local  --> shared    discard value, discard slot
1364         ;; local  -->  --       discard slot
1365         ;; shared --> local     transfer value
1366         ;; shared --> shared    -- (cf SHARED-INITIALIZE :AFTER STD-CLASS)
1367         ;; shared -->  --       discard value
1368         ;;  --    --> local     add slot
1369         ;;  --    --> shared    --
1370
1371         ;; Collect class slots from inherited wrappers. Needed for
1372         ;; shared -> local transfers of inherited slots.
1373         (let ((inherited (layout-inherits owrapper)))
1374           (loop for i from (1- (length inherited)) downto 0
1375                 for layout = (aref inherited i)
1376                 when (typep layout 'wrapper)
1377                 do (dolist (slot (wrapper-class-slots layout))
1378                      (pushnew slot oclass-slots :key #'car))))
1379
1380         ;; Go through all the old local slots.
1381         (let ((opos 0))
1382           (dolist (name olayout)
1383             (let ((npos (posq name nlayout)))
1384               (if npos
1385                   (setf (clos-slots-ref nslots npos)
1386                         (clos-slots-ref oslots opos))
1387                   (progn
1388                     (push name discarded)
1389                     (unless (eq (clos-slots-ref oslots opos) +slot-unbound+)
1390                       (setf (getf plist name) (clos-slots-ref oslots opos))))))
1391             (incf opos)))
1392
1393         ;; Go through all the old shared slots.
1394         (dolist (oclass-slot-and-val oclass-slots)
1395           (let ((name (car oclass-slot-and-val))
1396                 (val (cdr oclass-slot-and-val)))
1397             (let ((npos (posq name nlayout)))
1398               (when npos
1399                 (setf (clos-slots-ref nslots npos) val)))))
1400
1401         ;; Go through all the new local slots to compute the added slots.
1402         (dolist (nlocal nlayout)
1403           (unless (or (memq nlocal olayout)
1404                       (assq nlocal oclass-slots))
1405             (push nlocal added)))
1406
1407         (swap-wrappers-and-slots instance copy)
1408
1409         (update-instance-for-redefined-class instance
1410                                              added
1411                                              discarded
1412                                              plist)
1413         nwrapper)))
1414 \f
1415 (defun change-class-internal (instance new-class initargs)
1416   (let* ((old-class (class-of instance))
1417          (copy (allocate-instance new-class))
1418          (new-wrapper (get-wrapper copy))
1419          (old-wrapper (class-wrapper old-class))
1420          (old-layout (wrapper-instance-slots-layout old-wrapper))
1421          (new-layout (wrapper-instance-slots-layout new-wrapper))
1422          (old-slots (get-slots instance))
1423          (new-slots (get-slots copy))
1424          (old-class-slots (wrapper-class-slots old-wrapper)))
1425
1426     ;; "The values of local slots specified by both the class CTO and
1427     ;; CFROM are retained. If such a local slot was unbound, it
1428     ;; remains unbound."
1429     (let ((new-position 0))
1430       (dolist (new-slot new-layout)
1431         (let ((old-position (posq new-slot old-layout)))
1432           (when old-position
1433             (setf (clos-slots-ref new-slots new-position)
1434                   (clos-slots-ref old-slots old-position))))
1435         (incf new-position)))
1436
1437     ;; "The values of slots specified as shared in the class CFROM and
1438     ;; as local in the class CTO are retained."
1439     (dolist (slot-and-val old-class-slots)
1440       (let ((position (posq (car slot-and-val) new-layout)))
1441         (when position
1442           (setf (clos-slots-ref new-slots position) (cdr slot-and-val)))))
1443
1444     ;; Make the copy point to the old instance's storage, and make the
1445     ;; old instance point to the new storage.
1446     (swap-wrappers-and-slots instance copy)
1447
1448     (apply #'update-instance-for-different-class copy instance initargs)
1449     instance))
1450
1451 (defmethod change-class ((instance standard-object)
1452                          (new-class standard-class)
1453                          &rest initargs)
1454   (change-class-internal instance new-class initargs))
1455
1456 (defmethod change-class ((instance funcallable-standard-object)
1457                          (new-class funcallable-standard-class)
1458                          &rest initargs)
1459   (change-class-internal instance new-class initargs))
1460
1461 (defmethod change-class ((instance standard-object)
1462                          (new-class funcallable-standard-class)
1463                          &rest initargs)
1464   (declare (ignore initargs))
1465   (error "You can't change the class of ~S to ~S~@
1466           because it isn't already an instance with metaclass ~S."
1467          instance new-class 'standard-class))
1468
1469 (defmethod change-class ((instance funcallable-standard-object)
1470                          (new-class standard-class)
1471                          &rest initargs)
1472   (declare (ignore initargs))
1473   (error "You can't change the class of ~S to ~S~@
1474           because it isn't already an instance with metaclass ~S."
1475          instance new-class 'funcallable-standard-class))
1476
1477 (defmethod change-class ((instance t) (new-class-name symbol) &rest initargs)
1478   (apply #'change-class instance (find-class new-class-name) initargs))
1479 \f
1480 ;;;; The metaclass BUILT-IN-CLASS
1481 ;;;;
1482 ;;;; This metaclass is something of a weird creature. By this point, all
1483 ;;;; instances of it which will exist have been created, and no instance
1484 ;;;; is ever created by calling MAKE-INSTANCE.
1485 ;;;;
1486 ;;;; But, there are other parts of the protocol we must follow and those
1487 ;;;; definitions appear here.
1488
1489 (defmethod shared-initialize :before
1490            ((class built-in-class) slot-names &rest initargs)
1491   (declare (ignore slot-names initargs))
1492   (error "attempt to initialize or reinitialize a built in class"))
1493
1494 (defmethod class-direct-slots       ((class built-in-class)) ())
1495 (defmethod class-slots             ((class built-in-class)) ())
1496 (defmethod class-direct-default-initargs ((class built-in-class)) ())
1497 (defmethod class-default-initargs       ((class built-in-class)) ())
1498
1499 (defmethod validate-superclass ((c class) (s built-in-class))
1500   (or (eq s *the-class-t*)
1501       (eq s *the-class-stream*)))
1502 \f
1503 ;;; Some necessary methods for FORWARD-REFERENCED-CLASS
1504 (defmethod class-direct-slots ((class forward-referenced-class)) ())
1505 (defmethod class-direct-default-initargs ((class forward-referenced-class)) ())
1506 (macrolet ((def (method)
1507              `(defmethod ,method ((class forward-referenced-class))
1508                 (error "~@<~I~S was called on a forward referenced class:~2I~_~S~:>"
1509                        ',method class))))
1510   (def class-default-initargs)
1511   (def class-precedence-list)
1512   (def class-slots))
1513
1514 (defmethod validate-superclass ((c slot-class)
1515                                 (f forward-referenced-class))
1516   t)
1517 \f
1518 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1519   (pushnew dependent (plist-value metaobject 'dependents)))
1520
1521 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1522   (setf (plist-value metaobject 'dependents)
1523         (delete dependent (plist-value metaobject 'dependents))))
1524
1525 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1526   (dolist (dependent (plist-value metaobject 'dependents))
1527     (funcall function dependent)))
1528