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* ((pathname *load-pathname*)
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) ,pathname)))
100 (remove-method #'find-method-combination old-method))
101 (add-method #'find-method-combination new-method)
102 (setf (random-documentation type 'method-combination) doc)
105 (defun short-combine-methods (type options operator ioa method doc)
106 (cond ((null options) (setq options '(:most-specific-first)))
107 ((equal options '(:most-specific-first)))
108 ((equal options '(:most-specific-last)))
110 (method-combination-error
111 "Illegal options to a short method combination type.~%~
112 The method combination type ~S accepts one option which~%~
113 must be either :MOST-SPECIFIC-FIRST or :MOST-SPECIFIC-LAST."
115 (make-instance 'short-method-combination
119 :identity-with-one-argument ioa
120 :definition-source method
123 (defmethod compute-effective-method ((generic-function generic-function)
124 (combin short-method-combination)
126 (let ((type (method-combination-type combin))
127 (operator (short-combination-operator combin))
128 (ioa (short-combination-identity-with-one-argument combin))
129 (order (car (method-combination-options combin)))
132 (flet ((invalid (gf combin m)
133 (if *in-precompute-effective-methods-p*
134 (return-from compute-effective-method
135 `(%invalid-qualifiers ',gf ',combin ',m))
136 (invalid-qualifiers gf combin m))))
137 (dolist (m applicable-methods)
138 (let ((qualifiers (method-qualifiers m)))
139 (cond ((null qualifiers) (invalid generic-function combin m))
140 ((cdr qualifiers) (invalid generic-function combin m))
141 ((eq (car qualifiers) :around)
143 ((eq (car qualifiers) type)
145 (t (invalid generic-function combin m))))))
146 (setq around (nreverse around))
148 (:most-specific-last) ; nothing to be done, already in correct order
149 (:most-specific-first
150 (setq primary (nreverse primary))))
152 (if (and (null (cdr primary))
154 `(call-method ,(car primary) ())
155 `(,operator ,@(mapcar (lambda (m) `(call-method ,m ()))
157 (cond ((null primary)
158 ;; As of sbcl-0.8.0.80 we don't seem to need to need
159 ;; to do anything messy like
160 ;; `(APPLY (FUNCTION (IF AROUND
161 ;; 'NO-PRIMARY-METHOD
162 ;; 'NO-APPLICABLE-METHOD)
163 ;; ',GENERIC-FUNCTION
165 ;; here because (for reasons I don't understand at the
166 ;; moment -- WHN) control will never reach here if there
167 ;; are no applicable methods, but instead end up
168 ;; in NO-APPLICABLE-METHODS first.
170 ;; FIXME: The way that we arrange for .ARGS. to be bound
171 ;; here seems weird. We rely on EXPAND-EFFECTIVE-METHOD-FUNCTION
172 ;; recognizing any form whose operator is %NO-PRIMARY-METHOD
173 ;; as magical, and carefully surrounding it with a
174 ;; LAMBDA form which binds .ARGS. But...
175 ;; 1. That seems fragile, because the magicalness of
176 ;; %NO-PRIMARY-METHOD forms is scattered around
177 ;; the system. So it could easily be broken by
178 ;; locally-plausible maintenance changes like,
179 ;; e.g., using the APPLY expression above.
180 ;; 2. That seems buggy w.r.t. to MOPpish tricks in
182 ;; (DEFMETHOD COMPUTE-EFFECTIVE-METHOD :AROUND (...)
183 ;; `(PROGN ,(CALL-NEXT-METHOD) (INCF *MY-CTR*)))
184 `(%no-primary-method ',generic-function .args.))
185 ((null around) main-method)
187 `(call-method ,(car around)
188 (,@(cdr around) (make-method ,main-method))))))))
190 (defmethod invalid-qualifiers ((gf generic-function)
191 (combin short-method-combination)
193 (let ((qualifiers (method-qualifiers method))
194 (type (method-combination-type combin)))
196 ((null qualifiers) "has no qualifiers")
197 ((cdr qualifiers) "has too many qualifiers")
198 (t (aver (and (neq (car qualifiers) type)
199 (neq (car qualifiers) :around)))
200 "has an invalid qualifier"))))
201 (invalid-method-error
203 "The method ~S on ~S ~A.~%~
204 The method combination type ~S was defined with the~%~
205 short form of DEFINE-METHOD-COMBINATION and so requires~%~
206 all methods have either the single qualifier ~S or the~%~
207 single qualifier :AROUND."
208 method gf why type type))))
210 ;;;; long method combinations
212 (defun expand-long-defcombin (form)
213 (let ((type (cadr form))
214 (lambda-list (caddr form))
215 (method-group-specifiers (cadddr form))
219 (when (and (consp (car body)) (eq (caar body) :arguments))
220 (setq args-option (cdr (pop body))))
221 (when (and (consp (car body)) (eq (caar body) :generic-function))
222 (setq gf-var (cadr (pop body))))
223 (multiple-value-bind (documentation function)
224 (make-long-method-combination-function
225 type lambda-list method-group-specifiers args-option gf-var
227 `(load-long-defcombin ',type ',documentation #',function
230 (defvar *long-method-combination-functions* (make-hash-table :test 'eq))
232 (defun load-long-defcombin (type doc function args-lambda-list)
234 (list (find-class 'generic-function)
235 (intern-eql-specializer type)
238 (get-method #'find-method-combination () specializers nil))
240 (make-instance 'standard-method
242 :specializers specializers
243 :lambda-list '(generic-function type options)
244 :function (lambda (args nms &rest cm-args)
245 (declare (ignore nms cm-args))
247 (lambda (generic-function type options)
248 (declare (ignore generic-function))
249 (make-instance 'long-method-combination
252 :args-lambda-list args-lambda-list
255 :definition-source `((define-method-combination ,type)
257 (setf (gethash type *long-method-combination-functions*) function)
258 (when old-method (remove-method #'find-method-combination old-method))
259 (add-method #'find-method-combination new-method)
260 (setf (random-documentation type 'method-combination) doc)
263 (defmethod compute-effective-method ((generic-function generic-function)
264 (combin long-method-combination)
266 (funcall (gethash (method-combination-type combin)
267 *long-method-combination-functions*)
272 (defun make-long-method-combination-function
273 (type ll method-group-specifiers args-option gf-var body)
274 (declare (ignore type))
275 (multiple-value-bind (real-body declarations documentation)
278 (wrap-method-group-specifier-bindings method-group-specifiers
282 (push `(,gf-var .generic-function.) (cadr wrapped-body)))
285 (setq wrapped-body (deal-with-args-option wrapped-body args-option)))
289 `(apply #'(lambda ,ll ,wrapped-body)
290 (method-combination-options .method-combination.))))
294 `(lambda (.generic-function. .method-combination. .applicable-methods.)
295 (declare (ignorable .generic-function.
296 .method-combination. .applicable-methods.))
297 (block .long-method-combination-function. ,wrapped-body))))))
299 ;; parse-method-group-specifiers parse the method-group-specifiers
301 (defun wrap-method-group-specifier-bindings
302 (method-group-specifiers declarations real-body)
308 (dolist (method-group-specifier method-group-specifiers)
309 (multiple-value-bind (name tests description order required)
310 (parse-method-group-specifier method-group-specifier)
311 (declare (ignore description))
312 (let ((specializer-cache (gensym)))
314 (push specializer-cache specializer-caches)
316 (if (and (equal ,specializer-cache .specializers.)
317 (not (null .specializers.)))
318 (return-from .long-method-combination-function.
319 '(error "More than one method of type ~S ~
320 with the same specializers."
322 (setq ,specializer-cache .specializers.))
323 (push .method. ,name))
326 (push `(when (null ,name)
327 (return-from .long-method-combination-function.
328 '(error "No ~S methods." ',name)))
330 (loop (unless (and (constantp order)
331 (neq order (setq order (eval order))))
333 (push (cond ((eq order :most-specific-first)
334 `(setq ,name (nreverse ,name)))
335 ((eq order :most-specific-last) ())
338 (:most-specific-first
339 (setq ,name (nreverse ,name)))
340 (:most-specific-last))))
342 `(let (,@(nreverse names) ,@(nreverse specializer-caches))
344 (dolist (.method. .applicable-methods.)
345 (let ((.qualifiers. (method-qualifiers .method.))
346 (.specializers. (method-specializers .method.)))
347 (declare (ignorable .qualifiers. .specializers.))
348 (cond ,@(nreverse cond-clauses))))
349 ,@(nreverse required-checks)
350 ,@(nreverse order-cleanups)
353 (defun parse-method-group-specifier (method-group-specifier)
354 ;;(declare (values name tests description order required))
355 (let* ((name (pop method-group-specifier))
361 (if (or (null method-group-specifier)
362 (memq (car method-group-specifier)
363 '(:description :order :required)))
364 (return-from collect-tests t)
365 (let ((pattern (pop method-group-specifier)))
366 (push pattern patterns)
367 (push (parse-qualifier-pattern name pattern)
369 (nreverse collect))))
372 (getf method-group-specifier :description
373 (make-default-method-group-description patterns))
374 (getf method-group-specifier :order :most-specific-first)
375 (getf method-group-specifier :required nil))))
377 (defun parse-qualifier-pattern (name pattern)
378 (cond ((eq pattern '()) `(null .qualifiers.))
380 ((symbolp pattern) `(,pattern .qualifiers.))
381 ((listp pattern) `(qualifier-check-runtime ',pattern .qualifiers.))
382 (t (error "In the method group specifier ~S,~%~
383 ~S isn't a valid qualifier pattern."
386 (defun qualifier-check-runtime (pattern qualifiers)
387 (loop (cond ((and (null pattern) (null qualifiers))
389 ((eq pattern '*) (return t))
390 ((and pattern qualifiers (eq (car pattern) (car qualifiers)))
395 (defun make-default-method-group-description (patterns)
398 "methods matching one of the patterns: ~{~S, ~} ~S"
399 (butlast patterns) (car (last patterns)))
401 "methods matching the pattern: ~S"
404 ;;; This baby is a complete mess. I can't believe we put it in this
405 ;;; way. No doubt this is a large part of what drives MLY crazy.
407 ;;; At runtime (when the effective-method is run), we bind an intercept
408 ;;; lambda-list to the arguments to the generic function.
410 ;;; At compute-effective-method time, the symbols in the :arguments
411 ;;; option are bound to the symbols in the intercept lambda list.
413 ;;; FIXME: in here we have not one but two mini-copies of a weird
414 ;;; hybrid of PARSE-LAMBDA-LIST and PARSE-DEFMACRO-LAMBDA-LIST.
415 (defun deal-with-args-option (wrapped-body args-lambda-list)
416 (let ((intercept-rebindings
418 (dolist (arg args-lambda-list (nreverse rebindings))
419 (unless (member arg lambda-list-keywords)
421 (symbol (push `(,arg ',arg) rebindings))
423 (unless (symbolp (car arg))
424 (error "invalid lambda-list specifier: ~S." arg))
425 (push `(,(car arg) ',(car arg)) rebindings))
426 (t (error "invalid lambda-list-specifier: ~S." arg)))))))
430 ;; Count the number of required and optional parameters in
431 ;; ARGS-LAMBDA-LIST into NREQ and NOPT, and set WHOLE to the
432 ;; name of a &WHOLE parameter, if any.
433 (when (member '&whole (rest args-lambda-list))
434 (error 'simple-program-error
435 :format-control "~@<The value of the :ARGUMENTS option of ~
436 DEFINE-METHOD-COMBINATION is~2I~_~S,~I~_but &WHOLE may ~
437 only appear first in the lambda list.~:>"
438 :format-arguments (list args-lambda-list)))
439 (loop with state = 'required
440 for arg in args-lambda-list do
441 (if (memq arg lambda-list-keywords)
444 (required (incf nreq))
445 (&optional (incf nopt))
446 (&whole (setq whole arg state 'required)))))
447 ;; This assumes that the head of WRAPPED-BODY is a let, and it
448 ;; injects let-bindings of the form (ARG 'SYM) for all variables
449 ;; of the argument-lambda-list; SYM is a gensym.
450 (aver (memq (first wrapped-body) '(let let*)))
451 (setf (second wrapped-body)
452 (append intercept-rebindings (second wrapped-body)))
453 ;; Be sure to fill out the args lambda list so that it can be too
454 ;; short if it wants to.
455 (unless (or (memq '&rest args-lambda-list)
456 (memq '&allow-other-keys args-lambda-list))
457 (let ((aux (memq '&aux args-lambda-list)))
458 (setq args-lambda-list
459 (append (ldiff args-lambda-list aux)
460 (if (memq '&key args-lambda-list)
464 ;; .GENERIC-FUNCTION. is bound to the generic function in the
465 ;; method combination function, and .GF-ARGS* is bound to the
466 ;; generic function arguments in effective method functions
467 ;; created for generic functions having a method combination that
470 ;; The DESTRUCTURING-BIND binds the parameters of the
471 ;; ARGS-LAMBDA-LIST to actual generic function arguments. Because
472 ;; ARGS-LAMBDA-LIST may be shorter or longer than the generic
473 ;; function's lambda list, which is only known at run time, this
474 ;; destructuring has to be done on a slighly modified list of
475 ;; actual arguments, from which values might be stripped or added.
477 ;; Using one of the variable names in the body inserts a symbol
478 ;; into the effective method, and running the effective method
479 ;; produces the value of actual argument that is bound to the
481 `(let ((inner-result. ,wrapped-body)
482 (gf-lambda-list (generic-function-lambda-list .generic-function.)))
483 `(destructuring-bind ,',args-lambda-list
484 (frob-combined-method-args
485 .gf-args. ',gf-lambda-list
487 ,,(when (memq '.ignore. args-lambda-list)
488 ''(declare (ignore .ignore.)))
489 ;; If there is a &WHOLE in the args-lambda-list, let
490 ;; it result in the actual arguments of the generic-function
491 ;; not the frobbed list.
493 ``(setq ,',whole .gf-args.))
496 ;;; Partition VALUES into three sections: required, optional, and the
497 ;;; rest, according to required, optional, and other parameters in
498 ;;; LAMBDA-LIST. Make the required and optional sections NREQ and
499 ;;; NOPT elements long by discarding values or adding NILs. Value is
500 ;;; the concatenated list of required and optional sections, and what
501 ;;; is left as rest from VALUES.
502 (defun frob-combined-method-args (values lambda-list nreq nopt)
503 (loop with section = 'required
504 for arg in lambda-list
505 if (memq arg lambda-list-keywords) do
507 (unless (eq section '&optional)
509 else if (eq section 'required)
511 and collect (pop values) into required
512 else if (eq section '&optional)
514 and collect (pop values) into optional
516 (flet ((frob (list n m)
517 (cond ((> n m) (butlast list (- n m)))
518 ((< n m) (nconc list (make-list (- m n))))
520 (return (nconc (frob required nr nreq)
521 (frob optional no nopt)