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)
104 (defun short-combine-methods (type options operator ioa method doc)
105 (cond ((null options) (setq options '(:most-specific-first)))
106 ((equal options '(:most-specific-first)))
107 ((equal options '(:most-specific-last)))
109 (method-combination-error
110 "Illegal options to a short method combination type.~%~
111 The method combination type ~S accepts one option which~%~
112 must be either :MOST-SPECIFIC-FIRST or :MOST-SPECIFIC-LAST."
114 (make-instance 'short-method-combination
118 :identity-with-one-argument ioa
119 :definition-source method
122 (defmethod compute-effective-method ((generic-function generic-function)
123 (combin short-method-combination)
125 (let ((type (method-combination-type combin))
126 (operator (short-combination-operator combin))
127 (ioa (short-combination-identity-with-one-argument combin))
128 (order (car (method-combination-options combin)))
131 (dolist (m applicable-methods)
132 (let ((qualifiers (method-qualifiers m)))
133 (flet ((lose (method why)
134 (invalid-method-error
136 "The method ~S ~A.~%~
137 The method combination type ~S was defined with the~%~
138 short form of DEFINE-METHOD-COMBINATION and so requires~%~
139 all methods have either the single qualifier ~S or the~%~
140 single qualifier :AROUND."
141 method why type type)))
142 (cond ((null qualifiers)
143 (lose m "has no qualifiers"))
145 (lose m "has more than one qualifier"))
146 ((eq (car qualifiers) :around)
148 ((eq (car qualifiers) type)
151 (lose m "has an illegal qualifier"))))))
152 (setq around (nreverse around))
154 (:most-specific-last) ; nothing to be done, already in correct order
155 (:most-specific-first
156 (setq primary (nreverse primary))))
158 (if (and (null (cdr primary))
160 `(call-method ,(car primary) ())
161 `(,operator ,@(mapcar (lambda (m) `(call-method ,m ()))
163 (cond ((null primary)
164 ;; FIXME(?): NO-APPLICABLE-METHOD seems more appropriate
166 ;; (1) discussion with CSR on #lisp reminded me that it's
167 ;; a vexed question whether we can validly call
168 ;; N-A-M when an :AROUND method exists (and the
169 ;; definition of NO-NEXT-METHOD seems to discourage
170 ;; us from calling NO-NEXT-METHOD directly in that
171 ;; case, since it's supposed to be called from a
172 ;; CALL-NEXT-METHOD form), and
173 ;; (2) a call to N-A-M would require &REST FUN-ARGS, and
174 ;; we don't seem to have FUN-ARGS here.
175 ;; I think ideally failures in short method combination
176 ;; would end up either in NO-APPLICABLE-METHOD or
177 ;; NO-NEXT-METHOD, and I expect that's what ANSI
178 ;; generally intended, but it's not clear to me whether
179 ;; the details of what they actually specified let us
180 ;; make that happen. So for now I've just tried to
181 ;; clarify the error message text but left the general
182 ;; logic alone (and raised the question on sbcl-devel).
184 `(error "no ~S methods for ~S on these arguments"
187 ((null around) main-method)
189 `(call-method ,(car around)
190 (,@(cdr around) (make-method ,main-method))))))))
192 ;;;; long method combinations
194 (defun expand-long-defcombin (form)
195 (let ((type (cadr form))
196 (lambda-list (caddr form))
197 (method-group-specifiers (cadddr form))
201 (when (and (consp (car body)) (eq (caar body) :arguments))
202 (setq args-option (cdr (pop body))))
203 (when (and (consp (car body)) (eq (caar body) :generic-function))
204 (setq gf-var (cadr (pop body))))
205 (multiple-value-bind (documentation function)
206 (make-long-method-combination-function
207 type lambda-list method-group-specifiers args-option gf-var
209 `(load-long-defcombin ',type ',documentation #',function
212 (defvar *long-method-combination-functions* (make-hash-table :test 'eq))
214 (defun load-long-defcombin (type doc function args-lambda-list)
216 (list (find-class 'generic-function)
217 (intern-eql-specializer type)
220 (get-method #'find-method-combination () specializers nil))
222 (make-instance 'standard-method
224 :specializers specializers
225 :lambda-list '(generic-function type options)
226 :function (lambda (args nms &rest cm-args)
227 (declare (ignore nms cm-args))
229 (lambda (generic-function type options)
230 (declare (ignore generic-function))
231 (make-instance 'long-method-combination
234 :args-lambda-list args-lambda-list
237 :definition-source `((define-method-combination ,type)
239 (setf (gethash type *long-method-combination-functions*) function)
240 (when old-method (remove-method #'find-method-combination old-method))
241 (add-method #'find-method-combination new-method)
244 (defmethod compute-effective-method ((generic-function generic-function)
245 (combin long-method-combination)
247 (funcall (gethash (method-combination-type combin)
248 *long-method-combination-functions*)
253 (defun make-long-method-combination-function
254 (type ll method-group-specifiers args-option gf-var body)
255 (declare (ignore type))
256 (multiple-value-bind (real-body declarations documentation)
259 (wrap-method-group-specifier-bindings method-group-specifiers
263 (push `(,gf-var .generic-function.) (cadr wrapped-body)))
266 (setq wrapped-body (deal-with-args-option wrapped-body args-option)))
270 `(apply #'(lambda ,ll ,wrapped-body)
271 (method-combination-options .method-combination.))))
275 `(lambda (.generic-function. .method-combination. .applicable-methods.)
276 (declare (ignorable .generic-function.
277 .method-combination. .applicable-methods.))
278 (block .long-method-combination-function. ,wrapped-body))))))
280 ;; parse-method-group-specifiers parse the method-group-specifiers
282 (defun wrap-method-group-specifier-bindings
283 (method-group-specifiers declarations real-body)
289 (dolist (method-group-specifier method-group-specifiers)
290 (multiple-value-bind (name tests description order required)
291 (parse-method-group-specifier method-group-specifier)
292 (declare (ignore description))
293 (let ((specializer-cache (gensym)))
295 (push specializer-cache specializer-caches)
297 (if (and (equal ,specializer-cache .specializers.)
298 (not (null .specializers.)))
299 (return-from .long-method-combination-function.
300 '(error "More than one method of type ~S ~
301 with the same specializers."
303 (setq ,specializer-cache .specializers.))
304 (push .method. ,name))
307 (push `(when (null ,name)
308 (return-from .long-method-combination-function.
309 '(error "No ~S methods." ',name)))
311 (loop (unless (and (constantp order)
312 (neq order (setq order (eval order))))
314 (push (cond ((eq order :most-specific-first)
315 `(setq ,name (nreverse ,name)))
316 ((eq order :most-specific-last) ())
319 (:most-specific-first
320 (setq ,name (nreverse ,name)))
321 (:most-specific-last))))
323 `(let (,@(nreverse names) ,@(nreverse specializer-caches))
325 (dolist (.method. .applicable-methods.)
326 (let ((.qualifiers. (method-qualifiers .method.))
327 (.specializers. (method-specializers .method.)))
328 (declare (ignorable .qualifiers. .specializers.))
329 (cond ,@(nreverse cond-clauses))))
330 ,@(nreverse required-checks)
331 ,@(nreverse order-cleanups)
334 (defun parse-method-group-specifier (method-group-specifier)
335 ;;(declare (values name tests description order required))
336 (let* ((name (pop method-group-specifier))
342 (if (or (null method-group-specifier)
343 (memq (car method-group-specifier)
344 '(:description :order :required)))
345 (return-from collect-tests t)
346 (let ((pattern (pop method-group-specifier)))
347 (push pattern patterns)
348 (push (parse-qualifier-pattern name pattern)
350 (nreverse collect))))
353 (getf method-group-specifier :description
354 (make-default-method-group-description patterns))
355 (getf method-group-specifier :order :most-specific-first)
356 (getf method-group-specifier :required nil))))
358 (defun parse-qualifier-pattern (name pattern)
359 (cond ((eq pattern '()) `(null .qualifiers.))
361 ((symbolp pattern) `(,pattern .qualifiers.))
362 ((listp pattern) `(qualifier-check-runtime ',pattern .qualifiers.))
363 (t (error "In the method group specifier ~S,~%~
364 ~S isn't a valid qualifier pattern."
367 (defun qualifier-check-runtime (pattern qualifiers)
368 (loop (cond ((and (null pattern) (null qualifiers))
370 ((eq pattern '*) (return t))
371 ((and pattern qualifiers (eq (car pattern) (car qualifiers)))
376 (defun make-default-method-group-description (patterns)
379 "methods matching one of the patterns: ~{~S, ~} ~S"
380 (butlast patterns) (car (last patterns)))
382 "methods matching the pattern: ~S"
385 ;;; This baby is a complete mess. I can't believe we put it in this
386 ;;; way. No doubt this is a large part of what drives MLY crazy.
388 ;;; At runtime (when the effective-method is run), we bind an intercept
389 ;;; lambda-list to the arguments to the generic function.
391 ;;; At compute-effective-method time, the symbols in the :arguments
392 ;;; option are bound to the symbols in the intercept lambda list.
393 (defun deal-with-args-option (wrapped-body args-lambda-list)
394 (let ((intercept-rebindings
396 (dolist (arg args-lambda-list (nreverse rebindings))
397 (unless (member arg lambda-list-keywords)
398 (push `(,arg ',arg) rebindings)))))
402 ;; Count the number of required and optional parameters in
403 ;; ARGS-LAMBDA-LIST into NREQ and NOPT, and set WHOLE to the
404 ;; name of a &WHOLE parameter, if any.
405 (when (member '&whole (rest args-lambda-list))
406 (error 'simple-program-error
407 :format-control "~@<The value of the :ARGUMENTS option of ~
408 DEFINE-METHOD-COMBINATION is~2I~_~S,~I~_but &WHOLE may ~
409 only appear first in the lambda list.~:>"
410 :format-arguments (list args-lambda-list)))
411 (loop with state = 'required
412 for arg in args-lambda-list do
413 (if (memq arg lambda-list-keywords)
416 (required (incf nreq))
417 (&optional (incf nopt))
418 (&whole (setq whole arg state 'required)))))
419 ;; This assumes that the head of WRAPPED-BODY is a let, and it
420 ;; injects let-bindings of the form (ARG 'SYM) for all variables
421 ;; of the argument-lambda-list; SYM is a gensym.
422 (aver (memq (first wrapped-body) '(let let*)))
423 (setf (second wrapped-body)
424 (append intercept-rebindings (second wrapped-body)))
425 ;; Be sure to fill out the args lambda list so that it can be too
426 ;; short if it wants to.
427 (unless (or (memq '&rest args-lambda-list)
428 (memq '&allow-other-keys args-lambda-list))
429 (let ((aux (memq '&aux args-lambda-list)))
430 (setq args-lambda-list
431 (append (ldiff args-lambda-list aux)
432 (if (memq '&key args-lambda-list)
436 ;; .GENERIC-FUNCTION. is bound to the generic function in the
437 ;; method combination function, and .GF-ARGS* is bound to the
438 ;; generic function arguments in effective method functions
439 ;; created for generic functions having a method combination that
442 ;; The DESTRUCTURING-BIND binds the parameters of the
443 ;; ARGS-LAMBDA-LIST to actual generic function arguments. Because
444 ;; ARGS-LAMBDA-LIST may be shorter or longer than the generic
445 ;; function's lambda list, which is only known at run time, this
446 ;; destructuring has to be done on a slighly modified list of
447 ;; actual arguments, from which values might be stripped or added.
449 ;; Using one of the variable names in the body inserts a symbol
450 ;; into the effective method, and running the effective method
451 ;; produces the value of actual argument that is bound to the
453 `(let ((inner-result. ,wrapped-body)
454 (gf-lambda-list (generic-function-lambda-list .generic-function.)))
455 `(destructuring-bind ,',args-lambda-list
456 (frob-combined-method-args
457 .gf-args. ',gf-lambda-list
459 ,,(when (memq '.ignore. args-lambda-list)
460 ''(declare (ignore .ignore.)))
461 ;; If there is a &WHOLE in the args-lambda-list, let
462 ;; it result in the actual arguments of the generic-function
463 ;; not the frobbed list.
465 ``(setq ,',whole .gf-args.))
468 ;;; Partition VALUES into three sections: required, optional, and the
469 ;;; rest, according to required, optional, and other parameters in
470 ;;; LAMBDA-LIST. Make the required and optional sections NREQ and
471 ;;; NOPT elements long by discarding values or adding NILs. Value is
472 ;;; the concatenated list of required and optional sections, and what
473 ;;; is left as rest from VALUES.
474 (defun frob-combined-method-args (values lambda-list nreq nopt)
475 (loop with section = 'required
476 for arg in lambda-list
477 if (memq arg lambda-list-keywords) do
479 (unless (eq section '&optional)
481 else if (eq section 'required)
483 and collect (pop values) into required
484 else if (eq section '&optional)
486 and collect (pop values) into optional
488 (flet ((frob (list n m)
489 (cond ((> n m) (butlast list (- n m)))
490 ((< n m) (nconc list (make-list (- m n))))
492 (return (nconc (frob required nr nreq)
493 (frob optional no nopt)