0.9.18.38:
[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 ;;; A version of SET-SLOT-VALUE for use in safe code, where we want to
107 ;;; check types when writing to slots:
108 ;;;   * Doesn't have an optimizing compiler-macro
109 ;;;   * Isn't special-cased in WALK-METHOD-LAMBDA
110 (defun safe-set-slot-value (object slot-name new-value)
111   (set-slot-value object slot-name new-value))
112
113 (define-compiler-macro set-slot-value (&whole form object slot-name new-value
114                                               &environment env)
115   (if (and (constantp slot-name)
116            (interned-symbol-p (constant-form-value slot-name))
117            ;; We can't use the ACCESSOR-SET-SLOT-VALUE path in safe
118            ;; code, since it'll use the global automatically generated
119            ;; accessor, which won't do typechecking. (SLOT-OBJECT
120            ;; won't have been compiled with SAFETY 3, so SAFE-P will
121            ;; be NIL in MAKE-STD-WRITER-METHOD-FUNCTION).
122            (not (safe-code-p env)))
123       `(accessor-set-slot-value ,object ,slot-name ,new-value)
124       form))
125
126 (defun slot-boundp (object slot-name)
127   (let* ((class (class-of object))
128          (slot-definition (find-slot-definition class slot-name)))
129     (if (null slot-definition)
130         (not (not (slot-missing class object slot-name 'slot-boundp)))
131         (slot-boundp-using-class class object slot-definition))))
132
133 (setf (gdefinition 'slot-boundp-normal) #'slot-boundp)
134
135 (define-compiler-macro slot-boundp (&whole form object slot-name)
136   (if (and (constantp slot-name)
137            (interned-symbol-p (constant-form-value slot-name)))
138       `(accessor-slot-boundp ,object ,slot-name)
139       form))
140
141 (defun slot-makunbound (object slot-name)
142   (let* ((class (class-of object))
143          (slot-definition (find-slot-definition class slot-name)))
144     (if (null slot-definition)
145         (slot-missing class object slot-name 'slot-makunbound)
146         (slot-makunbound-using-class class object slot-definition))
147     object))
148
149 (defun slot-exists-p (object slot-name)
150   (let ((class (class-of object)))
151     (not (null (find-slot-definition class slot-name)))))
152
153 (defvar *unbound-slot-value-marker* (make-unprintable-object "unbound slot"))
154
155 ;;; This isn't documented, but is used within PCL in a number of print
156 ;;; object methods. (See NAMED-OBJECT-PRINT-FUNCTION.)
157 (defun slot-value-or-default (object slot-name &optional
158                               (default *unbound-slot-value-marker*))
159   (if (slot-boundp object slot-name)
160       (slot-value object slot-name)
161       default))
162 \f
163 (defun standard-instance-access (instance location)
164   (clos-slots-ref (std-instance-slots instance) location))
165
166 (defun funcallable-standard-instance-access (instance location)
167   (clos-slots-ref (fsc-instance-slots instance) location))
168
169 (defmethod slot-value-using-class ((class std-class)
170                                    (object standard-object)
171                                    (slotd standard-effective-slot-definition))
172   (check-obsolete-instance object)
173   (let* ((location (slot-definition-location slotd))
174          (value
175           (typecase location
176             (fixnum
177              (cond ((std-instance-p object)
178                     (clos-slots-ref (std-instance-slots object)
179                                     location))
180                    ((fsc-instance-p object)
181                     (clos-slots-ref (fsc-instance-slots object)
182                                     location))
183                    (t (bug "unrecognized instance type in ~S"
184                            'slot-value-using-class))))
185             (cons
186              (cdr location))
187             (t
188              (instance-structure-protocol-error slotd
189                                                 'slot-value-using-class)))))
190     (if (eq value +slot-unbound+)
191         (values (slot-unbound class object (slot-definition-name slotd)))
192         value)))
193
194 (defmethod (setf slot-value-using-class)
195            (new-value (class std-class)
196                       (object standard-object)
197                       (slotd standard-effective-slot-definition))
198   (check-obsolete-instance object)
199   (let ((location (slot-definition-location slotd))
200         (type-check-function
201          (when (safe-p class)
202            (slot-definition-type-check-function slotd))))
203     (flet ((check (new-value)
204              (when type-check-function
205                (funcall (the function type-check-function) new-value))
206              new-value))
207       (typecase location
208         (fixnum
209          (cond ((std-instance-p object)
210                 (setf (clos-slots-ref (std-instance-slots object) location)
211                       (check new-value)))
212                ((fsc-instance-p object)
213                 (setf (clos-slots-ref (fsc-instance-slots object) location)
214                       (check new-value)))
215                 (t (bug "unrecognized instance type in ~S"
216                         '(setf slot-value-using-class)))))
217         (cons
218          (setf (cdr location) (check new-value)))
219         (t
220          (instance-structure-protocol-error
221           slotd '(setf slot-value-using-class)))))))
222
223 (defmethod slot-boundp-using-class
224            ((class std-class)
225             (object standard-object)
226             (slotd standard-effective-slot-definition))
227   (check-obsolete-instance object)
228   (let* ((location (slot-definition-location slotd))
229          (value
230           (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 (bug "unrecognized instance type in ~S"
239                            'slot-boundp-using-class))))
240             (cons
241              (cdr location))
242             (t
243              (instance-structure-protocol-error slotd
244                                                 'slot-boundp-using-class)))))
245     (not (eq value +slot-unbound+))))
246
247 (defmethod slot-makunbound-using-class
248            ((class std-class)
249             (object standard-object)
250             (slotd standard-effective-slot-definition))
251   (check-obsolete-instance object)
252   (let ((location (slot-definition-location slotd)))
253     (typecase location
254       (fixnum
255        (cond ((std-instance-p object)
256               (setf (clos-slots-ref (std-instance-slots object) location)
257                     +slot-unbound+))
258              ((fsc-instance-p object)
259               (setf (clos-slots-ref (fsc-instance-slots object) location)
260                     +slot-unbound+))
261              (t (bug "unrecognized instance type in ~S"
262                      'slot-makunbound-using-class))))
263       (cons
264        (setf (cdr location) +slot-unbound+))
265       (t
266        (instance-structure-protocol-error slotd
267                                           'slot-makunbound-using-class))))
268   object)
269
270 (defmethod slot-value-using-class
271     ((class condition-class)
272      (object condition)
273      (slotd condition-effective-slot-definition))
274   (let ((fun (slot-definition-reader-function slotd)))
275     (declare (type function fun))
276     (funcall fun object)))
277
278 (defmethod (setf slot-value-using-class)
279     (new-value
280      (class condition-class)
281      (object condition)
282      (slotd condition-effective-slot-definition))
283   (let ((fun (slot-definition-writer-function slotd)))
284     (declare (type function fun))
285     (funcall fun new-value object)))
286
287 (defmethod slot-boundp-using-class
288     ((class condition-class)
289      (object condition)
290      (slotd condition-effective-slot-definition))
291   (let ((fun (slot-definition-boundp-function slotd)))
292     (declare (type function fun))
293     (funcall fun object)))
294
295 (defmethod slot-makunbound-using-class ((class condition-class) object slot)
296   (error "attempt to unbind slot ~S in condition object ~S."
297          slot object))
298
299 (defmethod slot-value-using-class
300     ((class structure-class)
301      (object structure-object)
302      (slotd structure-effective-slot-definition))
303   (let* ((function (slot-definition-internal-reader-function slotd))
304          (value (funcall function object)))
305     (declare (type function function))
306     (if (eq value +slot-unbound+)
307         (values (slot-unbound class object (slot-definition-name slotd)))
308         value)))
309
310 (defmethod (setf slot-value-using-class)
311     (new-value (class structure-class)
312                (object structure-object)
313                (slotd structure-effective-slot-definition))
314   (let ((function (slot-definition-internal-writer-function slotd)))
315     (declare (type function function))
316     (funcall function new-value object)))
317
318 (defmethod slot-boundp-using-class
319            ((class structure-class)
320             (object structure-object)
321             (slotd structure-effective-slot-definition))
322   t)
323
324 (defmethod slot-makunbound-using-class
325            ((class structure-class)
326             (object structure-object)
327             (slotd structure-effective-slot-definition))
328   (error "Structure slots can't be unbound."))
329 \f
330 (defmethod slot-missing
331            ((class t) instance slot-name operation &optional new-value)
332   (error "~@<When attempting to ~A, the slot ~S is missing from the ~
333           object ~S.~@:>"
334          (ecase operation
335            (slot-value "read the slot's value (slot-value)")
336            (setf (format nil
337                          "set the slot's value to ~S (SETF of SLOT-VALUE)"
338                          new-value))
339            (slot-boundp "test to see whether slot is bound (SLOT-BOUNDP)")
340            (slot-makunbound "make the slot unbound (SLOT-MAKUNBOUND)"))
341          slot-name
342          instance))
343
344 (defmethod slot-unbound ((class t) instance slot-name)
345   (restart-case
346       (error 'unbound-slot :name slot-name :instance instance)
347     (use-value (v)
348       :report "Return a value as the slot-value."
349       :interactive read-evaluated-form
350       v)
351     (store-value (v)
352       :report "Store and return a value as the slot-value."
353       :interactive read-evaluated-form
354       (setf (slot-value instance slot-name) v))))
355
356 (defun slot-unbound-internal (instance position)
357   (values
358    (slot-unbound
359     (class-of instance)
360     instance
361     (etypecase position
362       (fixnum
363        (nth position (wrapper-instance-slots-layout (wrapper-of instance))))
364       (cons
365        (car position))))))
366 \f
367 ;;; FIXME: AMOP says that allocate-instance imples finalize-inheritance
368 ;;; if the class is not yet finalized, but we don't seem to be taking
369 ;;; care of this for non-standard-classes.x
370 (defmethod allocate-instance ((class standard-class) &rest initargs)
371   (declare (ignore initargs))
372   (unless (class-finalized-p class)
373     (finalize-inheritance class))
374   (allocate-standard-instance (class-wrapper class)))
375
376 (defmethod allocate-instance ((class structure-class) &rest initargs)
377   (declare (ignore initargs))
378   (let ((constructor (class-defstruct-constructor class)))
379     (if constructor
380         (funcall constructor)
381         (allocate-standard-instance (class-wrapper class)))))
382
383 ;;; FIXME: It would be nicer to have allocate-instance return
384 ;;; uninitialized objects for conditions as well.
385 (defmethod allocate-instance ((class condition-class) &rest initargs)
386   (declare (ignore initargs))
387   (make-condition (class-name class)))
388
389 (defmethod allocate-instance ((class built-in-class) &rest initargs)
390   (declare (ignore initargs))
391   (error "Cannot allocate an instance of ~S." class)) ; So sayeth AMOP