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