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