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-function-pv-table fmf))))
35 (if (and fmf (or (null pv-table) wrappers))
36 (let* ((pv-wrappers (when pv-table
37 (pv-wrappers-from-all-wrappers
39 (pv-cell (when (and pv-table pv-wrappers)
40 (pv-table-lookup pv-table pv-wrappers))))
41 (values mf t fmf pv-cell))
43 (or mf (if (listp method)
45 (method-function-from-fast-function fmf))
46 (method-function method)))
49 (defun make-effective-method-function (generic-function form &optional
50 method-alist wrappers)
51 (funcall (make-effective-method-function1 generic-function form
52 (not (null method-alist))
53 (not (null wrappers)))
54 method-alist wrappers))
56 (defun make-effective-method-function1 (generic-function form
57 method-alist-p wrappers-p)
59 (eq (car form) 'call-method))
60 (make-effective-method-function-simple generic-function form)
61 ;; We have some sort of `real' effective method. Go off and get a
62 ;; compiled function for it. Most of the real hair here is done by
63 ;; the GET-FUN mechanism.
64 (make-effective-method-function-internal generic-function form
65 method-alist-p wrappers-p)))
67 (defun make-effective-method-fun-type (generic-function
72 (eq (car form) 'call-method))
73 (let* ((cm-args (cdr form))
74 (method (car cm-args)))
76 (if (if (listp method)
77 (eq (car method) :early-method)
81 (multiple-value-bind (mf fmf)
83 (early-method-function method)
84 (values nil (safe-method-fast-function method)))
86 (let* ((pv-table (and fmf (method-function-pv-table fmf))))
87 (if (and fmf (or (null pv-table) wrappers-p))
90 (if (and (consp method) (eq (car method) 'make-method))
91 (make-effective-method-fun-type
92 generic-function (cadr method) method-alist-p wrappers-p)
96 (defun make-effective-method-function-simple
97 (generic-function form &optional no-fmf-p)
98 ;; The effective method is just a call to CALL-METHOD. This opens up
99 ;; the possibility of just using the method function of the method as
100 ;; the effective method function.
102 ;; But we have to be careful. If that method function will ask for
103 ;; the next methods we have to provide them. We do not look to see
104 ;; if there are next methods, we look at whether the method function
105 ;; asks about them. If it does, we must tell it whether there are
106 ;; or aren't to prevent the leaky next methods bug.
107 (let* ((cm-args (cdr form))
108 (fmf-p (and (null no-fmf-p)
109 (or (not (eq *boot-state* 'complete))
110 (gf-fast-method-function-p generic-function))
111 (null (cddr cm-args))))
112 (method (car cm-args))
113 (cm-args1 (cdr cm-args)))
114 (lambda (method-alist wrappers)
115 (make-effective-method-function-simple1 generic-function
122 (defun make-emf-from-method
123 (method cm-args &optional gf fmf-p method-alist wrappers)
124 (multiple-value-bind (mf real-mf-p fmf pv-cell)
125 (get-method-function method method-alist wrappers)
127 (let* ((next-methods (car cm-args))
128 (next (make-effective-method-function-simple1
129 gf (car next-methods)
130 (list* (cdr next-methods) (cdr cm-args))
131 fmf-p method-alist wrappers))
132 (arg-info (method-function-get fmf :arg-info)))
133 (make-fast-method-call :function fmf
135 :next-method-call next
138 (flet ((frob-cm-arg (arg)
140 (eq (car arg) :early-method)
143 (if (and (consp arg) (eq (car arg) 'make-method))
144 (make-instance 'standard-method
145 :specializers nil ; XXX
147 :fast-function (fast-method-call-function
148 (make-effective-method-function
149 gf (cadr arg) method-alist wrappers)))
151 (make-method-call :function mf
152 ;; FIXME: this is wrong. Very wrong.
153 ;; It assumes that the only place that
154 ;; can have make-method calls is in
155 ;; the list structure of the second
156 ;; argument to CALL-METHOD, but AMOP
157 ;; says that CALL-METHOD can be more
159 ;; COMPUTE-EFFECTIVE-METHOD (and
160 ;; presumably MAKE-METHOD-LAMBDA) is
161 ;; adjusted to match.
163 ;; On the other hand, it's a start,
164 ;; because without this calls to
165 ;; MAKE-METHOD in method combination
166 ;; where one of the methods is of a
167 ;; user-defined class don't work at
168 ;; all. -- CSR, 2006-08-05
169 :call-method-args (cons (mapcar #'frob-cm-arg (car cm-args))
173 (defun make-effective-method-function-simple1
174 (gf method cm-args fmf-p &optional method-alist wrappers)
176 (if (if (listp method)
177 (eq (car method) :early-method)
179 (make-emf-from-method method cm-args gf fmf-p method-alist wrappers)
180 (if (and (consp method) (eq (car method) 'make-method))
181 (make-effective-method-function gf
183 method-alist wrappers)
186 (defvar *global-effective-method-gensyms* ())
187 (defvar *rebound-effective-method-gensyms*)
189 (defun get-effective-method-gensym ()
190 (or (pop *rebound-effective-method-gensyms*)
191 (let ((new (format-symbol *pcl-package*
192 "EFFECTIVE-METHOD-GENSYM-~D"
193 (length *global-effective-method-gensyms*))))
194 (setq *global-effective-method-gensyms*
195 (append *global-effective-method-gensyms* (list new)))
198 (let ((*rebound-effective-method-gensyms* ()))
199 (dotimes-fixnum (i 10) (get-effective-method-gensym)))
201 (defun expand-effective-method-function (gf effective-method &optional env)
202 (declare (ignore env))
203 (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
204 (get-generic-fun-info gf)
205 (declare (ignore nreq nkeys arg-info))
206 (let ((ll (make-fast-method-call-lambda-list metatypes applyp))
207 (check-applicable-keywords
208 (when (and applyp (gf-requires-emf-keyword-checks gf))
209 '((check-applicable-keywords))))
210 (error-p (or (eq (first effective-method) '%no-primary-method)
211 (eq (first effective-method) '%invalid-qualifiers)))
213 (when (eq *boot-state* 'complete)
214 ;; Otherwise the METHOD-COMBINATION slot is not bound.
215 (let ((combin (generic-function-method-combination gf)))
216 (and (long-method-combination-p combin)
217 (long-method-combination-args-lambda-list combin))))))
220 `(lambda (.pv-cell. .next-method-call. &rest .args.)
221 (declare (ignore .pv-cell. .next-method-call.))
222 (declare (ignorable .args.))
223 (flet ((%no-primary-method (gf args)
224 (apply #'no-primary-method gf args))
225 (%invalid-qualifiers (gf combin method)
226 (invalid-qualifiers gf combin method)))
227 (declare (ignorable #'%no-primary-method #'%invalid-qualifiers))
231 ;; FIXME: Ick. Shared idiom, too, with stuff in cache.lisp
233 (dotimes (i (length metatypes) (nreverse req))
234 (push (dfun-arg-symbol i) req))))
236 `(list* ,@required .dfun-rest-arg.)
237 `(list ,@required))))
239 (declare (ignore .pv-cell. .next-method-call.))
240 (let ((.gf-args. ,gf-args))
241 (declare (ignorable .gf-args.))
242 ,@check-applicable-keywords
243 ,effective-method))))
246 (declare (ignore ,@(if error-p ll '(.pv-cell. .next-method-call.))))
247 ,@check-applicable-keywords
248 ,effective-method))))))
250 (defun expand-emf-call-method (gf form metatypes applyp env)
251 (declare (ignore gf metatypes applyp env))
252 `(call-method ,(cdr form)))
254 (defmacro call-method (&rest args)
255 (declare (ignore args))
256 ;; the PROGN is here to defend against premature macroexpansion by
258 `(progn (error "~S outside of a effective method form" 'call-method)))
260 (defun make-effective-method-list-fun-type
261 (generic-function form method-alist-p wrappers-p)
262 (if (every (lambda (form)
263 (eq 'fast-method-call
264 (make-effective-method-fun-type
265 generic-function form method-alist-p wrappers-p)))
270 (defun memf-test-converter (form generic-function method-alist-p wrappers-p)
271 (case (and (consp form) (car form))
273 (case (make-effective-method-fun-type
274 generic-function form method-alist-p wrappers-p)
275 (fast-method-call '.fast-call-method.)
278 (case (make-effective-method-list-fun-type
279 generic-function form method-alist-p wrappers-p)
280 (fast-method-call '.fast-call-method-list.)
281 (t '.call-method-list.)))
282 (check-applicable-keywords 'check-applicable-keywords)
283 (t (default-test-converter form))))
285 ;;; CMUCL comment (2003-10-15):
287 ;;; This function is called via the GET-FUNCTION mechanism on forms
288 ;;; of an emf lambda. First value returned replaces FORM in the emf
289 ;;; lambda. Second value is a list of variable names that become
290 ;;; closure variables.
291 (defun memf-code-converter
292 (form generic-function metatypes applyp method-alist-p wrappers-p)
293 (case (and (consp form) (car form))
295 (let ((gensym (get-effective-method-gensym)))
296 (values (make-emf-call
297 metatypes applyp gensym
298 (make-effective-method-fun-type
299 generic-function form method-alist-p wrappers-p))
302 (let ((gensym (get-effective-method-gensym))
303 (type (make-effective-method-list-fun-type
304 generic-function form method-alist-p wrappers-p)))
305 (values `(dolist (emf ,gensym nil)
306 ,(make-emf-call metatypes applyp 'emf type))
308 (check-applicable-keywords
309 (values `(check-applicable-keywords
310 .dfun-rest-arg. .keyargs-start. .valid-keys.)
311 '(.keyargs-start. .valid-keys.)))
314 (default-code-converter form))))
316 (defun memf-constant-converter (form generic-function)
317 (case (and (consp form) (car form))
320 (make-effective-method-function-simple
321 generic-function form))))
323 (list (cons '.meth-list.
324 (mapcar (lambda (form)
325 (make-effective-method-function-simple
326 generic-function form))
328 (check-applicable-keywords
329 '(.keyargs-start. .valid-keys.))
331 (default-constant-converter form))))
333 (defvar *applicable-methods*)
334 (defun make-effective-method-function-internal
335 (generic-function effective-method method-alist-p wrappers-p)
336 (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
337 (get-generic-fun-info generic-function)
338 (declare (ignore nkeys arg-info))
339 (let* ((*rebound-effective-method-gensyms*
340 *global-effective-method-gensyms*)
341 (name (if (early-gf-p generic-function)
342 (!early-gf-name generic-function)
343 (generic-function-name generic-function)))
344 (arg-info (cons nreq applyp))
345 (effective-method-lambda (expand-effective-method-function
346 generic-function effective-method)))
347 (multiple-value-bind (cfunction constants)
348 (get-fun1 effective-method-lambda
350 (memf-test-converter form generic-function
351 method-alist-p wrappers-p))
353 (memf-code-converter form generic-function
355 method-alist-p wrappers-p))
357 (memf-constant-converter form generic-function)))
358 (lambda (method-alist wrappers)
359 (multiple-value-bind (valid-keys keyargs-start)
360 (when (memq '.valid-keys. constants)
361 (compute-applicable-keywords
362 generic-function *applicable-methods*))
363 (flet ((compute-constant (constant)
367 (funcall (cdr constant) method-alist wrappers))
370 (funcall fn method-alist wrappers))
374 (.keyargs-start. keyargs-start)
375 (.valid-keys. valid-keys)
377 (let ((fun (apply cfunction
378 (mapcar #'compute-constant constants))))
379 (set-fun-name fun `(combined-method ,name))
380 (make-fast-method-call :function fun
381 :arg-info arg-info)))))))))
383 (defmacro call-method-list (&rest calls)
386 (defun make-call-methods (methods)
388 ,@(mapcar (lambda (method) `(call-method ,method ())) methods)))
390 (defun gf-requires-emf-keyword-checks (generic-function)
391 (member '&key (gf-lambda-list generic-function)))
393 (defvar *in-precompute-effective-methods-p* nil)
395 (defun standard-compute-effective-method
396 (generic-function combin applicable-methods)
397 (collect ((before) (primary) (after) (around))
398 (flet ((invalid (gf combin m)
399 (if *in-precompute-effective-methods-p*
400 (return-from standard-compute-effective-method
401 `(%invalid-qualifiers ',gf ',combin ',m))
402 (invalid-qualifiers gf combin m))))
403 (dolist (m applicable-methods)
404 (let ((qualifiers (if (listp m)
405 (early-method-qualifiers m)
406 (method-qualifiers m))))
408 ((null qualifiers) (primary m))
409 ((cdr qualifiers) (invalid generic-function combin m))
410 ((eq (car qualifiers) :around) (around m))
411 ((eq (car qualifiers) :before) (before m))
412 ((eq (car qualifiers) :after) (after m))
413 (t (invalid generic-function combin m))))))
414 (cond ((null (primary))
415 `(%no-primary-method ',generic-function .args.))
416 ((and (null (before)) (null (after)) (null (around)))
417 ;; By returning a single call-method `form' here we enable
418 ;; an important implementation-specific optimization; that
419 ;; is, we can use the fast method function directly as the
420 ;; effective method function.
422 ;; However, the requirement by ANSI (CLHS 7.6.5) on generic
423 ;; function argument checking inhibits this, as we don't
424 ;; perform this checking in fast-method-functions given
425 ;; that they are not solely used for effective method
426 ;; functions, but also in combination, when they should not
427 ;; perform argument checks.
429 `(call-method ,(first (primary)) ,(rest (primary)))))
430 (if (gf-requires-emf-keyword-checks generic-function)
431 ;; the PROGN inhibits the above optimization
432 `(progn ,call-method)
435 (let ((main-effective-method
436 (if (or (before) (after))
437 `(multiple-value-prog1
439 ,(make-call-methods (before))
440 (call-method ,(first (primary))
442 ,(make-call-methods (reverse (after))))
443 `(call-method ,(first (primary)) ,(rest (primary))))))
445 `(call-method ,(first (around))
447 (make-method ,main-effective-method)))
448 main-effective-method))))))
450 ;;; helper code for checking keywords in generic function calls.
451 (defun compute-applicable-keywords (gf methods)
452 (let ((any-keyp nil))
453 (flet ((analyze (lambda-list)
454 (multiple-value-bind (nreq nopt keyp restp allowp keys)
455 (analyze-lambda-list lambda-list)
456 (declare (ignore nreq restp))
459 (values nopt allowp keys))))
460 (multiple-value-bind (nopt allowp keys)
461 (analyze (generic-function-lambda-list gf))
462 (dolist (method methods)
463 (let ((ll (if (consp method)
464 (early-method-lambda-list method)
465 (method-lambda-list method))))
466 (multiple-value-bind (n allowp method-keys)
470 (return-from compute-applicable-keywords (values t nopt)))
471 (setq keys (union method-keys keys)))))
473 (values (if allowp t keys) nopt)))))
475 (defun check-applicable-keywords (args start valid-keys)
476 (let ((allow-other-keys-seen nil)
477 (allow-other-keys nil)
478 (args (nthcdr start args)))
482 (when (and (invalid) (not allow-other-keys))
483 (error 'simple-program-error
484 :format-control "~@<invalid keyword argument~P: ~
485 ~{~S~^, ~} (valid keys are ~{~S~^, ~}).~@:>"
486 :format-arguments (list (length (invalid)) (invalid) valid-keys)))
488 (let ((key (pop args)))
491 (error 'simple-program-error
492 :format-control "~@<keyword argument not a symbol: ~S.~@:>"
493 :format-arguments (list key)))
494 ((null args) (sb-c::%odd-key-args-error))
495 ((eq key :allow-other-keys)
496 ;; only the leftmost :ALLOW-OTHER-KEYS has any effect
497 (unless allow-other-keys-seen
498 (setq allow-other-keys-seen t
499 allow-other-keys (car args))))
501 ((not (memq key valid-keys)) (invalid key))))
504 ;;;; the STANDARD method combination type. This is coded by hand
505 ;;;; (rather than with DEFINE-METHOD-COMBINATION) for bootstrapping
506 ;;;; and efficiency reasons. Note that the definition of the
507 ;;;; FIND-METHOD-COMBINATION-METHOD appears in the file
508 ;;;; defcombin.lisp. This is because EQL methods can't appear in the
511 ;;;; The DEFCLASS for the METHOD-COMBINATION and
512 ;;;; STANDARD-METHOD-COMBINATION classes has to appear here for this
513 ;;;; reason. This code must conform to the code in the file
514 ;;;; defcombin.lisp, look there for more details.
516 (defun compute-effective-method (generic-function combin applicable-methods)
517 (standard-compute-effective-method generic-function
521 (defun invalid-method-error (method format-control &rest format-arguments)
522 (let ((sb-debug:*stack-top-hint* (nth-value 1 (find-caller-name-and-frame))))
523 (error "~@<invalid method error for ~2I~_~S ~I~_method: ~2I~_~?~:>"
528 (defun method-combination-error (format-control &rest format-arguments)
529 (let ((sb-debug:*stack-top-hint* (nth-value 1 (find-caller-name-and-frame))))
530 (error "~@<method combination error in CLOS dispatch: ~2I~_~?~:>"