9054a5a563607dbf5aaac4436d17a85289dbd013
[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 three 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 %swap-wrappers-and-slots (i1 i2)
52   (cond ((std-instance-p i1)
53          (let ((w1 (std-instance-wrapper i1))
54                (s1 (std-instance-slots i1)))
55            (setf (std-instance-wrapper i1) (std-instance-wrapper i2))
56            (setf (std-instance-slots i1) (std-instance-slots i2))
57            (setf (std-instance-wrapper i2) w1)
58            (setf (std-instance-slots i2) s1)))
59         ((fsc-instance-p i1)
60          (let ((w1 (fsc-instance-wrapper i1))
61                (s1 (fsc-instance-slots i1)))
62            (setf (fsc-instance-wrapper i1) (fsc-instance-wrapper i2))
63            (setf (fsc-instance-slots i1) (fsc-instance-slots i2))
64            (setf (fsc-instance-wrapper i2) w1)
65            (setf (fsc-instance-slots i2) s1)))
66         (t
67          (error "unrecognized instance type"))))
68 \f
69 ;;;; STANDARD-INSTANCE-ACCESS
70
71 (declaim (inline standard-instance-access (setf standard-instance-access)
72                  funcallable-standard-instance-access
73                  (setf funcallable-standard-instance-access)))
74
75 (defun standard-instance-access (instance location)
76   (clos-slots-ref (std-instance-slots instance) location))
77
78 (defun (setf standard-instance-access) (new-value instance location)
79   (setf (clos-slots-ref (std-instance-slots instance) location) new-value))
80
81 (defun funcallable-standard-instance-access (instance location)
82   (clos-slots-ref (fsc-instance-slots instance) location))
83
84 (defun (setf funcallable-standard-instance-access) (new-value instance location)
85   (setf (clos-slots-ref (fsc-instance-slots instance) location) new-value))
86 \f
87 ;;;; SLOT-VALUE, (SETF SLOT-VALUE), SLOT-BOUNDP, SLOT-MAKUNBOUND
88
89 (declaim (ftype (sfunction (t symbol) t) slot-value))
90 (defun slot-value (object slot-name)
91   (let* ((wrapper (valid-wrapper-of object))
92          (cell (or (find-slot-cell wrapper slot-name)
93                    (return-from slot-value
94                      (values (slot-missing (wrapper-class* wrapper) object slot-name
95                                            'slot-value)))))
96          (location (car cell))
97          (value
98           (cond ((fixnump location)
99                  (if (std-instance-p object)
100                      (standard-instance-access object location)
101                      (funcallable-standard-instance-access object location)))
102                 ((consp location)
103                  (cdr location))
104                 ((not location)
105                  (return-from slot-value
106                    (funcall (slot-info-reader (cdr cell)) object)))
107                 (t
108                  (bug "Bogus slot cell in SLOT-VALUE: ~S" cell)))))
109     (if (eq +slot-unbound+ value)
110         (slot-unbound (wrapper-class* wrapper) object slot-name)
111         value)))
112
113 ;;; This is used during the PCL build, but gets replaced by a deftransform
114 ;;; in fixup.lisp.
115 (define-compiler-macro slot-value (&whole form object slot-name
116                                    &environment env)
117   (if (and (constantp slot-name env)
118            (interned-symbol-p (constant-form-value slot-name env)))
119       `(accessor-slot-value ,object ,slot-name)
120       form))
121
122 (defun set-slot-value (object slot-name new-value)
123   (let* ((wrapper (valid-wrapper-of object))
124          (cell (or (find-slot-cell wrapper slot-name)
125                    (return-from set-slot-value
126                      (values (slot-missing (wrapper-class* wrapper) object slot-name
127                                            'setf new-value)))))
128          (location (car cell))
129          (info (cdr cell))
130          (typecheck (slot-info-typecheck info)))
131     (when typecheck
132       (funcall typecheck new-value))
133     (cond ((fixnump location)
134            (if (std-instance-p object)
135                (setf (standard-instance-access object location) new-value)
136                (setf (funcallable-standard-instance-access object location)
137                      new-value)))
138           ((consp location)
139            (setf (cdr location) new-value))
140           ((not location)
141            (funcall (slot-info-writer info) new-value object))
142           (t
143            (bug "Bogus slot-cell in SET-SLOT-VALUE: ~S" cell))))
144   new-value)
145
146 ;;; A version of SET-SLOT-VALUE for use in safe code, where we want to
147 ;;; check types when writing to slots:
148 ;;;   * Doesn't have an optimizing compiler-macro
149 ;;;   * Isn't special-cased in WALK-METHOD-LAMBDA
150 (defun safe-set-slot-value (object slot-name new-value)
151   (set-slot-value object slot-name new-value))
152
153 ;;; This is used during the PCL build, but gets replaced by a deftransform
154 ;;; in fixup.lisp.
155 (define-compiler-macro set-slot-value (&whole form object slot-name new-value
156                                       &environment env)
157   (if (and (constantp slot-name env)
158            (interned-symbol-p (constant-form-value slot-name env))
159            ;; We can't use the ACCESSOR-SET-SLOT-VALUE path in safe
160            ;; code, since it'll use the global automatically generated
161            ;; accessor, which won't do typechecking. (SLOT-OBJECT
162            ;; won't have been compiled with SAFETY 3, so SAFE-P will
163            ;; be NIL in MAKE-STD-WRITER-METHOD-FUNCTION).
164            (not (safe-code-p env)))
165       `(accessor-set-slot-value ,object ,slot-name ,new-value)
166       form))
167
168 (defun slot-boundp (object slot-name)
169   (let* ((wrapper (valid-wrapper-of object))
170          (cell (or (find-slot-cell wrapper slot-name)
171                    (return-from slot-boundp
172                      (and (slot-missing (wrapper-class* wrapper) object slot-name
173                                         'slot-boundp)
174                           t))))
175          (location (car cell))
176          (value
177           (cond ((fixnump location)
178                  (if (std-instance-p object)
179                      (standard-instance-access object location)
180                      (funcallable-standard-instance-access object location)))
181                 ((consp location)
182                  (cdr location))
183                 ((not location)
184                  (return-from slot-boundp
185                    (funcall (slot-info-boundp (cdr cell)) object)))
186                 (t
187                  (bug "Bogus slot cell in SLOT-VALUE: ~S" cell)))))
188     (not (eq +slot-unbound+ value))))
189
190 (define-compiler-macro slot-boundp (&whole form object slot-name
191                                     &environment env)
192   (if (and (constantp slot-name env)
193            (interned-symbol-p (constant-form-value slot-name env)))
194       `(accessor-slot-boundp ,object ,slot-name)
195       form))
196
197 (defun slot-makunbound (object slot-name)
198   (let* ((wrapper (valid-wrapper-of object))
199          (cell (find-slot-cell wrapper slot-name))
200          (location (car cell)))
201     (cond ((fixnump location)
202            (if (std-instance-p object)
203                (setf (standard-instance-access object location) +slot-unbound+)
204                (setf (funcallable-standard-instance-access object location)
205                      +slot-unbound+)))
206           ((consp location)
207            (setf (cdr location) +slot-unbound+))
208           ((not cell)
209            (slot-missing (wrapper-class* wrapper) object slot-name 'slot-makunbound))
210           ((not location)
211            (let ((class (wrapper-class* wrapper)))
212              (slot-makunbound-using-class class object (find-slot-definition class slot-name))))
213           (t
214            (bug "Bogus slot-cell in SLOT-MAKUNBOUND: ~S" cell))))
215   object)
216
217 (defun slot-exists-p (object slot-name)
218   (let ((class (class-of object)))
219     (not (null (find-slot-definition class slot-name)))))
220
221 (defvar *unbound-slot-value-marker* (make-unprintable-object "unbound slot"))
222
223 ;;; This isn't documented, but is used within PCL in a number of print
224 ;;; object methods. (See NAMED-OBJECT-PRINT-FUNCTION.)
225 (defun slot-value-or-default (object slot-name &optional
226                               (default *unbound-slot-value-marker*))
227   (if (slot-boundp object slot-name)
228       (slot-value object slot-name)
229       default))
230
231 (defmethod slot-value-using-class ((class std-class)
232                                    (object standard-object)
233                                    (slotd standard-effective-slot-definition))
234   ;; FIXME: Do we need this? SLOT-VALUE checks for obsolete
235   ;; instances. Are users allowed to call this directly?
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-value-using-class))))
249             (cons
250              (cdr location))
251             (t
252              (instance-structure-protocol-error slotd
253                                                 'slot-value-using-class)))))
254     (if (eq value +slot-unbound+)
255         (values (slot-unbound class object (slot-definition-name slotd)))
256         value)))
257
258 (defmethod (setf slot-value-using-class)
259            (new-value (class std-class)
260                       (object standard-object)
261                       (slotd standard-effective-slot-definition))
262   ;; FIXME: Do we need this? SET-SLOT-VALUE checks for obsolete
263   ;; instances. Are users allowed to call this directly?
264   (check-obsolete-instance object)
265   (let* ((info (slot-definition-info slotd))
266          (location (slot-definition-location slotd))
267          (typecheck (slot-info-typecheck info))
268          (new-value (if typecheck
269                         (funcall (the function typecheck) new-value)
270                         new-value)))
271     (typecase location
272       (fixnum
273        (cond ((std-instance-p object)
274               (setf (clos-slots-ref (std-instance-slots object) location)
275                     new-value))
276              ((fsc-instance-p object)
277               (setf (clos-slots-ref (fsc-instance-slots object) location)
278                     new-value))
279              (t (bug "unrecognized instance type in ~S"
280                      '(setf slot-value-using-class)))))
281       (cons
282        (setf (cdr location) new-value))
283       (t
284        (instance-structure-protocol-error
285         slotd '(setf slot-value-using-class))))))
286
287 (defmethod slot-boundp-using-class
288            ((class std-class)
289             (object standard-object)
290             (slotd standard-effective-slot-definition))
291   ;; FIXME: Do we need this? SLOT-BOUNDP checks for obsolete
292   ;; instances. Are users allowed to call this directly?
293   (check-obsolete-instance object)
294   (let* ((location (slot-definition-location slotd))
295          (value
296           (typecase location
297             (fixnum
298              (cond ((std-instance-p object)
299                           (clos-slots-ref (std-instance-slots object)
300                                           location))
301                    ((fsc-instance-p object)
302                     (clos-slots-ref (fsc-instance-slots object)
303                                     location))
304                    (t (bug "unrecognized instance type in ~S"
305                            'slot-boundp-using-class))))
306             (cons
307              (cdr location))
308             (t
309              (instance-structure-protocol-error slotd
310                                                 'slot-boundp-using-class)))))
311     (not (eq value +slot-unbound+))))
312
313 (defmethod slot-makunbound-using-class
314            ((class std-class)
315             (object standard-object)
316             (slotd standard-effective-slot-definition))
317   (check-obsolete-instance object)
318   (let ((location (slot-definition-location slotd)))
319     (typecase location
320       (fixnum
321        (cond ((std-instance-p object)
322               (setf (clos-slots-ref (std-instance-slots object) location)
323                     +slot-unbound+))
324              ((fsc-instance-p object)
325               (setf (clos-slots-ref (fsc-instance-slots object) location)
326                     +slot-unbound+))
327              (t (bug "unrecognized instance type in ~S"
328                      'slot-makunbound-using-class))))
329       (cons
330        (setf (cdr location) +slot-unbound+))
331       (t
332        (instance-structure-protocol-error slotd
333                                           'slot-makunbound-using-class))))
334   object)
335
336 (defmethod slot-value-using-class
337     ((class condition-class)
338      (object condition)
339      (slotd condition-effective-slot-definition))
340   (let ((fun (slot-info-reader (slot-definition-info slotd))))
341     (funcall fun object)))
342
343 (defmethod (setf slot-value-using-class)
344     (new-value
345      (class condition-class)
346      (object condition)
347      (slotd condition-effective-slot-definition))
348   (let ((fun (slot-info-writer (slot-definition-info slotd))))
349     (funcall fun new-value object)))
350
351 (defmethod slot-boundp-using-class
352     ((class condition-class)
353      (object condition)
354      (slotd condition-effective-slot-definition))
355   (let ((fun (slot-info-boundp (slot-definition-info slotd))))
356     (funcall fun object)))
357
358 (defmethod slot-makunbound-using-class ((class condition-class) object slot)
359   (error "attempt to unbind slot ~S in condition object ~S."
360          slot object))
361
362 (defmethod slot-value-using-class
363     ((class structure-class)
364      (object structure-object)
365      (slotd structure-effective-slot-definition))
366   (let* ((function (slot-definition-internal-reader-function slotd))
367          (value (funcall function object)))
368     (declare (type function function))
369     ;; FIXME: Is this really necessary? Structure slots should surely
370     ;; never be unbound!
371     (if (eq value +slot-unbound+)
372         (values (slot-unbound class object (slot-definition-name slotd)))
373         value)))
374
375 (defmethod (setf slot-value-using-class)
376     (new-value (class structure-class)
377                (object structure-object)
378                (slotd structure-effective-slot-definition))
379   (let ((function (slot-definition-internal-writer-function slotd)))
380     (declare (type function function))
381     (funcall function new-value object)))
382
383 (defmethod slot-boundp-using-class
384            ((class structure-class)
385             (object structure-object)
386             (slotd structure-effective-slot-definition))
387   t)
388
389 (defmethod slot-makunbound-using-class
390            ((class structure-class)
391             (object structure-object)
392             (slotd structure-effective-slot-definition))
393   (error "Structure slots can't be unbound."))
394 \f
395 (defmethod slot-missing
396            ((class t) instance slot-name operation &optional new-value)
397   (error "~@<When attempting to ~A, the slot ~S is missing from the ~
398           object ~S.~@:>"
399          (ecase operation
400            (slot-value "read the slot's value (slot-value)")
401            (setf (format nil
402                          "set the slot's value to ~S (SETF of SLOT-VALUE)"
403                          new-value))
404            (slot-boundp "test to see whether slot is bound (SLOT-BOUNDP)")
405            (slot-makunbound "make the slot unbound (SLOT-MAKUNBOUND)"))
406          slot-name
407          instance))
408
409 (defmethod slot-unbound ((class t) instance slot-name)
410   (restart-case
411       (error 'unbound-slot :name slot-name :instance instance)
412     (use-value (v)
413       :report "Return a value as the slot-value."
414       :interactive read-evaluated-form
415       v)
416     (store-value (v)
417       :report "Store and return a value as the slot-value."
418       :interactive read-evaluated-form
419       (setf (slot-value instance slot-name) v))))
420
421 (defun slot-unbound-internal (instance position)
422   (values
423    (slot-unbound
424     (class-of instance)
425     instance
426     (etypecase position
427       (fixnum
428        ;; In the vast majority of cases location corresponds to the position
429        ;; in list. The only exceptions are when there are non-local slots
430        ;; before the one we want.
431        (let* ((slots (wrapper-slots (wrapper-of instance)))
432               (guess (nth position slots)))
433          (if (eql position (slot-definition-location guess))
434              (slot-definition-name guess)
435              (slot-definition-name
436               (car (member position (class-slots instance) :key #'slot-definition-location))))))
437       (cons
438        (car position))))))
439 \f
440 ;;; FIXME: AMOP says that allocate-instance imples finalize-inheritance
441 ;;; if the class is not yet finalized, but we don't seem to be taking
442 ;;; care of this for non-standard-classes.x
443 (defmethod allocate-instance ((class standard-class) &rest initargs)
444   (declare (ignore initargs))
445   (unless (class-finalized-p class)
446     (finalize-inheritance class))
447   (allocate-standard-instance (class-wrapper class)))
448
449 (defmethod allocate-instance ((class structure-class) &rest initargs)
450   (declare (ignore initargs))
451   (let ((constructor (class-defstruct-constructor class)))
452     (if constructor
453         (funcall constructor)
454         (error "Don't know how to allocate ~S" class))))
455
456 ;;; FIXME: It would be nicer to have allocate-instance return
457 ;;; uninitialized objects for conditions as well.
458 (defmethod allocate-instance ((class condition-class) &rest initargs)
459   (declare (ignore initargs))
460   (make-condition (class-name class)))
461
462 (defmethod allocate-instance ((class built-in-class) &rest initargs)
463   (declare (ignore initargs))
464   (error "Cannot allocate an instance of ~S." class)) ; So sayeth AMOP
465
466 ;;; AMOP says that CLASS-SLOTS signals an error for unfinalized classes.
467 (defmethod class-slots :before ((class slot-class))
468   (unless (class-finalized-p class)
469     (error 'simple-reference-error
470            :format-control "~S called on ~S, which is not yet finalized."
471            :format-arguments (list 'class-slots class)
472            :references (list '(:amop :generic-function class-slots)))))