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
29 ;;;; ANSI CL condition for unbound slots
31 (define-condition unbound-slot (cell-error)
32 ((instance :reader unbound-slot-instance :initarg :instance)
33 (slot :reader unbound-slot-slot :initarg :slot))
34 (:report (lambda(condition stream)
35 (format stream "The slot ~S is unbound in the object ~S"
36 (unbound-slot-slot condition)
37 (unbound-slot-instance condition)))))
39 (defmethod wrapper-fetcher ((class standard-class))
40 'std-instance-wrapper)
42 (defmethod slots-fetcher ((class standard-class))
45 (defmethod raw-instance-allocator ((class standard-class))
46 'allocate-standard-instance)
48 ;;; These four functions work on std-instances and fsc-instances. These are
49 ;;; instances for which it is possible to change the wrapper and the slots.
51 ;;; For these kinds of instances, most specified methods from the instance
52 ;;; structure protocol are promoted to the implementation-specific class
53 ;;; std-class. Many of these methods call these four functions.
55 (defun set-wrapper (inst new)
56 (cond ((std-instance-p inst)
57 (setf (std-instance-wrapper inst) new))
58 ((fsc-instance-p inst)
59 (setf (fsc-instance-wrapper inst) new))
61 (error "unrecognized instance type"))))
63 (defun swap-wrappers-and-slots (i1 i2)
65 (cond ((std-instance-p i1)
66 (let ((w1 (std-instance-wrapper i1))
67 (s1 (std-instance-slots i1)))
68 (setf (std-instance-wrapper i1) (std-instance-wrapper i2))
69 (setf (std-instance-slots i1) (std-instance-slots i2))
70 (setf (std-instance-wrapper i2) w1)
71 (setf (std-instance-slots i2) s1)))
73 (let ((w1 (fsc-instance-wrapper i1))
74 (s1 (fsc-instance-slots i1)))
75 (setf (fsc-instance-wrapper i1) (fsc-instance-wrapper i2))
76 (setf (fsc-instance-slots i1) (fsc-instance-slots i2))
77 (setf (fsc-instance-wrapper i2) w1)
78 (setf (fsc-instance-slots i2) s1)))
80 (error "unrecognized instance type")))))
82 (defun get-class-slot-value-1 (object wrapper slot-name)
83 (let ((entry (assoc slot-name (wrapper-class-slots wrapper))))
85 (slot-missing (wrapper-class wrapper) object slot-name 'slot-value)
86 (if (eq (cdr entry) *slot-unbound*)
87 (slot-unbound (wrapper-class wrapper) object slot-name)
90 (defun set-class-slot-value-1 (new-value object wrapper slot-name)
91 (let ((entry (assoc slot-name (wrapper-class-slots wrapper))))
93 (slot-missing (wrapper-class wrapper)
98 (setf (cdr entry) new-value))))
100 (defmethod class-slot-value ((class std-class) slot-name)
101 (let ((wrapper (class-wrapper class))
102 (prototype (class-prototype class)))
103 (get-class-slot-value-1 prototype wrapper slot-name)))
105 (defmethod (setf class-slot-value) (nv (class std-class) slot-name)
106 (let ((wrapper (class-wrapper class))
107 (prototype (class-prototype class)))
108 (set-class-slot-value-1 nv prototype wrapper slot-name)))
110 (defun find-slot-definition (class slot-name)
111 (dolist (slot (class-slots class) nil)
112 (when (eql slot-name (slot-definition-name slot))
115 (defun slot-value (object slot-name)
116 (let* ((class (class-of object))
117 (slot-definition (find-slot-definition class slot-name)))
118 (if (null slot-definition)
119 (slot-missing class object slot-name 'slot-value)
120 (slot-value-using-class class object slot-definition))))
122 (setf (gdefinition 'slot-value-normal) #'slot-value)
124 (define-compiler-macro slot-value (object-form slot-name-form)
125 (if (and (constantp slot-name-form)
126 (let ((slot-name (eval slot-name-form)))
127 (and (symbolp slot-name) (symbol-package slot-name))))
128 `(accessor-slot-value ,object-form ,slot-name-form)
129 `(slot-value-normal ,object-form ,slot-name-form)))
131 (defun set-slot-value (object slot-name new-value)
132 (let* ((class (class-of object))
133 (slot-definition (find-slot-definition class slot-name)))
134 (if (null slot-definition)
135 (slot-missing class object slot-name 'setf)
136 (setf (slot-value-using-class class object slot-definition)
139 (setf (gdefinition 'set-slot-value-normal) #'set-slot-value)
141 (define-compiler-macro set-slot-value (object-form slot-name-form new-value-form)
142 (if (and (constantp slot-name-form)
143 (let ((slot-name (eval slot-name-form)))
144 (and (symbolp slot-name) (symbol-package slot-name))))
145 `(accessor-set-slot-value ,object-form ,slot-name-form ,new-value-form)
146 `(set-slot-value-normal ,object-form ,slot-name-form ,new-value-form)))
148 (defconstant *optimize-slot-boundp* nil)
150 (defun slot-boundp (object slot-name)
151 (let* ((class (class-of object))
152 (slot-definition (find-slot-definition class slot-name)))
153 (if (null slot-definition)
154 (slot-missing class object slot-name 'slot-boundp)
155 (slot-boundp-using-class class object slot-definition))))
157 (setf (gdefinition 'slot-boundp-normal) #'slot-boundp)
159 (define-compiler-macro slot-boundp (object-form slot-name-form)
160 (if (and (constantp slot-name-form)
161 (let ((slot-name (eval slot-name-form)))
162 (and (symbolp slot-name) (symbol-package slot-name))))
163 `(accessor-slot-boundp ,object-form ,slot-name-form)
164 `(slot-boundp-normal ,object-form ,slot-name-form)))
166 (defun slot-makunbound (object slot-name)
167 (let* ((class (class-of object))
168 (slot-definition (find-slot-definition class slot-name)))
169 (if (null slot-definition)
170 (slot-missing class object slot-name 'slot-makunbound)
171 (slot-makunbound-using-class class object slot-definition))))
173 (defun slot-exists-p (object slot-name)
174 (let ((class (class-of object)))
175 (not (null (find-slot-definition class slot-name)))))
177 ;;; This isn't documented, but is used within PCL in a number of print
178 ;;; object methods. (See NAMED-OBJECT-PRINT-FUNCTION.)
179 (defun slot-value-or-default (object slot-name &optional (default "unbound"))
180 (if (slot-boundp object slot-name)
181 (slot-value object slot-name)
184 (defun standard-instance-access (instance location)
185 (%instance-ref (std-instance-slots instance) location))
187 (defun funcallable-standard-instance-access (instance location)
188 (%instance-ref (fsc-instance-slots instance) location))
190 (defmethod slot-value-using-class ((class std-class)
192 (slotd standard-effective-slot-definition))
193 (let* ((location (slot-definition-location slotd))
194 (value (typecase location
196 (cond ((std-instance-p object)
197 ;; FIXME: EQ T (WRAPPER-STATE ..) is better done
198 ;; through INVALID-WRAPPER-P (here and below).
199 (unless (eq 't (wrapper-state (std-instance-wrapper object)))
200 (check-wrapper-validity object))
201 (%instance-ref (std-instance-slots object) location))
202 ((fsc-instance-p object)
203 (unless (eq 't (wrapper-state (fsc-instance-wrapper object)))
204 (check-wrapper-validity object))
205 (%instance-ref (fsc-instance-slots object) location))
206 (t (error "unrecognized instance type"))))
210 (error "The slot ~S has neither :INSTANCE nor :CLASS allocation, ~@
211 so it can't be read by the default ~S method."
212 slotd 'slot-value-using-class)))))
213 (if (eq value *slot-unbound*)
214 (slot-unbound class object (slot-definition-name slotd))
217 (defmethod (setf slot-value-using-class)
218 (new-value (class std-class)
220 (slotd standard-effective-slot-definition))
221 (let ((location (slot-definition-location slotd)))
224 (cond ((std-instance-p object)
225 (unless (eq 't (wrapper-state (std-instance-wrapper object)))
226 (check-wrapper-validity object))
227 (setf (%instance-ref (std-instance-slots object) location) new-value))
228 ((fsc-instance-p object)
229 (unless (eq 't (wrapper-state (fsc-instance-wrapper object)))
230 (check-wrapper-validity object))
231 (setf (%instance-ref (fsc-instance-slots object) location) new-value))
232 (t (error "unrecognized instance type"))))
234 (setf (cdr location) new-value))
236 (error "The slot ~S has neither :INSTANCE nor :CLASS allocation, ~@
237 so it can't be written by the default ~S method."
238 slotd '(setf slot-value-using-class))))))
240 (defmethod slot-boundp-using-class
243 (slotd standard-effective-slot-definition))
244 (let* ((location (slot-definition-location slotd))
245 (value (typecase location
247 (cond ((std-instance-p object)
248 (unless (eq 't (wrapper-state (std-instance-wrapper object)))
249 (check-wrapper-validity object))
250 (%instance-ref (std-instance-slots object) location))
251 ((fsc-instance-p object)
252 (unless (eq 't (wrapper-state (fsc-instance-wrapper object)))
253 (check-wrapper-validity object))
254 (%instance-ref (fsc-instance-slots object) location))
255 (t (error "unrecognized instance type"))))
259 (error "The slot ~S has neither :INSTANCE nor :CLASS allocation, ~@
260 so it can't be read by the default ~S method."
261 slotd 'slot-boundp-using-class)))))
262 (not (eq value *slot-unbound*))))
264 (defmethod slot-makunbound-using-class
267 (slotd standard-effective-slot-definition))
268 (let ((location (slot-definition-location slotd)))
271 (cond ((std-instance-p object)
272 (unless (eq 't (wrapper-state (std-instance-wrapper object)))
273 (check-wrapper-validity object))
274 (setf (%instance-ref (std-instance-slots object) location) *slot-unbound*))
275 ((fsc-instance-p object)
276 (unless (eq 't (wrapper-state (fsc-instance-wrapper object)))
277 (check-wrapper-validity object))
278 (setf (%instance-ref (fsc-instance-slots object) location) *slot-unbound*))
279 (t (error "unrecognized instance type"))))
281 (setf (cdr location) *slot-unbound*))
283 (error "The slot ~S has neither :INSTANCE nor :CLASS allocation, ~@
284 so it can't be written by the default ~S method."
285 slotd 'slot-makunbound-using-class))))
288 (defmethod slot-value-using-class
289 ((class structure-class)
290 (object structure-object)
291 (slotd structure-effective-slot-definition))
292 (let* ((function (slot-definition-internal-reader-function slotd))
293 (value (funcall function object)))
294 (declare (type function function))
295 (if (eq value *slot-unbound*)
296 (slot-unbound class object (slot-definition-name slotd))
299 (defmethod (setf slot-value-using-class)
300 (new-value (class structure-class)
301 (object structure-object)
302 (slotd structure-effective-slot-definition))
303 (let ((function (slot-definition-internal-writer-function slotd)))
304 (declare (type function function))
305 (funcall function new-value object)))
307 (defmethod slot-boundp-using-class
308 ((class structure-class)
309 (object structure-object)
310 (slotd structure-effective-slot-definition))
313 (defmethod slot-makunbound-using-class
314 ((class structure-class)
315 (object structure-object)
316 (slotd structure-effective-slot-definition))
317 (error "Structure slots can't be unbound."))
319 (defmethod slot-missing
320 ((class t) instance slot-name operation &optional new-value)
321 (error "When attempting to ~A,~%the slot ~S is missing from the object ~S."
323 (slot-value "read the slot's value (slot-value)")
325 "set the slot's value to ~S (setf of slot-value)"
327 (slot-boundp "test to see whether slot is bound (slot-boundp)")
328 (slot-makunbound "make the slot unbound (slot-makunbound)"))
332 (defmethod slot-unbound ((class t) instance slot-name)
333 (error 'unbound-slot :slot slot-name :instance instance))
335 (defun slot-unbound-internal (instance position)
336 (slot-unbound (class-of instance) instance
340 (wrapper-instance-slots-layout (wrapper-of instance))))
344 (defmethod allocate-instance ((class standard-class) &rest initargs)
345 (declare (ignore initargs))
346 (unless (class-finalized-p class) (finalize-inheritance class))
347 (allocate-standard-instance (class-wrapper class)))
349 (defmethod allocate-instance ((class structure-class) &rest initargs)
350 (declare (ignore initargs))
351 (let ((constructor (class-defstruct-constructor class)))
353 (funcall constructor)
354 (error "can't allocate an instance of class ~S" (class-name class)))))