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 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.
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
44 (defmethod find-method-combination ((generic-function generic-function)
45 (type (eql 'standard))
48 (method-combination-error
49 "The method combination type STANDARD accepts no options."))
50 *standard-method-combination*)
52 ;;;; short method combinations
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.
59 (defclass short-method-combination (standard-method-combination)
61 :reader short-combination-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))
68 (defun expand-short-defcombin (whole)
69 (let* ((type (cadr whole))
71 (getf (cddr whole) :documentation ""))
72 (identity-with-one-arg
73 (getf (cddr whole) :identity-with-one-argument nil))
75 (getf (cddr whole) :operator type)))
76 `(load-short-defcombin
77 ',type ',operator ',identity-with-one-arg ',documentation)))
79 (defun load-short-defcombin (type operator ioa doc)
80 (let* ((truename *load-truename*)
82 (list (find-class 'generic-function)
83 (intern-eql-specializer type)
86 (get-method #'find-method-combination () specializers nil))
89 (make-instance 'standard-method
91 :specializers specializers
92 :lambda-list '(generic-function type options)
93 :function #'(lambda(args nms &rest cm-args)
94 (declare (ignore nms cm-args))
96 #'(lambda (gf type options)
98 (do-short-method-combination
99 type options operator ioa new-method doc))
101 :definition-source `((define-method-combination ,type) ,truename)))
103 (remove-method #'find-method-combination old-method))
104 (add-method #'find-method-combination new-method)))
106 (defun do-short-method-combination (type options operator ioa method doc)
107 (cond ((null options) (setq options '(:most-specific-first)))
108 ((equal options '(:most-specific-first)))
109 ((equal options '(:most-specific-last)))
111 (method-combination-error
112 "Illegal options to a short method combination type.~%~
113 The method combination type ~S accepts one option which~%~
114 must be either :MOST-SPECIFIC-FIRST or :MOST-SPECIFIC-LAST."
116 (make-instance 'short-method-combination
120 :identity-with-one-argument ioa
121 :definition-source method
124 (defmethod compute-effective-method ((generic-function generic-function)
125 (combin short-method-combination)
127 (let ((type (method-combination-type combin))
128 (operator (short-combination-operator combin))
129 (ioa (short-combination-identity-with-one-argument combin))
132 (dolist (m applicable-methods)
133 (let ((qualifiers (method-qualifiers m)))
134 (flet ((lose (method why)
135 (invalid-method-error
137 "The method ~S ~A.~%~
138 The method combination type ~S was defined with the~%~
139 short form of DEFINE-METHOD-COMBINATION and so requires~%~
140 all methods have either the single qualifier ~S or the~%~
141 single qualifier :AROUND."
142 method why type type)))
143 (cond ((null qualifiers)
144 (lose m "has no qualifiers"))
146 (lose m "has more than one qualifier"))
147 ((eq (car qualifiers) :around)
149 ((eq (car qualifiers) type)
152 (lose m "has an illegal qualifier"))))))
153 (setq around (nreverse around)
154 primary (nreverse primary))
156 (if (and (null (cdr primary))
158 `(call-method ,(car primary) ())
159 `(,operator ,@(mapcar #'(lambda (m) `(call-method ,m ()))
161 (cond ((null primary)
162 `(error "No ~S methods for the generic function ~S."
163 ',type ',generic-function))
164 ((null around) main-method)
166 `(call-method ,(car around)
167 (,@(cdr around) (make-method ,main-method))))))))
169 ;;;; long method combinations
171 (defclass long-method-combination (standard-method-combination)
172 ((function :initarg :function
173 :reader long-method-combination-function)))
175 (defun expand-long-defcombin (form)
176 (let ((type (cadr form))
177 (lambda-list (caddr form))
178 (method-group-specifiers (cadddr form))
180 (arguments-option ())
182 (when (and (consp (car body)) (eq (caar body) :arguments))
183 (setq arguments-option (cdr (pop body))))
184 (when (and (consp (car body)) (eq (caar body) :generic-function))
185 (setq gf-var (cadr (pop body))))
186 (multiple-value-bind (documentation function)
187 (make-long-method-combination-function
188 type lambda-list method-group-specifiers arguments-option gf-var
190 `(load-long-defcombin ',type ',documentation #',function))))
192 (defvar *long-method-combination-functions* (make-hash-table :test 'eq))
194 (defun load-long-defcombin (type doc function)
196 (list (find-class 'generic-function)
197 (intern-eql-specializer type)
200 (get-method #'find-method-combination () specializers nil))
202 (make-instance 'standard-method
204 :specializers specializers
205 :lambda-list '(generic-function type options)
206 :function #'(lambda (args nms &rest cm-args)
207 (declare (ignore nms cm-args))
209 #'(lambda (generic-function type options)
210 (declare (ignore generic-function options))
211 (make-instance 'long-method-combination
215 :definition-source `((define-method-combination ,type)
217 (setf (gethash type *long-method-combination-functions*) function)
218 (when old-method (remove-method #'find-method-combination old-method))
219 (add-method #'find-method-combination new-method)))
221 (defmethod compute-effective-method ((generic-function generic-function)
222 (combin long-method-combination)
224 (funcall (gethash (method-combination-type combin)
225 *long-method-combination-functions*)
230 (defun make-long-method-combination-function
231 (type ll method-group-specifiers arguments-option gf-var body)
232 ;;(declare (values documentation function))
233 (declare (ignore type))
234 (multiple-value-bind (documentation declarations real-body)
235 (extract-declarations body)
238 (wrap-method-group-specifier-bindings method-group-specifiers
242 (push `(,gf-var .generic-function.) (cadr wrapped-body)))
244 (when arguments-option
245 (setq wrapped-body (deal-with-arguments-option wrapped-body
250 `(apply #'(lambda ,ll ,wrapped-body)
251 (method-combination-options .method-combination.))))
255 `(lambda (.generic-function. .method-combination. .applicable-methods.)
256 (progn .generic-function. .method-combination. .applicable-methods.)
257 (block .long-method-combination-function. ,wrapped-body))))))
259 ;; parse-method-group-specifiers parse the method-group-specifiers
261 (defun wrap-method-group-specifier-bindings
262 (method-group-specifiers declarations real-body)
263 (with-gathering ((names (collecting))
264 (specializer-caches (collecting))
265 (cond-clauses (collecting))
266 (required-checks (collecting))
267 (order-cleanups (collecting)))
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)))
274 (gather specializer-cache specializer-caches)
275 (gather `((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."
281 (setq ,specializer-cache .specializers.))
282 (push .method. ,name))
285 (gather `(when (null ,name)
286 (return-from .long-method-combination-function.
287 '(error "No ~S methods." ',name)))
289 (loop (unless (and (constantp order)
290 (neq order (setq order (eval order))))
292 (gather (cond ((eq order :most-specific-first)
293 `(setq ,name (nreverse ,name)))
294 ((eq order :most-specific-last) ())
297 (:most-specific-first
298 (setq ,name (nreverse ,name)))
299 (:most-specific-last))))
301 `(let (,@names ,@specializer-caches)
303 (dolist (.method. .applicable-methods.)
304 (let ((.qualifiers. (method-qualifiers .method.))
305 (.specializers. (method-specializers .method.)))
306 (progn .qualifiers. .specializers.)
307 (cond ,@cond-clauses)))
312 (defun parse-method-group-specifier (method-group-specifier)
313 ;;(declare (values name tests description order required))
314 (let* ((name (pop method-group-specifier))
317 (gathering1 (collecting)
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 (gather1 (parse-qualifier-pattern name pattern)))))))))
329 (getf method-group-specifier :description
330 (make-default-method-group-description patterns))
331 (getf method-group-specifier :order :most-specific-first)
332 (getf method-group-specifier :required nil))))
334 (defun parse-qualifier-pattern (name pattern)
335 (cond ((eq pattern '()) `(null .qualifiers.))
337 ((symbolp pattern) `(,pattern .qualifiers.))
338 ((listp pattern) `(qualifier-check-runtime ',pattern .qualifiers.))
339 (t (error "In the method group specifier ~S,~%~
340 ~S isn't a valid qualifier pattern."
343 (defun qualifier-check-runtime (pattern qualifiers)
344 (loop (cond ((and (null pattern) (null qualifiers))
346 ((eq pattern '*) (return t))
347 ((and pattern qualifiers (eq (car pattern) (car qualifiers)))
352 (defun make-default-method-group-description (patterns)
355 "methods matching one of the patterns: ~{~S, ~} ~S"
356 (butlast patterns) (car (last patterns)))
358 "methods matching the pattern: ~S"
361 ;;; This baby is a complete mess. I can't believe we put it in this
362 ;;; way. No doubt this is a large part of what drives MLY crazy.
364 ;;; At runtime (when the effective-method is run), we bind an intercept
365 ;;; lambda-list to the arguments to the generic function.
367 ;;; At compute-effective-method time, the symbols in the :arguments
368 ;;; option are bound to the symbols in the intercept lambda list.
369 (defun deal-with-arguments-option (wrapped-body arguments-option)
370 (let* ((intercept-lambda-list
371 (gathering1 (collecting)
372 (dolist (arg arguments-option)
373 (if (memq arg lambda-list-keywords)
375 (gather1 (gensym))))))
376 (intercept-rebindings
377 (gathering1 (collecting)
378 (iterate ((arg (list-elements arguments-option))
379 (int (list-elements intercept-lambda-list)))
380 (unless (memq arg lambda-list-keywords)
381 (gather1 `(,arg ',int)))))))
383 (setf (cadr wrapped-body)
384 (append intercept-rebindings (cadr wrapped-body)))
386 ;; Be sure to fill out the intercept lambda list so that it can
387 ;; be too short if it wants to.
388 (cond ((memq '&rest intercept-lambda-list))
389 ((memq '&allow-other-keys intercept-lambda-list))
390 ((memq '&key intercept-lambda-list)
391 (setq intercept-lambda-list
392 (append intercept-lambda-list '(&allow-other-keys))))
394 (setq intercept-lambda-list
395 (append intercept-lambda-list '(&rest .ignore.)))))
397 `(let ((inner-result. ,wrapped-body))
398 `(apply #'(lambda ,',intercept-lambda-list
399 ,,(when (memq '.ignore. intercept-lambda-list)
400 ''(declare (ignore .ignore.)))
402 .combined-method-args.))))