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 (parse-specializer-using-class
184 ((generic-function specializer
)
185 (standard-generic-function t
)
187 real-parse-specializer-using-class
))
189 (unparse-specializer-using-class
191 ((generic-function specializer
)
192 (standard-generic-function t
)
194 real-unparse-specializer-using-class
))
196 (make-method-initargs-form
198 ((proto-generic-function proto-method
200 lambda-list environment
)
201 (standard-generic-function standard-method t t t
)
203 real-make-method-initargs-form
))
205 (compute-effective-method
207 ((generic-function combin applicable-methods
)
208 (generic-function standard-method-combination t
)
210 standard-compute-effective-method
)
211 ((generic-function combin applicable-methods
)
212 (generic-function short-method-combination t
)
214 short-compute-effective-method
))))
216 (defmacro defgeneric
(fun-name lambda-list
&body options
)
217 (declare (type list lambda-list
))
218 (unless (legal-fun-name-p fun-name
)
219 (error 'simple-program-error
220 :format-control
"illegal generic function name ~S"
221 :format-arguments
(list fun-name
)))
222 (check-gf-lambda-list lambda-list
)
225 (flet ((duplicate-option (name)
226 (error 'simple-program-error
227 :format-control
"The option ~S appears more than once."
228 :format-arguments
(list name
)))
229 (expand-method-definition (qab) ; QAB = qualifiers, arglist, body
230 (let* ((arglist-pos (position-if #'listp qab
))
231 (arglist (elt qab arglist-pos
))
232 (qualifiers (subseq qab
0 arglist-pos
))
233 (body (nthcdr (1+ arglist-pos
) qab
)))
234 `(push (defmethod ,fun-name
,@qualifiers
,arglist
,@body
)
235 (generic-function-initial-methods (fdefinition ',fun-name
))))))
236 (macrolet ((initarg (key) `(getf initargs
,key
)))
237 (dolist (option options
)
238 (let ((car-option (car option
)))
241 (dolist (spec (cdr option
))
243 (error 'simple-program-error
244 :format-control
"~@<Invalid declaration specifier in ~
246 :format-arguments
(list spec
)))
247 (when (member (first spec
)
248 ;; FIXME: this list is slightly weird.
249 ;; ANSI (on the DEFGENERIC page) in one
250 ;; place allows only OPTIMIZE; in
251 ;; another place gives this list of
252 ;; disallowed declaration specifiers.
253 ;; This seems to be the only place where
254 ;; the FUNCTION declaration is
255 ;; mentioned; TYPE seems to be missing.
256 ;; Very strange. -- CSR, 2002-10-21
257 '(declaration ftype function
258 inline notinline special
))
259 (error 'simple-program-error
260 :format-control
"The declaration specifier ~S ~
261 is not allowed inside DEFGENERIC."
262 :format-arguments
(list spec
)))
263 (if (or (eq 'optimize
(first spec
))
264 (info :declaration
:recognized
(first spec
)))
265 (push spec
(initarg :declarations
))
266 (warn "Ignoring unrecognized declaration in DEFGENERIC: ~S"
269 (when (initarg car-option
)
270 (duplicate-option car-option
))
271 (unless (symbolp (cadr option
))
272 (error 'simple-program-error
273 :format-control
"METHOD-COMBINATION name not a ~
275 :format-arguments
(list (cadr option
))))
276 (setf (initarg car-option
)
278 (:argument-precedence-order
279 (let* ((required (nth-value 1 (parse-lambda-list lambda-list
)))
280 (supplied (cdr option
)))
281 (unless (= (length required
) (length supplied
))
282 (error 'simple-program-error
283 :format-control
"argument count discrepancy in ~
284 :ARGUMENT-PRECEDENCE-ORDER clause."
285 :format-arguments nil
))
286 (when (set-difference required supplied
)
287 (error 'simple-program-error
288 :format-control
"unequal sets for ~
289 :ARGUMENT-PRECEDENCE-ORDER clause: ~
291 :format-arguments
(list required supplied
)))
292 (setf (initarg car-option
)
294 ((:documentation
:generic-function-class
:method-class
)
295 (unless (proper-list-of-length-p option
2)
296 (error "bad list length for ~S" option
))
297 (if (initarg car-option
)
298 (duplicate-option car-option
)
299 (setf (initarg car-option
) `',(cadr option
))))
301 (push (cdr option
) methods
))
303 ;; ANSI requires that unsupported things must get a
305 (error 'simple-program-error
306 :format-control
"unsupported option ~S"
307 :format-arguments
(list option
))))))
309 (when (initarg :declarations
)
310 (setf (initarg :declarations
)
311 `',(initarg :declarations
))))
313 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
314 (compile-or-load-defgeneric ',fun-name
))
315 (load-defgeneric ',fun-name
',lambda-list
316 (sb-c:source-location
) ,@initargs
)
317 ,@(mapcar #'expand-method-definition methods
)
318 (fdefinition ',fun-name
)))))
320 (defun compile-or-load-defgeneric (fun-name)
321 (proclaim-as-fun-name fun-name
)
322 (when (typep fun-name
'(cons (eql setf
)))
323 (sb-c::warn-if-setf-macro fun-name
))
324 (note-name-defined fun-name
:function
)
325 (unless (eq (info :function
:where-from fun-name
) :declared
)
326 ;; Hmm. This is similar to BECOME-DEFINED-FUN-NAME
327 ;; except that it doesn't clear an :ASSUMED-TYPE. Should it?
328 (setf (info :function
:where-from fun-name
) :defined
)
329 (setf (info :function
:type fun-name
)
330 (specifier-type 'function
))))
332 (defun load-defgeneric (fun-name lambda-list source-location
&rest initargs
)
333 (when (fboundp fun-name
)
334 (warn 'sb-kernel
:redefinition-with-defgeneric
336 :new-location source-location
)
337 (let ((fun (fdefinition fun-name
)))
338 (when (generic-function-p fun
)
339 (loop for method in
(generic-function-initial-methods fun
)
340 do
(remove-method fun method
))
341 (setf (generic-function-initial-methods fun
) '()))))
342 (apply #'ensure-generic-function
344 :lambda-list lambda-list
345 :definition-source source-location
348 (define-condition generic-function-lambda-list-error
349 (reference-condition simple-program-error
)
351 (:default-initargs
:references
(list '(:ansi-cl
:section
(3 4 2)))))
353 (defun check-gf-lambda-list (lambda-list)
354 (flet ((verify-each-atom-or-singleton (kind args
)
355 ;; PARSE-LAMBDA-LIST validates the skeleton,
356 ;; so just check for incorrect use of defaults.
357 ;; This works for both &OPTIONAL and &KEY.
359 (or (not (listp arg
))
361 (error 'generic-function-lambda-list-error
363 "~@<invalid ~A argument specifier ~S ~_in the ~
364 generic function lambda list ~S~:>"
365 :format-arguments
(list kind arg lambda-list
))))))
366 (multiple-value-bind (llks required optional rest keys
)
369 :accept
(lambda-list-keyword-mask
370 '(&optional
&rest
&key
&allow-other-keys
))
371 :condition-class
'generic-function-lambda-list-error
372 :context
"a generic function lambda list")
373 (declare (ignore llks required rest
))
374 ;; no defaults or supplied-p vars allowed for &OPTIONAL or &KEY
375 (verify-each-atom-or-singleton '&optional optional
)
376 (verify-each-atom-or-singleton '&key keys
))))
378 (defun check-method-lambda (method-lambda context
)
379 (unless (typep method-lambda
'(cons (eql lambda
)))
380 (error "~@<The METHOD-LAMBDA argument to ~
381 ~/sb-impl:print-symbol-with-prefix/, ~S, is not a lambda ~
383 context method-lambda
))
386 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
387 (fmakunbound 'defmethod
))
389 ;;; "defmethod is not required to perform any compile-time side effects."
390 ;;; and we don't do much other than to make the function be defined,
391 ;;; which means that checking of callers' arglists can only occur after called
392 ;;; methods are actually loaded.
393 (defmacro defmethod
(name &rest args
)
394 (multiple-value-bind (qualifiers lambda-list body
)
395 (parse-defmethod args)
397 (eval-when (:compile-toplevel
:execute
)
398 ;; :compile-toplevel is needed for subsequent forms
399 ;; :execute is needed for references to itself inside the body
400 (compile-or-load-defgeneric ',name
))
401 ;; KLUDGE: this double expansion is quite a monumental
402 ;; workaround: it comes about because of a fantastic interaction
403 ;; between the processing rules of CLHS 3.2.3.1 and the
404 ;; bizarreness of MAKE-METHOD-LAMBDA.
406 ;; MAKE-METHOD-LAMBDA can be called by the user, and if the
407 ;; lambda itself doesn't refer to outside bindings the return
408 ;; value must be compileable in the null lexical environment.
409 ;; However, the function must also refer somehow to the
410 ;; associated method object, so that it can call NO-NEXT-METHOD
411 ;; with the appropriate arguments if there is no next method --
412 ;; but when the function is generated, the method object doesn't
415 ;; In order to resolve this issue, we insert a literal cons cell
416 ;; into the body of the method lambda, return the same cons cell
417 ;; as part of the second (initargs) return value of
418 ;; MAKE-METHOD-LAMBDA, and a method on INITIALIZE-INSTANCE fills
419 ;; in the cell when the method is created. However, this
420 ;; strategy depends on having a fresh cons cell for every method
421 ;; lambda, which (without the workaround below) is skewered by
422 ;; the processing in CLHS 3.2.3.1, which permits implementations
423 ;; to macroexpand the bodies of EVAL-WHEN forms with both
424 ;; :COMPILE-TOPLEVEL and :LOAD-TOPLEVEL only once. The
425 ;; expansion below forces the double expansion in those cases,
426 ;; while expanding only once in the common case.
427 (eval-when (:load-toplevel
)
428 (%defmethod-expander
,name
,qualifiers
,lambda-list
,body
))
429 (eval-when (:execute
)
430 (%defmethod-expander
,name
,qualifiers
,lambda-list
,body
)))))
432 (defmacro %defmethod-expander
433 (name qualifiers lambda-list body
&environment env
)
434 (multiple-value-bind (proto-gf proto-method
)
435 (prototypes-for-make-method-lambda name
)
436 (expand-defmethod name proto-gf proto-method qualifiers
437 lambda-list body env
)))
440 (defun prototypes-for-make-method-lambda (name)
441 (if (not (eq **boot-state
** 'complete
))
443 (let ((gf?
(and (fboundp name
)
444 (gdefinition name
))))
446 (not (generic-function-p gf?
)))
447 (values (class-prototype (find-class 'standard-generic-function
))
448 (class-prototype (find-class 'standard-method
)))
450 (class-prototype (or (generic-function-method-class gf?
)
451 (find-class 'standard-method
))))))))
453 ;;; Take a name which is either a generic function name or a list specifying
454 ;;; a SETF generic function (like: (SETF <generic-function-name>)). Return
455 ;;; the prototype instance of the method-class for that generic function.
457 ;;; If there is no generic function by that name, this returns the
458 ;;; default value, the prototype instance of the class
459 ;;; STANDARD-METHOD. This default value is also returned if the spec
460 ;;; names an ordinary function or even a macro. In effect, this leaves
461 ;;; the signalling of the appropriate error until load time.
463 ;;; Note: During bootstrapping, this function is allowed to return NIL.
464 (defun method-prototype-for-gf (name)
465 (let ((gf?
(and (fboundp name
)
466 (gdefinition name
))))
467 (cond ((neq **boot-state
** 'complete
) nil
)
469 (not (generic-function-p gf?
))) ; Someone else MIGHT
470 ; error at load time.
471 (class-prototype (find-class 'standard-method
)))
473 (class-prototype (or (generic-function-method-class gf?
)
474 (find-class 'standard-method
)))))))
476 ;;; These are used to communicate the method name and lambda-list to
477 ;;; MAKE-METHOD-LAMBDA-INTERNAL.
478 (defvar *method-name
* nil
)
479 (defvar *method-lambda-list
* nil
)
481 (defun expand-defmethod (name proto-gf proto-method qualifiers lambda-list
483 (binding* (;; ENV could be of type SB!INTERPRETER:BASIC-ENV but I
484 ;; don't care to figure out what parts of PCL would have
485 ;; to change to accept that, so coerce.
486 (env (sb-kernel:coerce-to-lexenv env
))
487 ((nil unspecialized-lambda-list specializers
)
488 (parse-specialized-lambda-list lambda-list
))
489 (*method-name
* `(,name
,@qualifiers
,specializers
))
490 (method-lambda `(lambda ,unspecialized-lambda-list
,@body
))
491 ((method-function-lambda initargs new-lambda-list
)
492 (make-method-lambda-using-specializers
493 proto-gf proto-method qualifiers specializers method-lambda env
))
495 (make-method-initargs-form
496 proto-gf proto-method method-function-lambda initargs env
))
498 (make-method-specializers-form
499 proto-gf proto-method specializers env
)))
500 (mapc (lambda (specializer parameter
)
501 (when (typep specializer
'type-specifier
)
502 (with-current-source-form (parameter)
503 (check-deprecated-type specializer
))))
504 specializers lambda-list
)
505 ;; Note: We could DECLAIM the ftype of the generic function here,
506 ;; since ANSI specifies that we create it if it does not
507 ;; exist. However, I chose not to, because I think it's more
508 ;; useful to support a style of programming where every generic
509 ;; function has an explicit DEFGENERIC and any typos in DEFMETHODs
510 ;; are warned about. Otherwise
512 ;; (DEFGENERIC FOO-BAR-BLETCH (X))
513 ;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
514 ;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
515 ;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
516 ;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
517 ;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
519 ;; compiles without raising an error and runs without raising an
520 ;; error (since SIMPLE-VECTOR cases fall through to VECTOR) but
521 ;; still doesn't do what was intended. I hate that kind of bug
522 ;; (code which silently gives the wrong answer), so we don't do a
523 ;; DECLAIM here. -- WHN 20000229
524 (make-defmethod-form name qualifiers specializers-form
525 (or new-lambda-list unspecialized-lambda-list
)
527 (class-name (class-of proto-method
))
531 (defun interned-symbol-p (x)
532 (and (symbolp x
) (symbol-package x
)))
534 (defun make-defmethod-form
535 (name qualifiers specializers unspecialized-lambda-list
536 method-class-name initargs-form
)
537 (declare (sb-ext:muffle-conditions sb-ext
:code-deletion-note
))
540 (if (and (interned-symbol-p (fun-name-block-name name
))
541 (every #'interned-symbol-p qualifiers
)
544 (and (eq (car s
) 'eql
)
546 (let ((sv (constant-form-value (cadr s
))))
547 (or (interned-symbol-p sv
)
550 (standard-char-p sv
)))))
551 (interned-symbol-p s
)))
553 (consp initargs-form
)
554 (eq (car initargs-form
) 'list
*)
555 (memq (cadr initargs-form
) '(:function
))
556 (consp (setq fn
(caddr initargs-form
)))
557 (eq (car fn
) 'function
)
558 (consp (setq fn-lambda
(cadr fn
)))
559 (eq (car fn-lambda
) 'lambda
)
560 (bug "Really got here"))
561 (let* ((specls (mapcar (lambda (specl)
563 ;; CONSTANT-FORM-VALUE? What I
564 ;; kind of want to know, though,
565 ;; is what happens if we don't do
566 ;; this for some slow-method
567 ;; function because of a hairy
568 ;; lexenv -- is the only bad
569 ;; effect that the method
570 ;; function ends up unnamed? If
571 ;; so, couldn't we arrange to
573 `(,(car specl
) ,(eval (cadr specl
)))
576 (mname `(,(if (eq (cadr initargs-form
) :function
)
577 'slow-method
'fast-method
)
578 ,name
,@qualifiers
,specls
)))
580 (defun ,mname
,(cadr fn-lambda
)
582 ,(make-defmethod-form-internal
583 name qualifiers
`',specls
584 unspecialized-lambda-list method-class-name
585 `(list* ,(cadr initargs-form
)
587 ,@(cdddr initargs-form
)))))
588 (make-defmethod-form-internal
592 `(list ,@(mapcar (lambda (specializer)
593 (if (consp specializer
)
594 ``(,',(car specializer
)
595 ,,(cadr specializer
))
598 unspecialized-lambda-list
602 (defun make-defmethod-form-internal
603 (name qualifiers specializers-form unspecialized-lambda-list
604 method-class-name initargs-form
)
610 ',unspecialized-lambda-list
612 (sb-c:source-location
)))
614 (defmacro make-method-function
(method-lambda &environment env
)
615 (binding* (((proto-gf proto-method
)
616 (prototypes-for-make-method-lambda nil
))
617 ((method-function-lambda initargs
)
618 (make-method-lambda proto-gf proto-method method-lambda env
))) ; FIXME: coerce-to-lexenv?
619 (make-method-initargs-form
620 proto-gf proto-method method-function-lambda initargs env
)))
622 (defun real-make-method-initargs-form (proto-gf proto-method
623 method-lambda initargs env
)
624 (declare (ignore proto-gf proto-method
))
625 (check-method-lambda method-lambda
'make-method-initargs
)
626 (make-method-initargs-form-internal method-lambda initargs env
))
628 (unless (fboundp 'make-method-initargs-form
)
629 (setf (gdefinition 'make-method-initargs-form
)
630 (symbol-function 'real-make-method-initargs-form
)))
632 (defun real-make-method-lambda-using-specializers
633 (proto-gf proto-method qualifiers specializers method-lambda env
)
634 (declare (ignore qualifiers
))
635 (check-method-lambda method-lambda
'make-method-lambda
) ; TODO remove check in make-method-lambda
636 ;; Default behavior: delegate to MAKE-METHOD-LAMBDA.
637 (let* ((lambda-list (second method-lambda
))
638 (*method-lambda-list
*
640 (mapcar #'list
(subseq lambda-list
0 (length specializers
)) specializers
)
641 (subseq lambda-list
(length specializers
)))))
642 (make-method-lambda proto-gf proto-method method-lambda env
)))
644 (unless (fboundp 'make-method-lambda-using-specializers
)
645 (setf (gdefinition 'make-method-lambda-using-specializers
)
646 (symbol-function 'real-make-method-lambda-using-specializers
)))
648 ;;; When bootstrapping PCL MAKE-METHOD-LAMBDA starts out as a regular
649 ;;; function: REAL-MAKE-METHOD-LAMBDA set to the fdefinition of
650 ;;; MAKE-METHOD-LAMBDA. Once generic functions are born,
651 ;;; REAL-MAKE-METHOD-LAMBDA is used to implement the default method.
652 ;;; MAKE-METHOD-LAMBDA-INTERNAL is split out into a separate function
653 ;;; so that changing it in a live image is easy, and changes actually
655 (defun real-make-method-lambda (proto-gf proto-method method-lambda env
)
656 (make-method-lambda-internal proto-gf proto-method method-lambda env
))
658 (unless (fboundp 'make-method-lambda
)
659 (setf (gdefinition 'make-method-lambda
)
660 (symbol-function 'real-make-method-lambda
)))
662 (defun declared-specials (declarations)
663 (loop for
(declare . specifiers
) in declarations
664 append
(loop for specifier in specifiers
665 when
(eq 'special
(car specifier
))
666 append
(cdr specifier
))))
668 (defun make-method-lambda-internal (proto-gf proto-method method-lambda env
)
669 (declare (ignore proto-gf proto-method
))
670 (check-method-lambda method-lambda
'make-method-lambda
)
671 (multiple-value-bind (real-body declarations documentation
)
672 (parse-body (cddr method-lambda
) t
)
673 ;; We have the %METHOD-NAME declaration in the place where we expect it only
674 ;; if there is are no non-standard prior MAKE-METHOD-LAMBDA methods -- or
675 ;; unless they're fantastically unintrusive.
676 (let* ((method-name *method-name
*)
677 (method-lambda-list *method-lambda-list
*)
678 ;; Macroexpansion caused by code-walking may call make-method-lambda and
679 ;; end up with wrong values
681 (*method-lambda-list
* nil
)
682 (generic-function-name (when method-name
(car method-name
)))
683 (specialized-lambda-list (or method-lambda-list
684 (ecase (car method-lambda
)
685 (lambda (second method-lambda
))
686 (named-lambda (third method-lambda
)))))
687 ;; the method-cell is a way of communicating what method a
688 ;; method-function implements, for the purpose of
689 ;; NO-NEXT-METHOD. We need something that can be shared
690 ;; between function and initargs, but not something that
691 ;; will be coalesced as a constant (because we are naughty,
692 ;; oh yes) with the expansion of any other methods in the
693 ;; same file. -- CSR, 2007-05-30
694 (method-cell (list (make-symbol "METHOD-CELL"))))
695 (multiple-value-bind (parameters lambda-list specializers
)
696 (parse-specialized-lambda-list specialized-lambda-list
)
697 (let* ((required-parameters
698 (mapcar (lambda (r s
) (declare (ignore s
)) r
)
701 (slots (mapcar #'list required-parameters
))
704 ;; These declarations seem to be used by PCL to pass
705 ;; information to itself; when I tried to delete 'em
706 ;; ca. 0.6.10 it didn't work. I'm not sure how
707 ;; they work, but note the (VAR-DECLARATION '%CLASS ..)
708 ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30
710 (mapcar (lambda (a s
) (and (symbolp s
)
715 ;; These TYPE declarations weren't in the original
716 ;; PCL code, but the Python compiler likes them a
717 ;; lot. (We're telling the compiler about our
718 ;; knowledge of specialized argument types so that
719 ;; it can avoid run-time type dispatch overhead,
720 ;; which can be a huge win for Python.)
721 ,@(let ((specials (declared-specials declarations
)))
722 (mapcar (lambda (par spec
)
723 (parameter-specializer-declaration-in-defmethod
724 par spec specials env
))
728 ;; Remove the documentation string and insert the
729 ;; appropriate class declarations. The documentation
730 ;; string is removed to make it easy for us to insert
731 ;; new declarations later, they will just go after the
732 ;; CADR of the method lambda. The class declarations
733 ;; are inserted to communicate the class of the method's
734 ;; arguments to the code walk.
735 `(lambda ,lambda-list
736 ;; The default ignorability of method parameters
737 ;; doesn't seem to be specified by ANSI. PCL had
738 ;; them basically ignorable but was a little
739 ;; inconsistent. E.g. even though the two
740 ;; method definitions
741 ;; (DEFMETHOD FOO ((X T) (Y T)) "Z")
742 ;; (DEFMETHOD FOO ((X T) Y) "Z")
743 ;; are otherwise equivalent, PCL treated Y as
744 ;; ignorable in the first definition but not in the
745 ;; second definition. We make all required
746 ;; parameters ignorable as a way of systematizing
747 ;; the old PCL behavior. -- WHN 2000-11-24
748 (declare (ignorable ,@required-parameters
))
751 (block ,(fun-name-block-name generic-function-name
)
753 (constant-value-p (and (null (cdr real-body
))
754 (constantp (car real-body
))))
755 (constant-value (and constant-value-p
756 (constant-form-value (car real-body
))))
757 (plist (and constant-value-p
758 (or (typep constant-value
759 '(or number character
))
760 (and (symbolp constant-value
)
761 (symbol-package constant-value
)))
762 (list :constant-value constant-value
)))
763 (applyp (dolist (p lambda-list nil
)
764 (cond ((memq p
'(&optional
&rest
&key
))
768 (multiple-value-bind (walked-lambda call-next-method-p setq-p
770 (walk-method-lambda method-lambda
774 (multiple-value-bind (walked-lambda-body
776 walked-documentation
)
777 (parse-body (cddr walked-lambda
) t
)
778 (declare (ignore walked-documentation
))
779 (when (some #'cdr slots
)
780 (let ((slot-name-lists (slot-name-lists-from-slots slots
)))
782 `(,@(when slot-name-lists
783 `(:slot-name-lists
,slot-name-lists
))
785 (setq walked-lambda-body
786 `((pv-binding (,required-parameters
790 :slot-name-lists
',slot-name-lists
)))
791 ,@walked-lambda-body
)))))
792 (when (and (memq '&key lambda-list
)
793 (not (memq '&allow-other-keys lambda-list
)))
794 (let ((aux (memq '&aux lambda-list
)))
795 (setq lambda-list
(nconc (ldiff lambda-list aux
)
796 (list '&allow-other-keys
)
798 (values `(lambda (.method-args. .next-methods.
)
799 (simple-lexical-method-functions
800 (,lambda-list .method-args. .next-methods.
802 ,(when call-next-method-p t
)
804 :parameters-setqd
,parameters-setqd
805 :method-cell
,method-cell
807 ,@walked-declarations
809 (declare (disable-package-locks
810 %parameter-binding-modified
))
811 (symbol-macrolet ((%parameter-binding-modified
812 ',@parameters-setqd
))
813 (declare (enable-package-locks
814 %parameter-binding-modified
))
815 ,@walked-lambda-body
))))
816 `(,@(when call-next-method-p
`(method-cell ,method-cell
))
817 ,@(when (member call-next-method-p
'(:simple nil
))
818 '(simple-next-method-call t
))
819 ,@(when plist
`(plist ,plist
))
820 ,@(when documentation
`(:documentation
,documentation
)))))))))))
822 (defun real-make-method-specializers-form
823 (proto-generic-function proto-method specializer-names environment
)
824 (flet ((make-parse-form (name)
825 (make-specializer-form-using-class
826 proto-generic-function proto-method name environment
)))
827 `(list ,@(mapcar #'make-parse-form specializer-names
))))
829 (unless (fboundp 'make-method-specializers-form
)
830 (setf (gdefinition 'make-method-specializers-form
)
831 (symbol-function 'real-make-method-specializers-form
)))
833 (defun real-make-specializer-form-using-class/t
834 (proto-generic-function proto-method specializer-name environment
)
835 (declare (ignore proto-generic-function proto-method environment
))
836 (error 'simple-reference-error
838 "~@<~S is not a valid parameter specializer name.~@:>"
839 :format-arguments
(list specializer-name
)
840 :references
(list '(:ansi-cl
:macro defmethod
)
841 '(:ansi-cl
:glossary
"parameter specializer name"))))
843 (defun real-make-specializer-form-using-class/specializer
844 (proto-generic-function proto-method specializer-name environment
)
845 (declare (ignore proto-generic-function proto-method environment
))
846 (when (eq **boot-state
** 'complete
)
849 (defun real-make-specializer-form-using-class/symbol
850 (proto-generic-function proto-method specializer-name environment
)
851 (declare (ignore proto-generic-function proto-method environment
))
852 `(find-class ',specializer-name
))
854 (defun real-make-specializer-form-using-class/cons
855 (proto-generic-function proto-method specializer-name environment
)
856 (declare (ignore proto-generic-function proto-method environment
))
857 ;; In case of unknown specializer or known specializer with syntax
858 ;; error, TYPECASE may fall through to default method with error
860 (typecase specializer-name
861 ((cons (eql eql
) (cons t null
))
862 `(intern-eql-specializer ,(second specializer-name
)))
863 ((cons (eql class-eq
) (cons t null
))
864 `(class-eq-specializer (find-class ',(second specializer-name
))))))
866 (defun real-make-specializer-form-using-class
867 (proto-generic-function proto-method specializer-name environment
)
870 `(typecase specializer-name
875 'real-make-specializer-form-using-class
'#:/ type
)))
878 proto-generic-function proto-method specializer-name environment
))))
880 ; ^ apparently not needed during bootstrapping
884 (unless (fboundp 'make-specializer-form-using-class
)
885 (setf (gdefinition 'make-specializer-form-using-class
)
886 (symbol-function 'real-make-specializer-form-using-class
)))
888 (defun real-parse-specializer-using-class (generic-function specializer
)
889 (let ((result (specializer-from-type specializer
)))
890 (if (specializerp result
)
892 (error "~@<~S cannot be parsed as a specializer for ~S.~@:>"
893 specializer generic-function
))))
895 (unless (fboundp 'parse-specializer-using-class
)
896 (setf (gdefinition 'parse-specializer-using-class
)
897 (symbol-function 'real-parse-specializer-using-class
)))
899 (defun real-unparse-specializer-using-class (generic-function specializer
)
900 (if (specializerp specializer
)
901 ;; FIXME: this HANDLER-CASE is a bit of a hammer to crack a nut:
902 ;; the idea is that we want to unparse permissively, so that the
903 ;; lazy (or rather the "portable") specializer extender (who
904 ;; does not define methods on these new SBCL-specific MOP
905 ;; functions) can still subclass specializer and define methods
906 ;; without everything going wrong. Making it cleaner and
907 ;; clearer that that is what we are defending against would be
908 ;; nice. -- CSR, 2007-06-01
910 (let ((type (specializer-type specializer
)))
911 (if (and (consp type
) (eq (car type
) 'class
))
912 (let* ((class (cadr type
))
913 (class-name (class-name class
)))
914 (if (eq class
(find-class class-name nil
))
918 (error () specializer
))
919 (error "~@<~S is not a legal specializer for ~S.~@:>"
920 specializer generic-function
)))
922 (unless (fboundp 'unparse-specializer-using-class
)
923 (setf (gdefinition 'unparse-specializer-using-class
)
924 (symbol-function 'real-unparse-specializer-using-class
)))
926 ;;; a helper function for creating Python-friendly type declarations
927 ;;; in DEFMETHOD forms.
929 ;;; We're too lazy to cons up a new environment for this, so we just pass in
930 ;;; the list of locally declared specials in addition to the old environment.
931 (defun parameter-specializer-declaration-in-defmethod
932 (parameter specializer specials env
)
933 (cond ((and (consp specializer
)
934 (eq (car specializer
) 'eql
))
935 ;; KLUDGE: ANSI, in its wisdom, says that
936 ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at
937 ;; DEFMETHOD expansion time. Thus, although one might think
939 ;; (DEFMETHOD FOO ((X PACKAGE)
942 ;; the PACKAGE and (EQL 12) forms are both parallel type
943 ;; names, they're not, as is made clear when you do
944 ;; (DEFMETHOD FOO ((X PACKAGE)
947 ;; where Y needs to be a symbol named "BAR", not some cons
948 ;; made by (CONS 'QUOTE 'BAR). I.e. when the
949 ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument
950 ;; to be of type (EQL X). It'd be easy to transform one to
951 ;; the other, but it'd be somewhat messier to do so while
952 ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd
953 ;; once. (The new code wouldn't be messy, but it'd require a
954 ;; big transformation of the old code.) So instead we punt.
958 ;; KLUDGE: For some low-level implementation
959 ;; classes, perhaps because of some problems related
960 ;; to the incomplete integration of PCL into SBCL's
961 ;; type system, some specializer classes can't be
962 ;; declared as argument types. E.g.
963 ;; (DEFMETHOD FOO ((X SLOT-OBJECT))
964 ;; (DECLARE (TYPE SLOT-OBJECT X))
967 ;; (DEFSTRUCT BAR A B)
969 ;; perhaps because of the way that STRUCTURE-OBJECT
970 ;; inherits both from SLOT-OBJECT and from
971 ;; SB-KERNEL:INSTANCE. In an effort to sweep such
972 ;; problems under the rug, we exclude these problem
973 ;; cases by blacklisting them here. -- WHN 2001-01-19
974 (list 'slot-object
#+nil
(find-class 'slot-object
)))
976 ((not (eq **boot-state
** 'complete
))
977 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
978 ;; types which don't match their specializers. (Specifically,
979 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
980 ;; second argument.) Hopefully it only does this kind of
981 ;; weirdness when bootstrapping.. -- WHN 20000610
983 ((typep specializer
'eql-specializer
)
984 `(type (eql ,(eql-specializer-object specializer
)) ,parameter
))
985 ((or (var-special-p parameter env
) (member parameter specials
))
986 ;; Don't declare types for special variables -- our rebinding magic
987 ;; for SETQ cases don't work right there as SET, (SETF SYMBOL-VALUE),
988 ;; etc. make things undecidable.
991 ;; Otherwise, we can usually make Python very happy.
993 ;; KLUDGE: Since INFO doesn't work right for class objects here,
994 ;; and they are valid specializers, see if the specializer is
995 ;; a named class, and use the name in that case -- otherwise
996 ;; the class instance is ok, since info will just return NIL, NIL.
998 ;; We still need to deal with the class case too, but at
999 ;; least #.(find-class 'integer) and integer as equivalent
1000 ;; specializers with this.
1001 (let* ((specializer-nameoid
1002 (if (and (typep specializer
'class
)
1003 (let ((name (class-name specializer
)))
1004 (and name
(symbolp name
)
1005 (eq specializer
(find-class name nil
)))))
1006 (class-name specializer
)
1008 (kind (info :type
:kind specializer-nameoid
)))
1010 (flet ((specializer-nameoid-class ()
1011 (typecase specializer-nameoid
1012 (symbol (find-class specializer-nameoid nil
))
1013 (class specializer-nameoid
)
1014 (class-eq-specializer
1015 (specializer-class specializer-nameoid
))
1018 ((:primitive
) `(type ,specializer-nameoid
,parameter
))
1020 (let ((class (specializer-nameoid-class)))
1021 ;; CLASS can be null here if the user has
1022 ;; erroneously tried to use a defined type as a
1023 ;; specializer; it can be a non-SYSTEM-CLASS if
1024 ;; the user defines a type and calls (SETF
1025 ;; FIND-CLASS) in a consistent way.
1026 (when (and class
(typep class
'system-class
))
1027 `(type ,(class-name class
) ,parameter
))))
1029 (let ((class (specializer-nameoid-class)))
1032 (if (typep class
'(or system-class structure-class
))
1033 `(type ,class
,parameter
)
1034 ;; don't declare CLOS classes as parameters;
1035 ;; it's too expensive.
1038 ;; we can get here, and still not have a failure
1039 ;; case, by doing MOP programming like (PROGN
1040 ;; (ENSURE-CLASS 'FOO) (DEFMETHOD BAR ((X FOO))
1041 ;; ...)). Best to let the user know we haven't
1042 ;; been able to extract enough information:
1044 "~@<can't find type for specializer ~S in ~S.~@:>"
1046 'parameter-specializer-declaration-in-defmethod
)
1048 ((:forthcoming-defclass-type
)
1051 ;;; For passing a list (groveled by the walker) of the required
1052 ;;; parameters whose bindings are modified in the method body to the
1053 ;;; optimized-slot-value* macros.
1054 (define-symbol-macro %parameter-binding-modified
())
1056 (defmacro simple-lexical-method-functions
((lambda-list
1062 ,method-args
,next-methods
1063 (bind-simple-lexical-method-functions (,method-args
,next-methods
1065 (bind-args (,lambda-list
,method-args
)
1068 (defmacro fast-lexical-method-functions
((lambda-list
1074 `(bind-fast-lexical-method-functions (,args
,rest-arg
,next-method-call
,lmf-options
)
1075 (bind-args (,(nthcdr (length args
) lambda-list
) ,rest-arg
)
1078 (defmacro bind-simple-lexical-method-functions
1079 ((method-args next-methods
(&key call-next-method-p setq-p
1080 parameters-setqd applyp method-cell
))
1083 (declare (ignore parameters-setqd
))
1084 (if (not (or call-next-method-p setq-p applyp
))
1085 ;; always provide the lexical function NEXT-METHOD-P.
1086 ;; I would think this to be a good candidate for declaring INLINE
1087 ;; but that's not the way it was done before.
1088 `(flet ((next-method-p () (not (null (car ,next-methods
)))))
1089 (declare (ignorable #'next-method-p
))
1091 `(let ((.next-method.
(car ,next-methods
))
1092 (,next-methods
(cdr ,next-methods
)))
1093 (declare (ignorable .next-method.
,next-methods
))
1094 (flet (,@(when call-next-method-p
1095 `((call-next-method (&rest cnm-args
)
1096 (declare (dynamic-extent cnm-args
))
1097 ,@(if (safe-code-p env
)
1098 `((%check-cnm-args cnm-args
1103 (funcall (if (std-instance-p .next-method.
)
1104 (method-function .next-method.
)
1105 .next-method.
) ; for early methods
1106 (or cnm-args
,method-args
)
1108 (apply #'call-no-next-method
1110 (or cnm-args
,method-args
))))))
1111 (next-method-p () (not (null .next-method.
))))
1112 (declare (ignorable #'next-method-p
))
1115 (defun call-no-next-method (method-cell &rest args
)
1116 (let ((method (car method-cell
)))
1118 ;; Can't easily provide a RETRY restart here, as the return value here is
1119 ;; for the method, not the generic function.
1120 (apply #'no-next-method
(method-generic-function method
)
1123 (defun call-no-applicable-method (gf args
)
1125 (apply #'no-applicable-method gf args
)
1127 :report
"Retry calling the generic function."
1130 (defun call-no-primary-method (gf args
)
1132 (apply #'no-primary-method gf args
)
1134 :report
"Retry calling the generic function."
1137 (defstruct (method-call (:copier nil
))
1138 (function #'identity
:type function
)
1140 (defstruct (constant-method-call (:copier nil
) (:include method-call
))
1143 #-sb-fluid
(declaim (sb-ext:freeze-type method-call
))
1145 (defmacro invoke-method-call1
(function args cm-args
)
1146 `(let ((.function.
,function
)
1148 (.cm-args.
,cm-args
))
1149 (if (and .cm-args.
(null (cdr .cm-args.
)))
1150 (funcall .function. .args.
(car .cm-args.
))
1151 (apply .function. .args. .cm-args.
))))
1153 (defmacro invoke-method-call
(method-call restp
&rest required-args
+rest-arg
)
1154 `(invoke-method-call1 (method-call-function ,method-call
)
1156 `(list* ,@required-args
+rest-arg
)
1157 `(list ,@required-args
+rest-arg
))
1158 (method-call-call-method-args ,method-call
)))
1160 (defstruct (fast-method-call (:copier nil
))
1161 (function #'identity
:type function
)
1165 (defstruct (constant-fast-method-call
1166 (:copier nil
) (:include fast-method-call
))
1169 #-sb-fluid
(declaim (sb-ext:freeze-type fast-method-call
))
1171 ;; The two variants of INVOKE-FAST-METHOD-CALL differ in how REST-ARGs
1172 ;; are handled. The first one will get REST-ARG as a single list (as
1173 ;; the last argument), and will thus need to use APPLY. The second one
1174 ;; will get them as a &MORE argument, so we can pass the arguments
1175 ;; directly with MULTIPLE-VALUE-CALL and %MORE-ARG-VALUES.
1177 (defmacro invoke-fast-method-call
(method-call restp
&rest required-args
+rest-arg
)
1178 `(,(if restp
'apply
'funcall
) (fast-method-call-function ,method-call
)
1179 (fast-method-call-pv ,method-call
)
1180 (fast-method-call-next-method-call ,method-call
)
1181 ,@required-args
+rest-arg
))
1183 (defmacro invoke-fast-method-call
/more
(method-call
1186 &rest required-args
)
1187 (macrolet ((generate-call (n)
1188 ``(funcall (fast-method-call-function ,method-call
)
1189 (fast-method-call-pv ,method-call
)
1190 (fast-method-call-next-method-call ,method-call
)
1192 ,@(loop for x below
,n
1193 collect
`(sb-c::%more-arg
,more-context
,x
)))))
1194 ;; The cases with only small amounts of required arguments passed
1195 ;; are probably very common, and special-casing speeds them up by
1196 ;; a factor of 2 with very little effect on the other
1197 ;; cases. Though it'd be nice to have the generic case be equally
1200 (0 ,(generate-call 0))
1201 (1 ,(generate-call 1))
1202 (t (multiple-value-call (fast-method-call-function ,method-call
)
1203 (values (fast-method-call-pv ,method-call
))
1204 (values (fast-method-call-next-method-call ,method-call
))
1206 (sb-c::%more-arg-values
,more-context
0 ,more-count
))))))
1208 (defstruct (fast-instance-boundp (:copier nil
))
1209 (index 0 :type fixnum
))
1211 #-sb-fluid
(declaim (sb-ext:freeze-type fast-instance-boundp
))
1213 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
1214 (defvar *allow-emf-call-tracing-p
* nil
)
1215 (defvar *enable-emf-call-tracing-p
* #-sb-show nil
#+sb-show t
))
1217 ;;;; effective method functions
1219 (defvar *emf-call-trace-size
* 200)
1220 (defvar *emf-call-trace
* nil
)
1221 (defvar *emf-call-trace-index
* 0)
1223 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
1224 ;;; without explanation. It appears to be intended for debugging, so
1225 ;;; it might be useful someday, so I haven't deleted it.
1226 ;;; But it isn't documented and isn't used for anything now, so
1227 ;;; I've conditionalized it out of the base system. -- WHN 19991213
1229 (defun show-emf-call-trace ()
1230 (when *emf-call-trace
*
1231 (let ((j *emf-call-trace-index
*)
1232 (*enable-emf-call-tracing-p
* nil
))
1233 (format t
"~&(The oldest entries are printed first)~%")
1234 (dotimes-fixnum (i *emf-call-trace-size
*)
1235 (let ((ct (aref *emf-call-trace
* j
)))
1236 (when ct
(print ct
)))
1238 (when (= j
*emf-call-trace-size
*)
1241 (defun trace-emf-call-internal (emf format args
)
1242 (unless *emf-call-trace
*
1243 (setq *emf-call-trace
* (make-array *emf-call-trace-size
*)))
1244 (setf (aref *emf-call-trace
* *emf-call-trace-index
*)
1245 (list* emf format args
))
1246 (incf *emf-call-trace-index
*)
1247 (when (= *emf-call-trace-index
* *emf-call-trace-size
*)
1248 (setq *emf-call-trace-index
* 0)))
1250 (defmacro trace-emf-call
(emf format args
)
1251 (when *allow-emf-call-tracing-p
*
1252 `(when *enable-emf-call-tracing-p
*
1253 (trace-emf-call-internal ,emf
,format
,args
))))
1255 (defmacro invoke-effective-method-function-fast
1256 (emf restp
&key required-args rest-arg more-arg
)
1258 (trace-emf-call ,emf
,restp
(list ,@required-args rest-arg
))
1260 `(invoke-fast-method-call/more
,emf
1263 `(invoke-fast-method-call ,emf
1268 (defun effective-method-optimized-slot-access-clause
1269 (emf restp required-args
)
1270 ;; "What," you may wonder, "do these next two clauses do?" In that
1271 ;; case, you are not a PCL implementor, for they considered this to
1272 ;; be self-documenting.:-| Or CSR, for that matter, since he can
1273 ;; also figure it out by looking at it without breaking stride. For
1274 ;; the rest of us, though: From what the code is doing with .SLOTS.
1275 ;; and whatnot, evidently it's implementing SLOT-VALUEish and
1276 ;; GET-SLOT-VALUEish things. Then we can reason backwards and
1277 ;; conclude that setting EMF to a FIXNUM is an optimized way to
1278 ;; represent these slot access operations.
1280 (let ((length (length required-args
)))
1283 (let* ((.slots.
(get-slots-or-nil
1284 ,(car required-args
)))
1285 (value (when .slots.
(clos-slots-ref .slots.
,emf
))))
1286 (if (eq value
+slot-unbound
+)
1287 (slot-unbound-internal ,(car required-args
)
1292 (let ((.new-value.
,(car required-args
))
1293 (.slots.
(get-slots-or-nil
1294 ,(cadr required-args
))))
1296 (setf (clos-slots-ref .slots.
,emf
) .new-value.
)))))))
1297 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
1298 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
1299 ;; there was no explanation and presumably the code is 10+
1300 ;; years stale, I simply deleted it. -- WHN)
1303 ;;; Before SBCL 0.9.16.7 instead of
1304 ;;; INVOKE-NARROW-EFFECTIVE-METHOD-FUNCTION we passed a (THE (OR
1305 ;;; FUNCTION METHOD-CALL FAST-METHOD-CALL) EMF) form as the EMF. Now,
1306 ;;; to make less work for the compiler we take a path that doesn't
1307 ;;; involve the slot-accessor clause (where EMF is a FIXNUM) at all.
1308 (macrolet ((def (name &optional narrow
)
1309 `(defmacro ,name
(emf restp
&key required-args rest-arg more-arg
)
1310 (unless (constantp restp
)
1311 (error "The RESTP argument is not constant."))
1312 (setq restp
(constant-form-value restp
))
1313 (with-unique-names (emf-n)
1315 (declare (optimize (sb-c:insert-step-conditions
0)))
1316 (let ((,emf-n
,emf
))
1317 (trace-emf-call ,emf-n
,restp
(list ,@required-args
,@rest-arg
))
1321 `(invoke-fast-method-call/more
,emf-n
1324 `(invoke-fast-method-call ,emf-n
1329 `(effective-method-optimized-slot-access-clause
1330 emf-n restp required-args
))
1332 (invoke-method-call ,emf-n
,restp
,@required-args
1336 `(apply ,emf-n
,@required-args
,@rest-arg
)
1337 `(funcall ,emf-n
,@required-args
1338 ,@rest-arg
))))))))))
1339 (def invoke-effective-method-function nil
)
1340 (def invoke-narrow-effective-method-function t
))
1342 (defun invoke-emf (emf args
)
1343 (trace-emf-call emf t args
)
1346 (let* ((arg-info (fast-method-call-arg-info emf
))
1347 (restp (cdr arg-info
))
1348 (nreq (car arg-info
)))
1350 (apply (fast-method-call-function emf
)
1351 (fast-method-call-pv emf
)
1352 (fast-method-call-next-method-call emf
)
1356 (invoke-fast-method-call emf nil
)
1357 (error 'simple-program-error
1358 :format-control
"invalid number of arguments: 0"
1359 :format-arguments nil
)))
1362 (invoke-fast-method-call emf nil
(car args
))
1363 (error 'simple-program-error
1364 :format-control
"invalid number of arguments: 1"
1365 :format-arguments nil
)))
1368 (invoke-fast-method-call emf nil
(car args
) (cadr args
))
1369 (error 'simple-program-error
1370 :format-control
"invalid number of arguments: 2"
1371 :format-arguments nil
)))
1373 (apply (fast-method-call-function emf
)
1374 (fast-method-call-pv emf
)
1375 (fast-method-call-next-method-call emf
)
1378 (apply (method-call-function emf
)
1380 (method-call-call-method-args emf
)))
1383 (error 'simple-program-error
1384 :format-control
"invalid number of arguments: 0"
1385 :format-arguments nil
))
1387 (let* ((slots (get-slots (car args
)))
1388 (value (clos-slots-ref slots emf
)))
1389 (if (eq value
+slot-unbound
+)
1390 (slot-unbound-internal (car args
) emf
)
1393 (setf (clos-slots-ref (get-slots (cadr args
)) emf
)
1395 (t (error 'simple-program-error
1396 :format-control
"invalid number of arguments"
1397 :format-arguments nil
))))
1398 (fast-instance-boundp
1399 (if (or (null args
) (cdr args
))
1400 (error 'simple-program-error
1401 :format-control
"invalid number of arguments"
1402 :format-arguments nil
)
1403 (let ((slots (get-slots (car args
))))
1404 (not (eq (clos-slots-ref slots
(fast-instance-boundp-index emf
))
1410 (defmacro fast-call-next-method-body
((args next-method-call rest-arg
)
1413 `(if ,next-method-call
1414 ,(let ((call `(invoke-narrow-effective-method-function
1416 ,(not (null rest-arg
))
1417 :required-args
,args
1418 :rest-arg
,(when rest-arg
(list rest-arg
)))))
1422 `(&rest
,rest-arg
)))
1426 (call-no-next-method ',method-cell
1431 (defmacro bind-fast-lexical-method-functions
1432 ((args rest-arg next-method-call
(&key
1440 (let* ((next-method-p-def
1442 (declare (optimize (sb-c:insert-step-conditions
0)))
1443 (not (null ,next-method-call
)))))
1444 (rebindings (when (or setq-p call-next-method-p
)
1445 (mapcar (lambda (x) (list x x
)) parameters-setqd
))))
1446 (if (not (or call-next-method-p setq-p applyp
))
1447 `(flet ,next-method-p-def
1448 (declare (ignorable #'next-method-p
))
1450 `(flet (,@(when call-next-method-p
1451 `((call-next-method (&rest cnm-args
)
1452 (declare (dynamic-extent cnm-args
)
1453 (muffle-conditions code-deletion-note
)
1454 (optimize (sb-c:insert-step-conditions
0)))
1455 ,@(if (safe-code-p env
)
1456 `((%check-cnm-args cnm-args
(list ,@args
)
1459 (fast-call-next-method-body (,args
1464 ,@next-method-p-def
)
1465 (declare (ignorable #'next-method-p
))
1469 ;;; CMUCL comment (Gerd Moellmann):
1471 ;;; The standard says it's an error if CALL-NEXT-METHOD is called with
1472 ;;; arguments, and the set of methods applicable to those arguments is
1473 ;;; different from the set of methods applicable to the original
1474 ;;; method arguments. (According to Barry Margolin, this rule was
1475 ;;; probably added to ensure that before and around methods are always
1476 ;;; run before primary methods.)
1478 ;;; This could be optimized for the case that the generic function
1479 ;;; doesn't have hairy methods, does have standard method combination,
1480 ;;; is a standard generic function, there are no methods defined on it
1481 ;;; for COMPUTE-APPLICABLE-METHODS and probably a lot more of such
1482 ;;; preconditions. That looks hairy and is probably not worth it,
1483 ;;; because this check will never be fast.
1484 (defun %check-cnm-args
(cnm-args orig-args method-cell
)
1485 ;; 1. Check for no arguments.
1487 (let* ((gf (method-generic-function (car method-cell
)))
1488 (nreq (generic-function-nreq gf
)))
1489 (declare (fixnum nreq
))
1490 ;; 2. Requirement arguments pairwise: if all are EQL, the applicable
1491 ;; methods must be the same. This takes care of the relatively common
1492 ;; case of twiddling with &KEY arguments without being horribly
1494 (unless (do ((orig orig-args
(cdr orig
))
1495 (args cnm-args
(cdr args
))
1498 (unless (and orig args
(eql (car orig
) (car args
)))
1500 ;; 3. Only then do the full check.
1501 (let ((omethods (compute-applicable-methods gf orig-args
))
1502 (nmethods (compute-applicable-methods gf cnm-args
)))
1503 (unless (equal omethods nmethods
)
1504 (error "~@<The set of methods ~S applicable to argument~P ~
1505 ~{~S~^, ~} to call-next-method is different from ~
1506 the set of methods ~S applicable to the original ~
1507 method argument~P ~{~S~^, ~}.~@:>"
1508 nmethods
(length cnm-args
) cnm-args omethods
1509 (length orig-args
) orig-args
)))))))
1511 ;; FIXME: replacing this entire mess with DESTRUCTURING-BIND would correct
1512 ;; problems similar to those already solved by a correct implementation
1513 ;; of DESTRUCTURING-BIND, such as incorrect binding order:
1514 ;; e.g. (macroexpand-1 '(bind-args ((&optional (x nil xsp)) args) (form)))
1515 ;; -> (LET* ((.ARGS-TAIL. ARGS) (XSP (NOT (NULL .ARGS-TAIL.))) (X ...)))
1516 ;; It's mostly irrelevant unless a method uses CALL-NEXT-METHOD though.
1517 (defmacro bind-args
((lambda-list args
) &body body
)
1518 (let ((args-tail '.args-tail.
)
1521 (flet ((process-var (var)
1522 (if (memq var lambda-list-keywords
)
1525 (&optional
(setq state
'optional
))
1526 (&key
(setq state
'key
))
1528 (&rest
(setq state
'rest
))
1529 (&aux
(setq state
'aux
))
1532 "encountered the non-standard lambda list keyword ~S"
1536 (required `((,var
(pop ,args-tail
))))
1537 (optional (cond ((not (consp var
))
1538 `((,var
(when ,args-tail
1539 (pop ,args-tail
)))))
1541 `((,(car var
) (if ,args-tail
1545 `((,(caddr var
) (not (null ,args-tail
)))
1546 (,(car var
) (if ,args-tail
1549 (rest `((,var
,args-tail
)))
1550 (key (cond ((not (consp var
))
1552 (get-key-arg-tail ,(keywordicate var
)
1555 (multiple-value-bind (keyword variable
)
1556 (if (consp (car var
))
1559 (values (keywordicate (car var
))
1561 `((,key
(get-key-arg-tail ',keyword
1567 (multiple-value-bind (keyword variable
)
1568 (if (consp (car var
))
1571 (values (keywordicate (car var
))
1573 `((,key
(get-key-arg-tail ',keyword
1575 (,(caddr var
) (not (null,key
)))
1580 (let ((bindings (mapcan #'process-var lambda-list
)))
1581 `(let* ((,args-tail
,args
)
1584 ,@(when (eq state
'optional
)
1585 `((unless (null ,args-tail
)
1586 (error 'simple-program-error
1587 :format-control
"surplus arguments: ~S"
1588 :format-arguments
(list ,args-tail
)))))))
1589 (declare (ignorable ,args-tail .dummy0.
))
1592 (defun get-key-arg-tail (keyword list
)
1593 (loop for
(key . tail
) on list by
#'cddr
1595 ;; FIXME: Do we want to export this symbol? Or maybe use an
1596 ;; (ERROR 'SIMPLE-PROGRAM-ERROR) form?
1597 (sb-c::%odd-key-args-error
)
1598 when
(eq key keyword
)
1601 (defun walk-method-lambda (method-lambda required-parameters env slots
)
1602 (let (;; flag indicating that CALL-NEXT-METHOD should be in the
1603 ;; method definition
1604 (call-next-method-p nil
)
1605 ;; a list of all required parameters whose bindings might be
1606 ;; modified in the method body.
1607 (parameters-setqd nil
))
1608 (flet ((walk-function (form context env
)
1609 (unless (and (eq context
:eval
) (consp form
))
1610 (return-from walk-function form
))
1613 ;; hierarchy: nil -> :simple -> T.
1614 (unless (eq call-next-method-p t
)
1615 (setq call-next-method-p
(if (cdr form
) t
:simple
)))
1617 ((setq multiple-value-setq
)
1618 ;; The walker will split (SETQ A 1 B 2) to
1619 ;; separate (SETQ A 1) and (SETQ B 2) forms, so we
1620 ;; only need to handle the simple case of SETQ
1622 (let ((vars (if (eq (car form
) 'setq
)
1623 (list (second form
))
1626 ;; Note that we don't need to check for
1627 ;; %VARIABLE-REBINDING declarations like is
1628 ;; done in CAN-OPTIMIZE-ACCESS1, since the
1629 ;; bindings that will have that declation will
1631 (when (var-declaration '%class var env
)
1632 ;; If a parameter binding is shadowed by
1633 ;; another binding it won't have a %CLASS
1634 ;; declaration anymore, and this won't get
1636 (pushnew var parameters-setqd
:test
#'eq
))))
1639 (when (equal (cdr form
) '(call-next-method))
1640 (setq call-next-method-p t
))
1642 ((slot-value set-slot-value slot-boundp
)
1643 (if (constantp (third form
) env
)
1644 (let ((fun (ecase (car form
)
1645 (slot-value #'optimize-slot-value
)
1646 (set-slot-value #'optimize-set-slot-value
)
1647 (slot-boundp #'optimize-slot-boundp
))))
1648 (funcall fun form slots required-parameters env
))
1652 (let ((walked-lambda (walk-form method-lambda env
#'walk-function
)))
1653 ;;; FIXME: the walker's rewriting of the source code causes
1654 ;;; trouble when doing code coverage. The rewrites should be
1655 ;;; removed, and the same operations done using
1656 ;;; compiler-macros or tranforms.
1657 (values (if (sb-c:policy env
(= sb-c
:store-coverage-data
0))
1661 (not (null parameters-setqd
))
1662 parameters-setqd
)))))
1664 (defun generic-function-name-p (name)
1665 (and (legal-fun-name-p name
)
1667 (if (eq **boot-state
** 'complete
)
1668 (standard-generic-function-p (gdefinition name
))
1669 (funcallable-instance-p (gdefinition name
)))))
1671 (defun method-plist-value (method key
&optional default
)
1672 (let ((plist (if (consp method
)
1673 (getf (early-method-initargs method
) 'plist
)
1674 (object-plist method
))))
1675 (getf plist key default
)))
1677 (defun (setf method-plist-value
) (new-value method key
&optional default
)
1679 (setf (getf (getf (early-method-initargs method
) 'plist
) key default
)
1681 (setf (getf (object-plist method
) key default
) new-value
)))
1683 (defun load-defmethod (class name quals specls ll initargs source-location
)
1684 (let ((method-cell (getf initargs
'method-cell
)))
1685 (setq initargs
(copy-tree initargs
))
1687 (setf (getf initargs
'method-cell
) method-cell
))
1689 (setf (getf (getf initargs
'plist
) :name
)
1690 (make-method-spec name quals specls
))
1691 (load-defmethod-internal class name quals specls
1692 ll initargs source-location
)))
1694 (defun load-defmethod-internal
1695 (method-class gf-spec qualifiers specializers lambda-list
1696 initargs source-location
)
1697 (when (and (eq **boot-state
** 'complete
)
1699 (let* ((gf (fdefinition gf-spec
))
1700 (method (and (generic-function-p gf
)
1701 (generic-function-methods gf
)
1702 (find-method gf qualifiers specializers nil
))))
1704 (warn 'sb-kernel
:redefinition-with-defmethod
1706 :new-location source-location
1708 :qualifiers qualifiers
:specializers specializers
))))
1709 (let ((method (apply #'add-named-method
1710 gf-spec qualifiers specializers lambda-list
1711 :definition-source source-location
1713 (unless (or (eq method-class
'standard-method
)
1714 (eq (find-class method-class nil
) (class-of method
)))
1715 ;; FIXME: should be STYLE-WARNING?
1716 (format *error-output
*
1717 "~&At the time the method with qualifiers ~:S and~%~
1718 specializers ~:S on the generic function ~S~%~
1719 was compiled, the method-class for that generic function was~%~
1720 ~S. But, the method class is now ~S, this~%~
1721 may mean that this method was compiled improperly.~%"
1722 qualifiers specializers gf-spec
1723 method-class
(class-name (class-of method
))))
1726 (defun make-method-spec (gf qualifiers specializers
)
1727 (let ((name (generic-function-name gf
))
1728 (unparsed-specializers (unparse-specializers gf specializers
)))
1729 `(slow-method ,name
,@qualifiers
,unparsed-specializers
)))
1731 (defun initialize-method-function (initargs method
)
1732 (let* ((mf (getf initargs
:function
))
1733 (mff (and (typep mf
'%method-function
)
1734 (%method-function-fast-function mf
)))
1735 (plist (getf initargs
'plist
))
1736 (name (getf plist
:name
))
1737 (method-cell (getf initargs
'method-cell
)))
1739 (setf (car method-cell
) method
))
1742 (setq mf
(set-fun-name mf name
)))
1743 (when (and mff
(consp name
) (eq (car name
) 'slow-method
))
1744 (let ((fast-name `(fast-method ,@(cdr name
))))
1745 (set-fun-name mff fast-name
))))
1747 (let ((plist plist
))
1748 (let ((snl (getf plist
:slot-name-lists
)))
1750 (setf (method-plist-value method
:pv-table
)
1751 (intern-pv-table :slot-name-lists snl
))))))))
1753 (defun analyze-lambda-list (lambda-list)
1754 (multiple-value-bind (llks required optional rest keywords
)
1755 ;; We say "&MUMBLE is not allowed in a generic function lambda list"
1756 ;; whether this is called by DEFMETHOD or DEFGENERIC.
1757 ;; [It is used for either. Why else recognize and silently ignore &AUX?]
1758 (parse-lambda-list lambda-list
1759 :accept
(lambda-list-keyword-mask
1760 '(&optional
&rest
&key
&allow-other-keys
&aux
))
1762 :context
"a generic function lambda list")
1763 (declare (ignore rest
))
1764 (values llks
(length required
) (length optional
)
1765 (mapcar #'parse-key-arg-spec keywords
) keywords
)))
1767 ;; FIXME: this does more than return an FTYPE from a lambda list -
1768 ;; it unions the type with an existing ctype object. It needs a better name,
1769 ;; and to be reimplemented as "union and call sb-c::ftype-from-lambda-list".
1770 (defun ftype-declaration-from-lambda-list (lambda-list name
)
1771 (multiple-value-bind (llks nrequired noptional keywords keyword-parameters
)
1772 (analyze-lambda-list lambda-list
)
1773 (declare (ignore keyword-parameters
))
1774 (let* ((old (proclaimed-ftype name
)) ;FIXME:FDOCUMENTATION instead?
1775 (old-ftype (if (fun-type-p old
) old nil
))
1776 (old-restp (and old-ftype
(fun-type-rest old-ftype
)))
1777 (old-keys (and old-ftype
1778 (mapcar #'key-info-name
1781 (old-keysp (and old-ftype
(fun-type-keyp old-ftype
)))
1782 (old-allowp (and old-ftype
1783 (fun-type-allowp old-ftype
)))
1784 (keywords (union old-keys
(mapcar #'parse-key-arg-spec keywords
))))
1785 `(function ,(append (make-list nrequired
:initial-element t
)
1786 (when (plusp noptional
)
1787 (append '(&optional
)
1788 (make-list noptional
:initial-element t
)))
1789 (when (or (ll-kwds-restp llks
) old-restp
)
1791 (when (or (ll-kwds-keyp llks
) old-keysp
)
1793 (mapcar (lambda (key)
1796 (when (or (ll-kwds-allowp llks
) old-allowp
)
1797 '(&allow-other-keys
)))))
1800 ;;;; early generic function support
1802 (defvar *!early-generic-functions
* ())
1804 ;; CLHS doesn't specify &allow-other-keys here but I guess the supposition
1805 ;; is that they'll be checked by ENSURE-GENERIC-FUNCTION-USING-CLASS.
1806 ;; Except we don't do that either, so I think the blame, if any, lies there
1807 ;; for not catching errant keywords.
1808 (defun ensure-generic-function (fun-name &rest all-keys
)
1809 (let ((existing (and (fboundp fun-name
)
1810 (gdefinition fun-name
))))
1811 (cond ((and existing
1812 (eq **boot-state
** 'complete
)
1813 (null (generic-function-p existing
)))
1814 (generic-clobbers-function fun-name
)
1815 (fmakunbound fun-name
)
1816 (apply #'ensure-generic-function fun-name all-keys
))
1818 (apply #'ensure-generic-function-using-class
1819 existing fun-name all-keys
)))))
1821 (defun generic-clobbers-function (fun-name)
1822 (cerror "Replace the function binding"
1823 'simple-program-error
1824 :format-control
"~@<~/sb-impl:print-symbol-with-prefix/ ~
1825 already names an ordinary function or a ~
1827 :format-arguments
(list fun-name
)))
1829 (defvar *sgf-wrapper
*
1830 (!boot-make-wrapper
(!early-class-size
'standard-generic-function
)
1831 'standard-generic-function
))
1833 (defvar *sgf-slots-init
*
1834 (mapcar (lambda (canonical-slot)
1835 (if (memq (getf canonical-slot
:name
) '(arg-info source
))
1837 (let ((initfunction (getf canonical-slot
:initfunction
)))
1839 (funcall initfunction
)
1841 (!early-collect-inheritance
'standard-generic-function
)))
1843 (defconstant +sgf-method-class-index
+
1844 (!bootstrap-slot-index
'standard-generic-function
'method-class
))
1846 (defun early-gf-p (x)
1847 (and (fsc-instance-p x
)
1848 (eq (clos-slots-ref (get-slots x
) +sgf-method-class-index
+)
1851 (defconstant +sgf-methods-index
+
1852 (!bootstrap-slot-index
'standard-generic-function
'methods
))
1854 (defmacro early-gf-methods
(gf)
1855 `(clos-slots-ref (get-slots ,gf
) +sgf-methods-index
+))
1857 (defun safe-generic-function-methods (generic-function)
1858 (if (eq (class-of generic-function
) *the-class-standard-generic-function
*)
1859 (clos-slots-ref (get-slots generic-function
) +sgf-methods-index
+)
1860 (generic-function-methods generic-function
)))
1862 (defconstant +sgf-arg-info-index
+
1863 (!bootstrap-slot-index
'standard-generic-function
'arg-info
))
1865 (defmacro early-gf-arg-info
(gf)
1866 `(clos-slots-ref (get-slots ,gf
) +sgf-arg-info-index
+))
1868 (defconstant +sgf-dfun-state-index
+
1869 (!bootstrap-slot-index
'standard-generic-function
'dfun-state
))
1871 (defstruct (arg-info
1873 (:constructor make-arg-info
())
1875 (arg-info-lambda-list :no-lambda-list
)
1878 arg-info-number-optional
1880 arg-info-keys
;nil no &KEY or &REST allowed
1881 ;(k1 k2 ..) Each method must accept these &KEY arguments.
1882 ;T must have &KEY or &REST
1884 gf-info-simple-accessor-type
; nil, reader, writer, boundp
1885 (gf-precompute-dfun-and-emf-p nil
) ; set by set-arg-info
1887 gf-info-static-c-a-m-emf
1888 (gf-info-c-a-m-emf-std-p t
)
1891 #-sb-fluid
(declaim (sb-ext:freeze-type arg-info
))
1893 (defun arg-info-valid-p (arg-info)
1894 (not (null (arg-info-number-optional arg-info
))))
1896 (defun arg-info-applyp (arg-info)
1897 (or (plusp (arg-info-number-optional arg-info
))
1898 (arg-info-key/rest-p arg-info
)))
1900 (defun arg-info-number-required (arg-info)
1901 (length (arg-info-metatypes arg-info
)))
1903 (defun arg-info-nkeys (arg-info)
1904 (count-if (lambda (x) (neq x t
)) (arg-info-metatypes arg-info
)))
1906 (defun create-gf-lambda-list (lambda-list)
1907 ;;; Create a gf lambda list from a method lambda list
1908 (loop for x in lambda-list
1909 collect
(if (consp x
) (list (car x
)) x
)
1910 if
(eq x
'&key
) do
(loop-finish)))
1912 (defun ll-keyp-or-restp (bits)
1913 (logtest (lambda-list-keyword-mask '(&key
&rest
)) bits
))
1915 (defun remove-methods (gf)
1916 (loop for method in
(generic-function-methods gf
)
1917 do
(remove-method gf method
)))
1919 (defun set-arg-info (gf &key new-method
(lambda-list nil lambda-list-p
)
1920 argument-precedence-order
)
1921 (let* ((arg-info (if (eq **boot-state
** 'complete
)
1923 (early-gf-arg-info gf
)))
1924 (methods (if (eq **boot-state
** 'complete
)
1925 (generic-function-methods gf
)
1926 (early-gf-methods gf
)))
1927 (was-valid-p (integerp (arg-info-number-optional arg-info
)))
1928 (first-p (and new-method
(null (cdr methods
)))))
1929 (when (and (not lambda-list-p
) methods
)
1930 (setq lambda-list
(gf-lambda-list gf
)))
1931 (when (or lambda-list-p
1933 (eq (arg-info-lambda-list arg-info
) :no-lambda-list
)))
1934 (multiple-value-bind (llks nreq nopt keywords
)
1935 (analyze-lambda-list lambda-list
)
1936 (when (and methods
(not first-p
))
1937 (let ((gf-nreq (arg-info-number-required arg-info
))
1938 (gf-nopt (arg-info-number-optional arg-info
))
1939 (gf-key/rest-p
(arg-info-key/rest-p arg-info
)))
1940 (unless (and (= nreq gf-nreq
)
1942 (eq (ll-keyp-or-restp llks
) gf-key
/rest-p
))
1944 (error "New lambda-list ~S is incompatible with ~
1945 existing methods of ~S.~%~
1947 lambda-list gf
(arg-info-lambda-list arg-info
))
1949 :report
"Remove all methods."
1950 (remove-methods gf
))))))
1951 (setf (arg-info-lambda-list arg-info
)
1954 (create-gf-lambda-list lambda-list
)))
1955 (when (or lambda-list-p argument-precedence-order
1956 (null (arg-info-precedence arg-info
)))
1957 (setf (arg-info-precedence arg-info
)
1958 (compute-precedence lambda-list nreq argument-precedence-order
)))
1959 (setf (arg-info-metatypes arg-info
) (make-list nreq
))
1960 (setf (arg-info-number-optional arg-info
) nopt
)
1961 (setf (arg-info-key/rest-p arg-info
) (ll-keyp-or-restp llks
))
1962 (setf (arg-info-keys arg-info
)
1964 (if (ll-kwds-allowp llks
) t keywords
)
1965 (arg-info-key/rest-p arg-info
)))))
1967 (check-method-arg-info gf arg-info new-method
))
1968 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p
)
1971 (defun check-method-arg-info (gf arg-info method
)
1972 (multiple-value-bind (llks nreq nopt keywords
)
1973 (analyze-lambda-list (if (consp method
)
1974 (early-method-lambda-list method
)
1975 (method-lambda-list method
)))
1976 (flet ((lose (string &rest args
)
1977 (error 'simple-program-error
1978 :format-control
"~@<attempt to add the method~2I~_~S~I~_~
1979 to the generic function~2I~_~S;~I~_~
1981 :format-arguments
(list method gf string args
)))
1982 (comparison-description (x y
)
1983 (if (> x y
) "more" "fewer")))
1984 (let ((gf-nreq (arg-info-number-required arg-info
))
1985 (gf-nopt (arg-info-number-optional arg-info
))
1986 (gf-key/rest-p
(arg-info-key/rest-p arg-info
))
1987 (gf-keywords (arg-info-keys arg-info
)))
1988 (unless (= nreq gf-nreq
)
1990 "the method has ~A required arguments than the generic function."
1991 (comparison-description nreq gf-nreq
)))
1992 (unless (= nopt gf-nopt
)
1994 "the method has ~A optional arguments than the generic function."
1995 (comparison-description nopt gf-nopt
)))
1996 (unless (eq (ll-keyp-or-restp llks
) gf-key
/rest-p
)
1998 "the method and generic function differ in whether they accept~_~
1999 &REST or &KEY arguments."))
2000 (when (consp gf-keywords
)
2001 (unless (or (and (ll-kwds-restp llks
) (not (ll-kwds-keyp llks
)))
2002 (ll-kwds-allowp llks
)
2003 (every (lambda (k) (memq k keywords
)) gf-keywords
))
2004 (lose "the method does not accept each of the &KEY arguments~2I~_~
2008 (defconstant +sm-specializers-index
+
2009 (!bootstrap-slot-index
'standard-method
'specializers
))
2010 (defconstant +sm-%function-index
+
2011 (!bootstrap-slot-index
'standard-method
'%function
))
2012 (defconstant +sm-qualifiers-index
+
2013 (!bootstrap-slot-index
'standard-method
'qualifiers
))
2015 ;;; FIXME: we don't actually need this; we could test for the exact
2016 ;;; class and deal with it as appropriate. In fact we probably don't
2017 ;;; need it anyway because we only use this for METHOD-SPECIALIZERS on
2018 ;;; the standard reader method for METHOD-SPECIALIZERS. Probably.
2019 (dolist (s '(specializers %function
))
2020 (aver (= (symbol-value (intern (format nil
"+SM-~A-INDEX+" s
)))
2021 (!bootstrap-slot-index
'standard-reader-method s
)
2022 (!bootstrap-slot-index
'standard-writer-method s
)
2023 (!bootstrap-slot-index
'standard-boundp-method s
)
2024 (!bootstrap-slot-index
'global-reader-method s
)
2025 (!bootstrap-slot-index
'global-writer-method s
)
2026 (!bootstrap-slot-index
'global-boundp-method s
))))
2028 (defvar *standard-method-class-names
*
2029 '(standard-method standard-reader-method
2030 standard-writer-method standard-boundp-method
2031 global-reader-method global-writer-method
2032 global-boundp-method
))
2034 (declaim (list **standard-method-classes
**))
2035 (defglobal **standard-method-classes
** nil
)
2037 (defun safe-method-specializers (method)
2038 (if (member (class-of method
) **standard-method-classes
** :test
#'eq
)
2039 (clos-slots-ref (std-instance-slots method
) +sm-specializers-index
+)
2040 (method-specializers method
)))
2041 (defun safe-method-fast-function (method)
2042 (let ((mf (safe-method-function method
)))
2043 (and (typep mf
'%method-function
)
2044 (%method-function-fast-function mf
))))
2045 (defun safe-method-function (method)
2046 (if (member (class-of method
) **standard-method-classes
** :test
#'eq
)
2047 (clos-slots-ref (std-instance-slots method
) +sm-%function-index
+)
2048 (method-function method
)))
2049 (defun safe-method-qualifiers (method)
2050 (if (member (class-of method
) **standard-method-classes
** :test
#'eq
)
2051 (clos-slots-ref (std-instance-slots method
) +sm-qualifiers-index
+)
2052 (method-qualifiers method
)))
2054 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p
)
2055 (let* ((existing-p (and methods
(cdr methods
) new-method
))
2056 (nreq (length (arg-info-metatypes arg-info
)))
2057 (metatypes (if existing-p
2058 (arg-info-metatypes arg-info
)
2060 (type (if existing-p
2061 (gf-info-simple-accessor-type arg-info
)
2063 (when (arg-info-valid-p arg-info
)
2064 (dolist (method (if new-method
(list new-method
) methods
))
2065 (let* ((specializers (if (or (eq **boot-state
** 'complete
)
2066 (not (consp method
)))
2067 (safe-method-specializers method
)
2068 (early-method-specializers method t
)))
2069 (class (if (or (eq **boot-state
** 'complete
) (not (consp method
)))
2071 (early-method-class method
)))
2074 (or (not (eq **boot-state
** 'complete
))
2075 (eq (generic-function-method-combination gf
)
2076 *standard-method-combination
*)))
2077 (cond ((or (eq class
*the-class-standard-reader-method
*)
2078 (eq class
*the-class-global-reader-method
*))
2080 ((or (eq class
*the-class-standard-writer-method
*)
2081 (eq class
*the-class-global-writer-method
*))
2083 ((or (eq class
*the-class-standard-boundp-method
*)
2084 (eq class
*the-class-global-boundp-method
*))
2086 (setq metatypes
(mapcar #'raise-metatype metatypes specializers
))
2087 (setq type
(cond ((null type
) new-type
)
2088 ((eq type new-type
) type
)
2090 (setf (arg-info-metatypes arg-info
) metatypes
)
2091 (setf (gf-info-simple-accessor-type arg-info
) type
)))
2092 (when (or (not was-valid-p
) first-p
)
2093 (multiple-value-bind (c-a-m-emf std-p
)
2096 (compute-applicable-methods-emf gf
))
2097 (setf (gf-info-static-c-a-m-emf arg-info
) c-a-m-emf
)
2098 (setf (gf-info-c-a-m-emf-std-p arg-info
) std-p
)
2099 (unless (gf-info-c-a-m-emf-std-p arg-info
)
2100 (setf (gf-info-simple-accessor-type arg-info
) t
))))
2102 (let ((name (if (eq **boot-state
** 'complete
)
2103 (generic-function-name gf
)
2104 (!early-gf-name gf
))))
2105 (setf (gf-precompute-dfun-and-emf-p arg-info
)
2109 *internal-pcl-generalized-fun-name-symbols
*))
2111 (t (let* ((symbol (fun-name-block-name name
))
2112 (package (symbol-package symbol
)))
2113 (and (or (eq package
*pcl-package
*)
2114 (memq package
(package-use-list *pcl-package
*)))
2115 (not (eq package
*cl-package
*))
2116 ;; FIXME: this test will eventually be
2117 ;; superseded by the *internal-pcl...* test,
2118 ;; above. While we are in a process of
2119 ;; transition, however, it should probably
2121 (not (find #\Space
(symbol-name symbol
))))))))))
2122 (setf (gf-info-fast-mf-p arg-info
)
2123 (or (not (eq **boot-state
** 'complete
))
2124 (let* ((method-class (generic-function-method-class gf
))
2125 (methods (compute-applicable-methods
2126 #'make-method-lambda
2127 (list gf
(class-prototype method-class
)
2129 (and methods
(null (cdr methods
))
2130 (let ((specls (method-specializers (car methods
))))
2131 (and (classp (car specls
))
2132 (eq 'standard-generic-function
2133 (class-name (car specls
)))
2134 (classp (cadr specls
))
2135 (eq 'standard-method
2136 (class-name (cadr specls
)))))))))
2139 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
2141 ;;; The STATIC-SLOTS field of the funcallable instances used as early
2142 ;;; generic functions is used to store the early methods and early
2143 ;;; discriminator code for the early generic function. The static
2144 ;;; slots field of the fins contains a list whose:
2145 ;;; CAR - a list of the early methods on this early gf
2146 ;;; CADR - the early discriminator code for this method
2147 (defun ensure-generic-function-using-class (existing spec
&rest keys
2148 &key
(lambda-list nil
2150 argument-precedence-order
2154 (declare (ignore keys
))
2155 (cond ((and existing
(early-gf-p existing
))
2157 (set-arg-info existing
:lambda-list lambda-list
))
2159 ((assoc spec
*!generic-function-fixups
* :test
#'equal
)
2161 (make-early-gf spec lambda-list lambda-list-p existing
2162 argument-precedence-order definition-source
2164 (bug "The function ~S is not already defined." spec
)))
2166 (bug "~S should be on the list ~S."
2167 spec
'*!generic-function-fixups
*))
2169 (pushnew spec
*!early-generic-functions
* :test
#'equal
)
2170 (make-early-gf spec lambda-list lambda-list-p nil
2171 argument-precedence-order definition-source
2174 (defun make-early-gf (spec &optional lambda-list lambda-list-p
2175 function argument-precedence-order source-location
2177 (let ((fin (allocate-standard-funcallable-instance
2178 *sgf-wrapper
* *sgf-slots-init
*)))
2179 (set-funcallable-instance-function
2182 (if (eq spec
'print-object
)
2183 #'(lambda (instance stream
)
2184 (print-unreadable-object (instance stream
:identity t
)
2185 (format stream
"std-instance")))
2186 #'(lambda (&rest args
)
2187 (declare (ignore args
))
2188 (error "The function of the funcallable-instance ~S~
2189 has not been set." fin
)))))
2190 (setf (gdefinition spec
) fin
)
2191 (!bootstrap-set-slot
'standard-generic-function fin
'name spec
)
2192 (!bootstrap-set-slot
'standard-generic-function fin
2193 'source source-location
)
2194 (!bootstrap-set-slot
'standard-generic-function fin
2195 '%documentation documentation
)
2196 (let ((arg-info (make-arg-info)))
2197 (setf (early-gf-arg-info fin
) arg-info
)
2199 (setf (info :function
:type spec
)
2201 (ftype-declaration-from-lambda-list lambda-list spec
))
2202 (info :function
:where-from spec
) :defined-method
)
2203 (if argument-precedence-order
2205 :lambda-list lambda-list
2206 :argument-precedence-order argument-precedence-order
)
2207 (set-arg-info fin
:lambda-list lambda-list
))))
2210 (defun safe-gf-dfun-state (generic-function)
2211 (if (eq (class-of generic-function
) *the-class-standard-generic-function
*)
2212 (clos-slots-ref (fsc-instance-slots generic-function
) +sgf-dfun-state-index
+)
2213 (gf-dfun-state generic-function
)))
2214 (defun (setf safe-gf-dfun-state
) (new-value generic-function
)
2215 (if (eq (class-of generic-function
) *the-class-standard-generic-function
*)
2216 (setf (clos-slots-ref (fsc-instance-slots generic-function
)
2217 +sgf-dfun-state-index
+)
2219 (setf (gf-dfun-state generic-function
) new-value
)))
2221 (defun set-dfun (gf &optional dfun cache info
)
2222 (let ((new-state (if (and dfun
(or cache info
))
2223 (list* dfun cache info
)
2226 ((eq **boot-state
** 'complete
)
2227 ;; Check that we are under the lock.
2229 (aver (eq sb-thread
:*current-thread
* (sb-thread:mutex-owner
(gf-lock gf
))))
2230 (setf (safe-gf-dfun-state gf
) new-state
))
2232 (setf (clos-slots-ref (get-slots gf
) +sgf-dfun-state-index
+)
2236 (defun gf-dfun-cache (gf)
2237 (let ((state (if (eq **boot-state
** 'complete
)
2238 (safe-gf-dfun-state gf
)
2239 (clos-slots-ref (get-slots gf
) +sgf-dfun-state-index
+))))
2242 (cons (cadr state
)))))
2244 (defun gf-dfun-info (gf)
2245 (let ((state (if (eq **boot-state
** 'complete
)
2246 (safe-gf-dfun-state gf
)
2247 (clos-slots-ref (get-slots gf
) +sgf-dfun-state-index
+))))
2250 (cons (cddr state
)))))
2252 (defconstant +sgf-name-index
+
2253 (!bootstrap-slot-index
'standard-generic-function
'name
))
2255 (defun !early-gf-name
(gf)
2256 (clos-slots-ref (get-slots gf
) +sgf-name-index
+))
2258 (defun gf-lambda-list (gf)
2259 (let ((arg-info (if (eq **boot-state
** 'complete
)
2261 (early-gf-arg-info gf
))))
2262 (if (eq :no-lambda-list
(arg-info-lambda-list arg-info
))
2263 (let ((methods (if (eq **boot-state
** 'complete
)
2264 (generic-function-methods gf
)
2265 (early-gf-methods gf
))))
2268 (warn "no way to determine the lambda list for ~S" gf
)
2270 (let* ((method (car (last methods
)))
2271 (ll (if (consp method
)
2272 (early-method-lambda-list method
)
2273 (method-lambda-list method
))))
2274 (create-gf-lambda-list ll
))))
2275 (arg-info-lambda-list arg-info
))))
2277 (defmacro real-ensure-gf-internal
(gf-class all-keys env
)
2279 (cond ((symbolp ,gf-class
)
2280 (setq ,gf-class
(find-class ,gf-class t
,env
)))
2281 ((classp ,gf-class
))
2283 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
2284 class nor a symbol that names a class."
2286 (unless (class-finalized-p ,gf-class
)
2287 (if (class-has-a-forward-referenced-superclass-p ,gf-class
)
2288 ;; FIXME: reference MOP documentation -- this is an
2289 ;; additional requirement on our users
2290 (error "The generic function class ~S is not finalizeable" ,gf-class
)
2291 (finalize-inheritance ,gf-class
)))
2292 (remf ,all-keys
:generic-function-class
)
2293 (remf ,all-keys
:environment
)
2294 (let ((combin (getf ,all-keys
:method-combination
)))
2297 (setf (getf ,all-keys
:method-combination
)
2298 (find-method-combination (class-prototype ,gf-class
)
2301 ((or null method-combination
))))
2302 (let ((method-class (getf ,all-keys
:method-class
'.shes-not-there.
)))
2303 (unless (eq method-class
'.shes-not-there.
)
2304 (setf (getf ,all-keys
:method-class
)
2305 (cond ((classp method-class
)
2307 (t (find-class method-class t
,env
))))))))
2309 (defun note-gf-signature (fun-name lambda-list-p lambda-list
)
2310 (unless lambda-list-p
2311 ;; Use the existing lambda-list, if any. It is reasonable to do eg.
2313 ;; (if (fboundp name)
2314 ;; (ensure-generic-function name)
2315 ;; (ensure-generic-function name :lambda-list '(foo)))
2317 ;; in which case we end up here with no lambda-list in the first leg.
2318 (setf (values lambda-list lambda-list-p
)
2320 (values (generic-function-lambda-list (fdefinition fun-name
))
2322 ((or warning error
) ()
2323 (values nil nil
)))))
2327 (ftype-declaration-from-lambda-list lambda-list fun-name
)
2330 ;; FIXME: Ideally we would like to not clobber it, but because generic
2331 ;; functions assert their FTYPEs callers believing the FTYPE are left with
2332 ;; unsafe assumptions. Hence the clobbering. Be quiet when the new type
2333 ;; is a subtype of the old one, though -- even though the type is not
2334 ;; trusted anymore, the warning is still not quite as interesting.
2335 (when (and (eq :declared
(info :function
:where-from fun-name
))
2336 (not (csubtypep gf-type
(setf old-type
(proclaimed-ftype fun-name
)))))
2337 (style-warn "~@<Generic function ~S clobbers an earlier ~S proclamation ~S ~
2338 for the same name with ~S.~:@>"
2340 (type-specifier old-type
)
2341 (type-specifier gf-type
)))
2342 (setf (info :function
:type fun-name
) gf-type
2343 (info :function
:where-from fun-name
) :defined-method
)
2346 (defun real-ensure-gf-using-class--generic-function
2350 &key environment
(lambda-list nil lambda-list-p
)
2351 (generic-function-class 'standard-generic-function
)
2353 (real-ensure-gf-internal generic-function-class all-keys environment
)
2354 ;; KLUDGE: the above macro does SETQ on GENERIC-FUNCTION-CLASS,
2355 ;; which is what makes the next line work
2356 (unless (eq (class-of existing
) generic-function-class
)
2357 (change-class existing generic-function-class
))
2359 (apply #'reinitialize-instance existing all-keys
)
2360 (note-gf-signature fun-name lambda-list-p lambda-list
)))
2362 (defun real-ensure-gf-using-class--null
2366 &key environment
(lambda-list nil lambda-list-p
)
2367 (generic-function-class 'standard-generic-function
)
2369 (declare (ignore existing
))
2370 (real-ensure-gf-internal generic-function-class all-keys environment
)
2372 (setf (gdefinition fun-name
)
2373 (apply #'make-instance generic-function-class
2374 :name fun-name all-keys
))
2375 (note-gf-signature fun-name lambda-list-p lambda-list
)))
2377 (defun safe-gf-arg-info (generic-function)
2378 (if (eq (class-of generic-function
) *the-class-standard-generic-function
*)
2379 (clos-slots-ref (fsc-instance-slots generic-function
)
2380 +sgf-arg-info-index
+)
2381 (gf-arg-info generic-function
)))
2383 ;;; FIXME: this function took on a slightly greater role than it
2384 ;;; previously had around 2005-11-02, when CSR fixed the bug whereby
2385 ;;; having more than one subclass of standard-generic-function caused
2386 ;;; the whole system to die horribly through a metacircle in
2387 ;;; GF-ARG-INFO. The fix is to be slightly more disciplined about
2388 ;;; calling accessor methods -- we call GET-GENERIC-FUN-INFO when
2389 ;;; computing discriminating functions, so we need to be careful about
2390 ;;; having a base case for the recursion, and we provide that with the
2391 ;;; STANDARD-GENERIC-FUNCTION case below. However, we are not (yet)
2392 ;;; as disciplined as CLISP's CLOS/MOP, and it would be nice to get to
2393 ;;; that stage, where all potentially dangerous cases are enumerated
2394 ;;; and stopped. -- CSR, 2005-11-02.
2395 (defun get-generic-fun-info (gf)
2396 ;; values nreq applyp metatypes nkeys arg-info
2397 (multiple-value-bind (applyp metatypes arg-info
)
2398 (let* ((arg-info (if (early-gf-p gf
)
2399 (early-gf-arg-info gf
)
2400 (safe-gf-arg-info gf
)))
2401 (metatypes (arg-info-metatypes arg-info
)))
2402 (values (arg-info-applyp arg-info
)
2407 (declare (fixnum nreq nkeys
))
2408 (dolist (x metatypes
)
2412 (values nreq applyp metatypes
2416 (defun generic-function-nreq (gf)
2417 (let* ((arg-info (if (early-gf-p gf
)
2418 (early-gf-arg-info gf
)
2419 (safe-gf-arg-info gf
)))
2420 (metatypes (arg-info-metatypes arg-info
)))
2421 (declare (list metatypes
))
2422 (length metatypes
)))
2424 (defun !early-make-a-method
(class qualifiers arglist specializers initargs doc
2425 &key slot-name object-class method-class-function
2427 (aver (notany #'sb-pcl
::eql-specializer-p specializers
))
2429 ;; Figure out whether we got class objects or class names as the
2430 ;; specializers and set parsed and unparsed appropriately. If we
2431 ;; got class objects, then we can compute unparsed, but if we
2432 ;; got class names we don't try to compute parsed.
2434 (if (every #'classp specializers
)
2435 (values specializers
2437 (if (eq s t
) t
(class-name s
)))
2439 (values () specializers
)))
2443 (getf initargs
:function
)
2444 (let ((mf (getf initargs
:function
)))
2446 (and (typep mf
'%method-function
)
2447 (%method-function-fast-function mf
)))
2449 ;; the parsed specializers. This is used by
2450 ;; EARLY-METHOD-SPECIALIZERS to cache the parse.
2451 ;; Note that this only comes into play when there is
2452 ;; more than one early method on an early gf.
2455 ;; A list to which REAL-MAKE-A-METHOD can be applied
2456 ;; to make a real method corresponding to this early
2459 (list class qualifiers arglist unparsed
2462 (list :slot-name slot-name
:object-class object-class
2463 :method-class-function method-class-function
))
2464 (list :definition-source definition-source
)))))
2465 (initialize-method-function initargs result
)
2468 (defun real-make-a-method
2469 (class qualifiers lambda-list specializers initargs doc
2470 &rest args
&key slot-name object-class method-class-function
2472 (if method-class-function
2473 (let* ((object-class (if (classp object-class
) object-class
2474 (find-class object-class
)))
2475 (slots (class-direct-slots object-class
))
2476 (slot-definition (find slot-name slots
2477 :key
#'slot-definition-name
)))
2479 (aver slot-definition
)
2480 (let ((initargs (list* :qualifiers qualifiers
:lambda-list lambda-list
2481 :specializers specializers
:documentation doc
2482 :slot-definition slot-definition
2483 :slot-name slot-name initargs
)))
2484 (apply #'make-instance
2485 (apply method-class-function object-class slot-definition
2487 :definition-source definition-source
2489 (apply #'make-instance class
:qualifiers qualifiers
2490 :lambda-list lambda-list
:specializers specializers
2491 :documentation doc
(append args initargs
))))
2493 (defun early-method-function (early-method)
2494 (values (cadr early-method
) (caddr early-method
)))
2496 (defun early-method-class (early-method)
2497 (find-class (car (fifth early-method
))))
2499 (defun early-method-standard-accessor-p (early-method)
2500 (let ((class (first (fifth early-method
))))
2501 (or (eq class
'standard-reader-method
)
2502 (eq class
'standard-writer-method
)
2503 (eq class
'standard-boundp-method
))))
2505 (defun early-method-standard-accessor-slot-name (early-method)
2506 (eighth (fifth early-method
)))
2508 ;;; Fetch the specializers of an early method. This is basically just
2509 ;;; a simple accessor except that when the second argument is t, this
2510 ;;; converts the specializers from symbols into class objects. The
2511 ;;; class objects are cached in the early method, this makes
2512 ;;; bootstrapping faster because the class objects only have to be
2516 ;;; The second argument should only be passed as T by
2517 ;;; early-lookup-method. This is to implement the rule that only when
2518 ;;; there is more than one early method on a generic function is the
2519 ;;; conversion from class names to class objects done. This
2520 ;;; corresponds to the fact that we are only allowed to have one
2521 ;;; method on any generic function up until the time classes exist.
2522 (defun early-method-specializers (early-method &optional objectsp
)
2523 (if (and (listp early-method
)
2524 (eq (car early-method
) :early-method
))
2525 (cond ((eq objectsp t
)
2526 (or (fourth early-method
)
2527 (setf (fourth early-method
)
2528 (mapcar #'find-class
(cadddr (fifth early-method
))))))
2530 (fourth (fifth early-method
))))
2531 (error "~S is not an early-method." early-method
)))
2533 (defun early-method-qualifiers (early-method)
2534 (second (fifth early-method
)))
2536 (defun early-method-lambda-list (early-method)
2537 (third (fifth early-method
)))
2539 (defun early-method-initargs (early-method)
2540 (fifth (fifth early-method
)))
2542 (defun (setf early-method-initargs
) (new-value early-method
)
2543 (setf (fifth (fifth early-method
)) new-value
))
2545 (defun !early-add-named-method
(generic-function-name qualifiers
2546 specializers arglist
&rest initargs
2547 &key documentation definition-source
2549 (let* (;; we don't need to deal with the :generic-function-class
2550 ;; argument here because the default,
2551 ;; STANDARD-GENERIC-FUNCTION, is right for all early generic
2552 ;; functions. (See REAL-ADD-NAMED-METHOD)
2553 (gf (ensure-generic-function generic-function-name
))
2555 (dolist (m (early-gf-methods gf
))
2556 (when (and (equal (early-method-specializers m
) specializers
)
2557 (equal (early-method-qualifiers m
) qualifiers
))
2559 (setf (getf (getf initargs
'plist
) :name
)
2560 (make-method-spec gf qualifiers specializers
))
2561 (let ((new (make-a-method 'standard-method qualifiers arglist
2562 specializers initargs documentation
2563 :definition-source definition-source
)))
2564 (when existing
(remove-method gf existing
))
2565 (add-method gf new
))))
2567 ;;; This is the early version of ADD-METHOD. Later this will become a
2568 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2569 ;;; special knowledge about ADD-METHOD.
2570 (defun add-method (generic-function method
)
2571 (when (not (fsc-instance-p generic-function
))
2572 (error "Early ADD-METHOD didn't get a funcallable instance."))
2573 (when (not (and (listp method
) (eq (car method
) :early-method
)))
2574 (error "Early ADD-METHOD didn't get an early method."))
2575 (push method
(early-gf-methods generic-function
))
2576 (set-arg-info generic-function
:new-method method
)
2577 (unless (assoc (!early-gf-name generic-function
)
2578 *!generic-function-fixups
*
2580 (update-dfun generic-function
)))
2582 ;;; This is the early version of REMOVE-METHOD. See comments on
2583 ;;; the early version of ADD-METHOD.
2584 (defun remove-method (generic-function method
)
2585 (when (not (fsc-instance-p generic-function
))
2586 (error "An early remove-method didn't get a funcallable instance."))
2587 (when (not (and (listp method
) (eq (car method
) :early-method
)))
2588 (error "An early remove-method didn't get an early method."))
2589 (setf (early-gf-methods generic-function
)
2590 (remove method
(early-gf-methods generic-function
)))
2591 (set-arg-info generic-function
)
2592 (unless (assoc (!early-gf-name generic-function
)
2593 *!generic-function-fixups
*
2595 (update-dfun generic-function
)))
2597 ;;; This is the early version of GET-METHOD. See comments on the early
2598 ;;; version of ADD-METHOD.
2599 (defun get-method (generic-function qualifiers specializers
2600 &optional
(errorp t
))
2601 (if (early-gf-p generic-function
)
2602 (or (dolist (m (early-gf-methods generic-function
))
2603 (when (and (or (equal (early-method-specializers m nil
)
2605 (equal (early-method-specializers m t
)
2607 (equal (early-method-qualifiers m
) qualifiers
))
2610 (error "can't get early method")
2612 (real-get-method generic-function qualifiers specializers errorp
)))
2614 ;; minor KLUDGE: a separate code component for this function allows GCing
2615 ;; a few symbols and their associated code that would otherwise be retained:
2616 ;; *!EARLY-{GENERIC-}FUNCTIONS*, *!GENERIC-FUNCTION-FIXUPS*
2617 (defun early-gf-primary-slow-method-fn (fn)
2618 (lambda (args next-methods
)
2619 (declare (ignore next-methods
))
2622 (defun !fix-early-generic-functions
()
2623 (let ((accessors nil
))
2624 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2625 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2626 (dolist (early-gf-spec *!early-generic-functions
*)
2627 (when (every #'early-method-standard-accessor-p
2628 (early-gf-methods (gdefinition early-gf-spec
)))
2629 (push early-gf-spec accessors
)))
2630 (dolist (spec (nconc accessors
2631 '(accessor-method-slot-name
2632 generic-function-methods
2636 slot-definition-location
2637 slot-definition-name
2640 class-precedence-list
2641 slot-boundp-using-class
2642 (setf slot-value-using-class
)
2643 slot-value-using-class
)))
2645 (setq *!early-generic-functions
*
2647 (delete spec
*!early-generic-functions
* :test
#'equal
))))
2649 (dolist (early-gf-spec *!early-generic-functions
*)
2650 (/show early-gf-spec
)
2651 (let* ((gf (gdefinition early-gf-spec
))
2652 (methods (mapcar (lambda (early-method)
2653 (let ((args (copy-list (fifth
2656 (early-method-specializers
2658 (apply #'real-make-a-method args
)))
2659 (early-gf-methods gf
))))
2660 (setf (generic-function-method-class gf
) *the-class-standard-method
*)
2661 (setf (generic-function-method-combination gf
)
2662 *standard-method-combination
*)
2663 (set-methods gf methods
)))
2665 (dolist (fn *!early-functions
*)
2667 (setf (gdefinition (car fn
)) (fdefinition (caddr fn
))))
2669 (loop for
(fspec method-combination . methods
) in
*!generic-function-fixups
*
2670 for gf
= (gdefinition fspec
) do
2671 (labels ((translate-source-location (function)
2672 ;; This is lifted from sb-introspect, OAOO and all that.
2673 (let* ((function-object (sb-kernel::%fun-fun function
))
2674 (function-header (sb-kernel:fun-code-header function-object
))
2675 (debug-info (sb-kernel:%code-debug-info function-header
))
2676 (debug-source (sb-c::debug-info-source debug-info
))
2677 (debug-fun (debug-info-debug-function function debug-info
)))
2678 (sb-c::%make-definition-source-location
2679 (sb-c::debug-source-namestring debug-source
)
2680 (sb-c::compiled-debug-info-tlf-number debug-info
)
2681 (sb-c::compiled-debug-fun-form-number debug-fun
))))
2682 (debug-info-debug-function (function debug-info
)
2683 (let ((map (sb-c::compiled-debug-info-fun-map debug-info
))
2684 (name (sb-kernel:%simple-fun-name
(sb-kernel:%fun-fun function
))))
2689 (sb-c::compiled-debug-fun-p x
)
2690 (eq (sb-c::compiled-debug-fun-name x
) name
)))
2695 (lambda-list specializers qualifiers fun-name
) spec
2696 (let* ((specializers (mapcar #'find-class specializers
))
2697 (fun-name (or fun-name fspec
))
2698 (fun (fdefinition fun-name
))
2699 (initargs (list :function
2701 (early-gf-primary-slow-method-fn fun
)
2702 `(call ,fun-name
)))))
2703 (declare (type function fun
))
2706 qualifiers lambda-list specializers initargs nil
2707 :definition-source
(translate-source-location fun
))))))
2708 (setf (generic-function-method-class gf
)
2709 *the-class-standard-method
*
2710 (generic-function-method-combination gf
)
2711 (ecase method-combination
2712 (standard *standard-method-combination
*)
2713 (or *or-method-combination
*)))
2714 (set-methods gf
(mapcar #'make-method methods
)))))
2716 (/show
"leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2718 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2719 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2720 ;;; is really implemented.
2721 (defun parse-defmethod (cdr-of-form)
2722 (declare (list cdr-of-form
))
2723 (let ((qualifiers ())
2725 (loop (if (and (car cdr-of-form
) (atom (car cdr-of-form
)))
2726 (push (pop cdr-of-form
) qualifiers
)
2727 (return (setq qualifiers
(nreverse qualifiers
)))))
2728 (setq spec-ll
(pop cdr-of-form
))
2729 (values qualifiers spec-ll cdr-of-form
)))
2731 (defun parse-specializers (generic-function specializers
)
2732 (declare (list specializers
))
2733 (flet ((parse (spec)
2734 (parse-specializer-using-class generic-function spec
)))
2735 (mapcar #'parse specializers
)))
2737 (defun unparse-specializers (generic-function specializers
)
2738 (declare (list specializers
))
2739 (flet ((unparse (spec)
2740 (unparse-specializer-using-class generic-function spec
)))
2741 (mapcar #'unparse specializers
)))
2743 (macrolet ((def (n name
)
2744 `(defun ,name
(lambda-list)
2745 (nth-value ,n
(parse-specialized-lambda-list lambda-list
)))))
2746 ;; We don't need these, but according to the unit tests,
2747 ;; they're mandated by AMOP.
2748 (def 1 extract-lambda-list
)
2749 (def 2 extract-specializer-names
))
2751 (define-condition specialized-lambda-list-error
2752 (reference-condition simple-program-error
)
2754 (:default-initargs
:references
(list '(:ansi-cl
:section
(3 4 3)))))
2757 ;; - the bound variables, without defaults, supplied-p vars, or &AUX vars.
2758 ;; - the lambda list without specializers.
2759 ;; - just the specializers
2760 (defun parse-specialized-lambda-list (arglist)
2761 (multiple-value-bind (llks specialized optional rest key aux
)
2765 :accept
(lambda-list-keyword-mask
2766 '(&optional
&rest
&key
&allow-other-keys
&aux
))
2767 :silent t
; never signal &OPTIONAL + &KEY style-warning
2768 :condition-class
'specialized-lambda-list-error
)
2769 (let ((required (mapcar (lambda (x) (if (listp x
) (car x
) x
)) specialized
)))
2770 (values (append required
2771 (mapcar #'parse-optional-arg-spec optional
)
2773 ;; Preserve keyword-names when given as (:KEYWORD var)
2774 (mapcar (lambda (x) (if (typep x
'(cons cons
))
2776 (parse-key-arg-spec x
))) key
))
2777 (make-lambda-list llks nil required optional rest key aux
)
2778 (mapcar (lambda (x) (if (listp x
) (cadr x
) t
)) specialized
)))))
2780 (setq **boot-state
** 'early
)
2782 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2783 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2784 ;;; walker stuff was only used for implementing stuff like that; maybe
2785 ;;; it's not needed any more? Hunt down what it was used for and see.
2787 (defun extract-the (form)
2788 (cond ((and (consp form
) (eq (car form
) 'the
))
2789 (aver (proper-list-of-length-p form
3))
2794 (defmacro with-slots
(slots instance
&body body
)
2795 (let ((in (gensym)))
2796 `(let ((,in
,instance
))
2797 (declare (ignorable ,in
))
2798 ,@(let ((instance (extract-the instance
)))
2799 (and (symbolp instance
)
2800 `((declare (%variable-rebinding
,in
,instance
)))))
2802 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2804 (if (symbolp slot-entry
)
2808 (if (symbolp slot-entry
)
2810 (cadr slot-entry
))))
2812 (slot-value ,in
',slot-name
))))
2816 (defmacro with-accessors
(slots instance
&body body
)
2817 (let ((in (gensym)))
2818 `(let ((,in
,instance
))
2819 (declare (ignorable ,in
))
2820 ,@(let ((instance (extract-the instance
)))
2821 (and (symbolp instance
)
2822 `((declare (%variable-rebinding
,in
,instance
)))))
2824 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2825 (let ((var-name (car slot-entry
))
2826 (accessor-name (cadr slot-entry
)))
2827 `(,var-name
(,accessor-name
,in
))))