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