1.0.9.47: VALID-WRAPPER-OF
[sbcl/simd.git] / src / pcl / slots.lisp
blobf406c6fd91f603fa0a34eb2a3d7b2cdd87a8a1f3
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
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.
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.
24 (in-package "SB-PCL")
26 ;;;; ANSI CL condition for unbound slots
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)))))
35 (defmethod wrapper-fetcher ((class standard-class))
36 'std-instance-wrapper)
38 (defmethod slots-fetcher ((class standard-class))
39 'std-instance-slots)
41 (defmethod raw-instance-allocator ((class standard-class))
42 'allocate-standard-instance)
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.
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))
57 (error "unrecognized instance type"))))
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)))
76 (error "unrecognized instance type")))))
78 ;;;; STANDARD-INSTANCE-ACCESS
80 (declaim (inline standard-instance-access (setf standard-instance-access)
81 funcallable-standard-instance-access
82 (setf funcallable-standard-instance-access)))
84 (defun standard-instance-access (instance location)
85 (clos-slots-ref (std-instance-slots instance) location))
87 (defun (setf standard-instance-access) (new-value instance location)
88 (setf (clos-slots-ref (std-instance-slots instance) location) new-value))
90 (defun funcallable-standard-instance-access (instance location)
91 (clos-slots-ref (fsc-instance-slots instance) location))
93 (defun (setf funcallable-standard-instance-access) (new-value instance location)
94 (setf (clos-slots-ref (fsc-instance-slots instance) location) new-value))
96 ;;;; SLOT-VALUE, (SETF SLOT-VALUE), SLOT-BOUNDP, SLOT-MAKUNBOUND
98 (declaim (ftype (sfunction (t symbol) t) slot-value))
99 (defun slot-value (object slot-name)
100 (let* ((wrapper (valid-wrapper-of object))
101 (cell (find-slot-cell wrapper slot-name))
102 (location (car cell))
103 (value
104 (cond ((fixnump location)
105 (if (std-instance-p object)
106 (standard-instance-access object location)
107 (funcallable-standard-instance-access object location)))
108 ((consp location)
109 (cdr location))
110 ((not cell)
111 (return-from slot-value
112 (values (slot-missing (wrapper-class* wrapper) object slot-name
113 'slot-value))))
114 ((not location)
115 (return-from slot-value
116 (slot-value-using-class (wrapper-class* wrapper) object (cddr cell))))
118 (bug "Bogus slot cell in SLOT-VALUE: ~S" cell)))))
119 (if (eq +slot-unbound+ value)
120 (slot-unbound (wrapper-class* wrapper) object slot-name)
121 value)))
123 (define-compiler-macro slot-value (&whole form object slot-name
124 &environment env)
125 (if (and (constantp slot-name env)
126 (interned-symbol-p (constant-form-value slot-name env)))
127 `(accessor-slot-value ,object ,slot-name)
128 form))
130 (defun set-slot-value (object slot-name new-value)
131 (let* ((wrapper (valid-wrapper-of object))
132 (cell (find-slot-cell wrapper slot-name))
133 (location (car cell))
134 (type-check-function (cadr cell)))
135 (when type-check-function
136 (funcall (the function type-check-function) new-value))
137 (cond ((fixnump location)
138 (if (std-instance-p object)
139 (setf (standard-instance-access object location) new-value)
140 (setf (funcallable-standard-instance-access object location)
141 new-value)))
142 ((consp location)
143 (setf (cdr location) new-value))
144 ((not cell)
145 (slot-missing (wrapper-class* wrapper) object slot-name 'setf new-value))
146 ((not location)
147 (setf (slot-value-using-class (wrapper-class* wrapper) object (cddr cell))
148 new-value))
150 (bug "Bogus slot-cell in SET-SLOT-VALUE: ~S" cell))))
151 new-value)
153 ;;; A version of SET-SLOT-VALUE for use in safe code, where we want to
154 ;;; check types when writing to slots:
155 ;;; * Doesn't have an optimizing compiler-macro
156 ;;; * Isn't special-cased in WALK-METHOD-LAMBDA
157 (defun safe-set-slot-value (object slot-name new-value)
158 (set-slot-value object slot-name new-value))
160 (define-compiler-macro set-slot-value (&whole form object slot-name new-value
161 &environment env)
162 (if (and (constantp slot-name env)
163 (interned-symbol-p (constant-form-value slot-name env))
164 ;; We can't use the ACCESSOR-SET-SLOT-VALUE path in safe
165 ;; code, since it'll use the global automatically generated
166 ;; accessor, which won't do typechecking. (SLOT-OBJECT
167 ;; won't have been compiled with SAFETY 3, so SAFE-P will
168 ;; be NIL in MAKE-STD-WRITER-METHOD-FUNCTION).
169 (not (safe-code-p env)))
170 `(accessor-set-slot-value ,object ,slot-name ,new-value)
171 form))
173 (defun slot-boundp (object slot-name)
174 (let* ((wrapper (valid-wrapper-of object))
175 (cell (find-slot-cell wrapper slot-name))
176 (location (car cell))
177 (value
178 (cond ((fixnump location)
179 (if (std-instance-p object)
180 (standard-instance-access object location)
181 (funcallable-standard-instance-access object location)))
182 ((consp location)
183 (cdr location))
184 ((not cell)
185 (return-from slot-boundp
186 (and (slot-missing (wrapper-class* wrapper) object slot-name
187 'slot-boundp)
188 t)))
189 ((not location)
190 (return-from slot-boundp
191 (slot-boundp-using-class (wrapper-class* wrapper) object (cddr cell))))
193 (bug "Bogus slot cell in SLOT-VALUE: ~S" cell)))))
194 (not (eq +slot-unbound+ value))))
196 (define-compiler-macro slot-boundp (&whole form object slot-name
197 &environment env)
198 (if (and (constantp slot-name env)
199 (interned-symbol-p (constant-form-value slot-name env)))
200 `(accessor-slot-boundp ,object ,slot-name)
201 form))
203 (defun slot-makunbound (object slot-name)
204 (let* ((wrapper (valid-wrapper-of object))
205 (cell (find-slot-cell wrapper slot-name))
206 (location (car cell)))
207 (cond ((fixnump location)
208 (if (std-instance-p object)
209 (setf (standard-instance-access object location) +slot-unbound+)
210 (setf (funcallable-standard-instance-access object location)
211 +slot-unbound+)))
212 ((consp location)
213 (setf (cdr location) +slot-unbound+))
214 ((not cell)
215 (slot-missing (wrapper-class* wrapper) object slot-name 'slot-makunbound))
216 ((not location)
217 (slot-makunbound-using-class (wrapper-class* wrapper) object (cddr cell)))
219 (bug "Bogus slot-cell in SLOT-MAKUNBOUND: ~S" cell))))
220 object)
222 (defun slot-exists-p (object slot-name)
223 (let ((class (class-of object)))
224 (not (null (find-slot-definition class slot-name)))))
226 (defvar *unbound-slot-value-marker* (make-unprintable-object "unbound slot"))
228 ;;; This isn't documented, but is used within PCL in a number of print
229 ;;; object methods. (See NAMED-OBJECT-PRINT-FUNCTION.)
230 (defun slot-value-or-default (object slot-name &optional
231 (default *unbound-slot-value-marker*))
232 (if (slot-boundp object slot-name)
233 (slot-value object slot-name)
234 default))
236 (defmethod slot-value-using-class ((class std-class)
237 (object standard-object)
238 (slotd standard-effective-slot-definition))
239 ;; FIXME: Do we need this? SLOT-VALUE checks for obsolete
240 ;; instances. Are users allowed to call this directly?
241 (check-obsolete-instance object)
242 (let* ((location (slot-definition-location slotd))
243 (value
244 (typecase location
245 (fixnum
246 (cond ((std-instance-p object)
247 (clos-slots-ref (std-instance-slots object)
248 location))
249 ((fsc-instance-p object)
250 (clos-slots-ref (fsc-instance-slots object)
251 location))
252 (t (bug "unrecognized instance type in ~S"
253 'slot-value-using-class))))
254 (cons
255 (cdr location))
257 (instance-structure-protocol-error slotd
258 'slot-value-using-class)))))
259 (if (eq value +slot-unbound+)
260 (values (slot-unbound class object (slot-definition-name slotd)))
261 value)))
263 (defmethod (setf slot-value-using-class)
264 (new-value (class std-class)
265 (object standard-object)
266 (slotd standard-effective-slot-definition))
267 ;; FIXME: Do we need this? SET-SLOT-VALUE checks for obsolete
268 ;; instances. Are users allowed to call this directly?
269 (check-obsolete-instance object)
270 (let ((location (slot-definition-location slotd))
271 (type-check-function
272 (when (safe-p class)
273 (slot-definition-type-check-function slotd))))
274 (flet ((check (new-value)
275 (when type-check-function
276 (funcall (the function type-check-function) new-value))
277 new-value))
278 (typecase location
279 (fixnum
280 (cond ((std-instance-p object)
281 (setf (clos-slots-ref (std-instance-slots object) location)
282 (check new-value)))
283 ((fsc-instance-p object)
284 (setf (clos-slots-ref (fsc-instance-slots object) location)
285 (check new-value)))
286 (t (bug "unrecognized instance type in ~S"
287 '(setf slot-value-using-class)))))
288 (cons
289 (setf (cdr location) (check new-value)))
291 (instance-structure-protocol-error
292 slotd '(setf slot-value-using-class)))))))
294 (defmethod slot-boundp-using-class
295 ((class std-class)
296 (object standard-object)
297 (slotd standard-effective-slot-definition))
298 ;; FIXME: Do we need this? SLOT-BOUNDP checks for obsolete
299 ;; instances. Are users allowed to call this directly?
300 (check-obsolete-instance object)
301 (let* ((location (slot-definition-location slotd))
302 (value
303 (typecase location
304 (fixnum
305 (cond ((std-instance-p object)
306 (clos-slots-ref (std-instance-slots object)
307 location))
308 ((fsc-instance-p object)
309 (clos-slots-ref (fsc-instance-slots object)
310 location))
311 (t (bug "unrecognized instance type in ~S"
312 'slot-boundp-using-class))))
313 (cons
314 (cdr location))
316 (instance-structure-protocol-error slotd
317 'slot-boundp-using-class)))))
318 (not (eq value +slot-unbound+))))
320 (defmethod slot-makunbound-using-class
321 ((class std-class)
322 (object standard-object)
323 (slotd standard-effective-slot-definition))
324 (check-obsolete-instance object)
325 (let ((location (slot-definition-location slotd)))
326 (typecase location
327 (fixnum
328 (cond ((std-instance-p object)
329 (setf (clos-slots-ref (std-instance-slots object) location)
330 +slot-unbound+))
331 ((fsc-instance-p object)
332 (setf (clos-slots-ref (fsc-instance-slots object) location)
333 +slot-unbound+))
334 (t (bug "unrecognized instance type in ~S"
335 'slot-makunbound-using-class))))
336 (cons
337 (setf (cdr location) +slot-unbound+))
339 (instance-structure-protocol-error slotd
340 'slot-makunbound-using-class))))
341 object)
343 (defmethod slot-value-using-class
344 ((class condition-class)
345 (object condition)
346 (slotd condition-effective-slot-definition))
347 (let ((fun (slot-definition-reader-function slotd)))
348 (declare (type function fun))
349 (funcall fun object)))
351 (defmethod (setf slot-value-using-class)
352 (new-value
353 (class condition-class)
354 (object condition)
355 (slotd condition-effective-slot-definition))
356 (let ((fun (slot-definition-writer-function slotd)))
357 (declare (type function fun))
358 (funcall fun new-value object)))
360 (defmethod slot-boundp-using-class
361 ((class condition-class)
362 (object condition)
363 (slotd condition-effective-slot-definition))
364 (let ((fun (slot-definition-boundp-function slotd)))
365 (declare (type function fun))
366 (funcall fun object)))
368 (defmethod slot-makunbound-using-class ((class condition-class) object slot)
369 (error "attempt to unbind slot ~S in condition object ~S."
370 slot object))
372 (defmethod slot-value-using-class
373 ((class structure-class)
374 (object structure-object)
375 (slotd structure-effective-slot-definition))
376 (let* ((function (slot-definition-internal-reader-function slotd))
377 (value (funcall function object)))
378 (declare (type function function))
379 ;; FIXME: Is this really necessary? Structure slots should surely
380 ;; never be unbound!
381 (if (eq value +slot-unbound+)
382 (values (slot-unbound class object (slot-definition-name slotd)))
383 value)))
385 (defmethod (setf slot-value-using-class)
386 (new-value (class structure-class)
387 (object structure-object)
388 (slotd structure-effective-slot-definition))
389 (let ((function (slot-definition-internal-writer-function slotd)))
390 (declare (type function function))
391 (funcall function new-value object)))
393 (defmethod slot-boundp-using-class
394 ((class structure-class)
395 (object structure-object)
396 (slotd structure-effective-slot-definition))
399 (defmethod slot-makunbound-using-class
400 ((class structure-class)
401 (object structure-object)
402 (slotd structure-effective-slot-definition))
403 (error "Structure slots can't be unbound."))
405 (defmethod slot-missing
406 ((class t) instance slot-name operation &optional new-value)
407 (error "~@<When attempting to ~A, the slot ~S is missing from the ~
408 object ~S.~@:>"
409 (ecase operation
410 (slot-value "read the slot's value (slot-value)")
411 (setf (format nil
412 "set the slot's value to ~S (SETF of SLOT-VALUE)"
413 new-value))
414 (slot-boundp "test to see whether slot is bound (SLOT-BOUNDP)")
415 (slot-makunbound "make the slot unbound (SLOT-MAKUNBOUND)"))
416 slot-name
417 instance))
419 (defmethod slot-unbound ((class t) instance slot-name)
420 (restart-case
421 (error 'unbound-slot :name slot-name :instance instance)
422 (use-value (v)
423 :report "Return a value as the slot-value."
424 :interactive read-evaluated-form
426 (store-value (v)
427 :report "Store and return a value as the slot-value."
428 :interactive read-evaluated-form
429 (setf (slot-value instance slot-name) v))))
431 (defun slot-unbound-internal (instance position)
432 (values
433 (slot-unbound
434 (class-of instance)
435 instance
436 (etypecase position
437 (fixnum
438 (nth position (wrapper-instance-slots-layout (wrapper-of instance))))
439 (cons
440 (car position))))))
442 ;;; FIXME: AMOP says that allocate-instance imples finalize-inheritance
443 ;;; if the class is not yet finalized, but we don't seem to be taking
444 ;;; care of this for non-standard-classes.x
445 (defmethod allocate-instance ((class standard-class) &rest initargs)
446 (declare (ignore initargs))
447 (unless (class-finalized-p class)
448 (finalize-inheritance class))
449 (allocate-standard-instance (class-wrapper class)))
451 (defmethod allocate-instance ((class structure-class) &rest initargs)
452 (declare (ignore initargs))
453 (let ((constructor (class-defstruct-constructor class)))
454 (if constructor
455 (funcall constructor)
456 (allocate-standard-instance (class-wrapper class)))))
458 ;;; FIXME: It would be nicer to have allocate-instance return
459 ;;; uninitialized objects for conditions as well.
460 (defmethod allocate-instance ((class condition-class) &rest initargs)
461 (declare (ignore initargs))
462 (make-condition (class-name class)))
464 (defmethod allocate-instance ((class built-in-class) &rest initargs)
465 (declare (ignore initargs))
466 (error "Cannot allocate an instance of ~S." class)) ; So sayeth AMOP