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