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 ;;; FIXME: according to ANSI 3.4.10 this is supposed to allow &WHOLE
27 ;;; in the long syntax. But it clearly does not, because if you write
28 ;;; (&WHOLE v) then you get (LAMBDA (&WHOLE V ...) ...) which is illegal
30 (defmacro define-method-combination
(&whole form
&rest args
)
31 (declare (ignore args
))
33 (with-single-package-locked-error
34 (:symbol
',(second form
) "defining ~A as a method combination"))
37 (expand-long-defcombin form
)
38 (expand-short-defcombin form
))))
40 ;;;; standard method combination
42 ;;; The STANDARD method combination type is implemented directly by
43 ;;; the class STANDARD-METHOD-COMBINATION. The method on
44 ;;; COMPUTE-EFFECTIVE-METHOD does standard method combination directly
45 ;;; and is defined by hand in the file combin.lisp. The method for
46 ;;; FIND-METHOD-COMBINATION must appear in this file for bootstrapping
48 (defmethod find-method-combination ((generic-function generic-function
)
49 (type-name (eql 'standard
))
52 (method-combination-error
53 "STANDARD method combination accepts no options."))
54 *standard-method-combination
*)
56 ;;;; short method combinations
58 ;;;; Short method combinations all follow the same rule for computing the
59 ;;;; effective method. So, we just implement that rule once. Each short
60 ;;;; method combination object just reads the parameters out of the object
61 ;;;; and runs the same rule.
63 (defun expand-short-defcombin (whole)
64 (let* ((type-name (cadr whole
))
66 (getf (cddr whole
) :documentation
))
67 (identity-with-one-arg
68 (getf (cddr whole
) :identity-with-one-argument nil
))
70 (getf (cddr whole
) :operator type-name
)))
71 `(load-short-defcombin
72 ',type-name
',operator
',identity-with-one-arg
',documentation
73 (sb-c:source-location
))))
75 (defun load-short-defcombin (type-name operator ioa doc source-location
)
77 (list (find-class 'generic-function
)
78 (intern-eql-specializer type-name
)
81 (get-method #'find-method-combination
() specializers nil
))
84 (make-instance 'standard-method
86 :specializers specializers
87 :lambda-list
'(generic-function type-name options
)
88 :function
(lambda (args nms
&rest cm-args
)
89 (declare (ignore nms cm-args
))
91 (lambda (gf type-name options
)
93 (short-combine-methods
94 type-name options operator ioa new-method doc
))
96 :definition-source source-location
))
98 (remove-method #'find-method-combination old-method
))
99 (add-method #'find-method-combination new-method
)
100 (setf (random-documentation type-name
'method-combination
) doc
)
103 (defun short-combine-methods (type-name 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-name (method-combination-type-name combin
))
125 (operator (short-combination-operator combin
))
126 (ioa (short-combination-identity-with-one-argument combin
))
127 (order (car (method-combination-options combin
)))
130 (flet ((invalid (gf combin m
)
131 (return-from compute-effective-method
132 `(%invalid-qualifiers
',gf
',combin
',m
))))
133 (dolist (m applicable-methods
)
134 (let ((qualifiers (method-qualifiers m
)))
135 (cond ((null qualifiers
) (invalid generic-function combin m
))
136 ((cdr qualifiers
) (invalid generic-function combin m
))
137 ((eq (car qualifiers
) :around
)
139 ((eq (car qualifiers
) type-name
)
141 (t (invalid generic-function combin m
))))))
142 (setq around
(nreverse around
))
144 (:most-specific-last
) ; nothing to be done, already in correct order
145 (:most-specific-first
146 (setq primary
(nreverse primary
))))
148 (if (and (null (cdr primary
))
150 `(call-method ,(car primary
) ())
151 `(,operator
,@(mapcar (lambda (m) `(call-method ,m
()))
153 (cond ((null primary
)
154 ;; As of sbcl-0.8.0.80 we don't seem to need to need
155 ;; to do anything messy like
156 ;; `(APPLY (FUNCTION (IF AROUND
157 ;; 'NO-PRIMARY-METHOD
158 ;; 'NO-APPLICABLE-METHOD)
159 ;; ',GENERIC-FUNCTION
161 ;; here because (for reasons I don't understand at the
162 ;; moment -- WHN) control will never reach here if there
163 ;; are no applicable methods, but instead end up
164 ;; in NO-APPLICABLE-METHODS first.
166 ;; FIXME: The way that we arrange for .ARGS. to be bound
167 ;; here seems weird. We rely on EXPAND-EFFECTIVE-METHOD-FUNCTION
168 ;; recognizing any form whose operator is %NO-PRIMARY-METHOD
169 ;; as magical, and carefully surrounding it with a
170 ;; LAMBDA form which binds .ARGS. But...
171 ;; 1. That seems fragile, because the magicalness of
172 ;; %NO-PRIMARY-METHOD forms is scattered around
173 ;; the system. So it could easily be broken by
174 ;; locally-plausible maintenance changes like,
175 ;; e.g., using the APPLY expression above.
176 ;; 2. That seems buggy w.r.t. to MOPpish tricks in
178 ;; (DEFMETHOD COMPUTE-EFFECTIVE-METHOD :AROUND (...)
179 ;; `(PROGN ,(CALL-NEXT-METHOD) (INCF *MY-CTR*)))
180 `(%no-primary-method
',generic-function .args.
))
181 ((null around
) main-method
)
183 `(call-method ,(car around
)
184 (,@(cdr around
) (make-method ,main-method
))))))))
186 (defmethod invalid-qualifiers ((gf generic-function
)
187 (combin short-method-combination
)
189 (let ((qualifiers (method-qualifiers method
))
190 (type-name (method-combination-type-name combin
)))
192 ((null qualifiers
) "has no qualifiers")
193 ((cdr qualifiers
) "has too many qualifiers")
194 (t (aver (and (neq (car qualifiers
) type-name
)
195 (neq (car qualifiers
) :around
)))
196 "has an invalid qualifier"))))
197 (invalid-method-error
199 "The method ~S on ~S ~A.~%~
200 The method combination type ~S was defined with the~%~
201 short form of DEFINE-METHOD-COMBINATION and so requires~%~
202 all methods have either the single qualifier ~S or the~%~
203 single qualifier :AROUND."
204 method gf why type-name type-name
))))
206 ;;;; long method combinations
208 (defun expand-long-defcombin (form)
209 (let ((type-name (cadr form
))
210 (lambda-list (caddr form
))
211 (method-group-specifiers (cadddr form
))
215 (when (and (consp (car body
)) (eq (caar body
) :arguments
))
216 (setq args-option
(cdr (pop body
))))
217 (when (and (consp (car body
)) (eq (caar body
) :generic-function
))
218 (setq gf-var
(cadr (pop body
))))
219 (multiple-value-bind (documentation function
)
220 (make-long-method-combination-function
221 type-name lambda-list method-group-specifiers args-option gf-var
223 `(load-long-defcombin ',type-name
',documentation
#',function
224 ',args-option
(sb-c:source-location
)))))
226 (defvar *long-method-combination-functions
* (make-hash-table :test
'eq
))
228 (defun load-long-defcombin
229 (type-name doc function args-lambda-list source-location
)
231 (list (find-class 'generic-function
)
232 (intern-eql-specializer type-name
)
235 (get-method #'find-method-combination
() specializers nil
))
237 (make-instance 'standard-method
239 :specializers specializers
240 :lambda-list
'(generic-function type-name options
)
241 :function
(lambda (args nms
&rest cm-args
)
242 (declare (ignore nms cm-args
))
244 (lambda (generic-function type-name options
)
245 (declare (ignore generic-function
))
246 (make-instance 'long-method-combination
249 :args-lambda-list args-lambda-list
252 :definition-source source-location
)))
253 (setf (gethash type-name
*long-method-combination-functions
*) function
)
254 (when old-method
(remove-method #'find-method-combination old-method
))
255 (add-method #'find-method-combination new-method
)
256 (setf (random-documentation type-name
'method-combination
) doc
)
259 (defmethod compute-effective-method ((generic-function generic-function
)
260 (combin long-method-combination
)
262 (funcall (gethash (method-combination-type-name combin
)
263 *long-method-combination-functions
*)
268 (defun make-long-method-combination-function
269 (type-name ll method-group-specifiers args-option gf-var body
)
270 (declare (ignore type-name
))
271 (multiple-value-bind (real-body declarations documentation
)
274 (wrap-method-group-specifier-bindings method-group-specifiers
278 (push `(,gf-var .generic-function.
) (cadr wrapped-body
)))
281 (setq wrapped-body
(deal-with-args-option wrapped-body args-option
)))
285 `(apply #'(lambda ,ll
,wrapped-body
)
286 (method-combination-options .method-combination.
))))
290 `(lambda (.generic-function. .method-combination. .applicable-methods.
)
291 (declare (ignorable .generic-function.
292 .method-combination. .applicable-methods.
))
293 (block .long-method-combination-function.
,wrapped-body
))))))
295 (define-condition long-method-combination-error
296 (reference-condition simple-error
)
299 :references
(list '(:ansi-cl
:macro define-method-combination
))))
303 ;;; The semantics of long form method combination in the presence of
304 ;;; multiple methods with the same specializers in the same method
305 ;;; group are unclear by the spec: a portion of the standard implies
306 ;;; that an error should be signalled, and another is more lenient.
308 ;;; It is reasonable to allow a single method group of * to bypass all
309 ;;; rules, as this is explicitly stated in the standard.
311 (defun group-cond-clause (name tests specializer-cache star-only
)
312 (let ((maybe-error-clause
314 `(setq ,specializer-cache .specializers.
)
315 `(if (and (equal ,specializer-cache .specializers.
)
316 (not (null .specializers.
)))
317 (return-from .long-method-combination-function.
318 '(error 'long-method-combination-error
319 :format-control
"More than one method of type ~S ~
320 with the same specializers."
321 :format-arguments
(list ',name
)))
322 (setq ,specializer-cache .specializers.
)))))
325 (push .method.
,name
))))
327 (defun wrap-method-group-specifier-bindings
328 (method-group-specifiers declarations real-body
)
329 (let (names specializer-caches cond-clauses required-checks order-cleanups
)
330 (let ((nspecifiers (length method-group-specifiers
)))
331 (dolist (method-group-specifier method-group-specifiers
332 (push `(t (return-from .long-method-combination-function.
333 `(invalid-method-error , .method.
334 "~@<is applicable, but does not belong ~
335 to any method group~@:>")))
337 (multiple-value-bind (name tests description order required
)
338 (parse-method-group-specifier method-group-specifier
)
339 (declare (ignore description
))
340 (let ((specializer-cache (gensym)))
342 (push specializer-cache specializer-caches
)
343 (push (group-cond-clause name tests specializer-cache
344 (and (eq (cadr method-group-specifier
) '*)
348 (push `(when (null ,name
)
349 (return-from .long-method-combination-function.
350 '(error 'long-method-combination-error
351 :format-control
"No ~S methods."
352 :format-arguments
(list ',name
))))
354 (loop (unless (and (constantp order
)
355 (neq order
(setq order
356 (constant-form-value order
))))
358 (push (cond ((eq order
:most-specific-first
)
359 `(setq ,name
(nreverse ,name
)))
360 ((eq order
:most-specific-last
) ())
363 (:most-specific-first
364 (setq ,name
(nreverse ,name
)))
365 (:most-specific-last
))))
367 `(let (,@(nreverse names
) ,@(nreverse specializer-caches
))
369 (dolist (.method. .applicable-methods.
)
370 (let ((.qualifiers.
(method-qualifiers .method.
))
371 (.specializers.
(method-specializers .method.
)))
372 (declare (ignorable .qualifiers. .specializers.
))
373 (cond ,@(nreverse cond-clauses
))))
374 ,@(nreverse required-checks
)
375 ,@(nreverse order-cleanups
)
378 (defun parse-method-group-specifier (method-group-specifier)
379 ;;(declare (values name tests description order required))
380 (let* ((name (pop method-group-specifier
))
386 (if (or (null method-group-specifier
)
387 (memq (car method-group-specifier
)
388 '(:description
:order
:required
)))
389 (return-from collect-tests t
)
390 (let ((pattern (pop method-group-specifier
)))
391 (push pattern patterns
)
392 (push (parse-qualifier-pattern name pattern
)
394 (nreverse collect
))))
397 (getf method-group-specifier
:description
398 (make-default-method-group-description patterns
))
399 (getf method-group-specifier
:order
:most-specific-first
)
400 (getf method-group-specifier
:required nil
))))
402 (defun parse-qualifier-pattern (name pattern
)
403 (cond ((eq pattern
'()) `(null .qualifiers.
))
405 ((symbolp pattern
) `(,pattern .qualifiers.
))
406 ((listp pattern
) `(qualifier-check-runtime ',pattern .qualifiers.
))
407 (t (error "In the method group specifier ~S,~%~
408 ~S isn't a valid qualifier pattern."
411 (defun qualifier-check-runtime (pattern qualifiers
)
412 (loop (cond ((and (null pattern
) (null qualifiers
))
414 ((eq pattern
'*) (return t
))
415 ((and pattern qualifiers
(eq (car pattern
) (car qualifiers
)))
420 (defun make-default-method-group-description (patterns)
423 "methods matching one of the patterns: ~{~S, ~} ~S"
424 (butlast patterns
) (car (last patterns
)))
426 "methods matching the pattern: ~S"
429 ;;; This baby is a complete mess. I can't believe we put it in this
430 ;;; way. No doubt this is a large part of what drives MLY crazy.
432 ;;; At runtime (when the effective-method is run), we bind an intercept
433 ;;; lambda-list to the arguments to the generic function.
435 ;;; At compute-effective-method time, the symbols in the :arguments
436 ;;; option are bound to the symbols in the intercept lambda list.
438 ;;; FIXME: in here we have not one but two mini-copies of a weird
439 ;;; hybrid of PARSE-LAMBDA-LIST and PARSE-DEFMACRO-LAMBDA-LIST.
440 (defun deal-with-args-option (wrapped-body args-lambda-list
)
441 (let ((intercept-rebindings
443 (dolist (arg args-lambda-list
(nreverse rebindings
))
444 (unless (member arg lambda-list-keywords
:test
#'eq
)
446 (symbol (push `(,arg
',arg
) rebindings
))
448 (unless (symbolp (car arg
))
449 (error "invalid lambda-list specifier: ~S." arg
))
450 (push `(,(car arg
) ',(car arg
)) rebindings
))
451 (t (error "invalid lambda-list-specifier: ~S." arg
)))))))
455 ;; Count the number of required and optional parameters in
456 ;; ARGS-LAMBDA-LIST into NREQ and NOPT, and set WHOLE to the
457 ;; name of a &WHOLE parameter, if any.
458 (when (member '&whole
(rest args-lambda-list
))
459 (error 'simple-program-error
460 :format-control
"~@<The value of the :ARGUMENTS option of ~
461 DEFINE-METHOD-COMBINATION is~2I~_~S,~I~_but &WHOLE may ~
462 only appear first in the lambda list.~:>"
463 :format-arguments
(list args-lambda-list
)))
464 (loop with state
= 'required
465 for arg in args-lambda-list do
466 (if (memq arg lambda-list-keywords
)
469 (required (incf nreq
))
470 (&optional
(incf nopt
))
471 (&whole
(setq whole arg state
'required
)))))
472 ;; This assumes that the head of WRAPPED-BODY is a let, and it
473 ;; injects let-bindings of the form (ARG 'SYM) for all variables
474 ;; of the argument-lambda-list; SYM is a gensym.
475 (aver (memq (first wrapped-body
) '(let let
*)))
476 (setf (second wrapped-body
)
477 (append intercept-rebindings
(second wrapped-body
)))
478 ;; Be sure to fill out the args lambda list so that it can be too
479 ;; short if it wants to.
480 (unless (or (memq '&rest args-lambda-list
)
481 (memq '&allow-other-keys args-lambda-list
))
482 (let ((aux (memq '&aux args-lambda-list
)))
483 (setq args-lambda-list
484 (append (ldiff args-lambda-list aux
)
485 (if (memq '&key args-lambda-list
)
489 ;; .GENERIC-FUNCTION. is bound to the generic function in the
490 ;; method combination function, and .GF-ARGS* is bound to the
491 ;; generic function arguments in effective method functions
492 ;; created for generic functions having a method combination that
495 ;; The DESTRUCTURING-BIND binds the parameters of the
496 ;; ARGS-LAMBDA-LIST to actual generic function arguments. Because
497 ;; ARGS-LAMBDA-LIST may be shorter or longer than the generic
498 ;; function's lambda list, which is only known at run time, this
499 ;; destructuring has to be done on a slighly modified list of
500 ;; actual arguments, from which values might be stripped or added.
502 ;; Using one of the variable names in the body inserts a symbol
503 ;; into the effective method, and running the effective method
504 ;; produces the value of actual argument that is bound to the
506 `(let ((inner-result.
,wrapped-body
)
507 (gf-lambda-list (generic-function-lambda-list .generic-function.
)))
508 `(destructuring-bind ,',args-lambda-list
509 (frob-combined-method-args
510 .gf-args.
',gf-lambda-list
512 ,,(when (memq '.ignore. args-lambda-list
)
513 ''(declare (ignore .ignore.
)))
514 ;; If there is a &WHOLE in the args-lambda-list, let
515 ;; it result in the actual arguments of the generic-function
516 ;; not the frobbed list.
518 ``(setq ,',whole .gf-args.
))
521 ;;; Partition VALUES into three sections: required, optional, and the
522 ;;; rest, according to required, optional, and other parameters in
523 ;;; LAMBDA-LIST. Make the required and optional sections NREQ and
524 ;;; NOPT elements long by discarding values or adding NILs. Value is
525 ;;; the concatenated list of required and optional sections, and what
526 ;;; is left as rest from VALUES.
527 (defun frob-combined-method-args (values lambda-list nreq nopt
)
528 (loop with section
= 'required
529 for arg in lambda-list
530 if
(memq arg lambda-list-keywords
) do
532 (unless (eq section
'&optional
)
534 else if
(eq section
'required
)
536 and collect
(pop values
) into required
537 else if
(eq section
'&optional
)
539 and collect
(pop values
) into optional
541 (flet ((frob (list n m
)
542 (cond ((> n m
) (butlast list
(- n m
)))
543 ((< n m
) (nconc list
(make-list (- m n
))))
545 (return (nconc (frob required nr nreq
)
546 (frob optional no nopt
)