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 (defun find-slot-definition (class slot-name
)
79 (dolist (slot (class-slots class
) nil
)
80 (when (eql slot-name
(slot-definition-name slot
))
83 (defun slot-value (object slot-name
)
84 (let* ((class (class-of object
))
85 (slot-definition (find-slot-definition class slot-name
)))
86 (if (null slot-definition
)
87 (values (slot-missing class object slot-name
'slot-value
))
88 (slot-value-using-class class object slot-definition
))))
90 (define-compiler-macro slot-value
(&whole form object slot-name
)
91 (if (and (constantp slot-name
)
92 (interned-symbol-p (eval slot-name
)))
93 `(accessor-slot-value ,object
,slot-name
)
96 (defun set-slot-value (object slot-name new-value
)
97 (let* ((class (class-of object
))
98 (slot-definition (find-slot-definition class slot-name
)))
99 (if (null slot-definition
)
100 (progn (slot-missing class object slot-name
'setf new-value
)
102 (setf (slot-value-using-class class object slot-definition
)
105 (define-compiler-macro set-slot-value
(&whole form object slot-name new-value
)
106 (if (and (constantp slot-name
)
107 (interned-symbol-p (eval slot-name
)))
108 `(accessor-set-slot-value ,object
,slot-name
,new-value
)
111 (defun slot-boundp (object slot-name
)
112 (let* ((class (class-of object
))
113 (slot-definition (find-slot-definition class slot-name
)))
114 (if (null slot-definition
)
115 (not (not (slot-missing class object slot-name
'slot-boundp
)))
116 (slot-boundp-using-class class object slot-definition
))))
118 (setf (gdefinition 'slot-boundp-normal
) #'slot-boundp
)
120 (define-compiler-macro slot-boundp
(&whole form object slot-name
)
121 (if (and (constantp slot-name
)
122 (interned-symbol-p (eval slot-name
)))
123 `(accessor-slot-boundp ,object
,slot-name
)
126 (defun slot-makunbound (object slot-name
)
127 (let* ((class (class-of object
))
128 (slot-definition (find-slot-definition class slot-name
)))
129 (if (null slot-definition
)
130 (slot-missing class object slot-name
'slot-makunbound
)
131 (slot-makunbound-using-class class object slot-definition
))
134 (defun slot-exists-p (object slot-name
)
135 (let ((class (class-of object
)))
136 (not (null (find-slot-definition class slot-name
)))))
138 ;;; This isn't documented, but is used within PCL in a number of print
139 ;;; object methods. (See NAMED-OBJECT-PRINT-FUNCTION.)
140 (defun slot-value-or-default (object slot-name
&optional
(default "unbound"))
141 (if (slot-boundp object slot-name
)
142 (slot-value object slot-name
)
145 (defun standard-instance-access (instance location
)
146 (clos-slots-ref (std-instance-slots instance
) location
))
148 (defun funcallable-standard-instance-access (instance location
)
149 (clos-slots-ref (fsc-instance-slots instance
) location
))
151 (defmethod slot-value-using-class ((class std-class
)
153 (slotd standard-effective-slot-definition
))
154 (check-obsolete-instance object
)
155 (let* ((location (slot-definition-location slotd
))
156 (value (typecase location
158 (cond ((std-instance-p object
)
159 (clos-slots-ref (std-instance-slots object
)
161 ((fsc-instance-p object
)
162 (clos-slots-ref (fsc-instance-slots object
)
164 (t (error "unrecognized instance type"))))
168 (error "~@<The slot ~S has neither :INSTANCE nor :CLASS ~
169 allocation, so it can't be read by the default ~
171 slotd
'slot-value-using-class
)))))
172 (if (eq value
+slot-unbound
+)
173 (values (slot-unbound class object
(slot-definition-name slotd
)))
176 (defmethod (setf slot-value-using-class
)
177 (new-value (class std-class
)
179 (slotd standard-effective-slot-definition
))
180 (check-obsolete-instance object
)
181 (let ((location (slot-definition-location slotd
)))
184 (cond ((std-instance-p object
)
185 (setf (clos-slots-ref (std-instance-slots object
) location
)
187 ((fsc-instance-p object
)
188 (setf (clos-slots-ref (fsc-instance-slots object
) location
)
190 (t (error "unrecognized instance type"))))
192 (setf (cdr location
) new-value
))
194 (error "~@<The slot ~S has neither :INSTANCE nor :CLASS allocation, ~
195 so it can't be written by the default ~S method.~:@>"
196 slotd
'(setf slot-value-using-class
))))))
198 (defmethod slot-boundp-using-class
201 (slotd standard-effective-slot-definition
))
202 (check-obsolete-instance object
)
203 (let* ((location (slot-definition-location slotd
))
204 (value (typecase location
206 (cond ((std-instance-p object
)
207 (clos-slots-ref (std-instance-slots object
)
209 ((fsc-instance-p object
)
210 (clos-slots-ref (fsc-instance-slots object
)
212 (t (error "unrecognized instance type"))))
216 (error "~@<The slot ~S has neither :INSTANCE nor :CLASS ~
217 allocation, so it can't be read by the default ~S ~
219 slotd
'slot-boundp-using-class
)))))
220 (not (eq value
+slot-unbound
+))))
222 (defmethod slot-makunbound-using-class
225 (slotd standard-effective-slot-definition
))
226 (check-obsolete-instance object
)
227 (let ((location (slot-definition-location slotd
)))
230 (cond ((std-instance-p object
)
231 (setf (clos-slots-ref (std-instance-slots object
) location
)
233 ((fsc-instance-p object
)
234 (setf (clos-slots-ref (fsc-instance-slots object
) location
)
236 (t (error "unrecognized instance type"))))
238 (setf (cdr location
) +slot-unbound
+))
240 (error "~@<The slot ~S has neither :INSTANCE nor :CLASS allocation, ~
241 so it can't be written by the default ~S method.~@:>"
242 slotd
'slot-makunbound-using-class
))))
245 (defmethod slot-value-using-class
246 ((class condition-class
)
248 (slotd condition-effective-slot-definition
))
249 (let ((fun (slot-definition-reader-function slotd
)))
250 (declare (type function fun
))
251 (funcall fun object
)))
253 (defmethod (setf slot-value-using-class
)
255 (class condition-class
)
257 (slotd condition-effective-slot-definition
))
258 (let ((fun (slot-definition-writer-function slotd
)))
259 (declare (type function fun
))
260 (funcall fun new-value object
)))
262 (defmethod slot-boundp-using-class
263 ((class condition-class
)
265 (slotd condition-effective-slot-definition
))
266 (let ((fun (slot-definition-boundp-function slotd
)))
267 (declare (type function fun
))
268 (funcall fun object
)))
270 (defmethod slot-makunbound-using-class ((class condition-class
) object slot
)
271 (error "attempt to unbind slot ~S in condition object ~S."
274 (defmethod slot-value-using-class
275 ((class structure-class
)
276 (object structure-object
)
277 (slotd structure-effective-slot-definition
))
278 (let* ((function (slot-definition-internal-reader-function slotd
))
279 (value (funcall function object
)))
280 (declare (type function function
))
281 (if (eq value
+slot-unbound
+)
282 (slot-unbound class object
(slot-definition-name slotd
))
285 (defmethod (setf slot-value-using-class
)
286 (new-value (class structure-class
)
287 (object structure-object
)
288 (slotd structure-effective-slot-definition
))
289 (let ((function (slot-definition-internal-writer-function slotd
)))
290 (declare (type function function
))
291 (funcall function new-value object
)))
293 (defmethod slot-boundp-using-class
294 ((class structure-class
)
295 (object structure-object
)
296 (slotd structure-effective-slot-definition
))
299 (defmethod slot-makunbound-using-class
300 ((class structure-class
)
301 (object structure-object
)
302 (slotd structure-effective-slot-definition
))
303 (error "Structure slots can't be unbound."))
305 (defmethod slot-missing
306 ((class t
) instance slot-name operation
&optional new-value
)
307 (error "~@<When attempting to ~A, the slot ~S is missing from the ~
310 (slot-value "read the slot's value (slot-value)")
312 "set the slot's value to ~S (SETF of SLOT-VALUE)"
314 (slot-boundp "test to see whether slot is bound (SLOT-BOUNDP)")
315 (slot-makunbound "make the slot unbound (SLOT-MAKUNBOUND)"))
319 (defmethod slot-unbound ((class t
) instance slot-name
)
320 (error 'unbound-slot
:name slot-name
:instance instance
))
322 (defun slot-unbound-internal (instance position
)
329 (nth position
(wrapper-instance-slots-layout (wrapper-of instance
))))
333 (defmethod allocate-instance ((class standard-class
) &rest initargs
)
334 (declare (ignore initargs
))
335 (unless (class-finalized-p class
) (finalize-inheritance class
))
336 (allocate-standard-instance (class-wrapper class
)))
338 (defmethod allocate-instance ((class structure-class
) &rest initargs
)
339 (declare (ignore initargs
))
340 (let ((constructor (class-defstruct-constructor class
)))
342 (funcall constructor
)
343 (error "can't allocate an instance of class ~S" (class-name class
)))))
345 (defmethod allocate-instance ((class condition-class
) &rest initargs
)
346 (declare (ignore initargs
))
347 (make-condition (class-name class
)))