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 (slot :reader unbound-slot-slot :initarg :slot))
31 (:report (lambda(condition stream)
32 (format stream "The slot ~S is unbound in the object ~S"
33 (unbound-slot-slot condition)
34 (unbound-slot-instance condition)))))
36 (defmethod wrapper-fetcher ((class standard-class))
37 'std-instance-wrapper)
39 (defmethod slots-fetcher ((class standard-class))
42 (defmethod raw-instance-allocator ((class standard-class))
43 'allocate-standard-instance)
45 ;;; These four functions work on std-instances and fsc-instances. These are
46 ;;; instances for which it is possible to change the wrapper and the slots.
48 ;;; For these kinds of instances, most specified methods from the instance
49 ;;; structure protocol are promoted to the implementation-specific class
50 ;;; std-class. Many of these methods call these four functions.
52 (defun set-wrapper (inst new)
53 (cond ((std-instance-p inst)
54 (setf (std-instance-wrapper inst) new))
55 ((fsc-instance-p inst)
56 (setf (fsc-instance-wrapper inst) new))
58 (error "unrecognized instance type"))))
60 (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)))
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 (defun get-class-slot-value-1 (object wrapper slot-name)
80 (let ((entry (assoc slot-name (wrapper-class-slots wrapper))))
82 (slot-missing (wrapper-class wrapper) object slot-name 'slot-value)
83 (if (eq (cdr entry) *slot-unbound*)
84 (slot-unbound (wrapper-class wrapper) object slot-name)
87 (defun set-class-slot-value-1 (new-value object wrapper slot-name)
88 (let ((entry (assoc slot-name (wrapper-class-slots wrapper))))
90 (slot-missing (wrapper-class wrapper)
95 (setf (cdr entry) new-value))))
97 (defmethod class-slot-value ((class std-class) slot-name)
98 (let ((wrapper (class-wrapper class))
99 (prototype (class-prototype class)))
100 (get-class-slot-value-1 prototype wrapper slot-name)))
102 (defmethod (setf class-slot-value) (nv (class std-class) slot-name)
103 (let ((wrapper (class-wrapper class))
104 (prototype (class-prototype class)))
105 (set-class-slot-value-1 nv prototype wrapper slot-name)))
107 (defun find-slot-definition (class slot-name)
108 (dolist (slot (class-slots class) nil)
109 (when (eql slot-name (slot-definition-name slot))
112 (defun slot-value (object slot-name)
113 (let* ((class (class-of object))
114 (slot-definition (find-slot-definition class slot-name)))
115 (if (null slot-definition)
116 (slot-missing class object slot-name 'slot-value)
117 (slot-value-using-class class object slot-definition))))
119 (setf (gdefinition 'slot-value-normal) #'slot-value)
121 (define-compiler-macro slot-value (object-form slot-name-form)
122 (if (and (constantp slot-name-form)
123 (let ((slot-name (eval slot-name-form)))
124 (and (symbolp slot-name) (symbol-package slot-name))))
125 `(accessor-slot-value ,object-form ,slot-name-form)
126 `(slot-value-normal ,object-form ,slot-name-form)))
128 (defun set-slot-value (object slot-name new-value)
129 (let* ((class (class-of object))
130 (slot-definition (find-slot-definition class slot-name)))
131 (if (null slot-definition)
132 (slot-missing class object slot-name 'setf)
133 (setf (slot-value-using-class class object slot-definition)
136 (setf (gdefinition 'set-slot-value-normal) #'set-slot-value)
138 (define-compiler-macro set-slot-value (object-form slot-name-form new-value-form)
139 (if (and (constantp slot-name-form)
140 (let ((slot-name (eval slot-name-form)))
141 (and (symbolp slot-name) (symbol-package slot-name))))
142 `(accessor-set-slot-value ,object-form ,slot-name-form ,new-value-form)
143 `(set-slot-value-normal ,object-form ,slot-name-form ,new-value-form)))
145 (defconstant *optimize-slot-boundp* nil)
147 (defun slot-boundp (object slot-name)
148 (let* ((class (class-of object))
149 (slot-definition (find-slot-definition class slot-name)))
150 (if (null slot-definition)
151 (slot-missing class object slot-name 'slot-boundp)
152 (slot-boundp-using-class class object slot-definition))))
154 (setf (gdefinition 'slot-boundp-normal) #'slot-boundp)
156 (define-compiler-macro slot-boundp (object-form slot-name-form)
157 (if (and (constantp slot-name-form)
158 (let ((slot-name (eval slot-name-form)))
159 (and (symbolp slot-name) (symbol-package slot-name))))
160 `(accessor-slot-boundp ,object-form ,slot-name-form)
161 `(slot-boundp-normal ,object-form ,slot-name-form)))
163 (defun slot-makunbound (object slot-name)
164 (let* ((class (class-of object))
165 (slot-definition (find-slot-definition class slot-name)))
166 (if (null slot-definition)
167 (slot-missing class object slot-name 'slot-makunbound)
168 (slot-makunbound-using-class class object slot-definition))))
170 (defun slot-exists-p (object slot-name)
171 (let ((class (class-of object)))
172 (not (null (find-slot-definition class slot-name)))))
174 ;;; This isn't documented, but is used within PCL in a number of print
175 ;;; object methods. (See NAMED-OBJECT-PRINT-FUNCTION.)
176 (defun slot-value-or-default (object slot-name &optional (default "unbound"))
177 (if (slot-boundp object slot-name)
178 (slot-value object slot-name)
181 (defun standard-instance-access (instance location)
182 (%instance-ref (std-instance-slots instance) location))
184 (defun funcallable-standard-instance-access (instance location)
185 (%instance-ref (fsc-instance-slots instance) location))
187 (defmethod slot-value-using-class ((class std-class)
189 (slotd standard-effective-slot-definition))
190 (let* ((location (slot-definition-location slotd))
191 (value (typecase location
193 (cond ((std-instance-p object)
194 ;; FIXME: EQ T (WRAPPER-STATE ..) is better done
195 ;; through INVALID-WRAPPER-P (here and below).
196 (unless (eq 't (wrapper-state (std-instance-wrapper object)))
197 (check-wrapper-validity object))
198 (%instance-ref (std-instance-slots object) location))
199 ((fsc-instance-p object)
200 (unless (eq 't (wrapper-state (fsc-instance-wrapper object)))
201 (check-wrapper-validity object))
202 (%instance-ref (fsc-instance-slots object) location))
203 (t (error "unrecognized instance type"))))
207 (error "The slot ~S has neither :INSTANCE nor :CLASS allocation, ~@
208 so it can't be read by the default ~S method."
209 slotd 'slot-value-using-class)))))
210 (if (eq value *slot-unbound*)
211 (slot-unbound class object (slot-definition-name slotd))
214 (defmethod (setf slot-value-using-class)
215 (new-value (class std-class)
217 (slotd standard-effective-slot-definition))
218 (let ((location (slot-definition-location slotd)))
221 (cond ((std-instance-p object)
222 (unless (eq 't (wrapper-state (std-instance-wrapper object)))
223 (check-wrapper-validity object))
224 (setf (%instance-ref (std-instance-slots object) location) new-value))
225 ((fsc-instance-p object)
226 (unless (eq 't (wrapper-state (fsc-instance-wrapper object)))
227 (check-wrapper-validity object))
228 (setf (%instance-ref (fsc-instance-slots object) location) new-value))
229 (t (error "unrecognized instance type"))))
231 (setf (cdr location) new-value))
233 (error "The slot ~S has neither :INSTANCE nor :CLASS allocation, ~@
234 so it can't be written by the default ~S method."
235 slotd '(setf slot-value-using-class))))))
237 (defmethod slot-boundp-using-class
240 (slotd standard-effective-slot-definition))
241 (let* ((location (slot-definition-location slotd))
242 (value (typecase location
244 (cond ((std-instance-p object)
245 (unless (eq 't (wrapper-state (std-instance-wrapper object)))
246 (check-wrapper-validity object))
247 (%instance-ref (std-instance-slots object) location))
248 ((fsc-instance-p object)
249 (unless (eq 't (wrapper-state (fsc-instance-wrapper object)))
250 (check-wrapper-validity object))
251 (%instance-ref (fsc-instance-slots object) location))
252 (t (error "unrecognized instance type"))))
256 (error "The slot ~S has neither :INSTANCE nor :CLASS allocation, ~@
257 so it can't be read by the default ~S method."
258 slotd 'slot-boundp-using-class)))))
259 (not (eq value *slot-unbound*))))
261 (defmethod slot-makunbound-using-class
264 (slotd standard-effective-slot-definition))
265 (let ((location (slot-definition-location slotd)))
268 (cond ((std-instance-p object)
269 (unless (eq 't (wrapper-state (std-instance-wrapper object)))
270 (check-wrapper-validity object))
271 (setf (%instance-ref (std-instance-slots object) location) *slot-unbound*))
272 ((fsc-instance-p object)
273 (unless (eq 't (wrapper-state (fsc-instance-wrapper object)))
274 (check-wrapper-validity object))
275 (setf (%instance-ref (fsc-instance-slots object) location) *slot-unbound*))
276 (t (error "unrecognized instance type"))))
278 (setf (cdr location) *slot-unbound*))
280 (error "The slot ~S has neither :INSTANCE nor :CLASS allocation, ~@
281 so it can't be written by the default ~S method."
282 slotd 'slot-makunbound-using-class))))
285 (defmethod slot-value-using-class
286 ((class structure-class)
287 (object structure-object)
288 (slotd structure-effective-slot-definition))
289 (let* ((function (slot-definition-internal-reader-function slotd))
290 (value (funcall function object)))
291 (declare (type function function))
292 (if (eq value *slot-unbound*)
293 (slot-unbound class object (slot-definition-name slotd))
296 (defmethod (setf slot-value-using-class)
297 (new-value (class structure-class)
298 (object structure-object)
299 (slotd structure-effective-slot-definition))
300 (let ((function (slot-definition-internal-writer-function slotd)))
301 (declare (type function function))
302 (funcall function new-value object)))
304 (defmethod slot-boundp-using-class
305 ((class structure-class)
306 (object structure-object)
307 (slotd structure-effective-slot-definition))
310 (defmethod slot-makunbound-using-class
311 ((class structure-class)
312 (object structure-object)
313 (slotd structure-effective-slot-definition))
314 (error "Structure slots can't be unbound."))
316 (defmethod slot-missing
317 ((class t) instance slot-name operation &optional new-value)
318 (error "When attempting to ~A,~%the slot ~S is missing from the object ~S."
320 (slot-value "read the slot's value (slot-value)")
322 "set the slot's value to ~S (setf of slot-value)"
324 (slot-boundp "test to see whether slot is bound (slot-boundp)")
325 (slot-makunbound "make the slot unbound (slot-makunbound)"))
329 (defmethod slot-unbound ((class t) instance slot-name)
330 (error 'unbound-slot :slot slot-name :instance instance))
332 (defun slot-unbound-internal (instance position)
333 (slot-unbound (class-of instance) instance
337 (wrapper-instance-slots-layout (wrapper-of instance))))
341 (defmethod allocate-instance ((class standard-class) &rest initargs)
342 (declare (ignore initargs))
343 (unless (class-finalized-p class) (finalize-inheritance class))
344 (allocate-standard-instance (class-wrapper class)))
346 (defmethod allocate-instance ((class structure-class) &rest initargs)
347 (declare (ignore initargs))
348 (let ((constructor (class-defstruct-constructor class)))
350 (funcall constructor)
351 (error "can't allocate an instance of class ~S" (class-name class)))))