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