1;;;; This software is part of the SBCL system. See the README file for 2;;;; more information. 3 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 8;;;; information. 9 10;;;; copyright information from original PCL sources: 11;;;; 12;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation. 13;;;; All rights reserved. 14;;;; 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 18;;;; control laws. 19;;;; 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 22;;;; specification. 23 24(in-package "SB-PCL") 25 26#| 27 28The CommonLoops evaluator is meta-circular. 29 30Most of the code in PCL is methods on generic functions, including 31most of the code that actually implements generic functions and method 32lookup. 33 34So, we have a classic bootstrapping problem. The solution to this is 35to first get a cheap implementation of generic functions running, 36these are called early generic functions. These early generic 37functions and the corresponding early methods and early method lookup 38are used to get enough of the system running that it is possible to 39create real generic functions and methods and implement real method 40lookup. At that point (done in the file FIXUP) the function 41!FIX-EARLY-GENERIC-FUNCTIONS is called to convert all the early generic 42functions to real generic functions. 43 44The cheap generic functions are built using the same 45FUNCALLABLE-INSTANCE objects that real generic functions are made out of. 46This means that as PCL is being bootstrapped, the cheap generic 47function objects which are being created are the same objects which 48will later be real generic functions. This is good because: 49 - we don't cons garbage structure, and 50 - we can keep pointers to the cheap generic function objects 51 during booting because those pointers will still point to 52 the right object after the generic functions are all fixed up. 53 54This file defines the DEFMETHOD macro and the mechanism used to expand 55it. This includes the mechanism for processing the body of a method. 56DEFMETHOD basically expands into a call to LOAD-DEFMETHOD, which 57basically calls ADD-METHOD to add the method to the generic function. 58These expansions can be loaded either during bootstrapping or when PCL 59is fully up and running. 60 61An important effect of this arrangement is it means we can compile 62files with DEFMETHOD forms in them in a completely running PCL, but 63then load those files back in during bootstrapping. This makes 64development easier. It also means there is only one set of code for 65processing DEFMETHOD. Bootstrapping works by being sure to have 66LOAD-METHOD be careful to call only primitives which work during 67bootstrapping. 68 69|# 70 71(declaim (notinline make-a-method add-named-method 72 ensure-generic-function-using-class 73 add-method remove-method)) 74 75(defvar *!early-functions* 76 '((make-a-method !early-make-a-method real-make-a-method) 77 (add-named-method !early-add-named-method real-add-named-method))) 78 79;;; For each of the early functions, arrange to have it point to its 80;;; early definition. Do this in a way that makes sure that if we 81;;; redefine one of the early definitions the redefinition will take 82;;; effect. This makes development easier. 83(loop for (name early-name) in *!early-functions* 84 do (let ((early-name early-name)) 85 (setf (gdefinition name) 86 (set-fun-name 87 (lambda (&rest args) 88 (apply (fdefinition early-name) args)) 89 name)))) 90 91;;; *!GENERIC-FUNCTION-FIXUPS* is used by !FIX-EARLY-GENERIC-FUNCTIONS 92;;; to convert the few functions in the bootstrap which are supposed 93;;; to be generic functions but can't be early on. 94;;; 95;;; each entry is a list of the form 96;;; 97;;; (GENERIC-FUNCTION-NAME METHOD-COMBINATION-NAME METHODS) 98;;; 99;;; where methods is a list of lists of the form 100;;; 101;;; (LAMBDA-LIST SPECIALIZERS QUALIFIERS METHOD-BODY-FUNCTION-NAME) 102;;; 103;;;,where SPECIALIZERS is a list of class names. 104(defvar *!generic-function-fixups* 105 '((add-method 106 standard 107 ((generic-function method) 108 (standard-generic-function method) 109 () 110 real-add-method)) 111 112 (remove-method 113 standard 114 ((generic-function method) 115 (standard-generic-function method) 116 () 117 real-remove-method)) 118 119 (get-method 120 standard 121 ((generic-function qualifiers specializers &optional (errorp t)) 122 (standard-generic-function t t) 123 () 124 real-get-method)) 125 126 (ensure-generic-function-using-class 127 standard 128 ((generic-function fun-name 129 &key generic-function-class environment 130 &allow-other-keys) 131 (generic-function t) 132 () 133 real-ensure-gf-using-class--generic-function) 134 ((generic-function fun-name 135 &key generic-function-class environment 136 &allow-other-keys) 137 (null t) 138 () 139 real-ensure-gf-using-class--null)) 140 141 (make-method-lambda 142 standard 143 ((proto-generic-function proto-method lambda-expression environment) 144 (standard-generic-function standard-method t t) 145 () 146 real-make-method-lambda)) 147 148 (make-method-lambda-using-specializers 149 standard 150 ((proto-generic-function proto-method qualifiers specializers 151 lambda-expression environment) 152 (standard-generic-function standard-method t t t t) 153 () 154 real-make-method-lambda-using-specializers)) 155 156 (make-method-specializers-form 157 standard 158 ((proto-generic-function proto-method specializer-names environment) 159 (standard-generic-function standard-method t t) 160 () 161 real-make-method-specializers-form)) 162 163 (make-specializer-form-using-class 164 or 165 ((proto-generic-function proto-method specializer-name environment) 166 (standard-generic-function standard-method t t) 167 (or) 168 real-make-specializer-form-using-class/t) 169 ((proto-generic-function proto-method specializer-name environment) 170 (standard-generic-function standard-method specializer t) 171 (or) 172 real-make-specializer-form-using-class/specializer) 173 ((proto-generic-function proto-method specializer-name environment) 174 (standard-generic-function standard-method symbol t) 175 (or) 176 real-make-specializer-form-using-class/symbol) 177 ((proto-generic-function proto-method specializer-name environment) 178 (standard-generic-function standard-method cons t) 179 (or) 180 real-make-specializer-form-using-class/cons)) 181 182 (parse-specializer-using-class 183 standard 184 ((generic-function specializer) 185 (standard-generic-function t) 186 () 187 real-parse-specializer-using-class)) 188 189 (unparse-specializer-using-class 190 standard 191 ((generic-function specializer) 192 (standard-generic-function t) 193 () 194 real-unparse-specializer-using-class)) 195 196 (make-method-initargs-form 197 standard 198 ((proto-generic-function proto-method 199 lambda-expression 200 lambda-list environment) 201 (standard-generic-function standard-method t t t) 202 () 203 real-make-method-initargs-form)) 204 205 (compute-effective-method 206 standard 207 ((generic-function combin applicable-methods) 208 (generic-function standard-method-combination t) 209 () 210 standard-compute-effective-method) 211 ((generic-function combin applicable-methods) 212 (generic-function short-method-combination t) 213 () 214 short-compute-effective-method)))) 215 216(defmacro defgeneric (fun-name lambda-list &body options) 217 (declare (type list lambda-list)) 218 (unless (legal-fun-name-p fun-name) 219 (error 'simple-program-error 220 :format-control "illegal generic function name ~S" 221 :format-arguments (list fun-name))) 222 (check-gf-lambda-list lambda-list) 223 (let ((initargs ()) 224 (methods ())) 225 (flet ((duplicate-option (name) 226 (error 'simple-program-error 227 :format-control "The option ~S appears more than once." 228 :format-arguments (list name))) 229 (expand-method-definition (qab) ; QAB = qualifiers, arglist, body 230 (let* ((arglist-pos (position-if #'listp qab)) 231 (arglist (elt qab arglist-pos)) 232 (qualifiers (subseq qab 0 arglist-pos)) 233 (body (nthcdr (1+ arglist-pos) qab))) 234 `(push (defmethod ,fun-name ,@qualifiers ,arglist ,@body) 235 (generic-function-initial-methods (fdefinition ',fun-name)))))) 236 (macrolet ((initarg (key) `(getf initargs ,key))) 237 (dolist (option options) 238 (let ((car-option (car option))) 239 (case car-option 240 (declare 241 (dolist (spec (cdr option)) 242 (unless (consp spec) 243 (error 'simple-program-error 244 :format-control "~@<Invalid declaration specifier in ~ 245 DEFGENERIC: ~S~:@>" 246 :format-arguments (list spec))) 247 (when (member (first spec) 248 ;; FIXME: this list is slightly weird. 249 ;; ANSI (on the DEFGENERIC page) in one 250 ;; place allows only OPTIMIZE; in 251 ;; another place gives this list of 252 ;; disallowed declaration specifiers. 253 ;; This seems to be the only place where 254 ;; the FUNCTION declaration is 255 ;; mentioned; TYPE seems to be missing. 256 ;; Very strange. -- CSR, 2002-10-21 257 '(declaration ftype function 258 inline notinline special)) 259 (error 'simple-program-error 260 :format-control "The declaration specifier ~S ~ 261 is not allowed inside DEFGENERIC." 262 :format-arguments (list spec))) 263 (if (or (eq 'optimize (first spec)) 264 (info :declaration :recognized (first spec))) 265 (push spec (initarg :declarations)) 266 (warn "Ignoring unrecognized declaration in DEFGENERIC: ~S" 267 spec)))) 268 (:method-combination 269 (when (initarg car-option) 270 (duplicate-option car-option)) 271 (unless (symbolp (cadr option)) 272 (error 'simple-program-error 273 :format-control "METHOD-COMBINATION name not a ~ 274 symbol: ~S" 275 :format-arguments (list (cadr option)))) 276 (setf (initarg car-option) 277 `',(cdr option))) 278 (:argument-precedence-order 279 (let* ((required (nth-value 1 (parse-lambda-list lambda-list))) 280 (supplied (cdr option))) 281 (unless (= (length required) (length supplied)) 282 (error 'simple-program-error 283 :format-control "argument count discrepancy in ~ 284 :ARGUMENT-PRECEDENCE-ORDER clause." 285 :format-arguments nil)) 286 (when (set-difference required supplied) 287 (error 'simple-program-error 288 :format-control "unequal sets for ~ 289 :ARGUMENT-PRECEDENCE-ORDER clause: ~ 290 ~S and ~S" 291 :format-arguments (list required supplied))) 292 (setf (initarg car-option) 293 `',(cdr option)))) 294 ((:documentation :generic-function-class :method-class) 295 (unless (proper-list-of-length-p option 2) 296 (error "bad list length for ~S" option)) 297 (if (initarg car-option) 298 (duplicate-option car-option) 299 (setf (initarg car-option) `',(cadr option)))) 300 (:method 301 (push (cdr option) methods)) 302 (t 303 ;; ANSI requires that unsupported things must get a 304 ;; PROGRAM-ERROR. 305 (error 'simple-program-error 306 :format-control "unsupported option ~S" 307 :format-arguments (list option)))))) 308 309 (when (initarg :declarations) 310 (setf (initarg :declarations) 311 `',(initarg :declarations)))) 312 `(progn 313 (eval-when (:compile-toplevel :load-toplevel :execute) 314 (compile-or-load-defgeneric ',fun-name)) 315 (load-defgeneric ',fun-name ',lambda-list 316 (sb-c:source-location) ,@initargs) 317 ,@(mapcar #'expand-method-definition methods) 318 (fdefinition ',fun-name))))) 319 320(defun compile-or-load-defgeneric (fun-name) 321 (proclaim-as-fun-name fun-name) 322 (when (typep fun-name '(cons (eql setf))) 323 (sb-c::warn-if-setf-macro fun-name)) 324 (note-name-defined fun-name :function) 325 (unless (eq (info :function :where-from fun-name) :declared) 326 ;; Hmm. This is similar to BECOME-DEFINED-FUN-NAME 327 ;; except that it doesn't clear an :ASSUMED-TYPE. Should it? 328 (setf (info :function :where-from fun-name) :defined) 329 (setf (info :function :type fun-name) 330 (specifier-type 'function)))) 331 332(defun load-defgeneric (fun-name lambda-list source-location &rest initargs) 333 (when (fboundp fun-name) 334 (warn 'sb-kernel:redefinition-with-defgeneric 335 :name fun-name 336 :new-location source-location) 337 (let ((fun (fdefinition fun-name))) 338 (when (generic-function-p fun) 339 (loop for method in (generic-function-initial-methods fun) 340 do (remove-method fun method)) 341 (setf (generic-function-initial-methods fun) '())))) 342 (apply #'ensure-generic-function 343 fun-name 344 :lambda-list lambda-list 345 :definition-source source-location 346 initargs)) 347 348(define-condition generic-function-lambda-list-error 349 (reference-condition simple-program-error) 350 () 351 (:default-initargs :references (list '(:ansi-cl :section (3 4 2))))) 352 353(defun check-gf-lambda-list (lambda-list) 354 (flet ((verify-each-atom-or-singleton (kind args) 355 ;; PARSE-LAMBDA-LIST validates the skeleton, 356 ;; so just check for incorrect use of defaults. 357 ;; This works for both &OPTIONAL and &KEY. 358 (dolist (arg args) 359 (or (not (listp arg)) 360 (null (cdr arg)) 361 (error 'generic-function-lambda-list-error 362 :format-control 363 "~@<invalid ~A argument specifier ~S ~_in the ~ 364generic function lambda list ~S~:>" 365 :format-arguments (list kind arg lambda-list)))))) 366 (multiple-value-bind (llks required optional rest keys) 367 (parse-lambda-list 368 lambda-list 369 :accept (lambda-list-keyword-mask 370 '(&optional &rest &key &allow-other-keys)) 371 :condition-class 'generic-function-lambda-list-error 372 :context "a generic function lambda list") 373 (declare (ignore llks required rest)) 374 ;; no defaults or supplied-p vars allowed for &OPTIONAL or &KEY 375 (verify-each-atom-or-singleton '&optional optional) 376 (verify-each-atom-or-singleton '&key keys)))) 377 378(defun check-method-lambda (method-lambda context) 379 (unless (typep method-lambda '(cons (eql lambda))) 380 (error "~@<The METHOD-LAMBDA argument to ~ 381 ~/sb-impl:print-symbol-with-prefix/, ~S, is not a lambda ~ 382 form.~@:>" 383 context method-lambda)) 384 method-lambda) 385 386(eval-when (:compile-toplevel :load-toplevel :execute) 387 (fmakunbound 'defmethod)) 388;;; As per CLHS - 389;;; "defmethod is not required to perform any compile-time side effects." 390;;; and we don't do much other than to make the function be defined, 391;;; which means that checking of callers' arglists can only occur after called 392;;; methods are actually loaded. 393(defmacro defmethod (name &rest args) 394 (multiple-value-bind (qualifiers lambda-list body) 395 (parse-defmethod args) 396 `(progn 397 (eval-when (:compile-toplevel :execute) 398 ;; :compile-toplevel is needed for subsequent forms 399 ;; :execute is needed for references to itself inside the body 400 (compile-or-load-defgeneric ',name)) 401 ;; KLUDGE: this double expansion is quite a monumental 402 ;; workaround: it comes about because of a fantastic interaction 403 ;; between the processing rules of CLHS 3.2.3.1 and the 404 ;; bizarreness of MAKE-METHOD-LAMBDA. 405 ;; 406 ;; MAKE-METHOD-LAMBDA can be called by the user, and if the 407 ;; lambda itself doesn't refer to outside bindings the return 408 ;; value must be compileable in the null lexical environment. 409 ;; However, the function must also refer somehow to the 410 ;; associated method object, so that it can call NO-NEXT-METHOD 411 ;; with the appropriate arguments if there is no next method -- 412 ;; but when the function is generated, the method object doesn't 413 ;; exist yet. 414 ;; 415 ;; In order to resolve this issue, we insert a literal cons cell 416 ;; into the body of the method lambda, return the same cons cell 417 ;; as part of the second (initargs) return value of 418 ;; MAKE-METHOD-LAMBDA, and a method on INITIALIZE-INSTANCE fills 419 ;; in the cell when the method is created. However, this 420 ;; strategy depends on having a fresh cons cell for every method 421 ;; lambda, which (without the workaround below) is skewered by 422 ;; the processing in CLHS 3.2.3.1, which permits implementations 423 ;; to macroexpand the bodies of EVAL-WHEN forms with both 424 ;; :COMPILE-TOPLEVEL and :LOAD-TOPLEVEL only once. The 425 ;; expansion below forces the double expansion in those cases, 426 ;; while expanding only once in the common case. 427 (eval-when (:load-toplevel) 428 (%defmethod-expander ,name ,qualifiers ,lambda-list ,body)) 429 (eval-when (:execute) 430 (%defmethod-expander ,name ,qualifiers ,lambda-list ,body))))) 431 432(defmacro %defmethod-expander 433 (name qualifiers lambda-list body &environment env) 434 (multiple-value-bind (proto-gf proto-method) 435 (prototypes-for-make-method-lambda name) 436 (expand-defmethod name proto-gf proto-method qualifiers 437 lambda-list body env))) 438 439 440(defun prototypes-for-make-method-lambda (name) 441 (if (not (eq **boot-state** 'complete)) 442 (values nil nil) 443 (let ((gf? (and (fboundp name) 444 (gdefinition name)))) 445 (if (or (null gf?) 446 (not (generic-function-p gf?))) 447 (values (class-prototype (find-class 'standard-generic-function)) 448 (class-prototype (find-class 'standard-method))) 449 (values gf? 450 (class-prototype (or (generic-function-method-class gf?) 451 (find-class 'standard-method)))))))) 452 453;;; Take a name which is either a generic function name or a list specifying 454;;; a SETF generic function (like: (SETF <generic-function-name>)). Return 455;;; the prototype instance of the method-class for that generic function. 456;;; 457;;; If there is no generic function by that name, this returns the 458;;; default value, the prototype instance of the class 459;;; STANDARD-METHOD. This default value is also returned if the spec 460;;; names an ordinary function or even a macro. In effect, this leaves 461;;; the signalling of the appropriate error until load time. 462;;; 463;;; Note: During bootstrapping, this function is allowed to return NIL. 464(defun method-prototype-for-gf (name) 465 (let ((gf? (and (fboundp name) 466 (gdefinition name)))) 467 (cond ((neq **boot-state** 'complete) nil) 468 ((or (null gf?) 469 (not (generic-function-p gf?))) ; Someone else MIGHT 470 ; error at load time. 471 (class-prototype (find-class 'standard-method))) 472 (t 473 (class-prototype (or (generic-function-method-class gf?) 474 (find-class 'standard-method))))))) 475 476;;; These are used to communicate the method name and lambda-list to 477;;; MAKE-METHOD-LAMBDA-INTERNAL. 478(defvar *method-name* nil) 479(defvar *method-lambda-list* nil) 480 481(defun expand-defmethod (name proto-gf proto-method qualifiers lambda-list 482 body env) 483 (binding* (;; ENV could be of type SB!INTERPRETER:BASIC-ENV but I 484 ;; don't care to figure out what parts of PCL would have 485 ;; to change to accept that, so coerce. 486 (env (sb-kernel:coerce-to-lexenv env)) 487 ((nil unspecialized-lambda-list specializers) 488 (parse-specialized-lambda-list lambda-list)) 489 (*method-name* `(,name ,@qualifiers ,specializers)) 490 (method-lambda `(lambda ,unspecialized-lambda-list ,@body)) 491 ((method-function-lambda initargs new-lambda-list) 492 (make-method-lambda-using-specializers 493 proto-gf proto-method qualifiers specializers method-lambda env)) 494 (initargs-form 495 (make-method-initargs-form 496 proto-gf proto-method method-function-lambda initargs env)) 497 (specializers-form 498 (make-method-specializers-form 499 proto-gf proto-method specializers env))) 500 (mapc (lambda (specializer) 501 (when (typep specializer 'type-specifier) 502 (check-deprecated-type specializer))) 503 specializers) 504 ;; Note: We could DECLAIM the ftype of the generic function here, 505 ;; since ANSI specifies that we create it if it does not 506 ;; exist. However, I chose not to, because I think it's more 507 ;; useful to support a style of programming where every generic 508 ;; function has an explicit DEFGENERIC and any typos in DEFMETHODs 509 ;; are warned about. Otherwise 510 ;; 511 ;; (DEFGENERIC FOO-BAR-BLETCH (X)) 512 ;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..) 513 ;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..) 514 ;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..) 515 ;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..) 516 ;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..) 517 ;; 518 ;; compiles without raising an error and runs without raising an 519 ;; error (since SIMPLE-VECTOR cases fall through to VECTOR) but 520 ;; still doesn't do what was intended. I hate that kind of bug 521 ;; (code which silently gives the wrong answer), so we don't do a 522 ;; DECLAIM here. -- WHN 20000229 523 (make-defmethod-form name qualifiers specializers-form 524 (or new-lambda-list unspecialized-lambda-list) 525 (if proto-method 526 (class-name (class-of proto-method)) 527 'standard-method) 528 initargs-form))) 529 530(defun interned-symbol-p (x) 531 (and (symbolp x) (symbol-package x))) 532 533(defun make-defmethod-form 534 (name qualifiers specializers unspecialized-lambda-list 535 method-class-name initargs-form) 536 (declare (sb-ext:muffle-conditions sb-ext:code-deletion-note)) 537 (let (fn 538 fn-lambda) 539 (if (and (interned-symbol-p (fun-name-block-name name)) 540 (every #'interned-symbol-p qualifiers) 541 (every (lambda (s) 542 (if (consp s) 543 (and (eq (car s) 'eql) 544 (constantp (cadr s)) 545 (let ((sv (constant-form-value (cadr s)))) 546 (or (interned-symbol-p sv) 547 (integerp sv) 548 (and (characterp sv) 549 (standard-char-p sv))))) 550 (interned-symbol-p s))) 551 specializers) 552 (consp initargs-form) 553 (eq (car initargs-form) 'list*) 554 (memq (cadr initargs-form) '(:function)) 555 (consp (setq fn (caddr initargs-form))) 556 (eq (car fn) 'function) 557 (consp (setq fn-lambda (cadr fn))) 558 (eq (car fn-lambda) 'lambda) 559 (bug "Really got here")) 560 (let* ((specls (mapcar (lambda (specl) 561 (if (consp specl) 562 ;; CONSTANT-FORM-VALUE? What I 563 ;; kind of want to know, though, 564 ;; is what happens if we don't do 565 ;; this for some slow-method 566 ;; function because of a hairy 567 ;; lexenv -- is the only bad 568 ;; effect that the method 569 ;; function ends up unnamed? If 570 ;; so, couldn't we arrange to 571 ;; name it later? 572 `(,(car specl) ,(eval (cadr specl))) 573 specl)) 574 specializers)) 575 (mname `(,(if (eq (cadr initargs-form) :function) 576 'slow-method 'fast-method) 577 ,name ,@qualifiers ,specls))) 578 `(progn 579 (defun ,mname ,(cadr fn-lambda) 580 ,@(cddr fn-lambda)) 581 ,(make-defmethod-form-internal 582 name qualifiers `',specls 583 unspecialized-lambda-list method-class-name 584 `(list* ,(cadr initargs-form) 585 #',mname 586 ,@(cdddr initargs-form))))) 587 (make-defmethod-form-internal 588 name qualifiers 589 specializers 590 #+nil 591 `(list ,@(mapcar (lambda (specializer) 592 (if (consp specializer) 593 ``(,',(car specializer) 594 ,,(cadr specializer)) 595 `',specializer)) 596 specializers)) 597 unspecialized-lambda-list 598 method-class-name 599 initargs-form)))) 600 601(defun make-defmethod-form-internal 602 (name qualifiers specializers-form unspecialized-lambda-list 603 method-class-name initargs-form) 604 `(load-defmethod 605 ',method-class-name 606 ',name 607 ',qualifiers 608 ,specializers-form 609 ',unspecialized-lambda-list 610 ,initargs-form 611 (sb-c:source-location))) 612 613(defmacro make-method-function (method-lambda &environment env) 614 (binding* (((proto-gf proto-method) 615 (prototypes-for-make-method-lambda nil)) 616 ((method-function-lambda initargs) 617 (make-method-lambda proto-gf proto-method method-lambda env))) ; FIXME: coerce-to-lexenv? 618 (make-method-initargs-form 619 proto-gf proto-method method-function-lambda initargs env))) 620 621(defun real-make-method-initargs-form (proto-gf proto-method 622 method-lambda initargs env) 623 (declare (ignore proto-gf proto-method)) 624 (check-method-lambda method-lambda 'make-method-initargs) 625 (make-method-initargs-form-internal method-lambda initargs env)) 626 627(unless (fboundp 'make-method-initargs-form) 628 (setf (gdefinition 'make-method-initargs-form) 629 (symbol-function 'real-make-method-initargs-form))) 630 631(defun real-make-method-lambda-using-specializers 632 (proto-gf proto-method qualifiers specializers method-lambda env) 633 (declare (ignore qualifiers)) 634 (check-method-lambda method-lambda 'make-method-lambda) ; TODO remove check in make-method-lambda 635 ;; Default behavior: delegate to MAKE-METHOD-LAMBDA. 636 (let* ((lambda-list (second method-lambda)) 637 (*method-lambda-list* 638 (append 639 (mapcar #'list (subseq lambda-list 0 (length specializers)) specializers) 640 (subseq lambda-list (length specializers))))) 641 (make-method-lambda proto-gf proto-method method-lambda env))) 642 643(unless (fboundp 'make-method-lambda-using-specializers) 644 (setf (gdefinition 'make-method-lambda-using-specializers) 645 (symbol-function 'real-make-method-lambda-using-specializers))) 646 647;;; When bootstrapping PCL MAKE-METHOD-LAMBDA starts out as a regular 648;;; function: REAL-MAKE-METHOD-LAMBDA set to the fdefinition of 649;;; MAKE-METHOD-LAMBDA. Once generic functions are born, 650;;; REAL-MAKE-METHOD-LAMBDA is used to implement the default method. 651;;; MAKE-METHOD-LAMBDA-INTERNAL is split out into a separate function 652;;; so that changing it in a live image is easy, and changes actually 653;;; take effect. 654(defun real-make-method-lambda (proto-gf proto-method method-lambda env) 655 (make-method-lambda-internal proto-gf proto-method method-lambda env)) 656 657(unless (fboundp 'make-method-lambda) 658 (setf (gdefinition 'make-method-lambda) 659 (symbol-function 'real-make-method-lambda))) 660 661(defun declared-specials (declarations) 662 (loop for (declare . specifiers) in declarations 663 append (loop for specifier in specifiers 664 when (eq 'special (car specifier)) 665 append (cdr specifier)))) 666 667(defun make-method-lambda-internal (proto-gf proto-method method-lambda env) 668 (declare (ignore proto-gf proto-method)) 669 (check-method-lambda method-lambda 'make-method-lambda) 670 (multiple-value-bind (real-body declarations documentation) 671 (parse-body (cddr method-lambda) t) 672 ;; We have the %METHOD-NAME declaration in the place where we expect it only 673 ;; if there is are no non-standard prior MAKE-METHOD-LAMBDA methods -- or 674 ;; unless they're fantastically unintrusive. 675 (let* ((method-name *method-name*) 676 (method-lambda-list *method-lambda-list*) 677 ;; Macroexpansion caused by code-walking may call make-method-lambda and 678 ;; end up with wrong values 679 (*method-name* nil) 680 (*method-lambda-list* nil) 681 (generic-function-name (when method-name (car method-name))) 682 (specialized-lambda-list (or method-lambda-list 683 (ecase (car method-lambda) 684 (lambda (second method-lambda)) 685 (named-lambda (third method-lambda))))) 686 ;; the method-cell is a way of communicating what method a 687 ;; method-function implements, for the purpose of 688 ;; NO-NEXT-METHOD. We need something that can be shared 689 ;; between function and initargs, but not something that 690 ;; will be coalesced as a constant (because we are naughty, 691 ;; oh yes) with the expansion of any other methods in the 692 ;; same file. -- CSR, 2007-05-30 693 (method-cell (list (make-symbol "METHOD-CELL")))) 694 (multiple-value-bind (parameters lambda-list specializers) 695 (parse-specialized-lambda-list specialized-lambda-list) 696 (let* ((required-parameters 697 (mapcar (lambda (r s) (declare (ignore s)) r) 698 parameters 699 specializers)) 700 (slots (mapcar #'list required-parameters)) 701 (class-declarations 702 `(declare 703 ;; These declarations seem to be used by PCL to pass 704 ;; information to itself; when I tried to delete 'em 705 ;; ca. 0.6.10 it didn't work. I'm not sure how 706 ;; they work, but note the (VAR-DECLARATION '%CLASS ..) 707 ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30 708 ,@(remove nil 709 (mapcar (lambda (a s) (and (symbolp s) 710 (neq s t) 711 `(%class ,a ,s))) 712 parameters 713 specializers)) 714 ;; These TYPE declarations weren't in the original 715 ;; PCL code, but the Python compiler likes them a 716 ;; lot. (We're telling the compiler about our 717 ;; knowledge of specialized argument types so that 718 ;; it can avoid run-time type dispatch overhead, 719 ;; which can be a huge win for Python.) 720 ,@(let ((specials (declared-specials declarations))) 721 (mapcar (lambda (par spec) 722 (parameter-specializer-declaration-in-defmethod 723 par spec specials env)) 724 parameters 725 specializers)))) 726 (method-lambda 727 ;; Remove the documentation string and insert the 728 ;; appropriate class declarations. The documentation 729 ;; string is removed to make it easy for us to insert 730 ;; new declarations later, they will just go after the 731 ;; CADR of the method lambda. The class declarations 732 ;; are inserted to communicate the class of the method's 733 ;; arguments to the code walk. 734 `(lambda ,lambda-list 735 ;; The default ignorability of method parameters 736 ;; doesn't seem to be specified by ANSI. PCL had 737 ;; them basically ignorable but was a little 738 ;; inconsistent. E.g. even though the two 739 ;; method definitions 740 ;; (DEFMETHOD FOO ((X T) (Y T)) "Z") 741 ;; (DEFMETHOD FOO ((X T) Y) "Z") 742 ;; are otherwise equivalent, PCL treated Y as 743 ;; ignorable in the first definition but not in the 744 ;; second definition. We make all required 745 ;; parameters ignorable as a way of systematizing 746 ;; the old PCL behavior. -- WHN 2000-11-24 747 (declare (ignorable ,@required-parameters)) 748 ,class-declarations 749 ,@declarations 750 (block ,(fun-name-block-name generic-function-name) 751 ,@real-body))) 752 (constant-value-p (and (null (cdr real-body)) 753 (constantp (car real-body)))) 754 (constant-value (and constant-value-p 755 (constant-form-value (car real-body)))) 756 (plist (and constant-value-p 757 (or (typep constant-value 758 '(or number character)) 759 (and (symbolp constant-value) 760 (symbol-package constant-value))) 761 (list :constant-value constant-value))) 762 (applyp (dolist (p lambda-list nil) 763 (cond ((memq p '(&optional &rest &key)) 764 (return t)) 765 ((eq p '&aux) 766 (return nil)))))) 767 (multiple-value-bind (walked-lambda call-next-method-p setq-p 768 parameters-setqd) 769 (walk-method-lambda method-lambda 770 required-parameters 771 env 772 slots) 773 (multiple-value-bind (walked-lambda-body 774 walked-declarations 775 walked-documentation) 776 (parse-body (cddr walked-lambda) t) 777 (declare (ignore walked-documentation)) 778 (when (some #'cdr slots) 779 (let ((slot-name-lists (slot-name-lists-from-slots slots))) 780 (setq plist 781 `(,@(when slot-name-lists 782 `(:slot-name-lists ,slot-name-lists)) 783 ,@plist)) 784 (setq walked-lambda-body 785 `((pv-binding (,required-parameters 786 ,slot-name-lists 787 (load-time-value 788 (intern-pv-table 789 :slot-name-lists ',slot-name-lists))) 790 ,@walked-lambda-body))))) 791 (when (and (memq '&key lambda-list) 792 (not (memq '&allow-other-keys lambda-list))) 793 (let ((aux (memq '&aux lambda-list))) 794 (setq lambda-list (nconc (ldiff lambda-list aux) 795 (list '&allow-other-keys) 796 aux)))) 797 (values `(lambda (.method-args. .next-methods.) 798 (simple-lexical-method-functions 799 (,lambda-list .method-args. .next-methods. 800 :call-next-method-p 801 ,(when call-next-method-p t) 802 :setq-p ,setq-p 803 :parameters-setqd ,parameters-setqd 804 :method-cell ,method-cell 805 :applyp ,applyp) 806 ,@walked-declarations 807 (locally 808 (declare (disable-package-locks 809 %parameter-binding-modified)) 810 (symbol-macrolet ((%parameter-binding-modified 811 ',@parameters-setqd)) 812 (declare (enable-package-locks 813 %parameter-binding-modified)) 814 ,@walked-lambda-body)))) 815 `(,@(when call-next-method-p `(method-cell ,method-cell)) 816 ,@(when (member call-next-method-p '(:simple nil)) 817 '(simple-next-method-call t)) 818 ,@(when plist `(plist ,plist)) 819 ,@(when documentation `(:documentation ,documentation))))))))))) 820 821(defun real-make-method-specializers-form 822 (proto-generic-function proto-method specializer-names environment) 823 (flet ((make-parse-form (name) 824 (make-specializer-form-using-class 825 proto-generic-function proto-method name environment))) 826 `(list ,@(mapcar #'make-parse-form specializer-names)))) 827 828(unless (fboundp 'make-method-specializers-form) 829 (setf (gdefinition 'make-method-specializers-form) 830 (symbol-function 'real-make-method-specializers-form))) 831 832(defun real-make-specializer-form-using-class/t 833 (proto-generic-function proto-method specializer-name environment) 834 (declare (ignore proto-generic-function proto-method environment)) 835 (error 'simple-reference-error 836 :format-control 837 "~@<~S is not a valid parameter specializer name.~@:>" 838 :format-arguments (list specializer-name) 839 :references (list '(:ansi-cl :macro defmethod) 840 '(:ansi-cl :glossary "parameter specializer name")))) 841 842(defun real-make-specializer-form-using-class/specializer 843 (proto-generic-function proto-method specializer-name environment) 844 (declare (ignore proto-generic-function proto-method environment)) 845 (when (eq **boot-state** 'complete) 846 specializer-name)) 847 848(defun real-make-specializer-form-using-class/symbol 849 (proto-generic-function proto-method specializer-name environment) 850 (declare (ignore proto-generic-function proto-method environment)) 851 `(find-class ',specializer-name)) 852 853(defun real-make-specializer-form-using-class/cons 854 (proto-generic-function proto-method specializer-name environment) 855 (declare (ignore proto-generic-function proto-method environment)) 856 ;; In case of unknown specializer or known specializer with syntax 857 ;; error, TYPECASE may fall through to default method with error 858 ;; signaling. 859 (typecase specializer-name 860 ((cons (eql eql) (cons t null)) 861 `(intern-eql-specializer ,(second specializer-name))) 862 ((cons (eql class-eq) (cons t null)) 863 `(class-eq-specializer (find-class ',(second specializer-name)))))) 864 865(defun real-make-specializer-form-using-class 866 (proto-generic-function proto-method specializer-name environment) 867 (macrolet 868 ((delegations () 869 `(typecase specializer-name 870 ,@(mapcar 871 (lambda (type) 872 (let ((function-name 873 (symbolicate 874 'real-make-specializer-form-using-class '#:/ type))) 875 `(,type 876 (,function-name 877 proto-generic-function proto-method specializer-name environment)))) 878 '(; specializer 879 ; ^ apparently not needed during bootstrapping 880 symbol cons t))))) 881 (delegations))) 882 883(unless (fboundp 'make-specializer-form-using-class) 884 (setf (gdefinition 'make-specializer-form-using-class) 885 (symbol-function 'real-make-specializer-form-using-class))) 886 887(defun real-parse-specializer-using-class (generic-function specializer) 888 (let ((result (specializer-from-type specializer))) 889 (if (specializerp result) 890 result 891 (error "~@<~S cannot be parsed as a specializer for ~S.~@:>" 892 specializer generic-function)))) 893 894(unless (fboundp 'parse-specializer-using-class) 895 (setf (gdefinition 'parse-specializer-using-class) 896 (symbol-function 'real-parse-specializer-using-class))) 897 898(defun real-unparse-specializer-using-class (generic-function specializer) 899 (if (specializerp specializer) 900 ;; FIXME: this HANDLER-CASE is a bit of a hammer to crack a nut: 901 ;; the idea is that we want to unparse permissively, so that the 902 ;; lazy (or rather the "portable") specializer extender (who 903 ;; does not define methods on these new SBCL-specific MOP 904 ;; functions) can still subclass specializer and define methods 905 ;; without everything going wrong. Making it cleaner and 906 ;; clearer that that is what we are defending against would be 907 ;; nice. -- CSR, 2007-06-01 908 (handler-case 909 (let ((type (specializer-type specializer))) 910 (if (and (consp type) (eq (car type) 'class)) 911 (let* ((class (cadr type)) 912 (class-name (class-name class))) 913 (if (eq class (find-class class-name nil)) 914 class-name 915 type)) 916 type)) 917 (error () specializer)) 918 (error "~@<~S is not a legal specializer for ~S.~@:>" 919 specializer generic-function))) 920 921(unless (fboundp 'unparse-specializer-using-class) 922 (setf (gdefinition 'unparse-specializer-using-class) 923 (symbol-function 'real-unparse-specializer-using-class))) 924 925;;; a helper function for creating Python-friendly type declarations 926;;; in DEFMETHOD forms. 927;;; 928;;; We're too lazy to cons up a new environment for this, so we just pass in 929;;; the list of locally declared specials in addition to the old environment. 930(defun parameter-specializer-declaration-in-defmethod 931 (parameter specializer specials env) 932 (cond ((and (consp specializer) 933 (eq (car specializer) 'eql)) 934 ;; KLUDGE: ANSI, in its wisdom, says that 935 ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at 936 ;; DEFMETHOD expansion time. Thus, although one might think 937 ;; that in 938 ;; (DEFMETHOD FOO ((X PACKAGE) 939 ;; (Y (EQL 12)) 940 ;; ..)) 941 ;; the PACKAGE and (EQL 12) forms are both parallel type 942 ;; names, they're not, as is made clear when you do 943 ;; (DEFMETHOD FOO ((X PACKAGE) 944 ;; (Y (EQL 'BAR))) 945 ;; ..) 946 ;; where Y needs to be a symbol named "BAR", not some cons 947 ;; made by (CONS 'QUOTE 'BAR). I.e. when the 948 ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument 949 ;; to be of type (EQL X). It'd be easy to transform one to 950 ;; the other, but it'd be somewhat messier to do so while 951 ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd 952 ;; once. (The new code wouldn't be messy, but it'd require a 953 ;; big transformation of the old code.) So instead we punt. 954 ;; -- WHN 20000610 955 '(ignorable)) 956 ((member specializer 957 ;; KLUDGE: For some low-level implementation 958 ;; classes, perhaps because of some problems related 959 ;; to the incomplete integration of PCL into SBCL's 960 ;; type system, some specializer classes can't be 961 ;; declared as argument types. E.g. 962 ;; (DEFMETHOD FOO ((X SLOT-OBJECT)) 963 ;; (DECLARE (TYPE SLOT-OBJECT X)) 964 ;; ..) 965 ;; loses when 966 ;; (DEFSTRUCT BAR A B) 967 ;; (FOO (MAKE-BAR)) 968 ;; perhaps because of the way that STRUCTURE-OBJECT 969 ;; inherits both from SLOT-OBJECT and from 970 ;; SB-KERNEL:INSTANCE. In an effort to sweep such 971 ;; problems under the rug, we exclude these problem 972 ;; cases by blacklisting them here. -- WHN 2001-01-19 973 (list 'slot-object #+nil (find-class 'slot-object))) 974 '(ignorable)) 975 ((not (eq **boot-state** 'complete)) 976 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with 977 ;; types which don't match their specializers. (Specifically, 978 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL 979 ;; second argument.) Hopefully it only does this kind of 980 ;; weirdness when bootstrapping.. -- WHN 20000610 981 '(ignorable)) 982 ((typep specializer 'eql-specializer) 983 `(type (eql ,(eql-specializer-object specializer)) ,parameter)) 984 ((or (var-special-p parameter env) (member parameter specials)) 985 ;; Don't declare types for special variables -- our rebinding magic 986 ;; for SETQ cases don't work right there as SET, (SETF SYMBOL-VALUE), 987 ;; etc. make things undecidable. 988 '(ignorable)) 989 (t 990 ;; Otherwise, we can usually make Python very happy. 991 ;; 992 ;; KLUDGE: Since INFO doesn't work right for class objects here, 993 ;; and they are valid specializers, see if the specializer is 994 ;; a named class, and use the name in that case -- otherwise 995 ;; the class instance is ok, since info will just return NIL, NIL. 996 ;; 997 ;; We still need to deal with the class case too, but at 998 ;; least #.(find-class 'integer) and integer as equivalent 999 ;; specializers with this. 1000 (let* ((specializer-nameoid 1001 (if (and (typep specializer 'class) 1002 (let ((name (class-name specializer))) 1003 (and name (symbolp name) 1004 (eq specializer (find-class name nil))))) 1005 (class-name specializer) 1006 specializer)) 1007 (kind (info :type :kind specializer-nameoid))) 1008 1009 (flet ((specializer-nameoid-class () 1010 (typecase specializer-nameoid 1011 (symbol (find-class specializer-nameoid nil)) 1012 (class specializer-nameoid) 1013 (class-eq-specializer 1014 (specializer-class specializer-nameoid)) 1015 (t nil)))) 1016 (ecase kind 1017 ((:primitive) `(type ,specializer-nameoid ,parameter)) 1018 ((:defined) 1019 (let ((class (specializer-nameoid-class))) 1020 ;; CLASS can be null here if the user has 1021 ;; erroneously tried to use a defined type as a 1022 ;; specializer; it can be a non-SYSTEM-CLASS if 1023 ;; the user defines a type and calls (SETF 1024 ;; FIND-CLASS) in a consistent way. 1025 (when (and class (typep class 'system-class)) 1026 `(type ,(class-name class) ,parameter)))) 1027 ((:instance nil) 1028 (let ((class (specializer-nameoid-class))) 1029 (cond 1030 (class 1031 (if (typep class '(or system-class structure-class)) 1032 `(type ,class ,parameter) 1033 ;; don't declare CLOS classes as parameters; 1034 ;; it's too expensive. 1035 '(ignorable))) 1036 (t 1037 ;; we can get here, and still not have a failure 1038 ;; case, by doing MOP programming like (PROGN 1039 ;; (ENSURE-CLASS 'FOO) (DEFMETHOD BAR ((X FOO)) 1040 ;; ...)). Best to let the user know we haven't 1041 ;; been able to extract enough information: 1042 (style-warn 1043 "~@<can't find type for specializer ~S in ~S.~@:>" 1044 specializer-nameoid 1045 'parameter-specializer-declaration-in-defmethod) 1046 '(ignorable))))) 1047 ((:forthcoming-defclass-type) 1048 '(ignorable)))))))) 1049 1050;;; For passing a list (groveled by the walker) of the required 1051;;; parameters whose bindings are modified in the method body to the 1052;;; optimized-slot-value* macros. 1053(define-symbol-macro %parameter-binding-modified ()) 1054 1055(defmacro simple-lexical-method-functions ((lambda-list 1056 method-args 1057 next-methods 1058 &rest lmf-options) 1059 &body body) 1060 `(progn 1061 ,method-args ,next-methods 1062 (bind-simple-lexical-method-functions (,method-args ,next-methods 1063 ,lmf-options) 1064 (bind-args (,lambda-list ,method-args) 1065 ,@body)))) 1066 1067(defmacro fast-lexical-method-functions ((lambda-list 1068 next-method-call 1069 args 1070 rest-arg 1071 &rest lmf-options) 1072 &body body) 1073 `(bind-fast-lexical-method-functions (,args ,rest-arg ,next-method-call ,lmf-options) 1074 (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg) 1075 ,@body))) 1076 1077(defmacro bind-simple-lexical-method-functions 1078 ((method-args next-methods (&key call-next-method-p setq-p 1079 parameters-setqd applyp method-cell)) 1080 &body body 1081 &environment env) 1082 (declare (ignore parameters-setqd)) 1083 (if (not (or call-next-method-p setq-p applyp)) 1084 ;; always provide the lexical function NEXT-METHOD-P. 1085 ;; I would think this to be a good candidate for declaring INLINE 1086 ;; but that's not the way it was done before. 1087 `(flet ((next-method-p () (not (null (car ,next-methods))))) 1088 (declare (ignorable #'next-method-p)) 1089 ,@body) 1090 `(let ((.next-method. (car ,next-methods)) 1091 (,next-methods (cdr ,next-methods))) 1092 (declare (ignorable .next-method. ,next-methods)) 1093 (flet (,@(when call-next-method-p 1094 `((call-next-method (&rest cnm-args) 1095 (declare (dynamic-extent cnm-args)) 1096 ,@(if (safe-code-p env) 1097 `((%check-cnm-args cnm-args 1098 ,method-args 1099 ',method-cell)) 1100 nil) 1101 (if .next-method. 1102 (funcall (if (std-instance-p .next-method.) 1103 (method-function .next-method.) 1104 .next-method.) ; for early methods 1105 (or cnm-args ,method-args) 1106 ,next-methods) 1107 (apply #'call-no-next-method 1108 ',method-cell 1109 (or cnm-args ,method-args)))))) 1110 (next-method-p () (not (null .next-method.)))) 1111 (declare (ignorable #'next-method-p)) 1112 ,@body)))) 1113 1114(defun call-no-next-method (method-cell &rest args) 1115 (let ((method (car method-cell))) 1116 (aver method) 1117 ;; Can't easily provide a RETRY restart here, as the return value here is 1118 ;; for the method, not the generic function. 1119 (apply #'no-next-method (method-generic-function method) 1120 method args))) 1121 1122(defun call-no-applicable-method (gf args) 1123 (restart-case 1124 (apply #'no-applicable-method gf args) 1125 (retry () 1126 :report "Retry calling the generic function." 1127 (apply gf args)))) 1128 1129(defun call-no-primary-method (gf args) 1130 (restart-case 1131 (apply #'no-primary-method gf args) 1132 (retry () 1133 :report "Retry calling the generic function." 1134 (apply gf args)))) 1135 1136(defstruct (method-call (:copier nil)) 1137 (function #'identity :type function) 1138 call-method-args) 1139(defstruct (constant-method-call (:copier nil) (:include method-call)) 1140 value) 1141 1142#-sb-fluid (declaim (sb-ext:freeze-type method-call)) 1143 1144(defmacro invoke-method-call1 (function args cm-args) 1145 `(let ((.function. ,function) 1146 (.args. ,args) 1147 (.cm-args. ,cm-args)) 1148 (if (and .cm-args. (null (cdr .cm-args.))) 1149 (funcall .function. .args. (car .cm-args.)) 1150 (apply .function. .args. .cm-args.)))) 1151 1152(defmacro invoke-method-call (method-call restp &rest required-args+rest-arg) 1153 `(invoke-method-call1 (method-call-function ,method-call) 1154 ,(if restp 1155 `(list* ,@required-args+rest-arg) 1156 `(list ,@required-args+rest-arg)) 1157 (method-call-call-method-args ,method-call))) 1158 1159(defstruct (fast-method-call (:copier nil)) 1160 (function #'identity :type function) 1161 pv 1162 next-method-call 1163 arg-info) 1164(defstruct (constant-fast-method-call 1165 (:copier nil) (:include fast-method-call)) 1166 value) 1167 1168#-sb-fluid (declaim (sb-ext:freeze-type fast-method-call)) 1169 1170;; The two variants of INVOKE-FAST-METHOD-CALL differ in how REST-ARGs 1171;; are handled. The first one will get REST-ARG as a single list (as 1172;; the last argument), and will thus need to use APPLY. The second one 1173;; will get them as a &MORE argument, so we can pass the arguments 1174;; directly with MULTIPLE-VALUE-CALL and %MORE-ARG-VALUES. 1175 1176(defmacro invoke-fast-method-call (method-call restp &rest required-args+rest-arg) 1177 `(,(if restp 'apply 'funcall) (fast-method-call-function ,method-call) 1178 (fast-method-call-pv ,method-call) 1179 (fast-method-call-next-method-call ,method-call) 1180 ,@required-args+rest-arg)) 1181 1182(defmacro invoke-fast-method-call/more (method-call 1183 more-context 1184 more-count 1185 &rest required-args) 1186 (macrolet ((generate-call (n) 1187 ``(funcall (fast-method-call-function ,method-call) 1188 (fast-method-call-pv ,method-call) 1189 (fast-method-call-next-method-call ,method-call) 1190 ,@required-args 1191 ,@(loop for x below ,n 1192 collect `(sb-c::%more-arg ,more-context ,x))))) 1193 ;; The cases with only small amounts of required arguments passed 1194 ;; are probably very common, and special-casing speeds them up by 1195 ;; a factor of 2 with very little effect on the other 1196 ;; cases. Though it'd be nice to have the generic case be equally 1197 ;; fast. 1198 `(case ,more-count 1199 (0 ,(generate-call 0)) 1200 (1 ,(generate-call 1)) 1201 (t (multiple-value-call (fast-method-call-function ,method-call) 1202 (values (fast-method-call-pv ,method-call)) 1203 (values (fast-method-call-next-method-call ,method-call)) 1204 ,@required-args 1205 (sb-c::%more-arg-values ,more-context 0 ,more-count)))))) 1206 1207(defstruct (fast-instance-boundp (:copier nil)) 1208 (index 0 :type fixnum)) 1209 1210#-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp)) 1211 1212(eval-when (:compile-toplevel :load-toplevel :execute) 1213 (defvar *allow-emf-call-tracing-p* nil) 1214 (defvar *enable-emf-call-tracing-p* #-sb-show nil #+sb-show t)) 1215 1216;;;; effective method functions 1217 1218(defvar *emf-call-trace-size* 200) 1219(defvar *emf-call-trace* nil) 1220(defvar *emf-call-trace-index* 0) 1221 1222;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8) 1223;;; without explanation. It appears to be intended for debugging, so 1224;;; it might be useful someday, so I haven't deleted it. 1225;;; But it isn't documented and isn't used for anything now, so 1226;;; I've conditionalized it out of the base system. -- WHN 19991213 1227#+sb-show 1228(defun show-emf-call-trace () 1229 (when *emf-call-trace* 1230 (let ((j *emf-call-trace-index*) 1231 (*enable-emf-call-tracing-p* nil)) 1232 (format t "~&(The oldest entries are printed first)~%") 1233 (dotimes-fixnum (i *emf-call-trace-size*) 1234 (let ((ct (aref *emf-call-trace* j))) 1235 (when ct (print ct))) 1236 (incf j) 1237 (when (= j *emf-call-trace-size*) 1238 (setq j 0)))))) 1239 1240(defun trace-emf-call-internal (emf format args) 1241 (unless *emf-call-trace* 1242 (setq *emf-call-trace* (make-array *emf-call-trace-size*))) 1243 (setf (aref *emf-call-trace* *emf-call-trace-index*) 1244 (list* emf format args)) 1245 (incf *emf-call-trace-index*) 1246 (when (= *emf-call-trace-index* *emf-call-trace-size*) 1247 (setq *emf-call-trace-index* 0))) 1248 1249(defmacro trace-emf-call (emf format args) 1250 (when *allow-emf-call-tracing-p* 1251 `(when *enable-emf-call-tracing-p* 1252 (trace-emf-call-internal ,emf ,format ,args)))) 1253 1254(defmacro invoke-effective-method-function-fast 1255 (emf restp &key required-args rest-arg more-arg) 1256 `(progn 1257 (trace-emf-call ,emf ,restp (list ,@required-args rest-arg)) 1258 ,(if more-arg 1259 `(invoke-fast-method-call/more ,emf 1260 ,@more-arg 1261 ,@required-args) 1262 `(invoke-fast-method-call ,emf 1263 ,restp 1264 ,@required-args 1265 ,@rest-arg)))) 1266 1267(defun effective-method-optimized-slot-access-clause 1268 (emf restp required-args) 1269 ;; "What," you may wonder, "do these next two clauses do?" In that 1270 ;; case, you are not a PCL implementor, for they considered this to 1271 ;; be self-documenting.:-| Or CSR, for that matter, since he can 1272 ;; also figure it out by looking at it without breaking stride. For 1273 ;; the rest of us, though: From what the code is doing with .SLOTS. 1274 ;; and whatnot, evidently it's implementing SLOT-VALUEish and 1275 ;; GET-SLOT-VALUEish things. Then we can reason backwards and 1276 ;; conclude that setting EMF to a FIXNUM is an optimized way to 1277 ;; represent these slot access operations. 1278 (when (not restp) 1279 (let ((length (length required-args))) 1280 (cond ((= 1 length) 1281 `((fixnum 1282 (let* ((.slots. (get-slots-or-nil 1283 ,(car required-args))) 1284 (value (when .slots. (clos-slots-ref .slots. ,emf)))) 1285 (if (eq value +slot-unbound+) 1286 (slot-unbound-internal ,(car required-args) 1287 ,emf) 1288 value))))) 1289 ((= 2 length) 1290 `((fixnum 1291 (let ((.new-value. ,(car required-args)) 1292 (.slots. (get-slots-or-nil 1293 ,(cadr required-args)))) 1294 (when .slots. 1295 (setf (clos-slots-ref .slots. ,emf) .new-value.))))))) 1296 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN 1297 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since 1298 ;; there was no explanation and presumably the code is 10+ 1299 ;; years stale, I simply deleted it. -- WHN) 1300 ))) 1301 1302;;; Before SBCL 0.9.16.7 instead of 1303;;; INVOKE-NARROW-EFFECTIVE-METHOD-FUNCTION we passed a (THE (OR 1304;;; FUNCTION METHOD-CALL FAST-METHOD-CALL) EMF) form as the EMF. Now, 1305;;; to make less work for the compiler we take a path that doesn't 1306;;; involve the slot-accessor clause (where EMF is a FIXNUM) at all. 1307(macrolet ((def (name &optional narrow) 1308 `(defmacro ,name (emf restp &key required-args rest-arg more-arg) 1309 (unless (constantp restp) 1310 (error "The RESTP argument is not constant.")) 1311 (setq restp (constant-form-value restp)) 1312 (with-unique-names (emf-n) 1313 `(locally 1314 (declare (optimize (sb-c:insert-step-conditions 0))) 1315 (let ((,emf-n ,emf)) 1316 (trace-emf-call ,emf-n ,restp (list ,@required-args ,@rest-arg)) 1317 (etypecase ,emf-n 1318 (fast-method-call 1319 ,(if more-arg 1320 `(invoke-fast-method-call/more ,emf-n 1321 ,@more-arg 1322 ,@required-args) 1323 `(invoke-fast-method-call ,emf-n 1324 ,restp 1325 ,@required-args 1326 ,@rest-arg))) 1327 ,@,(unless narrow 1328 `(effective-method-optimized-slot-access-clause 1329 emf-n restp required-args)) 1330 (method-call 1331 (invoke-method-call ,emf-n ,restp ,@required-args 1332 ,@rest-arg)) 1333 (function 1334 ,(if restp 1335 `(apply ,emf-n ,@required-args ,@rest-arg) 1336 `(funcall ,emf-n ,@required-args 1337 ,@rest-arg)))))))))) 1338 (def invoke-effective-method-function nil) 1339 (def invoke-narrow-effective-method-function t)) 1340 1341(defun invoke-emf (emf args) 1342 (trace-emf-call emf t args) 1343 (etypecase emf 1344 (fast-method-call 1345 (let* ((arg-info (fast-method-call-arg-info emf)) 1346 (restp (cdr arg-info)) 1347 (nreq (car arg-info))) 1348 (if restp 1349 (apply (fast-method-call-function emf) 1350 (fast-method-call-pv emf) 1351 (fast-method-call-next-method-call emf) 1352 args) 1353 (cond ((null args) 1354 (if (eql nreq 0) 1355 (invoke-fast-method-call emf nil) 1356 (error 'simple-program-error 1357 :format-control "invalid number of arguments: 0" 1358 :format-arguments nil))) 1359 ((null (cdr args)) 1360 (if (eql nreq 1) 1361 (invoke-fast-method-call emf nil (car args)) 1362 (error 'simple-program-error 1363 :format-control "invalid number of arguments: 1" 1364 :format-arguments nil))) 1365 ((null (cddr args)) 1366 (if (eql nreq 2) 1367 (invoke-fast-method-call emf nil (car args) (cadr args)) 1368 (error 'simple-program-error 1369 :format-control "invalid number of arguments: 2" 1370 :format-arguments nil))) 1371 (t 1372 (apply (fast-method-call-function emf) 1373 (fast-method-call-pv emf) 1374 (fast-method-call-next-method-call emf) 1375 args)))))) 1376 (method-call 1377 (apply (method-call-function emf) 1378 args 1379 (method-call-call-method-args emf))) 1380 (fixnum 1381 (cond ((null args) 1382 (error 'simple-program-error 1383 :format-control "invalid number of arguments: 0" 1384 :format-arguments nil)) 1385 ((null (cdr args)) 1386 (let* ((slots (get-slots (car args))) 1387 (value (clos-slots-ref slots emf))) 1388 (if (eq value +slot-unbound+) 1389 (slot-unbound-internal (car args) emf) 1390 value))) 1391 ((null (cddr args)) 1392 (setf (clos-slots-ref (get-slots (cadr args)) emf) 1393 (car args))) 1394 (t (error 'simple-program-error 1395 :format-control "invalid number of arguments" 1396 :format-arguments nil)))) 1397 (fast-instance-boundp 1398 (if (or (null args) (cdr args)) 1399 (error 'simple-program-error 1400 :format-control "invalid number of arguments" 1401 :format-arguments nil) 1402 (let ((slots (get-slots (car args)))) 1403 (not (eq (clos-slots-ref slots (fast-instance-boundp-index emf)) 1404 +slot-unbound+))))) 1405 (function 1406 (apply emf args)))) 1407 1408 1409(defmacro fast-call-next-method-body ((args next-method-call rest-arg) 1410 method-cell 1411 cnm-args) 1412 `(if ,next-method-call 1413 ,(let ((call `(invoke-narrow-effective-method-function 1414 ,next-method-call 1415 ,(not (null rest-arg)) 1416 :required-args ,args 1417 :rest-arg ,(when rest-arg (list rest-arg))))) 1418 `(if ,cnm-args 1419 (bind-args ((,@args 1420 ,@(when rest-arg 1421 `(&rest ,rest-arg))) 1422 ,cnm-args) 1423 ,call) 1424 ,call)) 1425 (call-no-next-method ',method-cell 1426 ,@args 1427 ,@(when rest-arg 1428 `(,rest-arg))))) 1429 1430(defmacro bind-fast-lexical-method-functions 1431 ((args rest-arg next-method-call (&key 1432 call-next-method-p 1433 setq-p 1434 parameters-setqd 1435 method-cell 1436 applyp)) 1437 &body body 1438 &environment env) 1439 (let* ((next-method-p-def 1440 `((next-method-p () 1441 (declare (optimize (sb-c:insert-step-conditions 0))) 1442 (not (null ,next-method-call))))) 1443 (rebindings (when (or setq-p call-next-method-p) 1444 (mapcar (lambda (x) (list x x)) parameters-setqd)))) 1445 (if (not (or call-next-method-p setq-p applyp)) 1446 `(flet ,next-method-p-def 1447 (declare (ignorable #'next-method-p)) 1448 ,@body) 1449 `(flet (,@(when call-next-method-p 1450 `((call-next-method (&rest cnm-args) 1451 (declare (dynamic-extent cnm-args) 1452 (muffle-conditions code-deletion-note) 1453 (optimize (sb-c:insert-step-conditions 0))) 1454 ,@(if (safe-code-p env) 1455 `((%check-cnm-args cnm-args (list ,@args) 1456 ',method-cell)) 1457 nil) 1458 (fast-call-next-method-body (,args 1459 ,next-method-call 1460 ,rest-arg) 1461 ,method-cell 1462 cnm-args)))) 1463 ,@next-method-p-def) 1464 (declare (ignorable #'next-method-p)) 1465 (let ,rebindings 1466 ,@body))))) 1467 1468;;; CMUCL comment (Gerd Moellmann): 1469;;; 1470;;; The standard says it's an error if CALL-NEXT-METHOD is called with 1471;;; arguments, and the set of methods applicable to those arguments is 1472;;; different from the set of methods applicable to the original 1473;;; method arguments. (According to Barry Margolin, this rule was 1474;;; probably added to ensure that before and around methods are always 1475;;; run before primary methods.) 1476;;; 1477;;; This could be optimized for the case that the generic function 1478;;; doesn't have hairy methods, does have standard method combination, 1479;;; is a standard generic function, there are no methods defined on it 1480;;; for COMPUTE-APPLICABLE-METHODS and probably a lot more of such 1481;;; preconditions. That looks hairy and is probably not worth it, 1482;;; because this check will never be fast. 1483(defun %check-cnm-args (cnm-args orig-args method-cell) 1484 ;; 1. Check for no arguments. 1485 (when cnm-args 1486 (let* ((gf (method-generic-function (car method-cell))) 1487 (nreq (generic-function-nreq gf))) 1488 (declare (fixnum nreq)) 1489 ;; 2. Requirement arguments pairwise: if all are EQL, the applicable 1490 ;; methods must be the same. This takes care of the relatively common 1491 ;; case of twiddling with &KEY arguments without being horribly 1492 ;; expensive. 1493 (unless (do ((orig orig-args (cdr orig)) 1494 (args cnm-args (cdr args)) 1495 (n nreq (1- nreq))) 1496 ((zerop n) t) 1497 (unless (and orig args (eql (car orig) (car args))) 1498 (return nil))) 1499 ;; 3. Only then do the full check. 1500 (let ((omethods (compute-applicable-methods gf orig-args)) 1501 (nmethods (compute-applicable-methods gf cnm-args))) 1502 (unless (equal omethods nmethods) 1503 (error "~@<The set of methods ~S applicable to argument~P ~ 1504 ~{~S~^, ~} to call-next-method is different from ~ 1505 the set of methods ~S applicable to the original ~ 1506 method argument~P ~{~S~^, ~}.~@:>" 1507 nmethods (length cnm-args) cnm-args omethods 1508 (length orig-args) orig-args))))))) 1509 1510;; FIXME: replacing this entire mess with DESTRUCTURING-BIND would correct 1511;; problems similar to those already solved by a correct implementation 1512;; of DESTRUCTURING-BIND, such as incorrect binding order: 1513;; e.g. (macroexpand-1 '(bind-args ((&optional (x nil xsp)) args) (form))) 1514;; -> (LET* ((.ARGS-TAIL. ARGS) (XSP (NOT (NULL .ARGS-TAIL.))) (X ...))) 1515;; It's mostly irrelevant unless a method uses CALL-NEXT-METHOD though. 1516(defmacro bind-args ((lambda-list args) &body body) 1517 (let ((args-tail '.args-tail.) 1518 (key '.key.) 1519 (state 'required)) 1520 (flet ((process-var (var) 1521 (if (memq var lambda-list-keywords) 1522 (progn 1523 (case var 1524 (&optional (setq state 'optional)) 1525 (&key (setq state 'key)) 1526 (&allow-other-keys) 1527 (&rest (setq state 'rest)) 1528 (&aux (setq state 'aux)) 1529 (otherwise 1530 (error 1531 "encountered the non-standard lambda list keyword ~S" 1532 var))) 1533 nil) 1534 (case state 1535 (required `((,var (pop ,args-tail)))) 1536 (optional (cond ((not (consp var)) 1537 `((,var (when ,args-tail 1538 (pop ,args-tail))))) 1539 ((null (cddr var)) 1540 `((,(car var) (if ,args-tail 1541 (pop ,args-tail) 1542 ,(cadr var))))) 1543 (t 1544 `((,(caddr var) (not (null ,args-tail))) 1545 (,(car var) (if ,args-tail 1546 (pop ,args-tail) 1547 ,(cadr var))))))) 1548 (rest `((,var ,args-tail))) 1549 (key (cond ((not (consp var)) 1550 `((,var (car 1551 (get-key-arg-tail ,(keywordicate var) 1552 ,args-tail))))) 1553 ((null (cddr var)) 1554 (multiple-value-bind (keyword variable) 1555 (if (consp (car var)) 1556 (values (caar var) 1557 (cadar var)) 1558 (values (keywordicate (car var)) 1559 (car var))) 1560 `((,key (get-key-arg-tail ',keyword 1561 ,args-tail)) 1562 (,variable (if ,key 1563 (car ,key) 1564 ,(cadr var)))))) 1565 (t 1566 (multiple-value-bind (keyword variable) 1567 (if (consp (car var)) 1568 (values (caar var) 1569 (cadar var)) 1570 (values (keywordicate (car var)) 1571 (car var))) 1572 `((,key (get-key-arg-tail ',keyword 1573 ,args-tail)) 1574 (,(caddr var) (not (null,key))) 1575 (,variable (if ,key 1576 (car ,key) 1577 ,(cadr var)))))))) 1578 (aux `(,var)))))) 1579 (let ((bindings (mapcan #'process-var lambda-list))) 1580 `(let* ((,args-tail ,args) 1581 ,@bindings 1582 (.dummy0. 1583 ,@(when (eq state 'optional) 1584 `((unless (null ,args-tail) 1585 (error 'simple-program-error 1586 :format-control "surplus arguments: ~S" 1587 :format-arguments (list ,args-tail))))))) 1588 (declare (ignorable ,args-tail .dummy0.)) 1589 ,@body))))) 1590 1591(defun get-key-arg-tail (keyword list) 1592 (loop for (key . tail) on list by #'cddr 1593 when (null tail) do 1594 ;; FIXME: Do we want to export this symbol? Or maybe use an 1595 ;; (ERROR 'SIMPLE-PROGRAM-ERROR) form? 1596 (sb-c::%odd-key-args-error) 1597 when (eq key keyword) 1598 return tail)) 1599 1600(defun walk-method-lambda (method-lambda required-parameters env slots) 1601 (let (;; flag indicating that CALL-NEXT-METHOD should be in the 1602 ;; method definition 1603 (call-next-method-p nil) 1604 ;; a list of all required parameters whose bindings might be 1605 ;; modified in the method body. 1606 (parameters-setqd nil)) 1607 (flet ((walk-function (form context env) 1608 (unless (and (eq context :eval) (consp form)) 1609 (return-from walk-function form)) 1610 (case (car form) 1611 (call-next-method 1612 ;; hierarchy: nil -> :simple -> T. 1613 (unless (eq call-next-method-p t) 1614 (setq call-next-method-p (if (cdr form) t :simple))) 1615 form) 1616 ((setq multiple-value-setq) 1617 ;; The walker will split (SETQ A 1 B 2) to 1618 ;; separate (SETQ A 1) and (SETQ B 2) forms, so we 1619 ;; only need to handle the simple case of SETQ 1620 ;; here. 1621 (let ((vars (if (eq (car form) 'setq) 1622 (list (second form)) 1623 (second form)))) 1624 (dolist (var vars) 1625 ;; Note that we don't need to check for 1626 ;; %VARIABLE-REBINDING declarations like is 1627 ;; done in CAN-OPTIMIZE-ACCESS1, since the 1628 ;; bindings that will have that declation will 1629 ;; never be SETQd. 1630 (when (var-declaration '%class var env) 1631 ;; If a parameter binding is shadowed by 1632 ;; another binding it won't have a %CLASS 1633 ;; declaration anymore, and this won't get 1634 ;; executed. 1635 (pushnew var parameters-setqd :test #'eq)))) 1636 form) 1637 (function 1638 (when (equal (cdr form) '(call-next-method)) 1639 (setq call-next-method-p t)) 1640 form) 1641 ((slot-value set-slot-value slot-boundp) 1642 (if (constantp (third form) env) 1643 (let ((fun (ecase (car form) 1644 (slot-value #'optimize-slot-value) 1645 (set-slot-value #'optimize-set-slot-value) 1646 (slot-boundp #'optimize-slot-boundp)))) 1647 (funcall fun form slots required-parameters env)) 1648 form)) 1649 (t form)))) 1650 1651 (let ((walked-lambda (walk-form method-lambda env #'walk-function))) 1652 ;;; FIXME: the walker's rewriting of the source code causes 1653 ;;; trouble when doing code coverage. The rewrites should be 1654 ;;; removed, and the same operations done using 1655 ;;; compiler-macros or tranforms. 1656 (values (if (sb-c:policy env (= sb-c:store-coverage-data 0)) 1657 walked-lambda 1658 method-lambda) 1659 call-next-method-p 1660 (not (null parameters-setqd)) 1661 parameters-setqd))))) 1662 1663(defun generic-function-name-p (name) 1664 (and (legal-fun-name-p name) 1665 (fboundp name) 1666 (if (eq **boot-state** 'complete) 1667 (standard-generic-function-p (gdefinition name)) 1668 (funcallable-instance-p (gdefinition name))))) 1669 1670(defun method-plist-value (method key &optional default) 1671 (let ((plist (if (consp method) 1672 (getf (early-method-initargs method) 'plist) 1673 (object-plist method)))) 1674 (getf plist key default))) 1675 1676(defun (setf method-plist-value) (new-value method key &optional default) 1677 (if (consp method) 1678 (setf (getf (getf (early-method-initargs method) 'plist) key default) 1679 new-value) 1680 (setf (getf (object-plist method) key default) new-value))) 1681 1682(defun load-defmethod (class name quals specls ll initargs source-location) 1683 (let ((method-cell (getf initargs 'method-cell))) 1684 (setq initargs (copy-tree initargs)) 1685 (when method-cell 1686 (setf (getf initargs 'method-cell) method-cell)) 1687 #+nil 1688 (setf (getf (getf initargs 'plist) :name) 1689 (make-method-spec name quals specls)) 1690 (load-defmethod-internal class name quals specls 1691 ll initargs source-location))) 1692 1693(defun load-defmethod-internal 1694 (method-class gf-spec qualifiers specializers lambda-list 1695 initargs source-location) 1696 (when (and (eq **boot-state** 'complete) 1697 (fboundp gf-spec)) 1698 (let* ((gf (fdefinition gf-spec)) 1699 (method (and (generic-function-p gf) 1700 (generic-function-methods gf) 1701 (find-method gf qualifiers specializers nil)))) 1702 (when method 1703 (warn 'sb-kernel:redefinition-with-defmethod 1704 :name gf-spec 1705 :new-location source-location 1706 :old-method method 1707 :qualifiers qualifiers :specializers specializers)))) 1708 (let ((method (apply #'add-named-method 1709 gf-spec qualifiers specializers lambda-list 1710 :definition-source source-location 1711 initargs))) 1712 (unless (or (eq method-class 'standard-method) 1713 (eq (find-class method-class nil) (class-of method))) 1714 ;; FIXME: should be STYLE-WARNING? 1715 (format *error-output* 1716 "~&At the time the method with qualifiers ~:S and~%~ 1717 specializers ~:S on the generic function ~S~%~ 1718 was compiled, the method-class for that generic function was~%~ 1719 ~S. But, the method class is now ~S, this~%~ 1720 may mean that this method was compiled improperly.~%" 1721 qualifiers specializers gf-spec 1722 method-class (class-name (class-of method)))) 1723 method)) 1724 1725(defun make-method-spec (gf qualifiers specializers) 1726 (let ((name (generic-function-name gf)) 1727 (unparsed-specializers (unparse-specializers gf specializers))) 1728 `(slow-method ,name ,@qualifiers ,unparsed-specializers))) 1729 1730(defun initialize-method-function (initargs method) 1731 (let* ((mf (getf initargs :function)) 1732 (mff (and (typep mf '%method-function) 1733 (%method-function-fast-function mf))) 1734 (plist (getf initargs 'plist)) 1735 (name (getf plist :name)) 1736 (method-cell (getf initargs 'method-cell))) 1737 (when method-cell 1738 (setf (car method-cell) method)) 1739 (when name 1740 (when mf 1741 (setq mf (set-fun-name mf name))) 1742 (when (and mff (consp name) (eq (car name) 'slow-method)) 1743 (let ((fast-name `(fast-method ,@(cdr name)))) 1744 (set-fun-name mff fast-name)))) 1745 (when plist 1746 (let ((plist plist)) 1747 (let ((snl (getf plist :slot-name-lists))) 1748 (when snl 1749 (setf (method-plist-value method :pv-table) 1750 (intern-pv-table :slot-name-lists snl)))))))) 1751 1752(defun analyze-lambda-list (lambda-list) 1753 (multiple-value-bind (llks required optional rest keywords) 1754 ;; We say "&MUMBLE is not allowed in a generic function lambda list" 1755 ;; whether this is called by DEFMETHOD or DEFGENERIC. 1756 ;; [It is used for either. Why else recognize and silently ignore &AUX?] 1757 (parse-lambda-list lambda-list 1758 :accept (lambda-list-keyword-mask 1759 '(&optional &rest &key &allow-other-keys &aux)) 1760 :silent t 1761 :context "a generic function lambda list") 1762 (declare (ignore rest)) 1763 (values llks (length required) (length optional) 1764 (mapcar #'parse-key-arg-spec keywords) keywords))) 1765 1766;; FIXME: this does more than return an FTYPE from a lambda list - 1767;; it unions the type with an existing ctype object. It needs a better name, 1768;; and to be reimplemented as "union and call sb-c::ftype-from-lambda-list". 1769(defun ftype-declaration-from-lambda-list (lambda-list name) 1770 (multiple-value-bind (llks nrequired noptional keywords keyword-parameters) 1771 (analyze-lambda-list lambda-list) 1772 (declare (ignore keyword-parameters)) 1773 (let* ((old (proclaimed-ftype name)) ;FIXME:FDOCUMENTATION instead? 1774 (old-ftype (if (fun-type-p old) old nil)) 1775 (old-restp (and old-ftype (fun-type-rest old-ftype))) 1776 (old-keys (and old-ftype 1777 (mapcar #'key-info-name 1778 (fun-type-keywords 1779 old-ftype)))) 1780 (old-keysp (and old-ftype (fun-type-keyp old-ftype))) 1781 (old-allowp (and old-ftype 1782 (fun-type-allowp old-ftype))) 1783 (keywords (union old-keys (mapcar #'parse-key-arg-spec keywords)))) 1784 `(function ,(append (make-list nrequired :initial-element t) 1785 (when (plusp noptional) 1786 (append '(&optional) 1787 (make-list noptional :initial-element t))) 1788 (when (or (ll-kwds-restp llks) old-restp) 1789 '(&rest t)) 1790 (when (or (ll-kwds-keyp llks) old-keysp) 1791 (append '(&key) 1792 (mapcar (lambda (key) 1793 `(,key t)) 1794 keywords) 1795 (when (or (ll-kwds-allowp llks) old-allowp) 1796 '(&allow-other-keys))))) 1797 *)))) 1798 1799;;;; early generic function support 1800 1801(defvar *!early-generic-functions* ()) 1802 1803;; CLHS doesn't specify &allow-other-keys here but I guess the supposition 1804;; is that they'll be checked by ENSURE-GENERIC-FUNCTION-USING-CLASS. 1805;; Except we don't do that either, so I think the blame, if any, lies there 1806;; for not catching errant keywords. 1807(defun ensure-generic-function (fun-name &rest all-keys) 1808 (let ((existing (and (fboundp fun-name) 1809 (gdefinition fun-name)))) 1810 (cond ((and existing 1811 (eq **boot-state** 'complete) 1812 (null (generic-function-p existing))) 1813 (generic-clobbers-function fun-name) 1814 (fmakunbound fun-name) 1815 (apply #'ensure-generic-function fun-name all-keys)) 1816 (t 1817 (apply #'ensure-generic-function-using-class 1818 existing fun-name all-keys))))) 1819 1820(defun generic-clobbers-function (fun-name) 1821 (cerror "Replace the function binding" 1822 'simple-program-error 1823 :format-control "~@<~/sb-impl:print-symbol-with-prefix/ ~ 1824 already names an ordinary function or a ~ 1825 macro.~@:>" 1826 :format-arguments (list fun-name))) 1827 1828(defvar *sgf-wrapper* 1829 (!boot-make-wrapper (early-class-size 'standard-generic-function) 1830 'standard-generic-function)) 1831 1832(defvar *sgf-slots-init* 1833 (mapcar (lambda (canonical-slot) 1834 (if (memq (getf canonical-slot :name) '(arg-info source)) 1835 +slot-unbound+ 1836 (let ((initfunction (getf canonical-slot :initfunction))) 1837 (if initfunction 1838 (funcall initfunction) 1839 +slot-unbound+)))) 1840 (early-collect-inheritance 'standard-generic-function))) 1841 1842(defconstant +sgf-method-class-index+ 1843 (!bootstrap-slot-index 'standard-generic-function 'method-class)) 1844 1845(defun early-gf-p (x) 1846 (and (fsc-instance-p x) 1847 (eq (clos-slots-ref (get-slots x) +sgf-method-class-index+) 1848 +slot-unbound+))) 1849 1850(defconstant +sgf-methods-index+ 1851 (!bootstrap-slot-index 'standard-generic-function 'methods)) 1852 1853(defmacro early-gf-methods (gf) 1854 `(clos-slots-ref (get-slots ,gf) +sgf-methods-index+)) 1855 1856(defun safe-generic-function-methods (generic-function) 1857 (if (eq (class-of generic-function) *the-class-standard-generic-function*) 1858 (clos-slots-ref (get-slots generic-function) +sgf-methods-index+) 1859 (generic-function-methods generic-function))) 1860 1861(defconstant +sgf-arg-info-index+ 1862 (!bootstrap-slot-index 'standard-generic-function 'arg-info)) 1863 1864(defmacro early-gf-arg-info (gf) 1865 `(clos-slots-ref (get-slots ,gf) +sgf-arg-info-index+)) 1866 1867(defconstant +sgf-dfun-state-index+ 1868 (!bootstrap-slot-index 'standard-generic-function 'dfun-state)) 1869 1870(defstruct (arg-info 1871 (:conc-name nil) 1872 (:constructor make-arg-info ()) 1873 (:copier nil)) 1874 (arg-info-lambda-list :no-lambda-list) 1875 arg-info-precedence 1876 arg-info-metatypes 1877 arg-info-number-optional 1878 arg-info-key/rest-p 1879 arg-info-keys ;nil no &KEY or &REST allowed 1880 ;(k1 k2 ..) Each method must accept these &KEY arguments. 1881 ;T must have &KEY or &REST 1882 1883 gf-info-simple-accessor-type ; nil, reader, writer, boundp 1884 (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info 1885 1886 gf-info-static-c-a-m-emf 1887 (gf-info-c-a-m-emf-std-p t) 1888 gf-info-fast-mf-p) 1889 1890#-sb-fluid (declaim (sb-ext:freeze-type arg-info)) 1891 1892(defun arg-info-valid-p (arg-info) 1893 (not (null (arg-info-number-optional arg-info)))) 1894 1895(defun arg-info-applyp (arg-info) 1896 (or (plusp (arg-info-number-optional arg-info)) 1897 (arg-info-key/rest-p arg-info))) 1898 1899(defun arg-info-number-required (arg-info) 1900 (length (arg-info-metatypes arg-info))) 1901 1902(defun arg-info-nkeys (arg-info) 1903 (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info))) 1904 1905(defun create-gf-lambda-list (lambda-list) 1906 ;;; Create a gf lambda list from a method lambda list 1907 (loop for x in lambda-list 1908 collect (if (consp x) (list (car x)) x) 1909 if (eq x '&key) do (loop-finish))) 1910 1911(defun ll-keyp-or-restp (bits) 1912 (logtest (lambda-list-keyword-mask '(&key &rest)) bits)) 1913 1914(defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p) 1915 argument-precedence-order) 1916 (let* ((arg-info (if (eq **boot-state** 'complete) 1917 (gf-arg-info gf) 1918 (early-gf-arg-info gf))) 1919 (methods (if (eq **boot-state** 'complete) 1920 (generic-function-methods gf) 1921 (early-gf-methods gf))) 1922 (was-valid-p (integerp (arg-info-number-optional arg-info))) 1923 (first-p (and new-method (null (cdr methods))))) 1924 (when (and (not lambda-list-p) methods) 1925 (setq lambda-list (gf-lambda-list gf))) 1926 (when (or lambda-list-p 1927 (and first-p 1928 (eq (arg-info-lambda-list arg-info) :no-lambda-list))) 1929 (multiple-value-bind (llks nreq nopt keywords) 1930 (analyze-lambda-list lambda-list) 1931 (when (and methods (not first-p)) 1932 (let ((gf-nreq (arg-info-number-required arg-info)) 1933 (gf-nopt (arg-info-number-optional arg-info)) 1934 (gf-key/rest-p (arg-info-key/rest-p arg-info))) 1935 (unless (and (= nreq gf-nreq) 1936 (= nopt gf-nopt) 1937 (eq (ll-keyp-or-restp llks) gf-key/rest-p)) 1938 (error "The lambda-list ~S is incompatible with ~ 1939 existing methods of ~S." 1940 lambda-list gf)))) 1941 (setf (arg-info-lambda-list arg-info) 1942 (if lambda-list-p 1943 lambda-list 1944 (create-gf-lambda-list lambda-list))) 1945 (when (or lambda-list-p argument-precedence-order 1946 (null (arg-info-precedence arg-info))) 1947 (setf (arg-info-precedence arg-info) 1948 (compute-precedence lambda-list nreq argument-precedence-order))) 1949 (setf (arg-info-metatypes arg-info) (make-list nreq)) 1950 (setf (arg-info-number-optional arg-info) nopt) 1951 (setf (arg-info-key/rest-p arg-info) (ll-keyp-or-restp llks)) 1952 (setf (arg-info-keys arg-info) 1953 (if lambda-list-p 1954 (if (ll-kwds-allowp llks) t keywords) 1955 (arg-info-key/rest-p arg-info))))) 1956 (when new-method 1957 (check-method-arg-info gf arg-info new-method)) 1958 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p) 1959 arg-info)) 1960 1961(defun check-method-arg-info (gf arg-info method) 1962 (multiple-value-bind (llks nreq nopt keywords) 1963 (analyze-lambda-list (if (consp method) 1964 (early-method-lambda-list method) 1965 (method-lambda-list method))) 1966 (flet ((lose (string &rest args) 1967 (error 'simple-program-error 1968 :format-control "~@<attempt to add the method~2I~_~S~I~_~ 1969 to the generic function~2I~_~S;~I~_~ 1970 but ~?~:>" 1971 :format-arguments (list method gf string args))) 1972 (comparison-description (x y) 1973 (if (> x y) "more" "fewer"))) 1974 (let ((gf-nreq (arg-info-number-required arg-info)) 1975 (gf-nopt (arg-info-number-optional arg-info)) 1976 (gf-key/rest-p (arg-info-key/rest-p arg-info)) 1977 (gf-keywords (arg-info-keys arg-info))) 1978 (unless (= nreq gf-nreq) 1979 (lose 1980 "the method has ~A required arguments than the generic function." 1981 (comparison-description nreq gf-nreq))) 1982 (unless (= nopt gf-nopt) 1983 (lose 1984 "the method has ~A optional arguments than the generic function." 1985 (comparison-description nopt gf-nopt))) 1986 (unless (eq (ll-keyp-or-restp llks) gf-key/rest-p) 1987 (lose 1988 "the method and generic function differ in whether they accept~_~ 1989 &REST or &KEY arguments.")) 1990 (when (consp gf-keywords) 1991 (unless (or (and (ll-kwds-restp llks) (not (ll-kwds-keyp llks))) 1992 (ll-kwds-allowp llks) 1993 (every (lambda (k) (memq k keywords)) gf-keywords)) 1994 (lose "the method does not accept each of the &KEY arguments~2I~_~ 1995 ~S." 1996 gf-keywords))))))) 1997 1998(defconstant +sm-specializers-index+ 1999 (!bootstrap-slot-index 'standard-method 'specializers)) 2000(defconstant +sm-%function-index+ 2001 (!bootstrap-slot-index 'standard-method '%function)) 2002(defconstant +sm-qualifiers-index+ 2003 (!bootstrap-slot-index 'standard-method 'qualifiers)) 2004 2005;;; FIXME: we don't actually need this; we could test for the exact 2006;;; class and deal with it as appropriate. In fact we probably don't 2007;;; need it anyway because we only use this for METHOD-SPECIALIZERS on 2008;;; the standard reader method for METHOD-SPECIALIZERS. Probably. 2009(dolist (s '(specializers %function)) 2010 (aver (= (symbol-value (intern (format nil "+SM-~A-INDEX+" s))) 2011 (!bootstrap-slot-index 'standard-reader-method s) 2012 (!bootstrap-slot-index 'standard-writer-method s) 2013 (!bootstrap-slot-index 'standard-boundp-method s) 2014 (!bootstrap-slot-index 'global-reader-method s) 2015 (!bootstrap-slot-index 'global-writer-method s) 2016 (!bootstrap-slot-index 'global-boundp-method s)))) 2017 2018(defvar *standard-method-class-names* 2019 '(standard-method standard-reader-method 2020 standard-writer-method standard-boundp-method 2021 global-reader-method global-writer-method 2022 global-boundp-method)) 2023 2024(declaim (list **standard-method-classes**)) 2025(defglobal **standard-method-classes** nil) 2026 2027(defun safe-method-specializers (method) 2028 (if (member (class-of method) **standard-method-classes** :test #'eq) 2029 (clos-slots-ref (std-instance-slots method) +sm-specializers-index+) 2030 (method-specializers method))) 2031(defun safe-method-fast-function (method) 2032 (let ((mf (safe-method-function method))) 2033 (and (typep mf '%method-function) 2034 (%method-function-fast-function mf)))) 2035(defun safe-method-function (method) 2036 (if (member (class-of method) **standard-method-classes** :test #'eq) 2037 (clos-slots-ref (std-instance-slots method) +sm-%function-index+) 2038 (method-function method))) 2039(defun safe-method-qualifiers (method) 2040 (if (member (class-of method) **standard-method-classes** :test #'eq) 2041 (clos-slots-ref (std-instance-slots method) +sm-qualifiers-index+) 2042 (method-qualifiers method))) 2043 2044(defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p) 2045 (let* ((existing-p (and methods (cdr methods) new-method)) 2046 (nreq (length (arg-info-metatypes arg-info))) 2047 (metatypes (if existing-p 2048 (arg-info-metatypes arg-info) 2049 (make-list nreq))) 2050 (type (if existing-p 2051 (gf-info-simple-accessor-type arg-info) 2052 nil))) 2053 (when (arg-info-valid-p arg-info) 2054 (dolist (method (if new-method (list new-method) methods)) 2055 (let* ((specializers (if (or (eq **boot-state** 'complete) 2056 (not (consp method))) 2057 (safe-method-specializers method) 2058 (early-method-specializers method t))) 2059 (class (if (or (eq **boot-state** 'complete) (not (consp method))) 2060 (class-of method) 2061 (early-method-class method))) 2062 (new-type 2063 (when (and class 2064 (or (not (eq **boot-state** 'complete)) 2065 (eq (generic-function-method-combination gf) 2066 *standard-method-combination*))) 2067 (cond ((or (eq class *the-class-standard-reader-method*) 2068 (eq class *the-class-global-reader-method*)) 2069 'reader) 2070 ((or (eq class *the-class-standard-writer-method*) 2071 (eq class *the-class-global-writer-method*)) 2072 'writer) 2073 ((or (eq class *the-class-standard-boundp-method*) 2074 (eq class *the-class-global-boundp-method*)) 2075 'boundp))))) 2076 (setq metatypes (mapcar #'raise-metatype metatypes specializers)) 2077 (setq type (cond ((null type) new-type) 2078 ((eq type new-type) type) 2079 (t nil))))) 2080 (setf (arg-info-metatypes arg-info) metatypes) 2081 (setf (gf-info-simple-accessor-type arg-info) type))) 2082 (when (or (not was-valid-p) first-p) 2083 (multiple-value-bind (c-a-m-emf std-p) 2084 (if (early-gf-p gf) 2085 (values t t) 2086 (compute-applicable-methods-emf gf)) 2087 (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf) 2088 (setf (gf-info-c-a-m-emf-std-p arg-info) std-p) 2089 (unless (gf-info-c-a-m-emf-std-p arg-info) 2090 (setf (gf-info-simple-accessor-type arg-info) t)))) 2091 (unless was-valid-p 2092 (let ((name (if (eq **boot-state** 'complete) 2093 (generic-function-name gf) 2094 (!early-gf-name gf)))) 2095 (setf (gf-precompute-dfun-and-emf-p arg-info) 2096 (cond 2097 ((and (consp name) 2098 (member (car name) 2099 *internal-pcl-generalized-fun-name-symbols*)) 2100 nil) 2101 (t (let* ((symbol (fun-name-block-name name)) 2102 (package (symbol-package symbol))) 2103 (and (or (eq package *pcl-package*) 2104 (memq package (package-use-list *pcl-package*))) 2105 (not (eq package *cl-package*)) 2106 ;; FIXME: this test will eventually be 2107 ;; superseded by the *internal-pcl...* test, 2108 ;; above. While we are in a process of 2109 ;; transition, however, it should probably 2110 ;; remain. 2111 (not (find #\Space (symbol-name symbol)))))))))) 2112 (setf (gf-info-fast-mf-p arg-info) 2113 (or (not (eq **boot-state** 'complete)) 2114 (let* ((method-class (generic-function-method-class gf)) 2115 (methods (compute-applicable-methods 2116 #'make-method-lambda 2117 (list gf (class-prototype method-class) 2118 '(lambda) nil)))) 2119 (and methods (null (cdr methods)) 2120 (let ((specls (method-specializers (car methods)))) 2121 (and (classp (car specls)) 2122 (eq 'standard-generic-function 2123 (class-name (car specls))) 2124 (classp (cadr specls)) 2125 (eq 'standard-method 2126 (class-name (cadr specls))))))))) 2127 arg-info) 2128 2129;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS. 2130;;; 2131;;; The STATIC-SLOTS field of the funcallable instances used as early 2132;;; generic functions is used to store the early methods and early 2133;;; discriminator code for the early generic function. The static 2134;;; slots field of the fins contains a list whose: 2135;;; CAR - a list of the early methods on this early gf 2136;;; CADR - the early discriminator code for this method 2137(defun ensure-generic-function-using-class (existing spec &rest keys 2138 &key (lambda-list nil 2139 lambda-list-p) 2140 argument-precedence-order 2141 definition-source 2142 documentation 2143 &allow-other-keys) 2144 (declare (ignore keys)) 2145 (cond ((and existing (early-gf-p existing)) 2146 (when lambda-list-p 2147 (set-arg-info existing :lambda-list lambda-list)) 2148 existing) 2149 ((assoc spec *!generic-function-fixups* :test #'equal) 2150 (if existing 2151 (make-early-gf spec lambda-list lambda-list-p existing 2152 argument-precedence-order definition-source 2153 documentation) 2154 (bug "The function ~S is not already defined." spec))) 2155 (existing 2156 (bug "~S should be on the list ~S." 2157 spec '*!generic-function-fixups*)) 2158 (t 2159 (pushnew spec *!early-generic-functions* :test #'equal) 2160 (make-early-gf spec lambda-list lambda-list-p nil 2161 argument-precedence-order definition-source 2162 documentation)))) 2163 2164(defun make-early-gf (spec &optional lambda-list lambda-list-p 2165 function argument-precedence-order source-location 2166 documentation) 2167 (let ((fin (allocate-standard-funcallable-instance 2168 *sgf-wrapper* *sgf-slots-init*))) 2169 (set-funcallable-instance-function 2170 fin 2171 (or function 2172 (if (eq spec 'print-object) 2173 #'(lambda (instance stream) 2174 (print-unreadable-object (instance stream :identity t) 2175 (format stream "std-instance"))) 2176 #'(lambda (&rest args) 2177 (declare (ignore args)) 2178 (error "The function of the funcallable-instance ~S~ 2179 has not been set." fin))))) 2180 (setf (gdefinition spec) fin) 2181 (!bootstrap-set-slot 'standard-generic-function fin 'name spec) 2182 (!bootstrap-set-slot 'standard-generic-function fin 2183 'source source-location) 2184 (!bootstrap-set-slot 'standard-generic-function fin 2185 '%documentation documentation) 2186 (let ((arg-info (make-arg-info))) 2187 (setf (early-gf-arg-info fin) arg-info) 2188 (when lambda-list-p 2189 (setf (info :function :type spec) 2190 (specifier-type 2191 (ftype-declaration-from-lambda-list lambda-list spec)) 2192 (info :function :where-from spec) :defined-method) 2193 (if argument-precedence-order 2194 (set-arg-info fin 2195 :lambda-list lambda-list 2196 :argument-precedence-order argument-precedence-order) 2197 (set-arg-info fin :lambda-list lambda-list)))) 2198 fin)) 2199 2200(defun safe-gf-dfun-state (generic-function) 2201 (if (eq (class-of generic-function) *the-class-standard-generic-function*) 2202 (clos-slots-ref (fsc-instance-slots generic-function) +sgf-dfun-state-index+) 2203 (gf-dfun-state generic-function))) 2204(defun (setf safe-gf-dfun-state) (new-value generic-function) 2205 (if (eq (class-of generic-function) *the-class-standard-generic-function*) 2206 (setf (clos-slots-ref (fsc-instance-slots generic-function) 2207 +sgf-dfun-state-index+) 2208 new-value) 2209 (setf (gf-dfun-state generic-function) new-value))) 2210 2211(defun set-dfun (gf &optional dfun cache info) 2212 (let ((new-state (if (and dfun (or cache info)) 2213 (list* dfun cache info) 2214 dfun))) 2215 (cond 2216 ((eq **boot-state** 'complete) 2217 ;; Check that we are under the lock. 2218 #+sb-thread 2219 (aver (eq sb-thread:*current-thread* (sb-thread:mutex-owner (gf-lock gf)))) 2220 (setf (safe-gf-dfun-state gf) new-state)) 2221 (t 2222 (setf (clos-slots-ref (get-slots gf) +sgf-dfun-state-index+) 2223 new-state)))) 2224 dfun) 2225 2226(defun gf-dfun-cache (gf) 2227 (let ((state (if (eq **boot-state** 'complete) 2228 (safe-gf-dfun-state gf) 2229 (clos-slots-ref (get-slots gf) +sgf-dfun-state-index+)))) 2230 (typecase state 2231 (function nil) 2232 (cons (cadr state))))) 2233 2234(defun gf-dfun-info (gf) 2235 (let ((state (if (eq **boot-state** 'complete) 2236 (safe-gf-dfun-state gf) 2237 (clos-slots-ref (get-slots gf) +sgf-dfun-state-index+)))) 2238 (typecase state 2239 (function nil) 2240 (cons (cddr state))))) 2241 2242(defconstant +sgf-name-index+ 2243 (!bootstrap-slot-index 'standard-generic-function 'name)) 2244 2245(defun !early-gf-name (gf) 2246 (clos-slots-ref (get-slots gf) +sgf-name-index+)) 2247 2248(defun gf-lambda-list (gf) 2249 (let ((arg-info (if (eq **boot-state** 'complete) 2250 (gf-arg-info gf) 2251 (early-gf-arg-info gf)))) 2252 (if (eq :no-lambda-list (arg-info-lambda-list arg-info)) 2253 (let ((methods (if (eq **boot-state** 'complete) 2254 (generic-function-methods gf) 2255 (early-gf-methods gf)))) 2256 (if (null methods) 2257 (progn 2258 (warn "no way to determine the lambda list for ~S" gf) 2259 nil) 2260 (let* ((method (car (last methods))) 2261 (ll (if (consp method) 2262 (early-method-lambda-list method) 2263 (method-lambda-list method)))) 2264 (create-gf-lambda-list ll)))) 2265 (arg-info-lambda-list arg-info)))) 2266 2267(defmacro real-ensure-gf-internal (gf-class all-keys env) 2268 `(progn 2269 (cond ((symbolp ,gf-class) 2270 (setq ,gf-class (find-class ,gf-class t ,env))) 2271 ((classp ,gf-class)) 2272 (t 2273 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~ 2274 class nor a symbol that names a class." 2275 ,gf-class))) 2276 (unless (class-finalized-p ,gf-class) 2277 (if (class-has-a-forward-referenced-superclass-p ,gf-class) 2278 ;; FIXME: reference MOP documentation -- this is an 2279 ;; additional requirement on our users 2280 (error "The generic function class ~S is not finalizeable" ,gf-class) 2281 (finalize-inheritance ,gf-class))) 2282 (remf ,all-keys :generic-function-class) 2283 (remf ,all-keys :environment) 2284 (let ((combin (getf ,all-keys :method-combination))) 2285 (etypecase combin 2286 (cons 2287 (setf (getf ,all-keys :method-combination) 2288 (find-method-combination (class-prototype ,gf-class) 2289 (car combin) 2290 (cdr combin)))) 2291 ((or null method-combination)))) 2292 (let ((method-class (getf ,all-keys :method-class '.shes-not-there.))) 2293 (unless (eq method-class '.shes-not-there.) 2294 (setf (getf ,all-keys :method-class) 2295 (cond ((classp method-class) 2296 method-class) 2297 (t (find-class method-class t ,env)))))))) 2298 2299(defun note-gf-signature (fun-name lambda-list-p lambda-list) 2300 (unless lambda-list-p 2301 ;; Use the existing lambda-list, if any. It is reasonable to do eg. 2302 ;; 2303 ;; (if (fboundp name) 2304 ;; (ensure-generic-function name) 2305 ;; (ensure-generic-function name :lambda-list '(foo))) 2306 ;; 2307 ;; in which case we end up here with no lambda-list in the first leg. 2308 (setf (values lambda-list lambda-list-p) 2309 (handler-case 2310 (values (generic-function-lambda-list (fdefinition fun-name)) 2311 t) 2312 ((or warning error) () 2313 (values nil nil))))) 2314 (let ((gf-type 2315 (specifier-type 2316 (if lambda-list-p 2317 (ftype-declaration-from-lambda-list lambda-list fun-name) 2318 'function))) 2319 (old-type nil)) 2320 ;; FIXME: Ideally we would like to not clobber it, but because generic 2321 ;; functions assert their FTYPEs callers believing the FTYPE are left with 2322 ;; unsafe assumptions. Hence the clobbering. Be quiet when the new type 2323 ;; is a subtype of the old one, though -- even though the type is not 2324 ;; trusted anymore, the warning is still not quite as interesting. 2325 (when (and (eq :declared (info :function :where-from fun-name)) 2326 (not (csubtypep gf-type (setf old-type (proclaimed-ftype fun-name))))) 2327 (style-warn "~@<Generic function ~S clobbers an earlier ~S proclamation ~S ~ 2328 for the same name with ~S.~:@>" 2329 fun-name 'ftype 2330 (type-specifier old-type) 2331 (type-specifier gf-type))) 2332 (setf (info :function :type fun-name) gf-type 2333 (info :function :where-from fun-name) :defined-method) 2334 fun-name)) 2335 2336(defun real-ensure-gf-using-class--generic-function 2337 (existing 2338 fun-name 2339 &rest all-keys 2340 &key environment (lambda-list nil lambda-list-p) 2341 (generic-function-class 'standard-generic-function) 2342 &allow-other-keys) 2343 (real-ensure-gf-internal generic-function-class all-keys environment) 2344 ;; KLUDGE: the above macro does SETQ on GENERIC-FUNCTION-CLASS, 2345 ;; which is what makes the next line work 2346 (unless (eq (class-of existing) generic-function-class) 2347 (change-class existing generic-function-class)) 2348 (prog1 2349 (apply #'reinitialize-instance existing all-keys) 2350 (note-gf-signature fun-name lambda-list-p lambda-list))) 2351 2352(defun real-ensure-gf-using-class--null 2353 (existing 2354 fun-name 2355 &rest all-keys 2356 &key environment (lambda-list nil lambda-list-p) 2357 (generic-function-class 'standard-generic-function) 2358 &allow-other-keys) 2359 (declare (ignore existing)) 2360 (real-ensure-gf-internal generic-function-class all-keys environment) 2361 (prog1 2362 (setf (gdefinition fun-name) 2363 (apply #'make-instance generic-function-class 2364 :name fun-name all-keys)) 2365 (note-gf-signature fun-name lambda-list-p lambda-list))) 2366 2367(defun safe-gf-arg-info (generic-function) 2368 (if (eq (class-of generic-function) *the-class-standard-generic-function*) 2369 (clos-slots-ref (fsc-instance-slots generic-function) 2370 +sgf-arg-info-index+) 2371 (gf-arg-info generic-function))) 2372 2373;;; FIXME: this function took on a slightly greater role than it 2374;;; previously had around 2005-11-02, when CSR fixed the bug whereby 2375;;; having more than one subclass of standard-generic-function caused 2376;;; the whole system to die horribly through a metacircle in 2377;;; GF-ARG-INFO. The fix is to be slightly more disciplined about 2378;;; calling accessor methods -- we call GET-GENERIC-FUN-INFO when 2379;;; computing discriminating functions, so we need to be careful about 2380;;; having a base case for the recursion, and we provide that with the 2381;;; STANDARD-GENERIC-FUNCTION case below. However, we are not (yet) 2382;;; as disciplined as CLISP's CLOS/MOP, and it would be nice to get to 2383;;; that stage, where all potentially dangerous cases are enumerated 2384;;; and stopped. -- CSR, 2005-11-02. 2385(defun get-generic-fun-info (gf) 2386 ;; values nreq applyp metatypes nkeys arg-info 2387 (multiple-value-bind (applyp metatypes arg-info) 2388 (let* ((arg-info (if (early-gf-p gf) 2389 (early-gf-arg-info gf) 2390 (safe-gf-arg-info gf))) 2391 (metatypes (arg-info-metatypes arg-info))) 2392 (values (arg-info-applyp arg-info) 2393 metatypes 2394 arg-info)) 2395 (let ((nreq 0) 2396 (nkeys 0)) 2397 (declare (fixnum nreq nkeys)) 2398 (dolist (x metatypes) 2399 (incf nreq) 2400 (unless (eq x t) 2401 (incf nkeys))) 2402 (values nreq applyp metatypes 2403 nkeys 2404 arg-info)))) 2405 2406(defun generic-function-nreq (gf) 2407 (let* ((arg-info (if (early-gf-p gf) 2408 (early-gf-arg-info gf) 2409 (safe-gf-arg-info gf))) 2410 (metatypes (arg-info-metatypes arg-info))) 2411 (declare (list metatypes)) 2412 (length metatypes))) 2413 2414(defun !early-make-a-method (class qualifiers arglist specializers initargs doc 2415 &key slot-name object-class method-class-function 2416 definition-source) 2417 (aver (notany #'sb-pcl::eql-specializer-p specializers)) 2418 (binding* 2419 ;; Figure out whether we got class objects or class names as the 2420 ;; specializers and set parsed and unparsed appropriately. If we 2421 ;; got class objects, then we can compute unparsed, but if we 2422 ;; got class names we don't try to compute parsed. 2423 (((parsed unparsed) 2424 (if (every #'classp specializers) 2425 (values specializers 2426 (mapcar (lambda (s) 2427 (if (eq s t) t (class-name s))) 2428 specializers)) 2429 (values () specializers))) 2430 (result 2431 (list :early-method 2432 2433 (getf initargs :function) 2434 (let ((mf (getf initargs :function))) 2435 (aver mf) 2436 (and (typep mf '%method-function) 2437 (%method-function-fast-function mf))) 2438 2439 ;; the parsed specializers. This is used by 2440 ;; EARLY-METHOD-SPECIALIZERS to cache the parse. 2441 ;; Note that this only comes into play when there is 2442 ;; more than one early method on an early gf. 2443 parsed 2444 2445 ;; A list to which REAL-MAKE-A-METHOD can be applied 2446 ;; to make a real method corresponding to this early 2447 ;; one. 2448 (append 2449 (list class qualifiers arglist unparsed 2450 initargs doc) 2451 (when slot-name 2452 (list :slot-name slot-name :object-class object-class 2453 :method-class-function method-class-function)) 2454 (list :definition-source definition-source))))) 2455 (initialize-method-function initargs result) 2456 result)) 2457 2458(defun real-make-a-method 2459 (class qualifiers lambda-list specializers initargs doc 2460 &rest args &key slot-name object-class method-class-function 2461 definition-source) 2462 (if method-class-function 2463 (let* ((object-class (if (classp object-class) object-class 2464 (find-class object-class))) 2465 (slots (class-direct-slots object-class)) 2466 (slot-definition (find slot-name slots 2467 :key #'slot-definition-name))) 2468 (aver slot-name) 2469 (aver slot-definition) 2470 (let ((initargs (list* :qualifiers qualifiers :lambda-list lambda-list 2471 :specializers specializers :documentation doc 2472 :slot-definition slot-definition 2473 :slot-name slot-name initargs))) 2474 (apply #'make-instance 2475 (apply method-class-function object-class slot-definition 2476 initargs) 2477 :definition-source definition-source 2478 initargs))) 2479 (apply #'make-instance class :qualifiers qualifiers 2480 :lambda-list lambda-list :specializers specializers 2481 :documentation doc (append args initargs)))) 2482 2483(defun early-method-function (early-method) 2484 (values (cadr early-method) (caddr early-method))) 2485 2486(defun early-method-class (early-method) 2487 (find-class (car (fifth early-method)))) 2488 2489(defun early-method-standard-accessor-p (early-method) 2490 (let ((class (first (fifth early-method)))) 2491 (or (eq class 'standard-reader-method) 2492 (eq class 'standard-writer-method) 2493 (eq class 'standard-boundp-method)))) 2494 2495(defun early-method-standard-accessor-slot-name (early-method) 2496 (eighth (fifth early-method))) 2497 2498;;; Fetch the specializers of an early method. This is basically just 2499;;; a simple accessor except that when the second argument is t, this 2500;;; converts the specializers from symbols into class objects. The 2501;;; class objects are cached in the early method, this makes 2502;;; bootstrapping faster because the class objects only have to be 2503;;; computed once. 2504;;; 2505;;; NOTE: 2506;;; The second argument should only be passed as T by 2507;;; early-lookup-method. This is to implement the rule that only when 2508;;; there is more than one early method on a generic function is the 2509;;; conversion from class names to class objects done. This 2510;;; corresponds to the fact that we are only allowed to have one 2511;;; method on any generic function up until the time classes exist. 2512(defun early-method-specializers (early-method &optional objectsp) 2513 (if (and (listp early-method) 2514 (eq (car early-method) :early-method)) 2515 (cond ((eq objectsp t) 2516 (or (fourth early-method) 2517 (setf (fourth early-method) 2518 (mapcar #'find-class (cadddr (fifth early-method)))))) 2519 (t 2520 (fourth (fifth early-method)))) 2521 (error "~S is not an early-method." early-method))) 2522 2523(defun early-method-qualifiers (early-method) 2524 (second (fifth early-method))) 2525 2526(defun early-method-lambda-list (early-method) 2527 (third (fifth early-method))) 2528 2529(defun early-method-initargs (early-method) 2530 (fifth (fifth early-method))) 2531 2532(defun (setf early-method-initargs) (new-value early-method) 2533 (setf (fifth (fifth early-method)) new-value)) 2534 2535(defun !early-add-named-method (generic-function-name qualifiers 2536 specializers arglist &rest initargs 2537 &key documentation definition-source 2538 &allow-other-keys) 2539 (let* (;; we don't need to deal with the :generic-function-class 2540 ;; argument here because the default, 2541 ;; STANDARD-GENERIC-FUNCTION, is right for all early generic 2542 ;; functions. (See REAL-ADD-NAMED-METHOD) 2543 (gf (ensure-generic-function generic-function-name)) 2544 (existing 2545 (dolist (m (early-gf-methods gf)) 2546 (when (and (equal (early-method-specializers m) specializers) 2547 (equal (early-method-qualifiers m) qualifiers)) 2548 (return m))))) 2549 (setf (getf (getf initargs 'plist) :name) 2550 (make-method-spec gf qualifiers specializers)) 2551 (let ((new (make-a-method 'standard-method qualifiers arglist 2552 specializers initargs documentation 2553 :definition-source definition-source))) 2554 (when existing (remove-method gf existing)) 2555 (add-method gf new)))) 2556 2557;;; This is the early version of ADD-METHOD. Later this will become a 2558;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has 2559;;; special knowledge about ADD-METHOD. 2560(defun add-method (generic-function method) 2561 (when (not (fsc-instance-p generic-function)) 2562 (error "Early ADD-METHOD didn't get a funcallable instance.")) 2563 (when (not (and (listp method) (eq (car method) :early-method))) 2564 (error "Early ADD-METHOD didn't get an early method.")) 2565 (push method (early-gf-methods generic-function)) 2566 (set-arg-info generic-function :new-method method) 2567 (unless (assoc (!early-gf-name generic-function) 2568 *!generic-function-fixups* 2569 :test #'equal) 2570 (update-dfun generic-function))) 2571 2572;;; This is the early version of REMOVE-METHOD. See comments on 2573;;; the early version of ADD-METHOD. 2574(defun remove-method (generic-function method) 2575 (when (not (fsc-instance-p generic-function)) 2576 (error "An early remove-method didn't get a funcallable instance.")) 2577 (when (not (and (listp method) (eq (car method) :early-method))) 2578 (error "An early remove-method didn't get an early method.")) 2579 (setf (early-gf-methods generic-function) 2580 (remove method (early-gf-methods generic-function))) 2581 (set-arg-info generic-function) 2582 (unless (assoc (!early-gf-name generic-function) 2583 *!generic-function-fixups* 2584 :test #'equal) 2585 (update-dfun generic-function))) 2586 2587;;; This is the early version of GET-METHOD. See comments on the early 2588;;; version of ADD-METHOD. 2589(defun get-method (generic-function qualifiers specializers 2590 &optional (errorp t)) 2591 (if (early-gf-p generic-function) 2592 (or (dolist (m (early-gf-methods generic-function)) 2593 (when (and (or (equal (early-method-specializers m nil) 2594 specializers) 2595 (equal (early-method-specializers m t) 2596 specializers)) 2597 (equal (early-method-qualifiers m) qualifiers)) 2598 (return m))) 2599 (if errorp 2600 (error "can't get early method") 2601 nil)) 2602 (real-get-method generic-function qualifiers specializers errorp))) 2603 2604;; minor KLUDGE: a separate code component for this function allows GCing 2605;; a few symbols and their associated code that would otherwise be retained: 2606;; *!EARLY-{GENERIC-}FUNCTIONS*, *!GENERIC-FUNCTION-FIXUPS* 2607(defun early-gf-primary-slow-method-fn (fn) 2608 (lambda (args next-methods) 2609 (declare (ignore next-methods)) 2610 (apply fn args))) 2611 2612(defun !fix-early-generic-functions () 2613 (let ((accessors nil)) 2614 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up 2615 ;; FIX-EARLY-GENERIC-FUNCTIONS. 2616 (dolist (early-gf-spec *!early-generic-functions*) 2617 (when (every #'early-method-standard-accessor-p 2618 (early-gf-methods (gdefinition early-gf-spec))) 2619 (push early-gf-spec accessors))) 2620 (dolist (spec (nconc accessors 2621 '(accessor-method-slot-name 2622 generic-function-methods 2623 method-specializers 2624 specializer-type 2625 specializer-class 2626 slot-definition-location 2627 slot-definition-name 2628 class-slots 2629 gf-arg-info 2630 class-precedence-list 2631 slot-boundp-using-class 2632 (setf slot-value-using-class) 2633 slot-value-using-class))) 2634 (/show spec) 2635 (setq *!early-generic-functions* 2636 (cons spec 2637 (delete spec *!early-generic-functions* :test #'equal)))) 2638 2639 (dolist (early-gf-spec *!early-generic-functions*) 2640 (/show early-gf-spec) 2641 (let* ((gf (gdefinition early-gf-spec)) 2642 (methods (mapcar (lambda (early-method) 2643 (let ((args (copy-list (fifth 2644 early-method)))) 2645 (setf (fourth args) 2646 (early-method-specializers 2647 early-method t)) 2648 (apply #'real-make-a-method args))) 2649 (early-gf-methods gf)))) 2650 (setf (generic-function-method-class gf) *the-class-standard-method*) 2651 (setf (generic-function-method-combination gf) 2652 *standard-method-combination*) 2653 (set-methods gf methods))) 2654 2655 (dolist (fn *!early-functions*) 2656 (/show fn) 2657 (setf (gdefinition (car fn)) (fdefinition (caddr fn)))) 2658 2659 (loop for (fspec method-combination . methods) in *!generic-function-fixups* 2660 for gf = (gdefinition fspec) do 2661 (flet ((make-method (spec) 2662 (destructuring-bind 2663 (lambda-list specializers qualifiers fun-name) spec 2664 (let* ((specializers (mapcar #'find-class specializers)) 2665 (fun-name (or fun-name fspec)) 2666 (fun (fdefinition fun-name)) 2667 (initargs (list :function 2668 (set-fun-name 2669 (early-gf-primary-slow-method-fn fun) 2670 `(call ,fun-name))))) 2671 (declare (type function fun)) 2672 (make-a-method 2673 'standard-method 2674 qualifiers lambda-list specializers initargs nil))))) 2675 (setf (generic-function-method-class gf) 2676 *the-class-standard-method* 2677 (generic-function-method-combination gf) 2678 (ecase method-combination 2679 (standard *standard-method-combination*) 2680 (or *or-method-combination*))) 2681 (set-methods gf (mapcar #'make-method methods))))) 2682 2683 (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS")) 2684 2685;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument 2686;;; into the 'real' arguments. This is where the syntax of DEFMETHOD 2687;;; is really implemented. 2688(defun parse-defmethod (cdr-of-form) 2689 (declare (list cdr-of-form)) 2690 (let ((qualifiers ()) 2691 (spec-ll ())) 2692 (loop (if (and (car cdr-of-form) (atom (car cdr-of-form))) 2693 (push (pop cdr-of-form) qualifiers) 2694 (return (setq qualifiers (nreverse qualifiers))))) 2695 (setq spec-ll (pop cdr-of-form)) 2696 (values qualifiers spec-ll cdr-of-form))) 2697 2698(defun parse-specializers (generic-function specializers) 2699 (declare (list specializers)) 2700 (flet ((parse (spec) 2701 (parse-specializer-using-class generic-function spec))) 2702 (mapcar #'parse specializers))) 2703 2704(defun unparse-specializers (generic-function specializers) 2705 (declare (list specializers)) 2706 (flet ((unparse (spec) 2707 (unparse-specializer-using-class generic-function spec))) 2708 (mapcar #'unparse specializers))) 2709 2710(macrolet ((def (n name) 2711 `(defun ,name (lambda-list) 2712 (nth-value ,n (parse-specialized-lambda-list lambda-list))))) 2713 ;; We don't need these, but according to the unit tests, 2714 ;; they're mandated by AMOP. 2715 (def 1 extract-lambda-list) 2716 (def 2 extract-specializer-names)) 2717 2718(define-condition specialized-lambda-list-error 2719 (reference-condition simple-program-error) 2720 () 2721 (:default-initargs :references (list '(:ansi-cl :section (3 4 3))))) 2722 2723;; Return 3 values: 2724;; - the bound variables, without defaults, supplied-p vars, or &AUX vars. 2725;; - the lambda list without specializers. 2726;; - just the specializers 2727(defun parse-specialized-lambda-list (arglist) 2728 (multiple-value-bind (llks specialized optional rest key aux) 2729 (parse-lambda-list 2730 arglist 2731 :context 'defmethod 2732 :accept (lambda-list-keyword-mask 2733 '(&optional &rest &key &allow-other-keys &aux)) 2734 :silent t ; never signal &OPTIONAL + &KEY style-warning 2735 :condition-class 'specialized-lambda-list-error) 2736 (let ((required (mapcar (lambda (x) (if (listp x) (car x) x)) specialized))) 2737 (values (append required 2738 (mapcar #'parse-optional-arg-spec optional) 2739 rest 2740 ;; Preserve keyword-names when given as (:KEYWORD var) 2741 (mapcar (lambda (x) (if (typep x '(cons cons)) 2742 (car x) 2743 (parse-key-arg-spec x))) key)) 2744 (make-lambda-list llks nil required optional rest key aux) 2745 (mapcar (lambda (x) (if (listp x) (cadr x) t)) specialized))))) 2746 2747(setq **boot-state** 'early) 2748 2749;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET 2750;;; which used %WALKER stuff. That suggests to me that maybe the code 2751;;; walker stuff was only used for implementing stuff like that; maybe 2752;;; it's not needed any more? Hunt down what it was used for and see. 2753 2754(defun extract-the (form) 2755 (cond ((and (consp form) (eq (car form) 'the)) 2756 (aver (proper-list-of-length-p form 3)) 2757 (third form)) 2758 (t 2759 form))) 2760 2761(defmacro with-slots (slots instance &body body) 2762 (let ((in (gensym))) 2763 `(let ((,in ,instance)) 2764 (declare (ignorable ,in)) 2765 ,@(let ((instance (extract-the instance))) 2766 (and (symbolp instance) 2767 `((declare (%variable-rebinding ,in ,instance))))) 2768 ,in 2769 (symbol-macrolet ,(mapcar (lambda (slot-entry) 2770 (let ((var-name 2771 (if (symbolp slot-entry) 2772 slot-entry 2773 (car slot-entry))) 2774 (slot-name 2775 (if (symbolp slot-entry) 2776 slot-entry 2777 (cadr slot-entry)))) 2778 `(,var-name 2779 (slot-value ,in ',slot-name)))) 2780 slots) 2781 ,@body)))) 2782 2783(defmacro with-accessors (slots instance &body body) 2784 (let ((in (gensym))) 2785 `(let ((,in ,instance)) 2786 (declare (ignorable ,in)) 2787 ,@(let ((instance (extract-the instance))) 2788 (and (symbolp instance) 2789 `((declare (%variable-rebinding ,in ,instance))))) 2790 ,in 2791 (symbol-macrolet ,(mapcar (lambda (slot-entry) 2792 (let ((var-name (car slot-entry)) 2793 (accessor-name (cadr slot-entry))) 2794 `(,var-name (,accessor-name ,in)))) 2795 slots) 2796 ,@body)))) 2797