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