1 ;;;; This software is part of the SBCL system. See the README file for
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
10 ;;;; copyright information from original PCL sources:
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
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
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
26 (defmacro define-method-combination (&whole form &rest args)
27 (declare (ignore args))
30 (expand-long-defcombin form)
31 (expand-short-defcombin form)))
33 ;;;; standard method combination
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
41 (defmethod find-method-combination ((generic-function generic-function)
42 (type (eql 'standard))
45 (method-combination-error
46 "The method combination type STANDARD accepts no options."))
47 *standard-method-combination*)
49 ;;;; short method combinations
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.
56 (defclass short-method-combination (standard-method-combination)
58 :reader short-combination-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))
65 (defun expand-short-defcombin (whole)
66 (let* ((type (cadr whole))
68 (getf (cddr whole) :documentation ""))
69 (identity-with-one-arg
70 (getf (cddr whole) :identity-with-one-argument nil))
72 (getf (cddr whole) :operator type)))
73 `(load-short-defcombin
74 ',type ',operator ',identity-with-one-arg ',documentation)))
76 (defun load-short-defcombin (type operator ioa doc)
77 (let* ((truename *load-truename*)
79 (list (find-class 'generic-function)
80 (intern-eql-specializer type)
83 (get-method #'find-method-combination () specializers nil))
86 (make-instance 'standard-method
88 :specializers specializers
89 :lambda-list '(generic-function type options)
90 :function (lambda (args nms &rest cm-args)
91 (declare (ignore nms cm-args))
93 (lambda (gf type options)
95 (short-combine-methods
96 type options operator ioa new-method doc))
98 :definition-source `((define-method-combination ,type) ,truename)))
100 (remove-method #'find-method-combination old-method))
101 (add-method #'find-method-combination new-method)))
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)))
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."
113 (make-instance 'short-method-combination
117 :identity-with-one-argument ioa
118 :definition-source method
121 (defmethod compute-effective-method ((generic-function generic-function)
122 (combin short-method-combination)
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)))
130 (dolist (m applicable-methods)
131 (let ((qualifiers (method-qualifiers m)))
132 (flet ((lose (method why)
133 (invalid-method-error
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"))
144 (lose m "has more than one qualifier"))
145 ((eq (car qualifiers) :around)
147 ((eq (car qualifiers) type)
150 (lose m "has an illegal qualifier"))))))
151 (setq around (nreverse around))
153 (:most-specific-last) ; nothing to be done, already in correct order
154 (:most-specific-first
155 (setq primary (nreverse primary))))
157 (if (and (null (cdr primary))
159 `(call-method ,(car primary) ())
160 `(,operator ,@(mapcar (lambda (m) `(call-method ,m ()))
162 (cond ((null primary)
163 `(error "No ~S methods for the generic function ~S."
164 ',type ',generic-function))
165 ((null around) main-method)
167 `(call-method ,(car around)
168 (,@(cdr around) (make-method ,main-method))))))))
170 ;;;; long method combinations
172 (defclass long-method-combination (standard-method-combination)
173 ((function :initarg :function
174 :reader long-method-combination-function)))
176 (defun expand-long-defcombin (form)
177 (let ((type (cadr form))
178 (lambda-list (caddr form))
179 (method-group-specifiers (cadddr form))
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
191 `(load-long-defcombin ',type ',documentation #',function))))
193 (defvar *long-method-combination-functions* (make-hash-table :test 'eq))
195 (defun load-long-defcombin (type doc function)
197 (list (find-class 'generic-function)
198 (intern-eql-specializer type)
201 (get-method #'find-method-combination () specializers nil))
203 (make-instance 'standard-method
205 :specializers specializers
206 :lambda-list '(generic-function type options)
207 :function (lambda (args nms &rest cm-args)
208 (declare (ignore nms cm-args))
210 (lambda (generic-function type options)
211 (declare (ignore generic-function))
212 (make-instance 'long-method-combination
217 :definition-source `((define-method-combination ,type)
219 (setf (gethash type *long-method-combination-functions*) function)
220 (when old-method (remove-method #'find-method-combination old-method))
221 (add-method #'find-method-combination new-method)))
223 (defmethod compute-effective-method ((generic-function generic-function)
224 (combin long-method-combination)
226 (funcall (gethash (method-combination-type combin)
227 *long-method-combination-functions*)
232 (defun make-long-method-combination-function
233 (type ll method-group-specifiers args-option gf-var body)
234 (declare (ignore type))
235 (multiple-value-bind (real-body declarations documentation)
236 ;; (Note that PARSE-BODY ignores its second arg ENVIRONMENT.)
237 (parse-body body nil)
240 (wrap-method-group-specifier-bindings method-group-specifiers
244 (push `(,gf-var .generic-function.) (cadr wrapped-body)))
247 (setq wrapped-body (deal-with-args-option wrapped-body args-option)))
251 `(apply #'(lambda ,ll ,wrapped-body)
252 (method-combination-options .method-combination.))))
256 `(lambda (.generic-function. .method-combination. .applicable-methods.)
257 (progn .generic-function. .method-combination. .applicable-methods.)
258 (block .long-method-combination-function. ,wrapped-body))))))
260 ;; parse-method-group-specifiers parse the method-group-specifiers
262 (defun wrap-method-group-specifier-bindings
263 (method-group-specifiers declarations real-body)
269 (dolist (method-group-specifier method-group-specifiers)
270 (multiple-value-bind (name tests description order required)
271 (parse-method-group-specifier method-group-specifier)
272 (declare (ignore description))
273 (let ((specializer-cache (gensym)))
275 (push specializer-cache specializer-caches)
277 (if (equal ,specializer-cache .specializers.)
278 (return-from .long-method-combination-function.
279 '(error "More than one method of type ~S ~
280 with the same specializers."
282 (setq ,specializer-cache .specializers.))
283 (push .method. ,name))
286 (push `(when (null ,name)
287 (return-from .long-method-combination-function.
288 '(error "No ~S methods." ',name)))
290 (loop (unless (and (constantp order)
291 (neq order (setq order (eval order))))
293 (push (cond ((eq order :most-specific-first)
294 `(setq ,name (nreverse ,name)))
295 ((eq order :most-specific-last) ())
298 (:most-specific-first
299 (setq ,name (nreverse ,name)))
300 (:most-specific-last))))
302 `(let (,@(nreverse names) ,@(nreverse specializer-caches))
304 (dolist (.method. .applicable-methods.)
305 (let ((.qualifiers. (method-qualifiers .method.))
306 (.specializers. (method-specializers .method.)))
307 (progn .qualifiers. .specializers.)
308 (cond ,@(nreverse cond-clauses))))
309 ,@(nreverse required-checks)
310 ,@(nreverse order-cleanups)
313 (defun parse-method-group-specifier (method-group-specifier)
314 ;;(declare (values name tests description order required))
315 (let* ((name (pop method-group-specifier))
321 (if (or (null method-group-specifier)
322 (memq (car method-group-specifier)
323 '(:description :order :required)))
324 (return-from collect-tests t)
325 (let ((pattern (pop method-group-specifier)))
326 (push pattern patterns)
327 (push (parse-qualifier-pattern name pattern)
329 (nreverse collect))))
332 (getf method-group-specifier :description
333 (make-default-method-group-description patterns))
334 (getf method-group-specifier :order :most-specific-first)
335 (getf method-group-specifier :required nil))))
337 (defun parse-qualifier-pattern (name pattern)
338 (cond ((eq pattern '()) `(null .qualifiers.))
340 ((symbolp pattern) `(,pattern .qualifiers.))
341 ((listp pattern) `(qualifier-check-runtime ',pattern .qualifiers.))
342 (t (error "In the method group specifier ~S,~%~
343 ~S isn't a valid qualifier pattern."
346 (defun qualifier-check-runtime (pattern qualifiers)
347 (loop (cond ((and (null pattern) (null qualifiers))
349 ((eq pattern '*) (return t))
350 ((and pattern qualifiers (eq (car pattern) (car qualifiers)))
355 (defun make-default-method-group-description (patterns)
358 "methods matching one of the patterns: ~{~S, ~} ~S"
359 (butlast patterns) (car (last patterns)))
361 "methods matching the pattern: ~S"
364 ;;; This baby is a complete mess. I can't believe we put it in this
365 ;;; way. No doubt this is a large part of what drives MLY crazy.
367 ;;; At runtime (when the effective-method is run), we bind an intercept
368 ;;; lambda-list to the arguments to the generic function.
370 ;;; At compute-effective-method time, the symbols in the :arguments
371 ;;; option are bound to the symbols in the intercept lambda list.
372 (defun deal-with-args-option (wrapped-body args-option)
373 (let* ((intercept-lambda-list
375 (dolist (arg args-option)
376 (if (memq arg lambda-list-keywords)
378 (push (gensym) collect)))
380 (intercept-rebindings
381 (loop for arg in args-option
382 for int in intercept-lambda-list
383 unless (memq arg lambda-list-keywords)
384 collect `(,arg ',int))))
385 (setf (cadr wrapped-body)
386 (append intercept-rebindings (cadr wrapped-body)))
388 ;; Be sure to fill out the intercept lambda list so that it can
389 ;; be too short if it wants to.
390 (cond ((memq '&rest intercept-lambda-list))
391 ((memq '&allow-other-keys intercept-lambda-list))
392 ((memq '&key intercept-lambda-list)
393 (setq intercept-lambda-list
394 (append intercept-lambda-list '(&allow-other-keys))))
396 (setq intercept-lambda-list
397 (append intercept-lambda-list '(&rest .ignore.)))))
399 `(let ((inner-result. ,wrapped-body))
400 `(apply #'(lambda ,',intercept-lambda-list
401 ,,(when (memq '.ignore. intercept-lambda-list)
402 ''(declare (ignore .ignore.)))
404 .combined-method-args.))))