dacd5209aa812624c941f4e55ae3ced218bf5247
[sbcl.git] / src / pcl / defcombin.lisp
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 \f
26 (defmacro define-method-combination (&whole form &rest args)
27   (declare (ignore args))
28   (if (and (cddr form)
29            (listp (caddr form)))
30       (expand-long-defcombin form)
31       (expand-short-defcombin form)))
32 \f
33 ;;;; standard method combination
34
35 ;;; The STANDARD method combination type is implemented directly by
36 ;;; the class STANDARD-METHOD-COMBINATION. The method on
37 ;;; COMPUTE-EFFECTIVE-METHOD does standard method combination directly
38 ;;; and is defined by hand in the file combin.lisp. The method for
39 ;;; FIND-METHOD-COMBINATION must appear in this file for bootstrapping
40 ;;; reasons.
41 (defmethod find-method-combination ((generic-function generic-function)
42                                     (type (eql 'standard))
43                                     options)
44   (when options
45     (method-combination-error
46       "The method combination type STANDARD accepts no options."))
47   *standard-method-combination*)
48 \f
49 ;;;; short method combinations
50 ;;;;
51 ;;;; Short method combinations all follow the same rule for computing the
52 ;;;; effective method. So, we just implement that rule once. Each short
53 ;;;; method combination object just reads the parameters out of the object
54 ;;;; and runs the same rule.
55
56 (defclass short-method-combination (standard-method-combination)
57      ((operator
58         :reader short-combination-operator
59         :initarg :operator)
60       (identity-with-one-argument
61         :reader short-combination-identity-with-one-argument
62         :initarg :identity-with-one-argument))
63   (:predicate-name short-method-combination-p))
64
65 (defun expand-short-defcombin (whole)
66   (let* ((type (cadr whole))
67          (documentation
68            (getf (cddr whole) :documentation ""))
69          (identity-with-one-arg
70            (getf (cddr whole) :identity-with-one-argument nil))
71          (operator
72            (getf (cddr whole) :operator type)))
73     `(load-short-defcombin
74      ',type ',operator ',identity-with-one-arg ',documentation)))
75
76 (defun load-short-defcombin (type operator ioa doc)
77   (let* ((truename *load-truename*)
78          (specializers
79            (list (find-class 'generic-function)
80                  (intern-eql-specializer type)
81                  *the-class-t*))
82          (old-method
83            (get-method #'find-method-combination () specializers nil))
84          (new-method nil))
85     (setq new-method
86           (make-instance 'standard-method
87             :qualifiers ()
88             :specializers specializers
89             :lambda-list '(generic-function type options)
90             :function (lambda (args nms &rest cm-args)
91                         (declare (ignore nms cm-args))
92                         (apply
93                          (lambda (gf type options)
94                            (declare (ignore gf))
95                            (short-combine-methods
96                             type options operator ioa new-method doc))
97                          args))
98             :definition-source `((define-method-combination ,type) ,truename)))
99     (when old-method
100       (remove-method #'find-method-combination old-method))
101     (add-method #'find-method-combination new-method)))
102
103 (defun short-combine-methods (type options operator ioa method doc)
104   (cond ((null options) (setq options '(:most-specific-first)))
105         ((equal options '(:most-specific-first)))
106         ((equal options '(:most-specific-last)))
107         (t
108          (method-combination-error
109           "Illegal options to a short method combination type.~%~
110            The method combination type ~S accepts one option which~%~
111            must be either :MOST-SPECIFIC-FIRST or :MOST-SPECIFIC-LAST."
112           type)))
113   (make-instance 'short-method-combination
114                  :type type
115                  :options options
116                  :operator operator
117                  :identity-with-one-argument ioa
118                  :definition-source method
119                  :documentation doc))
120
121 (defmethod compute-effective-method ((generic-function generic-function)
122                                      (combin short-method-combination)
123                                      applicable-methods)
124   (let ((type (method-combination-type combin))
125         (operator (short-combination-operator combin))
126         (ioa (short-combination-identity-with-one-argument combin))
127         (order (car (method-combination-options combin)))
128         (around ())
129         (primary ()))
130     (dolist (m applicable-methods)
131       (let ((qualifiers (method-qualifiers m)))
132         (flet ((lose (method why)
133                  (invalid-method-error
134                    method
135                    "The method ~S ~A.~%~
136                     The method combination type ~S was defined with the~%~
137                     short form of DEFINE-METHOD-COMBINATION and so requires~%~
138                     all methods have either the single qualifier ~S or the~%~
139                     single qualifier :AROUND."
140                    method why type type)))
141           (cond ((null qualifiers)
142                  (lose m "has no qualifiers"))
143                 ((cdr qualifiers)
144                  (lose m "has more than one qualifier"))
145                 ((eq (car qualifiers) :around)
146                  (push m around))
147                 ((eq (car qualifiers) type)
148                  (push m primary))
149                 (t
150                  (lose m "has an illegal qualifier"))))))
151     (setq around (nreverse around))
152     (ecase order
153       (:most-specific-last) ; nothing to be done, already in correct order
154       (:most-specific-first
155        (setq primary (nreverse primary))))
156     (let ((main-method
157             (if (and (null (cdr primary))
158                      (not (null ioa)))
159                 `(call-method ,(car primary) ())
160                 `(,operator ,@(mapcar (lambda (m) `(call-method ,m ()))
161                                       primary)))))
162       (cond ((null primary)
163              `(error "No ~S methods for the generic function ~S."
164                      ',type ',generic-function))
165             ((null around) main-method)
166             (t
167              `(call-method ,(car around)
168                            (,@(cdr around) (make-method ,main-method))))))))
169 \f
170 ;;;; long method combinations
171
172 (defclass long-method-combination (standard-method-combination)
173      ((function :initarg :function
174                 :reader long-method-combination-function)))
175
176 (defun expand-long-defcombin (form)
177   (let ((type (cadr form))
178         (lambda-list (caddr form))
179         (method-group-specifiers (cadddr form))
180         (body (cddddr form))
181         (args-option ())
182         (gf-var nil))
183     (when (and (consp (car body)) (eq (caar body) :arguments))
184       (setq args-option (cdr (pop body))))
185     (when (and (consp (car body)) (eq (caar body) :generic-function))
186       (setq gf-var (cadr (pop body))))
187     (multiple-value-bind (documentation function)
188         (make-long-method-combination-function
189           type lambda-list method-group-specifiers args-option gf-var
190           body)
191       `(load-long-defcombin ',type ',documentation #',function))))
192
193 (defvar *long-method-combination-functions* (make-hash-table :test 'eq))
194
195 (defun load-long-defcombin (type doc function)
196   (let* ((specializers
197            (list (find-class 'generic-function)
198                  (intern-eql-specializer type)
199                  *the-class-t*))
200          (old-method
201            (get-method #'find-method-combination () specializers nil))
202          (new-method
203            (make-instance 'standard-method
204              :qualifiers ()
205              :specializers specializers
206              :lambda-list '(generic-function type options)
207              :function (lambda (args nms &rest cm-args)
208                          (declare (ignore nms cm-args))
209                          (apply
210                           (lambda (generic-function type options)
211                             (declare (ignore generic-function options))
212                             (make-instance 'long-method-combination
213                                            :type type
214                                            :documentation doc))
215                           args))
216              :definition-source `((define-method-combination ,type)
217                               ,*load-truename*))))
218     (setf (gethash type *long-method-combination-functions*) function)
219     (when old-method (remove-method #'find-method-combination old-method))
220     (add-method #'find-method-combination new-method)))
221
222 (defmethod compute-effective-method ((generic-function generic-function)
223                                      (combin long-method-combination)
224                                      applicable-methods)
225   (funcall (gethash (method-combination-type combin)
226                     *long-method-combination-functions*)
227            generic-function
228            combin
229            applicable-methods))
230
231 (defun make-long-method-combination-function
232        (type ll method-group-specifiers args-option gf-var body)
233   ;;(declare (values documentation function))
234   (declare (ignore type))
235   (multiple-value-bind (documentation declarations real-body)
236       (extract-declarations body)
237
238     (let ((wrapped-body
239             (wrap-method-group-specifier-bindings method-group-specifiers
240                                                   declarations
241                                                   real-body)))
242       (when gf-var
243         (push `(,gf-var .generic-function.) (cadr wrapped-body)))
244
245       (when args-option
246         (setq wrapped-body (deal-with-args-option wrapped-body args-option)))
247
248       (when ll
249         (setq wrapped-body
250               `(apply #'(lambda ,ll ,wrapped-body)
251                       (method-combination-options .method-combination.))))
252
253       (values
254         documentation
255         `(lambda (.generic-function. .method-combination. .applicable-methods.)
256            (progn .generic-function. .method-combination. .applicable-methods.)
257            (block .long-method-combination-function. ,wrapped-body))))))
258
259 ;; parse-method-group-specifiers parse the method-group-specifiers
260
261 (defun wrap-method-group-specifier-bindings
262        (method-group-specifiers declarations real-body)
263   (let (names
264         specializer-caches
265         cond-clauses
266         required-checks
267         order-cleanups)
268       (dolist (method-group-specifier method-group-specifiers)
269         (multiple-value-bind (name tests description order required)
270             (parse-method-group-specifier method-group-specifier)
271           (declare (ignore description))
272           (let ((specializer-cache (gensym)))
273             (push name names)
274             (push specializer-cache specializer-caches)
275             (push `((or ,@tests)
276                       (if  (equal ,specializer-cache .specializers.)
277                            (return-from .long-method-combination-function.
278                              '(error "More than one method of type ~S ~
279                                       with the same specializers."
280                                      ',name))
281                            (setq ,specializer-cache .specializers.))
282                       (push .method. ,name))
283                     cond-clauses)
284             (when required
285               (push `(when (null ,name)
286                          (return-from .long-method-combination-function.
287                            '(error "No ~S methods." ',name)))
288                       required-checks))
289             (loop (unless (and (constantp order)
290                                (neq order (setq order (eval order))))
291                     (return t)))
292             (push (cond ((eq order :most-specific-first)
293                            `(setq ,name (nreverse ,name)))
294                           ((eq order :most-specific-last) ())
295                           (t
296                            `(ecase ,order
297                               (:most-specific-first
298                                 (setq ,name (nreverse ,name)))
299                               (:most-specific-last))))
300                     order-cleanups))))
301    `(let (,@(nreverse names) ,@(nreverse specializer-caches))
302       ,@declarations
303       (dolist (.method. .applicable-methods.)
304         (let ((.qualifiers. (method-qualifiers .method.))
305               (.specializers. (method-specializers .method.)))
306           (progn .qualifiers. .specializers.)
307           (cond ,@(nreverse cond-clauses))))
308       ,@(nreverse required-checks)
309       ,@(nreverse order-cleanups)
310       ,@real-body)))
311
312 (defun parse-method-group-specifier (method-group-specifier)
313   ;;(declare (values name tests description order required))
314   (let* ((name (pop method-group-specifier))
315          (patterns ())
316          (tests
317            (let (collect)
318              (block collect-tests
319                (loop
320                  (if (or (null method-group-specifier)
321                          (memq (car method-group-specifier)
322                                '(:description :order :required)))
323                      (return-from collect-tests t)
324                      (let ((pattern (pop method-group-specifier)))
325                        (push pattern patterns)
326                        (push (parse-qualifier-pattern name pattern)
327                              collect)))))
328              (nreverse collect))))
329     (values name
330             tests
331             (getf method-group-specifier :description
332                   (make-default-method-group-description patterns))
333             (getf method-group-specifier :order :most-specific-first)
334             (getf method-group-specifier :required nil))))
335
336 (defun parse-qualifier-pattern (name pattern)
337   (cond ((eq pattern '()) `(null .qualifiers.))
338         ((eq pattern '*) t)
339         ((symbolp pattern) `(,pattern .qualifiers.))
340         ((listp pattern) `(qualifier-check-runtime ',pattern .qualifiers.))
341         (t (error "In the method group specifier ~S,~%~
342                    ~S isn't a valid qualifier pattern."
343                   name pattern))))
344
345 (defun qualifier-check-runtime (pattern qualifiers)
346   (loop (cond ((and (null pattern) (null qualifiers))
347                (return t))
348               ((eq pattern '*) (return t))
349               ((and pattern qualifiers (eq (car pattern) (car qualifiers)))
350                (pop pattern)
351                (pop qualifiers))
352               (t (return nil)))))
353
354 (defun make-default-method-group-description (patterns)
355   (if (cdr patterns)
356       (format nil
357               "methods matching one of the patterns: ~{~S, ~} ~S"
358               (butlast patterns) (car (last patterns)))
359       (format nil
360               "methods matching the pattern: ~S"
361               (car patterns))))
362
363 ;;; This baby is a complete mess. I can't believe we put it in this
364 ;;; way. No doubt this is a large part of what drives MLY crazy.
365 ;;;
366 ;;; At runtime (when the effective-method is run), we bind an intercept
367 ;;; lambda-list to the arguments to the generic function.
368 ;;;
369 ;;; At compute-effective-method time, the symbols in the :arguments
370 ;;; option are bound to the symbols in the intercept lambda list.
371 (defun deal-with-args-option (wrapped-body args-option)
372   (let* ((intercept-lambda-list
373            (let (collect)
374              (dolist (arg args-option)
375                (if (memq arg lambda-list-keywords)
376                    (push arg collect)
377                    (push (gensym) collect)))
378              (nreverse collect)))
379          (intercept-rebindings
380            (loop for arg in args-option
381                  for int in intercept-lambda-list
382                  unless (memq arg lambda-list-keywords)
383                  collect `(,arg ',int))))
384     (setf (cadr wrapped-body)
385           (append intercept-rebindings (cadr wrapped-body)))
386
387     ;; Be sure to fill out the intercept lambda list so that it can
388     ;; be too short if it wants to.
389     (cond ((memq '&rest intercept-lambda-list))
390           ((memq '&allow-other-keys intercept-lambda-list))
391           ((memq '&key intercept-lambda-list)
392            (setq intercept-lambda-list
393                  (append intercept-lambda-list '(&allow-other-keys))))
394           (t
395            (setq intercept-lambda-list
396                  (append intercept-lambda-list '(&rest .ignore.)))))
397
398     `(let ((inner-result. ,wrapped-body))
399        `(apply #'(lambda ,',intercept-lambda-list
400                    ,,(when (memq '.ignore. intercept-lambda-list)
401                        ''(declare (ignore .ignore.)))
402                    ,inner-result.)
403                .combined-method-args.))))