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 early-find-slot-definition (class slot-name)
79 (dolist (slot (class-slots class) nil)
80 (when (eql slot-name (slot-definition-name slot))
82 (setf (fdefinition 'find-slot-definition) #'early-find-slot-definition)
84 (declaim (ftype (sfunction (t symbol) t) slot-value))
85 (defun slot-value (object slot-name)
86 (let* ((class (class-of object))
87 (slot-definition (find-slot-definition class slot-name)))
88 (if (null slot-definition)
89 (values (slot-missing class object slot-name 'slot-value))
90 (slot-value-using-class class object slot-definition))))
92 (define-compiler-macro slot-value (&whole form object slot-name
94 (if (and (constantp slot-name env)
95 (interned-symbol-p (constant-form-value slot-name env)))
96 `(accessor-slot-value ,object ,slot-name)
99 (defun set-slot-value (object slot-name new-value)
100 (let* ((class (class-of object))
101 (slot-definition (find-slot-definition class slot-name)))
102 (if (null slot-definition)
103 (progn (slot-missing class object slot-name 'setf new-value)
105 (setf (slot-value-using-class class object slot-definition)
108 ;;; A version of SET-SLOT-VALUE for use in safe code, where we want to
109 ;;; check types when writing to slots:
110 ;;; * Doesn't have an optimizing compiler-macro
111 ;;; * Isn't special-cased in WALK-METHOD-LAMBDA
112 (defun safe-set-slot-value (object slot-name new-value)
113 (set-slot-value object slot-name new-value))
115 (define-compiler-macro set-slot-value (&whole form object slot-name new-value
117 (if (and (constantp slot-name env)
118 (interned-symbol-p (constant-form-value slot-name env))
119 ;; We can't use the ACCESSOR-SET-SLOT-VALUE path in safe
120 ;; code, since it'll use the global automatically generated
121 ;; accessor, which won't do typechecking. (SLOT-OBJECT
122 ;; won't have been compiled with SAFETY 3, so SAFE-P will
123 ;; be NIL in MAKE-STD-WRITER-METHOD-FUNCTION).
124 (not (safe-code-p env)))
125 `(accessor-set-slot-value ,object ,slot-name ,new-value)
128 (defun slot-boundp (object slot-name)
129 (let* ((class (class-of object))
130 (slot-definition (find-slot-definition class slot-name)))
131 (if (null slot-definition)
132 (not (not (slot-missing class object slot-name 'slot-boundp)))
133 (slot-boundp-using-class class object slot-definition))))
135 (setf (gdefinition 'slot-boundp-normal) #'slot-boundp)
137 (define-compiler-macro slot-boundp (&whole form object slot-name
139 (if (and (constantp slot-name env)
140 (interned-symbol-p (constant-form-value slot-name env)))
141 `(accessor-slot-boundp ,object ,slot-name)
144 (defun slot-makunbound (object slot-name)
145 (let* ((class (class-of object))
146 (slot-definition (find-slot-definition class slot-name)))
147 (if (null slot-definition)
148 (slot-missing class object slot-name 'slot-makunbound)
149 (slot-makunbound-using-class class object slot-definition))
152 (defun slot-exists-p (object slot-name)
153 (let ((class (class-of object)))
154 (not (null (find-slot-definition class slot-name)))))
156 (defvar *unbound-slot-value-marker* (make-unprintable-object "unbound slot"))
158 ;;; This isn't documented, but is used within PCL in a number of print
159 ;;; object methods. (See NAMED-OBJECT-PRINT-FUNCTION.)
160 (defun slot-value-or-default (object slot-name &optional
161 (default *unbound-slot-value-marker*))
162 (if (slot-boundp object slot-name)
163 (slot-value object slot-name)
166 (defun standard-instance-access (instance location)
167 (clos-slots-ref (std-instance-slots instance) location))
169 (defun funcallable-standard-instance-access (instance location)
170 (clos-slots-ref (fsc-instance-slots instance) location))
172 (defmethod slot-value-using-class ((class std-class)
173 (object standard-object)
174 (slotd standard-effective-slot-definition))
175 (check-obsolete-instance object)
176 (let* ((location (slot-definition-location slotd))
180 (cond ((std-instance-p object)
181 (clos-slots-ref (std-instance-slots object)
183 ((fsc-instance-p object)
184 (clos-slots-ref (fsc-instance-slots object)
186 (t (bug "unrecognized instance type in ~S"
187 'slot-value-using-class))))
191 (instance-structure-protocol-error slotd
192 'slot-value-using-class)))))
193 (if (eq value +slot-unbound+)
194 (values (slot-unbound class object (slot-definition-name slotd)))
197 (defmethod (setf slot-value-using-class)
198 (new-value (class std-class)
199 (object standard-object)
200 (slotd standard-effective-slot-definition))
201 (check-obsolete-instance object)
202 (let ((location (slot-definition-location slotd))
205 (slot-definition-type-check-function slotd))))
206 (flet ((check (new-value)
207 (when type-check-function
208 (funcall (the function type-check-function) new-value))
212 (cond ((std-instance-p object)
213 (setf (clos-slots-ref (std-instance-slots object) location)
215 ((fsc-instance-p object)
216 (setf (clos-slots-ref (fsc-instance-slots object) location)
218 (t (bug "unrecognized instance type in ~S"
219 '(setf slot-value-using-class)))))
221 (setf (cdr location) (check new-value)))
223 (instance-structure-protocol-error
224 slotd '(setf slot-value-using-class)))))))
226 (defmethod slot-boundp-using-class
228 (object standard-object)
229 (slotd standard-effective-slot-definition))
230 (check-obsolete-instance object)
231 (let* ((location (slot-definition-location slotd))
235 (cond ((std-instance-p object)
236 (clos-slots-ref (std-instance-slots object)
238 ((fsc-instance-p object)
239 (clos-slots-ref (fsc-instance-slots object)
241 (t (bug "unrecognized instance type in ~S"
242 'slot-boundp-using-class))))
246 (instance-structure-protocol-error slotd
247 'slot-boundp-using-class)))))
248 (not (eq value +slot-unbound+))))
250 (defmethod slot-makunbound-using-class
252 (object standard-object)
253 (slotd standard-effective-slot-definition))
254 (check-obsolete-instance object)
255 (let ((location (slot-definition-location slotd)))
258 (cond ((std-instance-p object)
259 (setf (clos-slots-ref (std-instance-slots object) location)
261 ((fsc-instance-p object)
262 (setf (clos-slots-ref (fsc-instance-slots object) location)
264 (t (bug "unrecognized instance type in ~S"
265 'slot-makunbound-using-class))))
267 (setf (cdr location) +slot-unbound+))
269 (instance-structure-protocol-error slotd
270 'slot-makunbound-using-class))))
273 (defmethod slot-value-using-class
274 ((class condition-class)
276 (slotd condition-effective-slot-definition))
277 (let ((fun (slot-definition-reader-function slotd)))
278 (declare (type function fun))
279 (funcall fun object)))
281 (defmethod (setf slot-value-using-class)
283 (class condition-class)
285 (slotd condition-effective-slot-definition))
286 (let ((fun (slot-definition-writer-function slotd)))
287 (declare (type function fun))
288 (funcall fun new-value object)))
290 (defmethod slot-boundp-using-class
291 ((class condition-class)
293 (slotd condition-effective-slot-definition))
294 (let ((fun (slot-definition-boundp-function slotd)))
295 (declare (type function fun))
296 (funcall fun object)))
298 (defmethod slot-makunbound-using-class ((class condition-class) object slot)
299 (error "attempt to unbind slot ~S in condition object ~S."
302 (defmethod slot-value-using-class
303 ((class structure-class)
304 (object structure-object)
305 (slotd structure-effective-slot-definition))
306 (let* ((function (slot-definition-internal-reader-function slotd))
307 (value (funcall function object)))
308 (declare (type function function))
309 (if (eq value +slot-unbound+)
310 (values (slot-unbound class object (slot-definition-name slotd)))
313 (defmethod (setf slot-value-using-class)
314 (new-value (class structure-class)
315 (object structure-object)
316 (slotd structure-effective-slot-definition))
317 (let ((function (slot-definition-internal-writer-function slotd)))
318 (declare (type function function))
319 (funcall function new-value object)))
321 (defmethod slot-boundp-using-class
322 ((class structure-class)
323 (object structure-object)
324 (slotd structure-effective-slot-definition))
327 (defmethod slot-makunbound-using-class
328 ((class structure-class)
329 (object structure-object)
330 (slotd structure-effective-slot-definition))
331 (error "Structure slots can't be unbound."))
333 (defmethod slot-missing
334 ((class t) instance slot-name operation &optional new-value)
335 (error "~@<When attempting to ~A, the slot ~S is missing from the ~
338 (slot-value "read the slot's value (slot-value)")
340 "set the slot's value to ~S (SETF of SLOT-VALUE)"
342 (slot-boundp "test to see whether slot is bound (SLOT-BOUNDP)")
343 (slot-makunbound "make the slot unbound (SLOT-MAKUNBOUND)"))
347 (defmethod slot-unbound ((class t) instance slot-name)
349 (error 'unbound-slot :name slot-name :instance instance)
351 :report "Return a value as the slot-value."
352 :interactive read-evaluated-form
355 :report "Store and return a value as the slot-value."
356 :interactive read-evaluated-form
357 (setf (slot-value instance slot-name) v))))
359 (defun slot-unbound-internal (instance position)
366 (nth position (wrapper-instance-slots-layout (wrapper-of instance))))
370 ;;; FIXME: AMOP says that allocate-instance imples finalize-inheritance
371 ;;; if the class is not yet finalized, but we don't seem to be taking
372 ;;; care of this for non-standard-classes.x
373 (defmethod allocate-instance ((class standard-class) &rest initargs)
374 (declare (ignore initargs))
375 (unless (class-finalized-p class)
376 (finalize-inheritance class))
377 (allocate-standard-instance (class-wrapper class)))
379 (defmethod allocate-instance ((class structure-class) &rest initargs)
380 (declare (ignore initargs))
381 (let ((constructor (class-defstruct-constructor class)))
383 (funcall constructor)
384 (allocate-standard-instance (class-wrapper class)))))
386 ;;; FIXME: It would be nicer to have allocate-instance return
387 ;;; uninitialized objects for conditions as well.
388 (defmethod allocate-instance ((class condition-class) &rest initargs)
389 (declare (ignore initargs))
390 (make-condition (class-name class)))
392 (defmethod allocate-instance ((class built-in-class) &rest initargs)
393 (declare (ignore initargs))
394 (error "Cannot allocate an instance of ~S." class)) ; So sayeth AMOP