| 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 |
(eval-when (:compile-toplevel :execute) |
| 13 |
(load "src:clos;hierarchy.lsp")) |
| 14 |
|
| 15 |
;;; ---------------------------------------------------------------------- |
| 16 |
;;; SLOTS READING AND WRITING |
| 17 |
;;; |
| 18 |
;;; Functional and macro interface for accessing the slots of an instance. |
| 19 |
;;; This interface is defined with specialization for classes that ECL |
| 20 |
;;; knows of such as standard classes and funcallable standard class. |
| 21 |
;;; This is needed to avoid circularity in compute-applicable-methods, |
| 22 |
;;; which needs the slot values and thus cannot go through a dispatch |
| 23 |
;;; itself. |
| 24 |
;;; |
| 25 |
;;; Note that using SLOT-VALUE or specialized versions of it is not |
| 26 |
;;; wrong because the MOP enforces various restrictions on portable |
| 27 |
;;; code: |
| 28 |
;;; 1) Accessors must behave as SLOT-VALUE |
| 29 |
;;; 2) In particular, any method defined by the user must be |
| 30 |
;;; specialized on at least one non-specified class. This means |
| 31 |
;;; that the user cannot change the behavoir of SLOT-VALUE for |
| 32 |
;;; standard classes. |
| 33 |
;;; |
| 34 |
;;; First of all we define WITH-SLOTS because it is going to be useful |
| 35 |
;;; for enforcing the use of SLOT-VALUE and not of accessors |
| 36 |
;;; throughout the bootstrap code. |
| 37 |
;;; |
| 38 |
(defmacro with-slots (slot-entries instance-form &body body) |
| 39 |
(let* ((temp (gensym)) |
| 40 |
(accessors |
| 41 |
(do ((scan slot-entries (cdr scan)) |
| 42 |
(res)) |
| 43 |
((null scan) (nreverse res)) |
| 44 |
(if (symbolp (first scan)) |
| 45 |
(push `(,(first scan) (slot-value ,temp ',(first scan))) res) |
| 46 |
(push `(,(caar scan) |
| 47 |
(slot-value ,temp ',(cadar scan))) res))))) |
| 48 |
`(let ((,temp ,instance-form)) |
| 49 |
(symbol-macrolet ,accessors ,@body)))) |
| 50 |
|
| 51 |
;;; |
| 52 |
;;; The following macro is a convenience that can be used to directly |
| 53 |
;;; access the slots of a class based on their s-form description. It |
| 54 |
;;; is used internally by ECL during bootstrap. Unlike WITH-SLOTS, |
| 55 |
;;; the macros directly access the slots by index. |
| 56 |
;;; |
| 57 |
(eval-when (:compile-toplevel :execute) |
| 58 |
(defmacro with-early-accessors ((&rest slot-definitions) &rest body) |
| 59 |
(let (functions) |
| 60 |
(loop for slots in slot-definitions |
| 61 |
do (loop for (name . slotd) in (if (symbolp slots) |
| 62 |
(symbol-value slots) |
| 63 |
slots) |
| 64 |
for index from 0 |
| 65 |
for accessor = (getf slotd :accessor) |
| 66 |
when (and accessor (not (member accessor functions :key #'first))) |
| 67 |
do (push `(,accessor (object) (si::instance-ref object ,index)) functions) |
| 68 |
(push `((setf ,accessor) (value object) (si::instance-set object ,index value)) functions))) |
| 69 |
`(flet ,functions |
| 70 |
(declare (inline ,@(mapcar #'first functions))) |
| 71 |
,@body)))) |
| 72 |
|
| 73 |
;;; |
| 74 |
;;; The following macro is also used at bootstap for instantiating |
| 75 |
;;; a class based only on the s-form description. |
| 76 |
;;; |
| 77 |
(eval-when (:compile-toplevel :execute) |
| 78 |
(defmacro with-early-make-instance (slots (object class &rest key-value-pairs) |
| 79 |
&rest body) |
| 80 |
(when (symbolp slots) |
| 81 |
(setf slots (symbol-value slots))) |
| 82 |
`(let* ((%class ,class) |
| 83 |
(,object (si::allocate-raw-instance nil %class |
| 84 |
,(length slots)))) |
| 85 |
(declare (type standard-object ,object)) |
| 86 |
,@(flet ((initializerp (name list) |
| 87 |
(not (eq (getf list name 'wrong) 'wrong)))) |
| 88 |
(loop for (name . slotd) in slots |
| 89 |
for initarg = (getf slotd :initarg) |
| 90 |
for initform = (getf slotd :initform (si::unbound)) |
| 91 |
for initvalue = (getf key-value-pairs initarg) |
| 92 |
for index from 0 |
| 93 |
do (cond ((and initarg (initializerp initarg key-value-pairs)) |
| 94 |
(setf initform (getf key-value-pairs initarg))) |
| 95 |
((initializerp name key-value-pairs) |
| 96 |
(setf initform (getf key-value-pairs name)))) |
| 97 |
when (si:sl-boundp initform) |
| 98 |
collect `(si::instance-set ,object ,index ,initform))) |
| 99 |
(when %class |
| 100 |
(si::instance-sig-set ,object)) |
| 101 |
(with-early-accessors (,slots) |
| 102 |
,@body)))) |
| 103 |
|
| 104 |
;;; |
| 105 |
;;; ECL classes store slots in a hash table for faster access. The |
| 106 |
;;; following functions create the cache and allow us to locate the |
| 107 |
;;; slots rapidly. |
| 108 |
;;; |
| 109 |
(defun std-create-slots-table (class) |
| 110 |
(with-slots ((all-slots slots) |
| 111 |
(slot-table slot-table) |
| 112 |
(location-table location-table)) |
| 113 |
class |
| 114 |
(let* ((size (max 32 (* 2 (length all-slots)))) |
| 115 |
(table (make-hash-table :size size))) |
| 116 |
(dolist (slotd all-slots) |
| 117 |
(setf (gethash (slot-definition-name slotd) table) slotd)) |
| 118 |
(let ((metaclass (si::instance-class class)) |
| 119 |
(locations nil)) |
| 120 |
(when (or (eq metaclass (find-class 'standard-class)) |
| 121 |
(eq metaclass (find-class 'funcallable-standard-class)) |
| 122 |
(eq metaclass (find-class 'structure-class))) |
| 123 |
(setf locations (make-hash-table :size size)) |
| 124 |
(dolist (slotd all-slots) |
| 125 |
(setf (gethash (slot-definition-name slotd) locations) |
| 126 |
(slot-definition-location slotd)))) |
| 127 |
(setf slot-table table |
| 128 |
location-table locations))))) |
| 129 |
|
| 130 |
(defun find-slot-definition (class slot-name) |
| 131 |
(with-slots ((slots slots) (slot-table slot-table)) |
| 132 |
class |
| 133 |
(if (or (eq (si:instance-class class) +the-standard-class+) |
| 134 |
(eq (si:instance-class class) +the-funcallable-standard-class+)) |
| 135 |
(gethash slot-name slot-table nil) |
| 136 |
(find slot-name slots :key #'slot-definition-name)))) |
| 137 |
|
| 138 |
;;; |
| 139 |
;;; INSTANCE UPDATE PREVIOUS |
| 140 |
;;; |
| 141 |
(eval-when (:compile-toplevel :execute) |
| 142 |
(defmacro ensure-up-to-date-instance (instance) |
| 143 |
`(let ((instance ,instance)) |
| 144 |
(declare (:read-only instance)) |
| 145 |
(when (si::instance-obsolete-p instance) |
| 146 |
(update-instance instance))))) |
| 147 |
|
| 148 |
(defun update-instance (x) |
| 149 |
(si::instance-sig-set x)) |
| 150 |
(declaim (notinline update-instance)) |
| 151 |
|
| 152 |
;;; |
| 153 |
;;; STANDARD-CLASS INTERFACE |
| 154 |
;;; |
| 155 |
;;; Specific functions for slot reading, writing, boundness checking, etc. |
| 156 |
;;; |
| 157 |
|
| 158 |
(defun standard-instance-access (instance location) |
| 159 |
(with-early-accessors (+standard-class-slots+ |
| 160 |
+slot-definition-slots+) |
| 161 |
(ensure-up-to-date-instance instance) |
| 162 |
(cond ((ext:fixnump location) |
| 163 |
;; local slot |
| 164 |
(si:instance-ref instance (truly-the fixnum location))) |
| 165 |
((consp location) |
| 166 |
;; shared slot |
| 167 |
(car location)) |
| 168 |
(t |
| 169 |
(invalid-slot-location instance location))))) |
| 170 |
|
| 171 |
(defun standard-instance-set (instance location val) |
| 172 |
(with-early-accessors (+standard-class-slots+ |
| 173 |
+slot-definition-slots+) |
| 174 |
(ensure-up-to-date-instance instance) |
| 175 |
(cond ((ext:fixnump location) |
| 176 |
;; local slot |
| 177 |
(si:instance-set instance (truly-the fixnum location) val)) |
| 178 |
((consp location) |
| 179 |
;; shared slot |
| 180 |
(setf (car location) val)) |
| 181 |
(t |
| 182 |
(invalid-slot-location instance location))) |
| 183 |
val)) |
| 184 |
|
| 185 |
(defsetf standard-instance-access standard-instance-set) |
| 186 |
(defsetf funcallable-standard-instance-access standard-instance-set) |
| 187 |
|
| 188 |
(defun slot-value (self slot-name) |
| 189 |
(with-early-accessors (+standard-class-slots+ |
| 190 |
+slot-definition-slots+) |
| 191 |
(let* ((class (class-of self)) |
| 192 |
(location-table (class-location-table class))) |
| 193 |
(if location-table |
| 194 |
(let ((location (gethash slot-name location-table nil))) |
| 195 |
(if location |
| 196 |
(let ((value (standard-instance-access self location))) |
| 197 |
(if (si:sl-boundp value) |
| 198 |
value |
| 199 |
(values (slot-unbound class self slot-name)))) |
| 200 |
(slot-missing class self slot-name 'SLOT-VALUE))) |
| 201 |
(let ((slotd (find slot-name (class-slots class) :key #'slot-definition-name))) |
| 202 |
(if slotd |
| 203 |
(slot-value-using-class class self slotd) |
| 204 |
(values (slot-missing class self slot-name 'SLOT-VALUE)))))))) |
| 205 |
|
| 206 |
(defun slot-exists-p (self slot-name) |
| 207 |
(and (find-slot-definition (class-of self) slot-name) |
| 208 |
t)) |
| 209 |
|
| 210 |
(defun slot-boundp (self slot-name) |
| 211 |
(with-early-accessors (+standard-class-slots+ |
| 212 |
+slot-definition-slots+) |
| 213 |
(let* ((class (class-of self)) |
| 214 |
(location-table (class-location-table class))) |
| 215 |
(if location-table |
| 216 |
(let ((location (gethash slot-name location-table nil))) |
| 217 |
(if location |
| 218 |
(si:sl-boundp (standard-instance-access self location)) |
| 219 |
(values (slot-missing class self slot-name 'SLOT-BOUNDP)))) |
| 220 |
(let ((slotd (find slot-name (class-slots class) :key #'slot-definition-name))) |
| 221 |
(if slotd |
| 222 |
(slot-boundp-using-class class self slotd) |
| 223 |
(values (slot-missing class self slot-name 'SLOT-BOUNDP)))))))) |
| 224 |
|
| 225 |
(defun clos::slot-value-set (value self slot-name) |
| 226 |
(with-early-accessors (+standard-class-slots+ |
| 227 |
+slot-definition-slots+) |
| 228 |
(let* ((class (class-of self)) |
| 229 |
(location-table (class-location-table class))) |
| 230 |
(if location-table |
| 231 |
(let ((location (gethash slot-name location-table nil))) |
| 232 |
(if location |
| 233 |
(setf (standard-instance-access self location) value) |
| 234 |
(slot-missing class self slot-name 'SETF value))) |
| 235 |
(let ((slotd (find slot-name (class-slots class) :key #'slot-definition-name))) |
| 236 |
(if slotd |
| 237 |
(setf (slot-value-using-class class self slotd) value) |
| 238 |
(slot-missing class self slot-name 'SETF value)))))) |
| 239 |
value) |
| 240 |
|
| 241 |
(setf (fdefinition '(setf slot-value)) #'clos::slot-value-set) |
| 242 |
|
| 243 |
;;; |
| 244 |
;;; 2) Overloadable methods on which the previous functions are based |
| 245 |
;;; |
| 246 |
|
| 247 |
(defun invalid-slot-location (instance location) |
| 248 |
(declare (si::c-local) |
| 249 |
(ignore instance)) |
| 250 |
(error "Invalid location ~A when accessing slot of class ~A" |
| 251 |
location (class-of location))) |
| 252 |
|
| 253 |
|
| 254 |
;;; |
| 255 |
;;; Atomic functions to set and increment slot values |
| 256 |
;;; |
| 257 |
#+threads |
| 258 |
(defun std-slot-location (self slot-name operation) |
| 259 |
(let* ((class (class-of self)) |
| 260 |
(location-table (class-location-table class)) |
| 261 |
(location (if location-table |
| 262 |
(gethash slot-name location-table nil) |
| 263 |
;; We ignore possible definitions of |
| 264 |
;; slot-value-using-class and hope for the |
| 265 |
;; best |
| 266 |
(let ((slotd (find slot-name (class-slots class) |
| 267 |
:key #'slot-definition-name))) |
| 268 |
(if slotd |
| 269 |
(slot-definition-location slotd) |
| 270 |
nil))))) |
| 271 |
(or location (slot-missing class self slot-name operation)))) |
| 272 |
|
| 273 |
#+threads |
| 274 |
(defun mp::compare-and-swap-standard-instance (instance location old new) |
| 275 |
(with-early-accessors (+standard-class-slots+ |
| 276 |
+slot-definition-slots+) |
| 277 |
(ensure-up-to-date-instance instance) |
| 278 |
(cond ((ext:fixnump location) |
| 279 |
;; local slot |
| 280 |
(mp:compare-and-swap-instance instance (truly-the fixnum location) old new)) |
| 281 |
((consp location) |
| 282 |
;; shared slot |
| 283 |
(mp:compare-and-swap (car location) old new)) |
| 284 |
(t |
| 285 |
(invalid-slot-location instance location))))) |
| 286 |
|
| 287 |
#+threads |
| 288 |
(defun mp::compare-and-swap-slot-value (self slot-name old new) |
| 289 |
(with-early-accessors (+standard-class-slots+ |
| 290 |
+slot-definition-slots+) |
| 291 |
(let* ((location (std-slot-location self slot-name 'MP:COMPARE-AND-SWAP)) |
| 292 |
(old-slot-value |
| 293 |
(mp::compare-and-swap-standard-instance self location old new))) |
| 294 |
(if (and (not (si:sl-boundp old-slot-value)) (si:sl-boundp old)) |
| 295 |
(slot-unbound (class-of self) self slot-name) |
| 296 |
old-slot-value)))) |
| 297 |
|
| 298 |
#+threads |
| 299 |
(defun mp::atomic-incf-standard-instance (instance location increment) |
| 300 |
(with-early-accessors (+standard-class-slots+ |
| 301 |
+slot-definition-slots+) |
| 302 |
(ensure-up-to-date-instance instance) |
| 303 |
(cond ((ext:fixnump location) |
| 304 |
;; local slot |
| 305 |
(mp:atomic-incf-instance instance (truly-the fixnum location) increment)) |
| 306 |
((consp location) |
| 307 |
;; shared slot |
| 308 |
(mp:atomic-incf (car location) increment)) |
| 309 |
(t |
| 310 |
(invalid-slot-location instance location))))) |
| 311 |
|
| 312 |
#+threads |
| 313 |
(defun mp::atomic-incf-slot-value (self slot-name increment) |
| 314 |
(with-early-accessors (+standard-class-slots+ |
| 315 |
+slot-definition-slots+) |
| 316 |
(mp::atomic-incf-standard-instance self |
| 317 |
(std-slot-location self slot-name 'MP:ATOMIC-INCF) |
| 318 |
increment))) |