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 get-class-slot-value-1 (object wrapper slot-name)
79 (let ((entry (assoc slot-name (wrapper-class-slots wrapper))))
81 (slot-missing (wrapper-class wrapper) object slot-name 'slot-value)
82 (if (eq (cdr entry) +slot-unbound+)
83 (slot-unbound (wrapper-class wrapper) object slot-name)
86 (defun set-class-slot-value-1 (new-value object wrapper slot-name)
87 (let ((entry (assoc slot-name (wrapper-class-slots wrapper))))
89 (slot-missing (wrapper-class wrapper)
94 (setf (cdr entry) new-value))))
96 (defmethod class-slot-value ((class std-class) slot-name)
97 (let ((wrapper (class-wrapper class))
98 (prototype (class-prototype class)))
99 (get-class-slot-value-1 prototype wrapper slot-name)))
101 (defmethod (setf class-slot-value) (nv (class std-class) slot-name)
102 (let ((wrapper (class-wrapper class))
103 (prototype (class-prototype class)))
104 (set-class-slot-value-1 nv prototype wrapper slot-name)))
106 (defun find-slot-definition (class slot-name)
107 (dolist (slot (class-slots class) nil)
108 (when (eql slot-name (slot-definition-name slot))
111 (defun slot-value (object slot-name)
112 (let* ((class (class-of object))
113 (slot-definition (find-slot-definition class slot-name)))
114 (if (null slot-definition)
115 (slot-missing class object slot-name 'slot-value)
116 (slot-value-using-class class object slot-definition))))
118 (define-compiler-macro slot-value (&whole form object slot-name)
119 (if (and (constantp slot-name)
120 (interned-symbol-p (eval slot-name)))
121 `(accessor-slot-value ,object ,slot-name)
124 (defun set-slot-value (object slot-name new-value)
125 (let* ((class (class-of object))
126 (slot-definition (find-slot-definition class slot-name)))
127 (if (null slot-definition)
128 (slot-missing class object slot-name 'setf new-value)
129 (setf (slot-value-using-class class object slot-definition)
132 (define-compiler-macro set-slot-value (&whole form object slot-name new-value)
133 (if (and (constantp slot-name)
134 (interned-symbol-p (eval slot-name)))
135 `(accessor-set-slot-value ,object ,slot-name ,new-value)
138 (defun slot-boundp (object slot-name)
139 (let* ((class (class-of object))
140 (slot-definition (find-slot-definition class slot-name)))
141 (if (null slot-definition)
142 (slot-missing class object slot-name 'slot-boundp)
143 (slot-boundp-using-class class object slot-definition))))
145 (setf (gdefinition 'slot-boundp-normal) #'slot-boundp)
147 (define-compiler-macro slot-boundp (&whole form object slot-name)
148 (if (and (constantp slot-name)
149 (interned-symbol-p (eval slot-name)))
150 `(accessor-slot-boundp ,object ,slot-name)
153 (defun slot-makunbound (object slot-name)
154 (let* ((class (class-of object))
155 (slot-definition (find-slot-definition class slot-name)))
156 (if (null slot-definition)
157 (slot-missing class object slot-name 'slot-makunbound)
158 (slot-makunbound-using-class class object slot-definition))))
160 (defun slot-exists-p (object slot-name)
161 (let ((class (class-of object)))
162 (not (null (find-slot-definition class slot-name)))))
164 ;;; This isn't documented, but is used within PCL in a number of print
165 ;;; object methods. (See NAMED-OBJECT-PRINT-FUNCTION.)
166 (defun slot-value-or-default (object slot-name &optional (default "unbound"))
167 (if (slot-boundp object slot-name)
168 (slot-value object slot-name)
171 (defun standard-instance-access (instance location)
172 (clos-slots-ref (std-instance-slots instance) location))
174 (defun funcallable-standard-instance-access (instance location)
175 (clos-slots-ref (fsc-instance-slots instance) location))
177 (defmethod slot-value-using-class ((class std-class)
179 (slotd standard-effective-slot-definition))
180 (check-obsolete-instance object)
181 (let* ((location (slot-definition-location slotd))
182 (value (typecase location
184 (cond ((std-instance-p object)
185 (clos-slots-ref (std-instance-slots object)
187 ((fsc-instance-p object)
188 (clos-slots-ref (fsc-instance-slots object)
190 (t (error "unrecognized instance type"))))
194 (error "~@<The slot ~S has neither :INSTANCE nor :CLASS ~
195 allocation, so it can't be read by the default ~
197 slotd 'slot-value-using-class)))))
198 (if (eq value +slot-unbound+)
199 (slot-unbound class object (slot-definition-name slotd))
202 (defmethod (setf slot-value-using-class)
203 (new-value (class std-class)
205 (slotd standard-effective-slot-definition))
206 (check-obsolete-instance object)
207 (let ((location (slot-definition-location slotd)))
210 (cond ((std-instance-p object)
211 (setf (clos-slots-ref (std-instance-slots object) location)
213 ((fsc-instance-p object)
214 (setf (clos-slots-ref (fsc-instance-slots object) location)
216 (t (error "unrecognized instance type"))))
218 (setf (cdr location) new-value))
220 (error "~@<The slot ~S has neither :INSTANCE nor :CLASS allocation, ~
221 so it can't be written by the default ~S method.~:@>"
222 slotd '(setf slot-value-using-class))))))
224 (defmethod slot-boundp-using-class
227 (slotd standard-effective-slot-definition))
228 (check-obsolete-instance object)
229 (let* ((location (slot-definition-location slotd))
230 (value (typecase location
232 (cond ((std-instance-p object)
233 (clos-slots-ref (std-instance-slots object)
235 ((fsc-instance-p object)
236 (clos-slots-ref (fsc-instance-slots object)
238 (t (error "unrecognized instance type"))))
242 (error "~@<The slot ~S has neither :INSTANCE nor :CLASS ~
243 allocation, so it can't be read by the default ~S ~
245 slotd 'slot-boundp-using-class)))))
246 (not (eq value +slot-unbound+))))
248 (defmethod slot-makunbound-using-class
251 (slotd standard-effective-slot-definition))
252 (check-obsolete-instance object)
253 (let ((location (slot-definition-location slotd)))
256 (cond ((std-instance-p object)
257 (setf (clos-slots-ref (std-instance-slots object) location)
259 ((fsc-instance-p object)
260 (setf (clos-slots-ref (fsc-instance-slots object) location)
262 (t (error "unrecognized instance type"))))
264 (setf (cdr location) +slot-unbound+))
266 (error "~@<The slot ~S has neither :INSTANCE nor :CLASS allocation, ~
267 so it can't be written by the default ~S method.~@:>"
268 slotd 'slot-makunbound-using-class))))
271 (defmethod slot-value-using-class
272 ((class condition-class)
274 (slotd condition-effective-slot-definition))
275 (let ((fun (slot-definition-reader-function slotd)))
276 (declare (type function fun))
277 (funcall fun object)))
279 (defmethod (setf slot-value-using-class)
281 (class condition-class)
283 (slotd condition-effective-slot-definition))
284 (let ((fun (slot-definition-writer-function slotd)))
285 (declare (type function fun))
286 (funcall fun new-value object)))
288 (defmethod slot-boundp-using-class
289 ((class condition-class)
291 (slotd condition-effective-slot-definition))
292 (let ((fun (slot-definition-boundp-function slotd)))
293 (declare (type function fun))
294 (funcall fun object)))
296 (defmethod slot-makunbound-using-class ((class condition-class) object slot)
297 (error "attempt to unbind slot ~S in condition object ~S."
300 (defmethod slot-value-using-class
301 ((class structure-class)
302 (object structure-object)
303 (slotd structure-effective-slot-definition))
304 (let* ((function (slot-definition-internal-reader-function slotd))
305 (value (funcall function object)))
306 (declare (type function function))
307 (if (eq value +slot-unbound+)
308 (slot-unbound class object (slot-definition-name slotd))
311 (defmethod (setf slot-value-using-class)
312 (new-value (class structure-class)
313 (object structure-object)
314 (slotd structure-effective-slot-definition))
315 (let ((function (slot-definition-internal-writer-function slotd)))
316 (declare (type function function))
317 (funcall function new-value object)))
319 (defmethod slot-boundp-using-class
320 ((class structure-class)
321 (object structure-object)
322 (slotd structure-effective-slot-definition))
325 (defmethod slot-makunbound-using-class
326 ((class structure-class)
327 (object structure-object)
328 (slotd structure-effective-slot-definition))
329 (error "Structure slots can't be unbound."))
331 (defmethod slot-missing
332 ((class t) instance slot-name operation &optional new-value)
333 (error "~@<When attempting to ~A, the slot ~S is missing from the ~
336 (slot-value "read the slot's value (slot-value)")
338 "set the slot's value to ~S (SETF of SLOT-VALUE)"
340 (slot-boundp "test to see whether slot is bound (SLOT-BOUNDP)")
341 (slot-makunbound "make the slot unbound (SLOT-MAKUNBOUND)"))
345 (defmethod slot-unbound ((class t) instance slot-name)
346 (error 'unbound-slot :name slot-name :instance instance))
348 (defun slot-unbound-internal (instance position)
349 (slot-unbound (class-of instance) instance
353 (wrapper-instance-slots-layout (wrapper-of instance))))
357 (defmethod allocate-instance ((class standard-class) &rest initargs)
358 (declare (ignore initargs))
359 (unless (class-finalized-p class) (finalize-inheritance class))
360 (allocate-standard-instance (class-wrapper class)))
362 (defmethod allocate-instance ((class structure-class) &rest initargs)
363 (declare (ignore initargs))
364 (let ((constructor (class-defstruct-constructor class)))
366 (funcall constructor)
367 (error "can't allocate an instance of class ~S" (class-name class)))))
369 (defmethod allocate-instance ((class condition-class) &rest initargs)
370 (declare (ignore initargs))
371 (make-condition (class-name class)))