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
28 The CommonLoops evaluator is meta-circular.
30 Most of the code in PCL is methods on generic functions
, including
31 most of the code that actually implements generic functions and method
34 So
, we have a classic bootstrapping problem. The solution to this is
35 to first get a cheap implementation of generic functions running
,
36 these are called early generic functions. These early generic
37 functions and the corresponding early methods and early method lookup
38 are used to get enough of the system running that it is possible to
39 create real generic functions and methods and implement real method
40 lookup. At that point
(done in the file FIXUP
) the function
41 !FIX-EARLY-GENERIC-FUNCTIONS is called to convert all the early generic
42 functions to real generic functions.
44 The cheap generic functions are built using the same
45 FUNCALLABLE-INSTANCE objects that real generic functions are made out of.
46 This means that as PCL is being bootstrapped
, the cheap generic
47 function objects which are being created are the same objects which
48 will later be real generic functions. This is good because
:
49 - we don
't cons garbage structure
, and
50 - we can keep pointers to the cheap generic function objects
51 during booting because those pointers will still point to
52 the right object after the generic functions are all fixed up.
54 This file defines the DEFMETHOD macro and the mechanism used to expand
55 it. This includes the mechanism for processing the body of a method.
56 DEFMETHOD basically expands into a call to LOAD-DEFMETHOD
, which
57 basically calls ADD-METHOD to add the method to the generic function.
58 These expansions can be loaded either during bootstrapping or when PCL
59 is fully up and running.
61 An important effect of this arrangement is it means we can compile
62 files with DEFMETHOD forms in them in a completely running PCL
, but
63 then load those files back in during bootstrapping. This makes
64 development easier. It also means there is only one set of code for
65 processing DEFMETHOD. Bootstrapping works by being sure to have
66 LOAD-METHOD be careful to call only primitives which work during
71 (declaim (notinline make-a-method add-named-method
72 ensure-generic-function-using-class
73 add-method remove-method
))
75 (defvar *!early-functions
*
76 '((make-a-method !early-make-a-method real-make-a-method
)
77 (add-named-method !early-add-named-method real-add-named-method
)))
79 ;;; For each of the early functions, arrange to have it point to its
80 ;;; early definition. Do this in a way that makes sure that if we
81 ;;; redefine one of the early definitions the redefinition will take
82 ;;; effect. This makes development easier.
83 (loop for
(name early-name
) in
*!early-functions
*
84 do
(let ((early-name early-name
))
85 (setf (gdefinition name
)
88 (apply (fdefinition early-name
) args
))
91 ;;; *!GENERIC-FUNCTION-FIXUPS* is used by !FIX-EARLY-GENERIC-FUNCTIONS
92 ;;; to convert the few functions in the bootstrap which are supposed
93 ;;; to be generic functions but can't be early on.
95 ;;; each entry is a list of the form
97 ;;; (GENERIC-FUNCTION-NAME METHOD-COMBINATION-NAME METHODS)
99 ;;; where methods is a list of lists of the form
101 ;;; (LAMBDA-LIST SPECIALIZERS QUALIFIERS METHOD-BODY-FUNCTION-NAME)
103 ;;;,where SPECIALIZERS is a list of class names.
104 (defvar *!generic-function-fixups
*
107 ((generic-function method
)
108 (standard-generic-function method
)
114 ((generic-function method
)
115 (standard-generic-function method
)
121 ((generic-function qualifiers specializers
&optional
(errorp t
))
122 (standard-generic-function t t
)
126 (ensure-generic-function-using-class
128 ((generic-function fun-name
129 &key generic-function-class environment
133 real-ensure-gf-using-class--generic-function
)
134 ((generic-function fun-name
135 &key generic-function-class environment
139 real-ensure-gf-using-class--null
))
143 ((proto-generic-function proto-method lambda-expression environment
)
144 (standard-generic-function standard-method t t
)
146 real-make-method-lambda
))
148 (make-method-lambda-using-specializers
150 ((proto-generic-function proto-method qualifiers specializers
151 lambda-expression environment
)
152 (standard-generic-function standard-method t t t t
)
154 real-make-method-lambda-using-specializers
))
156 (make-method-specializers-form
158 ((proto-generic-function proto-method specializer-names environment
)
159 (standard-generic-function standard-method t t
)
161 real-make-method-specializers-form
))
163 (make-specializer-form-using-class
165 ((proto-generic-function proto-method specializer-name environment
)
166 (standard-generic-function standard-method t t
)
168 real-make-specializer-form-using-class
/t
)
169 ((proto-generic-function proto-method specializer-name environment
)
170 (standard-generic-function standard-method specializer t
)
172 real-make-specializer-form-using-class
/specializer
)
173 ((proto-generic-function proto-method specializer-name environment
)
174 (standard-generic-function standard-method symbol t
)
176 real-make-specializer-form-using-class
/symbol
)
177 ((proto-generic-function proto-method specializer-name environment
)
178 (standard-generic-function standard-method cons t
)
180 real-make-specializer-form-using-class
/cons
))
182 (specializer-type-specifier
184 ((proto-generic-function proto-method specializer
)
185 (standard-generic-function standard-method specializer
)
187 real-specializer-type-specifier
/specializer
)
188 ((proto-generic-function proto-method specializer
)
189 (standard-generic-function standard-method symbol
)
191 real-specializer-type-specifier
/symbol
)
192 ((proto-generic-function proto-method specializer
)
193 (standard-generic-function standard-method t
)
195 real-specializer-type-specifier
/t
)
196 ((proto-generic-function proto-method specializer
)
197 (standard-generic-function standard-method class-eq-specializer
)
199 real-specializer-type-specifier
/class-eq-specializer
)
200 ((proto-generic-function proto-method specializer
)
201 (standard-generic-function standard-method eql-specializer
)
203 real-specializer-type-specifier
/eql-specializer
)
204 ((proto-generic-function proto-method specializer
)
205 (standard-generic-function standard-method structure-class
)
207 real-specializer-type-specifier
/structure-class
)
208 ((proto-generic-function proto-method specializer
)
209 (standard-generic-function standard-method system-class
)
211 real-specializer-type-specifier
/system-class
)
212 ((proto-generic-function proto-method specializer
)
213 (standard-generic-function standard-method class
)
215 real-specializer-type-specifier
/class
))
217 (parse-specializer-using-class
219 ((generic-function specializer
)
220 (standard-generic-function t
)
222 real-parse-specializer-using-class
))
224 (unparse-specializer-using-class
226 ((generic-function specializer
)
227 (standard-generic-function t
)
229 real-unparse-specializer-using-class
))
231 (make-method-initargs-form
233 ((proto-generic-function proto-method
235 lambda-list environment
)
236 (standard-generic-function standard-method t t t
)
238 real-make-method-initargs-form
))
240 (compute-effective-method
242 ((generic-function combin applicable-methods
)
243 (generic-function standard-method-combination t
)
245 standard-compute-effective-method
)
246 ((generic-function combin applicable-methods
)
247 (generic-function short-method-combination t
)
249 short-compute-effective-method
))))
251 (defmacro defgeneric
(fun-name lambda-list
&body options
)
252 (declare (type list lambda-list
))
253 (unless (legal-fun-name-p fun-name
)
254 (error 'simple-program-error
255 :format-control
"illegal generic function name ~S"
256 :format-arguments
(list fun-name
)))
257 (check-gf-lambda-list lambda-list
)
260 (flet ((duplicate-option (name)
261 (error 'simple-program-error
262 :format-control
"The option ~S appears more than once."
263 :format-arguments
(list name
)))
264 (expand-method-definition (qab) ; QAB = qualifiers, arglist, body
265 (let* ((arglist-pos (position-if #'listp qab
))
266 (arglist (elt qab arglist-pos
))
267 (qualifiers (subseq qab
0 arglist-pos
))
268 (body (nthcdr (1+ arglist-pos
) qab
)))
269 `(push (defmethod ,fun-name
,@qualifiers
,arglist
,@body
)
270 (generic-function-initial-methods (fdefinition ',fun-name
))))))
271 (macrolet ((initarg (key) `(getf initargs
,key
)))
272 (dolist (option options
)
273 (let ((car-option (car option
)))
276 (dolist (spec (cdr option
))
278 (error 'simple-program-error
279 :format-control
"~@<Invalid declaration specifier in ~
281 :format-arguments
(list spec
)))
282 (when (member (first spec
)
283 ;; FIXME: this list is slightly weird.
284 ;; ANSI (on the DEFGENERIC page) in one
285 ;; place allows only OPTIMIZE; in
286 ;; another place gives this list of
287 ;; disallowed declaration specifiers.
288 ;; This seems to be the only place where
289 ;; the FUNCTION declaration is
290 ;; mentioned; TYPE seems to be missing.
291 ;; Very strange. -- CSR, 2002-10-21
292 '(declaration ftype function
293 inline notinline special
))
294 (error 'simple-program-error
295 :format-control
"The declaration specifier ~S ~
296 is not allowed inside DEFGENERIC."
297 :format-arguments
(list spec
)))
298 (if (or (eq 'optimize
(first spec
))
299 (info :declaration
:recognized
(first spec
)))
300 (push spec
(initarg :declarations
))
301 (warn "Ignoring unrecognized declaration in DEFGENERIC: ~S"
304 (when (initarg car-option
)
305 (duplicate-option car-option
))
306 (unless (symbolp (cadr option
))
307 (error 'simple-program-error
308 :format-control
"METHOD-COMBINATION name not a ~
310 :format-arguments
(list (cadr option
))))
311 (setf (initarg car-option
)
313 (:argument-precedence-order
314 (let* ((required (nth-value 1 (parse-lambda-list lambda-list
)))
315 (supplied (cdr option
)))
316 (unless (= (length required
) (length supplied
))
317 (error 'simple-program-error
318 :format-control
"argument count discrepancy in ~
319 :ARGUMENT-PRECEDENCE-ORDER clause."
320 :format-arguments nil
))
321 (when (set-difference required supplied
)
322 (error 'simple-program-error
323 :format-control
"unequal sets for ~
324 :ARGUMENT-PRECEDENCE-ORDER clause: ~
326 :format-arguments
(list required supplied
)))
327 (setf (initarg car-option
)
329 ((:documentation
:generic-function-class
:method-class
)
330 (unless (proper-list-of-length-p option
2)
331 (error "bad list length for ~S" option
))
332 (if (initarg car-option
)
333 (duplicate-option car-option
)
334 (setf (initarg car-option
) `',(cadr option
))))
336 (push (cdr option
) methods
))
338 ;; ANSI requires that unsupported things must get a
340 (error 'simple-program-error
341 :format-control
"unsupported option ~S"
342 :format-arguments
(list option
))))))
344 (when (initarg :declarations
)
345 (setf (initarg :declarations
)
346 `',(initarg :declarations
))))
348 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
349 (compile-or-load-defgeneric ',fun-name
))
350 (load-defgeneric ',fun-name
',lambda-list
351 (sb-c:source-location
) ,@initargs
)
352 ,@(mapcar #'expand-method-definition methods
)
353 (fdefinition ',fun-name
)))))
355 (defun compile-or-load-defgeneric (fun-name)
356 (proclaim-as-fun-name fun-name
)
357 (when (typep fun-name
'(cons (eql setf
)))
358 (sb-c::warn-if-setf-macro fun-name
))
359 (note-name-defined fun-name
:function
)
360 (unless (eq (info :function
:where-from fun-name
) :declared
)
361 ;; Hmm. This is similar to BECOME-DEFINED-FUN-NAME
362 ;; except that it doesn't clear an :ASSUMED-TYPE. Should it?
363 (setf (info :function
:where-from fun-name
) :defined
)
364 (setf (info :function
:type fun-name
)
365 (specifier-type 'function
))))
367 (defun load-defgeneric (fun-name lambda-list source-location
&rest initargs
)
368 (when (fboundp fun-name
)
369 (warn 'sb-kernel
:redefinition-with-defgeneric
371 :new-location source-location
)
372 (let ((fun (fdefinition fun-name
)))
373 (when (generic-function-p fun
)
374 (loop for method in
(generic-function-initial-methods fun
)
375 do
(remove-method fun method
))
376 (setf (generic-function-initial-methods fun
) '()))))
377 (apply #'ensure-generic-function
379 :lambda-list lambda-list
380 :definition-source source-location
383 (define-condition generic-function-lambda-list-error
384 (reference-condition simple-program-error
)
386 (:default-initargs
:references
(list '(:ansi-cl
:section
(3 4 2)))))
388 (defun check-gf-lambda-list (lambda-list)
389 (flet ((verify-each-atom-or-singleton (kind args
)
390 ;; PARSE-LAMBDA-LIST validates the skeleton,
391 ;; so just check for incorrect use of defaults.
392 ;; This works for both &OPTIONAL and &KEY.
394 (or (not (listp arg
))
396 (error 'generic-function-lambda-list-error
398 "~@<invalid ~A argument specifier ~S ~_in the ~
399 generic function lambda list ~S~:>"
400 :format-arguments
(list kind arg lambda-list
))))))
401 (multiple-value-bind (llks required optional rest keys
)
404 :accept
(lambda-list-keyword-mask
405 '(&optional
&rest
&key
&allow-other-keys
))
406 :condition-class
'generic-function-lambda-list-error
407 :context
"a generic function lambda list")
408 (declare (ignore llks required rest
))
409 ;; no defaults or supplied-p vars allowed for &OPTIONAL or &KEY
410 (verify-each-atom-or-singleton '&optional optional
)
411 (verify-each-atom-or-singleton '&key keys
))))
413 (defun check-method-lambda (method-lambda context
)
414 (unless (typep method-lambda
'(cons (eql lambda
)))
415 (error "~@<The METHOD-LAMBDA argument to ~
416 ~/sb-impl:print-symbol-with-prefix/, ~S, is not a lambda ~
418 context method-lambda
))
421 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
422 (fmakunbound 'defmethod
))
424 ;;; "defmethod is not required to perform any compile-time side effects."
425 ;;; and we don't do much other than to make the function be defined,
426 ;;; which means that checking of callers' arglists can only occur after called
427 ;;; methods are actually loaded.
428 (defmacro defmethod
(name &rest args
)
429 (multiple-value-bind (qualifiers lambda-list body
)
430 (parse-defmethod args)
432 (eval-when (:compile-toplevel
:execute
)
433 ;; :compile-toplevel is needed for subsequent forms
434 ;; :execute is needed for references to itself inside the body
435 (compile-or-load-defgeneric ',name
))
436 ;; KLUDGE: this double expansion is quite a monumental
437 ;; workaround: it comes about because of a fantastic interaction
438 ;; between the processing rules of CLHS 3.2.3.1 and the
439 ;; bizarreness of MAKE-METHOD-LAMBDA.
441 ;; MAKE-METHOD-LAMBDA can be called by the user, and if the
442 ;; lambda itself doesn't refer to outside bindings the return
443 ;; value must be compileable in the null lexical environment.
444 ;; However, the function must also refer somehow to the
445 ;; associated method object, so that it can call NO-NEXT-METHOD
446 ;; with the appropriate arguments if there is no next method --
447 ;; but when the function is generated, the method object doesn't
450 ;; In order to resolve this issue, we insert a literal cons cell
451 ;; into the body of the method lambda, return the same cons cell
452 ;; as part of the second (initargs) return value of
453 ;; MAKE-METHOD-LAMBDA, and a method on INITIALIZE-INSTANCE fills
454 ;; in the cell when the method is created. However, this
455 ;; strategy depends on having a fresh cons cell for every method
456 ;; lambda, which (without the workaround below) is skewered by
457 ;; the processing in CLHS 3.2.3.1, which permits implementations
458 ;; to macroexpand the bodies of EVAL-WHEN forms with both
459 ;; :COMPILE-TOPLEVEL and :LOAD-TOPLEVEL only once. The
460 ;; expansion below forces the double expansion in those cases,
461 ;; while expanding only once in the common case.
462 (eval-when (:load-toplevel
)
463 (%defmethod-expander
,name
,qualifiers
,lambda-list
,body
))
464 (eval-when (:execute
)
465 (%defmethod-expander
,name
,qualifiers
,lambda-list
,body
)))))
467 (defmacro %defmethod-expander
468 (name qualifiers lambda-list body
&environment env
)
469 (multiple-value-bind (proto-gf proto-method
)
470 (prototypes-for-make-method-lambda name
)
471 (expand-defmethod name proto-gf proto-method qualifiers
472 lambda-list body env
)))
475 (defun prototypes-for-make-method-lambda (name)
476 (if (not (eq **boot-state
** 'complete
))
478 (let ((gf?
(and (fboundp name
)
479 (gdefinition name
))))
481 (not (generic-function-p gf?
)))
482 (values (class-prototype (find-class 'standard-generic-function
))
483 (class-prototype (find-class 'standard-method
)))
485 (class-prototype (or (generic-function-method-class gf?
)
486 (find-class 'standard-method
))))))))
488 ;;; Take a name which is either a generic function name or a list specifying
489 ;;; a SETF generic function (like: (SETF <generic-function-name>)). Return
490 ;;; the prototype instance of the method-class for that generic function.
492 ;;; If there is no generic function by that name, this returns the
493 ;;; default value, the prototype instance of the class
494 ;;; STANDARD-METHOD. This default value is also returned if the spec
495 ;;; names an ordinary function or even a macro. In effect, this leaves
496 ;;; the signalling of the appropriate error until load time.
498 ;;; Note: During bootstrapping, this function is allowed to return NIL.
499 (defun method-prototype-for-gf (name)
500 (let ((gf?
(and (fboundp name
)
501 (gdefinition name
))))
502 (cond ((neq **boot-state
** 'complete
) nil
)
504 (not (generic-function-p gf?
))) ; Someone else MIGHT
505 ; error at load time.
506 (class-prototype (find-class 'standard-method
)))
508 (class-prototype (or (generic-function-method-class gf?
)
509 (find-class 'standard-method
)))))))
511 ;;; These are used to communicate the method name and lambda-list to
512 ;;; MAKE-METHOD-LAMBDA-INTERNAL.
513 (defvar *method-name
* nil
)
514 (defvar *method-lambda-list
* nil
)
516 (defun expand-defmethod (name proto-gf proto-method qualifiers lambda-list
518 (binding* (;; ENV could be of type SB!INTERPRETER:BASIC-ENV but I
519 ;; don't care to figure out what parts of PCL would have
520 ;; to change to accept that, so coerce.
521 (env (sb-kernel:coerce-to-lexenv env
))
522 ((nil unspecialized-lambda-list specializers
)
523 (parse-specialized-lambda-list lambda-list
))
524 (*method-name
* `(,name
,@qualifiers
,specializers
))
525 (method-lambda `(lambda ,unspecialized-lambda-list
,@body
))
526 ((method-function-lambda initargs new-lambda-list
)
527 (make-method-lambda-using-specializers
528 proto-gf proto-method qualifiers specializers method-lambda env
))
530 (make-method-initargs-form
531 proto-gf proto-method method-function-lambda initargs env
))
533 (make-method-specializers-form
534 proto-gf proto-method specializers env
)))
535 (mapc (lambda (specializer parameter
)
536 (when (typep specializer
'type-specifier
)
537 (with-current-source-form (parameter)
538 (check-deprecated-type specializer
))))
539 specializers lambda-list
)
540 ;; Note: We could DECLAIM the ftype of the generic function here,
541 ;; since ANSI specifies that we create it if it does not
542 ;; exist. However, I chose not to, because I think it's more
543 ;; useful to support a style of programming where every generic
544 ;; function has an explicit DEFGENERIC and any typos in DEFMETHODs
545 ;; are warned about. Otherwise
547 ;; (DEFGENERIC FOO-BAR-BLETCH (X))
548 ;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
549 ;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
550 ;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
551 ;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
552 ;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
554 ;; compiles without raising an error and runs without raising an
555 ;; error (since SIMPLE-VECTOR cases fall through to VECTOR) but
556 ;; still doesn't do what was intended. I hate that kind of bug
557 ;; (code which silently gives the wrong answer), so we don't do a
558 ;; DECLAIM here. -- WHN 20000229
559 (make-defmethod-form name qualifiers specializers-form
560 (or new-lambda-list unspecialized-lambda-list
)
562 (class-name (class-of proto-method
))
566 (defun interned-symbol-p (x)
567 (and (symbolp x
) (symbol-package x
)))
569 (defun make-defmethod-form
570 (name qualifiers specializers unspecialized-lambda-list
571 method-class-name initargs-form
)
572 (declare (sb-ext:muffle-conditions sb-ext
:code-deletion-note
))
575 (if (and (interned-symbol-p (fun-name-block-name name
))
576 (every #'interned-symbol-p qualifiers
)
579 (and (eq (car s
) 'eql
)
581 (let ((sv (constant-form-value (cadr s
))))
582 (or (interned-symbol-p sv
)
585 (standard-char-p sv
)))))
586 (interned-symbol-p s
)))
588 (consp initargs-form
)
589 (eq (car initargs-form
) 'list
*)
590 (memq (cadr initargs-form
) '(:function
))
591 (consp (setq fn
(caddr initargs-form
)))
592 (eq (car fn
) 'function
)
593 (consp (setq fn-lambda
(cadr fn
)))
594 (eq (car fn-lambda
) 'lambda
)
595 (bug "Really got here"))
596 (let* ((specls (mapcar (lambda (specl)
598 ;; CONSTANT-FORM-VALUE? What I
599 ;; kind of want to know, though,
600 ;; is what happens if we don't do
601 ;; this for some slow-method
602 ;; function because of a hairy
603 ;; lexenv -- is the only bad
604 ;; effect that the method
605 ;; function ends up unnamed? If
606 ;; so, couldn't we arrange to
608 `(,(car specl
) ,(eval (cadr specl
)))
611 (mname `(,(if (eq (cadr initargs-form
) :function
)
612 'slow-method
'fast-method
)
613 ,name
,@qualifiers
,specls
)))
615 (defun ,mname
,(cadr fn-lambda
)
617 ,(make-defmethod-form-internal
618 name qualifiers
`',specls
619 unspecialized-lambda-list method-class-name
620 `(list* ,(cadr initargs-form
)
622 ,@(cdddr initargs-form
)))))
623 (make-defmethod-form-internal
627 `(list ,@(mapcar (lambda (specializer)
628 (if (consp specializer
)
629 ``(,',(car specializer
)
630 ,,(cadr specializer
))
633 unspecialized-lambda-list
637 (defun make-defmethod-form-internal
638 (name qualifiers specializers-form unspecialized-lambda-list
639 method-class-name initargs-form
)
645 ',unspecialized-lambda-list
647 (sb-c:source-location
)))
649 (defmacro make-method-function
(method-lambda &environment env
)
650 (binding* (((proto-gf proto-method
)
651 (prototypes-for-make-method-lambda nil
))
652 ((method-function-lambda initargs
)
653 (make-method-lambda proto-gf proto-method method-lambda env
))) ; FIXME: coerce-to-lexenv?
654 (make-method-initargs-form
655 proto-gf proto-method method-function-lambda initargs env
)))
657 (defun real-make-method-initargs-form (proto-gf proto-method
658 method-lambda initargs env
)
659 (declare (ignore proto-gf proto-method
))
660 (check-method-lambda method-lambda
'make-method-initargs
)
661 (make-method-initargs-form-internal method-lambda initargs env
))
663 (unless (fboundp 'make-method-initargs-form
)
664 (setf (gdefinition 'make-method-initargs-form
)
665 (symbol-function 'real-make-method-initargs-form
)))
667 (defun real-make-method-lambda-using-specializers
668 (proto-gf proto-method qualifiers specializers method-lambda env
)
669 (declare (ignore qualifiers
))
670 (check-method-lambda method-lambda
'make-method-lambda
) ; TODO remove check in make-method-lambda
671 ;; Default behavior: delegate to MAKE-METHOD-LAMBDA.
672 (let* ((lambda-list (second method-lambda
))
673 (*method-lambda-list
*
675 (mapcar #'list
(subseq lambda-list
0 (length specializers
)) specializers
)
676 (subseq lambda-list
(length specializers
)))))
677 (make-method-lambda proto-gf proto-method method-lambda env
)))
679 (unless (fboundp 'make-method-lambda-using-specializers
)
680 (setf (gdefinition 'make-method-lambda-using-specializers
)
681 (symbol-function 'real-make-method-lambda-using-specializers
)))
683 ;;; When bootstrapping PCL MAKE-METHOD-LAMBDA starts out as a regular
684 ;;; function: REAL-MAKE-METHOD-LAMBDA set to the fdefinition of
685 ;;; MAKE-METHOD-LAMBDA. Once generic functions are born,
686 ;;; REAL-MAKE-METHOD-LAMBDA is used to implement the default method.
687 ;;; MAKE-METHOD-LAMBDA-INTERNAL is split out into a separate function
688 ;;; so that changing it in a live image is easy, and changes actually
690 (defun real-make-method-lambda (proto-gf proto-method method-lambda env
)
691 (make-method-lambda-internal proto-gf proto-method method-lambda env
))
693 (unless (fboundp 'make-method-lambda
)
694 (setf (gdefinition 'make-method-lambda
)
695 (symbol-function 'real-make-method-lambda
)))
697 (defun declared-specials (declarations)
698 (loop for
(declare . specifiers
) in declarations
699 append
(loop for specifier in specifiers
700 when
(eq 'special
(car specifier
))
701 append
(cdr specifier
))))
703 ;;; A helper function for creating Python-friendly type declarations
704 ;;; in DEFMETHOD forms.
706 ;;; This function operates on
707 ;;; * non-parsed specializers, i.e. class names and extended
708 ;;; specializer syntaxes
709 ;;; * parsed specializers, i.e. CLASSes, EQL-SPECIALIZERs,
710 ;;; CLASS-EQ-SPECIALIZERs and generic SPECIALIZERs
712 ;;; We're too lazy to cons up a new environment for this, so we just
713 ;;; pass in the list of locally declared specials in addition to the
715 (defun parameter-specializer-declaration-in-defmethod
716 (proto-generic-function proto-method parameter specializer specials env
)
717 (flet ((declare-type (type)
718 (return-from parameter-specializer-declaration-in-defmethod
721 (t `(type ,type
,parameter
))))))
723 ((not (eq **boot-state
** 'complete
))
724 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
725 ;; types which don't match their specializers. (Specifically,
726 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
727 ;; second argument.) Hopefully it only does this kind of
728 ;; weirdness when bootstrapping.. -- WHN 20000610
731 ;; Independent of SPECIALIZER, bail out if the PARAMETER is
732 ;; known to be a special variable. Our rebinding magic for SETQ
733 ;; cases doesn't work right there as SET, (SETF SYMBOL-VALUE),
734 ;; etc. make things undecidable.
735 ((or (var-special-p parameter env
) (member parameter specials
))
738 ;; Bail out on SLOT-OBJECT special case.
740 ;; KLUDGE: For some low-level implementation classes, perhaps
741 ;; because of some problems related to the incomplete
742 ;; integration of PCL into SBCL's type system, some specializer
743 ;; classes can't be declared as argument types. E.g.
744 ;; (DEFMETHOD FOO ((X SLOT-OBJECT))
745 ;; (DECLARE (TYPE SLOT-OBJECT X))
748 ;; (DEFSTRUCT BAR A B)
750 ;; perhaps because of the way that STRUCTURE-OBJECT inherits
751 ;; both from SLOT-OBJECT and from SB-KERNEL:INSTANCE. In an
752 ;; effort to sweep such problems under the rug, we exclude these
753 ;; problem cases by blacklisting them here. -- WHN 2001-01-19
754 ((eq specializer
'slot-object
)
757 ;; Bail out on unparsed EQL-specializers.
759 ;; KLUDGE: ANSI, in its wisdom, says that EQL-SPECIALIZER-FORMs
760 ;; in EQL specializers are evaluated at DEFMETHOD expansion
761 ;; time. Thus, although one might think that in
762 ;; (DEFMETHOD FOO ((X PACKAGE)
765 ;; the PACKAGE and (EQL 12) forms are both parallel type names,
766 ;; they're not, as is made clear when you do
767 ;; (DEFMETHOD FOO ((X PACKAGE)
770 ;; where Y needs to be a symbol named "BAR", not some cons made
771 ;; by (CONS 'QUOTE 'BAR). I.e. when the EQL-SPECIALIZER-FORM is
772 ;; (EQL 'X), it requires an argument to be of type (EQL X). It'd
773 ;; be easy to transform one to the other, but it'd be somewhat
774 ;; messier to do so while ensuring that the EQL-SPECIALIZER-FORM
775 ;; is only EVAL'd once. (The new code wouldn't be messy, but
776 ;; it'd require a big transformation of the old code.) So
777 ;; instead we punt. -- WHN 20000610
778 ((typep specializer
'(cons (eql eql
)))
781 ;; Parsed specializer objects, i.e. CLASS, EQL-SPECIALIZER,
782 ;; CLASS-EQ-SPECIALIZER and generic SPECIALIZER.
784 ;; Also unparsed specializers other than EQL: these have to be
785 ;; either class names or extended specializers.
787 ;; For these, we can usually make Python very happy.
789 ;; KLUDGE: Since INFO doesn't work right for class objects here,
790 ;; and they are valid specializers, see if the specializer is
791 ;; a named class, and use the name in that case -- otherwise
792 ;; the class instance is ok, since info will just return NIL, NIL.
794 ;; We still need to deal with the class case too, but at
795 ;; least #.(find-class 'integer) and integer as equivalent
796 ;; specializers with this.
798 (declare-type (specializer-type-specifier
799 proto-generic-function proto-method specializer
))))))
801 (defun make-method-lambda-internal (proto-gf proto-method method-lambda env
)
802 (check-method-lambda method-lambda
'make-method-lambda
)
804 (binding* (((real-body declarations documentation
)
805 (parse-body (cddr method-lambda
) t
))
806 ;; We have the %METHOD-NAME declaration in the place
807 ;; where we expect it only if there is are no
808 ;; non-standard prior MAKE-METHOD-LAMBDA methods -- or
809 ;; unless they're fantastically unintrusive.
810 (method-name *method-name
*)
811 (method-lambda-list *method-lambda-list
*)
812 ;; Macroexpansion caused by code-walking may call
813 ;; make-method-lambda and end up with wrong values
815 (*method-lambda-list
* nil
)
816 (generic-function-name (when method-name
(car method-name
)))
817 ;; the method-cell is a way of communicating what method
818 ;; a method-function implements, for the purpose of
819 ;; NO-NEXT-METHOD. We need something that can be shared
820 ;; between function and initargs, but not something that
821 ;; will be coalesced as a constant (because we are
822 ;; naughty, oh yes) with the expansion of any other
823 ;; methods in the same file. -- CSR, 2007-05-30
824 (method-cell (list (make-symbol "METHOD-CELL")))
825 ((parameters lambda-list specializers
)
826 (parse-specialized-lambda-list
827 (or method-lambda-list
828 (ecase (car method-lambda
)
829 (lambda (second method-lambda
))
830 (named-lambda (third method-lambda
))))))
831 (required-parameters (subseq parameters
0 (length specializers
)))
832 (slots (mapcar #'list required-parameters
))
835 ;; These declarations seem to be used by PCL to pass
836 ;; information to itself; when I tried to delete 'em
837 ;; ca. 0.6.10 it didn't work. I'm not sure how they
838 ;; work, but note the (VAR-DECLARATION '%CLASS ..)
839 ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN
841 ,@(mapcan (lambda (parameter specializer
)
842 (when (typep specializer
'(and symbol
(not (eql t
))))
843 (list `(%class
,parameter
,specializer
))))
844 parameters specializers
)
845 ;; These TYPE declarations weren't in the original PCL
846 ;; code, but the Python compiler likes them a
847 ;; lot. (We're telling the compiler about our
848 ;; knowledge of specialized argument types so that it
849 ;; can avoid run-time type dispatch overhead, which
850 ;; can be a huge win for Python.)
851 ,@(let ((specials (declared-specials declarations
)))
852 (mapcar (lambda (par spec
)
853 (parameter-specializer-declaration-in-defmethod
854 proto-gf proto-method par spec specials env
))
855 parameters specializers
))))
857 ;; Remove the documentation string and insert the
858 ;; appropriate class declarations. The documentation
859 ;; string is removed to make it easy for us to insert
860 ;; new declarations later, they will just go after the
861 ;; CADR of the method lambda. The class declarations
862 ;; are inserted to communicate the class of the method's
863 ;; arguments to the code walk.
864 `(lambda ,lambda-list
865 ;; The default ignorability of method parameters
866 ;; doesn't seem to be specified by ANSI. PCL had
867 ;; them basically ignorable but was a little
868 ;; inconsistent. E.g. even though the two
869 ;; method definitions
870 ;; (DEFMETHOD FOO ((X T) (Y T)) "Z")
871 ;; (DEFMETHOD FOO ((X T) Y) "Z")
872 ;; are otherwise equivalent, PCL treated Y as
873 ;; ignorable in the first definition but not in the
874 ;; second definition. We make all required
875 ;; parameters ignorable as a way of systematizing
876 ;; the old PCL behavior. -- WHN 2000-11-24
877 (declare (ignorable ,@required-parameters
))
880 (block ,(fun-name-block-name generic-function-name
)
882 (constant-value-p (and (null (cdr real-body
))
883 (constantp (car real-body
))))
884 (constant-value (when constant-value-p
885 (constant-form-value (car real-body
))))
886 (plist (when (and constant-value-p
887 (or (typep constant-value
'(or number character
))
888 (and (symbolp constant-value
)
889 (symbol-package constant-value
))))
890 (list :constant-value constant-value
)))
891 (applyp (dolist (p lambda-list nil
)
892 (cond ((memq p
'(&optional
&rest
&key
))
896 ((walked-lambda call-next-method-p setq-p parameters-setqd
)
898 method-lambda required-parameters env slots
))
899 ((walked-lambda-body walked-declarations
)
900 (parse-body (cddr walked-lambda
) t
)))
901 (when (some #'cdr slots
)
902 (let ((slot-name-lists (slot-name-lists-from-slots slots
)))
904 `(,@(when slot-name-lists
905 `(:slot-name-lists
,slot-name-lists
))
908 `((pv-binding (,required-parameters
912 :slot-name-lists
',slot-name-lists
)))
913 ,@walked-lambda-body
)))))
914 (when (and (memq '&key lambda-list
)
915 (not (memq '&allow-other-keys lambda-list
)))
916 (let ((aux (memq '&aux lambda-list
)))
917 (setq lambda-list
(nconc (ldiff lambda-list aux
)
918 (list '&allow-other-keys
)
920 (values `(lambda (.method-args. .next-methods.
)
921 (simple-lexical-method-functions
922 (,lambda-list .method-args. .next-methods.
924 ,(when call-next-method-p t
)
926 :parameters-setqd
,parameters-setqd
927 :method-cell
,method-cell
929 ,@walked-declarations
930 (locally (declare (disable-package-locks
931 %parameter-binding-modified
))
932 (symbol-macrolet ((%parameter-binding-modified
933 ',@parameters-setqd
))
934 (declare (enable-package-locks
935 %parameter-binding-modified
))
936 ,@walked-lambda-body
))))
937 `(,@(when call-next-method-p
`(method-cell ,method-cell
))
938 ,@(when (member call-next-method-p
'(:simple nil
))
939 '(simple-next-method-call t
))
940 ,@(when plist
`(plist ,plist
))
941 ,@(when documentation
`(:documentation
,documentation
))))))
943 (defun real-make-method-specializers-form
944 (proto-generic-function proto-method specializer-names environment
)
945 (flet ((make-parse-form (name)
946 (make-specializer-form-using-class
947 proto-generic-function proto-method name environment
)))
948 `(list ,@(mapcar #'make-parse-form specializer-names
))))
950 (unless (fboundp 'make-method-specializers-form
)
951 (setf (gdefinition 'make-method-specializers-form
)
952 (symbol-function 'real-make-method-specializers-form
)))
954 (defun real-make-specializer-form-using-class/t
955 (proto-generic-function proto-method specializer-name environment
)
956 (declare (ignore proto-generic-function proto-method environment
))
957 (error 'simple-reference-error
959 "~@<~S is not a valid parameter specializer name.~@:>"
960 :format-arguments
(list specializer-name
)
961 :references
(list '(:ansi-cl
:macro defmethod
)
962 '(:ansi-cl
:glossary
"parameter specializer name"))))
964 (defun real-make-specializer-form-using-class/specializer
965 (proto-generic-function proto-method specializer-name environment
)
966 (declare (ignore proto-generic-function proto-method environment
))
967 (when (eq **boot-state
** 'complete
)
970 (defun real-make-specializer-form-using-class/symbol
971 (proto-generic-function proto-method specializer-name environment
)
972 (declare (ignore proto-generic-function proto-method environment
))
973 `(find-class ',specializer-name
))
975 (defun real-make-specializer-form-using-class/cons
976 (proto-generic-function proto-method specializer-name environment
)
977 (declare (ignore proto-generic-function proto-method environment
))
978 ;; In case of unknown specializer or known specializer with syntax
979 ;; error, TYPECASE may fall through to default method with error
981 (typecase specializer-name
982 ((cons (eql eql
) (cons t null
))
983 `(intern-eql-specializer ,(second specializer-name
)))
984 ((cons (eql class-eq
) (cons t null
))
985 `(class-eq-specializer (find-class ',(second specializer-name
))))))
987 (defun real-make-specializer-form-using-class
988 (proto-generic-function proto-method specializer-name environment
)
991 `(typecase specializer-name
996 'real-make-specializer-form-using-class
'#:/ type
)))
999 proto-generic-function proto-method specializer-name environment
))))
1001 ; ^ apparently not needed during bootstrapping
1005 (unless (fboundp 'make-specializer-form-using-class
)
1006 (setf (gdefinition 'make-specializer-form-using-class
)
1007 (symbol-function 'real-make-specializer-form-using-class
)))
1009 (defun real-specializer-type-specifier/specializer
1010 (proto-generic-function proto-method specializer
)
1011 (declare (ignore proto-generic-function proto-method
))
1012 ;; TODO later protocol-unimplemented-error?
1013 (style-warn "~@<No method on ~S for specializer ~S~@:>"
1014 'specializer-type-specifier specializer
)
1017 (labels ((warn-parse (specializer &optional condition
)
1019 "~@<Cannot parse specializer ~S in ~S~@[: ~A~].~@:>"
1020 specializer
'specializer-type-specifier condition
))
1021 (warn-find (condition name proto-generic-function proto-method
)
1024 "~@<Cannot find type for specializer ~
1025 ~/sb-ext:print-symbol-with-prefix/ when executing ~S ~
1026 for a ~/sb-ext:print-type-specifier/ of a ~
1027 ~/sb-ext:print-type-specifier/.~@:>"
1029 (list name
'specializer-type-specifier
1030 (class-name (class-of proto-method
))
1031 (class-name (class-of proto-generic-function
)))))
1032 (class-name-type-specifier (name proto-generic-function proto-method
)
1033 (let ((kind (info :type
:kind name
)))
1038 ;; This can happen if NAME is a DEFTYPE.
1039 (warn-find 'simple-warning
1040 name proto-generic-function proto-method
))
1041 ((:instance
:forthcoming-defclass-type
)
1042 ;; CLOS classes are too expensive to check (as opposed
1043 ;; to STRUCTURE-CLASS and SYSTEM-CLASS).
1046 ;; TODO proper warning condition?
1047 (warn-find 'simple-style-warning
1048 name proto-generic-function proto-method
)
1051 ;;; Non-parsed class specializers, i.e. class names
1053 ;;; Extended generic function classes with specializers which are
1054 ;;; designated by symbols have to install their own methods
1055 ;;; specialized on symbol to replace this logic.
1057 (defun real-specializer-type-specifier/symbol
1058 (proto-generic-function proto-method specializer
)
1061 ;; Usually tries to find the class named
1062 ;; SPECIALIZER. Can do something different when there
1063 ;; is a non-default method on
1064 ;; PARSE-SPECIALIZER-USING-CLASS.
1065 (parse-specializer-using-class
1066 proto-generic-function specializer
)
1067 (class-not-found-error ()
1068 ;; SPECIALIZER does not name a class, but maybe it is
1069 ;; known to name a :forthcoming-defclass-type.
1070 ;; CLASS-NAME-TYPE-SPECIFIER will emit the warning and
1071 ;; return nil if not.
1072 (class-name-type-specifier
1073 specializer proto-generic-function proto-method
))
1075 ;; This can only happen if there is an EQL-specialized
1076 ;; method on PARSE-SPECIALIZER-USING-CLASS matching
1077 ;; SPECIALIZER that signals an error.
1078 (warn-parse specializer condition
)
1081 (specializer-type-specifier
1082 proto-generic-function proto-method specializer
))))
1084 ;;; Non-parsed extended specializer with default syntax
1085 ;;; i.e. (SPECIALIZER-KIND &rest SPECIFIC-SYNTAX)
1087 (defun real-specializer-type-specifier/t
1088 (proto-generic-function proto-method specializer
)
1091 (parse-specializer-using-class
1092 proto-generic-function specializer
)
1094 ;; This can happen, for example, if SPECIALIZER does
1095 ;; not designate any extended specializer or if it does
1096 ;; but then does not conform to the respective extended
1097 ;; specializer syntax.
1098 (warn-parse specializer condition
)
1101 (specializer-type-specifier
1102 proto-generic-function proto-method specializer
))))
1104 ;;; Parsed EQL and CLASS-EQ specializers
1106 (defun real-specializer-type-specifier/class-eq-specializer
1107 (proto-generic-function proto-method specializer
)
1108 (specializer-type-specifier
1109 proto-generic-function proto-method
(specializer-class specializer
)))
1111 (defun real-specializer-type-specifier/eql-specializer
1112 (proto-generic-function proto-method specializer
)
1113 (declare (ignore proto-generic-function proto-method
))
1114 `(eql ,(eql-specializer-object specializer
)))
1116 ;;; Parsed class specializers
1118 (defun real-specializer-type-specifier/structure-class
1119 (proto-generic-function proto-method specializer
)
1120 (declare (ignore proto-generic-function proto-method
))
1121 (class-name specializer
))
1123 (defun real-specializer-type-specifier/system-class
1124 (proto-generic-function proto-method specializer
)
1125 (declare (ignore proto-generic-function proto-method
))
1126 (class-name specializer
))
1128 (defun real-specializer-type-specifier/class
1129 (proto-generic-function proto-method specializer
)
1130 (let ((name (class-name specializer
)))
1131 ;; Make sure SPECIALIZER has a proper class name and that name
1132 ;; designates the class SPECIALIZER in the global environment.
1133 (when (and (typep name
'(and symbol
(not null
)))
1134 (eq specializer
(find-class name nil
)))
1135 (class-name-type-specifier
1136 name proto-generic-function proto-method
)))))
1138 (defun real-specializer-type-specifier
1139 (proto-generic-function proto-method specializer
)
1142 `(typecase specializer
1145 (let ((function-name
1147 'real-specializer-type-specifier
'#:/ type
)))
1150 proto-generic-function proto-method specializer
))))
1151 '(specializer symbol t class-eq-specializer eql-specializer
1152 structure-class system-class class
)))))
1155 (unless (fboundp 'specializer-type-specifier
)
1156 (setf (gdefinition 'specializer-type-specifier
)
1157 (symbol-function 'real-specializer-type-specifier
)))
1159 (defun real-parse-specializer-using-class (generic-function specializer
)
1160 (let ((result (specializer-from-type specializer
)))
1161 (if (specializerp result
)
1163 (error "~@<~S cannot be parsed as a specializer for ~S.~@:>"
1164 specializer generic-function
))))
1166 (unless (fboundp 'parse-specializer-using-class
)
1167 (setf (gdefinition 'parse-specializer-using-class
)
1168 (symbol-function 'real-parse-specializer-using-class
)))
1170 (defun real-unparse-specializer-using-class (generic-function specializer
)
1171 (if (specializerp specializer
)
1172 ;; FIXME: this HANDLER-CASE is a bit of a hammer to crack a nut:
1173 ;; the idea is that we want to unparse permissively, so that the
1174 ;; lazy (or rather the "portable") specializer extender (who
1175 ;; does not define methods on these new SBCL-specific MOP
1176 ;; functions) can still subclass specializer and define methods
1177 ;; without everything going wrong. Making it cleaner and
1178 ;; clearer that that is what we are defending against would be
1179 ;; nice. -- CSR, 2007-06-01
1181 (let ((type (specializer-type specializer
)))
1182 (if (and (consp type
) (eq (car type
) 'class
))
1183 (let* ((class (cadr type
))
1184 (class-name (class-name class
)))
1185 (if (eq class
(find-class class-name nil
))
1189 (error () specializer
))
1190 (error "~@<~S is not a legal specializer for ~S.~@:>"
1191 specializer generic-function
)))
1193 (unless (fboundp 'unparse-specializer-using-class
)
1194 (setf (gdefinition 'unparse-specializer-using-class
)
1195 (symbol-function 'real-unparse-specializer-using-class
)))
1197 ;;; For passing a list (groveled by the walker) of the required
1198 ;;; parameters whose bindings are modified in the method body to the
1199 ;;; optimized-slot-value* macros.
1200 (define-symbol-macro %parameter-binding-modified
())
1202 (defmacro simple-lexical-method-functions
((lambda-list
1208 ,method-args
,next-methods
1209 (bind-simple-lexical-method-functions (,method-args
,next-methods
1211 (bind-args (,lambda-list
,method-args
)
1214 (defmacro fast-lexical-method-functions
((lambda-list
1220 `(bind-fast-lexical-method-functions (,args
,rest-arg
,next-method-call
,lmf-options
)
1221 (bind-args (,(nthcdr (length args
) lambda-list
) ,rest-arg
)
1224 (defmacro bind-simple-lexical-method-functions
1225 ((method-args next-methods
(&key call-next-method-p setq-p
1226 parameters-setqd applyp method-cell
))
1229 (declare (ignore parameters-setqd
))
1230 (if (not (or call-next-method-p setq-p applyp
))
1231 ;; always provide the lexical function NEXT-METHOD-P.
1232 ;; I would think this to be a good candidate for declaring INLINE
1233 ;; but that's not the way it was done before.
1234 `(flet ((next-method-p () (not (null (car ,next-methods
)))))
1235 (declare (ignorable #'next-method-p
))
1237 `(let ((.next-method.
(car ,next-methods
))
1238 (,next-methods
(cdr ,next-methods
)))
1239 (declare (ignorable .next-method.
,next-methods
))
1240 (flet (,@(when call-next-method-p
1241 `((call-next-method (&rest cnm-args
)
1242 (declare (dynamic-extent cnm-args
))
1243 ,@(if (safe-code-p env
)
1244 `((%check-cnm-args cnm-args
1249 (funcall (if (std-instance-p .next-method.
)
1250 (method-function .next-method.
)
1251 .next-method.
) ; for early methods
1252 (or cnm-args
,method-args
)
1254 (apply #'call-no-next-method
1256 (or cnm-args
,method-args
))))))
1257 (next-method-p () (not (null .next-method.
))))
1258 (declare (ignorable #'next-method-p
))
1261 (defun call-no-next-method (method-cell &rest args
)
1262 (let ((method (car method-cell
)))
1264 ;; Can't easily provide a RETRY restart here, as the return value here is
1265 ;; for the method, not the generic function.
1266 (apply #'no-next-method
(method-generic-function method
)
1269 (defun call-no-applicable-method (gf args
)
1271 (apply #'no-applicable-method gf args
)
1273 :report
"Retry calling the generic function."
1276 (defun call-no-primary-method (gf args
)
1278 (apply #'no-primary-method gf args
)
1280 :report
"Retry calling the generic function."
1283 (defstruct (method-call (:copier nil
))
1284 (function #'identity
:type function
)
1286 (defstruct (constant-method-call (:copier nil
) (:include method-call
))
1289 #-sb-fluid
(declaim (sb-ext:freeze-type method-call
))
1291 (defmacro invoke-method-call1
(function args cm-args
)
1292 `(let ((.function.
,function
)
1294 (.cm-args.
,cm-args
))
1295 (if (and .cm-args.
(null (cdr .cm-args.
)))
1296 (funcall .function. .args.
(car .cm-args.
))
1297 (apply .function. .args. .cm-args.
))))
1299 (defmacro invoke-method-call
(method-call restp
&rest required-args
+rest-arg
)
1300 `(invoke-method-call1 (method-call-function ,method-call
)
1302 `(list* ,@required-args
+rest-arg
)
1303 `(list ,@required-args
+rest-arg
))
1304 (method-call-call-method-args ,method-call
)))
1306 (defstruct (fast-method-call (:copier nil
))
1307 (function #'identity
:type function
)
1311 (defstruct (constant-fast-method-call
1312 (:copier nil
) (:include fast-method-call
))
1315 #-sb-fluid
(declaim (sb-ext:freeze-type fast-method-call
))
1317 ;; The two variants of INVOKE-FAST-METHOD-CALL differ in how REST-ARGs
1318 ;; are handled. The first one will get REST-ARG as a single list (as
1319 ;; the last argument), and will thus need to use APPLY. The second one
1320 ;; will get them as a &MORE argument, so we can pass the arguments
1321 ;; directly with MULTIPLE-VALUE-CALL and %MORE-ARG-VALUES.
1323 (defmacro invoke-fast-method-call
(method-call restp
&rest required-args
+rest-arg
)
1324 `(,(if restp
'apply
'funcall
) (fast-method-call-function ,method-call
)
1325 (fast-method-call-pv ,method-call
)
1326 (fast-method-call-next-method-call ,method-call
)
1327 ,@required-args
+rest-arg
))
1329 (defmacro invoke-fast-method-call
/more
(method-call
1332 &rest required-args
)
1333 (macrolet ((generate-call (n)
1334 ``(funcall (fast-method-call-function ,method-call
)
1335 (fast-method-call-pv ,method-call
)
1336 (fast-method-call-next-method-call ,method-call
)
1338 ,@(loop for x below
,n
1339 collect
`(sb-c::%more-arg
,more-context
,x
)))))
1340 ;; The cases with only small amounts of required arguments passed
1341 ;; are probably very common, and special-casing speeds them up by
1342 ;; a factor of 2 with very little effect on the other
1343 ;; cases. Though it'd be nice to have the generic case be equally
1346 (0 ,(generate-call 0))
1347 (1 ,(generate-call 1))
1348 (t (multiple-value-call (fast-method-call-function ,method-call
)
1349 (values (fast-method-call-pv ,method-call
))
1350 (values (fast-method-call-next-method-call ,method-call
))
1352 (sb-c::%more-arg-values
,more-context
0 ,more-count
))))))
1354 (defstruct (fast-instance-boundp (:copier nil
))
1355 (index 0 :type fixnum
))
1357 #-sb-fluid
(declaim (sb-ext:freeze-type fast-instance-boundp
))
1359 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
1360 (defvar *allow-emf-call-tracing-p
* nil
)
1361 (defvar *enable-emf-call-tracing-p
* #-sb-show nil
#+sb-show t
))
1363 ;;;; effective method functions
1365 (defvar *emf-call-trace-size
* 200)
1366 (defvar *emf-call-trace
* nil
)
1367 (defvar *emf-call-trace-index
* 0)
1369 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
1370 ;;; without explanation. It appears to be intended for debugging, so
1371 ;;; it might be useful someday, so I haven't deleted it.
1372 ;;; But it isn't documented and isn't used for anything now, so
1373 ;;; I've conditionalized it out of the base system. -- WHN 19991213
1375 (defun show-emf-call-trace ()
1376 (when *emf-call-trace
*
1377 (let ((j *emf-call-trace-index
*)
1378 (*enable-emf-call-tracing-p
* nil
))
1379 (format t
"~&(The oldest entries are printed first)~%")
1380 (dotimes-fixnum (i *emf-call-trace-size
*)
1381 (let ((ct (aref *emf-call-trace
* j
)))
1382 (when ct
(print ct
)))
1384 (when (= j
*emf-call-trace-size
*)
1387 (defun trace-emf-call-internal (emf format args
)
1388 (unless *emf-call-trace
*
1389 (setq *emf-call-trace
* (make-array *emf-call-trace-size
*)))
1390 (setf (aref *emf-call-trace
* *emf-call-trace-index
*)
1391 (list* emf format args
))
1392 (incf *emf-call-trace-index
*)
1393 (when (= *emf-call-trace-index
* *emf-call-trace-size
*)
1394 (setq *emf-call-trace-index
* 0)))
1396 (defmacro trace-emf-call
(emf format args
)
1397 (when *allow-emf-call-tracing-p
*
1398 `(when *enable-emf-call-tracing-p
*
1399 (trace-emf-call-internal ,emf
,format
,args
))))
1401 (defmacro invoke-effective-method-function-fast
1402 (emf restp
&key required-args rest-arg more-arg
)
1404 (trace-emf-call ,emf
,restp
(list ,@required-args rest-arg
))
1406 `(invoke-fast-method-call/more
,emf
1409 `(invoke-fast-method-call ,emf
1414 (defun effective-method-optimized-slot-access-clause
1415 (emf restp required-args
)
1416 ;; "What," you may wonder, "do these next two clauses do?" In that
1417 ;; case, you are not a PCL implementor, for they considered this to
1418 ;; be self-documenting.:-| Or CSR, for that matter, since he can
1419 ;; also figure it out by looking at it without breaking stride. For
1420 ;; the rest of us, though: From what the code is doing with .SLOTS.
1421 ;; and whatnot, evidently it's implementing SLOT-VALUEish and
1422 ;; GET-SLOT-VALUEish things. Then we can reason backwards and
1423 ;; conclude that setting EMF to a FIXNUM is an optimized way to
1424 ;; represent these slot access operations.
1426 (let ((length (length required-args
)))
1429 (let* ((.slots.
(get-slots-or-nil
1430 ,(car required-args
)))
1431 (value (when .slots.
(clos-slots-ref .slots.
,emf
))))
1432 (if (eq value
+slot-unbound
+)
1433 (slot-unbound-internal ,(car required-args
)
1438 (let ((.new-value.
,(car required-args
))
1439 (.slots.
(get-slots-or-nil
1440 ,(cadr required-args
))))
1442 (setf (clos-slots-ref .slots.
,emf
) .new-value.
)))))))
1443 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
1444 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
1445 ;; there was no explanation and presumably the code is 10+
1446 ;; years stale, I simply deleted it. -- WHN)
1449 ;;; Before SBCL 0.9.16.7 instead of
1450 ;;; INVOKE-NARROW-EFFECTIVE-METHOD-FUNCTION we passed a (THE (OR
1451 ;;; FUNCTION METHOD-CALL FAST-METHOD-CALL) EMF) form as the EMF. Now,
1452 ;;; to make less work for the compiler we take a path that doesn't
1453 ;;; involve the slot-accessor clause (where EMF is a FIXNUM) at all.
1454 (macrolet ((def (name &optional narrow
)
1455 `(defmacro ,name
(emf restp
&key required-args rest-arg more-arg
)
1456 (unless (constantp restp
)
1457 (error "The RESTP argument is not constant."))
1458 (setq restp
(constant-form-value restp
))
1459 (with-unique-names (emf-n)
1461 (declare (optimize (sb-c:insert-step-conditions
0)))
1462 (let ((,emf-n
,emf
))
1463 (trace-emf-call ,emf-n
,restp
(list ,@required-args
,@rest-arg
))
1467 `(invoke-fast-method-call/more
,emf-n
1470 `(invoke-fast-method-call ,emf-n
1475 `(effective-method-optimized-slot-access-clause
1476 emf-n restp required-args
))
1478 (invoke-method-call ,emf-n
,restp
,@required-args
1482 `(apply ,emf-n
,@required-args
,@rest-arg
)
1483 `(funcall ,emf-n
,@required-args
1484 ,@rest-arg
))))))))))
1485 (def invoke-effective-method-function nil
)
1486 (def invoke-narrow-effective-method-function t
))
1488 (defun invoke-emf (emf args
)
1489 (trace-emf-call emf t args
)
1492 (let* ((arg-info (fast-method-call-arg-info emf
))
1493 (restp (cdr arg-info
))
1494 (nreq (car arg-info
)))
1496 (apply (fast-method-call-function emf
)
1497 (fast-method-call-pv emf
)
1498 (fast-method-call-next-method-call emf
)
1502 (invoke-fast-method-call emf nil
)
1503 (error 'simple-program-error
1504 :format-control
"invalid number of arguments: 0"
1505 :format-arguments nil
)))
1508 (invoke-fast-method-call emf nil
(car args
))
1509 (error 'simple-program-error
1510 :format-control
"invalid number of arguments: 1"
1511 :format-arguments nil
)))
1514 (invoke-fast-method-call emf nil
(car args
) (cadr args
))
1515 (error 'simple-program-error
1516 :format-control
"invalid number of arguments: 2"
1517 :format-arguments nil
)))
1519 (apply (fast-method-call-function emf
)
1520 (fast-method-call-pv emf
)
1521 (fast-method-call-next-method-call emf
)
1524 (apply (method-call-function emf
)
1526 (method-call-call-method-args emf
)))
1529 (error 'simple-program-error
1530 :format-control
"invalid number of arguments: 0"
1531 :format-arguments nil
))
1533 (let* ((slots (get-slots (car args
)))
1534 (value (clos-slots-ref slots emf
)))
1535 (if (eq value
+slot-unbound
+)
1536 (slot-unbound-internal (car args
) emf
)
1539 (setf (clos-slots-ref (get-slots (cadr args
)) emf
)
1541 (t (error 'simple-program-error
1542 :format-control
"invalid number of arguments"
1543 :format-arguments nil
))))
1544 (fast-instance-boundp
1545 (if (or (null args
) (cdr args
))
1546 (error 'simple-program-error
1547 :format-control
"invalid number of arguments"
1548 :format-arguments nil
)
1549 (let ((slots (get-slots (car args
))))
1550 (not (eq (clos-slots-ref slots
(fast-instance-boundp-index emf
))
1556 (defmacro fast-call-next-method-body
((args next-method-call rest-arg
)
1559 `(if ,next-method-call
1560 ,(let ((call `(invoke-narrow-effective-method-function
1562 ,(not (null rest-arg
))
1563 :required-args
,args
1564 :rest-arg
,(when rest-arg
(list rest-arg
)))))
1568 `(&rest
,rest-arg
)))
1572 (call-no-next-method ',method-cell
1577 (defmacro bind-fast-lexical-method-functions
1578 ((args rest-arg next-method-call
(&key
1586 (let* ((next-method-p-def
1588 (declare (optimize (sb-c:insert-step-conditions
0)))
1589 (not (null ,next-method-call
)))))
1590 (rebindings (when (or setq-p call-next-method-p
)
1591 (mapcar (lambda (x) (list x x
)) parameters-setqd
))))
1592 (if (not (or call-next-method-p setq-p applyp
))
1593 `(flet ,next-method-p-def
1594 (declare (ignorable #'next-method-p
))
1596 `(flet (,@(when call-next-method-p
1597 `((call-next-method (&rest cnm-args
)
1598 (declare (dynamic-extent cnm-args
)
1599 (muffle-conditions code-deletion-note
)
1600 (optimize (sb-c:insert-step-conditions
0)))
1601 ,@(if (safe-code-p env
)
1602 `((%check-cnm-args cnm-args
(list ,@args
)
1605 (fast-call-next-method-body (,args
1610 ,@next-method-p-def
)
1611 (declare (ignorable #'next-method-p
))
1615 ;;; CMUCL comment (Gerd Moellmann):
1617 ;;; The standard says it's an error if CALL-NEXT-METHOD is called with
1618 ;;; arguments, and the set of methods applicable to those arguments is
1619 ;;; different from the set of methods applicable to the original
1620 ;;; method arguments. (According to Barry Margolin, this rule was
1621 ;;; probably added to ensure that before and around methods are always
1622 ;;; run before primary methods.)
1624 ;;; This could be optimized for the case that the generic function
1625 ;;; doesn't have hairy methods, does have standard method combination,
1626 ;;; is a standard generic function, there are no methods defined on it
1627 ;;; for COMPUTE-APPLICABLE-METHODS and probably a lot more of such
1628 ;;; preconditions. That looks hairy and is probably not worth it,
1629 ;;; because this check will never be fast.
1630 (defun %check-cnm-args
(cnm-args orig-args method-cell
)
1631 ;; 1. Check for no arguments.
1633 (let* ((gf (method-generic-function (car method-cell
)))
1634 (nreq (generic-function-nreq gf
)))
1635 (declare (fixnum nreq
))
1636 ;; 2. Requirement arguments pairwise: if all are EQL, the applicable
1637 ;; methods must be the same. This takes care of the relatively common
1638 ;; case of twiddling with &KEY arguments without being horribly
1640 (unless (do ((orig orig-args
(cdr orig
))
1641 (args cnm-args
(cdr args
))
1644 (unless (and orig args
(eql (car orig
) (car args
)))
1646 ;; 3. Only then do the full check.
1647 (let ((omethods (compute-applicable-methods gf orig-args
))
1648 (nmethods (compute-applicable-methods gf cnm-args
)))
1649 (unless (equal omethods nmethods
)
1650 (error "~@<The set of methods ~S applicable to argument~P ~
1651 ~{~S~^, ~} to call-next-method is different from ~
1652 the set of methods ~S applicable to the original ~
1653 method argument~P ~{~S~^, ~}.~@:>"
1654 nmethods
(length cnm-args
) cnm-args omethods
1655 (length orig-args
) orig-args
)))))))
1657 ;; FIXME: replacing this entire mess with DESTRUCTURING-BIND would correct
1658 ;; problems similar to those already solved by a correct implementation
1659 ;; of DESTRUCTURING-BIND, such as incorrect binding order:
1660 ;; e.g. (macroexpand-1 '(bind-args ((&optional (x nil xsp)) args) (form)))
1661 ;; -> (LET* ((.ARGS-TAIL. ARGS) (XSP (NOT (NULL .ARGS-TAIL.))) (X ...)))
1662 ;; It's mostly irrelevant unless a method uses CALL-NEXT-METHOD though.
1663 (defmacro bind-args
((lambda-list args
) &body body
)
1664 (let ((args-tail '.args-tail.
)
1667 (flet ((process-var (var)
1668 (if (memq var lambda-list-keywords
)
1671 (&optional
(setq state
'optional
))
1672 (&key
(setq state
'key
))
1674 (&rest
(setq state
'rest
))
1675 (&aux
(setq state
'aux
))
1678 "encountered the non-standard lambda list keyword ~S"
1682 (required `((,var
(pop ,args-tail
))))
1683 (optional (cond ((not (consp var
))
1684 `((,var
(when ,args-tail
1685 (pop ,args-tail
)))))
1687 `((,(car var
) (if ,args-tail
1691 `((,(caddr var
) (not (null ,args-tail
)))
1692 (,(car var
) (if ,args-tail
1695 (rest `((,var
,args-tail
)))
1696 (key (cond ((not (consp var
))
1698 (get-key-arg-tail ,(keywordicate var
)
1701 (multiple-value-bind (keyword variable
)
1702 (if (consp (car var
))
1705 (values (keywordicate (car var
))
1707 `((,key
(get-key-arg-tail ',keyword
1713 (multiple-value-bind (keyword variable
)
1714 (if (consp (car var
))
1717 (values (keywordicate (car var
))
1719 `((,key
(get-key-arg-tail ',keyword
1721 (,(caddr var
) (not (null,key
)))
1726 (let ((bindings (mapcan #'process-var lambda-list
)))
1727 `(let* ((,args-tail
,args
)
1730 ,@(when (eq state
'optional
)
1731 `((unless (null ,args-tail
)
1732 (error 'simple-program-error
1733 :format-control
"surplus arguments: ~S"
1734 :format-arguments
(list ,args-tail
)))))))
1735 (declare (ignorable ,args-tail .dummy0.
))
1738 (defun get-key-arg-tail (keyword list
)
1739 (loop for
(key . tail
) on list by
#'cddr
1741 ;; FIXME: Do we want to export this symbol? Or maybe use an
1742 ;; (ERROR 'SIMPLE-PROGRAM-ERROR) form?
1743 (sb-c::%odd-key-args-error
)
1744 when
(eq key keyword
)
1747 (defun walk-method-lambda (method-lambda required-parameters env slots
)
1748 (let (;; flag indicating that CALL-NEXT-METHOD should be in the
1749 ;; method definition
1750 (call-next-method-p nil
)
1751 ;; a list of all required parameters whose bindings might be
1752 ;; modified in the method body.
1753 (parameters-setqd nil
))
1754 (flet ((walk-function (form context env
)
1755 (unless (and (eq context
:eval
) (consp form
))
1756 (return-from walk-function form
))
1759 ;; hierarchy: nil -> :simple -> T.
1760 (unless (eq call-next-method-p t
)
1761 (setq call-next-method-p
(if (cdr form
) t
:simple
)))
1763 ((setq multiple-value-setq
)
1764 ;; The walker will split (SETQ A 1 B 2) to
1765 ;; separate (SETQ A 1) and (SETQ B 2) forms, so we
1766 ;; only need to handle the simple case of SETQ
1768 (let ((vars (if (eq (car form
) 'setq
)
1769 (list (second form
))
1772 ;; Note that we don't need to check for
1773 ;; %VARIABLE-REBINDING declarations like is
1774 ;; done in CAN-OPTIMIZE-ACCESS1, since the
1775 ;; bindings that will have that declation will
1777 (when (var-declaration '%class var env
)
1778 ;; If a parameter binding is shadowed by
1779 ;; another binding it won't have a %CLASS
1780 ;; declaration anymore, and this won't get
1782 (pushnew var parameters-setqd
:test
#'eq
))))
1785 (when (equal (cdr form
) '(call-next-method))
1786 (setq call-next-method-p t
))
1788 ((slot-value set-slot-value slot-boundp
)
1789 (if (constantp (third form
) env
)
1790 (let ((fun (ecase (car form
)
1791 (slot-value #'optimize-slot-value
)
1792 (set-slot-value #'optimize-set-slot-value
)
1793 (slot-boundp #'optimize-slot-boundp
))))
1794 (funcall fun form slots required-parameters env
))
1798 (let ((walked-lambda (walk-form method-lambda env
#'walk-function
)))
1799 ;;; FIXME: the walker's rewriting of the source code causes
1800 ;;; trouble when doing code coverage. The rewrites should be
1801 ;;; removed, and the same operations done using
1802 ;;; compiler-macros or tranforms.
1803 (values (if (sb-c:policy env
(= sb-c
:store-coverage-data
0))
1807 (not (null parameters-setqd
))
1808 parameters-setqd
)))))
1810 (defun generic-function-name-p (name)
1811 (and (legal-fun-name-p name
)
1813 (if (eq **boot-state
** 'complete
)
1814 (standard-generic-function-p (gdefinition name
))
1815 (funcallable-instance-p (gdefinition name
)))))
1817 (defun method-plist-value (method key
&optional default
)
1818 (let ((plist (if (consp method
)
1819 (getf (early-method-initargs method
) 'plist
)
1820 (object-plist method
))))
1821 (getf plist key default
)))
1823 (defun (setf method-plist-value
) (new-value method key
&optional default
)
1825 (setf (getf (getf (early-method-initargs method
) 'plist
) key default
)
1827 (setf (getf (object-plist method
) key default
) new-value
)))
1829 (defun load-defmethod (class name quals specls ll initargs source-location
)
1830 (let ((method-cell (getf initargs
'method-cell
)))
1831 (setq initargs
(copy-tree initargs
))
1833 (setf (getf initargs
'method-cell
) method-cell
))
1835 (setf (getf (getf initargs
'plist
) :name
)
1836 (make-method-spec name quals specls
))
1837 (load-defmethod-internal class name quals specls
1838 ll initargs source-location
)))
1840 (defun load-defmethod-internal
1841 (method-class gf-spec qualifiers specializers lambda-list
1842 initargs source-location
)
1843 (when (and (eq **boot-state
** 'complete
)
1845 (let* ((gf (fdefinition gf-spec
))
1846 (method (and (generic-function-p gf
)
1847 (generic-function-methods gf
)
1848 (find-method gf qualifiers specializers nil
))))
1850 (warn 'sb-kernel
:redefinition-with-defmethod
1852 :new-location source-location
1854 :qualifiers qualifiers
:specializers specializers
))))
1855 (let ((method (apply #'add-named-method
1856 gf-spec qualifiers specializers lambda-list
1857 :definition-source source-location
1859 (unless (or (eq method-class
'standard-method
)
1860 (eq (find-class method-class nil
) (class-of method
)))
1861 ;; FIXME: should be STYLE-WARNING?
1862 (format *error-output
*
1863 "~&At the time the method with qualifiers ~:S and~%~
1864 specializers ~:S on the generic function ~S~%~
1865 was compiled, the method-class for that generic function was~%~
1866 ~S. But, the method class is now ~S, this~%~
1867 may mean that this method was compiled improperly.~%"
1868 qualifiers specializers gf-spec
1869 method-class
(class-name (class-of method
))))
1872 (defun make-method-spec (gf qualifiers specializers
)
1873 (let ((name (generic-function-name gf
))
1874 (unparsed-specializers (unparse-specializers gf specializers
)))
1875 `(slow-method ,name
,@qualifiers
,unparsed-specializers
)))
1877 (defun initialize-method-function (initargs method
)
1878 (let* ((mf (getf initargs
:function
))
1879 (mff (and (typep mf
'%method-function
)
1880 (%method-function-fast-function mf
)))
1881 (plist (getf initargs
'plist
))
1882 (name (getf plist
:name
))
1883 (method-cell (getf initargs
'method-cell
)))
1885 (setf (car method-cell
) method
))
1888 (setq mf
(set-fun-name mf name
)))
1889 (when (and mff
(consp name
) (eq (car name
) 'slow-method
))
1890 (let ((fast-name `(fast-method ,@(cdr name
))))
1891 (set-fun-name mff fast-name
))))
1893 (let ((plist plist
))
1894 (let ((snl (getf plist
:slot-name-lists
)))
1896 (setf (method-plist-value method
:pv-table
)
1897 (intern-pv-table :slot-name-lists snl
))))))))
1899 (defun analyze-lambda-list (lambda-list)
1900 (multiple-value-bind (llks required optional rest keywords
)
1901 ;; We say "&MUMBLE is not allowed in a generic function lambda list"
1902 ;; whether this is called by DEFMETHOD or DEFGENERIC.
1903 ;; [It is used for either. Why else recognize and silently ignore &AUX?]
1904 (parse-lambda-list lambda-list
1905 :accept
(lambda-list-keyword-mask
1906 '(&optional
&rest
&key
&allow-other-keys
&aux
))
1908 :context
"a generic function lambda list")
1909 (declare (ignore rest
))
1910 (values llks
(length required
) (length optional
)
1911 (mapcar #'parse-key-arg-spec keywords
) keywords
)))
1913 ;; FIXME: this does more than return an FTYPE from a lambda list -
1914 ;; it unions the type with an existing ctype object. It needs a better name,
1915 ;; and to be reimplemented as "union and call sb-c::ftype-from-lambda-list".
1916 (defun ftype-declaration-from-lambda-list (lambda-list name
)
1917 (multiple-value-bind (llks nrequired noptional keywords keyword-parameters
)
1918 (analyze-lambda-list lambda-list
)
1919 (declare (ignore keyword-parameters
))
1920 (let* ((old (proclaimed-ftype name
)) ;FIXME:FDOCUMENTATION instead?
1921 (old-ftype (if (fun-type-p old
) old nil
))
1922 (old-restp (and old-ftype
(fun-type-rest old-ftype
)))
1923 (old-keys (and old-ftype
1924 (mapcar #'key-info-name
1927 (old-keysp (and old-ftype
(fun-type-keyp old-ftype
)))
1928 (old-allowp (and old-ftype
1929 (fun-type-allowp old-ftype
)))
1930 (keywords (union old-keys
(mapcar #'parse-key-arg-spec keywords
))))
1931 `(function ,(append (make-list nrequired
:initial-element t
)
1932 (when (plusp noptional
)
1933 (append '(&optional
)
1934 (make-list noptional
:initial-element t
)))
1935 (when (or (ll-kwds-restp llks
) old-restp
)
1937 (when (or (ll-kwds-keyp llks
) old-keysp
)
1939 (mapcar (lambda (key)
1942 (when (or (ll-kwds-allowp llks
) old-allowp
)
1943 '(&allow-other-keys
)))))
1946 ;;;; early generic function support
1948 (defvar *!early-generic-functions
* ())
1950 ;; CLHS doesn't specify &allow-other-keys here but I guess the supposition
1951 ;; is that they'll be checked by ENSURE-GENERIC-FUNCTION-USING-CLASS.
1952 ;; Except we don't do that either, so I think the blame, if any, lies there
1953 ;; for not catching errant keywords.
1954 (defun ensure-generic-function (fun-name &rest all-keys
)
1955 (let ((existing (and (fboundp fun-name
)
1956 (gdefinition fun-name
))))
1957 (cond ((and existing
1958 (eq **boot-state
** 'complete
)
1959 (null (generic-function-p existing
)))
1960 (generic-clobbers-function fun-name
)
1961 (fmakunbound fun-name
)
1962 (apply #'ensure-generic-function fun-name all-keys
))
1964 (apply #'ensure-generic-function-using-class
1965 existing fun-name all-keys
)))))
1967 (defun generic-clobbers-function (fun-name)
1968 (cerror "Replace the function binding"
1969 'simple-program-error
1970 :format-control
"~@<~/sb-impl:print-symbol-with-prefix/ ~
1971 already names an ordinary function or a ~
1973 :format-arguments
(list fun-name
)))
1975 (defvar *sgf-wrapper
*
1976 (!boot-make-wrapper
(!early-class-size
'standard-generic-function
)
1977 'standard-generic-function
))
1979 (defvar *sgf-slots-init
*
1980 (mapcar (lambda (canonical-slot)
1981 (if (memq (getf canonical-slot
:name
) '(arg-info source
))
1983 (let ((initfunction (getf canonical-slot
:initfunction
)))
1985 (funcall initfunction
)
1987 (!early-collect-inheritance
'standard-generic-function
)))
1989 (defconstant +sgf-method-class-index
+
1990 (!bootstrap-slot-index
'standard-generic-function
'method-class
))
1992 (defun early-gf-p (x)
1993 (and (fsc-instance-p x
)
1994 (eq (clos-slots-ref (get-slots x
) +sgf-method-class-index
+)
1997 (defconstant +sgf-methods-index
+
1998 (!bootstrap-slot-index
'standard-generic-function
'methods
))
2000 (defmacro early-gf-methods
(gf)
2001 `(clos-slots-ref (get-slots ,gf
) +sgf-methods-index
+))
2003 (defun safe-generic-function-methods (generic-function)
2004 (if (eq (class-of generic-function
) *the-class-standard-generic-function
*)
2005 (clos-slots-ref (get-slots generic-function
) +sgf-methods-index
+)
2006 (generic-function-methods generic-function
)))
2008 (defconstant +sgf-arg-info-index
+
2009 (!bootstrap-slot-index
'standard-generic-function
'arg-info
))
2011 (defmacro early-gf-arg-info
(gf)
2012 `(clos-slots-ref (get-slots ,gf
) +sgf-arg-info-index
+))
2014 (defconstant +sgf-dfun-state-index
+
2015 (!bootstrap-slot-index
'standard-generic-function
'dfun-state
))
2017 (defstruct (arg-info
2019 (:constructor make-arg-info
())
2021 (arg-info-lambda-list :no-lambda-list
)
2024 arg-info-number-optional
2026 arg-info-keys
;nil no &KEY or &REST allowed
2027 ;(k1 k2 ..) Each method must accept these &KEY arguments.
2028 ;T must have &KEY or &REST
2030 gf-info-simple-accessor-type
; nil, reader, writer, boundp
2031 (gf-precompute-dfun-and-emf-p nil
) ; set by set-arg-info
2033 gf-info-static-c-a-m-emf
2034 (gf-info-c-a-m-emf-std-p t
)
2037 #-sb-fluid
(declaim (sb-ext:freeze-type arg-info
))
2039 (defun arg-info-valid-p (arg-info)
2040 (not (null (arg-info-number-optional arg-info
))))
2042 (defun arg-info-applyp (arg-info)
2043 (or (plusp (arg-info-number-optional arg-info
))
2044 (arg-info-key/rest-p arg-info
)))
2046 (defun arg-info-number-required (arg-info)
2047 (length (arg-info-metatypes arg-info
)))
2049 (defun arg-info-nkeys (arg-info)
2050 (count-if (lambda (x) (neq x t
)) (arg-info-metatypes arg-info
)))
2052 (defun create-gf-lambda-list (lambda-list)
2053 ;;; Create a gf lambda list from a method lambda list
2054 (loop for x in lambda-list
2055 collect
(if (consp x
) (list (car x
)) x
)
2056 if
(eq x
'&key
) do
(loop-finish)))
2058 (defun ll-keyp-or-restp (bits)
2059 (logtest (lambda-list-keyword-mask '(&key
&rest
)) bits
))
2061 (defun remove-methods (gf)
2062 (loop for method in
(generic-function-methods gf
)
2063 do
(remove-method gf method
)))
2065 (defun set-arg-info (gf &key new-method
(lambda-list nil lambda-list-p
)
2066 argument-precedence-order
)
2067 (let* ((arg-info (if (eq **boot-state
** 'complete
)
2069 (early-gf-arg-info gf
)))
2070 (methods (if (eq **boot-state
** 'complete
)
2071 (generic-function-methods gf
)
2072 (early-gf-methods gf
)))
2073 (was-valid-p (integerp (arg-info-number-optional arg-info
)))
2074 (first-p (and new-method
(null (cdr methods
)))))
2075 (when (and (not lambda-list-p
) methods
)
2076 (setq lambda-list
(gf-lambda-list gf
)))
2077 (when (or lambda-list-p
2079 (eq (arg-info-lambda-list arg-info
) :no-lambda-list
)))
2080 (multiple-value-bind (llks nreq nopt keywords
)
2081 (analyze-lambda-list lambda-list
)
2082 (when (and methods
(not first-p
))
2083 (let ((gf-nreq (arg-info-number-required arg-info
))
2084 (gf-nopt (arg-info-number-optional arg-info
))
2085 (gf-key/rest-p
(arg-info-key/rest-p arg-info
)))
2086 (unless (and (= nreq gf-nreq
)
2088 (eq (ll-keyp-or-restp llks
) gf-key
/rest-p
))
2090 (error "New lambda-list ~S is incompatible with ~
2091 existing methods of ~S.~%~
2093 lambda-list gf
(arg-info-lambda-list arg-info
))
2095 :report
"Remove all methods."
2096 (remove-methods gf
))))))
2097 (setf (arg-info-lambda-list arg-info
)
2100 (create-gf-lambda-list lambda-list
)))
2101 (when (or lambda-list-p argument-precedence-order
2102 (null (arg-info-precedence arg-info
)))
2103 (setf (arg-info-precedence arg-info
)
2104 (compute-precedence lambda-list nreq argument-precedence-order
)))
2105 (setf (arg-info-metatypes arg-info
) (make-list nreq
))
2106 (setf (arg-info-number-optional arg-info
) nopt
)
2107 (setf (arg-info-key/rest-p arg-info
) (ll-keyp-or-restp llks
))
2108 (setf (arg-info-keys arg-info
)
2110 (if (ll-kwds-allowp llks
) t keywords
)
2111 (arg-info-key/rest-p arg-info
)))))
2113 (check-method-arg-info gf arg-info new-method
))
2114 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p
)
2117 (defun check-method-arg-info (gf arg-info method
)
2118 (multiple-value-bind (llks nreq nopt keywords
)
2119 (analyze-lambda-list (if (consp method
)
2120 (early-method-lambda-list method
)
2121 (method-lambda-list method
)))
2122 (flet ((lose (string &rest args
)
2123 (error 'simple-program-error
2124 :format-control
"~@<attempt to add the method~2I~_~S~I~_~
2125 to the generic function~2I~_~S;~I~_~
2127 :format-arguments
(list method gf string args
)))
2128 (comparison-description (x y
)
2129 (if (> x y
) "more" "fewer")))
2130 (let ((gf-nreq (arg-info-number-required arg-info
))
2131 (gf-nopt (arg-info-number-optional arg-info
))
2132 (gf-key/rest-p
(arg-info-key/rest-p arg-info
))
2133 (gf-keywords (arg-info-keys arg-info
)))
2134 (unless (= nreq gf-nreq
)
2136 "the method has ~A required arguments than the generic function."
2137 (comparison-description nreq gf-nreq
)))
2138 (unless (= nopt gf-nopt
)
2140 "the method has ~A optional arguments than the generic function."
2141 (comparison-description nopt gf-nopt
)))
2142 (unless (eq (ll-keyp-or-restp llks
) gf-key
/rest-p
)
2144 "the method and generic function differ in whether they accept~_~
2145 &REST or &KEY arguments."))
2146 (when (consp gf-keywords
)
2147 (unless (or (and (ll-kwds-restp llks
) (not (ll-kwds-keyp llks
)))
2148 (ll-kwds-allowp llks
)
2149 (every (lambda (k) (memq k keywords
)) gf-keywords
))
2150 (lose "the method does not accept each of the &KEY arguments~2I~_~
2154 (defconstant +sm-specializers-index
+
2155 (!bootstrap-slot-index
'standard-method
'specializers
))
2156 (defconstant +sm-%function-index
+
2157 (!bootstrap-slot-index
'standard-method
'%function
))
2158 (defconstant +sm-qualifiers-index
+
2159 (!bootstrap-slot-index
'standard-method
'qualifiers
))
2161 ;;; FIXME: we don't actually need this; we could test for the exact
2162 ;;; class and deal with it as appropriate. In fact we probably don't
2163 ;;; need it anyway because we only use this for METHOD-SPECIALIZERS on
2164 ;;; the standard reader method for METHOD-SPECIALIZERS. Probably.
2165 (dolist (s '(specializers %function
))
2166 (aver (= (symbol-value (intern (format nil
"+SM-~A-INDEX+" s
)))
2167 (!bootstrap-slot-index
'standard-reader-method s
)
2168 (!bootstrap-slot-index
'standard-writer-method s
)
2169 (!bootstrap-slot-index
'standard-boundp-method s
)
2170 (!bootstrap-slot-index
'global-reader-method s
)
2171 (!bootstrap-slot-index
'global-writer-method s
)
2172 (!bootstrap-slot-index
'global-boundp-method s
))))
2174 (defvar *standard-method-class-names
*
2175 '(standard-method standard-reader-method
2176 standard-writer-method standard-boundp-method
2177 global-reader-method global-writer-method
2178 global-boundp-method
))
2180 (declaim (list **standard-method-classes
**))
2181 (defglobal **standard-method-classes
** nil
)
2183 (defun safe-method-specializers (method)
2184 (if (member (class-of method
) **standard-method-classes
** :test
#'eq
)
2185 (clos-slots-ref (std-instance-slots method
) +sm-specializers-index
+)
2186 (method-specializers method
)))
2187 (defun safe-method-fast-function (method)
2188 (let ((mf (safe-method-function method
)))
2189 (and (typep mf
'%method-function
)
2190 (%method-function-fast-function mf
))))
2191 (defun safe-method-function (method)
2192 (if (member (class-of method
) **standard-method-classes
** :test
#'eq
)
2193 (clos-slots-ref (std-instance-slots method
) +sm-%function-index
+)
2194 (method-function method
)))
2195 (defun safe-method-qualifiers (method)
2196 (if (member (class-of method
) **standard-method-classes
** :test
#'eq
)
2197 (clos-slots-ref (std-instance-slots method
) +sm-qualifiers-index
+)
2198 (method-qualifiers method
)))
2200 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p
)
2201 (let* ((existing-p (and methods
(cdr methods
) new-method
))
2202 (nreq (length (arg-info-metatypes arg-info
)))
2203 (metatypes (if existing-p
2204 (arg-info-metatypes arg-info
)
2206 (type (if existing-p
2207 (gf-info-simple-accessor-type arg-info
)
2209 (when (arg-info-valid-p arg-info
)
2210 (dolist (method (if new-method
(list new-method
) methods
))
2211 (let* ((specializers (if (or (eq **boot-state
** 'complete
)
2212 (not (consp method
)))
2213 (safe-method-specializers method
)
2214 (early-method-specializers method t
)))
2215 (class (if (or (eq **boot-state
** 'complete
) (not (consp method
)))
2217 (early-method-class method
)))
2220 (or (not (eq **boot-state
** 'complete
))
2221 (eq (generic-function-method-combination gf
)
2222 *standard-method-combination
*)))
2223 (cond ((or (eq class
*the-class-standard-reader-method
*)
2224 (eq class
*the-class-global-reader-method
*))
2226 ((or (eq class
*the-class-standard-writer-method
*)
2227 (eq class
*the-class-global-writer-method
*))
2229 ((or (eq class
*the-class-standard-boundp-method
*)
2230 (eq class
*the-class-global-boundp-method
*))
2232 (setq metatypes
(mapcar #'raise-metatype metatypes specializers
))
2233 (setq type
(cond ((null type
) new-type
)
2234 ((eq type new-type
) type
)
2236 (setf (arg-info-metatypes arg-info
) metatypes
)
2237 (setf (gf-info-simple-accessor-type arg-info
) type
)))
2238 (when (or (not was-valid-p
) first-p
)
2239 (multiple-value-bind (c-a-m-emf std-p
)
2242 (compute-applicable-methods-emf gf
))
2243 (setf (gf-info-static-c-a-m-emf arg-info
) c-a-m-emf
)
2244 (setf (gf-info-c-a-m-emf-std-p arg-info
) std-p
)
2245 (unless (gf-info-c-a-m-emf-std-p arg-info
)
2246 (setf (gf-info-simple-accessor-type arg-info
) t
))))
2248 (let ((name (if (eq **boot-state
** 'complete
)
2249 (generic-function-name gf
)
2250 (!early-gf-name gf
))))
2251 (setf (gf-precompute-dfun-and-emf-p arg-info
)
2255 *internal-pcl-generalized-fun-name-symbols
*))
2257 (t (let* ((symbol (fun-name-block-name name
))
2258 (package (symbol-package symbol
)))
2259 (and (or (eq package
*pcl-package
*)
2260 (memq package
(package-use-list *pcl-package
*)))
2261 (not (eq package
*cl-package
*))
2262 ;; FIXME: this test will eventually be
2263 ;; superseded by the *internal-pcl...* test,
2264 ;; above. While we are in a process of
2265 ;; transition, however, it should probably
2267 (not (find #\Space
(symbol-name symbol
))))))))))
2268 (setf (gf-info-fast-mf-p arg-info
)
2269 (or (not (eq **boot-state
** 'complete
))
2270 (let* ((method-class (generic-function-method-class gf
))
2271 (methods (compute-applicable-methods
2272 #'make-method-lambda
2273 (list gf
(class-prototype method-class
)
2275 (and methods
(null (cdr methods
))
2276 (let ((specls (method-specializers (car methods
))))
2277 (and (classp (car specls
))
2278 (eq 'standard-generic-function
2279 (class-name (car specls
)))
2280 (classp (cadr specls
))
2281 (eq 'standard-method
2282 (class-name (cadr specls
)))))))))
2285 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
2287 ;;; The STATIC-SLOTS field of the funcallable instances used as early
2288 ;;; generic functions is used to store the early methods and early
2289 ;;; discriminator code for the early generic function. The static
2290 ;;; slots field of the fins contains a list whose:
2291 ;;; CAR - a list of the early methods on this early gf
2292 ;;; CADR - the early discriminator code for this method
2293 (defun ensure-generic-function-using-class (existing spec
&rest keys
2294 &key
(lambda-list nil
2296 argument-precedence-order
2300 (declare (ignore keys
))
2301 (cond ((and existing
(early-gf-p existing
))
2303 (set-arg-info existing
:lambda-list lambda-list
))
2305 ((assoc spec
*!generic-function-fixups
* :test
#'equal
)
2307 (make-early-gf spec lambda-list lambda-list-p existing
2308 argument-precedence-order definition-source
2310 (bug "The function ~S is not already defined." spec
)))
2312 (bug "~S should be on the list ~S."
2313 spec
'*!generic-function-fixups
*))
2315 (pushnew spec
*!early-generic-functions
* :test
#'equal
)
2316 (make-early-gf spec lambda-list lambda-list-p nil
2317 argument-precedence-order definition-source
2320 (defun make-early-gf (spec &optional lambda-list lambda-list-p
2321 function argument-precedence-order source-location
2323 (let ((fin (allocate-standard-funcallable-instance *sgf-wrapper
*)))
2324 (replace (fsc-instance-slots fin
) *sgf-slots-init
*)
2326 (set-funcallable-instance-function fin function
))
2327 (setf (gdefinition spec
) fin
)
2328 (!bootstrap-set-slot
'standard-generic-function fin
'name spec
)
2329 (!bootstrap-set-slot
'standard-generic-function fin
2330 'source source-location
)
2331 (!bootstrap-set-slot
'standard-generic-function fin
2332 '%documentation documentation
)
2333 (let ((arg-info (make-arg-info)))
2334 (setf (early-gf-arg-info fin
) arg-info
)
2336 (setf (info :function
:type spec
)
2338 (ftype-declaration-from-lambda-list lambda-list spec
))
2339 (info :function
:where-from spec
) :defined-method
)
2340 (if argument-precedence-order
2342 :lambda-list lambda-list
2343 :argument-precedence-order argument-precedence-order
)
2344 (set-arg-info fin
:lambda-list lambda-list
))))
2347 (defun safe-gf-dfun-state (generic-function)
2348 (if (eq (class-of generic-function
) *the-class-standard-generic-function
*)
2349 (clos-slots-ref (fsc-instance-slots generic-function
) +sgf-dfun-state-index
+)
2350 (gf-dfun-state generic-function
)))
2351 (defun (setf safe-gf-dfun-state
) (new-value generic-function
)
2352 (if (eq (class-of generic-function
) *the-class-standard-generic-function
*)
2353 (setf (clos-slots-ref (fsc-instance-slots generic-function
)
2354 +sgf-dfun-state-index
+)
2356 (setf (gf-dfun-state generic-function
) new-value
)))
2358 (defun set-dfun (gf &optional dfun cache info
)
2359 (let ((new-state (if (and dfun
(or cache info
))
2360 (list* dfun cache info
)
2363 ((eq **boot-state
** 'complete
)
2364 ;; Check that we are under the lock.
2366 (aver (eq sb-thread
:*current-thread
* (sb-thread:mutex-owner
(gf-lock gf
))))
2367 (setf (safe-gf-dfun-state gf
) new-state
))
2369 (setf (clos-slots-ref (get-slots gf
) +sgf-dfun-state-index
+)
2373 (defun gf-dfun-cache (gf)
2374 (let ((state (if (eq **boot-state
** 'complete
)
2375 (safe-gf-dfun-state gf
)
2376 (clos-slots-ref (get-slots gf
) +sgf-dfun-state-index
+))))
2379 (cons (cadr state
)))))
2381 (defun gf-dfun-info (gf)
2382 (let ((state (if (eq **boot-state
** 'complete
)
2383 (safe-gf-dfun-state gf
)
2384 (clos-slots-ref (get-slots gf
) +sgf-dfun-state-index
+))))
2387 (cons (cddr state
)))))
2389 (defconstant +sgf-name-index
+
2390 (!bootstrap-slot-index
'standard-generic-function
'name
))
2392 (defun !early-gf-name
(gf)
2393 (clos-slots-ref (get-slots gf
) +sgf-name-index
+))
2395 (defun gf-lambda-list (gf)
2396 (let ((arg-info (if (eq **boot-state
** 'complete
)
2398 (early-gf-arg-info gf
))))
2399 (if (eq :no-lambda-list
(arg-info-lambda-list arg-info
))
2400 (let ((methods (if (eq **boot-state
** 'complete
)
2401 (generic-function-methods gf
)
2402 (early-gf-methods gf
))))
2405 (warn "no way to determine the lambda list for ~S" gf
)
2407 (let* ((method (car (last methods
)))
2408 (ll (if (consp method
)
2409 (early-method-lambda-list method
)
2410 (method-lambda-list method
))))
2411 (create-gf-lambda-list ll
))))
2412 (arg-info-lambda-list arg-info
))))
2414 (defun note-gf-signature (fun-name lambda-list-p lambda-list
)
2415 (unless lambda-list-p
2416 ;; Use the existing lambda-list, if any. It is reasonable to do eg.
2418 ;; (if (fboundp name)
2419 ;; (ensure-generic-function name)
2420 ;; (ensure-generic-function name :lambda-list '(foo)))
2422 ;; in which case we end up here with no lambda-list in the first leg.
2423 (setf (values lambda-list lambda-list-p
)
2425 (values (generic-function-lambda-list (fdefinition fun-name
))
2427 ((or warning error
) ()
2428 (values nil nil
)))))
2432 (ftype-declaration-from-lambda-list lambda-list fun-name
)
2435 ;; FIXME: Ideally we would like to not clobber it, but because generic
2436 ;; functions assert their FTYPEs callers believing the FTYPE are left with
2437 ;; unsafe assumptions. Hence the clobbering. Be quiet when the new type
2438 ;; is a subtype of the old one, though -- even though the type is not
2439 ;; trusted anymore, the warning is still not quite as interesting.
2440 (when (and (eq :declared
(info :function
:where-from fun-name
))
2441 (not (csubtypep gf-type
(setf old-type
(proclaimed-ftype fun-name
)))))
2442 (style-warn "~@<Generic function ~S clobbers an earlier ~S proclamation ~S ~
2443 for the same name with ~S.~:@>"
2445 (type-specifier old-type
)
2446 (type-specifier gf-type
)))
2447 (setf (info :function
:type fun-name
) gf-type
2448 (info :function
:where-from fun-name
) :defined-method
)
2451 (labels ((resolve-class (context class-or-name environment
)
2452 (cond ((symbolp class-or-name
)
2453 (find-class class-or-name t environment
))
2454 ((classp class-or-name
)
2457 (error "~@<The ~A (~S) was neither a class nor a ~
2458 symbol that names a class.~@:>"
2459 context class-or-name
))))
2460 (resolve-and-finalize-class (class-or-name environment
)
2461 (let ((class (resolve-class ":GENERIC-FUNCTION-CLASS argument"
2462 class-or-name environment
)))
2463 (if (class-has-a-forward-referenced-superclass-p class
)
2464 ;; FIXME: reference MOP documentation -- this is an
2465 ;; additional requirement on our users
2466 (error "~@<The generic function class ~A is not ~
2469 (ensure-class-finalized class
))))
2470 (normalize-options (&rest options
&key
2472 (lambda-list nil lambda-list-p
)
2473 (generic-function-class 'standard-generic-function
)
2475 (let ((class (resolve-and-finalize-class
2476 generic-function-class environment
)))
2477 (collect ((initargs))
2478 (doplist (key value
) options
2480 ((:environment
:generic-function-class
))
2481 (:method-combination
2486 (destructuring-bind (type . options
) value
2487 (find-method-combination
2488 (class-prototype class
) type options
)))
2492 (initargs key
(resolve-class ":METHOD-CLASS argument"
2493 value environment
)))
2495 (initargs key value
))))
2496 (values class lambda-list lambda-list-p
(initargs))))))
2498 (defun real-ensure-gf-using-class--generic-function
2499 (existing fun-name
&rest options
&key
&allow-other-keys
)
2500 (multiple-value-bind
2501 (generic-function-class lambda-list lambda-list-p initargs
)
2502 (apply #'normalize-options options
)
2503 (unless (eq (class-of existing
) generic-function-class
)
2504 (change-class existing generic-function-class
))
2506 (apply #'reinitialize-instance existing initargs
)
2507 (note-gf-signature fun-name lambda-list-p lambda-list
))))
2509 (defun real-ensure-gf-using-class--null
2510 (existing fun-name
&rest options
&key
&allow-other-keys
)
2511 (declare (ignore existing
))
2512 (multiple-value-bind
2513 (generic-function-class lambda-list lambda-list-p initargs
)
2514 (apply #'normalize-options options
)
2516 (setf (gdefinition fun-name
)
2517 (apply #'make-instance generic-function-class
2518 :name fun-name initargs
))
2519 (note-gf-signature fun-name lambda-list-p lambda-list
)))))
2521 (defun safe-gf-arg-info (generic-function)
2522 (if (eq (class-of generic-function
) *the-class-standard-generic-function
*)
2523 (clos-slots-ref (fsc-instance-slots generic-function
)
2524 +sgf-arg-info-index
+)
2525 (gf-arg-info generic-function
)))
2527 ;;; FIXME: this function took on a slightly greater role than it
2528 ;;; previously had around 2005-11-02, when CSR fixed the bug whereby
2529 ;;; having more than one subclass of standard-generic-function caused
2530 ;;; the whole system to die horribly through a metacircle in
2531 ;;; GF-ARG-INFO. The fix is to be slightly more disciplined about
2532 ;;; calling accessor methods -- we call GET-GENERIC-FUN-INFO when
2533 ;;; computing discriminating functions, so we need to be careful about
2534 ;;; having a base case for the recursion, and we provide that with the
2535 ;;; STANDARD-GENERIC-FUNCTION case below. However, we are not (yet)
2536 ;;; as disciplined as CLISP's CLOS/MOP, and it would be nice to get to
2537 ;;; that stage, where all potentially dangerous cases are enumerated
2538 ;;; and stopped. -- CSR, 2005-11-02.
2539 (defun get-generic-fun-info (gf)
2540 ;; values nreq applyp metatypes nkeys arg-info
2541 (multiple-value-bind (applyp metatypes arg-info
)
2542 (let* ((arg-info (if (early-gf-p gf
)
2543 (early-gf-arg-info gf
)
2544 (safe-gf-arg-info gf
)))
2545 (metatypes (arg-info-metatypes arg-info
)))
2546 (values (arg-info-applyp arg-info
)
2551 (declare (fixnum nreq nkeys
))
2552 (dolist (x metatypes
)
2556 (values nreq applyp metatypes
2560 (defun generic-function-nreq (gf)
2561 (let* ((arg-info (if (early-gf-p gf
)
2562 (early-gf-arg-info gf
)
2563 (safe-gf-arg-info gf
)))
2564 (metatypes (arg-info-metatypes arg-info
)))
2565 (declare (list metatypes
))
2566 (length metatypes
)))
2568 (defun !early-make-a-method
(class qualifiers arglist specializers initargs doc
2569 &key slot-name object-class method-class-function
2571 (aver (notany #'sb-pcl
::eql-specializer-p specializers
))
2573 ;; Figure out whether we got class objects or class names as the
2574 ;; specializers and set parsed and unparsed appropriately. If we
2575 ;; got class objects, then we can compute unparsed, but if we
2576 ;; got class names we don't try to compute parsed.
2578 (if (every #'classp specializers
)
2579 (values specializers
2581 (if (eq s t
) t
(class-name s
)))
2583 (values () specializers
)))
2587 (getf initargs
:function
)
2588 (let ((mf (getf initargs
:function
)))
2590 (and (typep mf
'%method-function
)
2591 (%method-function-fast-function mf
)))
2593 ;; the parsed specializers. This is used by
2594 ;; EARLY-METHOD-SPECIALIZERS to cache the parse.
2595 ;; Note that this only comes into play when there is
2596 ;; more than one early method on an early gf.
2599 ;; A list to which REAL-MAKE-A-METHOD can be applied
2600 ;; to make a real method corresponding to this early
2603 (list class qualifiers arglist unparsed
2606 (list :slot-name slot-name
:object-class object-class
2607 :method-class-function method-class-function
))
2608 (list :definition-source definition-source
)))))
2609 (initialize-method-function initargs result
)
2612 (defun real-make-a-method
2613 (class qualifiers lambda-list specializers initargs doc
2614 &rest args
&key slot-name object-class method-class-function
2616 (if method-class-function
2617 (let* ((object-class (if (classp object-class
) object-class
2618 (find-class object-class
)))
2619 (slots (class-direct-slots object-class
))
2620 (slot-definition (find slot-name slots
2621 :key
#'slot-definition-name
)))
2623 (aver slot-definition
)
2624 (let ((initargs (list* :qualifiers qualifiers
:lambda-list lambda-list
2625 :specializers specializers
:documentation doc
2626 :slot-definition slot-definition
2627 :slot-name slot-name initargs
)))
2628 (apply #'make-instance
2629 (apply method-class-function object-class slot-definition
2631 :definition-source definition-source
2633 (apply #'make-instance class
:qualifiers qualifiers
2634 :lambda-list lambda-list
:specializers specializers
2635 :documentation doc
(append args initargs
))))
2637 (defun early-method-function (early-method)
2638 (values (cadr early-method
) (caddr early-method
)))
2640 (defun early-method-class (early-method)
2641 (find-class (car (fifth early-method
))))
2643 (defun early-method-standard-accessor-p (early-method)
2644 (let ((class (first (fifth early-method
))))
2645 (or (eq class
'standard-reader-method
)
2646 (eq class
'standard-writer-method
)
2647 (eq class
'standard-boundp-method
))))
2649 (defun early-method-standard-accessor-slot-name (early-method)
2650 (eighth (fifth early-method
)))
2652 ;;; Fetch the specializers of an early method. This is basically just
2653 ;;; a simple accessor except that when the second argument is t, this
2654 ;;; converts the specializers from symbols into class objects. The
2655 ;;; class objects are cached in the early method, this makes
2656 ;;; bootstrapping faster because the class objects only have to be
2660 ;;; The second argument should only be passed as T by
2661 ;;; early-lookup-method. This is to implement the rule that only when
2662 ;;; there is more than one early method on a generic function is the
2663 ;;; conversion from class names to class objects done. This
2664 ;;; corresponds to the fact that we are only allowed to have one
2665 ;;; method on any generic function up until the time classes exist.
2666 (defun early-method-specializers (early-method &optional objectsp
)
2667 (if (and (listp early-method
)
2668 (eq (car early-method
) :early-method
))
2669 (cond ((eq objectsp t
)
2670 (or (fourth early-method
)
2671 (setf (fourth early-method
)
2672 (mapcar #'find-class
(cadddr (fifth early-method
))))))
2674 (fourth (fifth early-method
))))
2675 (error "~S is not an early-method." early-method
)))
2677 (defun early-method-qualifiers (early-method)
2678 (second (fifth early-method
)))
2680 (defun early-method-lambda-list (early-method)
2681 (third (fifth early-method
)))
2683 (defun early-method-initargs (early-method)
2684 (fifth (fifth early-method
)))
2686 (defun (setf early-method-initargs
) (new-value early-method
)
2687 (setf (fifth (fifth early-method
)) new-value
))
2689 (defun !early-add-named-method
(generic-function-name qualifiers
2690 specializers arglist
&rest initargs
2691 &key documentation definition-source
2693 (let* (;; we don't need to deal with the :generic-function-class
2694 ;; argument here because the default,
2695 ;; STANDARD-GENERIC-FUNCTION, is right for all early generic
2696 ;; functions. (See REAL-ADD-NAMED-METHOD)
2697 (gf (ensure-generic-function generic-function-name
))
2699 (dolist (m (early-gf-methods gf
))
2700 (when (and (equal (early-method-specializers m
) specializers
)
2701 (equal (early-method-qualifiers m
) qualifiers
))
2703 (setf (getf (getf initargs
'plist
) :name
)
2704 (make-method-spec gf qualifiers specializers
))
2705 (let ((new (make-a-method 'standard-method qualifiers arglist
2706 specializers initargs documentation
2707 :definition-source definition-source
)))
2708 (when existing
(remove-method gf existing
))
2709 (add-method gf new
))))
2711 ;;; This is the early version of ADD-METHOD. Later this will become a
2712 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2713 ;;; special knowledge about ADD-METHOD.
2714 (defun add-method (generic-function method
)
2715 (when (not (fsc-instance-p generic-function
))
2716 (error "Early ADD-METHOD didn't get a funcallable instance."))
2717 (when (not (and (listp method
) (eq (car method
) :early-method
)))
2718 (error "Early ADD-METHOD didn't get an early method."))
2719 (push method
(early-gf-methods generic-function
))
2720 (set-arg-info generic-function
:new-method method
)
2721 (unless (assoc (!early-gf-name generic-function
)
2722 *!generic-function-fixups
*
2724 (update-dfun generic-function
)))
2726 ;;; This is the early version of REMOVE-METHOD. See comments on
2727 ;;; the early version of ADD-METHOD.
2728 (defun remove-method (generic-function method
)
2729 (when (not (fsc-instance-p generic-function
))
2730 (error "An early remove-method didn't get a funcallable instance."))
2731 (when (not (and (listp method
) (eq (car method
) :early-method
)))
2732 (error "An early remove-method didn't get an early method."))
2733 (setf (early-gf-methods generic-function
)
2734 (remove method
(early-gf-methods generic-function
)))
2735 (set-arg-info generic-function
)
2736 (unless (assoc (!early-gf-name generic-function
)
2737 *!generic-function-fixups
*
2739 (update-dfun generic-function
)))
2741 ;;; This is the early version of GET-METHOD. See comments on the early
2742 ;;; version of ADD-METHOD.
2743 (defun get-method (generic-function qualifiers specializers
2744 &optional
(errorp t
))
2745 (if (early-gf-p generic-function
)
2746 (or (dolist (m (early-gf-methods generic-function
))
2747 (when (and (or (equal (early-method-specializers m nil
)
2749 (equal (early-method-specializers m t
)
2751 (equal (early-method-qualifiers m
) qualifiers
))
2754 (error "can't get early method")
2756 (real-get-method generic-function qualifiers specializers errorp
)))
2758 ;; minor KLUDGE: a separate code component for this function allows GCing
2759 ;; a few symbols and their associated code that would otherwise be retained:
2760 ;; *!EARLY-{GENERIC-}FUNCTIONS*, *!GENERIC-FUNCTION-FIXUPS*
2761 (defun early-gf-primary-slow-method-fn (fn)
2762 (lambda (args next-methods
)
2763 (declare (ignore next-methods
))
2766 (defun !fix-early-generic-functions
()
2767 (let ((accessors nil
))
2768 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2769 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2770 (dolist (early-gf-spec *!early-generic-functions
*)
2771 (when (every #'early-method-standard-accessor-p
2772 (early-gf-methods (gdefinition early-gf-spec
)))
2773 (push early-gf-spec accessors
)))
2774 (dolist (spec (nconc accessors
2775 '(accessor-method-slot-name
2776 generic-function-methods
2780 slot-definition-location
2781 slot-definition-name
2784 class-precedence-list
2785 slot-boundp-using-class
2786 (setf slot-value-using-class
)
2787 slot-value-using-class
)))
2789 (setq *!early-generic-functions
*
2791 (delete spec
*!early-generic-functions
* :test
#'equal
))))
2793 (dolist (early-gf-spec *!early-generic-functions
*)
2794 (/show early-gf-spec
)
2795 (let* ((gf (gdefinition early-gf-spec
))
2796 (methods (mapcar (lambda (early-method)
2797 (let ((args (copy-list (fifth
2800 (early-method-specializers
2802 (apply #'real-make-a-method args
)))
2803 (early-gf-methods gf
))))
2804 (setf (generic-function-method-class gf
) *the-class-standard-method
*)
2805 (setf (generic-function-method-combination gf
)
2806 *standard-method-combination
*)
2807 (set-methods gf methods
)))
2809 (dolist (fn *!early-functions
*)
2811 (setf (gdefinition (car fn
)) (fdefinition (caddr fn
))))
2813 (loop for
(fspec method-combination . methods
) in
*!generic-function-fixups
*
2814 for gf
= (gdefinition fspec
) do
2815 (labels ((translate-source-location (function)
2816 ;; This is lifted from sb-introspect, OAOO and all that.
2817 (let* ((function-object (sb-kernel::%fun-fun function
))
2818 (function-header (sb-kernel:fun-code-header function-object
))
2819 (debug-info (sb-kernel:%code-debug-info function-header
))
2820 (debug-source (sb-c::debug-info-source debug-info
))
2821 (debug-fun (debug-info-debug-function function debug-info
)))
2822 (sb-c::%make-definition-source-location
2823 (sb-c::debug-source-namestring debug-source
)
2824 (sb-c::compiled-debug-info-tlf-number debug-info
)
2825 (sb-c::compiled-debug-fun-form-number debug-fun
))))
2826 (debug-info-debug-function (function debug-info
)
2827 (let ((map (sb-c::compiled-debug-info-fun-map debug-info
))
2828 (name (sb-kernel:%simple-fun-name
(sb-kernel:%fun-fun function
))))
2833 (sb-c::compiled-debug-fun-p x
)
2834 (eq (sb-c::compiled-debug-fun-name x
) name
)))
2839 (lambda-list specializers qualifiers fun-name
) spec
2840 (let* ((specializers (mapcar #'find-class specializers
))
2841 (fun-name (or fun-name fspec
))
2842 (fun (fdefinition fun-name
))
2843 (initargs (list :function
2845 (early-gf-primary-slow-method-fn fun
)
2846 `(call ,fun-name
)))))
2847 (declare (type function fun
))
2850 qualifiers lambda-list specializers initargs nil
2851 :definition-source
(translate-source-location fun
))))))
2852 (setf (generic-function-method-class gf
)
2853 *the-class-standard-method
*
2854 (generic-function-method-combination gf
)
2855 (ecase method-combination
2856 (standard *standard-method-combination
*)
2857 (or *or-method-combination
*)))
2858 (set-methods gf
(mapcar #'make-method methods
)))))
2860 (/show
"leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2862 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2863 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2864 ;;; is really implemented.
2865 (defun parse-defmethod (cdr-of-form)
2866 (declare (list cdr-of-form
))
2867 (let ((qualifiers ())
2869 (loop (if (and (car cdr-of-form
) (atom (car cdr-of-form
)))
2870 (push (pop cdr-of-form
) qualifiers
)
2871 (return (setq qualifiers
(nreverse qualifiers
)))))
2872 (setq spec-ll
(pop cdr-of-form
))
2873 (values qualifiers spec-ll cdr-of-form
)))
2875 (defun parse-specializers (generic-function specializers
)
2876 (declare (list specializers
))
2877 (flet ((parse (spec)
2878 (parse-specializer-using-class generic-function spec
)))
2879 (mapcar #'parse specializers
)))
2881 (defun unparse-specializers (generic-function specializers
)
2882 (declare (list specializers
))
2883 (flet ((unparse (spec)
2884 (unparse-specializer-using-class generic-function spec
)))
2885 (mapcar #'unparse specializers
)))
2887 (macrolet ((def (n name
)
2888 `(defun ,name
(lambda-list)
2889 (nth-value ,n
(parse-specialized-lambda-list lambda-list
)))))
2890 ;; We don't need these, but according to the unit tests,
2891 ;; they're mandated by AMOP.
2892 (def 1 extract-lambda-list
)
2893 (def 2 extract-specializer-names
))
2895 (define-condition specialized-lambda-list-error
2896 (reference-condition simple-program-error
)
2898 (:default-initargs
:references
(list '(:ansi-cl
:section
(3 4 3)))))
2901 ;; - the bound variables, without defaults, supplied-p vars, or &AUX vars.
2902 ;; - the lambda list without specializers.
2903 ;; - just the specializers
2904 (defun parse-specialized-lambda-list (arglist)
2905 (multiple-value-bind (llks specialized optional rest key aux
)
2909 :accept
(lambda-list-keyword-mask
2910 '(&optional
&rest
&key
&allow-other-keys
&aux
))
2911 :silent t
; never signal &OPTIONAL + &KEY style-warning
2912 :condition-class
'specialized-lambda-list-error
)
2913 (let ((required (mapcar (lambda (x) (if (listp x
) (car x
) x
)) specialized
)))
2914 (values (append required
2915 (mapcar #'parse-optional-arg-spec optional
)
2917 ;; Preserve keyword-names when given as (:KEYWORD var)
2918 (mapcar (lambda (x) (if (typep x
'(cons cons
))
2920 (parse-key-arg-spec x
))) key
))
2921 (make-lambda-list llks nil required optional rest key aux
)
2922 (mapcar (lambda (x) (if (listp x
) (cadr x
) t
)) specialized
)))))
2924 (setq **boot-state
** 'early
)
2926 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2927 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2928 ;;; walker stuff was only used for implementing stuff like that; maybe
2929 ;;; it's not needed any more? Hunt down what it was used for and see.
2931 (defun extract-the (form)
2932 (cond ((and (consp form
) (eq (car form
) 'the
))
2933 (aver (proper-list-of-length-p form
3))
2938 (defmacro with-slots
(slots instance
&body body
)
2939 (let ((in (gensym)))
2940 `(let ((,in
,instance
))
2941 (declare (ignorable ,in
))
2942 ,@(let ((instance (extract-the instance
)))
2943 (and (symbolp instance
)
2944 `((declare (%variable-rebinding
,in
,instance
)))))
2946 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2948 (if (symbolp slot-entry
)
2952 (if (symbolp slot-entry
)
2954 (cadr slot-entry
))))
2956 (slot-value ,in
',slot-name
))))
2960 (defmacro with-accessors
(slots instance
&body body
)
2961 (let ((in (gensym)))
2962 `(let ((,in
,instance
))
2963 (declare (ignorable ,in
))
2964 ,@(let ((instance (extract-the instance
)))
2965 (and (symbolp instance
)
2966 `((declare (%variable-rebinding
,in
,instance
)))))
2968 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2969 (let ((var-name (car slot-entry
))
2970 (accessor-name (cadr slot-entry
)))
2971 `(,var-name
(,accessor-name
,in
))))