1b1326be3792057048474ac7b365d118987a8e77
[sbcl.git] / src / pcl / slots.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3
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
8 ;;;; information.
9
10 ;;;; copyright information from original PCL sources:
11 ;;;;
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
14 ;;;;
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
18 ;;;; control laws.
19 ;;;;
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
22 ;;;; specification.
23
24 (in-package "SB-PCL")
25 \f
26 ;;;; ANSI CL condition for unbound slots
27
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)))))
34
35 (defmethod wrapper-fetcher ((class standard-class))
36   'std-instance-wrapper)
37
38 (defmethod slots-fetcher ((class standard-class))
39   'std-instance-slots)
40
41 (defmethod raw-instance-allocator ((class standard-class))
42   'allocate-standard-instance)
43
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.
46 ;;;
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.
50
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))
56         (t
57          (error "unrecognized instance type"))))
58
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)))
68          ((fsc-instance-p i1)
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)))
75          (t
76           (error "unrecognized instance type")))))
77 \f
78 (defun find-slot-definition (class slot-name)
79   (dolist (slot (class-slots class) nil)
80     (when (eql slot-name (slot-definition-name slot))
81       (return slot))))
82
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))))
90
91 (define-compiler-macro slot-value (&whole form object slot-name)
92   (if (and (constantp slot-name)
93            (interned-symbol-p (constant-form-value slot-name)))
94       `(accessor-slot-value ,object ,slot-name)
95       form))
96
97 (defun set-slot-value (object slot-name new-value)
98   (let* ((class (class-of object))
99          (slot-definition (find-slot-definition class slot-name)))
100     (if (null slot-definition)
101         (progn (slot-missing class object slot-name 'setf new-value)
102                new-value)
103         (setf (slot-value-using-class class object slot-definition)
104               new-value))))
105
106 (define-compiler-macro set-slot-value (&whole form object slot-name new-value)
107   (if (and (constantp slot-name)
108            (interned-symbol-p (constant-form-value slot-name)))
109       `(accessor-set-slot-value ,object ,slot-name ,new-value)
110       form))
111
112 (defun slot-boundp (object slot-name)
113   (let* ((class (class-of object))
114          (slot-definition (find-slot-definition class slot-name)))
115     (if (null slot-definition)
116         (not (not (slot-missing class object slot-name 'slot-boundp)))
117         (slot-boundp-using-class class object slot-definition))))
118
119 (setf (gdefinition 'slot-boundp-normal) #'slot-boundp)
120
121 (define-compiler-macro slot-boundp (&whole form object slot-name)
122   (if (and (constantp slot-name)
123            (interned-symbol-p (constant-form-value slot-name)))
124       `(accessor-slot-boundp ,object ,slot-name)
125       form))
126
127 (defun slot-makunbound (object slot-name)
128   (let* ((class (class-of object))
129          (slot-definition (find-slot-definition class slot-name)))
130     (if (null slot-definition)
131         (slot-missing class object slot-name 'slot-makunbound)
132         (slot-makunbound-using-class class object slot-definition))
133     object))
134
135 (defun slot-exists-p (object slot-name)
136   (let ((class (class-of object)))
137     (not (null (find-slot-definition class slot-name)))))
138
139 (defvar *unbound-slot-value-marker* (make-unprintable-object "unbound slot"))
140
141 ;;; This isn't documented, but is used within PCL in a number of print
142 ;;; object methods. (See NAMED-OBJECT-PRINT-FUNCTION.)
143 (defun slot-value-or-default (object slot-name &optional
144                               (default *unbound-slot-value-marker*))
145   (if (slot-boundp object slot-name)
146       (slot-value object slot-name)
147       default))
148 \f
149 (defun standard-instance-access (instance location)
150   (clos-slots-ref (std-instance-slots instance) location))
151
152 (defun funcallable-standard-instance-access (instance location)
153   (clos-slots-ref (fsc-instance-slots instance) location))
154
155 (defmethod slot-value-using-class ((class std-class)
156                                    (object standard-object)
157                                    (slotd standard-effective-slot-definition))
158   (check-obsolete-instance object)
159   (let* ((location (slot-definition-location slotd))
160          (value
161           (typecase location
162             (fixnum
163              (cond ((std-instance-p object)
164                     (clos-slots-ref (std-instance-slots object)
165                                     location))
166                    ((fsc-instance-p object)
167                     (clos-slots-ref (fsc-instance-slots object)
168                                     location))
169                    (t (bug "unrecognized instance type in ~S"
170                            'slot-value-using-class))))
171             (cons
172              (cdr location))
173             (t
174              (instance-structure-protocol-error slotd
175                                                 'slot-value-using-class)))))
176     (if (eq value +slot-unbound+)
177         (values (slot-unbound class object (slot-definition-name slotd)))
178         value)))
179
180 (defmethod (setf slot-value-using-class)
181            (new-value (class std-class)
182                       (object standard-object)
183                       (slotd standard-effective-slot-definition))
184   (check-obsolete-instance object)
185   (let ((location (slot-definition-location slotd)))
186     (typecase location
187       (fixnum
188        (cond ((std-instance-p object)
189               (setf (clos-slots-ref (std-instance-slots object) location)
190                     new-value))
191              ((fsc-instance-p object)
192               (setf (clos-slots-ref (fsc-instance-slots object) location)
193                     new-value))
194              (t (bug "unrecognized instance type in ~S"
195                      '(setf slot-value-using-class)))))
196       (cons
197        (setf (cdr location) new-value))
198       (t
199        (instance-structure-protocol-error slotd
200                                           '(setf slot-value-using-class))))))
201
202 (defmethod slot-boundp-using-class
203            ((class std-class)
204             (object standard-object)
205             (slotd standard-effective-slot-definition))
206   (check-obsolete-instance object)
207   (let* ((location (slot-definition-location slotd))
208          (value
209           (typecase location
210             (fixnum
211              (cond ((std-instance-p object)
212                           (clos-slots-ref (std-instance-slots object)
213                                           location))
214                    ((fsc-instance-p object)
215                     (clos-slots-ref (fsc-instance-slots object)
216                                     location))
217                    (t (bug "unrecognized instance type in ~S"
218                            'slot-boundp-using-class))))
219             (cons
220              (cdr location))
221             (t
222              (instance-structure-protocol-error slotd
223                                                 'slot-boundp-using-class)))))
224     (not (eq value +slot-unbound+))))
225
226 (defmethod slot-makunbound-using-class
227            ((class std-class)
228             (object standard-object)
229             (slotd standard-effective-slot-definition))
230   (check-obsolete-instance object)
231   (let ((location (slot-definition-location slotd)))
232     (typecase location
233       (fixnum
234        (cond ((std-instance-p object)
235               (setf (clos-slots-ref (std-instance-slots object) location)
236                     +slot-unbound+))
237              ((fsc-instance-p object)
238               (setf (clos-slots-ref (fsc-instance-slots object) location)
239                     +slot-unbound+))
240              (t (bug "unrecognized instance type in ~S"
241                      'slot-makunbound-using-class))))
242       (cons
243        (setf (cdr location) +slot-unbound+))
244       (t
245        (instance-structure-protocol-error slotd
246                                           'slot-makunbound-using-class))))
247   object)
248
249 (defmethod slot-value-using-class
250     ((class condition-class)
251      (object condition)
252      (slotd condition-effective-slot-definition))
253   (let ((fun (slot-definition-reader-function slotd)))
254     (declare (type function fun))
255     (funcall fun object)))
256
257 (defmethod (setf slot-value-using-class)
258     (new-value
259      (class condition-class)
260      (object condition)
261      (slotd condition-effective-slot-definition))
262   (let ((fun (slot-definition-writer-function slotd)))
263     (declare (type function fun))
264     (funcall fun new-value object)))
265
266 (defmethod slot-boundp-using-class
267     ((class condition-class)
268      (object condition)
269      (slotd condition-effective-slot-definition))
270   (let ((fun (slot-definition-boundp-function slotd)))
271     (declare (type function fun))
272     (funcall fun object)))
273
274 (defmethod slot-makunbound-using-class ((class condition-class) object slot)
275   (error "attempt to unbind slot ~S in condition object ~S."
276          slot object))
277
278 (defmethod slot-value-using-class
279     ((class structure-class)
280      (object structure-object)
281      (slotd structure-effective-slot-definition))
282   (let* ((function (slot-definition-internal-reader-function slotd))
283          (value (funcall function object)))
284     (declare (type function function))
285     (if (eq value +slot-unbound+)
286         (values (slot-unbound class object (slot-definition-name slotd)))
287         value)))
288
289 (defmethod (setf slot-value-using-class)
290     (new-value (class structure-class)
291                (object structure-object)
292                (slotd structure-effective-slot-definition))
293   (let ((function (slot-definition-internal-writer-function slotd)))
294     (declare (type function function))
295     (funcall function new-value object)))
296
297 (defmethod slot-boundp-using-class
298            ((class structure-class)
299             (object structure-object)
300             (slotd structure-effective-slot-definition))
301   t)
302
303 (defmethod slot-makunbound-using-class
304            ((class structure-class)
305             (object structure-object)
306             (slotd structure-effective-slot-definition))
307   (error "Structure slots can't be unbound."))
308 \f
309 (defmethod slot-missing
310            ((class t) instance slot-name operation &optional new-value)
311   (error "~@<When attempting to ~A, the slot ~S is missing from the ~
312           object ~S.~@:>"
313          (ecase operation
314            (slot-value "read the slot's value (slot-value)")
315            (setf (format nil
316                          "set the slot's value to ~S (SETF of SLOT-VALUE)"
317                          new-value))
318            (slot-boundp "test to see whether slot is bound (SLOT-BOUNDP)")
319            (slot-makunbound "make the slot unbound (SLOT-MAKUNBOUND)"))
320          slot-name
321          instance))
322
323 (defmethod slot-unbound ((class t) instance slot-name)
324   (restart-case
325       (error 'unbound-slot :name slot-name :instance instance)
326     (use-value (v)
327       :report "Return a value as the slot-value."
328       :interactive read-evaluated-form
329       v)
330     (store-value (v)
331       :report "Store and return a value as the slot-value."
332       :interactive read-evaluated-form
333       (setf (slot-value instance slot-name) v))))
334
335 (defun slot-unbound-internal (instance position)
336   (values
337    (slot-unbound
338     (class-of instance)
339     instance
340     (etypecase position
341       (fixnum
342        (nth position (wrapper-instance-slots-layout (wrapper-of instance))))
343       (cons
344        (car position))))))
345 \f
346 ;;; FIXME: AMOP says that allocate-instance imples finalize-inheritance
347 ;;; if the class is not yet finalized, but we don't seem to be taking
348 ;;; care of this for non-standard-classes.x
349 (defmethod allocate-instance ((class standard-class) &rest initargs)
350   (declare (ignore initargs))
351   (unless (class-finalized-p class)
352     (finalize-inheritance class))
353   (allocate-standard-instance (class-wrapper class)))
354
355 (defmethod allocate-instance ((class structure-class) &rest initargs)
356   (declare (ignore initargs))
357   (let ((constructor (class-defstruct-constructor class)))
358     (if constructor
359         (funcall constructor)
360         (allocate-standard-instance (class-wrapper class)))))
361
362 ;;; FIXME: It would be nicer to have allocate-instance return
363 ;;; uninitialized objects for conditions as well.
364 (defmethod allocate-instance ((class condition-class) &rest initargs)
365   (declare (ignore initargs))
366   (make-condition (class-name class)))
367
368 (defmethod allocate-instance ((class built-in-class) &rest initargs)
369   (declare (ignore initargs))
370   (error "Cannot allocate an instance of ~S." class)) ; So sayeth AMOP