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