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