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