1 ;;;; This software is part of the SBCL system. See the README file for
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
10 ;;;; copyright information from original PCL sources:
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
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
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
26 (defun ensure-accessor (type fun-name slot-name)
27 (unless (fboundp fun-name)
28 (multiple-value-bind (lambda-list specializers method-class initargs doc)
30 ;; FIXME: change SLOT-OBJECT here to T to get SLOT-MISSING
31 ;; behaviour for non-slot-objects too?
33 (values '(object) '(slot-object) 'standard-reader-method
34 (make-std-reader-method-function 'slot-object slot-name)
35 "automatically-generated reader method"))
37 (values '(new-value object) '(t slot-object) 'standard-writer-method
38 (make-std-writer-method-function 'slot-object slot-name)
39 "automatically-generated writer method"))
41 (values '(object) '(slot-object) 'standard-boundp-method
42 (make-std-boundp-method-function 'slot-object slot-name)
43 "automatically-generated boundp method")))
44 (let ((gf (ensure-generic-function fun-name :lambda-list lambda-list)))
45 (add-method gf (make-a-method method-class () lambda-list specializers
46 initargs doc slot-name)))))
49 (defmacro accessor-slot-value (object slot-name)
50 (aver (constantp slot-name))
51 (let* ((slot-name (eval slot-name))
52 (reader-name (slot-reader-name slot-name)))
53 `(let ((.ignore. (load-time-value
54 (ensure-accessor 'reader ',reader-name ',slot-name))))
55 (declare (ignore .ignore.))
56 (truly-the (values t &optional)
57 (funcall #',reader-name ,object)))))
59 (defmacro accessor-set-slot-value (object slot-name new-value &environment env)
60 (aver (constantp slot-name))
61 (setq object (macroexpand object env))
62 (setq slot-name (macroexpand slot-name env))
63 (let* ((slot-name (eval slot-name))
64 (bindings (unless (or (constantp new-value) (atom new-value))
65 (let ((object-var (gensym)))
66 (prog1 `((,object-var ,object))
67 (setq object object-var)))))
68 (writer-name (slot-writer-name slot-name))
72 (ensure-accessor 'writer ',writer-name ',slot-name)))
73 (.new-value. ,new-value))
74 (declare (ignore .ignore.))
75 (funcall #',writer-name .new-value. ,object)
78 `(let ,bindings ,form)
81 (defmacro accessor-slot-boundp (object slot-name)
82 (aver (constantp slot-name))
83 (let* ((slot-name (eval slot-name))
84 (boundp-name (slot-boundp-name slot-name)))
85 `(let ((.ignore. (load-time-value
86 (ensure-accessor 'boundp ',boundp-name ',slot-name))))
87 (declare (ignore .ignore.))
88 (funcall #',boundp-name ,object))))
90 (defun make-structure-slot-boundp-function (slotd)
91 (declare (ignore slotd))
93 (declare (ignore object))
96 (define-condition instance-structure-protocol-error
97 (reference-condition error)
98 ((slotd :initarg :slotd :reader instance-structure-protocol-error-slotd)
99 (fun :initarg :fun :reader instance-structure-protocol-error-fun))
102 (format s "~@<The slot ~S has neither ~S nor ~S ~
103 allocation, so it can't be ~A by the default ~
105 (instance-structure-protocol-error-slotd c)
108 ((member (instance-structure-protocol-error-fun c)
109 '(slot-value-using-class slot-boundp-using-class))
112 (instance-structure-protocol-error-fun c)))))
114 (defun instance-structure-protocol-error (slotd fun)
115 (error 'instance-structure-protocol-error
116 :slotd slotd :fun fun
117 :references (list `(:amop :generic-function ,fun)
118 '(:amop :section (5 5 3)))))
120 (defun get-optimized-std-accessor-method-function (class slotd name)
122 ((structure-class-p class)
124 (reader (slot-definition-internal-reader-function slotd))
125 (writer (slot-definition-internal-writer-function slotd))
126 (boundp (make-structure-slot-boundp-function slotd))))
127 ((condition-class-p class)
129 (reader (slot-definition-reader-function slotd))
130 (writer (slot-definition-writer-function slotd))
131 (boundp (slot-definition-boundp-function slotd))))
133 (let* ((fsc-p (cond ((standard-class-p class) nil)
134 ((funcallable-standard-class-p class) t)
136 ;; Shouldn't be using the optimized-std-accessors
138 #+nil (format t "* warning: ~S ~S~% ~S~%"
141 (t (error "~S is not a STANDARD-CLASS." class))))
142 (slot-name (slot-definition-name slotd))
143 (location (slot-definition-location slotd))
144 (function (ecase name
145 (reader #'make-optimized-std-reader-method-function)
146 (writer #'make-optimized-std-writer-method-function)
147 (boundp #'make-optimized-std-boundp-method-function)))
148 ;; KLUDGE: we need this slightly hacky calling convention
149 ;; for these functions for bootstrapping reasons: see
150 ;; !BOOTSTRAP-MAKE-SLOT-DEFINITION in braid.lisp. -- CSR,
152 (value (funcall function fsc-p slotd slot-name location)))
153 (declare (type function function))
154 (values value (slot-definition-location slotd))))))
156 (defun make-optimized-std-reader-method-function
157 (fsc-p slotd slot-name location)
158 (declare #.*optimize-speed*)
164 (check-obsolete-instance instance)
165 (let ((value (clos-slots-ref (fsc-instance-slots instance)
167 (if (eq value +slot-unbound+)
169 (slot-unbound (class-of instance) instance slot-name))
172 (check-obsolete-instance instance)
173 (let ((value (clos-slots-ref (std-instance-slots instance)
175 (if (eq value +slot-unbound+)
177 (slot-unbound (class-of instance) instance slot-name))
181 (check-obsolete-instance instance)
182 (let ((value (cdr location)))
183 (if (eq value +slot-unbound+)
184 (values (slot-unbound (class-of instance) instance slot-name))
188 (instance-structure-protocol-error slotd 'slot-value-using-class))))
189 `(reader ,slot-name)))
191 (defun make-optimized-std-writer-method-function
192 (fsc-p slotd slot-name location)
193 (declare #.*optimize-speed*)
197 (lambda (nv instance)
198 (check-obsolete-instance instance)
199 (setf (clos-slots-ref (fsc-instance-slots instance)
202 (lambda (nv instance)
203 (check-obsolete-instance instance)
204 (setf (clos-slots-ref (std-instance-slots instance)
207 (cons (lambda (nv instance)
208 (check-obsolete-instance instance)
209 (setf (cdr location) nv)))
211 (lambda (nv instance)
212 (declare (ignore nv))
213 (instance-structure-protocol-error slotd
214 '(setf slot-value-using-class)))))
215 `(writer ,slot-name)))
217 (defun make-optimized-std-boundp-method-function
218 (fsc-p slotd slot-name location)
219 (declare #.*optimize-speed*)
224 (check-obsolete-instance instance)
225 (not (eq (clos-slots-ref (fsc-instance-slots instance)
229 (check-obsolete-instance instance)
230 (not (eq (clos-slots-ref (std-instance-slots instance)
233 (cons (lambda (instance)
234 (check-obsolete-instance instance)
235 (not (eq (cdr location) +slot-unbound+))))
238 (instance-structure-protocol-error slotd 'slot-boundp-using-class))))
239 `(boundp ,slot-name)))
241 (defun make-optimized-structure-slot-value-using-class-method-function
243 (declare (type function function))
244 (lambda (class object slotd)
245 (declare (ignore class slotd))
246 (funcall function object)))
248 (defun make-optimized-structure-setf-slot-value-using-class-method-function
250 (declare (type function function))
251 (lambda (nv class object slotd)
252 (declare (ignore class slotd))
253 (funcall function nv object)))
255 (defun make-optimized-structure-slot-boundp-using-class-method-function ()
256 (lambda (class object slotd)
257 (declare (ignore class object slotd))
260 (defun get-optimized-std-slot-value-using-class-method-function
263 ((structure-class-p class)
265 (reader (make-optimized-structure-slot-value-using-class-method-function
266 (slot-definition-internal-reader-function slotd)))
267 (writer (make-optimized-structure-setf-slot-value-using-class-method-function
268 (slot-definition-internal-writer-function slotd)))
269 (boundp (make-optimized-structure-slot-boundp-using-class-method-function))))
270 ((condition-class-p class)
273 (let ((fun (slot-definition-reader-function slotd)))
274 (declare (type function fun))
275 (lambda (class object slotd)
276 (declare (ignore class slotd))
277 (funcall fun object))))
279 (let ((fun (slot-definition-writer-function slotd)))
280 (declare (type function fun))
281 (lambda (new-value class object slotd)
282 (declare (ignore class slotd))
283 (funcall fun new-value object))))
285 (let ((fun (slot-definition-boundp-function slotd)))
286 (declare (type function fun))
287 (lambda (class object slotd)
288 (declare (ignore class slotd))
289 (funcall fun object))))))
291 (let* ((fsc-p (cond ((standard-class-p class) nil)
292 ((funcallable-standard-class-p class) t)
293 (t (error "~S is not a standard-class" class))))
297 #'make-optimized-std-slot-value-using-class-method-function)
299 #'make-optimized-std-setf-slot-value-using-class-method-function)
301 #'make-optimized-std-slot-boundp-using-class-method-function))))
302 (declare (type function function))
303 (values (funcall function fsc-p slotd)
304 (slot-definition-location slotd))))))
306 (defun make-optimized-std-slot-value-using-class-method-function (fsc-p slotd)
307 (declare #.*optimize-speed*)
308 (let ((location (slot-definition-location slotd))
309 (slot-name (slot-definition-name slotd)))
312 (lambda (class instance slotd)
313 (declare (ignore slotd))
314 (check-obsolete-instance instance)
315 (let ((value (clos-slots-ref (fsc-instance-slots instance)
317 (if (eq value +slot-unbound+)
318 (values (slot-unbound class instance slot-name))
320 (lambda (class instance slotd)
321 (declare (ignore slotd))
322 (check-obsolete-instance instance)
323 (let ((value (clos-slots-ref (std-instance-slots instance)
325 (if (eq value +slot-unbound+)
326 (values (slot-unbound class instance slot-name))
328 (cons (lambda (class instance slotd)
329 (declare (ignore slotd))
330 (check-obsolete-instance instance)
331 (let ((value (cdr location)))
332 (if (eq value +slot-unbound+)
333 (values (slot-unbound class instance slot-name))
336 (lambda (class instance slotd)
337 (declare (ignore class instance))
338 (instance-structure-protocol-error slotd 'slot-value-using-class))))))
340 (defun make-optimized-std-setf-slot-value-using-class-method-function
342 (declare #.*optimize-speed*)
343 (let ((location (slot-definition-location slotd)))
347 (lambda (nv class instance slotd)
348 (declare (ignore class slotd))
349 (check-obsolete-instance instance)
350 (setf (clos-slots-ref (fsc-instance-slots instance) location)
352 (lambda (nv class instance slotd)
353 (declare (ignore class slotd))
354 (check-obsolete-instance instance)
355 (setf (clos-slots-ref (std-instance-slots instance) location)
357 (cons (lambda (nv class instance slotd)
358 (declare (ignore class slotd))
359 (check-obsolete-instance instance)
360 (setf (cdr location) nv)))
361 (null (lambda (nv class instance slotd)
362 (declare (ignore nv class instance))
363 (instance-structure-protocol-error
364 slotd '(setf slot-value-using-class)))))))
366 (defun make-optimized-std-slot-boundp-using-class-method-function
368 (declare #.*optimize-speed*)
369 (let ((location (slot-definition-location slotd)))
373 (lambda (class instance slotd)
374 (declare (ignore class slotd))
375 (check-obsolete-instance instance)
376 (not (eq (clos-slots-ref (fsc-instance-slots instance) location)
378 (lambda (class instance slotd)
379 (declare (ignore class slotd))
380 (check-obsolete-instance instance)
381 (not (eq (clos-slots-ref (std-instance-slots instance) location)
383 (cons (lambda (class instance slotd)
384 (declare (ignore class slotd))
385 (check-obsolete-instance instance)
386 (not (eq (cdr location) +slot-unbound+))))
388 (lambda (class instance slotd)
389 (declare (ignore class instance))
390 (instance-structure-protocol-error slotd
391 'slot-boundp-using-class))))))
393 (defun get-accessor-from-svuc-method-function (class slotd sdfun name)
394 (macrolet ((emf-funcall (emf &rest args)
395 `(invoke-effective-method-function ,emf nil ,@args)))
398 (reader (lambda (instance)
399 (emf-funcall sdfun class instance slotd)))
400 (writer (lambda (nv instance)
401 (emf-funcall sdfun nv class instance slotd)))
402 (boundp (lambda (instance)
403 (emf-funcall sdfun class instance slotd))))
404 `(,name ,(class-name class) ,(slot-definition-name slotd)))))
406 (defun make-internal-reader-method-function (class-name slot-name)
407 (list* :method-spec `(internal-reader-method ,class-name ,slot-name)
408 (make-method-function
410 (let ((wrapper (get-instance-wrapper-or-nil instance)))
412 (let* ((class (wrapper-class* wrapper))
413 (index (or (instance-slot-index wrapper slot-name)
415 (wrapper-class-slots wrapper)))))
418 (let ((value (clos-slots-ref (get-slots instance)
420 (if (eq value +slot-unbound+)
421 (values (slot-unbound (class-of instance)
426 (let ((value (cdr index)))
427 (if (eq value +slot-unbound+)
428 (values (slot-unbound (class-of instance)
433 (error "~@<The wrapper for class ~S does not have ~
436 (slot-value instance slot-name)))))))
438 (defun make-std-reader-method-function (class-name slot-name)
439 (let* ((pv-table-symbol (gensym))
441 (make-method-function
443 (pv-binding1 (.pv. .calls.
444 (symbol-value pv-table-symbol)
445 (instance) (instance-slots))
446 (instance-read-internal
447 .pv. instance-slots 1
448 (slot-value instance slot-name))))))))
449 (setf (getf (getf initargs :plist) :slot-name-lists)
450 (list (list nil slot-name)))
451 (setf (getf (getf initargs :plist) :pv-table-symbol) pv-table-symbol)
452 (list* :method-spec `(reader-method ,class-name ,slot-name)
455 (defun make-std-writer-method-function (class-name slot-name)
456 (let* ((pv-table-symbol (gensym))
458 (make-method-function
459 (lambda (nv instance)
460 (pv-binding1 (.pv. .calls.
461 (symbol-value pv-table-symbol)
462 (instance) (instance-slots))
463 (instance-write-internal
464 .pv. instance-slots 1 nv
465 (setf (slot-value instance slot-name) nv))))))))
466 (setf (getf (getf initargs :plist) :slot-name-lists)
467 (list nil (list nil slot-name)))
468 (setf (getf (getf initargs :plist) :pv-table-symbol) pv-table-symbol)
469 (list* :method-spec `(writer-method ,class-name ,slot-name)
472 (defun make-std-boundp-method-function (class-name slot-name)
473 (let* ((pv-table-symbol (gensym))
475 (make-method-function
477 (pv-binding1 (.pv. .calls.
478 (symbol-value pv-table-symbol)
479 (instance) (instance-slots))
480 (instance-boundp-internal
481 .pv. instance-slots 1
482 (slot-boundp instance slot-name))))))))
483 (setf (getf (getf initargs :plist) :slot-name-lists)
484 (list (list nil slot-name)))
485 (setf (getf (getf initargs :plist) :pv-table-symbol) pv-table-symbol)
486 (list* :method-spec `(boundp-method ,class-name ,slot-name)