0.8.0.78:
[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 (defun slot-value (object slot-name)
84   (let* ((class (class-of object))
85          (slot-definition (find-slot-definition class slot-name)))
86     (if (null slot-definition)
87         (values (slot-missing class object slot-name 'slot-value))
88         (slot-value-using-class class object slot-definition))))
89
90 (define-compiler-macro slot-value (&whole form object slot-name)
91   (if (and (constantp slot-name)
92            (interned-symbol-p (eval slot-name)))
93       `(accessor-slot-value ,object ,slot-name)
94       form))
95
96 (defun set-slot-value (object slot-name new-value)
97   (let* ((class (class-of object))
98          (slot-definition (find-slot-definition class slot-name)))
99     (if (null slot-definition)
100         (progn (slot-missing class object slot-name 'setf new-value)
101                new-value)
102         (setf (slot-value-using-class class object slot-definition)
103               new-value))))
104
105 (define-compiler-macro set-slot-value (&whole form object slot-name new-value)
106   (if (and (constantp slot-name)
107            (interned-symbol-p (eval slot-name)))
108       `(accessor-set-slot-value ,object ,slot-name ,new-value)
109       form))
110
111 (defun slot-boundp (object slot-name)
112   (let* ((class (class-of object))
113          (slot-definition (find-slot-definition class slot-name)))
114     (if (null slot-definition)
115         (not (not (slot-missing class object slot-name 'slot-boundp)))
116         (slot-boundp-using-class class object slot-definition))))
117
118 (setf (gdefinition 'slot-boundp-normal) #'slot-boundp)
119
120 (define-compiler-macro slot-boundp (&whole form object slot-name)
121   (if (and (constantp slot-name)
122            (interned-symbol-p (eval slot-name)))
123       `(accessor-slot-boundp ,object ,slot-name)
124       form))
125
126 (defun slot-makunbound (object slot-name)
127   (let* ((class (class-of object))
128          (slot-definition (find-slot-definition class slot-name)))
129     (if (null slot-definition)
130         (slot-missing class object slot-name 'slot-makunbound)
131         (slot-makunbound-using-class class object slot-definition))
132     object))
133
134 (defun slot-exists-p (object slot-name)
135   (let ((class (class-of object)))
136     (not (null (find-slot-definition class slot-name)))))
137
138 ;;; This isn't documented, but is used within PCL in a number of print
139 ;;; object methods. (See NAMED-OBJECT-PRINT-FUNCTION.)
140 (defun slot-value-or-default (object slot-name &optional (default "unbound"))
141   (if (slot-boundp object slot-name)
142       (slot-value object slot-name)
143       default))
144 \f
145 (defun standard-instance-access (instance location)
146   (clos-slots-ref (std-instance-slots instance) location))
147
148 (defun funcallable-standard-instance-access (instance location)
149   (clos-slots-ref (fsc-instance-slots instance) location))
150
151 (defmethod slot-value-using-class ((class std-class)
152                                    (object std-object)
153                                    (slotd standard-effective-slot-definition))
154   (check-obsolete-instance object)
155   (let* ((location (slot-definition-location slotd))
156          (value (typecase location
157                   (fixnum
158                    (cond ((std-instance-p object)
159                           (clos-slots-ref (std-instance-slots object)
160                                           location))
161                          ((fsc-instance-p object)
162                           (clos-slots-ref (fsc-instance-slots object)
163                                           location))
164                          (t (error "unrecognized instance type"))))
165                   (cons
166                    (cdr location))
167                   (t
168                    (error "~@<The slot ~S has neither :INSTANCE nor :CLASS ~
169                            allocation, so it can't be read by the default ~
170                            ~S method.~@:>"
171                           slotd 'slot-value-using-class)))))
172     (if (eq value +slot-unbound+)
173         (values (slot-unbound class object (slot-definition-name slotd)))
174         value)))
175
176 (defmethod (setf slot-value-using-class)
177            (new-value (class std-class)
178                       (object std-object)
179                       (slotd standard-effective-slot-definition))
180   (check-obsolete-instance object)
181   (let ((location (slot-definition-location slotd)))
182     (typecase location
183       (fixnum
184        (cond ((std-instance-p object)
185               (setf (clos-slots-ref (std-instance-slots object) location)
186                     new-value))
187              ((fsc-instance-p object)
188               (setf (clos-slots-ref (fsc-instance-slots object) location)
189                     new-value))
190              (t (error "unrecognized instance type"))))
191       (cons
192        (setf (cdr location) new-value))
193       (t
194        (error "~@<The slot ~S has neither :INSTANCE nor :CLASS allocation, ~
195                    so it can't be written by the default ~S method.~:@>"
196               slotd '(setf slot-value-using-class))))))
197
198 (defmethod slot-boundp-using-class
199            ((class std-class)
200             (object std-object)
201             (slotd standard-effective-slot-definition))
202   (check-obsolete-instance object)
203   (let* ((location (slot-definition-location slotd))
204          (value (typecase location
205                   (fixnum
206                    (cond ((std-instance-p object)
207                           (clos-slots-ref (std-instance-slots object)
208                                           location))
209                          ((fsc-instance-p object)
210                           (clos-slots-ref (fsc-instance-slots object)
211                                           location))
212                          (t (error "unrecognized instance type"))))
213                   (cons
214                    (cdr location))
215                   (t
216                    (error "~@<The slot ~S has neither :INSTANCE nor :CLASS ~
217                            allocation, so it can't be read by the default ~S ~
218                            method.~@:>"
219                           slotd 'slot-boundp-using-class)))))
220     (not (eq value +slot-unbound+))))
221
222 (defmethod slot-makunbound-using-class
223            ((class std-class)
224             (object std-object)
225             (slotd standard-effective-slot-definition))
226   (check-obsolete-instance object)
227   (let ((location (slot-definition-location slotd)))
228     (typecase location
229       (fixnum
230        (cond ((std-instance-p object)
231               (setf (clos-slots-ref (std-instance-slots object) location)
232                     +slot-unbound+))
233              ((fsc-instance-p object)
234               (setf (clos-slots-ref (fsc-instance-slots object) location)
235                     +slot-unbound+))
236              (t (error "unrecognized instance type"))))
237       (cons
238        (setf (cdr location) +slot-unbound+))
239       (t
240        (error "~@<The slot ~S has neither :INSTANCE nor :CLASS allocation, ~
241                so it can't be written by the default ~S method.~@:>"
242               slotd 'slot-makunbound-using-class))))
243   object)
244
245 (defmethod slot-value-using-class
246     ((class condition-class)
247      (object condition)
248      (slotd condition-effective-slot-definition))
249   (let ((fun (slot-definition-reader-function slotd)))
250     (declare (type function fun))
251     (funcall fun object)))
252
253 (defmethod (setf slot-value-using-class)
254     (new-value
255      (class condition-class)
256      (object condition)
257      (slotd condition-effective-slot-definition))
258   (let ((fun (slot-definition-writer-function slotd)))
259     (declare (type function fun))
260     (funcall fun new-value object)))
261
262 (defmethod slot-boundp-using-class
263     ((class condition-class)
264      (object condition)
265      (slotd condition-effective-slot-definition))
266   (let ((fun (slot-definition-boundp-function slotd)))
267     (declare (type function fun))
268     (funcall fun object)))
269
270 (defmethod slot-makunbound-using-class ((class condition-class) object slot)
271   (error "attempt to unbind slot ~S in condition object ~S."
272          slot object))
273
274 (defmethod slot-value-using-class
275     ((class structure-class)
276      (object structure-object)
277      (slotd structure-effective-slot-definition))
278   (let* ((function (slot-definition-internal-reader-function slotd))
279          (value (funcall function object)))
280     (declare (type function function))
281     (if (eq value +slot-unbound+)
282         (slot-unbound class object (slot-definition-name slotd))
283         value)))
284
285 (defmethod (setf slot-value-using-class)
286     (new-value (class structure-class)
287                (object structure-object)
288                (slotd structure-effective-slot-definition))
289   (let ((function (slot-definition-internal-writer-function slotd)))
290     (declare (type function function))
291     (funcall function new-value object)))
292
293 (defmethod slot-boundp-using-class
294            ((class structure-class)
295             (object structure-object)
296             (slotd structure-effective-slot-definition))
297   t)
298
299 (defmethod slot-makunbound-using-class
300            ((class structure-class)
301             (object structure-object)
302             (slotd structure-effective-slot-definition))
303   (error "Structure slots can't be unbound."))
304 \f
305 (defmethod slot-missing
306            ((class t) instance slot-name operation &optional new-value)
307   (error "~@<When attempting to ~A, the slot ~S is missing from the ~
308           object ~S.~@:>"
309          (ecase operation
310            (slot-value "read the slot's value (slot-value)")
311            (setf (format nil
312                          "set the slot's value to ~S (SETF of SLOT-VALUE)"
313                          new-value))
314            (slot-boundp "test to see whether slot is bound (SLOT-BOUNDP)")
315            (slot-makunbound "make the slot unbound (SLOT-MAKUNBOUND)"))
316          slot-name
317          instance))
318
319 (defmethod slot-unbound ((class t) instance slot-name)
320   (error 'unbound-slot :name slot-name :instance instance))
321
322 (defun slot-unbound-internal (instance position)
323   (values
324    (slot-unbound
325     (class-of instance)
326     instance
327     (etypecase position
328       (fixnum
329        (nth position (wrapper-instance-slots-layout (wrapper-of instance))))
330       (cons
331        (car position))))))
332 \f
333 (defmethod allocate-instance ((class standard-class) &rest initargs)
334   (declare (ignore initargs))
335   (unless (class-finalized-p class) (finalize-inheritance class))
336   (allocate-standard-instance (class-wrapper class)))
337
338 (defmethod allocate-instance ((class structure-class) &rest initargs)
339   (declare (ignore initargs))
340   (let ((constructor (class-defstruct-constructor class)))
341     (if constructor
342         (funcall constructor)
343         (error "can't allocate an instance of class ~S" (class-name class)))))
344
345 (defmethod allocate-instance ((class condition-class) &rest initargs)
346   (declare (ignore initargs))
347   (make-condition (class-name class)))