1 ;;;; This software is part of the SBCL system. See the README file for
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
10 ;;;; copyright information from original PCL sources:
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
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
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
29 ;;; Methods themselves are simple inanimate objects. Most properties of
30 ;;; methods are immutable, methods cannot be reinitialized. The following
31 ;;; properties of methods can be changed:
32 ;;; METHOD-GENERIC-FUNCTION
33 ;;; METHOD-FUNCTION ??
35 (defmethod method-function ((method standard-method))
36 (or (slot-value method '%function)
37 (let ((fmf (slot-value method 'fast-function)))
38 (unless fmf ; The :BEFORE SHARED-INITIALIZE method prevents this.
39 (error "~S doesn't seem to have a METHOD-FUNCTION." method))
40 (setf (slot-value method '%function)
41 (method-function-from-fast-function fmf)))))
43 (defmethod accessor-method-class ((method standard-accessor-method))
44 (car (slot-value method 'specializers)))
46 (defmethod accessor-method-class ((method standard-writer-method))
47 (cadr (slot-value method 'specializers)))
51 ;;; Error checking is done in before methods. Because of the simplicity of
52 ;;; standard method objects the standard primary method can fill the slots.
54 ;;; Methods are not reinitializable.
56 (define-condition metaobject-initialization-violation
57 (reference-condition simple-error)
60 (macrolet ((def (name args control)
61 `(defmethod ,name ,args
62 (declare (ignore initargs))
63 (error 'metaobject-initialization-violation
64 :format-control ,(format nil "~@<~A~@:>" control)
65 :format-arguments (list ',name)
66 :references (list '(:amop :initialization method))))))
67 (def reinitialize-instance ((method method) &rest initargs)
68 "Method objects cannot be redefined by ~S.")
69 (def change-class ((method method) new &rest initargs)
70 "Method objects cannot be redefined by ~S.")
71 ;; NEW being a subclass of method is dealt with in the general
72 ;; method of CHANGE-CLASS
73 (def update-instance-for-redefined-class ((method method) added discarded
75 "No behaviour specified for ~S on method objects.")
76 (def update-instance-for-different-class (old (new method) &rest initargs)
77 "No behaviour specified for ~S on method objects.")
78 (def update-instance-for-different-class ((old method) new &rest initargs)
79 "No behaviour specified for ~S on method objects."))
81 (defmethod legal-documentation-p ((object standard-method) x)
82 (if (or (null x) (stringp x))
86 (defmethod legal-lambda-list-p ((object standard-method) x)
90 (defmethod legal-method-function-p ((object standard-method) x)
95 (defmethod legal-qualifiers-p ((object standard-method) x)
96 (flet ((improper-list ()
97 (return-from legal-qualifiers-p "Is not a proper list.")))
98 (dolist-carefully (q x improper-list)
99 (let ((ok (legal-qualifier-p object q)))
101 (return-from legal-qualifiers-p
102 (format nil "Contains ~S which ~A" q ok)))))
105 (defmethod legal-qualifier-p ((object standard-method) x)
108 "is not a non-null atom"))
110 (defmethod legal-slot-name-p ((object standard-method) x)
111 (cond ((not (symbolp x)) "is not a symbol")
114 (defmethod legal-specializers-p ((object standard-method) x)
115 (flet ((improper-list ()
116 (return-from legal-specializers-p "Is not a proper list.")))
117 (dolist-carefully (s x improper-list)
118 (let ((ok (legal-specializer-p object s)))
120 (return-from legal-specializers-p
121 (format nil "Contains ~S which ~A" s ok)))))
124 (defvar *allow-experimental-specializers-p* nil)
126 (defmethod legal-specializer-p ((object standard-method) x)
127 (if (if *allow-experimental-specializers-p*
130 (eql-specializer-p x)))
132 "is neither a class object nor an EQL specializer"))
134 (defmethod shared-initialize :before ((method standard-method)
142 (declare (ignore slot-names))
143 (flet ((lose (initarg value string)
144 (error "when initializing the method ~S:~%~
145 The ~S initialization argument was: ~S.~%~
147 method initarg value string)))
148 (let ((check-qualifiers (legal-qualifiers-p method qualifiers))
149 (check-lambda-list (legal-lambda-list-p method lambda-list))
150 (check-specializers (legal-specializers-p method specializers))
151 (check-fun (legal-method-function-p method
154 (check-documentation (legal-documentation-p method documentation)))
155 (unless (eq check-qualifiers t)
156 (lose :qualifiers qualifiers check-qualifiers))
157 (unless (eq check-lambda-list t)
158 (lose :lambda-list lambda-list check-lambda-list))
159 (unless (eq check-specializers t)
160 (lose :specializers specializers check-specializers))
161 (unless (eq check-fun t)
162 (lose :function function check-fun))
163 (unless (eq check-documentation t)
164 (lose :documentation documentation check-documentation)))))
166 (defmethod shared-initialize :before ((method standard-accessor-method)
168 &key slot-name slot-definition)
169 (declare (ignore slot-names))
170 (unless slot-definition
171 (let ((legalp (legal-slot-name-p method slot-name)))
172 ;; FIXME: nasty convention; should be renamed to ILLEGAL-SLOT-NAME-P and
173 ;; ILLEGALP, and the convention redone to be less twisty
174 (unless (eq legalp t)
175 (error "The value of the :SLOT-NAME initarg ~A." legalp)))))
177 (defmethod shared-initialize :after ((method standard-method) slot-names
179 &key qualifiers method-spec plist)
180 (declare (ignore slot-names method-spec plist))
181 (initialize-method-function initargs nil method)
182 (setf (plist-value method 'qualifiers) qualifiers)
184 (setf (slot-value method 'closure-generator)
185 (method-function-closure-generator (slot-value method '%function))))
187 (defmethod shared-initialize :after ((method standard-accessor-method)
190 (declare (ignore slot-names))
191 (with-slots (slot-name %slot-definition) method
192 (unless %slot-definition
193 (let ((class (accessor-method-class method)))
194 (when (slot-class-p class)
195 (setq %slot-definition (find slot-name (class-direct-slots class)
196 :key #'slot-definition-name)))))
197 (when (and %slot-definition (null slot-name))
198 (setq slot-name (slot-definition-name %slot-definition)))))
200 (defmethod method-qualifiers ((method standard-method))
201 (plist-value method 'qualifiers))
203 (defvar *the-class-generic-function*
204 (find-class 'generic-function))
205 (defvar *the-class-standard-generic-function*
206 (find-class 'standard-generic-function))
208 (defmethod shared-initialize :before
209 ((generic-function standard-generic-function)
211 &key (name nil namep)
212 (lambda-list () lambda-list-p)
213 argument-precedence-order
216 (method-class nil method-class-supplied-p)
217 (method-combination nil method-combination-supplied-p))
218 (declare (ignore slot-names
219 declarations argument-precedence-order documentation
220 lambda-list lambda-list-p))
223 (set-fun-name generic-function name))
225 (flet ((initarg-error (initarg value string)
226 (error "when initializing the generic function ~S:~%~
227 The ~S initialization argument was: ~A.~%~
229 generic-function initarg value string)))
230 (cond (method-class-supplied-p
231 (when (symbolp method-class)
232 (setq method-class (find-class method-class)))
233 (unless (and (classp method-class)
234 (*subtypep (class-eq-specializer method-class)
236 (initarg-error :method-class
238 "a subclass of the class METHOD"))
239 (setf (slot-value generic-function 'method-class) method-class))
240 ((slot-boundp generic-function 'method-class))
242 (initarg-error :method-class
244 "a subclass of the class METHOD")))
245 (cond (method-combination-supplied-p
246 (unless (method-combination-p method-combination)
247 (initarg-error :method-combination
249 "a method combination object")))
250 ((slot-boundp generic-function '%method-combination))
252 (initarg-error :method-combination
254 "a method combination object")))))
257 (defmethod reinitialize-instance ((generic-function standard-generic-function)
261 argument-precedence-order
266 (declare (ignore documentation declarations argument-precedence-order
267 lambda-list name method-class method-combination))
268 (macrolet ((add-initarg (check name slot-name)
270 (push (slot-value generic-function ,slot-name) initargs)
271 (push ,name initargs))))
272 ; (add-initarg name :name 'name)
273 ; (add-initarg lambda-list :lambda-list 'lambda-list)
274 ; (add-initarg argument-precedence-order
275 ; :argument-precedence-order
276 ; 'argument-precedence-order)
277 ; (add-initarg declarations :declarations 'declarations)
278 ; (add-initarg documentation :documentation '%documentation)
279 ; (add-initarg method-class :method-class 'method-class)
280 ; (add-initarg method-combination :method-combination '%method-combination)
281 (apply #'call-next-method generic-function initargs)))
284 ;;; These two are scheduled for demolition.
285 (defun real-add-named-method (generic-function-name
289 &rest other-initargs)
290 (unless (and (fboundp generic-function-name)
291 (typep (fdefinition generic-function-name) 'generic-function))
292 (style-warn "implicitly creating new generic function ~S"
293 generic-function-name))
294 ;; XXX What about changing the class of the generic function if
295 ;; there is one? Whose job is that, anyway? Do we need something
296 ;; kind of like CLASS-FOR-REDEFINITION?
297 (let* ((generic-function
298 (ensure-generic-function generic-function-name))
299 (specs (parse-specializers specializers))
300 (proto (method-prototype-for-gf generic-function-name))
301 (new (apply #'make-instance (class-of proto)
302 :qualifiers qualifiers
304 :lambda-list lambda-list
306 (add-method generic-function new)
309 (define-condition find-method-length-mismatch
310 (reference-condition simple-error)
312 (:default-initargs :references (list '(:ansi-cl :function find-method))))
314 (defun real-get-method (generic-function qualifiers specializers
316 always-check-specializers)
317 (let ((lspec (length specializers))
318 (methods (generic-function-methods generic-function)))
319 (when (or methods always-check-specializers)
320 (let ((nreq (length (arg-info-metatypes (gf-arg-info
321 generic-function)))))
322 ;; Since we internally bypass FIND-METHOD by using GET-METHOD
323 ;; instead we need to to this here or users may get hit by a
324 ;; failed AVER instead of a sensible error message.
325 (when (/= lspec nreq)
327 'find-method-length-mismatch
329 "~@<The generic function ~S takes ~D required argument~:P; ~
330 was asked to find a method with specializers ~S~@:>"
331 :format-arguments (list generic-function nreq specializers)))))
333 (dolist (method methods)
334 (let ((mspecializers (method-specializers method)))
335 (aver (= lspec (length mspecializers)))
336 (when (and (equal qualifiers (method-qualifiers method))
337 (every #'same-specializer-p specializers
338 (method-specializers method)))
343 (error "~@<There is no method on ~S with ~
344 ~:[no qualifiers~;~:*qualifiers ~S~] ~
345 and specializers ~S.~@:>"
346 generic-function qualifiers specializers))))))
348 (defmethod find-method ((generic-function standard-generic-function)
349 qualifiers specializers &optional (errorp t))
350 ;; ANSI about FIND-METHOD: "The specializers argument contains the
351 ;; parameter specializers for the method. It must correspond in
352 ;; length to the number of required arguments of the generic
353 ;; function, or an error is signaled."
355 ;; This error checking is done by REAL-GET-METHOD.
356 (real-get-method generic-function
358 (parse-specializers specializers)
362 ;;; Compute various information about a generic-function's arglist by looking
363 ;;; at the argument lists of the methods. The hair for trying not to use
364 ;;; &REST arguments lives here.
365 ;;; The values returned are:
366 ;;; number-of-required-arguments
367 ;;; the number of required arguments to this generic-function's
368 ;;; discriminating function
370 ;;; whether or not this generic-function's discriminating
371 ;;; function takes an &rest argument.
372 ;;; specialized-argument-positions
373 ;;; a list of the positions of the arguments this generic-function
374 ;;; specializes (e.g. for a classical generic-function this is the
376 (defmethod compute-discriminating-function-arglist-info
377 ((generic-function standard-generic-function))
378 ;;(declare (values number-of-required-arguments &rest-argument-p
379 ;; specialized-argument-postions))
380 (let ((number-required nil)
382 (specialized-positions ())
383 (methods (generic-function-methods generic-function)))
384 (dolist (method methods)
385 (multiple-value-setq (number-required restp specialized-positions)
386 (compute-discriminating-function-arglist-info-internal
387 generic-function method number-required restp specialized-positions)))
388 (values number-required restp (sort specialized-positions #'<))))
390 (defun compute-discriminating-function-arglist-info-internal
391 (generic-function method number-of-requireds restp
392 specialized-argument-positions)
393 (declare (ignore generic-function)
394 (type (or null fixnum) number-of-requireds))
396 (declare (fixnum requireds))
397 ;; Go through this methods arguments seeing how many are required,
398 ;; and whether there is an &rest argument.
399 (dolist (arg (method-lambda-list method))
400 (cond ((eq arg '&aux) (return))
401 ((memq arg '(&optional &rest &key))
402 (return (setq restp t)))
403 ((memq arg lambda-list-keywords))
404 (t (incf requireds))))
405 ;; Now go through this method's type specifiers to see which
406 ;; argument positions are type specified. Treat T specially
407 ;; in the usual sort of way. For efficiency don't bother to
408 ;; keep specialized-argument-positions sorted, rather depend
409 ;; on our caller to do that.
411 (dolist (type-spec (method-specializers method))
412 (unless (eq type-spec *the-class-t*)
413 (pushnew pos specialized-argument-positions))
415 ;; Finally merge the values for this method into the values
416 ;; for the exisiting methods and return them. Note that if
417 ;; num-of-requireds is NIL it means this is the first method
418 ;; and we depend on that.
419 (values (min (or number-of-requireds requireds) requireds)
421 (and number-of-requireds (/= number-of-requireds requireds)))
422 specialized-argument-positions)))
424 (defun make-discriminating-function-arglist (number-required-arguments restp)
425 (nconc (let ((args nil))
426 (dotimes (i number-required-arguments)
427 (push (format-symbol *package* ;; ! is this right?
428 "Discriminating Function Arg ~D"
433 `(&rest ,(format-symbol *package*
434 "Discriminating Function &rest Arg")))))
436 (defmethod generic-function-argument-precedence-order
437 ((gf standard-generic-function))
438 (aver (eq *boot-state* 'complete))
439 (loop with arg-info = (gf-arg-info gf)
440 with lambda-list = (arg-info-lambda-list arg-info)
441 for argument-position in (arg-info-precedence arg-info)
442 collect (nth argument-position lambda-list)))
444 (defmethod generic-function-lambda-list ((gf generic-function))
447 (defmethod gf-fast-method-function-p ((gf standard-generic-function))
448 (gf-info-fast-mf-p (slot-value gf 'arg-info)))
450 (defmethod initialize-instance :after ((gf standard-generic-function)
451 &key (lambda-list nil lambda-list-p)
452 argument-precedence-order)
453 (with-slots (arg-info) gf
456 :lambda-list lambda-list
457 :argument-precedence-order argument-precedence-order)
459 (when (arg-info-valid-p arg-info)
462 (defmethod reinitialize-instance :around
463 ((gf standard-generic-function) &rest args &key
464 (lambda-list nil lambda-list-p) (argument-precedence-order nil apo-p))
465 (let ((old-mc (generic-function-method-combination gf)))
466 (prog1 (call-next-method)
467 ;; KLUDGE: EQ is too strong a test.
468 (unless (eq old-mc (generic-function-method-combination gf))
469 (flush-effective-method-cache gf))
471 ((and lambda-list-p apo-p)
473 :lambda-list lambda-list
474 :argument-precedence-order argument-precedence-order))
475 (lambda-list-p (set-arg-info gf :lambda-list lambda-list))
476 (t (set-arg-info gf)))
477 (when (arg-info-valid-p (gf-arg-info gf))
479 (map-dependents gf (lambda (dependent)
480 (apply #'update-dependent gf dependent args))))))
482 (declaim (special *lazy-dfun-compute-p*))
484 (defun set-methods (gf methods)
485 (setf (generic-function-methods gf) nil)
486 (loop (when (null methods) (return gf))
487 (real-add-method gf (pop methods) methods)))
489 (define-condition new-value-specialization (reference-condition error)
490 ((%method :initarg :method :reader new-value-specialization-method))
493 (format s "~@<Cannot add method ~S to ~S, as it specializes the ~
494 new-value argument.~@:>"
495 (new-value-specialization-method c)
496 #'(setf slot-value-using-class))))
497 (:default-initargs :references
498 (list '(:sbcl :node "Metaobject Protocol")
499 '(:amop :generic-function (setf slot-value-using-class)))))
501 (defun real-add-method (generic-function method &optional skip-dfun-update-p)
502 (when (method-generic-function method)
503 (error "~@<The method ~S is already part of the generic ~
504 function ~S; it can't be added to another generic ~
505 function until it is removed from the first one.~@:>"
506 method (method-generic-function method)))
507 (flet ((similar-lambda-lists-p (method-a method-b)
508 (multiple-value-bind (a-nreq a-nopt a-keyp a-restp)
509 (analyze-lambda-list (method-lambda-list method-a))
510 (multiple-value-bind (b-nreq b-nopt b-keyp b-restp)
511 (analyze-lambda-list (method-lambda-list method-b))
512 (and (= a-nreq b-nreq)
514 (eq (or a-keyp a-restp)
515 (or b-keyp b-restp)))))))
516 (let* ((name (generic-function-name generic-function))
517 (qualifiers (method-qualifiers method))
518 (specializers (method-specializers method))
519 (existing (get-method generic-function
524 ;; If there is already a method like this one then we must get
525 ;; rid of it before proceeding. Note that we call the generic
526 ;; function REMOVE-METHOD to remove it rather than doing it in
527 ;; some internal way.
528 (when (and existing (similar-lambda-lists-p existing method))
529 (remove-method generic-function existing))
531 ;; KLUDGE: We have a special case here, as we disallow
532 ;; specializations of the NEW-VALUE argument to (SETF
533 ;; SLOT-VALUE-USING-CLASS). GET-ACCESSOR-METHOD-FUNCTION is
534 ;; the optimizing function here: it precomputes the effective
535 ;; method, assuming that there is no dispatch to be done on
536 ;; the new-value argument.
537 (when (and (eq generic-function #'(setf slot-value-using-class))
538 (not (eq *the-class-t* (first specializers))))
539 (error 'new-value-specialization
542 (setf (method-generic-function method) generic-function)
543 (pushnew method (generic-function-methods generic-function))
544 (dolist (specializer specializers)
545 (add-direct-method specializer method))
547 ;; KLUDGE: SET-ARG-INFO contains the error-detecting logic for
548 ;; detecting attempts to add methods with incongruent lambda
549 ;; lists. However, according to Gerd Moellmann on cmucl-imp,
550 ;; it also depends on the new method already having been added
551 ;; to the generic function. Therefore, we need to remove it
553 (let ((remove-again-p t))
556 (set-arg-info generic-function :new-method method)
557 (setq remove-again-p nil))
559 (remove-method generic-function method))))
561 ;; KLUDGE II: ANSI saith that it is not an error to add a
562 ;; method with invalid qualifiers to a generic function of the
563 ;; wrong kind; it's only an error at generic function
564 ;; invocation time; I dunno what the rationale was, and it
565 ;; sucks. Nevertheless, it's probably a programmer error, so
566 ;; let's warn anyway. -- CSR, 2003-08-20
567 (let ((mc (generic-function-method-combination generic-functioN)))
569 ((eq mc *standard-method-combination*)
570 (when (and qualifiers
572 (not (memq (car qualifiers)
573 '(:around :before :after)))))
574 (warn "~@<Invalid qualifiers for standard method combination ~
575 in method ~S:~2I~_~S.~@:>"
577 ((short-method-combination-p mc)
578 (let ((mc-name (method-combination-type-name mc)))
579 (when (or (null qualifiers)
581 (and (neq (car qualifiers) :around)
582 (neq (car qualifiers) mc-name)))
583 (warn "~@<Invalid qualifiers for ~S method combination ~
584 in method ~S:~2I~_~S.~@:>"
585 mc-name method qualifiers))))))
587 (unless skip-dfun-update-p
588 (update-ctors 'add-method
589 :generic-function generic-function
591 (update-dfun generic-function))
592 (map-dependents generic-function
594 (update-dependent generic-function
595 dep 'add-method method)))
598 (defun real-remove-method (generic-function method)
599 (when (eq generic-function (method-generic-function method))
600 (let* ((name (generic-function-name generic-function))
601 (specializers (method-specializers method))
602 (methods (generic-function-methods generic-function))
603 (new-methods (remove method methods)))
604 (setf (method-generic-function method) nil)
605 (setf (generic-function-methods generic-function) new-methods)
606 (dolist (specializer (method-specializers method))
607 (remove-direct-method specializer method))
608 (set-arg-info generic-function)
609 (update-ctors 'remove-method
610 :generic-function generic-function
612 (update-dfun generic-function)
613 (map-dependents generic-function
615 (update-dependent generic-function
616 dep 'remove-method method)))))
619 (defun compute-applicable-methods-function (generic-function arguments)
620 (values (compute-applicable-methods-using-types
622 (types-from-args generic-function arguments 'eql))))
624 (defmethod compute-applicable-methods
625 ((generic-function generic-function) arguments)
626 (values (compute-applicable-methods-using-types
628 (types-from-args generic-function arguments 'eql))))
630 (defmethod compute-applicable-methods-using-classes
631 ((generic-function generic-function) classes)
632 (compute-applicable-methods-using-types
634 (types-from-args generic-function classes 'class-eq)))
636 (defun proclaim-incompatible-superclasses (classes)
637 (setq classes (mapcar (lambda (class)
642 (dolist (class classes)
643 (dolist (other-class classes)
644 (unless (eq class other-class)
645 (pushnew other-class (class-incompatible-superclass-list class))))))
647 (defun superclasses-compatible-p (class1 class2)
648 (let ((cpl1 (cpl-or-nil class1))
649 (cpl2 (cpl-or-nil class2)))
651 (dolist (ic (class-incompatible-superclass-list sc1))
653 (return-from superclasses-compatible-p nil))))))
656 #'proclaim-incompatible-superclasses
657 '(;; superclass class
658 (built-in-class std-class structure-class) ; direct subclasses of pcl-class
659 (standard-class funcallable-standard-class)
660 ;; superclass metaobject
661 (class eql-specializer class-eq-specializer method method-combination
662 generic-function slot-definition)
663 ;; metaclass built-in-class
664 (number sequence character ; direct subclasses of t, but not array
665 standard-object structure-object) ; or symbol
666 (number array character symbol ; direct subclasses of t, but not
667 standard-object structure-object) ; sequence
668 (complex float rational) ; direct subclasses of number
669 (integer ratio) ; direct subclasses of rational
670 (list vector) ; direct subclasses of sequence
671 (cons null) ; direct subclasses of list
672 (string bit-vector) ; direct subclasses of vector
675 (defmethod same-specializer-p ((specl1 specializer) (specl2 specializer))
678 (defmethod same-specializer-p ((specl1 class) (specl2 class))
681 (defmethod specializer-class ((specializer class))
684 (defmethod same-specializer-p ((specl1 class-eq-specializer)
685 (specl2 class-eq-specializer))
686 (eq (specializer-class specl1) (specializer-class specl2)))
688 (defmethod same-specializer-p ((specl1 eql-specializer)
689 (specl2 eql-specializer))
690 (eq (specializer-object specl1) (specializer-object specl2)))
692 (defmethod specializer-class ((specializer eql-specializer))
693 (class-of (slot-value specializer 'object)))
695 (defvar *in-gf-arg-info-p* nil)
696 (setf (gdefinition 'arg-info-reader)
697 (let ((mf (initialize-method-function
698 (make-internal-reader-method-function
699 'standard-generic-function 'arg-info)
701 (lambda (&rest args) (funcall mf args nil))))
704 (defun error-need-at-least-n-args (function n)
705 (error 'simple-program-error
706 :format-control "~@<The function ~2I~_~S ~I~_requires ~
707 at least ~W argument~:P.~:>"
708 :format-arguments (list function n)))
710 (defun types-from-args (generic-function arguments &optional type-modifier)
711 (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
712 (get-generic-fun-info generic-function)
713 (declare (ignore applyp metatypes nkeys))
714 (let ((types-rev nil))
715 (dotimes-fixnum (i nreq)
718 (error-need-at-least-n-args (generic-function-name generic-function)
720 (let ((arg (pop arguments)))
721 (push (if type-modifier `(,type-modifier ,arg) arg) types-rev)))
722 (values (nreverse types-rev) arg-info))))
724 (defun get-wrappers-from-classes (nkeys wrappers classes metatypes)
725 (let* ((w wrappers) (w-tail w) (mt-tail metatypes))
726 (dolist (class (if (listp classes) classes (list classes)))
727 (unless (eq t (car mt-tail))
728 (let ((c-w (class-wrapper class)))
729 (unless c-w (return-from get-wrappers-from-classes nil))
732 (setf (car w-tail) c-w
733 w-tail (cdr w-tail)))))
734 (setq mt-tail (cdr mt-tail)))
737 (defun sdfun-for-caching (gf classes)
738 (let ((types (mapcar #'class-eq-type classes)))
739 (multiple-value-bind (methods all-applicable-and-sorted-p)
740 (compute-applicable-methods-using-types gf types)
741 (let ((generator (get-secondary-dispatch-function1
742 gf methods types nil t all-applicable-and-sorted-p)))
743 (make-callable gf methods generator
744 nil (mapcar #'class-wrapper classes))))))
746 (defun value-for-caching (gf classes)
747 (let ((methods (compute-applicable-methods-using-types
748 gf (mapcar #'class-eq-type classes))))
749 (method-function-get (or (safe-method-fast-function (car methods))
750 (safe-method-function (car methods)))
753 (defun default-secondary-dispatch-function (generic-function)
755 (let ((methods (compute-applicable-methods generic-function args)))
757 (let ((emf (get-effective-method-function generic-function
759 (invoke-emf emf args))
760 (apply #'no-applicable-method generic-function args)))))
763 (loop (when (atom x) (return (eq x y)))
764 (when (atom y) (return nil))
765 (unless (eq (car x) (car y)) (return nil))
769 (defvar *std-cam-methods* nil)
771 (defun compute-applicable-methods-emf (generic-function)
772 (if (eq *boot-state* 'complete)
773 (let* ((cam (gdefinition 'compute-applicable-methods))
774 (cam-methods (compute-applicable-methods-using-types
775 cam (list `(eql ,generic-function) t))))
776 (values (get-effective-method-function cam cam-methods)
778 (or *std-cam-methods*
779 (setq *std-cam-methods*
780 (compute-applicable-methods-using-types
781 cam (list `(eql ,cam) t)))))))
782 (values #'compute-applicable-methods-function t)))
784 (defun compute-applicable-methods-emf-std-p (gf)
785 (gf-info-c-a-m-emf-std-p (gf-arg-info gf)))
787 (defvar *old-c-a-m-gf-methods* nil)
789 (defun update-all-c-a-m-gf-info (c-a-m-gf)
790 (let ((methods (generic-function-methods c-a-m-gf)))
791 (if (and *old-c-a-m-gf-methods*
792 (every (lambda (old-method)
793 (member old-method methods))
794 *old-c-a-m-gf-methods*))
795 (let ((gfs-to-do nil)
796 (gf-classes-to-do nil))
797 (dolist (method methods)
798 (unless (member method *old-c-a-m-gf-methods*)
799 (let ((specl (car (method-specializers method))))
800 (if (eql-specializer-p specl)
801 (pushnew (specializer-object specl) gfs-to-do)
802 (pushnew (specializer-class specl) gf-classes-to-do)))))
803 (map-all-generic-functions
805 (when (or (member gf gfs-to-do)
806 (dolist (class gf-classes-to-do nil)
808 (class-precedence-list (class-of gf)))))
809 (update-c-a-m-gf-info gf)))))
810 (map-all-generic-functions #'update-c-a-m-gf-info))
811 (setq *old-c-a-m-gf-methods* methods)))
813 (defun update-gf-info (gf)
814 (update-c-a-m-gf-info gf)
815 (update-gf-simple-accessor-type gf))
817 (defun update-c-a-m-gf-info (gf)
818 (unless (early-gf-p gf)
819 (multiple-value-bind (c-a-m-emf std-p)
820 (compute-applicable-methods-emf gf)
821 (let ((arg-info (gf-arg-info gf)))
822 (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
823 (setf (gf-info-c-a-m-emf-std-p arg-info) std-p)))))
825 (defun update-gf-simple-accessor-type (gf)
826 (let ((arg-info (gf-arg-info gf)))
827 (setf (gf-info-simple-accessor-type arg-info)
828 (let* ((methods (generic-function-methods gf))
829 (class (and methods (class-of (car methods))))
832 *the-class-standard-reader-method*)
835 *the-class-standard-writer-method*)
838 *the-class-standard-boundp-method*)
840 (when (and (gf-info-c-a-m-emf-std-p arg-info)
842 (dolist (method (cdr methods) t)
843 (unless (eq class (class-of method)) (return nil)))
844 (eq (generic-function-method-combination gf)
845 *standard-method-combination*))
849 ;;; CMUCL (Gerd's PCL, 2002-04-25) comment:
851 ;;; Return two values. First value is a function to be stored in
852 ;;; effective slot definition SLOTD for reading it with
853 ;;; SLOT-VALUE-USING-CLASS, setting it with (SETF
854 ;;; SLOT-VALUE-USING-CLASS) or testing it with
855 ;;; SLOT-BOUNDP-USING-CLASS. GF is one of these generic functions,
856 ;;; TYPE is one of the symbols READER, WRITER, BOUNDP. CLASS is
859 ;;; Second value is true if the function returned is one of the
860 ;;; optimized standard functions for the purpose, which are used
861 ;;; when only standard methods are applicable.
863 ;;; FIXME: Change all these wacky function names to something sane.
864 (defun get-accessor-method-function (gf type class slotd)
865 (let* ((std-method (standard-svuc-method type))
866 (str-method (structure-svuc-method type))
867 (types1 `((eql ,class) (class-eq ,class) (eql ,slotd)))
868 (types (if (eq type 'writer) `(t ,@types1) types1))
869 (methods (compute-applicable-methods-using-types gf types))
870 (std-p (null (cdr methods))))
873 (get-optimized-std-accessor-method-function class slotd type)
874 (let* ((optimized-std-fun
875 (get-optimized-std-slot-value-using-class-method-function
878 `((,(car (or (member std-method methods)
879 (member str-method methods)
881 'get-accessor-method-function)))
882 ,optimized-std-fun)))
884 (let ((wrappers (list (wrapper-of class)
885 (class-wrapper class)
886 (wrapper-of slotd))))
887 (if (eq type 'writer)
888 (cons (class-wrapper *the-class-t*) wrappers)
890 (sdfun (get-secondary-dispatch-function
891 gf methods types method-alist wrappers)))
892 (get-accessor-from-svuc-method-function class slotd sdfun type)))
895 ;;; used by OPTIMIZE-SLOT-VALUE-BY-CLASS-P (vector.lisp)
896 (defun update-slot-value-gf-info (gf type)
898 (update-std-or-str-methods gf type))
899 (when (and (standard-svuc-method type) (structure-svuc-method type))
900 (flet ((update-class (class)
901 (when (class-finalized-p class)
902 (dolist (slotd (class-slots class))
903 (compute-slot-accessor-info slotd type gf)))))
905 (update-class *new-class*)
906 (map-all-classes #'update-class 'slot-object)))))
908 (defvar *standard-slot-value-using-class-method* nil)
909 (defvar *standard-setf-slot-value-using-class-method* nil)
910 (defvar *standard-slot-boundp-using-class-method* nil)
911 (defvar *condition-slot-value-using-class-method* nil)
912 (defvar *condition-setf-slot-value-using-class-method* nil)
913 (defvar *condition-slot-boundp-using-class-method* nil)
914 (defvar *structure-slot-value-using-class-method* nil)
915 (defvar *structure-setf-slot-value-using-class-method* nil)
916 (defvar *structure-slot-boundp-using-class-method* nil)
918 (defun standard-svuc-method (type)
920 (reader *standard-slot-value-using-class-method*)
921 (writer *standard-setf-slot-value-using-class-method*)
922 (boundp *standard-slot-boundp-using-class-method*)))
924 (defun set-standard-svuc-method (type method)
926 (reader (setq *standard-slot-value-using-class-method* method))
927 (writer (setq *standard-setf-slot-value-using-class-method* method))
928 (boundp (setq *standard-slot-boundp-using-class-method* method))))
930 (defun condition-svuc-method (type)
932 (reader *condition-slot-value-using-class-method*)
933 (writer *condition-setf-slot-value-using-class-method*)
934 (boundp *condition-slot-boundp-using-class-method*)))
936 (defun set-condition-svuc-method (type method)
938 (reader (setq *condition-slot-value-using-class-method* method))
939 (writer (setq *condition-setf-slot-value-using-class-method* method))
940 (boundp (setq *condition-slot-boundp-using-class-method* method))))
942 (defun structure-svuc-method (type)
944 (reader *structure-slot-value-using-class-method*)
945 (writer *structure-setf-slot-value-using-class-method*)
946 (boundp *structure-slot-boundp-using-class-method*)))
948 (defun set-structure-svuc-method (type method)
950 (reader (setq *structure-slot-value-using-class-method* method))
951 (writer (setq *structure-setf-slot-value-using-class-method* method))
952 (boundp (setq *structure-slot-boundp-using-class-method* method))))
954 (defun update-std-or-str-methods (gf type)
955 (dolist (method (generic-function-methods gf))
956 (let ((specls (method-specializers method)))
957 (when (and (or (not (eq type 'writer))
958 (eq (pop specls) *the-class-t*))
959 (every #'classp specls))
960 (cond ((and (eq (class-name (car specls)) 'std-class)
961 (eq (class-name (cadr specls)) 'standard-object)
962 (eq (class-name (caddr specls))
963 'standard-effective-slot-definition))
964 (set-standard-svuc-method type method))
965 ((and (eq (class-name (car specls)) 'condition-class)
966 (eq (class-name (cadr specls)) 'condition)
967 (eq (class-name (caddr specls))
968 'condition-effective-slot-definition))
969 (set-condition-svuc-method type method))
970 ((and (eq (class-name (car specls)) 'structure-class)
971 (eq (class-name (cadr specls)) 'structure-object)
972 (eq (class-name (caddr specls))
973 'structure-effective-slot-definition))
974 (set-structure-svuc-method type method)))))))
976 (defun mec-all-classes-internal (spec precompute-p)
977 (unless (invalid-wrapper-p (class-wrapper (specializer-class spec)))
978 (cons (specializer-class spec)
981 (not (or (eq spec *the-class-t*)
982 (eq spec *the-class-slot-object*)
983 (eq spec *the-class-standard-object*)
984 (eq spec *the-class-structure-object*)))
985 (let ((sc (class-direct-subclasses spec)))
987 (mapcan (lambda (class)
988 (mec-all-classes-internal class precompute-p))
991 (defun mec-all-classes (spec precompute-p)
992 (let ((classes (mec-all-classes-internal spec precompute-p)))
993 (if (null (cdr classes))
995 (let* ((a-classes (cons nil classes))
997 (loop (when (null (cdr tail))
998 (return (cdr a-classes)))
999 (let ((class (cadr tail))
1000 (ttail (cddr tail)))
1001 (if (dolist (c ttail nil)
1002 (when (eq class c) (return t)))
1003 (setf (cdr tail) (cddr tail))
1004 (setf tail (cdr tail)))))))))
1006 (defun mec-all-class-lists (spec-list precompute-p)
1007 (if (null spec-list)
1009 (let* ((car-all-classes (mec-all-classes (car spec-list)
1011 (all-class-lists (mec-all-class-lists (cdr spec-list)
1013 (mapcan (lambda (list)
1014 (mapcar (lambda (c) (cons c list)) car-all-classes))
1017 (defun make-emf-cache (generic-function valuep cache classes-list new-class)
1018 (let* ((arg-info (gf-arg-info generic-function))
1019 (nkeys (arg-info-nkeys arg-info))
1020 (metatypes (arg-info-metatypes arg-info))
1021 (wrappers (unless (eq nkeys 1) (make-list nkeys)))
1022 (precompute-p (gf-precompute-dfun-and-emf-p arg-info))
1023 (default '(default)))
1024 (flet ((add-class-list (classes)
1025 (when (or (null new-class) (memq new-class classes))
1026 (let ((wrappers (get-wrappers-from-classes
1027 nkeys wrappers classes metatypes)))
1029 (eq default (probe-cache cache wrappers default)))
1030 (let ((value (cond ((eq valuep t)
1031 (sdfun-for-caching generic-function
1033 ((eq valuep :constant-value)
1034 (value-for-caching generic-function
1036 (setq cache (fill-cache cache wrappers value))))))))
1038 (mapc #'add-class-list classes-list)
1039 (dolist (method (generic-function-methods generic-function))
1040 (mapc #'add-class-list
1041 (mec-all-class-lists (method-specializers method)
1045 (defmacro class-test (arg class)
1047 ((eq class *the-class-t*) t)
1048 ((eq class *the-class-slot-object*)
1049 `(not (typep (classoid-of ,arg) 'built-in-classoid)))
1050 ((eq class *the-class-standard-object*)
1051 `(or (std-instance-p ,arg) (fsc-instance-p ,arg)))
1052 ((eq class *the-class-funcallable-standard-object*)
1053 `(fsc-instance-p ,arg))
1055 `(typep ,arg ',(class-name class)))))
1057 (defmacro class-eq-test (arg class)
1058 `(eq (class-of ,arg) ',class))
1060 (defmacro eql-test (arg object)
1061 `(eql ,arg ',object))
1063 (defun dnet-methods-p (form)
1065 (or (eq (car form) 'methods)
1066 (eq (car form) 'unordered-methods))))
1068 ;;; This is CASE, but without gensyms.
1069 (defmacro scase (arg &rest clauses)
1070 `(let ((.case-arg. ,arg))
1071 (cond ,@(mapcar (lambda (clause)
1072 (list* (cond ((null (car clause))
1074 ((consp (car clause))
1075 (if (null (cdar clause))
1080 ((member (car clause) '(t otherwise))
1083 `(eql .case-arg. ',(car clause))))
1088 (defmacro mcase (arg &rest clauses) `(scase ,arg ,@clauses))
1090 (defun generate-discrimination-net (generic-function methods types sorted-p)
1091 (let* ((arg-info (gf-arg-info generic-function))
1092 (c-a-m-emf-std-p (gf-info-c-a-m-emf-std-p arg-info))
1093 (precedence (arg-info-precedence arg-info)))
1094 (generate-discrimination-net-internal
1095 generic-function methods types
1096 (lambda (methods known-types)
1098 (and c-a-m-emf-std-p
1100 (let ((sorted-methods nil))
1102 (copy-list methods) precedence
1104 (when sorted-methods (return-from one-order-p nil))
1105 (setq sorted-methods methods)))
1106 (setq methods sorted-methods))
1108 `(methods ,methods ,known-types)
1109 `(unordered-methods ,methods ,known-types)))
1110 (lambda (position type true-value false-value)
1111 (let ((arg (dfun-arg-symbol position)))
1112 (if (eq (car type) 'eql)
1113 (let* ((false-case-p (and (consp false-value)
1114 (or (eq (car false-value) 'scase)
1115 (eq (car false-value) 'mcase))
1116 (eq arg (cadr false-value))))
1117 (false-clauses (if false-case-p
1119 `((t ,false-value))))
1120 (case-sym (if (and (dnet-methods-p true-value)
1122 (eq (car false-value) 'mcase)
1123 (dnet-methods-p false-value)))
1126 (type-sym `(,(cadr type))))
1128 (,type-sym ,true-value)
1130 `(if ,(let ((arg (dfun-arg-symbol position)))
1132 (class `(class-test ,arg ,(cadr type)))
1133 (class-eq `(class-eq-test ,arg ,(cadr type)))))
1138 (defun class-from-type (type)
1139 (if (or (atom type) (eq (car type) t))
1142 (and (dolist (type (cdr type) *the-class-t*)
1143 (when (and (consp type) (not (eq (car type) 'not)))
1144 (return (class-from-type type)))))
1146 (eql (class-of (cadr type)))
1147 (class-eq (cadr type))
1148 (class (cadr type)))))
1150 (defun precompute-effective-methods (gf caching-p &optional classes-list-p)
1151 (let* ((arg-info (gf-arg-info gf))
1152 (methods (generic-function-methods gf))
1153 (precedence (arg-info-precedence arg-info))
1154 (*in-precompute-effective-methods-p* t)
1156 (generate-discrimination-net-internal
1158 (lambda (methods known-types)
1160 (when classes-list-p
1161 (push (mapcar #'class-from-type known-types) classes-list))
1162 (let ((no-eql-specls-p (not (methods-contain-eql-specializer-p
1167 (get-secondary-dispatch-function1
1168 gf methods known-types
1169 nil caching-p no-eql-specls-p))))))
1170 (lambda (position type true-value false-value)
1171 (declare (ignore position type true-value false-value))
1174 (if (and (consp type) (eq (car type) 'eql))
1175 `(class-eq ,(class-of (cadr type)))
1179 ;;; We know that known-type implies neither new-type nor `(not ,new-type).
1180 (defun augment-type (new-type known-type)
1181 (if (or (eq known-type t)
1182 (eq (car new-type) 'eql))
1184 (let ((so-far (if (and (consp known-type) (eq (car known-type) 'and))
1186 (list known-type))))
1187 (unless (eq (car new-type) 'not)
1189 (mapcan (lambda (type)
1190 (unless (*subtypep new-type type)
1195 `(and ,new-type ,@so-far)))))
1197 (defun generate-discrimination-net-internal
1198 (gf methods types methods-function test-fun type-function)
1199 (let* ((arg-info (gf-arg-info gf))
1200 (precedence (arg-info-precedence arg-info))
1201 (nreq (arg-info-number-required arg-info))
1202 (metatypes (arg-info-metatypes arg-info)))
1203 (labels ((do-column (p-tail contenders known-types)
1205 (let* ((position (car p-tail))
1206 (known-type (or (nth position types) t)))
1207 (if (eq (nth position metatypes) t)
1208 (do-column (cdr p-tail) contenders
1209 (cons (cons position known-type)
1211 (do-methods p-tail contenders
1212 known-type () known-types)))
1213 (funcall methods-function contenders
1214 (let ((k-t (make-list nreq)))
1215 (dolist (index+type known-types)
1216 (setf (nth (car index+type) k-t)
1219 (do-methods (p-tail contenders known-type winners known-types)
1221 ;; is a (sorted) list of methods that must be discriminated.
1223 ;; is the type of this argument, constructed from tests
1226 ;; is a (sorted) list of methods that are potentially
1227 ;; applicable after the discrimination has been made.
1228 (if (null contenders)
1229 (do-column (cdr p-tail)
1231 (cons (cons (car p-tail) known-type)
1233 (let* ((position (car p-tail))
1234 (method (car contenders))
1235 (specl (nth position (method-specializers method)))
1236 (type (funcall type-function
1237 (type-from-specializer specl))))
1238 (multiple-value-bind (app-p maybe-app-p)
1239 (specializer-applicable-using-type-p type known-type)
1240 (flet ((determined-to-be (truth-value)
1241 (if truth-value app-p (not maybe-app-p)))
1242 (do-if (truth &optional implied)
1243 (let ((ntype (if truth type `(not ,type))))
1248 (augment-type ntype known-type))
1250 (append winners `(,method))
1253 (cond ((determined-to-be nil) (do-if nil t))
1254 ((determined-to-be t) (do-if t t))
1255 (t (funcall test-fun position type
1256 (do-if t) (do-if nil))))))))))
1257 (do-column precedence methods ()))))
1259 (defun compute-secondary-dispatch-function (generic-function net &optional
1260 method-alist wrappers)
1261 (function-funcall (compute-secondary-dispatch-function1 generic-function net)
1262 method-alist wrappers))
1264 (defvar *eq-case-table-limit* 15)
1265 (defvar *case-table-limit* 10)
1267 (defun compute-mcase-parameters (case-list)
1268 (unless (eq t (caar (last case-list)))
1269 (error "The key for the last case arg to mcase was not T"))
1270 (let* ((eq-p (dolist (case case-list t)
1271 (unless (or (eq (car case) t)
1272 (symbolp (caar case)))
1274 (len (1- (length case-list)))
1275 (type (cond ((= len 1)
1279 *eq-case-table-limit*
1280 *case-table-limit*))
1286 (defmacro mlookup (key info default &optional eq-p type)
1287 (unless (or (eq eq-p t) (null eq-p))
1288 (bug "Invalid eq-p argument: ~S" eq-p))
1292 (declare (optimize (inhibit-warnings 3)))
1293 (,(if eq-p 'eq 'eql) ,key (car ,info)))
1297 `(dolist (e ,info ,default)
1299 (declare (optimize (inhibit-warnings 3)))
1300 (,(if eq-p 'eq 'eql) (car e) ,key))
1303 `(gethash ,key ,info ,default))))
1305 (defun net-test-converter (form)
1307 (default-test-converter form)
1309 ((invoke-effective-method-function invoke-fast-method-call)
1316 `(mlookup ,(cadr form)
1319 ,@(compute-mcase-parameters (cddr form))))
1320 (t (default-test-converter form)))))
1322 (defun net-code-converter (form)
1324 (default-code-converter form)
1326 ((methods unordered-methods)
1327 (let ((gensym (gensym)))
1331 (let ((mp (compute-mcase-parameters (cddr form)))
1332 (gensym (gensym)) (default (gensym)))
1333 (values `(mlookup ,(cadr form) ,gensym ,default ,@mp)
1334 (list gensym default))))
1336 (default-code-converter form)))))
1338 (defun net-constant-converter (form generic-function)
1339 (or (let ((c (methods-converter form generic-function)))
1342 (default-constant-converter form)
1345 (let* ((mp (compute-mcase-parameters (cddr form)))
1346 (list (mapcar (lambda (clause)
1347 (let ((key (car clause))
1348 (meth (cadr clause)))
1349 (cons (if (consp key) (car key) key)
1351 meth generic-function))))
1353 (default (car (last list))))
1354 (list (list* :mcase mp (nbutlast list))
1357 (default-constant-converter form))))))
1359 (defun methods-converter (form generic-function)
1360 (cond ((and (consp form) (eq (car form) 'methods))
1362 (get-effective-method-function1 generic-function (cadr form))))
1363 ((and (consp form) (eq (car form) 'unordered-methods))
1364 (default-secondary-dispatch-function generic-function))))
1366 (defun convert-methods (constant method-alist wrappers)
1367 (if (and (consp constant)
1368 (eq (car constant) '.methods.))
1369 (funcall (cdr constant) method-alist wrappers)
1372 (defun convert-table (constant method-alist wrappers)
1373 (cond ((and (consp constant)
1374 (eq (car constant) :mcase))
1375 (let ((alist (mapcar (lambda (k+m)
1377 (convert-methods (cdr k+m)
1381 (mp (cadr constant)))
1388 (let ((table (make-hash-table :test (if (car mp) 'eq 'eql))))
1390 (setf (gethash (car k+m) table) (cdr k+m)))
1393 (defun compute-secondary-dispatch-function1 (generic-function net
1394 &optional function-p)
1396 ((and (eq (car net) 'methods) (not function-p))
1397 (get-effective-method-function1 generic-function (cadr net)))
1399 (let* ((name (generic-function-name generic-function))
1400 (arg-info (gf-arg-info generic-function))
1401 (metatypes (arg-info-metatypes arg-info))
1402 (applyp (arg-info-applyp arg-info))
1403 (fmc-arg-info (cons (length metatypes) applyp))
1404 (arglist (if function-p
1405 (make-dfun-lambda-list metatypes applyp)
1406 (make-fast-method-call-lambda-list metatypes applyp))))
1407 (multiple-value-bind (cfunction constants)
1410 ,@(unless function-p
1411 `((declare (ignore .pv-cell.
1412 .next-method-call.))))
1413 (locally (declare #.*optimize-speed*)
1415 ,(make-emf-call metatypes applyp 'emf))))
1416 #'net-test-converter
1417 #'net-code-converter
1419 (net-constant-converter form generic-function)))
1420 (lambda (method-alist wrappers)
1421 (let* ((alist (list nil))
1423 (dolist (constant constants)
1424 (let* ((a (or (dolist (a alist nil)
1425 (when (eq (car a) constant)
1429 constant method-alist wrappers)
1431 constant method-alist wrappers)))))
1433 (setf (cdr alist-tail) new)
1434 (setf alist-tail new)))
1435 (let ((function (apply cfunction (mapcar #'cdr (cdr alist)))))
1438 (make-fast-method-call
1439 :function (set-fun-name function `(sdfun-method ,name))
1440 :arg-info fmc-arg-info))))))))))
1442 (defvar *show-make-unordered-methods-emf-calls* nil)
1444 (defun make-unordered-methods-emf (generic-function methods)
1445 (when *show-make-unordered-methods-emf-calls*
1446 (format t "~&make-unordered-methods-emf ~S~%"
1447 (generic-function-name generic-function)))
1448 (lambda (&rest args)
1449 (let* ((types (types-from-args generic-function args 'eql))
1450 (smethods (sort-applicable-methods generic-function
1453 (emf (get-effective-method-function generic-function smethods)))
1454 (invoke-emf emf args))))
1456 ;;; The value returned by compute-discriminating-function is a function
1457 ;;; object. It is called a discriminating function because it is called
1458 ;;; when the generic function is called and its role is to discriminate
1459 ;;; on the arguments to the generic function and then call appropriate
1460 ;;; method functions.
1462 ;;; A discriminating function can only be called when it is installed as
1463 ;;; the funcallable instance function of the generic function for which
1464 ;;; it was computed.
1466 ;;; More precisely, if compute-discriminating-function is called with
1467 ;;; an argument <gf1>, and returns a result <df1>, that result must
1468 ;;; not be passed to apply or funcall directly. Rather, <df1> must be
1469 ;;; stored as the funcallable instance function of the same generic
1470 ;;; function <gf1> (using SET-FUNCALLABLE-INSTANCE-FUNCTION). Then the
1471 ;;; generic function can be passed to funcall or apply.
1473 ;;; An important exception is that methods on this generic function are
1474 ;;; permitted to return a function which itself ends up calling the value
1475 ;;; returned by a more specific method. This kind of `encapsulation' of
1476 ;;; discriminating function is critical to many uses of the MOP.
1478 ;;; As an example, the following canonical case is legal:
1480 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1481 ;;; (let ((std (call-next-method)))
1483 ;;; (print (list 'call-to-gf gf arg))
1484 ;;; (funcall std arg))))
1486 ;;; Because many discriminating functions would like to use a dynamic
1487 ;;; strategy in which the precise discriminating function changes with
1488 ;;; time it is important to specify how a discriminating function is
1489 ;;; permitted itself to change the funcallable instance function of the
1490 ;;; generic function.
1492 ;;; Discriminating functions may set the funcallable instance function
1493 ;;; of the generic function, but the new value must be generated by making
1494 ;;; a call to COMPUTE-DISCRIMINATING-FUNCTION. This is to ensure that any
1495 ;;; more specific methods which may have encapsulated the discriminating
1496 ;;; function will get a chance to encapsulate the new, inner discriminating
1499 ;;; This implies that if a discriminating function wants to modify itself
1500 ;;; it should first store some information in the generic function proper,
1501 ;;; and then call compute-discriminating-function. The appropriate method
1502 ;;; on compute-discriminating-function will see the information stored in
1503 ;;; the generic function and generate a discriminating function accordingly.
1505 ;;; The following is an example of a discriminating function which modifies
1506 ;;; itself in accordance with this protocol:
1508 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1510 ;;; (cond (<some condition>
1511 ;;; <store some info in the generic function>
1512 ;;; (set-funcallable-instance-function
1514 ;;; (compute-discriminating-function gf))
1515 ;;; (funcall gf arg))
1517 ;;; <call-a-method-of-gf>))))
1519 ;;; Whereas this code would not be legal:
1521 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1523 ;;; (cond (<some condition>
1524 ;;; (set-funcallable-instance-function
1526 ;;; (lambda (a) ..))
1527 ;;; (funcall gf arg))
1529 ;;; <call-a-method-of-gf>))))
1531 ;;; NOTE: All the examples above assume that all instances of the class
1532 ;;; my-generic-function accept only one argument.
1534 (defun slot-value-using-class-dfun (class object slotd)
1535 (declare (ignore class))
1536 (function-funcall (slot-definition-reader-function slotd) object))
1538 (defun setf-slot-value-using-class-dfun (new-value class object slotd)
1539 (declare (ignore class))
1540 (function-funcall (slot-definition-writer-function slotd) new-value object))
1542 (defun slot-boundp-using-class-dfun (class object slotd)
1543 (declare (ignore class))
1544 (function-funcall (slot-definition-boundp-function slotd) object))
1546 (defun special-case-for-compute-discriminating-function-p (gf)
1547 (or (eq gf #'slot-value-using-class)
1548 (eq gf #'(setf slot-value-using-class))
1549 (eq gf #'slot-boundp-using-class)))
1551 (defmethod compute-discriminating-function ((gf standard-generic-function))
1552 (with-slots (dfun-state arg-info) gf
1553 (when (special-case-for-compute-discriminating-function-p gf)
1554 ;; if we have a special case for
1555 ;; COMPUTE-DISCRIMINATING-FUNCTION, then (at least for the
1556 ;; special cases implemented as of 2006-05-09) any information
1557 ;; in the cache is misplaced.
1558 (aver (null dfun-state)))
1559 (typecase dfun-state
1561 (when (eq gf #'compute-applicable-methods)
1562 (update-all-c-a-m-gf-info gf))
1564 ((eq gf #'slot-value-using-class)
1565 (update-slot-value-gf-info gf 'reader)
1566 #'slot-value-using-class-dfun)
1567 ((eq gf #'(setf slot-value-using-class))
1568 (update-slot-value-gf-info gf 'writer)
1569 #'setf-slot-value-using-class-dfun)
1570 ((eq gf #'slot-boundp-using-class)
1571 (update-slot-value-gf-info gf 'boundp)
1572 #'slot-boundp-using-class-dfun)
1573 ((gf-precompute-dfun-and-emf-p arg-info)
1574 (make-final-dfun gf))
1576 (make-initial-dfun gf))))
1577 (function dfun-state)
1578 (cons (car dfun-state)))))
1580 (defmethod update-gf-dfun ((class std-class) gf)
1581 (let ((*new-class* class)
1582 (arg-info (gf-arg-info gf)))
1584 ((special-case-for-compute-discriminating-function-p gf))
1585 ((gf-precompute-dfun-and-emf-p arg-info)
1586 (multiple-value-bind (dfun cache info)
1587 (make-final-dfun-internal gf)
1588 (set-dfun gf dfun cache info) ; lest the cache be freed twice
1589 (update-dfun gf dfun cache info))))))
1591 (defmethod (setf class-name) (new-value class)
1592 (let ((classoid (%wrapper-classoid (class-wrapper class))))
1593 (setf (classoid-name classoid) new-value))
1594 (reinitialize-instance class :name new-value)
1597 (defmethod (setf generic-function-name) (new-value generic-function)
1598 (reinitialize-instance generic-function :name new-value)
1601 (defmethod function-keywords ((method standard-method))
1602 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1603 (analyze-lambda-list (if (consp method)
1604 (early-method-lambda-list method)
1605 (method-lambda-list method)))
1606 (declare (ignore nreq nopt keysp restp))
1607 (values keywords allow-other-keys-p)))
1609 (defun method-ll->generic-function-ll (ll)
1610 (multiple-value-bind
1611 (nreq nopt keysp restp allow-other-keys-p keywords keyword-parameters)
1612 (analyze-lambda-list ll)
1613 (declare (ignore nreq nopt keysp restp allow-other-keys-p keywords))
1614 (remove-if (lambda (s)
1615 (or (memq s keyword-parameters)
1616 (eq s '&allow-other-keys)))
1619 ;;; This is based on the rules of method lambda list congruency defined in
1620 ;;; the spec. The lambda list it constructs is the pretty union of the
1621 ;;; lambda lists of all the methods. It doesn't take method applicability
1622 ;;; into account at all yet.
1623 (defmethod generic-function-pretty-arglist
1624 ((generic-function standard-generic-function))
1625 (let ((methods (generic-function-methods generic-function)))
1628 ;; arglist is constructed from the GF's methods - maybe with
1629 ;; keys and rest stuff added
1630 (multiple-value-bind (required optional rest key allow-other-keys)
1631 (method-pretty-arglist (car methods))
1632 (dolist (m (cdr methods))
1633 (multiple-value-bind (method-key-keywords
1634 method-allow-other-keys
1636 (function-keywords m)
1637 ;; we've modified function-keywords to return what we want as
1638 ;; the third value, no other change here.
1639 (declare (ignore method-key-keywords))
1640 (setq key (union key method-key))
1641 (setq allow-other-keys (or allow-other-keys
1642 method-allow-other-keys))))
1643 (when allow-other-keys
1644 (setq arglist '(&allow-other-keys)))
1646 (setq arglist (nconc (list '&key) key arglist)))
1648 (setq arglist (nconc (list '&rest rest) arglist)))
1650 (setq arglist (nconc (list '&optional) optional arglist)))
1651 (nconc required arglist)))
1652 ;; otherwise we take the lambda-list from the GF directly, with no
1653 ;; other 'keys' added ...
1654 (let ((lambda-list (generic-function-lambda-list generic-function)))
1657 (defmethod method-pretty-arglist ((method standard-method))
1662 (allow-other-keys nil)
1664 (arglist (method-lambda-list method)))
1665 (dolist (arg arglist)
1666 (cond ((eq arg '&optional) (setq state 'optional))
1667 ((eq arg '&rest) (setq state 'rest))
1668 ((eq arg '&key) (setq state 'key))
1669 ((eq arg '&allow-other-keys) (setq allow-other-keys t))
1670 ((memq arg lambda-list-keywords))
1673 (required (push arg required))
1674 (optional (push arg optional))
1675 (key (push arg key))
1676 (rest (setq rest arg))))))
1677 (values (nreverse required)