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 (declaim (ftype (sfunction (t symbol) t) slot-value))
84 (defun slot-value (object slot-name)
85 (let* ((class (class-of object))
86 (slot-definition (find-slot-definition class slot-name)))
87 (if (null slot-definition)
88 (values (slot-missing class object slot-name 'slot-value))
89 (slot-value-using-class class object slot-definition))))
91 (define-compiler-macro slot-value (&whole form object slot-name
93 (if (and (constantp slot-name env)
94 (interned-symbol-p (constant-form-value slot-name env)))
95 `(accessor-slot-value ,object ,slot-name)
98 (defun set-slot-value (object slot-name new-value)
99 (let* ((class (class-of object))
100 (slot-definition (find-slot-definition class slot-name)))
101 (if (null slot-definition)
102 (progn (slot-missing class object slot-name 'setf new-value)
104 (setf (slot-value-using-class class object slot-definition)
107 ;;; A version of SET-SLOT-VALUE for use in safe code, where we want to
108 ;;; check types when writing to slots:
109 ;;; * Doesn't have an optimizing compiler-macro
110 ;;; * Isn't special-cased in WALK-METHOD-LAMBDA
111 (defun safe-set-slot-value (object slot-name new-value)
112 (set-slot-value object slot-name new-value))
114 (define-compiler-macro set-slot-value (&whole form object slot-name new-value
116 (if (and (constantp slot-name env)
117 (interned-symbol-p (constant-form-value slot-name env))
118 ;; We can't use the ACCESSOR-SET-SLOT-VALUE path in safe
119 ;; code, since it'll use the global automatically generated
120 ;; accessor, which won't do typechecking. (SLOT-OBJECT
121 ;; won't have been compiled with SAFETY 3, so SAFE-P will
122 ;; be NIL in MAKE-STD-WRITER-METHOD-FUNCTION).
123 (not (safe-code-p env)))
124 `(accessor-set-slot-value ,object ,slot-name ,new-value)
127 (defun slot-boundp (object slot-name)
128 (let* ((class (class-of object))
129 (slot-definition (find-slot-definition class slot-name)))
130 (if (null slot-definition)
131 (not (not (slot-missing class object slot-name 'slot-boundp)))
132 (slot-boundp-using-class class object slot-definition))))
134 (setf (gdefinition 'slot-boundp-normal) #'slot-boundp)
136 (define-compiler-macro slot-boundp (&whole form object slot-name
138 (if (and (constantp slot-name env)
139 (interned-symbol-p (constant-form-value slot-name env)))
140 `(accessor-slot-boundp ,object ,slot-name)
143 (defun slot-makunbound (object slot-name)
144 (let* ((class (class-of object))
145 (slot-definition (find-slot-definition class slot-name)))
146 (if (null slot-definition)
147 (slot-missing class object slot-name 'slot-makunbound)
148 (slot-makunbound-using-class class object slot-definition))
151 (defun slot-exists-p (object slot-name)
152 (let ((class (class-of object)))
153 (not (null (find-slot-definition class slot-name)))))
155 (defvar *unbound-slot-value-marker* (make-unprintable-object "unbound slot"))
157 ;;; This isn't documented, but is used within PCL in a number of print
158 ;;; object methods. (See NAMED-OBJECT-PRINT-FUNCTION.)
159 (defun slot-value-or-default (object slot-name &optional
160 (default *unbound-slot-value-marker*))
161 (if (slot-boundp object slot-name)
162 (slot-value object slot-name)
165 (defun standard-instance-access (instance location)
166 (clos-slots-ref (std-instance-slots instance) location))
168 (defun funcallable-standard-instance-access (instance location)
169 (clos-slots-ref (fsc-instance-slots instance) location))
171 (defmethod slot-value-using-class ((class std-class)
172 (object standard-object)
173 (slotd standard-effective-slot-definition))
174 (check-obsolete-instance object)
175 (let* ((location (slot-definition-location slotd))
179 (cond ((std-instance-p object)
180 (clos-slots-ref (std-instance-slots object)
182 ((fsc-instance-p object)
183 (clos-slots-ref (fsc-instance-slots object)
185 (t (bug "unrecognized instance type in ~S"
186 'slot-value-using-class))))
190 (instance-structure-protocol-error slotd
191 'slot-value-using-class)))))
192 (if (eq value +slot-unbound+)
193 (values (slot-unbound class object (slot-definition-name slotd)))
196 (defmethod (setf slot-value-using-class)
197 (new-value (class std-class)
198 (object standard-object)
199 (slotd standard-effective-slot-definition))
200 (check-obsolete-instance object)
201 (let ((location (slot-definition-location slotd))
204 (slot-definition-type-check-function slotd))))
205 (flet ((check (new-value)
206 (when type-check-function
207 (funcall (the function type-check-function) new-value))
211 (cond ((std-instance-p object)
212 (setf (clos-slots-ref (std-instance-slots object) location)
214 ((fsc-instance-p object)
215 (setf (clos-slots-ref (fsc-instance-slots object) location)
217 (t (bug "unrecognized instance type in ~S"
218 '(setf slot-value-using-class)))))
220 (setf (cdr location) (check new-value)))
222 (instance-structure-protocol-error
223 slotd '(setf slot-value-using-class)))))))
225 (defmethod slot-boundp-using-class
227 (object standard-object)
228 (slotd standard-effective-slot-definition))
229 (check-obsolete-instance object)
230 (let* ((location (slot-definition-location slotd))
234 (cond ((std-instance-p object)
235 (clos-slots-ref (std-instance-slots object)
237 ((fsc-instance-p object)
238 (clos-slots-ref (fsc-instance-slots object)
240 (t (bug "unrecognized instance type in ~S"
241 'slot-boundp-using-class))))
245 (instance-structure-protocol-error slotd
246 'slot-boundp-using-class)))))
247 (not (eq value +slot-unbound+))))
249 (defmethod slot-makunbound-using-class
251 (object standard-object)
252 (slotd standard-effective-slot-definition))
253 (check-obsolete-instance object)
254 (let ((location (slot-definition-location slotd)))
257 (cond ((std-instance-p object)
258 (setf (clos-slots-ref (std-instance-slots object) location)
260 ((fsc-instance-p object)
261 (setf (clos-slots-ref (fsc-instance-slots object) location)
263 (t (bug "unrecognized instance type in ~S"
264 'slot-makunbound-using-class))))
266 (setf (cdr location) +slot-unbound+))
268 (instance-structure-protocol-error slotd
269 'slot-makunbound-using-class))))
272 (defmethod slot-value-using-class
273 ((class condition-class)
275 (slotd condition-effective-slot-definition))
276 (let ((fun (slot-definition-reader-function slotd)))
277 (declare (type function fun))
278 (funcall fun object)))
280 (defmethod (setf slot-value-using-class)
282 (class condition-class)
284 (slotd condition-effective-slot-definition))
285 (let ((fun (slot-definition-writer-function slotd)))
286 (declare (type function fun))
287 (funcall fun new-value object)))
289 (defmethod slot-boundp-using-class
290 ((class condition-class)
292 (slotd condition-effective-slot-definition))
293 (let ((fun (slot-definition-boundp-function slotd)))
294 (declare (type function fun))
295 (funcall fun object)))
297 (defmethod slot-makunbound-using-class ((class condition-class) object slot)
298 (error "attempt to unbind slot ~S in condition object ~S."
301 (defmethod slot-value-using-class
302 ((class structure-class)
303 (object structure-object)
304 (slotd structure-effective-slot-definition))
305 (let* ((function (slot-definition-internal-reader-function slotd))
306 (value (funcall function object)))
307 (declare (type function function))
308 (if (eq value +slot-unbound+)
309 (values (slot-unbound class object (slot-definition-name slotd)))
312 (defmethod (setf slot-value-using-class)
313 (new-value (class structure-class)
314 (object structure-object)
315 (slotd structure-effective-slot-definition))
316 (let ((function (slot-definition-internal-writer-function slotd)))
317 (declare (type function function))
318 (funcall function new-value object)))
320 (defmethod slot-boundp-using-class
321 ((class structure-class)
322 (object structure-object)
323 (slotd structure-effective-slot-definition))
326 (defmethod slot-makunbound-using-class
327 ((class structure-class)
328 (object structure-object)
329 (slotd structure-effective-slot-definition))
330 (error "Structure slots can't be unbound."))
332 (defmethod slot-missing
333 ((class t) instance slot-name operation &optional new-value)
334 (error "~@<When attempting to ~A, the slot ~S is missing from the ~
337 (slot-value "read the slot's value (slot-value)")
339 "set the slot's value to ~S (SETF of SLOT-VALUE)"
341 (slot-boundp "test to see whether slot is bound (SLOT-BOUNDP)")
342 (slot-makunbound "make the slot unbound (SLOT-MAKUNBOUND)"))
346 (defmethod slot-unbound ((class t) instance slot-name)
348 (error 'unbound-slot :name slot-name :instance instance)
350 :report "Return a value as the slot-value."
351 :interactive read-evaluated-form
354 :report "Store and return a value as the slot-value."
355 :interactive read-evaluated-form
356 (setf (slot-value instance slot-name) v))))
358 (defun slot-unbound-internal (instance position)
365 (nth position (wrapper-instance-slots-layout (wrapper-of instance))))
369 ;;; FIXME: AMOP says that allocate-instance imples finalize-inheritance
370 ;;; if the class is not yet finalized, but we don't seem to be taking
371 ;;; care of this for non-standard-classes.x
372 (defmethod allocate-instance ((class standard-class) &rest initargs)
373 (declare (ignore initargs))
374 (unless (class-finalized-p class)
375 (finalize-inheritance class))
376 (allocate-standard-instance (class-wrapper class)))
378 (defmethod allocate-instance ((class structure-class) &rest initargs)
379 (declare (ignore initargs))
380 (let ((constructor (class-defstruct-constructor class)))
382 (funcall constructor)
383 (allocate-standard-instance (class-wrapper class)))))
385 ;;; FIXME: It would be nicer to have allocate-instance return
386 ;;; uninitialized objects for conditions as well.
387 (defmethod allocate-instance ((class condition-class) &rest initargs)
388 (declare (ignore initargs))
389 (make-condition (class-name class)))
391 (defmethod allocate-instance ((class built-in-class) &rest initargs)
392 (declare (ignore initargs))
393 (error "Cannot allocate an instance of ~S." class)) ; So sayeth AMOP