7dac38610e9415a244e313dbdad3a6047409e91a
[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
26 (sb-int:file-comment
27   "$Header$")
28 \f
29 ;;;; ANSI CL condition for unbound slots
30
31 (define-condition unbound-slot (cell-error)
32   ((instance :reader unbound-slot-instance :initarg :instance)
33    (slot :reader unbound-slot-slot :initarg :slot))
34   (:report (lambda(condition stream)
35              (format stream "The slot ~S is unbound in the object ~S"
36                      (unbound-slot-slot condition)
37                      (unbound-slot-instance condition)))))
38
39 (defmethod wrapper-fetcher ((class standard-class))
40   'std-instance-wrapper)
41
42 (defmethod slots-fetcher ((class standard-class))
43   'std-instance-slots)
44
45 (defmethod raw-instance-allocator ((class standard-class))
46   'allocate-standard-instance)
47
48 ;;; These four functions work on std-instances and fsc-instances. These are
49 ;;; instances for which it is possible to change the wrapper and the slots.
50 ;;;
51 ;;; For these kinds of instances, most specified methods from the instance
52 ;;; structure protocol are promoted to the implementation-specific class
53 ;;; std-class. Many of these methods call these four functions.
54
55 (defun set-wrapper (inst new)
56   (cond ((std-instance-p inst)
57          (setf (std-instance-wrapper inst) new))
58         ((fsc-instance-p inst)
59          (setf (fsc-instance-wrapper inst) new))
60         (t
61          (error "unrecognized instance type"))))
62
63 (defun swap-wrappers-and-slots (i1 i2)
64   (without-interrupts
65    (cond ((std-instance-p i1)
66           (let ((w1 (std-instance-wrapper i1))
67                 (s1 (std-instance-slots i1)))
68             (setf (std-instance-wrapper i1) (std-instance-wrapper i2))
69             (setf (std-instance-slots i1) (std-instance-slots i2))
70             (setf (std-instance-wrapper i2) w1)
71             (setf (std-instance-slots i2) s1)))
72          ((fsc-instance-p i1)
73           (let ((w1 (fsc-instance-wrapper i1))
74                 (s1 (fsc-instance-slots i1)))
75             (setf (fsc-instance-wrapper i1) (fsc-instance-wrapper i2))
76             (setf (fsc-instance-slots i1) (fsc-instance-slots i2))
77             (setf (fsc-instance-wrapper i2) w1)
78             (setf (fsc-instance-slots i2) s1)))
79          (t
80           (error "unrecognized instance type")))))
81 \f
82 (defun get-class-slot-value-1 (object wrapper slot-name)
83   (let ((entry (assoc slot-name (wrapper-class-slots wrapper))))
84     (if (null entry)
85         (slot-missing (wrapper-class wrapper) object slot-name 'slot-value)
86         (if (eq (cdr entry) *slot-unbound*)
87             (slot-unbound (wrapper-class wrapper) object slot-name)
88             (cdr entry)))))
89
90 (defun set-class-slot-value-1 (new-value object wrapper slot-name)
91   (let ((entry (assoc slot-name (wrapper-class-slots wrapper))))
92     (if (null entry)
93         (slot-missing (wrapper-class wrapper)
94                       object
95                       slot-name
96                       'setf
97                       new-value)
98         (setf (cdr entry) new-value))))
99
100 (defmethod class-slot-value ((class std-class) slot-name)
101   (let ((wrapper (class-wrapper class))
102         (prototype (class-prototype class)))
103     (get-class-slot-value-1 prototype wrapper slot-name)))
104
105 (defmethod (setf class-slot-value) (nv (class std-class) slot-name)
106   (let ((wrapper (class-wrapper class))
107         (prototype (class-prototype class)))
108     (set-class-slot-value-1 nv prototype wrapper slot-name)))
109 \f
110 (defun find-slot-definition (class slot-name)
111   (dolist (slot (class-slots class) nil)
112     (when (eql slot-name (slot-definition-name slot))
113       (return slot))))
114
115 (defun slot-value (object slot-name)
116   (let* ((class (class-of object))
117          (slot-definition (find-slot-definition class slot-name)))
118     (if (null slot-definition)
119         (slot-missing class object slot-name 'slot-value)
120         (slot-value-using-class class object slot-definition))))
121
122 (setf (gdefinition 'slot-value-normal) #'slot-value)
123
124 (define-compiler-macro slot-value (object-form slot-name-form)
125   (if (and (constantp slot-name-form)
126            (let ((slot-name (eval slot-name-form)))
127              (and (symbolp slot-name) (symbol-package slot-name))))
128       `(accessor-slot-value ,object-form ,slot-name-form)
129       `(slot-value-normal ,object-form ,slot-name-form)))
130
131 (defun set-slot-value (object slot-name new-value)
132   (let* ((class (class-of object))
133          (slot-definition (find-slot-definition class slot-name)))
134     (if (null slot-definition)
135         (slot-missing class object slot-name 'setf)
136         (setf (slot-value-using-class class object slot-definition)
137               new-value))))
138
139 (setf (gdefinition 'set-slot-value-normal) #'set-slot-value)
140
141 (define-compiler-macro set-slot-value (object-form slot-name-form new-value-form)
142   (if (and (constantp slot-name-form)
143            (let ((slot-name (eval slot-name-form)))
144              (and (symbolp slot-name) (symbol-package slot-name))))
145       `(accessor-set-slot-value ,object-form ,slot-name-form ,new-value-form)
146       `(set-slot-value-normal ,object-form ,slot-name-form ,new-value-form)))
147
148 (defconstant *optimize-slot-boundp* nil)
149
150 (defun slot-boundp (object slot-name)
151   (let* ((class (class-of object))
152          (slot-definition (find-slot-definition class slot-name)))
153     (if (null slot-definition)
154         (slot-missing class object slot-name 'slot-boundp)
155         (slot-boundp-using-class class object slot-definition))))
156
157 (setf (gdefinition 'slot-boundp-normal) #'slot-boundp)
158
159 (define-compiler-macro slot-boundp (object-form slot-name-form)
160   (if (and (constantp slot-name-form)
161            (let ((slot-name (eval slot-name-form)))
162              (and (symbolp slot-name) (symbol-package slot-name))))
163       `(accessor-slot-boundp ,object-form ,slot-name-form)
164       `(slot-boundp-normal ,object-form ,slot-name-form)))
165
166 (defun slot-makunbound (object slot-name)
167   (let* ((class (class-of object))
168          (slot-definition (find-slot-definition class slot-name)))
169     (if (null slot-definition)
170         (slot-missing class object slot-name 'slot-makunbound)
171         (slot-makunbound-using-class class object slot-definition))))
172
173 (defun slot-exists-p (object slot-name)
174   (let ((class (class-of object)))
175     (not (null (find-slot-definition class slot-name)))))
176
177 ;;; This isn't documented, but is used within PCL in a number of print
178 ;;; object methods. (See NAMED-OBJECT-PRINT-FUNCTION.)
179 (defun slot-value-or-default (object slot-name &optional (default "unbound"))
180   (if (slot-boundp object slot-name)
181       (slot-value object slot-name)
182       default))
183 \f
184 (defun standard-instance-access (instance location)
185   (%instance-ref (std-instance-slots instance) location))
186
187 (defun funcallable-standard-instance-access (instance location)
188   (%instance-ref (fsc-instance-slots instance) location))
189
190 (defmethod slot-value-using-class ((class std-class)
191                                    (object std-object)
192                                    (slotd standard-effective-slot-definition))
193   (let* ((location (slot-definition-location slotd))
194          (value (typecase location
195                   (fixnum
196                    (cond ((std-instance-p object)
197                           ;; FIXME: EQ T (WRAPPER-STATE ..) is better done
198                           ;; through INVALID-WRAPPER-P (here and below).
199                           (unless (eq 't (wrapper-state (std-instance-wrapper object)))
200                             (check-wrapper-validity object))
201                           (%instance-ref (std-instance-slots object) location))
202                          ((fsc-instance-p object)
203                           (unless (eq 't (wrapper-state (fsc-instance-wrapper object)))
204                             (check-wrapper-validity object))
205                           (%instance-ref (fsc-instance-slots object) location))
206                          (t (error "unrecognized instance type"))))
207                   (cons
208                    (cdr location))
209                   (t
210                    (error "The slot ~S has neither :INSTANCE nor :CLASS allocation, ~@
211                            so it can't be read by the default ~S method."
212                           slotd 'slot-value-using-class)))))
213     (if (eq value *slot-unbound*)
214         (slot-unbound class object (slot-definition-name slotd))
215         value)))
216
217 (defmethod (setf slot-value-using-class)
218            (new-value (class std-class)
219                       (object std-object)
220                       (slotd standard-effective-slot-definition))
221   (let ((location (slot-definition-location slotd)))
222     (typecase location
223       (fixnum
224        (cond ((std-instance-p object)
225               (unless (eq 't (wrapper-state (std-instance-wrapper object)))
226                 (check-wrapper-validity object))
227               (setf (%instance-ref (std-instance-slots object) location) new-value))
228              ((fsc-instance-p object)
229               (unless (eq 't (wrapper-state (fsc-instance-wrapper object)))
230                 (check-wrapper-validity object))
231               (setf (%instance-ref (fsc-instance-slots object) location) new-value))
232              (t (error "unrecognized instance type"))))
233       (cons
234        (setf (cdr location) new-value))
235       (t
236        (error "The slot ~S has neither :INSTANCE nor :CLASS allocation, ~@
237                            so it can't be written by the default ~S method."
238               slotd '(setf slot-value-using-class))))))
239
240 (defmethod slot-boundp-using-class
241            ((class std-class)
242             (object std-object)
243             (slotd standard-effective-slot-definition))
244   (let* ((location (slot-definition-location slotd))
245          (value (typecase location
246                   (fixnum
247                    (cond ((std-instance-p object)
248                           (unless (eq 't (wrapper-state (std-instance-wrapper object)))
249                             (check-wrapper-validity object))
250                           (%instance-ref (std-instance-slots object) location))
251                          ((fsc-instance-p object)
252                           (unless (eq 't (wrapper-state (fsc-instance-wrapper object)))
253                             (check-wrapper-validity object))
254                           (%instance-ref (fsc-instance-slots object) location))
255                          (t (error "unrecognized instance type"))))
256                   (cons
257                    (cdr location))
258                   (t
259                    (error "The slot ~S has neither :INSTANCE nor :CLASS allocation, ~@
260                            so it can't be read by the default ~S method."
261                           slotd 'slot-boundp-using-class)))))
262     (not (eq value *slot-unbound*))))
263
264 (defmethod slot-makunbound-using-class
265            ((class std-class)
266             (object std-object)
267             (slotd standard-effective-slot-definition))
268   (let ((location (slot-definition-location slotd)))
269     (typecase location
270       (fixnum
271        (cond ((std-instance-p object)
272               (unless (eq 't (wrapper-state (std-instance-wrapper object)))
273                 (check-wrapper-validity object))
274               (setf (%instance-ref (std-instance-slots object) location) *slot-unbound*))
275              ((fsc-instance-p object)
276               (unless (eq 't (wrapper-state (fsc-instance-wrapper object)))
277                 (check-wrapper-validity object))
278               (setf (%instance-ref (fsc-instance-slots object) location) *slot-unbound*))
279              (t (error "unrecognized instance type"))))
280       (cons
281        (setf (cdr location) *slot-unbound*))
282       (t
283        (error "The slot ~S has neither :INSTANCE nor :CLASS allocation, ~@
284                            so it can't be written by the default ~S method."
285               slotd 'slot-makunbound-using-class))))
286   nil)
287
288 (defmethod slot-value-using-class
289     ((class structure-class)
290      (object structure-object)
291      (slotd structure-effective-slot-definition))
292   (let* ((function (slot-definition-internal-reader-function slotd))
293          (value (funcall function object)))
294     (declare (type function function))
295     (if (eq value *slot-unbound*)
296         (slot-unbound class object (slot-definition-name slotd))
297         value)))
298
299 (defmethod (setf slot-value-using-class)
300     (new-value (class structure-class)
301                (object structure-object)
302                (slotd structure-effective-slot-definition))
303   (let ((function (slot-definition-internal-writer-function slotd)))
304     (declare (type function function))
305     (funcall function new-value object)))
306
307 (defmethod slot-boundp-using-class
308            ((class structure-class)
309             (object structure-object)
310             (slotd structure-effective-slot-definition))
311   t)
312
313 (defmethod slot-makunbound-using-class
314            ((class structure-class)
315             (object structure-object)
316             (slotd structure-effective-slot-definition))
317   (error "Structure slots can't be unbound."))
318 \f
319 (defmethod slot-missing
320            ((class t) instance slot-name operation &optional new-value)
321   (error "When attempting to ~A,~%the slot ~S is missing from the object ~S."
322          (ecase operation
323            (slot-value "read the slot's value (slot-value)")
324            (setf (format nil
325                          "set the slot's value to ~S (setf of slot-value)"
326                          new-value))
327            (slot-boundp "test to see whether slot is bound (slot-boundp)")
328            (slot-makunbound "make the slot unbound (slot-makunbound)"))
329          slot-name
330          instance))
331
332 (defmethod slot-unbound ((class t) instance slot-name)
333   (error 'unbound-slot :slot slot-name :instance instance))
334
335 (defun slot-unbound-internal (instance position)
336   (slot-unbound (class-of instance) instance
337                 (etypecase position
338                   (fixnum
339                    (nth position
340                         (wrapper-instance-slots-layout (wrapper-of instance))))
341                   (cons
342                    (car position)))))
343 \f
344 (defmethod allocate-instance ((class standard-class) &rest initargs)
345   (declare (ignore initargs))
346   (unless (class-finalized-p class) (finalize-inheritance class))
347   (allocate-standard-instance (class-wrapper class)))
348
349 (defmethod allocate-instance ((class structure-class) &rest initargs)
350   (declare (ignore initargs))
351   (let ((constructor (class-defstruct-constructor class)))
352     (if constructor
353         (funcall constructor)
354         (error "can't allocate an instance of class ~S" (class-name class)))))