0.6.7.22: removed CVS dollar-Header-dollar tags from sources
[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 the class
36 ;;; STANDARD-METHOD-COMBINATION. The method on COMPUTE-EFFECTIVE-METHOD does
37 ;;; standard method combination directly and is defined by hand in the file
38 ;;; combin.lisp. The method for FIND-METHOD-COMBINATION must appear in this
39 ;;; file for bootstrapping reasons.
40 ;;;
41 ;;; A commented out copy of this definition appears in combin.lisp.
42 ;;; If you change this definition here, be sure to change it there
43 ;;; also.
44 (defmethod find-method-combination ((generic-function generic-function)
45                                     (type (eql 'standard))
46                                     options)
47   (when options
48     (method-combination-error
49       "The method combination type STANDARD accepts no options."))
50   *standard-method-combination*)
51 \f
52 ;;;; short method combinations
53 ;;;;
54 ;;;; Short method combinations all follow the same rule for computing the
55 ;;;; effective method. So, we just implement that rule once. Each short
56 ;;;; method combination object just reads the parameters out of the object
57 ;;;; and runs the same rule.
58
59 (defclass short-method-combination (standard-method-combination)
60      ((operator
61         :reader short-combination-operator
62         :initarg :operator)
63       (identity-with-one-argument
64         :reader short-combination-identity-with-one-argument
65         :initarg :identity-with-one-argument))
66   (:predicate-name short-method-combination-p))
67
68 (defun expand-short-defcombin (whole)
69   (let* ((type (cadr whole))
70          (documentation
71            (getf (cddr whole) :documentation ""))
72          (identity-with-one-arg
73            (getf (cddr whole) :identity-with-one-argument nil))
74          (operator
75            (getf (cddr whole) :operator type)))
76     (make-top-level-form `(define-method-combination ,type)
77                          '(:load-toplevel :execute)
78       `(load-short-defcombin
79          ',type ',operator ',identity-with-one-arg ',documentation))))
80
81 (defun load-short-defcombin (type operator ioa doc)
82   (let* ((truename *load-truename*)
83          (specializers
84            (list (find-class 'generic-function)
85                  (intern-eql-specializer type)
86                  *the-class-t*))
87          (old-method
88            (get-method #'find-method-combination () specializers nil))
89          (new-method nil))
90     (setq new-method
91           (make-instance 'standard-method
92             :qualifiers ()
93             :specializers specializers
94             :lambda-list '(generic-function type options)
95             :function #'(lambda(args nms &rest cm-args)
96                           (declare (ignore nms cm-args))
97                           (apply
98                            #'(lambda (gf type options)
99                                (declare (ignore gf))
100                                (do-short-method-combination
101                                 type options operator ioa new-method doc))
102                            args))
103             :definition-source `((define-method-combination ,type) ,truename)))
104     (when old-method
105       (remove-method #'find-method-combination old-method))
106     (add-method #'find-method-combination new-method)))
107
108 (defun do-short-method-combination (type options operator ioa method doc)
109   (cond ((null options) (setq options '(:most-specific-first)))
110         ((equal options '(:most-specific-first)))
111         ((equal options '(:most-specific-last)))
112         (t
113          (method-combination-error
114            "Illegal options to a short method combination type.~%~
115             The method combination type ~S accepts one option which~%~
116             must be either :MOST-SPECIFIC-FIRST or :MOST-SPECIFIC-LAST."
117            type)))
118   (make-instance 'short-method-combination
119                  :type type
120                  :options options
121                  :operator operator
122                  :identity-with-one-argument ioa
123                  :definition-source method
124                  :documentation doc))
125
126 (defmethod compute-effective-method ((generic-function generic-function)
127                                      (combin short-method-combination)
128                                      applicable-methods)
129   (let ((type (method-combination-type combin))
130         (operator (short-combination-operator combin))
131         (ioa (short-combination-identity-with-one-argument combin))
132         (around ())
133         (primary ()))
134     (dolist (m applicable-methods)
135       (let ((qualifiers (method-qualifiers m)))
136         (flet ((lose (method why)
137                  (invalid-method-error
138                    method
139                    "The method ~S ~A.~%~
140                     The method combination type ~S was defined with the~%~
141                     short form of DEFINE-METHOD-COMBINATION and so requires~%~
142                     all methods have either the single qualifier ~S or the~%~
143                     single qualifier :AROUND."
144                    method why type type)))
145           (cond ((null qualifiers)
146                  (lose m "has no qualifiers"))
147                 ((cdr qualifiers)
148                  (lose m "has more than one qualifier"))
149                 ((eq (car qualifiers) :around)
150                  (push m around))
151                 ((eq (car qualifiers) type)
152                  (push m primary))
153                 (t
154                  (lose m "has an illegal qualifier"))))))
155     (setq around (nreverse around)
156           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         (arguments-option ())
183         (gf-var nil))
184     (when (and (consp (car body)) (eq (caar body) :arguments))
185       (setq arguments-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 arguments-option gf-var
191           body)
192       (make-top-level-form `(define-method-combination ,type)
193                            '(:load-toplevel :execute)
194         `(load-long-defcombin ',type ',documentation #',function)))))
195
196 (defvar *long-method-combination-functions* (make-hash-table :test 'eq))
197
198 (defun load-long-defcombin (type doc function)
199   (let* ((specializers
200            (list (find-class 'generic-function)
201                  (intern-eql-specializer type)
202                  *the-class-t*))
203          (old-method
204            (get-method #'find-method-combination () specializers nil))
205          (new-method
206            (make-instance 'standard-method
207              :qualifiers ()
208              :specializers specializers
209              :lambda-list '(generic-function type options)
210              :function #'(lambda (args nms &rest cm-args)
211                            (declare (ignore nms cm-args))
212                            (apply
213                             #'(lambda (generic-function type options)
214                                 (declare (ignore generic-function options))
215                                 (make-instance 'long-method-combination
216                                                :type type
217                                                :documentation doc))
218                             args))
219          :definition-source `((define-method-combination ,type)
220                               ,*load-truename*))))
221     (setf (gethash type *long-method-combination-functions*) function)
222     (when old-method (remove-method #'find-method-combination old-method))
223     (add-method #'find-method-combination new-method)))
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 arguments-option gf-var body)
236   ;;(declare (values documentation function))
237   (declare (ignore type))
238   (multiple-value-bind (documentation declarations real-body)
239       (extract-declarations body)
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 arguments-option
249         (setq wrapped-body (deal-with-arguments-option wrapped-body
250                                                        arguments-option)))
251
252       (when ll
253         (setq wrapped-body
254               `(apply #'(lambda ,ll ,wrapped-body)
255                       (method-combination-options .method-combination.))))
256
257       (values
258         documentation
259         `(lambda (.generic-function. .method-combination. .applicable-methods.)
260            (progn .generic-function. .method-combination. .applicable-methods.)
261            (block .long-method-combination-function. ,wrapped-body))))))
262
263 ;; parse-method-group-specifiers parse the method-group-specifiers
264
265 (defun wrap-method-group-specifier-bindings
266        (method-group-specifiers declarations real-body)
267   (with-gathering ((names (collecting))
268                    (specializer-caches (collecting))
269                    (cond-clauses (collecting))
270                    (required-checks (collecting))
271                    (order-cleanups (collecting)))
272       (dolist (method-group-specifier method-group-specifiers)
273         (multiple-value-bind (name tests description order required)
274             (parse-method-group-specifier method-group-specifier)
275           (declare (ignore description))
276           (let ((specializer-cache (gensym)))
277             (gather name names)
278             (gather specializer-cache specializer-caches)
279             (gather `((or ,@tests)
280                       (if  (equal ,specializer-cache .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               (gather `(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             (gather (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 (,@names ,@specializer-caches)
306       ,@declarations
307       (dolist (.method. .applicable-methods.)
308         (let ((.qualifiers. (method-qualifiers .method.))
309               (.specializers. (method-specializers .method.)))
310           (progn .qualifiers. .specializers.)
311           (cond ,@cond-clauses)))
312       ,@required-checks
313       ,@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            (gathering1 (collecting)
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                        (gather1 (parse-qualifier-pattern name pattern)))))))))
331     (values name
332             tests
333             (getf method-group-specifier :description
334                   (make-default-method-group-description patterns))
335             (getf method-group-specifier :order :most-specific-first)
336             (getf method-group-specifier :required nil))))
337
338 (defun parse-qualifier-pattern (name pattern)
339   (cond ((eq pattern '()) `(null .qualifiers.))
340         ((eq pattern '*) 't)
341         ((symbolp pattern) `(,pattern .qualifiers.))
342         ((listp pattern) `(qualifier-check-runtime ',pattern .qualifiers.))
343         (t (error "In the method group specifier ~S,~%~
344                    ~S isn't a valid qualifier pattern."
345                   name pattern))))
346
347 (defun qualifier-check-runtime (pattern qualifiers)
348   (loop (cond ((and (null pattern) (null qualifiers))
349                (return t))
350               ((eq pattern '*) (return t))
351               ((and pattern qualifiers (eq (car pattern) (car qualifiers)))
352                (pop pattern)
353                (pop qualifiers))
354               (t (return nil)))))
355
356 (defun make-default-method-group-description (patterns)
357   (if (cdr patterns)
358       (format nil
359               "methods matching one of the patterns: ~{~S, ~} ~S"
360               (butlast patterns) (car (last patterns)))
361       (format nil
362               "methods matching the pattern: ~S"
363               (car patterns))))
364
365 ;;; This baby is a complete mess. I can't believe we put it in this
366 ;;; way. No doubt this is a large part of what drives MLY crazy.
367 ;;;
368 ;;; At runtime (when the effective-method is run), we bind an intercept
369 ;;; lambda-list to the arguments to the generic function.
370 ;;;
371 ;;; At compute-effective-method time, the symbols in the :arguments
372 ;;; option are bound to the symbols in the intercept lambda list.
373 (defun deal-with-arguments-option (wrapped-body arguments-option)
374   (let* ((intercept-lambda-list
375            (gathering1 (collecting)
376              (dolist (arg arguments-option)
377                (if (memq arg lambda-list-keywords)
378                    (gather1 arg)
379                    (gather1 (gensym))))))
380          (intercept-rebindings
381            (gathering1 (collecting)
382              (iterate ((arg (list-elements arguments-option))
383                        (int (list-elements intercept-lambda-list)))
384                (unless (memq arg lambda-list-keywords)
385                  (gather1 `(,arg ',int)))))))
386
387     (setf (cadr wrapped-body)
388           (append intercept-rebindings (cadr wrapped-body)))
389
390     ;; Be sure to fill out the intercept lambda list so that it can
391     ;; be too short if it wants to.
392     (cond ((memq '&rest intercept-lambda-list))
393           ((memq '&allow-other-keys intercept-lambda-list))
394           ((memq '&key intercept-lambda-list)
395            (setq intercept-lambda-list
396                  (append intercept-lambda-list '(&allow-other-keys))))
397           (t
398            (setq intercept-lambda-list
399                  (append intercept-lambda-list '(&rest .ignore.)))))
400
401     `(let ((inner-result. ,wrapped-body))
402        `(apply #'(lambda ,',intercept-lambda-list
403                    ,,(when (memq '.ignore. intercept-lambda-list)
404                        ''(declare (ignore .ignore.)))
405                    ,inner-result.)
406                .combined-method-args.))))