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