0.8.0.75:
[sbcl.git] / src / pcl / defcombin.lisp
index 342f98f..897b644 100644 (file)
 \f
 ;;;; standard method combination
 
-;;; The STANDARD method combination type is implemented directly by the class
-;;; STANDARD-METHOD-COMBINATION. The method on COMPUTE-EFFECTIVE-METHOD does
-;;; standard method combination directly and is defined by hand in the file
-;;; combin.lisp. The method for FIND-METHOD-COMBINATION must appear in this
-;;; file for bootstrapping reasons.
-;;;
-;;; A commented out copy of this definition appears in combin.lisp.
-;;; If you change this definition here, be sure to change it there
-;;; also.
+;;; The STANDARD method combination type is implemented directly by
+;;; the class STANDARD-METHOD-COMBINATION. The method on
+;;; COMPUTE-EFFECTIVE-METHOD does standard method combination directly
+;;; and is defined by hand in the file combin.lisp. The method for
+;;; FIND-METHOD-COMBINATION must appear in this file for bootstrapping
+;;; reasons.
 (defmethod find-method-combination ((generic-function generic-function)
                                    (type (eql 'standard))
                                    options)
 ;;;; and runs the same rule.
 
 (defclass short-method-combination (standard-method-combination)
-     ((operator
-       :reader short-combination-operator
-       :initarg :operator)
-      (identity-with-one-argument
-       :reader short-combination-identity-with-one-argument
-       :initarg :identity-with-one-argument))
+  ((operator
+    :reader short-combination-operator
+    :initarg :operator)
+   (identity-with-one-argument
+    :reader short-combination-identity-with-one-argument
+    :initarg :identity-with-one-argument))
   (:predicate-name short-method-combination-p))
 
 (defun expand-short-defcombin (whole)
           (getf (cddr whole) :identity-with-one-argument nil))
         (operator
           (getf (cddr whole) :operator type)))
-    (make-top-level-form `(define-method-combination ,type)
-                        '(:load-toplevel :execute)
-      `(load-short-defcombin
-        ',type ',operator ',identity-with-one-arg ',documentation))))
+    `(load-short-defcombin
+     ',type ',operator ',identity-with-one-arg ',documentation)))
 
 (defun load-short-defcombin (type operator ioa doc)
-  (let* ((truename *load-truename*)
+  (let* ((pathname *load-pathname*)
         (specializers
           (list (find-class 'generic-function)
                 (intern-eql-specializer type)
            :qualifiers ()
            :specializers specializers
            :lambda-list '(generic-function type options)
-           :function #'(lambda(args nms &rest cm-args)
-                         (declare (ignore nms cm-args))
-                         (apply
-                          #'(lambda (gf type options)
-                              (declare (ignore gf))
-                              (do-short-method-combination
-                               type options operator ioa new-method doc))
-                          args))
-           :definition-source `((define-method-combination ,type) ,truename)))
+           :function (lambda (args nms &rest cm-args)
+                       (declare (ignore nms cm-args))
+                       (apply
+                        (lambda (gf type options)
+                          (declare (ignore gf))
+                          (short-combine-methods
+                           type options operator ioa new-method doc))
+                        args))
+           :definition-source `((define-method-combination ,type) ,pathname)))
     (when old-method
       (remove-method #'find-method-combination old-method))
-    (add-method #'find-method-combination new-method)))
+    (add-method #'find-method-combination new-method)
+    type))
 
-(defun do-short-method-combination (type options operator ioa method doc)
+(defun short-combine-methods (type options operator ioa method doc)
   (cond ((null options) (setq options '(:most-specific-first)))
        ((equal options '(:most-specific-first)))
        ((equal options '(:most-specific-last)))
        (t
         (method-combination-error
-          "Illegal options to a short method combination type.~%~
-           The method combination type ~S accepts one option which~%~
-           must be either :MOST-SPECIFIC-FIRST or :MOST-SPECIFIC-LAST."
-          type)))
+         "Illegal options to a short method combination type.~%~
+          The method combination type ~S accepts one option which~%~
+          must be either :MOST-SPECIFIC-FIRST or :MOST-SPECIFIC-LAST."
+         type)))
   (make-instance 'short-method-combination
                 :type type
                 :options options
   (let ((type (method-combination-type combin))
        (operator (short-combination-operator combin))
        (ioa (short-combination-identity-with-one-argument combin))
+       (order (car (method-combination-options combin)))
        (around ())
        (primary ()))
     (dolist (m applicable-methods)
                 (push m primary))
                (t
                 (lose m "has an illegal qualifier"))))))
-    (setq around (nreverse around)
-         primary (nreverse primary))
+    (setq around (nreverse around))
+    (ecase order
+      (:most-specific-last) ; nothing to be done, already in correct order
+      (:most-specific-first
+       (setq primary (nreverse primary))))
     (let ((main-method
            (if (and (null (cdr primary))
                     (not (null ioa)))
                `(call-method ,(car primary) ())
