| 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. |
| 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 |
|
| 14 |
(defparameter *method-size* 32) ; Size of methods hash tables |
| 15 |
|
| 16 |
;;; This holds fake methods created during bootstrap. |
| 17 |
;;; It is an alist of: |
| 18 |
;;; (method-name {method}+) |
| 19 |
(defparameter *early-methods* nil) |
| 20 |
|
| 21 |
;;; |
| 22 |
;;; This is used by combined methods to communicate the next methods to |
| 23 |
;;; the methods they call. |
| 24 |
;;; |
| 25 |
(defparameter *next-methods* nil) |
| 26 |
|
| 27 |
;;; Add type declarations for the arguments of a METHOD. This implies |
| 28 |
;;; copying the method arguments because the arguments may be modified. |
| 29 |
(defparameter *add-method-argument-declarations* nil) |
| 30 |
|
| 31 |
|
| 32 |
;;; ---------------------------------------------------------------------- |
| 33 |
;;; DEFMETHOD |
| 34 |
;;; |
| 35 |
|
| 36 |
(defun generic-function-method-class (generic-function) |
| 37 |
(if *clos-booted* |
| 38 |
(slot-value generic-function 'method-class) |
| 39 |
(find-class 'standard-method))) |
| 40 |
|
| 41 |
(defun prototypes-for-make-method-lambda (name) |
| 42 |
(if (not *clos-booted*) |
| 43 |
(values nil nil) |
| 44 |
(let ((gf? (and (fboundp name) |
| 45 |
(fdefinition name)))) |
| 46 |
(if (or (null gf?) |
| 47 |
(not (si:instancep gf?))) |
| 48 |
(values (class-prototype (find-class 'standard-generic-function)) |
| 49 |
(class-prototype (find-class 'standard-method))) |
| 50 |
(values gf? |
| 51 |
(class-prototype (or (generic-function-method-class gf?) |
| 52 |
(find-class 'standard-method)))))))) |
| 53 |
|
| 54 |
(defmacro defmethod (&whole whole name &rest args &environment env) |
| 55 |
(declare (notinline make-method-lambda)) |
| 56 |
(multiple-value-bind (qualifiers specialized-lambda-list body) |
| 57 |
(parse-defmethod args) |
| 58 |
(multiple-value-bind (lambda-list required-parameters specializers) |
| 59 |
(parse-specialized-lambda-list specialized-lambda-list) |
| 60 |
(multiple-value-bind (lambda-form declarations documentation) |
| 61 |
(make-raw-lambda name lambda-list required-parameters specializers body env) |
| 62 |
(declare (ignore declarations)) |
| 63 |
(multiple-value-bind (proto-gf proto-method) |
| 64 |
(prototypes-for-make-method-lambda name) |
| 65 |
(multiple-value-bind (fn-form options) |
| 66 |
(make-method-lambda proto-gf proto-method lambda-form env) |
| 67 |
(when documentation |
| 68 |
(setf options (list* :documentation documentation options))) |
| 69 |
(ext:register-with-pde |
| 70 |
whole |
| 71 |
`(install-method ',name ',qualifiers |
| 72 |
,(specializers-expression specializers) |
| 73 |
',lambda-list |
| 74 |
,(maybe-remove-block (simplify-lambda fn-form)) |
| 75 |
,@(mapcar #'si::maybe-quote options))))))))) |
| 76 |
|
| 77 |
(defun specializers-expression (specializers) |
| 78 |
(declare (si::c-local)) |
| 79 |
;; The list of specializers contains either types or (EQL value) |
| 80 |
;; expressions. The 'value' has to be obtained by evaluating a lisp form |
| 81 |
;; We save some space/time by including directly the value of those |
| 82 |
;; forms which do not involve any evaluations, such as literals or QUOTE |
| 83 |
;; expressions. Note that we cannot use CONSTANTP because this would extract |
| 84 |
;; the value of constant symbols, which would not be EQL to the value of |
| 85 |
;; those same constants when the code is reloaded. |
| 86 |
(list 'si::quasiquote |
| 87 |
(loop for spec in specializers |
| 88 |
collect (if (atom spec) |
| 89 |
spec |
| 90 |
`(eql ,(let ((value (second spec))) |
| 91 |
(if (if (atom value) |
| 92 |
(not (symbolp value)) |
| 93 |
(eq (car value) 'quote)) |
| 94 |
(ext:constant-form-value value) |
| 95 |
(list 'si::unquote value)))))))) |
| 96 |
|
| 97 |
(defun maybe-remove-block (method-lambda) |
| 98 |
(when (eq (first method-lambda) 'lambda) |
| 99 |
(multiple-value-bind (declarations body documentation) |
| 100 |
(si::find-declarations (cddr method-lambda)) |
| 101 |
(declare (ignore documentation)) |
| 102 |
(let (block) |
| 103 |
(when (and (null (rest body)) |
| 104 |
(listp (setf block (first body))) |
| 105 |
(eq (first block) 'block)) |
| 106 |
(setf method-lambda `(ext:lambda-block ,(second block) ,(second method-lambda) |
| 107 |
,@declarations |
| 108 |
,@(cddr block))) |
| 109 |
)))) |
| 110 |
method-lambda) |
| 111 |
|
| 112 |
(defun simplify-lambda (fn-form) |
| 113 |
(let ((aux fn-form)) |
| 114 |
(if (and (eq (pop aux) 'lambda) |
| 115 |
(equalp (pop aux) '(.combined-method-args. *next-methods*)) |
| 116 |
(equalp (pop aux) '(declare (special .combined-method-args. *next-methods*))) |
| 117 |
(null (rest aux)) |
| 118 |
(= (length (setf aux (first aux))) 3) |
| 119 |
(eq (first aux) 'apply) |
| 120 |
(eq (third aux) '.combined-method-args.) |
| 121 |
(listp (setf aux (second aux))) |
| 122 |
(eq (first aux) 'lambda)) |
| 123 |
aux |
| 124 |
(error "Unable to unwrap function")))) |
| 125 |
|
| 126 |
(defun make-raw-lambda (name lambda-list required-parameters specializers body env) |
| 127 |
(declare (si::c-local) |
| 128 |
(ignore env)) |
| 129 |
(multiple-value-bind (declarations real-body documentation) |
| 130 |
(sys::find-declarations body) |
| 131 |
;; FIXME!! This deactivates the checking of keyword arguments |
| 132 |
;; inside methods. The reason is that this checking must be |
| 133 |
;; supplemented the knowledge of the keyword arguments of all |
| 134 |
;; applicable methods (X3J13 7.6.5). Therefore, we should insert |
| 135 |
;; that check, either in the method itself so that it is done |
| 136 |
;; incrementally, or in COMPUTE-EFFECTIVE-METHOD. |
| 137 |
(when (and (member '&key lambda-list) |
| 138 |
(not (member '&allow-other-keys lambda-list))) |
| 139 |
(let ((x (position '&aux lambda-list))) |
| 140 |
(setf lambda-list |
| 141 |
(append (subseq lambda-list 0 x) |
| 142 |
'(&allow-other-keys) |
| 143 |
(and x (subseq lambda-list x)) |
| 144 |
nil)))) |
| 145 |
(let* ((copied-variables '()) |
| 146 |
(ignorable `(declare (ignorable ,@required-parameters))) |
| 147 |
(block-name (si:function-block-name name)) |
| 148 |
(class-declarations |
| 149 |
(nconc (when *add-method-argument-declarations* |
| 150 |
(loop for name in required-parameters |
| 151 |
for type in specializers |
| 152 |
when (and (not (eq type t)) (symbolp type)) |
| 153 |
do (push `(,name ,name) copied-variables) and |
| 154 |
nconc `((type ,type ,name) |
| 155 |
(si::no-check-type ,name)))) |
| 156 |
(list (list 'si:function-block-name block-name)) |
| 157 |
(cdar declarations))) |
| 158 |
(block `(block ,block-name ,@real-body)) |
| 159 |
(method-lambda |
| 160 |
;; Remove the documentation string and insert the |
| 161 |
;; appropriate class declarations. The documentation |
| 162 |
;; string is removed to make it easy for us to insert |
| 163 |
;; new declarations later, they will just go after the |
| 164 |
;; second of the method lambda. The class declarations |
| 165 |
;; are inserted to communicate the class of the method's |
| 166 |
;; arguments to the code walk. |
| 167 |
`(lambda ,lambda-list |
| 168 |
,@(and class-declarations `((declare ,@class-declarations))) |
| 169 |
,ignorable |
| 170 |
,(if copied-variables |
| 171 |
`(let* ,copied-variables |
| 172 |
,ignorable |
| 173 |
,block) |
| 174 |
block)))) |
| 175 |
(values method-lambda declarations documentation)))) |
| 176 |
|
| 177 |
(defun make-method-lambda (gf method method-lambda env) |
| 178 |
(declare (ignore method gf)) |
| 179 |
(multiple-value-bind (call-next-method-p next-method-p-p in-closure-p) |
| 180 |
(walk-method-lambda method-lambda env) |
| 181 |
(declare (ignore call-next-method-p next-method-p-p)) |
| 182 |
(values `(lambda (.combined-method-args. *next-methods*) |
| 183 |
(declare (special .combined-method-args. *next-methods*)) |
| 184 |
(apply ,(if in-closure-p |
| 185 |
(add-call-next-method-closure method-lambda) |
| 186 |
method-lambda) |
| 187 |
.combined-method-args.)) |
| 188 |
nil))) |
| 189 |
|
| 190 |
(defun add-call-next-method-closure (method-lambda) |
| 191 |
(multiple-value-bind (declarations real-body documentation) |
| 192 |
(si::find-declarations (cddr method-lambda)) |
| 193 |
(declare (ignore documentation)) |
| 194 |
`(lambda ,(second method-lambda) |
| 195 |
,@declarations |
| 196 |
(let* ((.closed-combined-method-args. |
| 197 |
(if (listp .combined-method-args.) |
| 198 |
.combined-method-args. |
| 199 |
(apply #'list .combined-method-args.))) |
| 200 |
(.next-methods. *next-methods*)) |
| 201 |
(flet ((call-next-method (&rest args) |
| 202 |
(unless .next-methods. |
| 203 |
(error "No next method")) |
| 204 |
(funcall (car .next-methods.) |
| 205 |
(or args .closed-combined-method-args.) |
| 206 |
(rest .next-methods.))) |
| 207 |
(next-method-p () |
| 208 |
.next-methods.)) |
| 209 |
,@real-body))))) |
| 210 |
|
| 211 |
(defun environment-contains-closure (env) |
| 212 |
;; |
| 213 |
;; As explained in compiler.d (make_lambda()), we use a symbol with |
| 214 |
;; name "FUNCTION-BOUNDARY" to mark the beginning of a function. If |
| 215 |
;; we find that symbol twice, it is quite likely that this form will |
| 216 |
;; end up in a closure. |
| 217 |
;; |
| 218 |
(let ((counter 0)) |
| 219 |
(declare (fixnum counter)) |
| 220 |
(dolist (item (car env)) |
| 221 |
(when (and (eq item 'si::function-boundary) |
| 222 |
(> (incf counter) 1)) |
| 223 |
(return t))))) |
| 224 |
|
| 225 |
(defun walk-method-lambda (method-lambda env) |
| 226 |
(declare (si::c-local)) |
| 227 |
(let ((call-next-method-p nil) |
| 228 |
(next-method-p-p nil) |
| 229 |
(in-closure-p nil)) |
| 230 |
(flet ((code-walker (form env) |
| 231 |
(unless (atom form) |
| 232 |
(let ((name (first form))) |
| 233 |
(case name |
| 234 |
(CALL-NEXT-METHOD |
| 235 |
(setf call-next-method-p |
| 236 |
(or call-next-method-p T) |
| 237 |
in-closure-p |
| 238 |
(or in-closure-p (environment-contains-closure env)))) |
| 239 |
(NEXT-METHOD-P |
| 240 |
(setf next-method-p-p t |
| 241 |
in-closure-p (or in-closure-p (environment-contains-closure env)))) |
| 242 |
(FUNCTION |
| 243 |
(when (eq (second form) 'CALL-NEXT-METHOD) |
| 244 |
(setf in-closure-p t |
| 245 |
call-next-method-p 'FUNCTION)) |
| 246 |
(when (eq (second form) 'NEXT-METHOD-P) |
| 247 |
(setf next-method-p-p 'FUNCTION |
| 248 |
in-closure-p t)))))) |
| 249 |
form)) |
| 250 |
(let ((si::*code-walker* #'code-walker)) |
| 251 |
;; Instead of (coerce method-lambda 'function) we use |
| 252 |
;; explicitely the bytecodes compiler with an environment, no |
| 253 |
;; stepping, compiler-env-p = t and mode = :compile-toplevel, |
| 254 |
;; so that the form does not get executed. |
| 255 |
(si::eval-with-env method-lambda env nil t :compile-toplevel))) |
| 256 |
(values call-next-method-p |
| 257 |
next-method-p-p |
| 258 |
in-closure-p))) |
| 259 |
|
| 260 |
;;; ---------------------------------------------------------------------- |
| 261 |
;;; parsing |
| 262 |
|
| 263 |
(defun legal-generic-function-name-p (name) |
| 264 |
(si::valid-function-name-p name)) |
| 265 |
|
| 266 |
;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument |
| 267 |
;;; into the 'real' arguments. This is where the syntax of DEFMETHOD |
| 268 |
;;; is really implemented. |
| 269 |
(defun parse-defmethod (cdr-of-form) |
| 270 |
(declare (si::c-local) |
| 271 |
(list cdr-of-form)) |
| 272 |
(let ((qualifiers ()) |
| 273 |
(spec-ll ())) |
| 274 |
(loop (if (and (car cdr-of-form) (atom (car cdr-of-form))) |
| 275 |
(push (pop cdr-of-form) qualifiers) |
| 276 |
(return (setq qualifiers (nreverse qualifiers))))) |
| 277 |
(setq spec-ll (pop cdr-of-form)) |
| 278 |
(values qualifiers spec-ll cdr-of-form))) |
| 279 |
|
| 280 |
(defun implicit-generic-lambda (lambda-list) |
| 281 |
"Implicit defgeneric declaration removes all &key arguments (preserving &key)" |
| 282 |
(when lambda-list |
| 283 |
(let (acc) |
| 284 |
(do* ((ll lambda-list (cdr ll)) |
| 285 |
(elt (car ll) (car ll))) |
| 286 |
((or (endp (rest ll)) |
| 287 |
(eql elt '&key)) |
| 288 |
(nreverse (cons elt acc))) |
| 289 |
(push elt acc))))) |
| 290 |
|
| 291 |
(defun extract-lambda-list (specialized-lambda-list) |
| 292 |
(values (parse-specialized-lambda-list specialized-lambda-list))) |
| 293 |
|
| 294 |
(defun extract-specializer-names (specialized-lambda-list) |
| 295 |
(nth-value 2 (parse-specialized-lambda-list specialized-lambda-list))) |
| 296 |
|
| 297 |
(defun parse-specialized-lambda-list (specialized-lambda-list) |
| 298 |
"This function takes a method lambda list and outputs the list of required |
| 299 |
arguments, the list of specializers and a new lambda list where the specializer |
| 300 |
have disappeared." |
| 301 |
(declare (si::c-local)) |
| 302 |
;; SI:PROCESS-LAMBDA-LIST will ensure that the lambda list is |
| 303 |
;; syntactically correct and will output as a first argument the |
| 304 |
;; list of required arguments. We use this list to extract the |
| 305 |
;; specializers and build a lambda list without specializers. |
| 306 |
(do* ((arglist (rest (si::process-lambda-list specialized-lambda-list 'METHOD)) |
| 307 |
(rest arglist)) |
| 308 |
(lambda-list (copy-list specialized-lambda-list)) |
| 309 |
(ll lambda-list (rest ll)) |
| 310 |
(required-parameters '()) |
| 311 |
(specializers '()) |
| 312 |
arg variable specializer) |
| 313 |
((null arglist) |
| 314 |
(values lambda-list |
| 315 |
(nreverse required-parameters) |
| 316 |
(nreverse specializers))) |
| 317 |
(setf arg (first arglist)) |
| 318 |
(cond |
| 319 |
;; Just a variable |
| 320 |
((atom arg) |
| 321 |
(setf variable arg specializer T)) |
| 322 |
;; List contains more elements than variable and specializer |
| 323 |
((not (endp (cddr arg))) |
| 324 |
(si::simple-program-error "Syntax error in method specializer ~A" arg)) |
| 325 |
;; Specializer is NIL |
| 326 |
((null (setf variable (first arg) |
| 327 |
specializer (second arg))) |
| 328 |
(si::simple-program-error |
| 329 |
"NIL is not a valid specializer in a method lambda list")) |
| 330 |
;; Specializer is a class name |
| 331 |
((atom specializer)) |
| 332 |
;; Specializer is (EQL value) |
| 333 |
((and (eql (first specializer) 'EQL) |
| 334 |
(cdr specializer) |
| 335 |
(endp (cddr specializer)))) |
| 336 |
;; Otherwise, syntax error |
| 337 |
(t |
| 338 |
(si::simple-program-error "Syntax error in method specializer ~A" arg))) |
| 339 |
(setf (first ll) variable) |
| 340 |
(push variable required-parameters) |
| 341 |
(push specializer specializers))) |
| 342 |
|
| 343 |
(defun declaration-specializers (arglist declarations) |
| 344 |
(declare (si::c-local)) |
| 345 |
(do ((argscan arglist (cdr argscan)) |
| 346 |
(declist (when declarations (cdr declarations)))) |
| 347 |
((or |
| 348 |
(null argscan) |
| 349 |
(member (first argscan) '(&OPTIONAL &REST &KEY &ALLOW-OTHER-KEYS &AUX))) |
| 350 |
`(DECLARE ,@declist)) |
| 351 |
(when (listp (first argscan)) |
| 352 |
(push `(TYPE ,(cadar argscan) ,(caar argscan)) declist)))) |
| 353 |
|
| 354 |
|
| 355 |
;;; ---------------------------------------------------------------------- |
| 356 |
;;; operations |
| 357 |
|
| 358 |
(defun compute-method-keywords (lambda-list) |
| 359 |
(multiple-value-bind (reqs opts rest key-flag keywords allow-other-keys) |
| 360 |
(si::process-lambda-list lambda-list t) |
| 361 |
(declare (ignore reqs opts rest key-flag)) |
| 362 |
(if allow-other-keys |
| 363 |
't |
| 364 |
(loop for k in (rest keywords) by #'cddddr |
| 365 |
collect k)))) |
| 366 |
|
| 367 |
(defun make-method (method-class qualifiers specializers lambda-list fun options) |
| 368 |
(declare (ignore options)) |
| 369 |
;; We choose the largest list of slots |
| 370 |
(with-early-make-instance +standard-accessor-method-slots+ |
| 371 |
(method (if (si::instancep method-class) |
| 372 |
method-class |
| 373 |
(find-class method-class)) |
| 374 |
:generic-function nil |
| 375 |
:lambda-list lambda-list |
| 376 |
:function fun |
| 377 |
:specializers specializers |
| 378 |
:qualifiers qualifiers |
| 379 |
:keywords (compute-method-keywords lambda-list)) |
| 380 |
method)) |
| 381 |
|
| 382 |
;;; early version used during bootstrap |
| 383 |
(defun method-p (x) |
| 384 |
(si::instancep x)) |
| 385 |
|
| 386 |
;;; early version used during bootstrap |
| 387 |
(defun add-method (gf method) |
| 388 |
;; Add the method to *EARLY-METHODS*. |
| 389 |
(let* ((name (slot-value gf 'name)) |
| 390 |
(method-entry (assoc name *early-methods*))) |
| 391 |
(unless method-entry |
| 392 |
(setq method-entry (list name)) |
| 393 |
(push method-entry *early-methods*)) |
| 394 |
(push method (cdr method-entry))) |
| 395 |
;; Add the method to the generic function. |
| 396 |
(with-early-accessors |
| 397 |
(+standard-method-slots+ +standard-generic-function-slots+) |
| 398 |
(push method (generic-function-methods gf)) |
| 399 |
(setf (method-generic-function method) gf) |
| 400 |
(unless (si::sl-boundp (generic-function-lambda-list gf)) |
| 401 |
(setf (generic-function-lambda-list gf) |
| 402 |
(implicit-generic-lambda (method-lambda-list method))) |
| 403 |
(setf (generic-function-argument-precedence-order gf) |
| 404 |
(rest (si::process-lambda-list (method-lambda-list method) t)))) |
| 405 |
(compute-g-f-spec-list gf) |
| 406 |
(set-generic-function-dispatch gf) |
| 407 |
method)) |
| 408 |
|
| 409 |
(defun find-method (gf qualifiers specializers &optional (errorp t)) |
| 410 |
(declare (notinline method-qualifiers)) |
| 411 |
(flet ((filter-specializer (name) |
| 412 |
(cond ((typep name 'specializer) |
| 413 |
name) |
| 414 |
((atom name) |
| 415 |
(let ((class (find-class name nil))) |
| 416 |
(unless class |
| 417 |
(error "~A is not a valid specializer name" name)) |
| 418 |
class)) |
| 419 |
((and (eq (first name) 'EQL) |
| 420 |
(null (cddr name))) |
| 421 |
(cdr name)) |
| 422 |
(t |
| 423 |
(error "~A is not a valid specializer name" name)))) |
| 424 |
(specializer= (cons-or-class specializer) |
| 425 |
(if (consp cons-or-class) |
| 426 |
(and (eql-specializer-flag specializer) |
| 427 |
(eql (car cons-or-class) |
| 428 |
(eql-specializer-object specializer))) |
| 429 |
(eq cons-or-class specializer)))) |
| 430 |
(when (/= (length specializers) |
| 431 |
(length (generic-function-argument-precedence-order gf))) |
| 432 |
(error |
| 433 |
"The specializers list~%~A~%does not match the number of required arguments in ~A" |
| 434 |
specializers (generic-function-name gf))) |
| 435 |
(loop with specializers = (mapcar #'filter-specializer specializers) |
| 436 |
for method in (generic-function-methods gf) |
| 437 |
when (and (equal qualifiers (method-qualifiers method)) |
| 438 |
(every #'specializer= specializers (method-specializers method))) |
| 439 |
do (return-from find-method method)) |
| 440 |
;; If we did not find any matching method, then the list of |
| 441 |
;; specializers might have the wrong size and we must signal |
| 442 |
;; an error. |
| 443 |
(when errorp |
| 444 |
(error "There is no method on the generic function ~S that agrees on qualifiers ~S and specializers ~S" |
| 445 |
(generic-function-name gf) |
| 446 |
qualifiers specializers))) |
| 447 |
nil) |
| 448 |
|
| 449 |
;;; ---------------------------------------------------------------------- |
| 450 |
;;; with-accessors |
| 451 |
|
| 452 |
(defmacro with-accessors (slot-accessor-pairs instance-form &body body) |
| 453 |
(let* ((temp (gensym)) |
| 454 |
(accessors (do ((scan slot-accessor-pairs (cdr scan)) |
| 455 |
(res)) |
| 456 |
((null scan) (nreverse res)) |
| 457 |
(push `(,(caar scan) (,(cadar scan) ,temp)) res)))) |
| 458 |
`(let ((,temp ,instance-form)) |
| 459 |
(symbol-macrolet ,accessors ,@body)))) |
| 460 |
|
| 461 |
;;; Force the compiler into optimizing use of gethash inside methods: |
| 462 |
(setf (symbol-function 'SLOT-INDEX) (symbol-function 'GETHASH)) |