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 (multiple-value-bind (mf real-mf-p fmf pv
)
124 (get-method-function method method-alist wrappers
)
126 (let* ((next-methods (car cm-args
))
127 (next (make-effective-method-function-simple1
128 gf
(car next-methods
)
129 (list* (cdr next-methods
) (cdr cm-args
))
130 fmf-p method-alist wrappers
))
131 (arg-info (method-plist-value method
:arg-info
))
132 (default (cons nil nil
))
133 (value (method-plist-value method
:constant-value default
)))
134 (if (eq value default
)
135 (make-fast-method-call :function fmf
:pv pv
136 :next-method-call next
:arg-info arg-info
)
137 (make-constant-fast-method-call
138 :function fmf
:pv pv
:next-method-call next
139 :arg-info arg-info
:value value
)))
141 (flet ((frob-cm-arg (arg)
143 (eq (car arg
) :early-method
)
146 (if (and (consp arg
) (eq (car arg
) 'make-method
))
147 (let ((emf (make-effective-method-function
148 gf
(cadr arg
) method-alist wrappers
)))
151 (make-instance 'standard-method
152 :specializers nil
; XXX
153 :qualifiers nil
; XXX
154 :function
(method-call-function emf
)))
156 (let* ((fmf (fast-method-call-function emf
))
157 (fun (method-function-from-fast-method-call emf
))
158 (mf (%make-method-function fmf nil
)))
159 (set-funcallable-instance-function mf fun
)
160 (make-instance 'standard-method
161 :specializers nil
; XXX
165 (let* ((default (cons nil nil
))
167 (method-plist-value method
:constant-value default
))
168 ;; FIXME: this is wrong. Very wrong. It assumes
169 ;; that the only place that can have make-method
170 ;; calls is in the list structure of the second
171 ;; argument to CALL-METHOD, but AMOP says that
172 ;; CALL-METHOD can be more complicated if
173 ;; COMPUTE-EFFECTIVE-METHOD (and presumably
174 ;; MAKE-METHOD-LAMBDA) is adjusted to match.
176 ;; On the other hand, it's a start, because
177 ;; without this calls to MAKE-METHOD in method
178 ;; combination where one of the methods is of a
179 ;; user-defined class don't work at all. -- CSR,
181 (args (cons (mapcar #'frob-cm-arg
(car cm-args
))
183 (if (eq value default
)
184 (make-method-call :function mf
:call-method-args args
)
185 (make-constant-method-call :function mf
:value value
186 :call-method-args args
))))
189 (defun make-effective-method-function-simple1
190 (gf method cm-args fmf-p
&optional method-alist wrappers
)
192 (if (if (listp method
)
193 (eq (car method
) :early-method
)
195 (make-emf-from-method method cm-args gf fmf-p method-alist wrappers
)
196 (if (and (consp method
) (eq (car method
) 'make-method
))
197 (make-effective-method-function gf
199 method-alist wrappers
)
202 (defvar *global-effective-method-gensyms
* ())
203 (defvar *rebound-effective-method-gensyms
*)
205 (defun get-effective-method-gensym ()
206 (or (pop *rebound-effective-method-gensyms
*)
207 (let ((new (format-symbol *pcl-package
*
208 "EFFECTIVE-METHOD-GENSYM-~D"
209 (length *global-effective-method-gensyms
*))))
210 (setq *global-effective-method-gensyms
*
211 (append *global-effective-method-gensyms
* (list new
)))
214 (let ((*rebound-effective-method-gensyms
* ()))
215 (dotimes-fixnum (i 10) (get-effective-method-gensym)))
217 (defun expand-effective-method-function (gf effective-method
&optional env
)
218 (declare (ignore env
))
219 (multiple-value-bind (nreq applyp
)
220 (get-generic-fun-info gf
)
221 (let ((ll (make-fast-method-call-lambda-list nreq applyp
))
222 (check-applicable-keywords
223 (when (and applyp
(gf-requires-emf-keyword-checks gf
))
224 '((check-applicable-keywords))))
225 (error-p (or (eq (first effective-method
) '%no-primary-method
)
226 (eq (first effective-method
) '%invalid-qualifiers
)))
228 (when (eq **boot-state
** 'complete
)
229 ;; Otherwise the METHOD-COMBINATION slot is not bound.
230 (let ((combin (generic-function-method-combination gf
)))
231 (and (long-method-combination-p combin
)
232 (long-method-combination-args-lambda-list combin
)))))
233 (name `(emf ,(generic-function-name gf
))))
236 `(named-lambda ,name
(.pv. .next-method-call.
&rest .args.
)
237 (declare (ignore .pv. .next-method-call.
))
238 (declare (ignorable .args.
))
239 (flet ((%no-primary-method
(gf args
)
240 (call-no-primary-method gf args
))
241 (%invalid-qualifiers
(gf combin method
)
242 (invalid-qualifiers gf combin method
)))
243 (declare (ignorable #'%no-primary-method
#'%invalid-qualifiers
))
246 (let* ((required (make-dfun-required-args nreq
))
249 (sb-c::%listify-rest-args
251 (the (and unsigned-byte fixnum
)
253 `(list ,@required
))))
254 `(named-lambda ,name
,ll
255 (declare (ignore .pv. .next-method-call.
))
256 (let ((.gf-args.
,gf-args
))
257 (declare (ignorable .gf-args.
))
258 ,@check-applicable-keywords
259 ,effective-method
))))
261 `(named-lambda ,name
,ll
262 (declare (ignore ,@(if error-p ll
'(.pv. .next-method-call.
))))
263 ,@check-applicable-keywords
264 ,effective-method
))))))
266 (defun expand-emf-call-method (gf form metatypes applyp env
)
267 (declare (ignore gf metatypes applyp env
))
268 `(call-method ,(cdr form
)))
270 (defmacro call-method
(&rest args
)
271 (declare (ignore args
))
272 ;; the PROGN is here to defend against premature macroexpansion by
274 `(progn (error "~S outside of a effective method form" 'call-method
)))
276 (defun make-effective-method-list-fun-type
277 (generic-function form method-alist-p wrappers-p
)
278 (if (every (lambda (form)
279 (eq 'fast-method-call
280 (make-effective-method-fun-type
281 generic-function form method-alist-p wrappers-p
)))
286 (defun memf-test-converter (form generic-function method-alist-p wrappers-p
)
287 (case (and (consp form
) (car form
))
289 (case (make-effective-method-fun-type
290 generic-function form method-alist-p wrappers-p
)
291 (fast-method-call '.fast-call-method.
)
294 (case (make-effective-method-list-fun-type
295 generic-function form method-alist-p wrappers-p
)
296 (fast-method-call '.fast-call-method-list.
)
297 (t '.call-method-list.
)))
298 (check-applicable-keywords 'check-applicable-keywords
)
299 (t (default-test-converter form
))))
301 ;;; CMUCL comment (2003-10-15):
303 ;;; This function is called via the GET-FUNCTION mechanism on forms
304 ;;; of an emf lambda. First value returned replaces FORM in the emf
305 ;;; lambda. Second value is a list of variable names that become
306 ;;; closure variables.
307 (defun memf-code-converter
308 (form generic-function metatypes applyp method-alist-p wrappers-p
)
309 (case (and (consp form
) (car form
))
311 (let ((gensym (get-effective-method-gensym)))
312 (values (make-emf-call
313 (length metatypes
) applyp gensym
314 (make-effective-method-fun-type
315 generic-function form method-alist-p wrappers-p
))
318 (let ((gensym (get-effective-method-gensym))
319 (type (make-effective-method-list-fun-type
320 generic-function form method-alist-p wrappers-p
)))
321 (values `(dolist (emf ,gensym nil
)
322 ,(make-emf-call (length metatypes
) applyp
'emf type
))
324 (check-applicable-keywords
325 (values `(check-applicable-keywords .keyargs-start.
329 '(.keyargs-start. .valid-keys.
)))
331 (default-code-converter form
))))
333 (defun memf-constant-converter (form generic-function
)
334 (case (and (consp form
) (car form
))
337 (make-effective-method-function-simple
338 generic-function form
))))
340 (list (cons '.meth-list.
341 (mapcar (lambda (form)
342 (make-effective-method-function-simple
343 generic-function form
))
345 (check-applicable-keywords
346 '(.keyargs-start. .valid-keys.
))
348 (default-constant-converter form
))))
350 (defvar *applicable-methods
*)
351 (defun make-effective-method-function-internal
352 (generic-function effective-method method-alist-p wrappers-p
)
353 (multiple-value-bind (nreq applyp metatypes nkeys arg-info
)
354 (get-generic-fun-info generic-function
)
355 (declare (ignore nkeys arg-info
))
356 (let* ((*rebound-effective-method-gensyms
*
357 *global-effective-method-gensyms
*)
358 (name (if (early-gf-p generic-function
)
359 (!early-gf-name generic-function
)
360 (generic-function-name generic-function
)))
361 (arg-info (cons nreq applyp
))
362 (effective-method-lambda (expand-effective-method-function
363 generic-function effective-method
)))
364 (multiple-value-bind (cfunction constants
)
365 (get-fun1 effective-method-lambda
367 (memf-test-converter form generic-function
368 method-alist-p wrappers-p
))
370 (memf-code-converter form generic-function
372 method-alist-p wrappers-p
))
374 (memf-constant-converter form generic-function
)))
375 (lambda (method-alist wrappers
)
376 (multiple-value-bind (valid-keys keyargs-start
)
377 (when (memq '.valid-keys. constants
)
378 (compute-applicable-keywords
379 generic-function
*applicable-methods
*))
380 (flet ((compute-constant (constant)
384 (funcall (cdr constant
) method-alist wrappers
))
387 (funcall fn method-alist wrappers
))
391 (.keyargs-start. keyargs-start
)
392 (.valid-keys. valid-keys
)
394 (let ((fun (apply cfunction
395 (mapcar #'compute-constant constants
))))
396 (set-fun-name fun
`(combined-method ,name
))
397 (make-fast-method-call :function fun
398 :arg-info arg-info
)))))))))
400 (defmacro call-method-list
(&rest calls
)
403 (defun make-call-methods (methods)
405 ,@(mapcar (lambda (method) `(call-method ,method
())) methods
)))
407 (defun gf-requires-emf-keyword-checks (generic-function)
408 (member '&key
(gf-lambda-list generic-function
)))
410 (defun standard-compute-effective-method
411 (generic-function combin applicable-methods
)
412 (collect ((before) (primary) (after) (around))
413 (flet ((invalid (gf combin m
) (invalid-qualifiers gf combin m
)))
414 (dolist (m applicable-methods
)
415 (let ((qualifiers (if (listp m
)
416 (early-method-qualifiers m
)
417 (safe-method-qualifiers m
))))
419 ((null qualifiers
) (primary m
))
420 ((cdr qualifiers
) (invalid generic-function combin m
))
421 ((eq (car qualifiers
) :around
) (around m
))
422 ((eq (car qualifiers
) :before
) (before m
))
423 ((eq (car qualifiers
) :after
) (after m
))
424 (t (invalid generic-function combin m
))))))
425 (cond ((null (primary))
426 `(%no-primary-method
',generic-function .args.
))
427 ((and (null (before)) (null (after)) (null (around)))
428 ;; By returning a single call-method `form' here we enable
429 ;; an important implementation-specific optimization; that
430 ;; is, we can use the fast method function directly as the
431 ;; effective method function.
433 ;; However, the requirement by ANSI (CLHS 7.6.5) on generic
434 ;; function argument checking inhibits this, as we don't
435 ;; perform this checking in fast-method-functions given
436 ;; that they are not solely used for effective method
437 ;; functions, but also in combination, when they should not
438 ;; perform argument checks.
440 `(call-method ,(first (primary)) ,(rest (primary)))))
441 (if (gf-requires-emf-keyword-checks generic-function
)
442 ;; the PROGN inhibits the above optimization
443 `(progn ,call-method
)
446 (let ((main-effective-method
447 (if (or (before) (after))
448 `(multiple-value-prog1
450 ,(make-call-methods (before))
451 (call-method ,(first (primary))
453 ,(make-call-methods (reverse (after))))
454 `(call-method ,(first (primary)) ,(rest (primary))))))
456 `(call-method ,(first (around))
458 (make-method ,main-effective-method
)))
459 main-effective-method
))))))
461 ;;; helper code for checking keywords in generic function calls.
462 (defun compute-applicable-keywords (gf methods
)
463 (let ((any-keyp nil
))
464 (flet ((analyze (lambda-list)
465 (multiple-value-bind (nreq nopt keyp restp allowp keys
)
466 (analyze-lambda-list lambda-list
)
467 (declare (ignore nreq restp
))
470 (values nopt allowp keys
))))
471 (multiple-value-bind (nopt allowp keys
)
472 (analyze (generic-function-lambda-list gf
))
473 (dolist (method methods
)
474 (let ((ll (if (consp method
)
475 (early-method-lambda-list method
)
476 (method-lambda-list method
))))
477 (multiple-value-bind (n allowp method-keys
)
481 (return-from compute-applicable-keywords
(values t nopt
)))
482 (setq keys
(union method-keys keys
)))))
484 (values (if allowp t keys
) nopt
)))))
486 (defun check-applicable-keywords (start valid-keys more-context more-count
)
487 (let ((allow-other-keys-seen nil
)
488 (allow-other-keys nil
)
490 (declare (type index i more-count
)
492 (flet ((current-value ()
493 (sb-c::%more-arg more-context i
)))
494 (declare (inline current-value
))
497 (when (>= i more-count
)
498 (when (and (invalid) (not allow-other-keys
))
499 (error 'simple-program-error
500 :format-control
"~@<invalid keyword argument~P: ~
501 ~{~S~^, ~} (valid keys are ~{~S~^, ~}).~@:>"
502 :format-arguments
(list (length (invalid)) (invalid) valid-keys
)))
504 (let ((key (current-value)))
508 (error 'simple-program-error
509 :format-control
"~@<keyword argument not a symbol: ~S.~@:>"
510 :format-arguments
(list key
)))
512 (sb-c::%odd-key-args-error
))
513 ((eq key
:allow-other-keys
)
514 ;; only the leftmost :ALLOW-OTHER-KEYS has any effect
515 (unless allow-other-keys-seen
516 (setq allow-other-keys-seen t
517 allow-other-keys
(current-value))))
519 ((not (memq key valid-keys
)) (invalid key
))))
522 ;;;; the STANDARD method combination type. This is coded by hand
523 ;;;; (rather than with DEFINE-METHOD-COMBINATION) for bootstrapping
524 ;;;; and efficiency reasons. Note that the definition of the
525 ;;;; FIND-METHOD-COMBINATION-METHOD appears in the file
526 ;;;; defcombin.lisp. This is because EQL methods can't appear in the
529 ;;;; The DEFCLASS for the METHOD-COMBINATION and
530 ;;;; STANDARD-METHOD-COMBINATION classes has to appear here for this
531 ;;;; reason. This code must conform to the code in the file
532 ;;;; defcombin.lisp, look there for more details.
534 (defun compute-effective-method (generic-function combin applicable-methods
)
535 (standard-compute-effective-method generic-function
539 (defun invalid-method-error (method format-control
&rest format-arguments
)
540 (let ((sb-debug:*stack-top-hint
* (nth-value 1 (find-caller-name-and-frame))))
541 (error "~@<invalid method error for ~2I~_~S ~I~_method: ~2I~_~?~:>"
546 (defun method-combination-error (format-control &rest format-arguments
)
547 (let ((sb-debug:*stack-top-hint
* (nth-value 1 (find-caller-name-and-frame))))
548 (error "~@<method combination error in CLOS dispatch: ~2I~_~?~:>"