-               `(,operator ,@(mapcar #'(lambda (m) `(call-method ,m ()))
+               `(,operator ,@(mapcar (lambda (m) `(call-method ,m ()))
                                      primary)))))
       (cond ((null primary)
-            `(error "No ~S methods for the generic function ~S."
-                    ',type ',generic-function))
+            ;; FIXME(?): NO-APPLICABLE-METHOD seems more appropriate
+            ;; here, but
+            ;;   (1) discussion with CSR on #lisp reminded me that it's
+            ;;       a vexed question whether we can validly call
+            ;;       N-A-M when an :AROUND method exists (and the
+            ;;       definition of NO-NEXT-METHOD seems to discourage
+            ;;       us from calling NO-NEXT-METHOD directly in that
+            ;;       case, since it's supposed to be called from a
+            ;;       CALL-NEXT-METHOD form), and
+            ;;   (2) a call to N-A-M would require &REST FUN-ARGS, and
+            ;;       we don't seem to have FUN-ARGS here.
+            ;; I think ideally failures in short method combination
+            ;; would end up either in NO-APPLICABLE-METHOD or
+            ;; NO-NEXT-METHOD, and I expect that's what ANSI
+            ;; generally intended, but it's not clear to me whether
+            ;; the details of what they actually specified let us
+            ;; make that happen. So for now I've just tried to
+            ;; clarify the error message text but left the general
+            ;; logic alone (and raised the question on sbcl-devel).
+            ;; -- WHN 2003-06-16
+            `(error "no ~S methods for ~S on these arguments"
+                    ',type
+                    ',generic-function))
            ((null around) main-method)
            (t
             `(call-method ,(car around)
 \f
 ;;;; long method combinations
 
-(defclass long-method-combination (standard-method-combination)
-     ((function :initarg :function
-               :reader long-method-combination-function)))
-
 (defun expand-long-defcombin (form)
   (let ((type (cadr form))
        (lambda-list (caddr form))
        (method-group-specifiers (cadddr form))
        (body (cddddr form))
-       (arguments-option ())
+       (args-option ())
        (gf-var nil))
     (when (and (consp (car body)) (eq (caar body) :arguments))
-      (setq arguments-option (cdr (pop body))))
+      (setq args-option (cdr (pop body))))
     (when (and (consp (car body)) (eq (caar body) :generic-function))
       (setq gf-var (cadr (pop body))))
     (multiple-value-bind (documentation function)
        (make-long-method-combination-function
-         type lambda-list method-group-specifiers arguments-option gf-var
+         type lambda-list method-group-specifiers args-option gf-var
          body)
-      (make-top-level-form `(define-method-combination ,type)
-                          '(:load-toplevel :execute)
-       `(load-long-defcombin ',type ',documentation #',function)))))
+      `(load-long-defcombin ',type ',documentation #',function
+                           ',args-option))))
 
 (defvar *long-method-combination-functions* (make-hash-table :test 'eq))
 
-(defun load-long-defcombin (type doc function)
+(defun load-long-defcombin (type doc function args-lambda-list)
   (let* ((specializers
           (list (find-class 'generic-function)
                 (intern-eql-specializer type)
             :qualifiers ()
             :specializers specializers
             :lambda-list '(generic-function type options)
-            :function #'(lambda (args nms &rest cm-args)
-                          (declare (ignore nms cm-args))
-                          (apply
-                           #'(lambda (generic-function type options)
-                               (declare (ignore generic-function options))
-                               (make-instance 'long-method-combination
-                                              :type type
-                                              :documentation doc))
-                           args))
-        :definition-source `((define-method-combination ,type)
-                             ,*load-truename*))))
+            :function (lambda (args nms &rest cm-args)
+                        (declare (ignore nms cm-args))
+                        (apply
+                         (lambda (generic-function type options)
+                           (declare (ignore generic-function))
+                           (make-instance 'long-method-combination
+                                          :type type
+                                          :options options
+                                          :args-lambda-list args-lambda-list
+                                          :documentation doc))
+                         args))
+            :definition-source `((define-method-combination ,type)
+                                 ,*load-pathname*))))
     (setf (gethash type *long-method-combination-functions*) function)
     (when old-method (remove-method #'find-method-combination old-method))
-    (add-method #'find-method-combination new-method)))
+    (add-method #'find-method-combination new-method)
+    type))
 
 (defmethod compute-effective-method ((generic-function generic-function)
                                     (combin long-method-combination)
           applicable-methods))
 
 (defun make-long-method-combination-function
-       (type ll method-group-specifiers arguments-option gf-var body)
-  ;;(declare (values documentation function))
+       (type ll method-group-specifiers args-option gf-var body)
   (declare (ignore type))
-  (multiple-value-bind (documentation declarations real-body)
-      (extract-declarations body)
-
+  (multiple-value-bind (real-body declarations documentation)
+      (parse-body body)
     (let ((wrapped-body
            (wrap-method-group-specifier-bindings method-group-specifiers
                                                  declarations
       (when gf-var
        (push `(,gf-var .generic-function.) (cadr wrapped-body)))
 
-      (when arguments-option
-       (setq wrapped-body (deal-with-arguments-option wrapped-body
-                                                      arguments-option)))
+      (when args-option
+       (setq wrapped-body (deal-with-args-option wrapped-body args-option)))
 
       (when ll
        (setq wrapped-body
       (values
        documentation
        `(lambda (.generic-function. .method-combination. .applicable-methods.)
-          (progn .generic-function. .method-combination. .applicable-methods.)
+          (declare (ignorable .generic-function.
+                    .method-combination. .applicable-methods.))
           (block .long-method-combination-function. ,wrapped-body))))))
 
 ;; parse-method-group-specifiers parse the method-group-specifiers
 
 (defun wrap-method-group-specifier-bindings
        (method-group-specifiers declarations real-body)
-  (with-gathering ((names (collecting))
-                  (specializer-caches (collecting))
-                  (cond-clauses (collecting))
-                  (required-checks (collecting))
-                  (order-cleanups (collecting)))
+  (let (names
+        specializer-caches
+        cond-clauses
+        required-checks
+        order-cleanups)
       (dolist (method-group-specifier method-group-specifiers)
        (multiple-value-bind (name tests description order required)
            (parse-method-group-specifier method-group-specifier)
          (declare (ignore description))
          (let ((specializer-cache (gensym)))
-           (gather name names)
-           (gather specializer-cache specializer-caches)
-           (gather `((or ,@tests)
-                     (if  (equal ,specializer-cache .specializers.)
-                          (return-from .long-method-combination-function.
-                            '(error "More than one method of type ~S ~
+           (push name names)
+           (push specializer-cache specializer-caches)
+           (push `((or ,@tests)
+                   (if (and (equal ,specializer-cache .specializers.)
+                            (not (null .specializers.)))
+                       (return-from .long-method-combination-function.
+                         '(error "More than one method of type ~S ~
                                      with the same specializers."
-                                    ',name))
-                          (setq ,specializer-cache .specializers.))
-                     (push .method. ,name))
-                   cond-clauses)
+                                  ',name))
+                       (setq ,specializer-cache .specializers.))
+                   (push .method. ,name))
+                 cond-clauses)
            (when required
-             (gather `(when (null ,name)
+             (push `(when (null ,name)
                         (return-from .long-method-combination-function.
                           '(error "No ~S methods." ',name)))
                      required-checks))
            (loop (unless (and (constantp order)
                               (neq order (setq order (eval order))))
                    (return t)))
-           (gather (cond ((eq order :most-specific-first)
+           (push (cond ((eq order :most-specific-first)
                           `(setq ,name (nreverse ,name)))
                          ((eq order :most-specific-last) ())
                          (t
                                (setq ,name (nreverse ,name)))
                              (:most-specific-last))))
                    order-cleanups))))
-   `(let (,@names ,@specializer-caches)
+   `(let (,@(nreverse names) ,@(nreverse specializer-caches))
       ,@declarations
       (dolist (.method. .applicable-methods.)
        (let ((.qualifiers. (method-qualifiers .method.))
              (.specializers. (method-specializers .method.)))
-         (progn .qualifiers. .specializers.)
-         (cond ,@cond-clauses)))
-      ,@required-checks
-      ,@order-cleanups
+         (declare (ignorable .qualifiers. .specializers.))
+         (cond ,@(nreverse cond-clauses))))
+      ,@(nreverse required-checks)
+      ,@(nreverse order-cleanups)
       ,@real-body)))
 
 (defun parse-method-group-specifier (method-group-specifier)
   (let* ((name (pop method-group-specifier))
         (patterns ())
         (tests
-          (gathering1 (collecting)
+          (let (collect)
             (block collect-tests
               (loop
                 (if (or (null method-group-specifier)
                     (return-from collect-tests t)
                     (let ((pattern (pop method-group-specifier)))
                       (push pattern patterns)
-                      (gather1 (parse-qualifier-pattern name pattern)))))))))
+                      (push (parse-qualifier-pattern name pattern)
+                             collect)))))
+             (nreverse collect))))
     (values name
            tests
            (getf method-group-specifier :description
 
 (defun parse-qualifier-pattern (name pattern)
   (cond ((eq pattern '()) `(null .qualifiers.))
-       ((eq pattern '*) 't)
+       ((eq pattern '*) t)
        ((symbolp pattern) `(,pattern .qualifiers.))
        ((listp pattern) `(qualifier-check-runtime ',pattern .qualifiers.))
        (t (error "In the method group specifier ~S,~%~
 ;;;
 ;;; At compute-effective-method time, the symbols in the :arguments
 ;;; option are bound to the symbols in the intercept lambda list.
-(defun deal-with-arguments-option (wrapped-body arguments-option)
-  (let* ((intercept-lambda-list
-          (gathering1 (collecting)
-            (dolist (arg arguments-option)
-              (if (memq arg lambda-list-keywords)
-                  (gather1 arg)
-                  (gather1 (gensym))))))
-        (intercept-rebindings
-          (gathering1 (collecting)
-            (iterate ((arg (list-elements arguments-option))
-                      (int (list-elements intercept-lambda-list)))
-              (unless (memq arg lambda-list-keywords)
-                (gather1 `(,arg ',int)))))))
-
-    (setf (cadr wrapped-body)
-         (append intercept-rebindings (cadr wrapped-body)))
-
-    ;; Be sure to fill out the intercept lambda list so that it can
-    ;; be too short if it wants to.
-    (cond ((memq '&rest intercept-lambda-list))
-         ((memq '&allow-other-keys intercept-lambda-list))
-         ((memq '&key intercept-lambda-list)
-          (setq intercept-lambda-list
-                (append intercept-lambda-list '(&allow-other-keys))))
-         (t
-          (setq intercept-lambda-list
-                (append intercept-lambda-list '(&rest .ignore.)))))
+(defun deal-with-args-option (wrapped-body args-lambda-list)
+  (let ((intercept-rebindings
+        (let (rebindings)
+          (dolist (arg args-lambda-list (nreverse rebindings))
+            (unless (member arg lambda-list-keywords)
+              (push `(,arg ',arg) rebindings)))))
+       (nreq 0)
+       (nopt 0)
+       (whole nil))
+    ;; Count the number of required and optional parameters in
+    ;; ARGS-LAMBDA-LIST into NREQ and NOPT, and set WHOLE to the
+    ;; name of a &WHOLE parameter, if any.
+    (when (member '&whole (rest args-lambda-list))
+      (error 'simple-program-error
+            :format-control "~@<The value of the :ARGUMENTS option of ~
+                DEFINE-METHOD-COMBINATION is~2I~_~S,~I~_but &WHOLE may ~
+                only appear first in the lambda list.~:>"
+            :format-arguments (list args-lambda-list)))
+    (loop with state = 'required
+         for arg in args-lambda-list do
+           (if (memq arg lambda-list-keywords)
+               (setq state arg)
+               (case state
+                 (required (incf nreq))
+                 (&optional (incf nopt))
+                 (&whole (setq whole arg state 'required)))))
+    ;; This assumes that the head of WRAPPED-BODY is a let, and it
+    ;; injects let-bindings of the form (ARG 'SYM) for all variables
+    ;; of the argument-lambda-list; SYM is a gensym.
+    (aver (memq (first wrapped-body) '(let let*)))
+    (setf (second wrapped-body)
+         (append intercept-rebindings (second wrapped-body)))
+    ;; Be sure to fill out the args lambda list so that it can be too
+    ;; short if it wants to.
+    (unless (or (memq '&rest args-lambda-list)
+               (memq '&allow-other-keys args-lambda-list))
+      (let ((aux (memq '&aux args-lambda-list)))
+       (setq args-lambda-list
+             (append (ldiff args-lambda-list aux)
+                     (if (memq '&key args-lambda-list)
+                         '(&allow-other-keys)
+                         '(&rest .ignore.))
+                     aux))))
+    ;; .GENERIC-FUNCTION. is bound to the generic function in the
+    ;; method combination function, and .GF-ARGS* is bound to the
+    ;; generic function arguments in effective method functions
+    ;; created for generic functions having a method combination that
+    ;; uses :ARGUMENTS.
+    ;;
+    ;; The DESTRUCTURING-BIND binds the parameters of the
+    ;; ARGS-LAMBDA-LIST to actual generic function arguments.  Because
+    ;; ARGS-LAMBDA-LIST may be shorter or longer than the generic
+    ;; function's lambda list, which is only known at run time, this
+    ;; destructuring has to be done on a slighly modified list of
+    ;; actual arguments, from which values might be stripped or added.
+    ;;
+    ;; Using one of the variable names in the body inserts a symbol
+    ;; into the effective method, and running the effective method
+    ;; produces the value of actual argument that is bound to the
+    ;; symbol.
+    `(let ((inner-result. ,wrapped-body)
+          (gf-lambda-list (generic-function-lambda-list .generic-function.)))
+       `(destructuring-bind ,',args-lambda-list
+           (frob-combined-method-args
+            .gf-args. ',gf-lambda-list
+            ,',nreq ,',nopt)
+         ,,(when (memq '.ignore. args-lambda-list)
+             ''(declare (ignore .ignore.)))
+         ;; If there is a &WHOLE in the args-lambda-list, let
+         ;; it result in the actual arguments of the generic-function
+         ;; not the frobbed list.
+         ,,(when whole
+             ``(setq ,',whole .gf-args.))
+         ,inner-result.))))
 
-    `(let ((inner-result. ,wrapped-body))
-       `(apply #'(lambda ,',intercept-lambda-list
-                  ,,(when (memq '.ignore. intercept-lambda-list)
-                      ''(declare (ignore .ignore.)))
-                  ,inner-result.)
-              .combined-method-args.))))
+;;; Partition VALUES into three sections: required, optional, and the
+;;; rest, according to required, optional, and other parameters in
+;;; LAMBDA-LIST.  Make the required and optional sections NREQ and
+;;; NOPT elements long by discarding values or adding NILs.  Value is
+;;; the concatenated list of required and optional sections, and what
+;;; is left as rest from VALUES.
+(defun frob-combined-method-args (values lambda-list nreq nopt)
+  (loop with section = 'required
+       for arg in lambda-list
+       if (memq arg lambda-list-keywords) do
+         (setq section arg)
+         (unless (eq section '&optional)
+           (loop-finish))
+       else if (eq section 'required)
+         count t into nr
+         and collect (pop values) into required
+       else if (eq section '&optional)
+         count t into no
+         and collect (pop values) into optional
+       finally
+         (flet ((frob (list n m)
+                  (cond ((> n m) (butlast list (- n m)))
+                        ((< n m) (nconc list (make-list (- m n))))
+                        (t list))))
+           (return (nconc (frob required nr nreq)
+                          (frob optional no nopt)
+                          values)))))