| 1 |
;;;; |
| 2 |
;;;; Copyright (c) 1992, Giuseppe Attardi. |
| 3 |
;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll. |
| 4 |
;;;; Copyright (c) 2015-2021, Daniel Kochmański |
| 5 |
;;;; |
| 6 |
;;;; See file 'LICENSE' for the copyright details. |
| 7 |
;;;; |
| 8 |
|
| 9 |
;;; Bootstrapping CLOS is prone to metastability issues. To avoid them some |
| 10 |
;;; operators have different definitions during the bootstrap and the runtime. |
| 11 |
;;; This file "fixes" these operators to have their final form. |
| 12 |
|
| 13 |
(in-package "CLOS") |
| 14 |
|
| 15 |
;;; --------------------------------------------------------------------- |
| 16 |
;;; Fixup |
| 17 |
|
| 18 |
(defun register-method-with-specializers (method) |
| 19 |
(declare (si::c-local)) |
| 20 |
(with-early-accessors (+standard-method-slots+ +specializer-slots+) |
| 21 |
(loop for spec in (method-specializers method) |
| 22 |
;; Inlined "early" ADD-DIRECT-METHODS. |
| 23 |
do (let ((cell (specializer-method-holder spec))) |
| 24 |
(setf (cdr cell) nil |
| 25 |
(car cell) (adjoin method (car cell) :test #'eq)))))) |
| 26 |
|
| 27 |
(defun fixup-early-methods () |
| 28 |
(dolist (method-info *early-methods* (makunbound '*EARLY-METHODS*)) |
| 29 |
(let* ((method-name (car method-info)) |
| 30 |
(gfun (fdefinition method-name)) |
| 31 |
(standard-method-class (find-class 'standard-method))) |
| 32 |
(with-early-accessors (+class-slots+) |
| 33 |
(when (eq 'T (class-id (si:instance-class gfun))) |
| 34 |
;; complete the generic function object |
| 35 |
(si:instance-class-set gfun (find-class 'STANDARD-GENERIC-FUNCTION)) |
| 36 |
(si::instance-sig-set gfun) |
| 37 |
(setf (slot-value gfun 'method-class) standard-method-class) |
| 38 |
(setf (slot-value gfun 'docstring) nil))) |
| 39 |
(dolist (method (cdr method-info)) |
| 40 |
;; complete the method object |
| 41 |
(let ((old-class (si::instance-class method))) |
| 42 |
(si::instance-class-set method |
| 43 |
(cond ((null old-class) |
| 44 |
(find-class 'standard-method)) |
| 45 |
((symbolp old-class) |
| 46 |
(find-class (truly-the |
| 47 |
symbol |
| 48 |
old-class))) |
| 49 |
(t |
| 50 |
old-class)))) |
| 51 |
(si::instance-sig-set gfun) |
| 52 |
(register-method-with-specializers method))))) |
| 53 |
|
| 54 |
(fixup-early-methods) |
| 55 |
|
| 56 |
;;; Now we protect classes from redefinition: |
| 57 |
(eval-when (:compile-toplevel :load-toplevel) |
| 58 |
(defun setf-find-class (new-value name &optional errorp env) |
| 59 |
(declare (ignore errorp)) |
| 60 |
(let ((old-class (find-class name nil env))) |
| 61 |
(cond |
| 62 |
((typep old-class 'built-in-class) |
| 63 |
(error "The class associated to the CL specifier ~S cannot be changed." |
| 64 |
name)) |
| 65 |
((member name '(CLASS BUILT-IN-CLASS) :test #'eq) |
| 66 |
(error "The kernel CLOS class ~S cannot be changed." name)) |
| 67 |
((classp new-value) |
| 68 |
(setf (gethash name si:*class-name-hash-table*) new-value)) |
| 69 |
((null new-value) (remhash name si:*class-name-hash-table*)) |
| 70 |
(t (error "~A is not a class." new-value)))) |
| 71 |
new-value)) |
| 72 |
|
| 73 |
|
| 74 |
;;; --------------------------------------------------------------------- |
| 75 |
;;; Redefined functions |
| 76 |
|
| 77 |
(defun method-p (method) (typep method 'METHOD)) |
| 78 |
|
| 79 |
(defun make-method (method-class qualifiers specializers arglist |
| 80 |
function options) |
| 81 |
(apply #'make-instance |
| 82 |
method-class |
| 83 |
:generic-function nil |
| 84 |
:qualifiers qualifiers |
| 85 |
:lambda-list arglist |
| 86 |
:specializers specializers |
| 87 |
:function function |
| 88 |
:allow-other-keys t |
| 89 |
options)) |
| 90 |
|
| 91 |
(defun all-keywords (l) |
| 92 |
(declare (si::c-local)) |
| 93 |
(let ((all-keys '())) |
| 94 |
(do ((l (rest l) (cddddr l))) |
| 95 |
((null l) |
| 96 |
all-keys) |
| 97 |
(push (first l) all-keys)))) |
| 98 |
|
| 99 |
(defun congruent-lambda-p (l1 l2) |
| 100 |
(multiple-value-bind (r1 opts1 rest1 key-flag1 keywords1 a-o-k1) |
| 101 |
(si::process-lambda-list l1 'FUNCTION) |
| 102 |
(multiple-value-bind (r2 opts2 rest2 key-flag2 keywords2 a-o-k2) |
| 103 |
(si::process-lambda-list l2 'FUNCTION) |
| 104 |
(and (= (length r2) (length r1)) |
| 105 |
(= (length opts1) (length opts2)) |
| 106 |
(eq (and (null rest1) (null key-flag1)) |
| 107 |
(and (null rest2) (null key-flag2))) |
| 108 |
;; All keywords mentioned in the generic function must be |
| 109 |
;; accepted by the method. |
| 110 |
(or (null key-flag1) |
| 111 |
(null key-flag2) |
| 112 |
;; Testing for a-o-k1 here may not be conformant when |
| 113 |
;; the fourth point of 7.6.4 is read literally, but it |
| 114 |
;; is more consistent with the generic function calling |
| 115 |
;; specification. Also it is compatible with popular |
| 116 |
;; implementations like SBCL and CCL. -- jd 2020-04-07 |
| 117 |
a-o-k1 |
| 118 |
a-o-k2 |
| 119 |
(null (set-difference (all-keywords keywords1) |
| 120 |
(all-keywords keywords2)))) |
| 121 |
t)))) |
| 122 |
|
| 123 |
(defun add-method (gf method) |
| 124 |
;; during boot it's a structure accessor |
| 125 |
(declare (notinline method-qualifiers remove-method)) |
| 126 |
;; |
| 127 |
;; 1) The method must not be already installed in another generic |
| 128 |
;; function. |
| 129 |
;; |
| 130 |
(let ((other-gf (method-generic-function method))) |
| 131 |
(unless (or (null other-gf) (eq other-gf gf)) |
| 132 |
(error "The method ~A belongs to the generic function ~A ~ |
| 133 |
and cannot be added to ~A." method other-gf gf))) |
| 134 |
;; |
| 135 |
;; 2) The method and the generic function should have congruent |
| 136 |
;; lambda lists. That is, it should accept the same number of |
| 137 |
;; required and optional arguments, and only accept keyword |
| 138 |
;; arguments when the generic function does. |
| 139 |
;; |
| 140 |
(let ((new-lambda-list (method-lambda-list method))) |
| 141 |
(if (slot-boundp gf 'lambda-list) |
| 142 |
(let ((old-lambda-list (generic-function-lambda-list gf))) |
| 143 |
(unless (congruent-lambda-p old-lambda-list new-lambda-list) |
| 144 |
(error "Cannot add the method ~A to the generic function ~A because ~ |
| 145 |
their lambda lists ~A and ~A are not congruent." |
| 146 |
method gf old-lambda-list new-lambda-list))) |
| 147 |
(reinitialize-instance |
| 148 |
gf :lambda-list (implicit-generic-lambda new-lambda-list)))) |
| 149 |
;; |
| 150 |
;; 3) Finally, it is inserted in the list of methods, and the method is |
| 151 |
;; marked as belonging to a generic function. |
| 152 |
;; |
| 153 |
(when (generic-function-methods gf) |
| 154 |
(let* ((method-qualifiers (method-qualifiers method)) |
| 155 |
(specializers (method-specializers method)) |
| 156 |
(found (find-method gf method-qualifiers specializers nil))) |
| 157 |
(when found |
| 158 |
(remove-method gf found)))) |
| 159 |
;; |
| 160 |
;; We install the method by: |
| 161 |
;; i) Adding it to the list of methods |
| 162 |
(push method (generic-function-methods gf)) |
| 163 |
(setf (method-generic-function method) gf) |
| 164 |
;; ii) Updating the specializers list of the generic function. |
| 165 |
(dolist (spec (method-specializers method)) |
| 166 |
(add-direct-method spec method)) |
| 167 |
;; iii) Computing a new discriminating function... Well, since the core ECL |
| 168 |
;; does not need the discriminating function because we always use the same |
| 169 |
;; one, we just update the spec-how list of the generic function.. |
| 170 |
(compute-g-f-spec-list gf) |
| 171 |
(set-generic-function-dispatch gf) |
| 172 |
;; iv) Update dependents. |
| 173 |
(update-dependents gf (list 'add-method method)) |
| 174 |
gf) |
| 175 |
|
| 176 |
(defun remove-method (gf method) |
| 177 |
(setf (generic-function-methods gf) |
| 178 |
(delete method (generic-function-methods gf)) |
| 179 |
(method-generic-function method) nil) |
| 180 |
(si:clear-gfun-hash gf) |
| 181 |
(loop for spec in (method-specializers method) |
| 182 |
do (remove-direct-method spec method)) |
| 183 |
(compute-g-f-spec-list gf) |
| 184 |
(set-generic-function-dispatch gf) |
| 185 |
(update-dependents gf (list 'remove-method method)) |
| 186 |
gf) |
| 187 |
|
| 188 |
|
| 189 |
;;; ---------------------------------------------------------------------------- |
| 190 |
;;; Converting functions to methods |
| 191 |
;;; |
| 192 |
;;; To avoid metastability issues we install the old function directly as the |
| 193 |
;;; discriminator (with necessary checks). That works because there are no |
| 194 |
;;; other methods. When a new method is added then the discriminating function |
| 195 |
;;; will be recomputed. |
| 196 |
|
| 197 |
(macrolet |
| 198 |
((do-function-to-method (name signature) |
| 199 |
(multiple-value-bind (lambda-list type-checks ignores) |
| 200 |
(loop for argument in signature |
| 201 |
if (consp argument) |
| 202 |
do (assert (atom (second argument))) |
| 203 |
and collect (first argument) |
| 204 |
into lambda-list |
| 205 |
and collect `(typep ,(first argument) |
| 206 |
',(second argument)) |
| 207 |
into type-checks |
| 208 |
else |
| 209 |
collect argument into lambda-list |
| 210 |
and unless (member argument lambda-list-keywords) |
| 211 |
collect argument into ignores |
| 212 |
finally |
| 213 |
(return (values lambda-list type-checks ignores))) |
| 214 |
`(let* ((aux-name 'temp-method) |
| 215 |
(old-function (fdefinition ',name)) |
| 216 |
(method (eval `(defmethod ,aux-name ,',signature))) |
| 217 |
(generic-function (fdefinition aux-name))) |
| 218 |
(with-early-accessors (+standard-method-slots+) |
| 219 |
(setf (method-function method) |
| 220 |
(wrapped-method-function old-function))) |
| 221 |
(setf (fdefinition ',name) generic-function) |
| 222 |
(setf (slot-value generic-function 'name) ',name) |
| 223 |
(set-funcallable-instance-function |
| 224 |
generic-function |
| 225 |
#'(lambda (&rest args) |
| 226 |
(destructuring-bind ,lambda-list args |
| 227 |
(declare (ignore ,@ignores)) |
| 228 |
(unless (or (null *clos-booted*) |
| 229 |
(and ,@type-checks)) |
| 230 |
(apply #'no-applicable-method generic-function args))) |
| 231 |
(apply old-function args))) |
| 232 |
(fmakunbound aux-name))))) |
| 233 |
(do-function-to-method add-method |
| 234 |
((gf standard-generic-function) (method standard-method))) |
| 235 |
(do-function-to-method remove-method |
| 236 |
((gf standard-generic-function) (method standard-method))) |
| 237 |
(do-function-to-method find-method |
| 238 |
((gf standard-generic-function) qualifiers specializers &optional error)) |
| 239 |
(do-function-to-method compute-discriminating-function |
| 240 |
((gf standard-generic-function))) |
| 241 |
(do-function-to-method generic-function-method-class |
| 242 |
((gf standard-generic-function))) |
| 243 |
(do-function-to-method (setf generic-function-name) |
| 244 |
(name (gf generic-function))) |
| 245 |
(do-function-to-method find-method-combination |
| 246 |
((gf standard-generic-function) |
| 247 |
method-combination-type-name |
| 248 |
method-combination-options)) |
| 249 |
(do-function-to-method make-method-lambda |
| 250 |
((gf standard-generic-function) (method standard-method) |
| 251 |
lambda-form environment)) |
| 252 |
(do-function-to-method compute-applicable-methods-using-classes |
| 253 |
((gf standard-generic-function) classes)) |
| 254 |
(do-function-to-method compute-applicable-methods |
| 255 |
((gf standard-generic-function) arguments)) |
| 256 |
(do-function-to-method compute-effective-method |
| 257 |
((gf standard-generic-function) method-combination applicable-methods))) |
| 258 |
|
| 259 |
|
| 260 |
;;; ---------------------------------------------------------------------------- |
| 261 |
;;; Missing methods |
| 262 |
|
| 263 |
(defmethod reader-method-class ((class std-class) |
| 264 |
(direct-slot direct-slot-definition) |
| 265 |
&rest initargs) |
| 266 |
(declare (ignore class direct-slot initargs)) |
| 267 |
(find-class (if (member (class-name (class-of class)) |
| 268 |
'(standard-class |
| 269 |
funcallable-standard-class |
| 270 |
structure-class)) |
| 271 |
'standard-optimized-reader-method |
| 272 |
'standard-reader-method))) |
| 273 |
|
| 274 |
(defmethod writer-method-class ((class std-class) |
| 275 |
(direct-slot direct-slot-definition) |
| 276 |
&rest initargs) |
| 277 |
(declare (ignore class direct-slot initargs)) |
| 278 |
(find-class (if (member (class-name (class-of class)) |
| 279 |
'(standard-class |
| 280 |
funcallable-standard-class |
| 281 |
structure-class)) |
| 282 |
'standard-optimized-writer-method |
| 283 |
'standard-reader-method))) |
| 284 |
|
| 285 |
;;; Error messages |
| 286 |
(defmethod no-applicable-method (gf &rest args) |
| 287 |
(error "No applicable method for ~S with ~ |
| 288 |
~:[no arguments~;arguments of types ~:*~{~& ~A~}~]." |
| 289 |
(generic-function-name gf) |
| 290 |
(mapcar #'type-of args))) |
| 291 |
|
| 292 |
(defmethod no-next-method (gf method &rest args) |
| 293 |
(declare (ignore gf)) |
| 294 |
(error "In method ~A~%No next method given arguments ~A" method args)) |
| 295 |
|
| 296 |
(defun no-primary-method (gf &rest args) |
| 297 |
(error "Generic function: ~A. No primary method given arguments: ~S" |
| 298 |
(generic-function-name gf) args)) |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
;;; ---------------------------------------------------------------------------- |
| 303 |
;;; Dependent maintenance protocol |
| 304 |
|
| 305 |
(defmethod add-dependent ((c class) dep) |
| 306 |
(pushnew dep (class-dependents c))) |
| 307 |
|
| 308 |
(defmethod add-dependent ((c generic-function) dependent) |
| 309 |
(pushnew dependent (generic-function-dependents c))) |
| 310 |
|
| 311 |
(defmethod remove-dependent ((c class) dep) |
| 312 |
(setf (class-dependents c) |
| 313 |
(remove dep (class-dependents c)))) |
| 314 |
|
| 315 |
(defmethod remove-dependent ((c standard-generic-function) dep) |
| 316 |
(setf (generic-function-dependents c) |
| 317 |
(remove dep (generic-function-dependents c)))) |
| 318 |
|
| 319 |
(defmethod map-dependents ((c class) function) |
| 320 |
(dolist (d (class-dependents c)) |
| 321 |
(funcall function d))) |
| 322 |
|
| 323 |
(defmethod map-dependents ((c standard-generic-function) function) |
| 324 |
(dolist (d (generic-function-dependents c)) |
| 325 |
(funcall function d))) |
| 326 |
|
| 327 |
(ensure-generic-function 'update-dependent |
| 328 |
:lambda-list '(object dependent &rest initargs)) |
| 329 |
|
| 330 |
;; After this, update-dependents will work |
| 331 |
(setf *clos-booted* 'map-dependents) |
| 332 |
|
| 333 |
(defclass initargs-updater () ()) |
| 334 |
|
| 335 |
(defun recursively-update-classes (a-class) |
| 336 |
(slot-makunbound a-class 'valid-initargs) |
| 337 |
(mapc #'recursively-update-classes |
| 338 |
(class-direct-subclasses a-class))) |
| 339 |
|
| 340 |
(defmethod update-dependent ((object generic-function) (dep initargs-updater) |
| 341 |
&rest initargs |
| 342 |
&key |
| 343 |
((add-method added-method) nil am-p) |
| 344 |
((remove-method removed-method) nil rm-p) |
| 345 |
&allow-other-keys) |
| 346 |
(declare (ignore object dep initargs)) |
| 347 |
(when-let ((method (cond (am-p added-method) |
| 348 |
(rm-p removed-method)))) |
| 349 |
;; update-dependent is also called when the gf itself is reinitialized, |
| 350 |
;; so make sure we actually have a method that's added or removed |
| 351 |
(let ((spec (first (method-specializers method)))) ; the class being initialized or allocated |
| 352 |
(when (classp spec) ; sanity check against eql specialization |
| 353 |
(recursively-update-classes spec))))) |
| 354 |
|
| 355 |
(let ((x (make-instance 'initargs-updater))) |
| 356 |
(add-dependent #'shared-initialize x) |
| 357 |
(add-dependent #'initialize-instance x) |
| 358 |
(add-dependent #'allocate-instance x)) |