a19070bd352cf556561c751bb2ae45275f673bad
[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    (slot :reader unbound-slot-slot :initarg :slot))
31   (:report (lambda (condition stream)
32              (format stream "The slot ~S is unbound in the object ~S."
33                      (unbound-slot-slot condition)
34                      (unbound-slot-instance condition)))))
35
36 (defmethod wrapper-fetcher ((class standard-class))
37   'std-instance-wrapper)
38
39 (defmethod slots-fetcher ((class standard-class))
40   'std-instance-slots)
41
42 (defmethod raw-instance-allocator ((class standard-class))
43   'allocate-standard-instance)
44
45 ;;; These four functions work on std-instances and fsc-instances. These are
46 ;;; instances for which it is possible to change the wrapper and the slots.
47 ;;;
48 ;;; For these kinds of instances, most specified methods from the instance
49 ;;; structure protocol are promoted to the implementation-specific class
50 ;;; std-class. Many of these methods call these four functions.
51
52 (defun set-wrapper (inst new)
53   (cond ((std-instance-p inst)
54          (setf (std-instance-wrapper inst) new))
55         ((fsc-instance-p inst)
56          (setf (fsc-instance-wrapper inst) new))
57         (t
58          (error "unrecognized instance type"))))
59
60 (defun swap-wrappers-and-slots (i1 i2)
61   (sb-sys:without-interrupts
62    (cond ((std-instance-p i1)
63           (let ((w1 (std-instance-wrapper i1))
64                 (s1 (std-instance-slots i1)))
65             (setf (std-instance-wrapper i1) (std-instance-wrapper i2))
66             (setf (std-instance-slots i1) (std-instance-slots i2))
67             (setf (std-instance-wrapper i2) w1)
68             (setf (std-instance-slots i2) s1)))
69          ((fsc-instance-p i1)
70           (let ((w1 (fsc-instance-wrapper i1))
71                 (s1 (fsc-instance-slots i1)))
72             (setf (fsc-instance-wrapper i1) (fsc-instance-wrapper i2))
73             (setf (fsc-instance-slots i1) (fsc-instance-slots i2))
74             (setf (fsc-instance-wrapper i2) w1)
75             (setf (fsc-instance-slots i2) s1)))
76          (t
77           (error "unrecognized instance type")))))
78 \f
79 (defun get-class-slot-value-1 (object wrapper slot-name)
80   (let ((entry (assoc slot-name (wrapper-class-slots wrapper))))
81     (if (null entry)
82         (slot-missing (wrapper-class wrapper) object slot-name 'slot-value)
83         (if (eq (cdr entry) +slot-unbound+)
84             (slot-unbound (wrapper-class wrapper) object slot-name)
85             (cdr entry)))))
86
87 (defun set-class-slot-value-1 (new-value object wrapper slot-name)
88   (let ((entry (assoc slot-name (wrapper-class-slots wrapper))))
89     (if (null entry)
90         (slot-missing (wrapper-class wrapper)
91                       object
92                       slot-name
93                       'setf
94                       new-value)
95         (setf (cdr entry) new-value))))
96
97 (defmethod class-slot-value ((class std-class) slot-name)
98   (let ((wrapper (class-wrapper class))
99         (prototype (class-prototype class)))
100     (get-class-slot-value-1 prototype wrapper slot-name)))
101
102 (defmethod (setf class-slot-value) (nv (class std-class) slot-name)
103   (let ((wrapper (class-wrapper class))
104         (prototype (class-prototype class)))
105     (set-class-slot-value-1 nv prototype wrapper slot-name)))
106 \f
107 (defun find-slot-definition (class slot-name)
108   (dolist (slot (class-slots class) nil)
109     (when (eql slot-name (slot-definition-name slot))
110       (return slot))))
111
112 (defun slot-value (object slot-name)
113   (let* ((class (class-of object))
114          (slot-definition (find-slot-definition class slot-name)))
115     (if (null slot-definition)
116         (slot-missing class object slot-name 'slot-value)
117         (slot-value-using-class class object slot-definition))))
118
119 (define-compiler-macro slot-value (&whole form object slot-name)
120   (if (and (constantp slot-name)
121            (interned-symbol-p (eval slot-name)))
122       `(accessor-slot-value ,object ,slot-name)
123       form))
124
125 (defun set-slot-value (object slot-name new-value)
126   (let* ((class (class-of object))
127          (slot-definition (find-slot-definition class slot-name)))
128     (if (null slot-definition)
129         (slot-missing class object slot-name 'setf new-value)
130         (setf (slot-value-using-class class object slot-definition)
131               new-value))))
132
133 (define-compiler-macro set-slot-value (&whole form object slot-name new-value)
134   (if (and (constantp slot-name)
135            (interned-symbol-p (eval slot-name)))
136       `(accessor-set-slot-value ,object ,slot-name ,new-value)
137       form))
138
139 (defun slot-boundp (object slot-name)
140   (let* ((class (class-of object))
141          (slot-definition (find-slot-definition class slot-name)))
142     (if (null slot-definition)
143         (slot-missing class object slot-name 'slot-boundp)
144         (slot-boundp-using-class class object slot-definition))))
145
146 (setf (gdefinition 'slot-boundp-normal) #'slot-boundp)
147
148 (define-compiler-macro slot-boundp (&whole form object slot-name)
149   (if (and (constantp slot-name)
150            (interned-symbol-p (eval slot-name)))
151       `(accessor-slot-boundp ,object ,slot-name)
152       form))
153
154 (defun slot-makunbound (object slot-name)
155   (let* ((class (class-of object))
156          (slot-definition (find-slot-definition class slot-name)))
157     (if (null slot-definition)
158         (slot-missing class object slot-name 'slot-makunbound)
159         (slot-makunbound-using-class class object slot-definition))))
160
161 (defun slot-exists-p (object slot-name)
162   (let ((class (class-of object)))
163     (not (null (find-slot-definition class slot-name)))))
164
165 ;;; This isn't documented, but is used within PCL in a number of print
166 ;;; object methods. (See NAMED-OBJECT-PRINT-FUNCTION.)
167 (defun slot-value-or-default (object slot-name &optional (default "unbound"))
168   (if (slot-boundp object slot-name)
169       (slot-value object slot-name)
170       default))
171 \f
172 (defun standard-instance-access (instance location)
173   (clos-slots-ref (std-instance-slots instance) location))
174
175 (defun funcallable-standard-instance-access (instance location)
176   (clos-slots-ref (fsc-instance-slots instance) location))
177
178 (defmethod slot-value-using-class ((class std-class)
179                                    (object std-object)
180                                    (slotd standard-effective-slot-definition))
181   (check-obsolete-instance object)
182   (let* ((location (slot-definition-location slotd))
183          (value (typecase location
184                   (fixnum
185                    (cond ((std-instance-p object)
186                           (clos-slots-ref (std-instance-slots object)
187                                           location))
188                          ((fsc-instance-p object)
189                           (clos-slots-ref (fsc-instance-slots object)
190                                           location))
191                          (t (error "unrecognized instance type"))))
192                   (cons
193                    (cdr location))
194                   (t
195                    (error "~@<The slot ~S has neither :INSTANCE nor :CLASS ~
196                            allocation, so it can't be read by the default ~
197                            ~S method.~@:>"
198                           slotd 'slot-value-using-class)))))
199     (if (eq value +slot-unbound+)
200         (slot-unbound class object (slot-definition-name slotd))
201         value)))
202
203 (defmethod (setf slot-value-using-class)
204            (new-value (class std-class)
205                       (object std-object)
206                       (slotd standard-effective-slot-definition))
207   (check-obsolete-instance object)
208   (let ((location (slot-definition-location slotd)))
209     (typecase location
210       (fixnum
211        (cond ((std-instance-p object)
212               (setf (clos-slots-ref (std-instance-slots object) location)
213                     new-value))
214              ((fsc-instance-p object)
215               (setf (clos-slots-ref (fsc-instance-slots object) location)
216                     new-value))
217              (t (error "unrecognized instance type"))))
218       (cons
219        (setf (cdr location) new-value))
220       (t
221        (error "~@<The slot ~S has neither :INSTANCE nor :CLASS allocation, ~
222                    so it can't be written by the default ~S method.~:@>"
223               slotd '(setf slot-value-using-class))))))
224
225 (defmethod slot-boundp-using-class
226            ((class std-class)
227             (object std-object)
228             (slotd standard-effective-slot-definition))
229   (check-obsolete-instance object)
230   (let* ((location (slot-definition-location slotd))
231          (value (typecase location
232                   (fixnum
233                    (cond ((std-instance-p object)
234                           (clos-slots-ref (std-instance-slots object)
235                                           location))
236                          ((fsc-instance-p object)
237                           (clos-slots-ref (fsc-instance-slots object)
238                                           location))
239                          (t (error "unrecognized instance type"))))
240                   (cons
241                    (cdr location))
242                   (t
243                    (error "~@<The slot ~S has neither :INSTANCE nor :CLASS ~
244                            allocation, so it can't be read by the default ~S ~
245                            method.~@:>"
246                           slotd 'slot-boundp-using-class)))))
247     (not (eq value +slot-unbound+))))
248
249 (defmethod slot-makunbound-using-class
250            ((class std-class)
251             (object std-object)
252             (slotd standard-effective-slot-definition))
253   (check-obsolete-instance object)
254   (let ((location (slot-definition-location slotd)))
255     (typecase location
256       (fixnum
257        (cond ((std-instance-p object)
258               (setf (clos-slots-ref (std-instance-slots object) location)
259                     +slot-unbound+))
260              ((fsc-instance-p object)
261               (setf (clos-slots-ref (fsc-instance-slots object) location)
262                     +slot-unbound+))
263              (t (error "unrecognized instance type"))))
264       (cons
265        (setf (cdr location) +slot-unbound+))
266       (t
267        (error "~@<The slot ~S has neither :INSTANCE nor :CLASS allocation, ~
268                so it can't be written by the default ~S method.~@:>"
269               slotd 'slot-makunbound-using-class))))
270   nil)
271
272 (defmethod slot-value-using-class
273     ((class structure-class)
274      (object structure-object)
275      (slotd structure-effective-slot-definition))
276   (let* ((function (slot-definition-internal-reader-function slotd))
277          (value (funcall function object)))
278     (declare (type function function))
279     (if (eq value +slot-unbound+)
280         (slot-unbound class object (slot-definition-name slotd))
281         value)))
282
283 (defmethod (setf slot-value-using-class)
284     (new-value (class structure-class)
285                (object structure-object)
286                (slotd structure-effective-slot-definition))
287   (let ((function (slot-definition-internal-writer-function slotd)))
288     (declare (type function function))
289     (funcall function new-value object)))
290
291 (defmethod slot-boundp-using-class
292            ((class structure-class)
293             (object structure-object)
294             (slotd structure-effective-slot-definition))
295   t)
296
297 (defmethod slot-makunbound-using-class
298            ((class structure-class)
299             (object structure-object)
300             (slotd structure-effective-slot-definition))
301   (error "Structure slots can't be unbound."))
302 \f
303 (defmethod slot-missing
304            ((class t) instance slot-name operation &optional new-value)
305   (error "~@<When attempting to ~A, the slot ~S is missing from the ~
306           object ~S.~@:>"
307          (ecase operation
308            (slot-value "read the slot's value (slot-value)")
309            (setf (format nil
310                          "set the slot's value to ~S (SETF of SLOT-VALUE)"
311                          new-value))
312            (slot-boundp "test to see whether slot is bound (SLOT-BOUNDP)")
313            (slot-makunbound "make the slot unbound (SLOT-MAKUNBOUND)"))
314          slot-name
315          instance))
316
317 (defmethod slot-unbound ((class t) instance slot-name)
318   (error 'unbound-slot :slot slot-name :instance instance))
319
320 (defun slot-unbound-internal (instance position)
321   (slot-unbound (class-of instance) instance
322                 (etypecase position
323                   (fixnum
324                    (nth position
325                         (wrapper-instance-slots-layout (wrapper-of instance))))
326                   (cons
327                    (car position)))))
328 \f
329 (defmethod allocate-instance ((class standard-class) &rest initargs)
330   (declare (ignore initargs))
331   (unless (class-finalized-p class) (finalize-inheritance class))
332   (allocate-standard-instance (class-wrapper class)))
333
334 (defmethod allocate-instance ((class structure-class) &rest initargs)
335   (declare (ignore initargs))
336   (let ((constructor (class-defstruct-constructor class)))
337     (if constructor
338         (funcall constructor)
339         (error "can't allocate an instance of class ~S" (class-name class)))))