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 ;;;; STANDARD-INSTANCE-ACCESS
80 (declaim (inline standard-instance-access (setf standard-instance-access)
81 funcallable-standard-instance-access
82 (setf funcallable-standard-instance-access)))
84 (defun standard-instance-access (instance location)
85 (clos-slots-ref (std-instance-slots instance) location))
87 (defun (setf standard-instance-access) (new-value instance location)
88 (setf (clos-slots-ref (std-instance-slots instance) location) new-value))
90 (defun funcallable-standard-instance-access (instance location)
91 (clos-slots-ref (fsc-instance-slots instance) location))
93 (defun (setf funcallable-standard-instance-access) (new-value instance location)
94 (setf (clos-slots-ref (fsc-instance-slots instance) location) new-value))
96 ;;;; SLOT-VALUE, (SETF SLOT-VALUE), SLOT-BOUNDP, SLOT-MAKUNBOUND
98 (declaim (ftype (sfunction (t symbol) t) slot-value))
99 (defun slot-value (object slot-name)
100 (let* ((wrapper (valid-wrapper-of object))
101 (cell (find-slot-cell wrapper slot-name))
102 (location (car cell))
104 (cond ((fixnump location)
105 (if (std-instance-p object)
106 (standard-instance-access object location)
107 (funcallable-standard-instance-access object location)))
111 (return-from slot-value
112 (values (slot-missing (wrapper-class* wrapper) object slot-name
115 (return-from slot-value
116 (slot-value-using-class (wrapper-class* wrapper) object (cddr cell))))
118 (bug "Bogus slot cell in SLOT-VALUE: ~S" cell)))))
119 (if (eq +slot-unbound+ value)
120 (slot-unbound (wrapper-class* wrapper) object slot-name)
123 (define-compiler-macro slot-value (&whole form object slot-name
125 (if (and (constantp slot-name env)
126 (interned-symbol-p (constant-form-value slot-name env)))
127 `(accessor-slot-value ,object ,slot-name)
130 (defun set-slot-value (object slot-name new-value)
131 (let* ((wrapper (valid-wrapper-of object))
132 (cell (find-slot-cell wrapper slot-name))
133 (location (car cell))
134 (type-check-function (cadr cell)))
135 (when type-check-function
136 (funcall (the function type-check-function) new-value))
137 (cond ((fixnump location)
138 (if (std-instance-p object)
139 (setf (standard-instance-access object location) new-value)
140 (setf (funcallable-standard-instance-access object location)
143 (setf (cdr location) new-value))
145 (slot-missing (wrapper-class* wrapper) object slot-name 'setf new-value))
147 (setf (slot-value-using-class (wrapper-class* wrapper) object (cddr cell))
150 (bug "Bogus slot-cell in SET-SLOT-VALUE: ~S" cell))))
153 ;;; A version of SET-SLOT-VALUE for use in safe code, where we want to
154 ;;; check types when writing to slots:
155 ;;; * Doesn't have an optimizing compiler-macro
156 ;;; * Isn't special-cased in WALK-METHOD-LAMBDA
157 (defun safe-set-slot-value (object slot-name new-value)
158 (set-slot-value object slot-name new-value))
160 (define-compiler-macro set-slot-value (&whole form object slot-name new-value
162 (if (and (constantp slot-name env)
163 (interned-symbol-p (constant-form-value slot-name env))
164 ;; We can't use the ACCESSOR-SET-SLOT-VALUE path in safe
165 ;; code, since it'll use the global automatically generated
166 ;; accessor, which won't do typechecking. (SLOT-OBJECT
167 ;; won't have been compiled with SAFETY 3, so SAFE-P will
168 ;; be NIL in MAKE-STD-WRITER-METHOD-FUNCTION).
169 (not (safe-code-p env)))
170 `(accessor-set-slot-value ,object ,slot-name ,new-value)
173 (defun slot-boundp (object slot-name)
174 (let* ((wrapper (valid-wrapper-of object))
175 (cell (find-slot-cell wrapper slot-name))
176 (location (car cell))
178 (cond ((fixnump location)
179 (if (std-instance-p object)
180 (standard-instance-access object location)
181 (funcallable-standard-instance-access object location)))
185 (return-from slot-boundp
186 (and (slot-missing (wrapper-class* wrapper) object slot-name
190 (return-from slot-boundp
191 (slot-boundp-using-class (wrapper-class* wrapper) object (cddr cell))))
193 (bug "Bogus slot cell in SLOT-VALUE: ~S" cell)))))
194 (not (eq +slot-unbound+ value))))
196 (define-compiler-macro slot-boundp (&whole form object slot-name
198 (if (and (constantp slot-name env)
199 (interned-symbol-p (constant-form-value slot-name env)))
200 `(accessor-slot-boundp ,object ,slot-name)
203 (defun slot-makunbound (object slot-name)
204 (let* ((wrapper (valid-wrapper-of object))
205 (cell (find-slot-cell wrapper slot-name))
206 (location (car cell)))
207 (cond ((fixnump location)
208 (if (std-instance-p object)
209 (setf (standard-instance-access object location) +slot-unbound+)
210 (setf (funcallable-standard-instance-access object location)
213 (setf (cdr location) +slot-unbound+))
215 (slot-missing (wrapper-class* wrapper) object slot-name 'slot-makunbound))
217 (slot-makunbound-using-class (wrapper-class* wrapper) object (cddr cell)))
219 (bug "Bogus slot-cell in SLOT-MAKUNBOUND: ~S" cell))))
222 (defun slot-exists-p (object slot-name)
223 (let ((class (class-of object)))
224 (not (null (find-slot-definition class slot-name)))))
226 (defvar *unbound-slot-value-marker* (make-unprintable-object "unbound slot"))
228 ;;; This isn't documented, but is used within PCL in a number of print
229 ;;; object methods. (See NAMED-OBJECT-PRINT-FUNCTION.)
230 (defun slot-value-or-default (object slot-name &optional
231 (default *unbound-slot-value-marker*))
232 (if (slot-boundp object slot-name)
233 (slot-value object slot-name)
236 (defmethod slot-value-using-class ((class std-class)
237 (object standard-object)
238 (slotd standard-effective-slot-definition))
239 ;; FIXME: Do we need this? SLOT-VALUE checks for obsolete
240 ;; instances. Are users allowed to call this directly?
241 (check-obsolete-instance object)
242 (let* ((location (slot-definition-location slotd))
246 (cond ((std-instance-p object)
247 (clos-slots-ref (std-instance-slots object)
249 ((fsc-instance-p object)
250 (clos-slots-ref (fsc-instance-slots object)
252 (t (bug "unrecognized instance type in ~S"
253 'slot-value-using-class))))
257 (instance-structure-protocol-error slotd
258 'slot-value-using-class)))))
259 (if (eq value +slot-unbound+)
260 (values (slot-unbound class object (slot-definition-name slotd)))
263 (defmethod (setf slot-value-using-class)
264 (new-value (class std-class)
265 (object standard-object)
266 (slotd standard-effective-slot-definition))
267 ;; FIXME: Do we need this? SET-SLOT-VALUE checks for obsolete
268 ;; instances. Are users allowed to call this directly?
269 (check-obsolete-instance object)
270 (let ((location (slot-definition-location slotd))
273 (slot-definition-type-check-function slotd))))
274 (flet ((check (new-value)
275 (when type-check-function
276 (funcall (the function type-check-function) new-value))
280 (cond ((std-instance-p object)
281 (setf (clos-slots-ref (std-instance-slots object) location)
283 ((fsc-instance-p object)
284 (setf (clos-slots-ref (fsc-instance-slots object) location)
286 (t (bug "unrecognized instance type in ~S"
287 '(setf slot-value-using-class)))))
289 (setf (cdr location) (check new-value)))
291 (instance-structure-protocol-error
292 slotd '(setf slot-value-using-class)))))))
294 (defmethod slot-boundp-using-class
296 (object standard-object)
297 (slotd standard-effective-slot-definition))
298 ;; FIXME: Do we need this? SLOT-BOUNDP checks for obsolete
299 ;; instances. Are users allowed to call this directly?
300 (check-obsolete-instance object)
301 (let* ((location (slot-definition-location slotd))
305 (cond ((std-instance-p object)
306 (clos-slots-ref (std-instance-slots object)
308 ((fsc-instance-p object)
309 (clos-slots-ref (fsc-instance-slots object)
311 (t (bug "unrecognized instance type in ~S"
312 'slot-boundp-using-class))))
316 (instance-structure-protocol-error slotd
317 'slot-boundp-using-class)))))
318 (not (eq value +slot-unbound+))))
320 (defmethod slot-makunbound-using-class
322 (object standard-object)
323 (slotd standard-effective-slot-definition))
324 (check-obsolete-instance object)
325 (let ((location (slot-definition-location slotd)))
328 (cond ((std-instance-p object)
329 (setf (clos-slots-ref (std-instance-slots object) location)
331 ((fsc-instance-p object)
332 (setf (clos-slots-ref (fsc-instance-slots object) location)
334 (t (bug "unrecognized instance type in ~S"
335 'slot-makunbound-using-class))))
337 (setf (cdr location) +slot-unbound+))
339 (instance-structure-protocol-error slotd
340 'slot-makunbound-using-class))))
343 (defmethod slot-value-using-class
344 ((class condition-class)
346 (slotd condition-effective-slot-definition))
347 (let ((fun (slot-definition-reader-function slotd)))
348 (declare (type function fun))
349 (funcall fun object)))
351 (defmethod (setf slot-value-using-class)
353 (class condition-class)
355 (slotd condition-effective-slot-definition))
356 (let ((fun (slot-definition-writer-function slotd)))
357 (declare (type function fun))
358 (funcall fun new-value object)))
360 (defmethod slot-boundp-using-class
361 ((class condition-class)
363 (slotd condition-effective-slot-definition))
364 (let ((fun (slot-definition-boundp-function slotd)))
365 (declare (type function fun))
366 (funcall fun object)))
368 (defmethod slot-makunbound-using-class ((class condition-class) object slot)
369 (error "attempt to unbind slot ~S in condition object ~S."
372 (defmethod slot-value-using-class
373 ((class structure-class)
374 (object structure-object)
375 (slotd structure-effective-slot-definition))
376 (let* ((function (slot-definition-internal-reader-function slotd))
377 (value (funcall function object)))
378 (declare (type function function))
379 ;; FIXME: Is this really necessary? Structure slots should surely
381 (if (eq value +slot-unbound+)
382 (values (slot-unbound class object (slot-definition-name slotd)))
385 (defmethod (setf slot-value-using-class)
386 (new-value (class structure-class)
387 (object structure-object)
388 (slotd structure-effective-slot-definition))
389 (let ((function (slot-definition-internal-writer-function slotd)))
390 (declare (type function function))
391 (funcall function new-value object)))
393 (defmethod slot-boundp-using-class
394 ((class structure-class)
395 (object structure-object)
396 (slotd structure-effective-slot-definition))
399 (defmethod slot-makunbound-using-class
400 ((class structure-class)
401 (object structure-object)
402 (slotd structure-effective-slot-definition))
403 (error "Structure slots can't be unbound."))
405 (defmethod slot-missing
406 ((class t) instance slot-name operation &optional new-value)
407 (error "~@<When attempting to ~A, the slot ~S is missing from the ~
410 (slot-value "read the slot's value (slot-value)")
412 "set the slot's value to ~S (SETF of SLOT-VALUE)"
414 (slot-boundp "test to see whether slot is bound (SLOT-BOUNDP)")
415 (slot-makunbound "make the slot unbound (SLOT-MAKUNBOUND)"))
419 (defmethod slot-unbound ((class t) instance slot-name)
421 (error 'unbound-slot :name slot-name :instance instance)
423 :report "Return a value as the slot-value."
424 :interactive read-evaluated-form
427 :report "Store and return a value as the slot-value."
428 :interactive read-evaluated-form
429 (setf (slot-value instance slot-name) v))))
431 (defun slot-unbound-internal (instance position)
438 (nth position (wrapper-instance-slots-layout (wrapper-of instance))))
442 ;;; FIXME: AMOP says that allocate-instance imples finalize-inheritance
443 ;;; if the class is not yet finalized, but we don't seem to be taking
444 ;;; care of this for non-standard-classes.x
445 (defmethod allocate-instance ((class standard-class) &rest initargs)
446 (declare (ignore initargs))
447 (unless (class-finalized-p class)
448 (finalize-inheritance class))
449 (allocate-standard-instance (class-wrapper class)))
451 (defmethod allocate-instance ((class structure-class) &rest initargs)
452 (declare (ignore initargs))
453 (let ((constructor (class-defstruct-constructor class)))
455 (funcall constructor)
456 (allocate-standard-instance (class-wrapper class)))))
458 ;;; FIXME: It would be nicer to have allocate-instance return
459 ;;; uninitialized objects for conditions as well.
460 (defmethod allocate-instance ((class condition-class) &rest initargs)
461 (declare (ignore initargs))
462 (make-condition (class-name class)))
464 (defmethod allocate-instance ((class built-in-class) &rest initargs)
465 (declare (ignore initargs))
466 (error "Cannot allocate an instance of ~S." class)) ; So sayeth AMOP
468 ;;; AMOP says that CLASS-SLOTS signals an error for unfinalized classes.
469 (defmethod class-slots :before ((class slot-class))
470 (unless (class-finalized-p class)
471 (error 'simple-reference-error
472 :format-control "~S called on ~S, which is not yet finalized."
473 :format-arguments (list 'class-slots class)
474 :references (list '(:amop :generic-function class-slots)))))