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 (defun get-method-function (method &optional method-alist wrappers
)
27 (let ((fn (cadr (assoc method method-alist
))))
29 (values fn nil nil nil
)
30 (multiple-value-bind (mf fmf
)
32 (early-method-function method
)
33 (values nil
(safe-method-fast-function method
)))
34 (let* ((pv-table (and fmf
(method-plist-value method
:pv-table
))))
35 (if (and fmf
(or (null pv-table
) wrappers
))
36 (let* ((pv-wrappers (when pv-table
37 (pv-wrappers-from-all-wrappers
39 (pv (when (and pv-table pv-wrappers
)
40 (pv-table-lookup pv-table pv-wrappers
))))
43 (or mf
(if (listp method
)
44 (bug "early method with no method-function")
45 (method-function method
)))
48 (defun make-effective-method-function (generic-function form
&optional
49 method-alist wrappers
)
50 (funcall (make-effective-method-function1 generic-function form
51 (not (null method-alist
))
52 (not (null wrappers
)))
53 method-alist wrappers
))
55 (defun make-effective-method-function1 (generic-function form
56 method-alist-p wrappers-p
)
58 (eq (car form
) 'call-method
))
59 (make-effective-method-function-simple generic-function form
)
60 ;; We have some sort of `real' effective method. Go off and get a
61 ;; compiled function for it. Most of the real hair here is done by
62 ;; the GET-FUN mechanism.
63 (make-effective-method-function-internal generic-function form
64 method-alist-p wrappers-p
)))
66 (defun make-effective-method-fun-type (generic-function
71 (eq (car form
) 'call-method
))
72 (let* ((cm-args (cdr form
))
73 (method (car cm-args
)))
75 (if (if (listp method
)
76 (eq (car method
) :early-method
)
80 (multiple-value-bind (mf fmf
)
82 (early-method-function method
)
83 (values nil
(safe-method-fast-function method
)))
85 (let* ((pv-table (and fmf
(method-plist-value method
:pv-table
))))
86 (if (and fmf
(or (null pv-table
) wrappers-p
))
89 (if (and (consp method
) (eq (car method
) 'make-method
))
90 (make-effective-method-fun-type
91 generic-function
(cadr method
) method-alist-p wrappers-p
)
95 (defun make-effective-method-function-simple
96 (generic-function form
&optional no-fmf-p
)
97 ;; The effective method is just a call to CALL-METHOD. This opens up
98 ;; the possibility of just using the method function of the method as
99 ;; the effective method function.
101 ;; But we have to be careful. If that method function will ask for
102 ;; the next methods we have to provide them. We do not look to see
103 ;; if there are next methods, we look at whether the method function
104 ;; asks about them. If it does, we must tell it whether there are
105 ;; or aren't to prevent the leaky next methods bug.
106 (let* ((cm-args (cdr form
))
107 (fmf-p (and (null no-fmf-p
)
108 (or (not (eq **boot-state
** 'complete
))
109 (gf-fast-method-function-p generic-function
))
110 (null (cddr cm-args
))))
111 (method (car cm-args
))
112 (cm-args1 (cdr cm-args
)))
113 (lambda (method-alist wrappers
)
114 (make-effective-method-function-simple1 generic-function
121 (defun make-emf-from-method
122 (method cm-args
&optional gf fmf-p method-alist wrappers
)
123 ;; Avoid style-warning about compiler-macro being unavailable.
124 (declare (notinline make-instance
))
125 (multiple-value-bind (mf real-mf-p fmf pv
)
126 (get-method-function method method-alist wrappers
)
128 (let* ((next-methods (car cm-args
))
129 (next (make-effective-method-function-simple1
130 gf
(car next-methods
)
131 (list* (cdr next-methods
) (cdr cm-args
))
132 fmf-p method-alist wrappers
))
133 (arg-info (method-plist-value method
:arg-info
))
134 (default (cons nil nil
))
135 (value (method-plist-value method
:constant-value default
)))
136 (if (eq value default
)
137 (make-fast-method-call :function fmf
:pv pv
138 :next-method-call next
:arg-info arg-info
)
139 (make-constant-fast-method-call
140 :function fmf
:pv pv
:next-method-call next
141 :arg-info arg-info
:value value
)))
143 (flet ((frob-cm-arg (arg)
145 (eq (car arg
) :early-method
)
148 (if (and (consp arg
) (eq (car arg
) 'make-method
))
149 (let ((emf (make-effective-method-function
150 gf
(cadr arg
) method-alist wrappers
)))
153 (make-instance 'standard-method
154 :specializers nil
; XXX
155 :qualifiers nil
; XXX
156 :function
(method-call-function emf
)))
158 (let* ((fmf (fast-method-call-function emf
))
159 (fun (method-function-from-fast-method-call emf
))
160 (mf (%make-method-function fmf nil
)))
161 (set-funcallable-instance-function mf fun
)
162 (make-instance 'standard-method
163 :specializers nil
; XXX
167 (let* ((default (cons nil nil
))
169 (method-plist-value method
:constant-value default
))
170 ;; FIXME: this is wrong. Very wrong. It assumes
171 ;; that the only place that can have make-method
172 ;; calls is in the list structure of the second
173 ;; argument to CALL-METHOD, but AMOP says that
174 ;; CALL-METHOD can be more complicated if
175 ;; COMPUTE-EFFECTIVE-METHOD (and presumably
176 ;; MAKE-METHOD-LAMBDA) is adjusted to match.
178 ;; On the other hand, it's a start, because
179 ;; without this calls to MAKE-METHOD in method
180 ;; combination where one of the methods is of a
181 ;; user-defined class don't work at all. -- CSR,
183 (args (cons (mapcar #'frob-cm-arg
(car cm-args
))
185 (if (eq value default
)
186 (make-method-call :function mf
:call-method-args args
)
187 (make-constant-method-call :function mf
:value value
188 :call-method-args args
))))
191 (defun make-effective-method-function-simple1
192 (gf method cm-args fmf-p
&optional method-alist wrappers
)
194 (if (if (listp method
)
195 (eq (car method
) :early-method
)
197 (make-emf-from-method method cm-args gf fmf-p method-alist wrappers
)
198 (if (and (consp method
) (eq (car method
) 'make-method
))
199 (make-effective-method-function gf
201 method-alist wrappers
)
204 (defvar *global-effective-method-gensyms
* ())
205 (defvar *rebound-effective-method-gensyms
*)
207 (defun get-effective-method-gensym ()
208 (or (pop *rebound-effective-method-gensyms
*)
209 (let ((new (format-symbol *pcl-package
*
210 "EFFECTIVE-METHOD-GENSYM-~D"
211 (length *global-effective-method-gensyms
*))))
212 (setq *global-effective-method-gensyms
*
213 (append *global-effective-method-gensyms
* (list new
)))
216 (let ((*rebound-effective-method-gensyms
* ()))
217 (dotimes-fixnum (i 10) (get-effective-method-gensym)))
219 (defun expand-effective-method-function (gf effective-method
&optional env
)
220 (declare (ignore env
))
221 (multiple-value-bind (nreq applyp
)
222 (get-generic-fun-info gf
)
223 (let ((ll (make-fast-method-call-lambda-list nreq applyp
))
224 (check-applicable-keywords
225 (when (and applyp
(gf-requires-emf-keyword-checks gf
))
226 '((check-applicable-keywords))))
227 (error-p (or (eq (first effective-method
) '%no-primary-method
)
228 (eq (first effective-method
) '%invalid-qualifiers
)))
230 (when (eq **boot-state
** 'complete
)
231 ;; Otherwise the METHOD-COMBINATION slot is not bound.
232 (let ((combin (generic-function-method-combination gf
)))
233 (and (long-method-combination-p combin
)
234 (long-method-combination-args-lambda-list combin
)))))
235 (name `(emf ,(generic-function-name gf
))))
238 `(named-lambda ,name
(.pv. .next-method-call.
&rest .args.
)
239 (declare (ignore .pv. .next-method-call.
))
240 (declare (ignorable .args.
))
241 (flet ((%no-primary-method
(gf args
)
242 (call-no-primary-method gf args
))
243 (%invalid-qualifiers
(gf combin method
)
244 (invalid-qualifiers gf combin method
)))
245 (declare (ignorable #'%no-primary-method
#'%invalid-qualifiers
))
248 (let* ((required (make-dfun-required-args nreq
))
251 (sb-c::%listify-rest-args
253 (the (and unsigned-byte fixnum
)
255 `(list ,@required
))))
256 `(named-lambda ,name
,ll
257 (declare (ignore .pv. .next-method-call.
))
258 (let ((.gf-args.
,gf-args
))
259 (declare (ignorable .gf-args.
))
260 ,@check-applicable-keywords
261 ,effective-method
))))
263 `(named-lambda ,name
,ll
264 (declare (ignore ,@(if error-p ll
'(.pv. .next-method-call.
))))
265 ,@check-applicable-keywords
266 ,effective-method
))))))
268 (defun expand-emf-call-method (gf form metatypes applyp env
)
269 (declare (ignore gf metatypes applyp env
))
270 `(call-method ,(cdr form
)))
272 (defmacro call-method
(&rest args
)
273 (declare (ignore args
))
274 ;; the PROGN is here to defend against premature macroexpansion by
276 `(progn (error "~S outside of a effective method form" 'call-method
)))
278 (defun make-effective-method-list-fun-type
279 (generic-function form method-alist-p wrappers-p
)
280 (if (every (lambda (form)
281 (eq 'fast-method-call
282 (make-effective-method-fun-type
283 generic-function form method-alist-p wrappers-p
)))
288 (defun memf-test-converter (form generic-function method-alist-p wrappers-p
)
289 (case (and (consp form
) (car form
))
291 (case (make-effective-method-fun-type
292 generic-function form method-alist-p wrappers-p
)
293 (fast-method-call '.fast-call-method.
)
296 (case (make-effective-method-list-fun-type
297 generic-function form method-alist-p wrappers-p
)
298 (fast-method-call '.fast-call-method-list.
)
299 (t '.call-method-list.
)))
300 (check-applicable-keywords 'check-applicable-keywords
)
301 (t (default-test-converter form
))))
303 ;;; CMUCL comment (2003-10-15):
305 ;;; This function is called via the GET-FUNCTION mechanism on forms
306 ;;; of an emf lambda. First value returned replaces FORM in the emf
307 ;;; lambda. Second value is a list of variable names that become
308 ;;; closure variables.
309 (defun memf-code-converter
310 (form generic-function metatypes applyp method-alist-p wrappers-p
)
311 (case (and (consp form
) (car form
))
313 (let ((gensym (get-effective-method-gensym)))
314 (values (make-emf-call
315 (length metatypes
) applyp gensym
316 (make-effective-method-fun-type
317 generic-function form method-alist-p wrappers-p
))
320 (let ((gensym (get-effective-method-gensym))
321 (type (make-effective-method-list-fun-type
322 generic-function form method-alist-p wrappers-p
)))
323 (values `(dolist (emf ,gensym nil
)
324 ,(make-emf-call (length metatypes
) applyp
'emf type
))
326 (check-applicable-keywords
327 (values `(check-applicable-keywords .keyargs-start.
331 '(.keyargs-start. .valid-keys.
)))
333 (default-code-converter form
))))
335 (defun memf-constant-converter (form generic-function
)
336 (case (and (consp form
) (car form
))
339 (make-effective-method-function-simple
340 generic-function form
))))
342 (list (cons '.meth-list.
343 (mapcar (lambda (form)
344 (make-effective-method-function-simple
345 generic-function form
))
347 (check-applicable-keywords
348 '(.keyargs-start. .valid-keys.
))
350 (default-constant-converter form
))))
352 (defvar *applicable-methods
*)
353 (defun make-effective-method-function-internal
354 (generic-function effective-method method-alist-p wrappers-p
)
355 (multiple-value-bind (nreq applyp metatypes nkeys arg-info
)
356 (get-generic-fun-info generic-function
)
357 (declare (ignore nkeys arg-info
))
358 (let* ((*rebound-effective-method-gensyms
*
359 *global-effective-method-gensyms
*)
360 (name (if (early-gf-p generic-function
)
361 (!early-gf-name generic-function
)
362 (generic-function-name generic-function
)))
363 (arg-info (cons nreq applyp
))
364 (effective-method-lambda (expand-effective-method-function
365 generic-function effective-method
)))
366 (multiple-value-bind (cfunction constants
)
367 (get-fun1 effective-method-lambda
369 (memf-test-converter form generic-function
370 method-alist-p wrappers-p
))
372 (memf-code-converter form generic-function
374 method-alist-p wrappers-p
))
376 (memf-constant-converter form generic-function
)))
377 (lambda (method-alist wrappers
)
378 (multiple-value-bind (valid-keys keyargs-start
)
379 (when (memq '.valid-keys. constants
)
380 (compute-applicable-keywords
381 generic-function
*applicable-methods
*))
382 (flet ((compute-constant (constant)
386 (funcall (cdr constant
) method-alist wrappers
))
389 (funcall fn method-alist wrappers
))
393 (.keyargs-start. keyargs-start
)
394 (.valid-keys. valid-keys
)
396 (let ((fun (apply cfunction
397 (mapcar #'compute-constant constants
))))
398 (set-fun-name fun
`(combined-method ,name
))
399 (make-fast-method-call :function fun
400 :arg-info arg-info
)))))))))
402 (defmacro call-method-list
(&rest calls
)
405 (defun make-call-methods (methods)
407 ,@(mapcar (lambda (method) `(call-method ,method
())) methods
)))
409 (defun gf-requires-emf-keyword-checks (generic-function)
410 (member '&key
(gf-lambda-list generic-function
)))
412 (defun standard-compute-effective-method
413 (generic-function combin applicable-methods
)
414 (collect ((before) (primary) (after) (around))
415 (flet ((invalid (gf combin m
) (invalid-qualifiers gf combin m
)))
416 (dolist (m applicable-methods
)
417 (let ((qualifiers (if (listp m
)
418 (early-method-qualifiers m
)
419 (safe-method-qualifiers m
))))
421 ((null qualifiers
) (primary m
))
422 ((cdr qualifiers
) (invalid generic-function combin m
))
423 ((eq (car qualifiers
) :around
) (around m
))
424 ((eq (car qualifiers
) :before
) (before m
))
425 ((eq (car qualifiers
) :after
) (after m
))
426 (t (invalid generic-function combin m
))))))
427 (cond ((null (primary))
428 `(%no-primary-method
',generic-function .args.
))
429 ((and (null (before)) (null (after)) (null (around)))
430 ;; By returning a single call-method `form' here we enable
431 ;; an important implementation-specific optimization; that
432 ;; is, we can use the fast method function directly as the
433 ;; effective method function.
435 ;; However, the requirement by ANSI (CLHS 7.6.5) on generic
436 ;; function argument checking inhibits this, as we don't
437 ;; perform this checking in fast-method-functions given
438 ;; that they are not solely used for effective method
439 ;; functions, but also in combination, when they should not
440 ;; perform argument checks.
442 `(call-method ,(first (primary)) ,(rest (primary)))))
443 (if (gf-requires-emf-keyword-checks generic-function
)
444 ;; the PROGN inhibits the above optimization
445 `(progn ,call-method
)
448 (let ((main-effective-method
449 (if (or (before) (after))
450 `(multiple-value-prog1
452 ,(make-call-methods (before))
453 (call-method ,(first (primary))
455 ,(make-call-methods (reverse (after))))
456 `(call-method ,(first (primary)) ,(rest (primary))))))
458 `(call-method ,(first (around))
460 (make-method ,main-effective-method
)))
461 main-effective-method
))))))
463 ;;; helper code for checking keywords in generic function calls.
464 (defun compute-applicable-keywords (gf methods
)
465 (let ((any-keyp nil
))
466 (flet ((analyze (lambda-list)
467 (multiple-value-bind (llks nreq nopt keys
)
468 (analyze-lambda-list lambda-list
)
469 (declare (ignore nreq
))
470 (when (ll-kwds-keyp llks
)
472 (values nopt
(ll-kwds-allowp llks
) keys
))))
473 (multiple-value-bind (nopt allowp keys
)
474 (analyze (generic-function-lambda-list gf
))
475 (dolist (method methods
)
476 (let ((ll (if (consp method
)
477 (early-method-lambda-list method
)
478 (method-lambda-list method
))))
479 (multiple-value-bind (n allowp method-keys
)
483 (return-from compute-applicable-keywords
(values t nopt
)))
484 (setq keys
(union method-keys keys
)))))
486 (values (if allowp t keys
) nopt
)))))
488 (defun check-applicable-keywords (start valid-keys more-context more-count
)
489 (let ((allow-other-keys-seen nil
)
490 (allow-other-keys nil
)
492 (declare (type index i more-count
)
494 (flet ((current-value ()
495 (sb-c::%more-arg more-context i
)))
496 (declare (inline current-value
))
499 (when (>= i more-count
)
500 (when (and (invalid) (not allow-other-keys
))
501 (error 'simple-program-error
502 :format-control
"~@<invalid keyword argument~P: ~
503 ~{~S~^, ~} (valid keys are ~{~S~^, ~}).~@:>"
504 :format-arguments
(list (length (invalid)) (invalid) valid-keys
)))
506 (let ((key (current-value)))
510 (error 'simple-program-error
511 :format-control
"~@<keyword argument not a symbol: ~S.~@:>"
512 :format-arguments
(list key
)))
514 (sb-c::%odd-key-args-error
))
515 ((eq key
:allow-other-keys
)
516 ;; only the leftmost :ALLOW-OTHER-KEYS has any effect
517 (unless allow-other-keys-seen
518 (setq allow-other-keys-seen t
519 allow-other-keys
(current-value))))
521 ((not (memq key valid-keys
)) (invalid key
))))
524 ;;;; the STANDARD method combination type. This is coded by hand
525 ;;;; (rather than with DEFINE-METHOD-COMBINATION) for bootstrapping
526 ;;;; and efficiency reasons. Note that the definition of the
527 ;;;; FIND-METHOD-COMBINATION-METHOD appears in the file
528 ;;;; defcombin.lisp. This is because EQL methods can't appear in the
531 ;;;; The DEFCLASS for the METHOD-COMBINATION and
532 ;;;; STANDARD-METHOD-COMBINATION classes has to appear here for this
533 ;;;; reason. This code must conform to the code in the file
534 ;;;; defcombin.lisp, look there for more details.
536 (defun compute-effective-method (generic-function combin applicable-methods
)
537 (standard-compute-effective-method generic-function
541 (defun invalid-method-error (method format-control
&rest format-arguments
)
542 (let ((sb-debug:*stack-top-hint
* (find-caller-frame)))
543 (error "~@<invalid method error for ~2I~_~S ~I~_method: ~2I~_~?~:>"
548 (defun method-combination-error (format-control &rest format-arguments
)
549 (let ((sb-debug:*stack-top-hint
* (find-caller-frame)))
550 (error "~@<method combination error in CLOS dispatch: ~2I~_~?~:>"