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