Declare COERCE and two helpers as EXPLICIT-CHECK.
[sbcl.git] / src / pcl / slots.lisp
blob12d1ab0bf10d7bea673d72c259f8a51c4ebcd3fa
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
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.
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.
24 (in-package "SB-PCL")
26 ;;;; ANSI CL condition for unbound slots
28 (define-condition unbound-slot (cell-error)
29 ((instance :reader unbound-slot-instance :initarg :instance))
30 (:report (lambda (condition stream)
31 (handler-case
32 (format stream "~@<The slot ~/sb-ext:print-symbol-with-prefix/ ~
33 is unbound in the object ~A.~@:>"
34 (cell-error-name condition)
35 (unbound-slot-instance condition))
36 (serious-condition ()
37 ;; In case of an error try again avoiding custom PRINT-OBJECT's.
38 (format stream "~&Error during printing.~%~@<The slot ~
39 ~/sb-ext:print-symbol-with-prefix/ ~
40 is unbound in an instance of ~
41 ~/sb-ext:print-symbol-with-prefix/.~@:>"
42 (cell-error-name condition)
43 (type-of (unbound-slot-instance condition))))))))
45 (defmethod wrapper-fetcher ((class standard-class))
46 'std-instance-wrapper)
48 (defmethod slots-fetcher ((class standard-class))
49 'std-instance-slots)
51 (defmethod raw-instance-allocator ((class standard-class))
52 'allocate-standard-instance)
54 ;;; These three functions work on std-instances and fsc-instances. These are
55 ;;; instances for which it is possible to change the wrapper and the slots.
56 ;;;
57 ;;; For these kinds of instances, most specified methods from the instance
58 ;;; structure protocol are promoted to the implementation-specific class
59 ;;; std-class. Many of these methods call these four functions.
61 (defun %swap-wrappers-and-slots (i1 i2)
62 (cond ((std-instance-p i1)
63 (let ((w1 (std-instance-wrapper i1))
64 (s1 (std-instance-slots i1)))
65 (setf (std-instance-wrapper i1) (std-instance-wrapper i2))
66 (setf (std-instance-slots i1) (std-instance-slots i2))
67 (setf (std-instance-wrapper i2) w1)
68 (setf (std-instance-slots i2) s1)))
69 ((fsc-instance-p i1)
70 (let ((w1 (fsc-instance-wrapper i1))
71 (s1 (fsc-instance-slots i1)))
72 (setf (fsc-instance-wrapper i1) (fsc-instance-wrapper i2))
73 (setf (fsc-instance-slots i1) (fsc-instance-slots i2))
74 (setf (fsc-instance-wrapper i2) w1)
75 (setf (fsc-instance-slots i2) s1)))
77 (error "unrecognized instance type"))))
79 ;;;; STANDARD-INSTANCE-ACCESS
81 (declaim (inline standard-instance-access
82 (setf standard-instance-access)
83 (cas stadard-instance-access)
84 funcallable-standard-instance-access
85 (setf funcallable-standard-instance-access)
86 (cas funcallable-standard-instance-access)))
88 (defun standard-instance-access (instance location)
89 (clos-slots-ref (std-instance-slots instance) location))
91 (defun (setf standard-instance-access) (new-value instance location)
92 (setf (clos-slots-ref (std-instance-slots instance) location) new-value))
94 (defun (cas standard-instance-access) (old-value new-value instance location)
95 ;; FIXME: Maybe get rid of CLOS-SLOTS-REF entirely?
96 (cas (svref (std-instance-slots instance) location) old-value new-value))
98 (defun funcallable-standard-instance-access (instance location)
99 (clos-slots-ref (fsc-instance-slots instance) location))
101 (defun (setf funcallable-standard-instance-access) (new-value instance location)
102 (setf (clos-slots-ref (fsc-instance-slots instance) location) new-value))
104 (defun (cas funcallable-standard-instance-access) (old-value new-value instance location)
105 ;; FIXME: Maybe get rid of CLOS-SLOTS-REF entirely?
106 (cas (svref (fsc-instance-slots instance) location) old-value new-value))
108 ;;;; SLOT-VALUE, (SETF SLOT-VALUE), SLOT-BOUNDP, SLOT-MAKUNBOUND
110 (declaim (ftype (sfunction (t symbol) t) slot-value))
111 (defun slot-value (object slot-name)
112 (let* ((wrapper (valid-wrapper-of object))
113 (cell (find-slot-cell wrapper slot-name))
114 (location (car cell))
115 (value
116 (cond ((fixnump location)
117 (if (std-instance-p object)
118 (standard-instance-access object location)
119 (funcallable-standard-instance-access object location)))
120 ((not location)
121 (return-from slot-value
122 (if cell
123 (funcall (slot-info-reader (cdr cell)) object)
124 (values (slot-missing (wrapper-class* wrapper) object
125 slot-name 'slot-value)))))
126 ;; this next test means CONSP, but the transform that weakens
127 ;; CONSP to LISTP isn't working here for some reason.
128 ((listp location)
129 (cdr location))
131 (bug "Bogus slot cell in SLOT-VALUE: ~S" cell)))))
132 (if (eq +slot-unbound+ value)
133 (slot-unbound (wrapper-class* wrapper) object slot-name)
134 value)))
136 ;;; This is used during the PCL build, but gets replaced by a deftransform
137 ;;; in fixup.lisp.
138 (define-compiler-macro slot-value (&whole form object slot-name
139 &environment env)
140 (if (and (constantp slot-name env)
141 (interned-symbol-p (constant-form-value slot-name env)))
142 `(accessor-slot-value ,object ,slot-name)
143 form))
145 (defun set-slot-value (object slot-name new-value)
146 (let* ((wrapper (valid-wrapper-of object))
147 (cell (or (find-slot-cell wrapper slot-name)
148 (return-from set-slot-value
149 (progn (slot-missing (wrapper-class* wrapper)
150 object slot-name 'setf new-value)
151 new-value))))
152 (location (car cell))
153 (info (cdr cell))
154 (typecheck (slot-info-typecheck info)))
155 (when typecheck
156 (funcall typecheck new-value))
157 (cond ((fixnump location)
158 (if (std-instance-p object)
159 (setf (standard-instance-access object location) new-value)
160 (setf (funcallable-standard-instance-access object location)
161 new-value)))
162 ((not location)
163 (funcall (slot-info-writer info) new-value object))
164 ((listp location) ; forcibly transform CONSP to LISTP
165 (setf (cdr location) new-value))
167 (bug "Bogus slot-cell in SET-SLOT-VALUE: ~S" cell))))
168 new-value)
170 ;;; A version of SET-SLOT-VALUE for use in safe code, where we want to
171 ;;; check types when writing to slots:
172 ;;; * Doesn't have an optimizing compiler-macro
173 ;;; * Isn't special-cased in WALK-METHOD-LAMBDA
174 (defun safe-set-slot-value (object slot-name new-value)
175 (set-slot-value object slot-name new-value))
177 ;;; This is used during the PCL build, but gets replaced by a deftransform
178 ;;; in fixup.lisp.
179 (define-compiler-macro set-slot-value (&whole form object slot-name new-value
180 &environment env)
181 (if (and (constantp slot-name env)
182 (interned-symbol-p (constant-form-value slot-name env))
183 ;; We can't use the ACCESSOR-SET-SLOT-VALUE path in safe
184 ;; code, since it'll use the global automatically generated
185 ;; accessor, which won't do typechecking. (SLOT-OBJECT
186 ;; won't have been compiled with SAFETY 3, so SAFE-P will
187 ;; be NIL in MAKE-STD-WRITER-METHOD-FUNCTION).
188 (not (safe-code-p env)))
189 `(accessor-set-slot-value ,object ,slot-name ,new-value)
190 form))
192 (defun (cas slot-value) (old-value new-value object slot-name)
193 (let* ((wrapper (valid-wrapper-of object))
194 (cell (or (find-slot-cell wrapper slot-name)
195 (return-from slot-value
196 (values (slot-missing (wrapper-class* wrapper) object slot-name
197 'cas (list old-value new-value))))))
198 (location (car cell))
199 (info (cdr cell))
200 (typecheck (slot-info-typecheck info)))
201 (when typecheck
202 (funcall typecheck new-value))
203 (let ((old (cond ((fixnump location)
204 (if (std-instance-p object)
205 (cas (standard-instance-access object location) old-value new-value)
206 (cas (funcallable-standard-instance-access object location)
207 old-value new-value)))
208 ((not location)
209 ;; FIXME: (CAS SLOT-VALUE-USING-CLASS)...
210 (error "Cannot compare-and-swap slot ~S on: ~S" slot-name object))
211 ((listp location) ; forcibly transform CONSP to LISTP
212 (cas (cdr location) old-value new-value))
214 (bug "Bogus slot-cell in (CAS SLOT-VALUE): ~S" cell)))))
215 (if (and (eq +slot-unbound+ old)
216 (neq old old-value))
217 (slot-unbound (wrapper-class* wrapper) object slot-name)
218 old))))
220 (defun slot-boundp (object slot-name)
221 (let* ((wrapper (valid-wrapper-of object))
222 (cell (find-slot-cell wrapper slot-name))
223 (location (car cell))
224 (value
225 (cond ((fixnump location)
226 (if (std-instance-p object)
227 (standard-instance-access object location)
228 (funcallable-standard-instance-access object location)))
229 ((not location)
230 (return-from slot-boundp
231 (if cell
232 (funcall (slot-info-boundp (cdr cell)) object)
233 (and (slot-missing (wrapper-class* wrapper) object
234 slot-name 'slot-boundp)
235 t))))
236 ((listp location) ; forcibly transform CONSP to LISTP
237 (cdr location))
239 (bug "Bogus slot cell in SLOT-VALUE: ~S" cell)))))
240 (not (eq +slot-unbound+ value))))
242 (define-compiler-macro slot-boundp (&whole form object slot-name
243 &environment env)
244 (if (and (constantp slot-name env)
245 (interned-symbol-p (constant-form-value slot-name env)))
246 `(accessor-slot-boundp ,object ,slot-name)
247 form))
249 (defun slot-makunbound (object slot-name)
250 (let* ((wrapper (valid-wrapper-of object))
251 (cell (find-slot-cell wrapper slot-name))
252 (location (car cell)))
253 (cond ((fixnump location)
254 (if (std-instance-p object)
255 (setf (standard-instance-access object location) +slot-unbound+)
256 (setf (funcallable-standard-instance-access object location)
257 +slot-unbound+)))
258 ((not location)
259 (if cell
260 (let ((class (wrapper-class* wrapper)))
261 (slot-makunbound-using-class class object
262 (find-slot-definition class slot-name)))
263 (slot-missing (wrapper-class* wrapper) object slot-name
264 'slot-makunbound)))
265 ((listp location) ; forcibly transform CONSP to LISTP
266 (setf (cdr location) +slot-unbound+))
268 (bug "Bogus slot-cell in SLOT-MAKUNBOUND: ~S" cell))))
269 object)
271 ;; Note that CLHS "encourages" implementors to base this on
272 ;; SLOT-EXISTS-P-USING-CLASS, whereas 88-002R made no such claim,
273 ;; however Appendix D of AMOP sketches out such an implementation.
274 (defun slot-exists-p (object slot-name)
275 (not (null (find-slot-cell (valid-wrapper-of object) slot-name))))
277 (defvar *unbound-slot-value-marker* (make-unprintable-object "unbound slot"))
279 ;;; This isn't documented, but is used within PCL in a number of print
280 ;;; object methods. (See NAMED-OBJECT-PRINT-FUNCTION.)
281 (defun slot-value-or-default (object slot-name &optional
282 (default *unbound-slot-value-marker*))
283 (if (slot-boundp object slot-name)
284 (slot-value object slot-name)
285 default))
287 (defmethod slot-value-using-class ((class std-class)
288 (object standard-object)
289 (slotd standard-effective-slot-definition))
290 ;; FIXME: Do we need this? SLOT-VALUE checks for obsolete
291 ;; instances. Are users allowed to call this directly?
292 (check-obsolete-instance object)
293 (let* ((location (slot-definition-location slotd))
294 (value
295 (typecase location
296 (fixnum
297 (cond ((std-instance-p object)
298 (clos-slots-ref (std-instance-slots object)
299 location))
300 ((fsc-instance-p object)
301 (clos-slots-ref (fsc-instance-slots object)
302 location))
303 (t (bug "unrecognized instance type in ~S"
304 'slot-value-using-class))))
305 (cons
306 (cdr location))
308 (instance-structure-protocol-error slotd
309 'slot-value-using-class)))))
310 (if (eq value +slot-unbound+)
311 (values (slot-unbound class object (slot-definition-name slotd)))
312 value)))
314 (defmethod (setf slot-value-using-class)
315 (new-value (class std-class)
316 (object standard-object)
317 (slotd standard-effective-slot-definition))
318 ;; FIXME: Do we need this? SET-SLOT-VALUE checks for obsolete
319 ;; instances. Are users allowed to call this directly?
320 (check-obsolete-instance object)
321 (let* ((info (slot-definition-info slotd))
322 (location (slot-definition-location slotd))
323 (typecheck (slot-info-typecheck info))
324 (new-value (if typecheck
325 (funcall (the function typecheck) new-value)
326 new-value)))
327 (typecase location
328 (fixnum
329 (cond ((std-instance-p object)
330 (setf (clos-slots-ref (std-instance-slots object) location)
331 new-value))
332 ((fsc-instance-p object)
333 (setf (clos-slots-ref (fsc-instance-slots object) location)
334 new-value))
335 (t (bug "unrecognized instance type in ~S"
336 '(setf slot-value-using-class)))))
337 (cons
338 (setf (cdr location) new-value))
340 (instance-structure-protocol-error
341 slotd '(setf slot-value-using-class))))))
343 (defmethod slot-boundp-using-class
344 ((class std-class)
345 (object standard-object)
346 (slotd standard-effective-slot-definition))
347 ;; FIXME: Do we need this? SLOT-BOUNDP checks for obsolete
348 ;; instances. Are users allowed to call this directly?
349 (check-obsolete-instance object)
350 (let* ((location (slot-definition-location slotd))
351 (value
352 (typecase location
353 (fixnum
354 (cond ((std-instance-p object)
355 (clos-slots-ref (std-instance-slots object)
356 location))
357 ((fsc-instance-p object)
358 (clos-slots-ref (fsc-instance-slots object)
359 location))
360 (t (bug "unrecognized instance type in ~S"
361 'slot-boundp-using-class))))
362 (cons
363 (cdr location))
365 (instance-structure-protocol-error slotd
366 'slot-boundp-using-class)))))
367 (not (eq value +slot-unbound+))))
369 (defmethod slot-makunbound-using-class
370 ((class std-class)
371 (object standard-object)
372 (slotd standard-effective-slot-definition))
373 (check-obsolete-instance object)
374 (let ((location (slot-definition-location slotd)))
375 (typecase location
376 (fixnum
377 (cond ((std-instance-p object)
378 (setf (clos-slots-ref (std-instance-slots object) location)
379 +slot-unbound+))
380 ((fsc-instance-p object)
381 (setf (clos-slots-ref (fsc-instance-slots object) location)
382 +slot-unbound+))
383 (t (bug "unrecognized instance type in ~S"
384 'slot-makunbound-using-class))))
385 (cons
386 (setf (cdr location) +slot-unbound+))
388 (instance-structure-protocol-error slotd
389 'slot-makunbound-using-class))))
390 object)
392 (defmethod slot-value-using-class
393 ((class condition-class)
394 (object condition)
395 (slotd condition-effective-slot-definition))
396 (let ((fun (slot-info-reader (slot-definition-info slotd))))
397 (funcall fun object)))
399 (defmethod (setf slot-value-using-class)
400 (new-value
401 (class condition-class)
402 (object condition)
403 (slotd condition-effective-slot-definition))
404 (let ((fun (slot-info-writer (slot-definition-info slotd))))
405 (funcall fun new-value object)))
407 (defmethod slot-boundp-using-class
408 ((class condition-class)
409 (object condition)
410 (slotd condition-effective-slot-definition))
411 (let ((fun (slot-info-boundp (slot-definition-info slotd))))
412 (funcall fun object)))
414 (defmethod slot-makunbound-using-class ((class condition-class) object slot)
415 (error "attempt to unbind slot ~S in condition object ~S."
416 slot object))
418 (defmethod slot-value-using-class
419 ((class structure-class)
420 (object structure-object)
421 (slotd structure-effective-slot-definition))
422 (let* ((function (slot-definition-internal-reader-function slotd))
423 (value (funcall function object)))
424 (declare (type function function))
425 ;; FIXME: Is this really necessary? Structure slots should surely
426 ;; never be unbound!
427 (if (eq value +slot-unbound+)
428 (values (slot-unbound class object (slot-definition-name slotd)))
429 value)))
431 (defmethod (setf slot-value-using-class)
432 (new-value (class structure-class)
433 (object structure-object)
434 (slotd structure-effective-slot-definition))
435 (let ((function (slot-definition-internal-writer-function slotd)))
436 (declare (type function function))
437 (funcall function new-value object)))
439 (defmethod slot-boundp-using-class
440 ((class structure-class)
441 (object structure-object)
442 (slotd structure-effective-slot-definition))
445 (defmethod slot-makunbound-using-class
446 ((class structure-class)
447 (object structure-object)
448 (slotd structure-effective-slot-definition))
449 (error "Structure slots can't be unbound."))
451 (defmethod slot-missing
452 ((class t) instance slot-name operation &optional new-value)
453 (error "~@<When attempting to ~A, the slot ~S is missing from the ~
454 object ~S.~@:>"
455 (ecase operation
456 (slot-value "read the slot's value (slot-value)")
457 (setf (format nil
458 "set the slot's value to ~S (SETF of SLOT-VALUE)"
459 new-value))
460 (slot-boundp "test to see whether slot is bound (SLOT-BOUNDP)")
461 (slot-makunbound "make the slot unbound (SLOT-MAKUNBOUND)"))
462 slot-name
463 instance))
465 (defmethod slot-unbound ((class t) instance slot-name)
466 (restart-case
467 (error 'unbound-slot :name slot-name :instance instance)
468 (use-value (v)
469 :report "Return a value as the slot-value."
470 :interactive read-evaluated-form
472 (store-value (v)
473 :report "Store and return a value as the slot-value."
474 :interactive read-evaluated-form
475 (setf (slot-value instance slot-name) v))))
477 (defun slot-unbound-internal (instance position)
478 (values
479 (slot-unbound
480 (class-of instance)
481 instance
482 (etypecase position
483 (fixnum
484 ;; In the vast majority of cases location corresponds to the position
485 ;; in list. The only exceptions are when there are non-local slots
486 ;; before the one we want.
487 (let* ((slots (layout-slot-list (layout-of instance)))
488 (guess (nth position slots)))
489 (if (eql position (slot-definition-location guess))
490 (slot-definition-name guess)
491 (slot-definition-name
492 (car (member position (class-slots instance) :key #'slot-definition-location))))))
493 (cons
494 (car position))))))
496 ;;; FIXME: AMOP says that allocate-instance imples finalize-inheritance
497 ;;; if the class is not yet finalized, but we don't seem to be taking
498 ;;; care of this for non-standard-classes.
499 (defmethod allocate-instance ((class standard-class) &rest initargs)
500 (declare (ignore initargs))
501 (allocate-standard-instance
502 (class-wrapper (ensure-class-finalized class))))
504 (defmethod allocate-instance ((class structure-class) &rest initargs)
505 (declare (ignore initargs))
506 (let ((constructor (class-defstruct-constructor class)))
507 (if constructor
508 (funcall constructor)
509 (error "Don't know how to allocate ~S" class))))
511 (defmethod allocate-instance ((class condition-class) &rest initargs)
512 (declare (ignore initargs))
513 (values (allocate-condition (class-name class))))
515 (macrolet ((def (name class)
516 `(defmethod ,name ((class ,class) &rest initargs)
517 (declare (ignore initargs))
518 (error "Cannot allocate an instance of ~S." class))))
519 (def allocate-instance system-class))
521 ;;; AMOP says that CLASS-SLOTS signals an error for unfinalized classes.
522 (defmethod class-slots :before ((class slot-class))
523 (unless (class-finalized-p class)
524 (error 'simple-reference-error
525 :format-control "~S called on ~S, which is not yet finalized."
526 :format-arguments (list 'class-slots class)
527 :references (list '(:amop :generic-function class-slots)))))