Declare COERCE and two helpers as EXPLICIT-CHECK.
[sbcl.git] / src / pcl / defcombin.lisp
blob9b3f46bf51438186e339bddfddc71e6e39170508
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
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
8 ;;;; information.
10 ;;;; copyright information from original PCL sources:
11 ;;;;
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
14 ;;;;
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
18 ;;;; control laws.
19 ;;;;
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
22 ;;;; specification.
24 (in-package "SB-PCL")
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
29 ;;;
30 (defmacro define-method-combination (&whole form &rest args)
31 (declare (ignore args))
32 `(progn
33 (with-single-package-locked-error
34 (:symbol ',(second form) "defining ~A as a method combination"))
35 ,(if (and (cddr form)
36 (listp (caddr form)))
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
47 ;;; reasons.
48 (defmethod find-method-combination ((generic-function generic-function)
49 (type-name (eql 'standard))
50 options)
51 (when options
52 (method-combination-error
53 "STANDARD method combination accepts no options."))
54 *standard-method-combination*)
56 ;;;; short method combinations
57 ;;;;
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))
65 (documentation
66 (getf (cddr whole) :documentation))
67 (identity-with-one-arg
68 (getf (cddr whole) :identity-with-one-argument nil))
69 (operator
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)
76 (let* ((specializers
77 (list (find-class 'generic-function)
78 (intern-eql-specializer type-name)
79 *the-class-t*))
80 (old-method
81 (get-method #'find-method-combination () specializers nil))
82 (new-method nil))
83 (setq new-method
84 (make-instance 'standard-method
85 :qualifiers ()
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))
90 (apply
91 (lambda (gf type-name options)
92 (declare (ignore gf))
93 (short-combine-methods
94 type-name options operator ioa new-method doc))
95 args))
96 :definition-source source-location))
97 (when old-method
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)
101 type-name))
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."
112 type-name)))
113 (make-instance 'short-method-combination
114 :type-name type-name
115 :options options
116 :operator operator
117 :identity-with-one-argument ioa
118 :definition-source method
119 :documentation doc))
121 (defmethod compute-effective-method ((generic-function generic-function)
122 (combin short-method-combination)
123 applicable-methods)
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)))
128 (around ())
129 (primary ()))
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)
138 (push m around))
139 ((eq (car qualifiers) type-name)
140 (push m primary))
141 (t (invalid generic-function combin m))))))
142 (setq around (nreverse around))
143 (ecase order
144 (:most-specific-last) ; nothing to be done, already in correct order
145 (:most-specific-first
146 (setq primary (nreverse primary))))
147 (let ((main-method
148 (if (and (null (cdr primary))
149 (not (null ioa)))
150 `(call-method ,(car primary) ())
151 `(,operator ,@(mapcar (lambda (m) `(call-method ,m ()))
152 primary)))))
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
160 ;; .ARGS.)
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
177 ;; user code, e.g.
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)
188 method)
189 (let ((qualifiers (method-qualifiers method))
190 (type-name (method-combination-type-name combin)))
191 (let ((why (cond
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
198 method
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))
212 (body (cddddr form))
213 (args-option ())
214 (gf-var nil))
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
222 body)
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)
230 (let* ((specializers
231 (list (find-class 'generic-function)
232 (intern-eql-specializer type-name)
233 *the-class-t*))
234 (old-method
235 (get-method #'find-method-combination () specializers nil))
236 (new-method
237 (make-instance 'standard-method
238 :qualifiers ()
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))
243 (apply
244 (lambda (generic-function type-name options)
245 (declare (ignore generic-function))
246 (make-instance 'long-method-combination
247 :type-name type-name
248 :options options
249 :args-lambda-list args-lambda-list
250 :documentation doc))
251 args))
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)
257 type-name))
259 (defmethod compute-effective-method ((generic-function generic-function)
260 (combin long-method-combination)
261 applicable-methods)
262 (funcall (gethash (method-combination-type-name combin)
263 *long-method-combination-functions*)
264 generic-function
265 combin
266 applicable-methods))
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)
272 (parse-body body)
273 (let ((wrapped-body
274 (wrap-method-group-specifier-bindings method-group-specifiers
275 declarations
276 real-body)))
277 (when gf-var
278 (push `(,gf-var .generic-function.) (cadr wrapped-body)))
280 (when args-option
281 (setq wrapped-body (deal-with-args-option wrapped-body args-option)))
283 (when ll
284 (setq wrapped-body
285 `(apply #'(lambda ,ll ,wrapped-body)
286 (method-combination-options .method-combination.))))
288 (values
289 documentation
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)
298 (:default-initargs
299 :references (list '(:ansi-cl :macro define-method-combination))))
301 ;;; NOTE:
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
313 (if star-only
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.)))))
323 `((or ,@tests)
324 ,maybe-error-clause
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~@:>")))
336 cond-clauses))
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)))
341 (push name names)
342 (push specializer-cache specializer-caches)
343 (push (group-cond-clause name tests specializer-cache
344 (and (eq (cadr method-group-specifier) '*)
345 (= nspecifiers 1)))
346 cond-clauses)
347 (when required
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))))
353 required-checks))
354 (loop (unless (and (constantp order)
355 (neq order (setq order
356 (constant-form-value order))))
357 (return t)))
358 (push (cond ((eq order :most-specific-first)
359 `(setq ,name (nreverse ,name)))
360 ((eq order :most-specific-last) ())
362 `(ecase ,order
363 (:most-specific-first
364 (setq ,name (nreverse ,name)))
365 (:most-specific-last))))
366 order-cleanups))))
367 `(let (,@(nreverse names) ,@(nreverse specializer-caches))
368 ,@declarations
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)
376 ,@real-body))))
378 (defun parse-method-group-specifier (method-group-specifier)
379 ;;(declare (values name tests description order required))
380 (let* ((name (pop method-group-specifier))
381 (patterns ())
382 (tests
383 (let (collect)
384 (block collect-tests
385 (loop
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)
393 collect)))))
394 (nreverse collect))))
395 (values name
396 tests
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.))
404 ((eq pattern '*) t)
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."
409 name pattern))))
411 (defun qualifier-check-runtime (pattern qualifiers)
412 (loop (cond ((and (null pattern) (null qualifiers))
413 (return t))
414 ((eq pattern '*) (return t))
415 ((and pattern qualifiers (eq (car pattern) (car qualifiers)))
416 (pop pattern)
417 (pop qualifiers))
418 (t (return nil)))))
420 (defun make-default-method-group-description (patterns)
421 (if (cdr patterns)
422 (format nil
423 "methods matching one of the patterns: ~{~S, ~} ~S"
424 (butlast patterns) (car (last patterns)))
425 (format nil
426 "methods matching the pattern: ~S"
427 (car patterns))))
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 (obsolete) PARSE-DEFMACRO-LAMBDA-LIST.
440 (defun deal-with-args-option (wrapped-body args-lambda-list)
441 (let ((intercept-rebindings
442 (let (rebindings)
443 (dolist (arg args-lambda-list (nreverse rebindings))
444 (unless (member arg lambda-list-keywords :test #'eq)
445 (typecase arg
446 (symbol (push `(,arg ',arg) rebindings))
447 (cons
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)))))))
452 (nreq 0)
453 (nopt 0)
454 (whole nil))
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)
467 (setq state arg)
468 (case state
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)
486 '(&allow-other-keys)
487 '(&rest .ignore.))
488 aux))))
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
493 ;; uses :ARGUMENTS.
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
505 ;; symbol.
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
511 ,',nreq ,',nopt)
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.
517 ,,(when whole
518 ``(setq ,',whole .gf-args.))
519 ,inner-result.))))
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
531 (setq section arg)
532 (unless (eq section '&optional)
533 (loop-finish))
534 else if (eq section 'required)
535 count t into nr
536 and collect (pop values) into required
537 else if (eq section '&optional)
538 count t into no
539 and collect (pop values) into optional
540 finally
541 (flet ((frob (list n m)
542 (cond ((> n m) (butlast list (- n m)))
543 ((< n m) (nconc list (make-list (- m n))))
544 (t list))))
545 (return (nconc (frob required nr nreq)
546 (frob optional no nopt)
547 values)))))