1 ;;;; This software is part of the SBCL system. See the README file for
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
10 ;;;; copyright information from original PCL sources:
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
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
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
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 (format stream
"The slot ~S is unbound in the object ~S."
32 (cell-error-name condition
)
33 (unbound-slot-instance condition
)))))
35 (defmethod wrapper-fetcher ((class standard-class
))
36 'std-instance-wrapper
)
38 (defmethod slots-fetcher ((class standard-class
))
41 (defmethod raw-instance-allocator ((class standard-class
))
42 'allocate-standard-instance
)
44 ;;; These four functions work on std-instances and fsc-instances. These are
45 ;;; instances for which it is possible to change the wrapper and the slots.
47 ;;; For these kinds of instances, most specified methods from the instance
48 ;;; structure protocol are promoted to the implementation-specific class
49 ;;; std-class. Many of these methods call these four functions.
51 (defun set-wrapper (inst new
)
52 (cond ((std-instance-p inst
)
53 (setf (std-instance-wrapper inst
) new
))
54 ((fsc-instance-p inst
)
55 (setf (fsc-instance-wrapper inst
) new
))
57 (error "unrecognized instance type"))))
59 (defun swap-wrappers-and-slots (i1 i2
)
60 (with-pcl-lock ;FIXME is this sufficient?
61 (cond ((std-instance-p i1
)
62 (let ((w1 (std-instance-wrapper i1
))
63 (s1 (std-instance-slots i1
)))
64 (setf (std-instance-wrapper i1
) (std-instance-wrapper i2
))
65 (setf (std-instance-slots i1
) (std-instance-slots i2
))
66 (setf (std-instance-wrapper i2
) w1
)
67 (setf (std-instance-slots i2
) s1
)))
69 (let ((w1 (fsc-instance-wrapper i1
))
70 (s1 (fsc-instance-slots i1
)))
71 (setf (fsc-instance-wrapper i1
) (fsc-instance-wrapper i2
))
72 (setf (fsc-instance-slots i1
) (fsc-instance-slots i2
))
73 (setf (fsc-instance-wrapper i2
) w1
)
74 (setf (fsc-instance-slots i2
) s1
)))
76 (error "unrecognized instance type")))))
78 ;;;; SLOT-VALUE, (SETF SLOT-VALUE), SLOT-BOUNDP
80 (declaim (ftype (sfunction (t symbol
) t
) slot-value
))
81 (defun slot-value (object slot-name
)
82 (let* ((class (class-of object
))
83 (slot-definition (find-slot-definition class slot-name
)))
84 (if (null slot-definition
)
85 (values (slot-missing class object slot-name
'slot-value
))
86 (slot-value-using-class class object slot-definition
))))
88 (define-compiler-macro slot-value
(&whole form object slot-name
90 (if (and (constantp slot-name env
)
91 (interned-symbol-p (constant-form-value slot-name env
)))
92 `(accessor-slot-value ,object
,slot-name
)
95 (defun set-slot-value (object slot-name new-value
)
96 (let* ((class (class-of object
))
97 (slot-definition (find-slot-definition class slot-name
)))
98 (if (null slot-definition
)
99 (progn (slot-missing class object slot-name
'setf new-value
)
101 (setf (slot-value-using-class class object slot-definition
)
104 ;;; A version of SET-SLOT-VALUE for use in safe code, where we want to
105 ;;; check types when writing to slots:
106 ;;; * Doesn't have an optimizing compiler-macro
107 ;;; * Isn't special-cased in WALK-METHOD-LAMBDA
108 (defun safe-set-slot-value (object slot-name new-value
)
109 (set-slot-value object slot-name new-value
))
111 (define-compiler-macro set-slot-value
(&whole form object slot-name new-value
113 (if (and (constantp slot-name env
)
114 (interned-symbol-p (constant-form-value slot-name env
))
115 ;; We can't use the ACCESSOR-SET-SLOT-VALUE path in safe
116 ;; code, since it'll use the global automatically generated
117 ;; accessor, which won't do typechecking. (SLOT-OBJECT
118 ;; won't have been compiled with SAFETY 3, so SAFE-P will
119 ;; be NIL in MAKE-STD-WRITER-METHOD-FUNCTION).
120 (not (safe-code-p env
)))
121 `(accessor-set-slot-value ,object
,slot-name
,new-value
)
124 (defun slot-boundp (object slot-name
)
125 (let* ((class (class-of object
))
126 (slot-definition (find-slot-definition class slot-name
)))
127 (if (null slot-definition
)
128 (not (not (slot-missing class object slot-name
'slot-boundp
)))
129 (slot-boundp-using-class class object slot-definition
))))
131 (setf (gdefinition 'slot-boundp-normal
) #'slot-boundp
)
133 (define-compiler-macro slot-boundp
(&whole form object slot-name
135 (if (and (constantp slot-name env
)
136 (interned-symbol-p (constant-form-value slot-name env
)))
137 `(accessor-slot-boundp ,object
,slot-name
)
140 (defun slot-makunbound (object slot-name
)
141 (let* ((class (class-of object
))
142 (slot-definition (find-slot-definition class slot-name
)))
143 (if (null slot-definition
)
144 (slot-missing class object slot-name
'slot-makunbound
)
145 (slot-makunbound-using-class class object slot-definition
))
148 (defun slot-exists-p (object slot-name
)
149 (let ((class (class-of object
)))
150 (not (null (find-slot-definition class slot-name
)))))
152 (defvar *unbound-slot-value-marker
* (make-unprintable-object "unbound slot"))
154 ;;; This isn't documented, but is used within PCL in a number of print
155 ;;; object methods. (See NAMED-OBJECT-PRINT-FUNCTION.)
156 (defun slot-value-or-default (object slot-name
&optional
157 (default *unbound-slot-value-marker
*))
158 (if (slot-boundp object slot-name
)
159 (slot-value object slot-name
)
162 (declaim (inline standard-instance-access
(setf standard-instance-access
)
163 funcallable-standard-instance-access
164 (setf funcallable-standard-instance-access
)))
166 (defun standard-instance-access (instance location
)
167 (clos-slots-ref (std-instance-slots instance
) location
))
169 (defun (setf standard-instance-access
) (new-value instance location
)
170 (setf (clos-slots-ref (std-instance-slots instance
) location
) new-value
))
172 (defun funcallable-standard-instance-access (instance location
)
173 (clos-slots-ref (fsc-instance-slots instance
) location
))
175 (defun (setf funcallable-standard-instance-access
) (new-value instance location
)
176 (setf (clos-slots-ref (fsc-instance-slots instance
) location
) new-value
))
178 (defmethod slot-value-using-class ((class std-class
)
179 (object standard-object
)
180 (slotd standard-effective-slot-definition
))
181 (check-obsolete-instance object
)
182 (let* ((location (slot-definition-location slotd
))
186 (cond ((std-instance-p object
)
187 (clos-slots-ref (std-instance-slots object
)
189 ((fsc-instance-p object
)
190 (clos-slots-ref (fsc-instance-slots object
)
192 (t (bug "unrecognized instance type in ~S"
193 'slot-value-using-class
))))
197 (instance-structure-protocol-error slotd
198 'slot-value-using-class
)))))
199 (if (eq value
+slot-unbound
+)
200 (values (slot-unbound class object
(slot-definition-name slotd
)))
203 (defmethod (setf slot-value-using-class
)
204 (new-value (class std-class
)
205 (object standard-object
)
206 (slotd standard-effective-slot-definition
))
207 (check-obsolete-instance object
)
208 (let ((location (slot-definition-location slotd
))
211 (slot-definition-type-check-function slotd
))))
212 (flet ((check (new-value)
213 (when type-check-function
214 (funcall (the function type-check-function
) new-value
))
218 (cond ((std-instance-p object
)
219 (setf (clos-slots-ref (std-instance-slots object
) location
)
221 ((fsc-instance-p object
)
222 (setf (clos-slots-ref (fsc-instance-slots object
) location
)
224 (t (bug "unrecognized instance type in ~S"
225 '(setf slot-value-using-class
)))))
227 (setf (cdr location
) (check new-value
)))
229 (instance-structure-protocol-error
230 slotd
'(setf slot-value-using-class
)))))))
232 (defmethod slot-boundp-using-class
234 (object standard-object
)
235 (slotd standard-effective-slot-definition
))
236 (check-obsolete-instance object
)
237 (let* ((location (slot-definition-location slotd
))
241 (cond ((std-instance-p object
)
242 (clos-slots-ref (std-instance-slots object
)
244 ((fsc-instance-p object
)
245 (clos-slots-ref (fsc-instance-slots object
)
247 (t (bug "unrecognized instance type in ~S"
248 'slot-boundp-using-class
))))
252 (instance-structure-protocol-error slotd
253 'slot-boundp-using-class
)))))
254 (not (eq value
+slot-unbound
+))))
256 (defmethod slot-makunbound-using-class
258 (object standard-object
)
259 (slotd standard-effective-slot-definition
))
260 (check-obsolete-instance object
)
261 (let ((location (slot-definition-location slotd
)))
264 (cond ((std-instance-p object
)
265 (setf (clos-slots-ref (std-instance-slots object
) location
)
267 ((fsc-instance-p object
)
268 (setf (clos-slots-ref (fsc-instance-slots object
) location
)
270 (t (bug "unrecognized instance type in ~S"
271 'slot-makunbound-using-class
))))
273 (setf (cdr location
) +slot-unbound
+))
275 (instance-structure-protocol-error slotd
276 'slot-makunbound-using-class
))))
279 (defmethod slot-value-using-class
280 ((class condition-class
)
282 (slotd condition-effective-slot-definition
))
283 (let ((fun (slot-definition-reader-function slotd
)))
284 (declare (type function fun
))
285 (funcall fun object
)))
287 (defmethod (setf slot-value-using-class
)
289 (class condition-class
)
291 (slotd condition-effective-slot-definition
))
292 (let ((fun (slot-definition-writer-function slotd
)))
293 (declare (type function fun
))
294 (funcall fun new-value object
)))
296 (defmethod slot-boundp-using-class
297 ((class condition-class
)
299 (slotd condition-effective-slot-definition
))
300 (let ((fun (slot-definition-boundp-function slotd
)))
301 (declare (type function fun
))
302 (funcall fun object
)))
304 (defmethod slot-makunbound-using-class ((class condition-class
) object slot
)
305 (error "attempt to unbind slot ~S in condition object ~S."
308 (defmethod slot-value-using-class
309 ((class structure-class
)
310 (object structure-object
)
311 (slotd structure-effective-slot-definition
))
312 (let* ((function (slot-definition-internal-reader-function slotd
))
313 (value (funcall function object
)))
314 (declare (type function function
))
315 (if (eq value
+slot-unbound
+)
316 (values (slot-unbound class object
(slot-definition-name slotd
)))
319 (defmethod (setf slot-value-using-class
)
320 (new-value (class structure-class
)
321 (object structure-object
)
322 (slotd structure-effective-slot-definition
))
323 (let ((function (slot-definition-internal-writer-function slotd
)))
324 (declare (type function function
))
325 (funcall function new-value object
)))
327 (defmethod slot-boundp-using-class
328 ((class structure-class
)
329 (object structure-object
)
330 (slotd structure-effective-slot-definition
))
333 (defmethod slot-makunbound-using-class
334 ((class structure-class
)
335 (object structure-object
)
336 (slotd structure-effective-slot-definition
))
337 (error "Structure slots can't be unbound."))
339 (defmethod slot-missing
340 ((class t
) instance slot-name operation
&optional new-value
)
341 (error "~@<When attempting to ~A, the slot ~S is missing from the ~
344 (slot-value "read the slot's value (slot-value)")
346 "set the slot's value to ~S (SETF of SLOT-VALUE)"
348 (slot-boundp "test to see whether slot is bound (SLOT-BOUNDP)")
349 (slot-makunbound "make the slot unbound (SLOT-MAKUNBOUND)"))
353 (defmethod slot-unbound ((class t
) instance slot-name
)
355 (error 'unbound-slot
:name slot-name
:instance instance
)
357 :report
"Return a value as the slot-value."
358 :interactive read-evaluated-form
361 :report
"Store and return a value as the slot-value."
362 :interactive read-evaluated-form
363 (setf (slot-value instance slot-name
) v
))))
365 (defun slot-unbound-internal (instance position
)
372 (nth position
(wrapper-instance-slots-layout (wrapper-of instance
))))
376 ;;; FIXME: AMOP says that allocate-instance imples finalize-inheritance
377 ;;; if the class is not yet finalized, but we don't seem to be taking
378 ;;; care of this for non-standard-classes.x
379 (defmethod allocate-instance ((class standard-class
) &rest initargs
)
380 (declare (ignore initargs
))
381 (unless (class-finalized-p class
)
382 (finalize-inheritance class
))
383 (allocate-standard-instance (class-wrapper class
)))
385 (defmethod allocate-instance ((class structure-class
) &rest initargs
)
386 (declare (ignore initargs
))
387 (let ((constructor (class-defstruct-constructor class
)))
389 (funcall constructor
)
390 (allocate-standard-instance (class-wrapper class
)))))
392 ;;; FIXME: It would be nicer to have allocate-instance return
393 ;;; uninitialized objects for conditions as well.
394 (defmethod allocate-instance ((class condition-class
) &rest initargs
)
395 (declare (ignore initargs
))
396 (make-condition (class-name class
)))
398 (defmethod allocate-instance ((class built-in-class
) &rest initargs
)
399 (declare (ignore initargs
))
400 (error "Cannot allocate an instance of ~S." class
)) ; So sayeth AMOP