Kill *PCL-CLASS-BOOT* hack.
[sbcl.git] / src / pcl / wrapper.lisp
blob1e083d93ed9ef6df3a40f5d771ceb350cbe2193f
1 ;;;; Bits and pieces of the wrapper machninery. This used to live in cache.lisp,
2 ;;;; but doesn't really logically belong there.
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
7 ;;;; This software is derived from software originally released by Xerox
8 ;;;; Corporation. Copyright and release statements follow. Later modifications
9 ;;;; to the software are in the public domain and are provided with
10 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
11 ;;;; information.
13 ;;;; copyright information from original PCL sources:
14 ;;;;
15 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
16 ;;;; All rights reserved.
17 ;;;;
18 ;;;; Use and copying of this software and preparation of derivative works based
19 ;;;; upon this software are permitted. Any distribution of this software or
20 ;;;; derivative works must comply with all applicable United States export
21 ;;;; control laws.
22 ;;;;
23 ;;;; This software is made available AS IS, and Xerox Corporation makes no
24 ;;;; warranty about the software, its performance or its conformity to any
25 ;;;; specification.
27 (in-package "SB-PCL")
29 (defmacro wrapper-class (wrapper)
30 `(classoid-pcl-class (layout-classoid ,wrapper)))
31 (defmacro wrapper-no-of-instance-slots (wrapper)
32 `(layout-length ,wrapper))
34 (declaim (inline make-wrapper-internal))
35 (defun make-wrapper-internal (&key length classoid)
36 (make-layout :length length :classoid classoid :invalid nil
37 :%for-std-class-b 1))
39 ;;; This is called in BRAID when we are making wrappers for classes
40 ;;; whose slots are not initialized yet, and which may be built-in
41 ;;; classes. We pass in the class name in addition to the class.
42 (defun !boot-make-wrapper (length name &optional class)
43 (let ((found (find-classoid name nil)))
44 (cond
45 (found
46 (unless (classoid-pcl-class found)
47 (setf (classoid-pcl-class found) class))
48 (aver (eq (classoid-pcl-class found) class))
49 (let ((layout (classoid-layout found)))
50 (aver layout)
51 layout))
53 (make-wrapper-internal
54 :length length
55 :classoid (make-standard-classoid
56 :name name :pcl-class class))))))
58 ;;; In SBCL, as in CMU CL, the layouts (a.k.a wrappers) for built-in
59 ;;; and structure classes already exist when PCL is initialized, so we
60 ;;; don't necessarily always make a wrapper. Also, we help maintain
61 ;;; the mapping between CL:CLASS and SB-KERNEL:CLASSOID objects.
62 (defun make-wrapper (length class)
63 (declare (notinline slot-value))
64 (cond
65 ((or (typep class 'std-class)
66 (typep class 'forward-referenced-class))
67 (make-wrapper-internal
68 :length length
69 :classoid
70 (let ((owrap (class-wrapper class)))
71 (cond (owrap
72 (layout-classoid owrap))
73 ((or (*subtypep (class-of class) *the-class-standard-class*)
74 (*subtypep (class-of class) *the-class-funcallable-standard-class*)
75 (typep class 'forward-referenced-class))
76 (let ((name (slot-value class 'name)))
77 (make-standard-classoid :pcl-class class
78 :name (and (symbolp name) name))))
80 (bug "Got to T branch in ~S" 'make-wrapper))))))
82 (let* ((found (find-classoid (slot-value class 'name)))
83 (layout (classoid-layout found)))
84 (unless (classoid-pcl-class found)
85 (setf (classoid-pcl-class found) class))
86 (aver (eq (classoid-pcl-class found) class))
87 (aver layout)
88 layout))))
90 (declaim (inline wrapper-class*))
91 (defun wrapper-class* (wrapper)
92 (or (wrapper-class wrapper)
93 ;; FIXME: this branch seems unreachable.
94 ;; It would be nice to eliminate WRAPPER-CLASS* if we can show that it
95 ;; is only a holdover from an earlier way of bootstrapping that resulted
96 ;; in the temporary absence of a PCL-CLASS for some non-standard-class.
97 ;; Certainly no test gets here [changing it to (BUG "got here") worked].
98 ;; Note however that
99 ;; (CLASSOID-PCL-CLASS (FIND-CLASSOID 'STANDARD-INSTANCE)) => NIL
100 ;; which can be resolved by just ensuring one time that it has a CLASS.
101 ;; And nothing else seems to be problematic.
102 (let ((classoid (layout-classoid wrapper)))
103 (ensure-non-standard-class
104 (classoid-name classoid)
105 classoid))))
107 ;;; The wrapper cache machinery provides general mechanism for
108 ;;; trapping on the next access to any instance of a given class. This
109 ;;; mechanism is used to implement the updating of instances when the
110 ;;; class is redefined (MAKE-INSTANCES-OBSOLETE). The same mechanism
111 ;;; is also used to update generic function caches when there is a
112 ;;; change to the superclasses of a class.
114 ;;; Basically, a given wrapper can be valid or invalid. If it is
115 ;;; invalid, it means that any attempt to do a wrapper cache lookup
116 ;;; using the wrapper should trap. Also, methods on
117 ;;; SLOT-VALUE-USING-CLASS check the wrapper validity as well. This is
118 ;;; done by calling CHECK-WRAPPER-VALIDITY.
120 (declaim (inline invalid-wrapper-p))
121 (defun invalid-wrapper-p (wrapper)
122 (not (null (layout-invalid wrapper))))
124 ;;; We only use this inside INVALIDATE-WRAPPER.
125 (defvar *previous-nwrappers* (make-hash-table))
127 (defun %invalidate-wrapper (owrapper state nwrapper)
128 (aver (member state '(:flush :obsolete) :test #'eq))
129 (let ((new-previous ()))
130 ;; First off, a previous call to INVALIDATE-WRAPPER may have
131 ;; recorded OWRAPPER as an NWRAPPER to update to. Since OWRAPPER
132 ;; is about to be invalid, it no longer makes sense to update to
133 ;; it.
135 ;; We go back and change the previously invalidated wrappers so
136 ;; that they will now update directly to NWRAPPER. This
137 ;; corresponds to a kind of transitivity of wrapper updates.
138 (dolist (previous (gethash owrapper *previous-nwrappers*))
139 (when (eq state :obsolete)
140 (setf (car previous) :obsolete))
141 (setf (cadr previous) nwrapper)
142 (push previous new-previous))
144 ;; FIXME: We are here inside PCL lock, but might someone be
145 ;; accessing the wrapper at the same time from outside the lock?
146 (setf (layout-clos-hash owrapper) 0)
148 ;; FIXME: We could save a whopping cons by using (STATE . WRAPPER)
149 ;; instead
150 (push (setf (layout-invalid owrapper) (list state nwrapper))
151 new-previous)
153 (remhash owrapper *previous-nwrappers*)
154 (setf (gethash nwrapper *previous-nwrappers*) new-previous)))
156 ;;; FIXME: This is not a good name: part of the contract here is that
157 ;;; we return the valid wrapper, which is not obvious from the name
158 ;;; (or the names of our callees.)
159 (defun check-wrapper-validity (instance)
160 (with-world-lock ()
161 (let* ((owrapper (layout-of instance))
162 (state (layout-invalid owrapper)))
163 (aver (not (eq state :uninitialized)))
164 (cond ((not state)
165 owrapper)
166 ((not (layout-for-std-class-p owrapper))
167 ;; Obsolete structure trap.
168 (%obsolete-instance-trap owrapper nil instance))
169 ((eq t state)
170 ;; FIXME: I can't help thinking that, while this does cure
171 ;; the symptoms observed from some class redefinitions,
172 ;; this isn't the place to be doing this flushing.
173 ;; Nevertheless... -- CSR, 2003-05-31
175 ;; CMUCL comment:
176 ;; We assume in this case, that the :INVALID is from a
177 ;; previous call to REGISTER-LAYOUT for a superclass of
178 ;; INSTANCE's class. See also the comment above
179 ;; FORCE-CACHE-FLUSHES. Paul Dietz has test cases for this.
180 (let ((class (wrapper-class* owrapper)))
181 (%force-cache-flushes class)
182 ;; KLUDGE: avoid an infinite recursion, it's still better to
183 ;; bail out with an error for server softwares. see FIXME above.
184 ;; details: http://thread.gmane.org/gmane.lisp.steel-bank.devel/10175
186 ;; Error message here is trying to figure out a bit more about the
187 ;; situation, since we don't have anything approaching a test-case
188 ;; for the bug.
189 (let ((new-state (layout-invalid (layout-of instance))))
190 (when (eq new-state t)
191 (cerror "Nevermind and recurse." 'bug
192 :format-control "~@<~4IProblem forcing cache flushes. Please report ~
193 to sbcl-devel.~
194 ~% Owrapper: ~S~
195 ~% Wrapper-of: ~S~
196 ~% Class-wrapper: ~S~%~:@>"
197 :format-arguments (mapcar (lambda (x)
198 (cons x (layout-invalid x)))
199 (list owrapper
200 (layout-of instance)
201 (class-wrapper class)))))))
202 (check-wrapper-validity instance))
203 ((consp state)
204 (ecase (car state)
205 (:flush
206 (let ((new (cadr state)))
207 (cond ((std-instance-p instance)
208 (setf (std-instance-wrapper instance) new))
209 ((fsc-instance-p instance)
210 (setf (fsc-instance-wrapper instance) new))
212 (bug "unrecognized instance type")))))
213 (:obsolete
214 (%obsolete-instance-trap owrapper (cadr state) instance))))))))
216 (declaim (inline check-obsolete-instance))
217 (defun check-obsolete-instance (instance)
218 (when (invalid-wrapper-p (layout-of instance))
219 (check-wrapper-validity instance)))
221 (defun valid-wrapper-of (instance)
222 (let ((wrapper (layout-of instance)))
223 (if (invalid-wrapper-p wrapper)
224 (check-wrapper-validity instance)
225 wrapper)))
227 ;;; NIL: means nothing so far, no actual arg info has NILs in the
228 ;;; metatype.
230 ;;; CLASS: seen all sorts of metaclasses (specifically, more than one
231 ;;; of the next 5 values) or else have seen something which doesn't
232 ;;; fall into a single category (SLOT-INSTANCE, FORWARD). Also used
233 ;;; when seen a non-standard specializer.
235 ;;; T: means everything so far is the class T.
237 ;;; The above three are the really important ones, as they affect how
238 ;;; discriminating functions are computed. There are some other
239 ;;; possible metatypes:
241 ;;; * STANDARD-INSTANCE: seen only standard classes
242 ;;; * BUILT-IN-INSTANCE: seen only built in classes
243 ;;; * STRUCTURE-INSTANCE: seen only structure classes
244 ;;; * CONDITION-INSTANCE: seen only condition classes
246 ;;; but these are largely unexploited as of 2007-05-10. The
247 ;;; distinction between STANDARD-INSTANCE and the others is used in
248 ;;; emitting wrapper/slot-getting code in accessor discriminating
249 ;;; functions (see EMIT-FETCH-WRAPPER and EMIT-READER/WRITER); it is
250 ;;; possible that there was an intention to use these metatypes to
251 ;;; specialize cache implementation or discrimination nets, but this
252 ;;; has not occurred as yet.
253 (defun raise-metatype (metatype new-specializer)
254 (let ((slot *the-class-slot-class*)
255 (standard *the-class-standard-class*)
256 (fsc *the-class-funcallable-standard-class*)
257 (condition *the-class-condition-class*)
258 (structure *the-class-structure-class*)
259 (system *the-class-system-class*)
260 (frc *the-class-forward-referenced-class*))
261 (flet ((specializer->metatype (x)
262 (let* ((specializer-class (if (eq **boot-state** 'complete)
263 (specializer-class-or-nil x)
265 (meta-specializer (class-of specializer-class)))
266 (cond
267 ((eq x *the-class-t*) t)
268 ((not specializer-class) 'non-standard)
269 ((*subtypep meta-specializer standard) 'standard-instance)
270 ((*subtypep meta-specializer fsc) 'standard-instance)
271 ((*subtypep meta-specializer condition) 'condition-instance)
272 ((*subtypep meta-specializer structure) 'structure-instance)
273 ((*subtypep meta-specializer system) 'system-instance)
274 ((*subtypep meta-specializer slot) 'slot-instance)
275 ((*subtypep meta-specializer frc) 'forward)
276 (t (error "~@<PCL cannot handle the specializer ~S ~
277 (meta-specializer ~S).~@:>"
278 new-specializer meta-specializer))))))
279 ;; We implement the following table. The notation is
280 ;; that X and Y are distinct meta specializer names.
282 ;; NIL <anything> ===> <anything>
283 ;; X X ===> X
284 ;; X Y ===> CLASS
285 (let ((new-metatype (specializer->metatype new-specializer)))
286 (cond ((eq new-metatype 'slot-instance) 'class)
287 ((eq new-metatype 'forward) 'class)
288 ((eq new-metatype 'non-standard) 'class)
289 ((null metatype) new-metatype)
290 ((eq metatype new-metatype) new-metatype)
291 (t 'class))))))
293 (defmacro with-dfun-wrappers ((args metatypes)
294 (dfun-wrappers invalid-wrapper-p
295 &optional wrappers classes types)
296 invalid-arguments-form
297 &body body)
298 `(let* ((args-tail ,args) (,invalid-wrapper-p nil) (invalid-arguments-p nil)
299 (,dfun-wrappers nil) (dfun-wrappers-tail nil)
300 ,@(when wrappers
301 `((wrappers-rev nil) (types-rev nil) (classes-rev nil))))
302 (dolist (mt ,metatypes)
303 (unless args-tail
304 (setq invalid-arguments-p t)
305 (return nil))
306 (let* ((arg (pop args-tail))
307 (wrapper nil)
308 ,@(when wrappers
309 `((class *the-class-t*)
310 (type t))))
311 (unless (eq mt t)
312 (setq wrapper (layout-of arg))
313 (when (invalid-wrapper-p wrapper)
314 (setq ,invalid-wrapper-p t)
315 (setq wrapper (check-wrapper-validity arg)))
316 (cond ((null ,dfun-wrappers)
317 (setq ,dfun-wrappers wrapper))
318 ((not (consp ,dfun-wrappers))
319 (setq dfun-wrappers-tail (list wrapper))
320 (setq ,dfun-wrappers (cons ,dfun-wrappers dfun-wrappers-tail)))
322 (let ((new-dfun-wrappers-tail (list wrapper)))
323 (setf (cdr dfun-wrappers-tail) new-dfun-wrappers-tail)
324 (setf dfun-wrappers-tail new-dfun-wrappers-tail))))
325 ,@(when wrappers
326 `((setq class (wrapper-class* wrapper))
327 (setq type `(class-eq ,class)))))
328 ,@(when wrappers
329 `((push wrapper wrappers-rev)
330 (push class classes-rev)
331 (push type types-rev)))))
332 (if invalid-arguments-p
333 ,invalid-arguments-form
334 (let* (,@(when wrappers
335 `((,wrappers (nreverse wrappers-rev))
336 (,classes (nreverse classes-rev))
337 (,types (mapcar (lambda (class)
338 `(class-eq ,class))
339 ,classes)))))
340 ,@body))))