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