| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: CLOS -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; Copyright (c) 1992, Giuseppe Attardi.o |
| 6 |
;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll. |
| 7 |
;;;; |
| 8 |
;;;; See file 'LICENSE' for the copyright details. |
| 9 |
|
| 10 |
(in-package "CLOS") |
| 11 |
|
| 12 |
;;; ---------------------------------------------------------------------- |
| 13 |
;;; ACCESSOR / READER / WRITER METHOD CREATION |
| 14 |
;;; |
| 15 |
;;; The following code creates optimized and unoptimized versions of the |
| 16 |
;;; slot accessors defined for a class. They are designed so that at least |
| 17 |
;;; some varieties work at boot time. |
| 18 |
;;; |
| 19 |
|
| 20 |
(defun safe-slot-definition-location (slotd &optional default) |
| 21 |
(declare (ignore default)) |
| 22 |
(cond ((listp slotd) |
| 23 |
(error "List instead of a slot definition object")) |
| 24 |
(t |
| 25 |
(slot-value slotd 'location)))) |
| 26 |
|
| 27 |
(defun std-class-sealed-accessors (index) |
| 28 |
(declare (si::c-local) |
| 29 |
(fixnum index)) |
| 30 |
(values #'(lambda (self) |
| 31 |
(declare (optimize (safety 0) (speed 3) (debug 0)) |
| 32 |
(standard-object self)) |
| 33 |
(ensure-up-to-date-instance self) |
| 34 |
(safe-instance-ref self index)) |
| 35 |
#'(lambda (value self) |
| 36 |
(declare (optimize (safety 0) (speed 3) (debug 0)) |
| 37 |
(standard-object self)) |
| 38 |
(ensure-up-to-date-instance self) |
| 39 |
(si:instance-set self index value)))) |
| 40 |
|
| 41 |
(defun std-class-accessors (slot-name) |
| 42 |
(declare (si::c-local)) |
| 43 |
;; The following are very slow. We do not optimize for the slot position. |
| 44 |
(values #'(lambda (self) |
| 45 |
(slot-value self slot-name)) |
| 46 |
#'(lambda (value self) |
| 47 |
(setf (slot-value self slot-name) value)))) |
| 48 |
|
| 49 |
(defun safe-add-method (name method) |
| 50 |
;; Adds a method to a function which might have been previously defined |
| 51 |
;; as non-generic, without breaking the function |
| 52 |
(cond ((or *clos-booted* |
| 53 |
(not (fboundp name)) |
| 54 |
(si::instancep (fdefinition name))) |
| 55 |
(add-method (ensure-generic-function name) method)) |
| 56 |
(t |
| 57 |
(let* ((alt-name '#:foo) |
| 58 |
(gf (ensure-generic-function alt-name))) |
| 59 |
(add-method gf method) |
| 60 |
(setf (generic-function-name gf) name) |
| 61 |
(setf (fdefinition name) gf) |
| 62 |
(fmakunbound alt-name))))) |
| 63 |
|
| 64 |
(defun std-class-generate-accessors (standard-class &optional (optimizable t)) |
| 65 |
;; |
| 66 |
;; The accessors are closures, which are generated every time the |
| 67 |
;; slots of the class change. The accessors are safe: they check that |
| 68 |
;; the slot is bound after retreiving the value, and they may take |
| 69 |
;; the liberty of using SI:INSTANCE-REF because they know the class of |
| 70 |
;; the instance. |
| 71 |
;; |
| 72 |
(dolist (slotd (slot-value standard-class 'direct-slots)) |
| 73 |
(with-slots ((name name) (allocation allocation) (location location) |
| 74 |
(readers readers) (writers writers)) |
| 75 |
slotd |
| 76 |
;; When a class is of a specified class in the MOP (such as |
| 77 |
;; STANDARD-CLASS), then the user may not write any method |
| 78 |
;; around SLOT-VALUE-USING-CLASS. This allows us to write |
| 79 |
;; optimized versions of the accessors. |
| 80 |
(unless (member (slot-value standard-class 'name) |
| 81 |
'(standard-class |
| 82 |
funcallable-standard-class |
| 83 |
structure-class)) |
| 84 |
(setf optimizable nil)) |
| 85 |
(multiple-value-bind (reader writer) |
| 86 |
(cond ((and optimizable |
| 87 |
(eq allocation :instance) |
| 88 |
;; This is an extension by ECL in which a direct slot |
| 89 |
;; definition specifies the location of a slot. It |
| 90 |
;; only happens for sealed classes. |
| 91 |
(typep location 'fixnum)) |
| 92 |
(std-class-sealed-accessors location)) |
| 93 |
(t |
| 94 |
(std-class-accessors name))) |
| 95 |
(let* ((options (list :slot-definition slotd)) |
| 96 |
(reader-args (list* :function reader |
| 97 |
:generic-function nil |
| 98 |
:qualifiers nil |
| 99 |
:lambda-list '(object) |
| 100 |
:specializers `(,standard-class) |
| 101 |
options)) |
| 102 |
(reader-class (if (boundp '*early-methods*) |
| 103 |
'standard-reader-method |
| 104 |
(apply #'reader-method-class standard-class slotd |
| 105 |
reader-args))) |
| 106 |
(writer-args (list* :function writer |
| 107 |
:generic-function nil |
| 108 |
:qualifiers nil |
| 109 |
:lambda-list '(value object) |
| 110 |
:specializers `(,(find-class t) ,standard-class) |
| 111 |
options)) |
| 112 |
(writer-class (if (boundp '*early-methods*) |
| 113 |
'standard-writer-method |
| 114 |
(apply #'writer-method-class standard-class slotd |
| 115 |
writer-args)))) |
| 116 |
(dolist (fname readers) |
| 117 |
(let ((method (make-method reader-class nil `(,standard-class) '(self) |
| 118 |
(wrapped-method-function reader) |
| 119 |
options))) |
| 120 |
(safe-add-method fname method) |
| 121 |
;; This is redundant, but we need it at boot time because |
| 122 |
;; the early MAKE-METHOD does not use the options field. |
| 123 |
(unless *clos-booted* |
| 124 |
(setf (slot-value method 'slot-definition) slotd)))) |
| 125 |
(dolist (fname writers) |
| 126 |
(let ((method (make-method writer-class nil |
| 127 |
`(,(find-class t) ,standard-class) '(value self) |
| 128 |
(wrapped-method-function writer) |
| 129 |
options))) |
| 130 |
(safe-add-method fname method) |
| 131 |
;; This is redundant, but we need it at boot time because |
| 132 |
;; the early MAKE-METHOD does not use the options field. |
| 133 |
(unless *clos-booted* |
| 134 |
(setf (slot-value method 'slot-definition) slotd))))))))) |
| 135 |
|
| 136 |
(defun reader-closure (index) |
| 137 |
(declare (si::c-local)) |
| 138 |
(lambda (object) (si::instance-ref object index))) |
| 139 |
|
| 140 |
(defun writer-closure (index) |
| 141 |
(declare (si::c-local)) |
| 142 |
(lambda (value object) (si::instance-set object index value))) |
| 143 |
|
| 144 |
(labels ((generate-accessors (class) |
| 145 |
(declare (optimize speed (safety 0))) |
| 146 |
(if (typep class 'std-class) |
| 147 |
(std-class-generate-accessors class t) |
| 148 |
(loop for slotd in (slot-value class 'slots) |
| 149 |
for index = (slot-value slotd 'location) |
| 150 |
do (loop for reader in (slot-value slotd 'readers) |
| 151 |
do (setf (fdefinition reader) (reader-closure index))) |
| 152 |
do (loop for writer in (slot-value slotd 'writers) |
| 153 |
do (setf (fdefinition writer) (writer-closure index))))) |
| 154 |
(mapc #'generate-accessors (slot-value class 'direct-subclasses)))) |
| 155 |
(generate-accessors +the-t-class+)) |