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