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 three 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 %swap-wrappers-and-slots (i1 i2)
52 (cond ((std-instance-p i1)
53 (let ((w1 (std-instance-wrapper i1))
54 (s1 (std-instance-slots i1)))
55 (setf (std-instance-wrapper i1) (std-instance-wrapper i2))
56 (setf (std-instance-slots i1) (std-instance-slots i2))
57 (setf (std-instance-wrapper i2) w1)
58 (setf (std-instance-slots i2) s1)))
60 (let ((w1 (fsc-instance-wrapper i1))
61 (s1 (fsc-instance-slots i1)))
62 (setf (fsc-instance-wrapper i1) (fsc-instance-wrapper i2))
63 (setf (fsc-instance-slots i1) (fsc-instance-slots i2))
64 (setf (fsc-instance-wrapper i2) w1)
65 (setf (fsc-instance-slots i2) s1)))
67 (error "unrecognized instance type"))))
69 ;;;; STANDARD-INSTANCE-ACCESS
71 (declaim (inline standard-instance-access
72 (setf standard-instance-access)
73 (cas stadard-instance-access)
74 funcallable-standard-instance-access
75 (setf funcallable-standard-instance-access)
76 (cas funcallable-standard-instance-access)))
78 (defun standard-instance-access (instance location)
79 (clos-slots-ref (std-instance-slots instance) location))
81 (defun (setf standard-instance-access) (new-value instance location)
82 (setf (clos-slots-ref (std-instance-slots instance) location) new-value))
84 (defun (cas standard-instance-access) (old-value new-value instance location)
85 ;; FIXME: Maybe get rid of CLOS-SLOTS-REF entirely?
86 (cas (svref (std-instance-slots instance) location) old-value new-value))
88 (defun funcallable-standard-instance-access (instance location)
89 (clos-slots-ref (fsc-instance-slots instance) location))
91 (defun (setf funcallable-standard-instance-access) (new-value instance location)
92 (setf (clos-slots-ref (fsc-instance-slots instance) location) new-value))
94 (defun (cas funcallable-standard-instance-access) (old-value new-value instance location)
95 ;; FIXME: Maybe get rid of CLOS-SLOTS-REF entirely?
96 (cas (svref (fsc-instance-slots instance) location) old-value new-value))
98 ;;;; SLOT-VALUE, (SETF SLOT-VALUE), SLOT-BOUNDP, SLOT-MAKUNBOUND
100 (declaim (ftype (sfunction (t symbol) t) slot-value))
101 (defun slot-value (object slot-name)
102 (let* ((wrapper (valid-wrapper-of object))
103 (cell (or (find-slot-cell wrapper slot-name)
104 (return-from slot-value
105 (values (slot-missing (wrapper-class* wrapper) object slot-name
107 (location (car cell))
109 (cond ((fixnump location)
110 (if (std-instance-p object)
111 (standard-instance-access object location)
112 (funcallable-standard-instance-access object location)))
116 (return-from slot-value
117 (funcall (slot-info-reader (cdr cell)) object)))
119 (bug "Bogus slot cell in SLOT-VALUE: ~S" cell)))))
120 (if (eq +slot-unbound+ value)
121 (slot-unbound (wrapper-class* wrapper) object slot-name)
124 ;;; This is used during the PCL build, but gets replaced by a deftransform
126 (define-compiler-macro slot-value (&whole form object slot-name
128 (if (and (constantp slot-name env)
129 (interned-symbol-p (constant-form-value slot-name env)))
130 `(accessor-slot-value ,object ,slot-name)
133 (defun set-slot-value (object slot-name new-value)
134 (let* ((wrapper (valid-wrapper-of object))
135 (cell (or (find-slot-cell wrapper slot-name)
136 (return-from set-slot-value
137 (values (slot-missing (wrapper-class* wrapper) object slot-name
139 (location (car cell))
141 (typecheck (slot-info-typecheck info)))
143 (funcall typecheck new-value))
144 (cond ((fixnump location)
145 (if (std-instance-p object)
146 (setf (standard-instance-access object location) new-value)
147 (setf (funcallable-standard-instance-access object location)
150 (setf (cdr location) new-value))
152 (funcall (slot-info-writer info) new-value object))
154 (bug "Bogus slot-cell in SET-SLOT-VALUE: ~S" cell))))
157 ;;; A version of SET-SLOT-VALUE for use in safe code, where we want to
158 ;;; check types when writing to slots:
159 ;;; * Doesn't have an optimizing compiler-macro
160 ;;; * Isn't special-cased in WALK-METHOD-LAMBDA
161 (defun safe-set-slot-value (object slot-name new-value)
162 (set-slot-value object slot-name new-value))
164 ;;; This is used during the PCL build, but gets replaced by a deftransform
166 (define-compiler-macro set-slot-value (&whole form object slot-name new-value
168 (if (and (constantp slot-name env)
169 (interned-symbol-p (constant-form-value slot-name env))
170 ;; We can't use the ACCESSOR-SET-SLOT-VALUE path in safe
171 ;; code, since it'll use the global automatically generated
172 ;; accessor, which won't do typechecking. (SLOT-OBJECT
173 ;; won't have been compiled with SAFETY 3, so SAFE-P will
174 ;; be NIL in MAKE-STD-WRITER-METHOD-FUNCTION).
175 (not (safe-code-p env)))
176 `(accessor-set-slot-value ,object ,slot-name ,new-value)
179 (defun (cas slot-value) (old-value new-value object slot-name)
180 (let* ((wrapper (valid-wrapper-of object))
181 (cell (or (find-slot-cell wrapper slot-name)
182 (return-from slot-value
183 (values (slot-missing (wrapper-class* wrapper) object slot-name
184 'cas (list old-value new-value))))))
185 (location (car cell))
187 (typecheck (slot-info-typecheck info)))
189 (funcall typecheck new-value))
190 (let ((old (cond ((fixnump location)
191 (if (std-instance-p object)
192 (cas (standard-instance-access object location) old-value new-value)
193 (cas (funcallable-standard-instance-access object location)
194 old-value new-value)))
196 (cas (cdr location) old-value new-value))
198 ;; FIXME: (CAS SLOT-VALUE-USING-CLASS)...
199 (error "Cannot compare-and-swap slot ~S on: ~S" slot-name object))
201 (bug "Bogus slot-cell in (CAS SLOT-VALUE): ~S" cell)))))
202 (if (and (eq +slot-unbound+ old)
204 (slot-unbound (wrapper-class* wrapper) object slot-name)
207 (defun slot-boundp (object slot-name)
208 (let* ((wrapper (valid-wrapper-of object))
209 (cell (or (find-slot-cell wrapper slot-name)
210 (return-from slot-boundp
211 (and (slot-missing (wrapper-class* wrapper) object slot-name
214 (location (car cell))
216 (cond ((fixnump location)
217 (if (std-instance-p object)
218 (standard-instance-access object location)
219 (funcallable-standard-instance-access object location)))
223 (return-from slot-boundp
224 (funcall (slot-info-boundp (cdr cell)) object)))
226 (bug "Bogus slot cell in SLOT-VALUE: ~S" cell)))))
227 (not (eq +slot-unbound+ value))))
229 (define-compiler-macro slot-boundp (&whole form object slot-name
231 (if (and (constantp slot-name env)
232 (interned-symbol-p (constant-form-value slot-name env)))
233 `(accessor-slot-boundp ,object ,slot-name)
236 (defun slot-makunbound (object slot-name)
237 (let* ((wrapper (valid-wrapper-of object))
238 (cell (find-slot-cell wrapper slot-name))
239 (location (car cell)))
240 (cond ((fixnump location)
241 (if (std-instance-p object)
242 (setf (standard-instance-access object location) +slot-unbound+)
243 (setf (funcallable-standard-instance-access object location)
246 (setf (cdr location) +slot-unbound+))
248 (slot-missing (wrapper-class* wrapper) object slot-name 'slot-makunbound))
250 (let ((class (wrapper-class* wrapper)))
251 (slot-makunbound-using-class class object (find-slot-definition class slot-name))))
253 (bug "Bogus slot-cell in SLOT-MAKUNBOUND: ~S" cell))))
256 (defun slot-exists-p (object slot-name)
257 (let ((class (class-of object)))
258 (not (null (find-slot-definition class slot-name)))))
260 (defvar *unbound-slot-value-marker* (make-unprintable-object "unbound slot"))
262 ;;; This isn't documented, but is used within PCL in a number of print
263 ;;; object methods. (See NAMED-OBJECT-PRINT-FUNCTION.)
264 (defun slot-value-or-default (object slot-name &optional
265 (default *unbound-slot-value-marker*))
266 (if (slot-boundp object slot-name)
267 (slot-value object slot-name)
270 (defmethod slot-value-using-class ((class std-class)
271 (object standard-object)
272 (slotd standard-effective-slot-definition))
273 ;; FIXME: Do we need this? SLOT-VALUE checks for obsolete
274 ;; instances. Are users allowed to call this directly?
275 (check-obsolete-instance object)
276 (let* ((location (slot-definition-location slotd))
280 (cond ((std-instance-p object)
281 (clos-slots-ref (std-instance-slots object)
283 ((fsc-instance-p object)
284 (clos-slots-ref (fsc-instance-slots object)
286 (t (bug "unrecognized instance type in ~S"
287 'slot-value-using-class))))
291 (instance-structure-protocol-error slotd
292 'slot-value-using-class)))))
293 (if (eq value +slot-unbound+)
294 (values (slot-unbound class object (slot-definition-name slotd)))
297 (defmethod (setf slot-value-using-class)
298 (new-value (class std-class)
299 (object standard-object)
300 (slotd standard-effective-slot-definition))
301 ;; FIXME: Do we need this? SET-SLOT-VALUE checks for obsolete
302 ;; instances. Are users allowed to call this directly?
303 (check-obsolete-instance object)
304 (let* ((info (slot-definition-info slotd))
305 (location (slot-definition-location slotd))
306 (typecheck (slot-info-typecheck info))
307 (new-value (if typecheck
308 (funcall (the function typecheck) new-value)
312 (cond ((std-instance-p object)
313 (setf (clos-slots-ref (std-instance-slots object) location)
315 ((fsc-instance-p object)
316 (setf (clos-slots-ref (fsc-instance-slots object) location)
318 (t (bug "unrecognized instance type in ~S"
319 '(setf slot-value-using-class)))))
321 (setf (cdr location) new-value))
323 (instance-structure-protocol-error
324 slotd '(setf slot-value-using-class))))))
326 (defmethod slot-boundp-using-class
328 (object standard-object)
329 (slotd standard-effective-slot-definition))
330 ;; FIXME: Do we need this? SLOT-BOUNDP checks for obsolete
331 ;; instances. Are users allowed to call this directly?
332 (check-obsolete-instance object)
333 (let* ((location (slot-definition-location slotd))
337 (cond ((std-instance-p object)
338 (clos-slots-ref (std-instance-slots object)
340 ((fsc-instance-p object)
341 (clos-slots-ref (fsc-instance-slots object)
343 (t (bug "unrecognized instance type in ~S"
344 'slot-boundp-using-class))))
348 (instance-structure-protocol-error slotd
349 'slot-boundp-using-class)))))
350 (not (eq value +slot-unbound+))))
352 (defmethod slot-makunbound-using-class
354 (object standard-object)
355 (slotd standard-effective-slot-definition))
356 (check-obsolete-instance object)
357 (let ((location (slot-definition-location slotd)))
360 (cond ((std-instance-p object)
361 (setf (clos-slots-ref (std-instance-slots object) location)
363 ((fsc-instance-p object)
364 (setf (clos-slots-ref (fsc-instance-slots object) location)
366 (t (bug "unrecognized instance type in ~S"
367 'slot-makunbound-using-class))))
369 (setf (cdr location) +slot-unbound+))
371 (instance-structure-protocol-error slotd
372 'slot-makunbound-using-class))))
375 (defmethod slot-value-using-class
376 ((class condition-class)
378 (slotd condition-effective-slot-definition))
379 (let ((fun (slot-info-reader (slot-definition-info slotd))))
380 (funcall fun object)))
382 (defmethod (setf slot-value-using-class)
384 (class condition-class)
386 (slotd condition-effective-slot-definition))
387 (let ((fun (slot-info-writer (slot-definition-info slotd))))
388 (funcall fun new-value object)))
390 (defmethod slot-boundp-using-class
391 ((class condition-class)
393 (slotd condition-effective-slot-definition))
394 (let ((fun (slot-info-boundp (slot-definition-info slotd))))
395 (funcall fun object)))
397 (defmethod slot-makunbound-using-class ((class condition-class) object slot)
398 (error "attempt to unbind slot ~S in condition object ~S."
401 (defmethod slot-value-using-class
402 ((class structure-class)
403 (object structure-object)
404 (slotd structure-effective-slot-definition))
405 (let* ((function (slot-definition-internal-reader-function slotd))
406 (value (funcall function object)))
407 (declare (type function function))
408 ;; FIXME: Is this really necessary? Structure slots should surely
410 (if (eq value +slot-unbound+)
411 (values (slot-unbound class object (slot-definition-name slotd)))
414 (defmethod (setf slot-value-using-class)
415 (new-value (class structure-class)
416 (object structure-object)
417 (slotd structure-effective-slot-definition))
418 (let ((function (slot-definition-internal-writer-function slotd)))
419 (declare (type function function))
420 (funcall function new-value object)))
422 (defmethod slot-boundp-using-class
423 ((class structure-class)
424 (object structure-object)
425 (slotd structure-effective-slot-definition))
428 (defmethod slot-makunbound-using-class
429 ((class structure-class)
430 (object structure-object)
431 (slotd structure-effective-slot-definition))
432 (error "Structure slots can't be unbound."))
434 (defmethod slot-missing
435 ((class t) instance slot-name operation &optional new-value)
436 (error "~@<When attempting to ~A, the slot ~S is missing from the ~
439 (slot-value "read the slot's value (slot-value)")
441 "set the slot's value to ~S (SETF of SLOT-VALUE)"
443 (slot-boundp "test to see whether slot is bound (SLOT-BOUNDP)")
444 (slot-makunbound "make the slot unbound (SLOT-MAKUNBOUND)"))
448 (defmethod slot-unbound ((class t) instance slot-name)
450 (error 'unbound-slot :name slot-name :instance instance)
452 :report "Return a value as the slot-value."
453 :interactive read-evaluated-form
456 :report "Store and return a value as the slot-value."
457 :interactive read-evaluated-form
458 (setf (slot-value instance slot-name) v))))
460 (defun slot-unbound-internal (instance position)
467 ;; In the vast majority of cases location corresponds to the position
468 ;; in list. The only exceptions are when there are non-local slots
469 ;; before the one we want.
470 (let* ((slots (wrapper-slots (wrapper-of instance)))
471 (guess (nth position slots)))
472 (if (eql position (slot-definition-location guess))
473 (slot-definition-name guess)
474 (slot-definition-name
475 (car (member position (class-slots instance) :key #'slot-definition-location))))))
479 ;;; FIXME: AMOP says that allocate-instance imples finalize-inheritance
480 ;;; if the class is not yet finalized, but we don't seem to be taking
481 ;;; care of this for non-standard-classes.
482 (defmethod allocate-instance ((class standard-class) &rest initargs)
483 (declare (ignore initargs))
484 (unless (class-finalized-p class)
485 (finalize-inheritance class))
486 (allocate-standard-instance (class-wrapper class)))
488 (defmethod allocate-instance ((class structure-class) &rest initargs)
489 (declare (ignore initargs))
490 (let ((constructor (class-defstruct-constructor class)))
492 (funcall constructor)
493 (error "Don't know how to allocate ~S" class))))
495 (defmethod allocate-instance ((class condition-class) &rest initargs)
496 (declare (ignore initargs))
497 (allocate-condition (class-name class)))
499 (defmethod allocate-instance ((class built-in-class) &rest initargs)
500 (declare (ignore initargs))
501 (error "Cannot allocate an instance of ~S." class)) ; So sayeth AMOP
503 ;;; AMOP says that CLASS-SLOTS signals an error for unfinalized classes.
504 (defmethod class-slots :before ((class slot-class))
505 (unless (class-finalized-p class)
506 (error 'simple-reference-error
507 :format-control "~S called on ~S, which is not yet finalized."
508 :format-arguments (list 'class-slots class)
509 :references (list '(:amop :generic-function class-slots)))))