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