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