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 ;;; FIXME: As of sbcl-0.6.9.10, PCL still uses this nonstandard type
72 ;;; of declaration internally. It would be good to figure out how to
73 ;;; get rid of it, or failing that, (1) document why it's needed and
74 ;;; (2) use a private symbol with a forbidding name which suggests
75 ;;; it's not to be messed with by the user (e.g. SB-PCL:%CLASS)
76 ;;; instead of the too-inviting CLASS. (I tried just deleting the
77 ;;; declarations in MAKE-METHOD-LAMBDA-INTERNAL ca. sbcl-0.6.9.10, but
78 ;;; then things break.)
79 (declaim (declaration class))
81 ;;; FIXME: SB-KERNEL::PCL-CHECK-WRAPPER-VALIDITY-HOOK shouldn't be a
82 ;;; separate function. Instead, we should define a simple placeholder
83 ;;; version of SB-PCL:CHECK-WRAPPER-VALIDITY where
84 ;;; SB-KERNEL::PCL-CHECK-WRAPPER-VALIDITY is defined now, then just
85 ;;; let the later real PCL DEFUN of SB-PCL:CHECK-WRAPPER-VALIDITY
87 (setf (symbol-function 'sb-kernel::pcl-check-wrapper-validity-hook)
88 #'check-wrapper-validity)
90 (declaim (notinline make-a-method
92 ensure-generic-function-using-class
96 (defvar *!early-functions*
97 '((make-a-method early-make-a-method
99 (add-named-method early-add-named-method
100 real-add-named-method)
103 ;;; For each of the early functions, arrange to have it point to its
104 ;;; early definition. Do this in a way that makes sure that if we
105 ;;; redefine one of the early definitions the redefinition will take
106 ;;; effect. This makes development easier.
107 (dolist (fns *!early-functions*)
108 (let ((name (car fns))
109 (early-name (cadr fns)))
110 (setf (gdefinition name)
113 (apply (fdefinition early-name) args))
116 ;;; *!GENERIC-FUNCTION-FIXUPS* is used by !FIX-EARLY-GENERIC-FUNCTIONS
117 ;;; to convert the few functions in the bootstrap which are supposed
118 ;;; to be generic functions but can't be early on.
119 (defvar *!generic-function-fixups*
121 ((generic-function method) ;lambda-list
122 (standard-generic-function method) ;specializers
123 real-add-method)) ;method-function
125 ((generic-function method)
126 (standard-generic-function method)
129 ((generic-function qualifiers specializers &optional (errorp t))
130 (standard-generic-function t t)
132 (ensure-generic-function-using-class
133 ((generic-function fun-name
134 &key generic-function-class environment
137 real-ensure-gf-using-class--generic-function)
138 ((generic-function fun-name
139 &key generic-function-class environment
142 real-ensure-gf-using-class--null))
144 ((proto-generic-function proto-method lambda-expression environment)
145 (standard-generic-function standard-method t t)
146 real-make-method-lambda))
147 (make-method-initargs-form
148 ((proto-generic-function proto-method
150 lambda-list environment)
151 (standard-generic-function standard-method t t t)
152 real-make-method-initargs-form))
153 (compute-effective-method
154 ((generic-function combin applicable-methods)
155 (generic-function standard-method-combination t)
156 standard-compute-effective-method))))
158 (defmacro defgeneric (fun-name lambda-list &body options)
159 (declare (type list lambda-list))
160 (unless (legal-fun-name-p fun-name)
161 (error 'simple-program-error
162 :format-control "illegal generic function name ~S"
163 :format-arguments (list fun-name)))
166 (flet ((duplicate-option (name)
167 (error 'simple-program-error
168 :format-control "The option ~S appears more than once."
169 :format-arguments (list name)))
170 (expand-method-definition (qab) ; QAB = qualifiers, arglist, body
171 (let* ((arglist-pos (position-if #'listp qab))
172 (arglist (elt qab arglist-pos))
173 (qualifiers (subseq qab 0 arglist-pos))
174 (body (nthcdr (1+ arglist-pos) qab)))
175 `(push (defmethod ,fun-name ,@qualifiers ,arglist ,@body)
176 (generic-function-initial-methods #',fun-name)))))
177 (macrolet ((initarg (key) `(getf initargs ,key)))
178 (dolist (option options)
179 (let ((car-option (car option)))
182 (push (cdr option) (initarg :declarations)))
183 ((:argument-precedence-order :method-combination)
184 (if (initarg car-option)
185 (duplicate-option car-option)
186 (setf (initarg car-option)
188 ((:documentation :generic-function-class :method-class)
189 (unless (proper-list-of-length-p option 2)
190 (error "bad list length for ~S" option))
191 (if (initarg car-option)
192 (duplicate-option car-option)
193 (setf (initarg car-option) `',(cadr option))))
195 (push (cdr option) methods))
197 ;; ANSI requires that unsupported things must get a
199 (error 'simple-program-error
200 :format-control "unsupported option ~S"
201 :format-arguments (list option))))))
203 (when (initarg :declarations)
204 (setf (initarg :declarations)
205 `',(initarg :declarations))))
207 (eval-when (:compile-toplevel :load-toplevel :execute)
208 (compile-or-load-defgeneric ',fun-name))
209 (load-defgeneric ',fun-name ',lambda-list ,@initargs)
210 ,@(mapcar #'expand-method-definition methods)
213 (defun compile-or-load-defgeneric (fun-name)
214 (sb-kernel:proclaim-as-fun-name fun-name)
215 (sb-kernel:note-name-defined fun-name :function)
216 (unless (eq (info :function :where-from fun-name) :declared)
217 (setf (info :function :where-from fun-name) :defined)
218 (setf (info :function :type fun-name)
219 (sb-kernel:specifier-type 'function))))
221 (defun load-defgeneric (fun-name lambda-list &rest initargs)
222 (when (fboundp fun-name)
223 (sb-kernel::style-warn "redefining ~S in DEFGENERIC" fun-name)
224 (let ((fun (fdefinition fun-name)))
225 (when (generic-function-p fun)
226 (loop for method in (generic-function-initial-methods fun)
227 do (remove-method fun method))
228 (setf (generic-function-initial-methods fun) '()))))
229 (apply #'ensure-generic-function
231 :lambda-list lambda-list
232 :definition-source `((defgeneric ,fun-name) ,*load-truename*)
235 (defmacro defmethod (&rest args &environment env)
236 (multiple-value-bind (name qualifiers lambda-list body)
237 (parse-defmethod args)
238 (multiple-value-bind (proto-gf proto-method)
239 (prototypes-for-make-method-lambda name)
240 (expand-defmethod name
248 (defun prototypes-for-make-method-lambda (name)
249 (if (not (eq *boot-state* 'complete))
251 (let ((gf? (and (gboundp name)
252 (gdefinition name))))
254 (not (generic-function-p gf?)))
255 (values (class-prototype (find-class 'standard-generic-function))
256 (class-prototype (find-class 'standard-method)))
258 (class-prototype (or (generic-function-method-class gf?)
259 (find-class 'standard-method))))))))
261 ;;; Take a name which is either a generic function name or a list specifying
262 ;;; a SETF generic function (like: (SETF <generic-function-name>)). Return
263 ;;; the prototype instance of the method-class for that generic function.
265 ;;; If there is no generic function by that name, this returns the
266 ;;; default value, the prototype instance of the class
267 ;;; STANDARD-METHOD. This default value is also returned if the spec
268 ;;; names an ordinary function or even a macro. In effect, this leaves
269 ;;; the signalling of the appropriate error until load time.
271 ;;; Note: During bootstrapping, this function is allowed to return NIL.
272 (defun method-prototype-for-gf (name)
273 (let ((gf? (and (gboundp name)
274 (gdefinition name))))
275 (cond ((neq *boot-state* 'complete) nil)
277 (not (generic-function-p gf?))) ; Someone else MIGHT
278 ; error at load time.
279 (class-prototype (find-class 'standard-method)))
281 (class-prototype (or (generic-function-method-class gf?)
282 (find-class 'standard-method)))))))
284 (defvar *optimize-asv-funcall-p* nil)
285 (defvar *asv-readers*)
286 (defvar *asv-writers*)
287 (defvar *asv-boundps*)
289 (defun expand-defmethod (name
296 (let ((*make-instance-function-keys* nil)
297 (*optimize-asv-funcall-p* t)
298 (*asv-readers* nil) (*asv-writers* nil) (*asv-boundps* nil))
299 (declare (special *make-instance-function-keys*))
300 (multiple-value-bind (method-lambda unspecialized-lambda-list specializers)
301 (add-method-declarations name qualifiers lambda-list body env)
302 (multiple-value-bind (method-function-lambda initargs)
303 (make-method-lambda proto-gf proto-method method-lambda env)
304 (let ((initargs-form (make-method-initargs-form proto-gf
306 method-function-lambda
310 ;; Note: We could DECLAIM the ftype of the generic
311 ;; function here, since ANSI specifies that we create it
312 ;; if it does not exist. However, I chose not to, because
313 ;; I think it's more useful to support a style of
314 ;; programming where every generic function has an
315 ;; explicit DEFGENERIC and any typos in DEFMETHODs are
316 ;; warned about. Otherwise
317 ;; (DEFGENERIC FOO-BAR-BLETCH ((X T)))
318 ;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
319 ;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
320 ;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
321 ;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
322 ;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
323 ;; compiles without raising an error and runs without
324 ;; raising an error (since SIMPLE-VECTOR cases fall
325 ;; through to VECTOR) but still doesn't do what was
326 ;; intended. I hate that kind of bug (code which silently
327 ;; gives the wrong answer), so we don't do a DECLAIM
328 ;; here. -- WHN 20000229
329 ,@(when *make-instance-function-keys*
330 `((get-make-instance-functions
331 ',*make-instance-function-keys*)))
332 ,@(when (or *asv-readers* *asv-writers* *asv-boundps*)
333 `((initialize-internal-slot-gfs*
334 ',*asv-readers* ',*asv-writers* ',*asv-boundps*)))
335 ,(make-defmethod-form name qualifiers specializers
336 unspecialized-lambda-list
338 (class-name (class-of proto-method))
341 (getf (getf initargs :plist)
342 :pv-table-symbol))))))))
344 (defun interned-symbol-p (x)
345 (and (symbolp x) (symbol-package x)))
347 (defun make-defmethod-form (name qualifiers specializers
348 unspecialized-lambda-list method-class-name
349 initargs-form &optional pv-table-symbol)
352 (if (and (interned-symbol-p (fun-name-block-name name))
353 (every #'interned-symbol-p qualifiers)
356 (and (eq (car s) 'eql)
358 (let ((sv (eval (cadr s))))
359 (or (interned-symbol-p sv)
362 (standard-char-p sv)))))
363 (interned-symbol-p s)))
365 (consp initargs-form)
366 (eq (car initargs-form) 'list*)
367 (memq (cadr initargs-form) '(:function :fast-function))
368 (consp (setq fn (caddr initargs-form)))
369 (eq (car fn) 'function)
370 (consp (setq fn-lambda (cadr fn)))
371 (eq (car fn-lambda) 'lambda))
372 (let* ((specls (mapcar (lambda (specl)
374 `(,(car specl) ,(eval (cadr specl)))
377 (mname `(,(if (eq (cadr initargs-form) :function)
378 'method 'fast-method)
379 ,name ,@qualifiers ,specls))
380 (mname-sym (intern (let ((*print-pretty* nil)
381 ;; (We bind *PACKAGE* to
382 ;; KEYWORD here as a way to
383 ;; force symbols to be printed
384 ;; with explicit package
386 (*package* *keyword-package*))
387 (format nil "~S" mname)))))
389 (defun ,mname-sym ,(cadr fn-lambda)
391 ,(make-defmethod-form-internal
392 name qualifiers `',specls
393 unspecialized-lambda-list method-class-name
394 `(list* ,(cadr initargs-form)
396 ,@(cdddr initargs-form))
398 (make-defmethod-form-internal
400 `(list ,@(mapcar (lambda (specializer)
401 (if (consp specializer)
402 ``(,',(car specializer)
403 ,,(cadr specializer))
406 unspecialized-lambda-list
411 (defun make-defmethod-form-internal
412 (name qualifiers specializers-form unspecialized-lambda-list
413 method-class-name initargs-form &optional pv-table-symbol)
419 ',unspecialized-lambda-list
421 ;; Paper over a bug in KCL by passing the cache-symbol here in
422 ;; addition to in the list. FIXME: We should no longer need to do
423 ;; this, since the CLOS code is now SBCL-specific, and doesn't
424 ;; need to be ported to every buggy compiler in existence.
427 (defmacro make-method-function (method-lambda &environment env)
428 (make-method-function-internal method-lambda env))
430 (defun make-method-function-internal (method-lambda &optional env)
431 (multiple-value-bind (proto-gf proto-method)
432 (prototypes-for-make-method-lambda nil)
433 (multiple-value-bind (method-function-lambda initargs)
434 (make-method-lambda proto-gf proto-method method-lambda env)
435 (make-method-initargs-form proto-gf
437 method-function-lambda
441 (defun add-method-declarations (name qualifiers lambda-list body env)
442 (multiple-value-bind (parameters unspecialized-lambda-list specializers)
443 (parse-specialized-lambda-list lambda-list)
444 (declare (ignore parameters))
445 (multiple-value-bind (documentation declarations real-body)
446 (extract-declarations body env)
447 (values `(lambda ,unspecialized-lambda-list
448 ,@(when documentation `(,documentation))
449 ;; (Old PCL code used a somewhat different style of
450 ;; list for %METHOD-NAME values. Our names use
451 ;; ,@QUALIFIERS instead of ,QUALIFIERS so that the
452 ;; method names look more like what you see in a
455 ;; FIXME: As of sbcl-0.7.0.6, code elsewhere, at
456 ;; least the code to set up named BLOCKs around the
457 ;; bodies of methods, depends on the function's base
458 ;; name being the first element of the %METHOD-NAME
459 ;; list. It would be good to remove this dependency,
460 ;; perhaps by building the BLOCK here, or by using
461 ;; another declaration (e.g. %BLOCK-NAME), so that
462 ;; our method debug names are free to have any format,
463 ;; e.g. (:METHOD PRINT-OBJECT :AROUND (CLOWN T)).
464 (declare (%method-name (,name
467 (declare (%method-lambda-list ,@lambda-list))
470 unspecialized-lambda-list specializers))))
472 (defun real-make-method-initargs-form (proto-gf proto-method
473 method-lambda initargs env)
474 (declare (ignore proto-gf proto-method))
475 (unless (and (consp method-lambda)
476 (eq (car method-lambda) 'lambda))
477 (error "The METHOD-LAMBDA argument to MAKE-METHOD-FUNCTION, ~S, ~
478 is not a lambda form."
480 (make-method-initargs-form-internal method-lambda initargs env))
482 (unless (fboundp 'make-method-initargs-form)
483 (setf (gdefinition 'make-method-initargs-form)
484 (symbol-function 'real-make-method-initargs-form)))
486 (defun real-make-method-lambda (proto-gf proto-method method-lambda env)
487 (declare (ignore proto-gf proto-method))
488 (make-method-lambda-internal method-lambda env))
490 ;;; a helper function for creating Python-friendly type declarations
491 ;;; in DEFMETHOD forms
492 (defun parameter-specializer-declaration-in-defmethod (parameter specializer)
493 (cond ((and (consp specializer)
494 (eq (car specializer) 'eql))
495 ;; KLUDGE: ANSI, in its wisdom, says that
496 ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at
497 ;; DEFMETHOD expansion time. Thus, although one might think
499 ;; (DEFMETHOD FOO ((X PACKAGE)
502 ;; the PACKAGE and (EQL 12) forms are both parallel type
503 ;; names, they're not, as is made clear when you do
504 ;; (DEFMETHOD FOO ((X PACKAGE)
507 ;; where Y needs to be a symbol named "BAR", not some cons
508 ;; made by (CONS 'QUOTE 'BAR). I.e. when the
509 ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument
510 ;; to be of type (EQL X). It'd be easy to transform one to
511 ;; the other, but it'd be somewhat messier to do so while
512 ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd
513 ;; once. (The new code wouldn't be messy, but it'd require a
514 ;; big transformation of the old code.) So instead we punt.
518 ;; KLUDGE: For some low-level implementation
519 ;; classes, perhaps because of some problems related
520 ;; to the incomplete integration of PCL into SBCL's
521 ;; type system, some specializer classes can't be
522 ;; declared as argument types. E.g.
523 ;; (DEFMETHOD FOO ((X SLOT-OBJECT))
524 ;; (DECLARE (TYPE SLOT-OBJECT X))
527 ;; (DEFSTRUCT BAR A B)
529 ;; perhaps because of the way that STRUCTURE-OBJECT
530 ;; inherits both from SLOT-OBJECT and from
531 ;; SB-KERNEL:INSTANCE. In an effort to sweep such
532 ;; problems under the rug, we exclude these problem
533 ;; cases by blacklisting them here. -- WHN 2001-01-19
536 ((not (eq *boot-state* 'complete))
537 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
538 ;; types which don't match their specializers. (Specifically,
539 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
540 ;; second argument.) Hopefully it only does this kind of
541 ;; weirdness when bootstrapping.. -- WHN 20000610
544 ;; Otherwise, we can make Python very happy.
545 `(type ,specializer ,parameter))))
547 (defun make-method-lambda-internal (method-lambda &optional env)
548 (unless (and (consp method-lambda) (eq (car method-lambda) 'lambda))
549 (error "The METHOD-LAMBDA argument to MAKE-METHOD-LAMBDA, ~S, ~
550 is not a lambda form."
552 (multiple-value-bind (documentation declarations real-body)
553 (extract-declarations (cddr method-lambda) env)
554 (let* ((name-decl (get-declaration '%method-name declarations))
555 (sll-decl (get-declaration '%method-lambda-list declarations))
556 (method-name (when (consp name-decl) (car name-decl)))
557 (generic-function-name (when method-name (car method-name)))
558 (specialized-lambda-list (or sll-decl (cadr method-lambda))))
559 (multiple-value-bind (parameters lambda-list specializers)
560 (parse-specialized-lambda-list specialized-lambda-list)
561 (let* ((required-parameters
562 (mapcar (lambda (r s) (declare (ignore s)) r)
565 (slots (mapcar #'list required-parameters))
569 ;; These declarations seem to be used by PCL to pass
570 ;; information to itself; when I tried to delete 'em
571 ;; ca. 0.6.10 it didn't work. I'm not sure how
572 ;; they work, but note the (VAR-DECLARATION '%CLASS ..)
573 ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30
575 (mapcar (lambda (a s) (and (symbolp s)
580 ;; These TYPE declarations weren't in the original
581 ;; PCL code, but the Python compiler likes them a
582 ;; lot. (We're telling the compiler about our
583 ;; knowledge of specialized argument types so that
584 ;; it can avoid run-time type dispatch overhead,
585 ;; which can be a huge win for Python.)
587 ;; FIXME: Perhaps these belong in
588 ;; ADD-METHOD-DECLARATIONS instead of here?
589 ,@(mapcar #'parameter-specializer-declaration-in-defmethod
593 ;; Remove the documentation string and insert the
594 ;; appropriate class declarations. The documentation
595 ;; string is removed to make it easy for us to insert
596 ;; new declarations later, they will just go after the
597 ;; CADR of the method lambda. The class declarations
598 ;; are inserted to communicate the class of the method's
599 ;; arguments to the code walk.
600 `(lambda ,lambda-list
601 ;; The default ignorability of method parameters
602 ;; doesn't seem to be specified by ANSI. PCL had
603 ;; them basically ignorable but was a little
604 ;; inconsistent. E.g. even though the two
605 ;; method definitions
606 ;; (DEFMETHOD FOO ((X T) (Y T)) "Z")
607 ;; (DEFMETHOD FOO ((X T) Y) "Z")
608 ;; are otherwise equivalent, PCL treated Y as
609 ;; ignorable in the first definition but not in the
610 ;; second definition. We make all required
611 ;; parameters ignorable as a way of systematizing
612 ;; the old PCL behavior. -- WHN 2000-11-24
613 (declare (ignorable ,@required-parameters))
616 (block ,(fun-name-block-name generic-function-name)
618 (constant-value-p (and (null (cdr real-body))
619 (constantp (car real-body))))
620 (constant-value (and constant-value-p
621 (eval (car real-body))))
622 (plist (and constant-value-p
623 (or (typep constant-value
624 '(or number character))
625 (and (symbolp constant-value)
626 (symbol-package constant-value)))
627 (list :constant-value constant-value)))
628 (applyp (dolist (p lambda-list nil)
629 (cond ((memq p '(&optional &rest &key))
634 (walked-lambda call-next-method-p closurep next-method-p-p)
635 (walk-method-lambda method-lambda
641 (ignore walked-declarations walked-lambda-body)
642 (extract-declarations (cddr walked-lambda))
643 (declare (ignore ignore))
644 (when (or next-method-p-p call-next-method-p)
645 (setq plist (list* :needs-next-methods-p t plist)))
646 (when (some #'cdr slots)
647 (multiple-value-bind (slot-name-lists call-list)
648 (slot-name-lists-from-slots slots calls)
649 (let ((pv-table-symbol (make-symbol "pv-table")))
651 `(,@(when slot-name-lists
652 `(:slot-name-lists ,slot-name-lists))
654 `(:call-list ,call-list))
655 :pv-table-symbol ,pv-table-symbol
657 (setq walked-lambda-body
658 `((pv-binding (,required-parameters
661 ,@walked-lambda-body))))))
662 (when (and (memq '&key lambda-list)
663 (not (memq '&allow-other-keys lambda-list)))
664 (let ((aux (memq '&aux lambda-list)))
665 (setq lambda-list (nconc (ldiff lambda-list aux)
666 (list '&allow-other-keys)
668 (values `(lambda (.method-args. .next-methods.)
669 (simple-lexical-method-functions
670 (,lambda-list .method-args. .next-methods.
673 :next-method-p-p ,next-method-p-p
676 ,@walked-declarations
677 ,@walked-lambda-body))
680 ,@(when documentation
681 `(:documentation ,documentation)))))))))))
683 (unless (fboundp 'make-method-lambda)
684 (setf (gdefinition 'make-method-lambda)
685 (symbol-function 'real-make-method-lambda)))
687 (defmacro simple-lexical-method-functions ((lambda-list
693 ,method-args ,next-methods
694 (bind-simple-lexical-method-macros (,method-args ,next-methods)
695 (bind-lexical-method-functions (,@lmf-options)
696 (bind-args (,lambda-list ,method-args)
699 (defmacro fast-lexical-method-functions ((lambda-list
705 `(bind-fast-lexical-method-macros (,args ,rest-arg ,next-method-call)
706 (bind-lexical-method-functions (,@lmf-options)
707 (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg)
710 (defmacro bind-simple-lexical-method-macros ((method-args next-methods)
712 `(macrolet ((call-next-method-bind (&body body)
713 `(let ((.next-method. (car ,',next-methods))
714 (,',next-methods (cdr ,',next-methods)))
715 .next-method. ,',next-methods
717 (call-next-method-body (cnm-args)
719 (funcall (if (std-instance-p .next-method.)
720 (method-function .next-method.)
721 .next-method.) ; for early methods
722 (or ,cnm-args ,',method-args)
724 (error "no next method")))
725 (next-method-p-body ()
726 `(not (null .next-method.))))
729 (defstruct (method-call (:copier nil))
730 (function #'identity :type function)
733 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
735 (defmacro invoke-method-call1 (function args cm-args)
736 `(let ((.function. ,function)
738 (.cm-args. ,cm-args))
739 (if (and .cm-args. (null (cdr .cm-args.)))
740 (funcall .function. .args. (car .cm-args.))
741 (apply .function. .args. .cm-args.))))
743 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
744 `(invoke-method-call1 (method-call-function ,method-call)
746 `(list* ,@required-args+rest-arg)
747 `(list ,@required-args+rest-arg))
748 (method-call-call-method-args ,method-call)))
750 (defstruct (fast-method-call (:copier nil))
751 (function #'identity :type function)
756 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
758 (defmacro fmc-funcall (fn pv-cell next-method-call &rest args)
759 `(funcall ,fn ,pv-cell ,next-method-call ,@args))
761 (defmacro invoke-fast-method-call (method-call &rest required-args+rest-arg)
762 `(fmc-funcall (fast-method-call-function ,method-call)
763 (fast-method-call-pv-cell ,method-call)
764 (fast-method-call-next-method-call ,method-call)
765 ,@required-args+rest-arg))
767 (defstruct (fast-instance-boundp (:copier nil))
768 (index 0 :type fixnum))
770 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
772 (eval-when (:compile-toplevel :load-toplevel :execute)
774 (defvar *allow-emf-call-tracing-p* nil)
775 (defvar *enable-emf-call-tracing-p* #-testing nil #+testing t)
779 ;;;; effective method functions
781 (defvar *emf-call-trace-size* 200)
782 (defvar *emf-call-trace* nil)
783 (defvar *emf-call-trace-index* 0)
785 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
786 ;;; without explanation. It appears to be intended for debugging, so
787 ;;; it might be useful someday, so I haven't deleted it.
788 ;;; But it isn't documented and isn't used for anything now, so
789 ;;; I've conditionalized it out of the base system. -- WHN 19991213
791 (defun show-emf-call-trace ()
792 (when *emf-call-trace*
793 (let ((j *emf-call-trace-index*)
794 (*enable-emf-call-tracing-p* nil))
795 (format t "~&(The oldest entries are printed first)~%")
796 (dotimes-fixnum (i *emf-call-trace-size*)
797 (let ((ct (aref *emf-call-trace* j)))
798 (when ct (print ct)))
800 (when (= j *emf-call-trace-size*)
803 (defun trace-emf-call-internal (emf format args)
804 (unless *emf-call-trace*
805 (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
806 (setf (aref *emf-call-trace* *emf-call-trace-index*)
807 (list* emf format args))
808 (incf *emf-call-trace-index*)
809 (when (= *emf-call-trace-index* *emf-call-trace-size*)
810 (setq *emf-call-trace-index* 0)))
812 (defmacro trace-emf-call (emf format args)
813 (when *allow-emf-call-tracing-p*
814 `(when *enable-emf-call-tracing-p*
815 (trace-emf-call-internal ,emf ,format ,args))))
817 (defmacro invoke-effective-method-function-fast
818 (emf restp &rest required-args+rest-arg)
820 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
821 (invoke-fast-method-call ,emf ,@required-args+rest-arg)))
823 (defmacro invoke-effective-method-function (emf restp
824 &rest required-args+rest-arg)
825 (unless (constantp restp)
826 (error "The RESTP argument is not constant."))
827 (setq restp (eval restp))
830 ;; In sbcl-0.6.11.43, the compiler would issue bogus warnings
831 ;; about type mismatches in unreachable code when we
832 ;; macroexpanded the GET-SLOTS-OR-NIL expressions here and
833 ;; byte-compiled the code. GET-SLOTS-OR-NIL is now an inline
834 ;; function instead of a macro, which seems sufficient to solve
835 ;; the problem all by itself (probably because of some quirk in
836 ;; the relative order of expansion and type inference) but we
837 ;; also use overkill by NOTINLINEing GET-SLOTS-OR-NIL, because it
838 ;; looks as though (1) inlining isn't that much of a win anyway,
839 ;; and (2a) once you miss the FAST-METHOD-CALL clause you're
840 ;; going to be slow anyway, but (2b) code bloat still hurts even
841 ;; when it's off the critical path.
842 (declare (notinline get-slots-or-nil))
844 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
845 (cond ((typep ,emf 'fast-method-call)
846 (invoke-fast-method-call ,emf ,@required-args+rest-arg))
847 ,@(when (and (null restp) (= 1 (length required-args+rest-arg)))
848 `(((typep ,emf 'fixnum)
849 (let* ((.slots. (get-slots-or-nil
850 ,(car required-args+rest-arg)))
851 (value (when .slots. (clos-slots-ref .slots. ,emf))))
852 (if (eq value +slot-unbound+)
853 (slot-unbound-internal ,(car required-args+rest-arg)
856 ,@(when (and (null restp) (= 2 (length required-args+rest-arg)))
857 `(((typep ,emf 'fixnum)
858 (let ((.new-value. ,(car required-args+rest-arg))
859 (.slots. (get-slots-or-nil
860 ,(car required-args+rest-arg))))
862 (setf (clos-slots-ref .slots. ,emf) .new-value.))))))
864 ,@(when (and (null restp) (= 1 (length required-args+rest-arg)))
865 `(((typep ,emf 'fast-instance-boundp)
866 (let ((.slots. (get-slots-or-nil
867 ,(car required-args+rest-arg))))
869 (not (eq (clos-slots-ref
870 .slots. (fast-instance-boundp-index ,emf))
871 +slot-unbound+)))))))
876 (invoke-method-call ,emf ,restp ,@required-args+rest-arg))
879 `(apply (the function ,emf) ,@required-args+rest-arg)
880 `(funcall (the function ,emf)
881 ,@required-args+rest-arg))))))))
883 (defun invoke-emf (emf args)
884 (trace-emf-call emf t args)
887 (let* ((arg-info (fast-method-call-arg-info emf))
888 (restp (cdr arg-info))
889 (nreq (car arg-info)))
891 (let* ((rest-args (nthcdr nreq args))
892 (req-args (ldiff args rest-args)))
893 (apply (fast-method-call-function emf)
894 (fast-method-call-pv-cell emf)
895 (fast-method-call-next-method-call emf)
896 (nconc req-args (list rest-args))))
899 (invoke-fast-method-call emf)
900 (error "wrong number of args")))
903 (invoke-fast-method-call emf (car args))
904 (error "wrong number of args")))
907 (invoke-fast-method-call emf (car args) (cadr args))
908 (error "wrong number of args")))
910 (apply (fast-method-call-function emf)
911 (fast-method-call-pv-cell emf)
912 (fast-method-call-next-method-call emf)
915 (apply (method-call-function emf)
917 (method-call-call-method-args emf)))
919 (cond ((null args) (error "1 or 2 args were expected."))
921 (let* ((slots (get-slots (car args)))
922 (value (clos-slots-ref slots emf)))
923 (if (eq value +slot-unbound+)
924 (slot-unbound-internal (car args) emf)
927 (setf (clos-slots-ref (get-slots (cadr args)) emf)
929 (t (error "1 or 2 args were expected."))))
930 (fast-instance-boundp
931 (if (or (null args) (cdr args))
932 (error "1 arg was expected.")
933 (let ((slots (get-slots (car args))))
934 (not (eq (clos-slots-ref slots
935 (fast-instance-boundp-index emf))
940 ;; KLUDGE: A comment from the original PCL said "This can be improved alot."
941 (defun gf-make-function-from-emf (gf emf)
943 (fast-method-call (let* ((arg-info (gf-arg-info gf))
944 (nreq (arg-info-number-required arg-info))
945 (restp (arg-info-applyp arg-info)))
947 (trace-emf-call emf t args)
948 (apply (fast-method-call-function emf)
949 (fast-method-call-pv-cell emf)
950 (fast-method-call-next-method-call emf)
952 (let* ((rest-args (nthcdr nreq args))
953 (req-args (ldiff args
955 (nconc req-args rest-args))
957 (method-call (lambda (&rest args)
958 (trace-emf-call emf t args)
959 (apply (method-call-function emf)
961 (method-call-call-method-args emf))))
964 (defmacro bind-fast-lexical-method-macros ((args rest-arg next-method-call)
966 `(macrolet ((call-next-method-bind (&body body)
968 (call-next-method-body (cnm-args)
969 `(if ,',next-method-call
971 ;; This declaration suppresses a "deleting
972 ;; unreachable code" note for the following IF when
973 ;; REST-ARG is NIL. It is not nice for debugging
974 ;; SBCL itself, but at least it keeps us from
976 (declare (optimize (inhibit-warnings 3)))
977 (if (and (null ',rest-arg)
979 (eq (car cnm-args) 'list))
980 `(invoke-effective-method-function
981 ,',next-method-call nil
983 (let ((call `(invoke-effective-method-function
985 ,',(not (null rest-arg))
987 ,@',(when rest-arg `(,rest-arg)))))
989 (bind-args ((,@',args
995 (error "no next method")))
996 (next-method-p-body ()
997 `(not (null ,',next-method-call))))
1000 (defmacro bind-lexical-method-functions
1001 ((&key call-next-method-p next-method-p-p closurep applyp)
1003 (cond ((and (null call-next-method-p) (null next-method-p-p)
1007 ((and (null closurep)
1009 ;; OK to use MACROLET, and all args are mandatory
1010 ;; (else APPLYP would be true).
1011 `(call-next-method-bind
1012 (macrolet ((call-next-method (&rest cnm-args)
1013 `(call-next-method-body ,(when cnm-args
1014 `(list ,@cnm-args))))
1016 `(next-method-p-body)))
1019 `(call-next-method-bind
1020 (flet (,@(and call-next-method-p
1021 '((call-next-method (&rest cnm-args)
1022 (call-next-method-body cnm-args))))
1023 ,@(and next-method-p-p
1025 (next-method-p-body)))))
1028 (defmacro bind-args ((lambda-list args) &body body)
1029 (let ((args-tail '.args-tail.)
1032 (flet ((process-var (var)
1033 (if (memq var lambda-list-keywords)
1036 (&optional (setq state 'optional))
1037 (&key (setq state 'key))
1039 (&rest (setq state 'rest))
1040 (&aux (setq state 'aux))
1043 "encountered the non-standard lambda list keyword ~S"
1047 (required `((,var (pop ,args-tail))))
1048 (optional (cond ((not (consp var))
1049 `((,var (when ,args-tail
1050 (pop ,args-tail)))))
1052 `((,(car var) (if ,args-tail
1056 `((,(caddr var) ,args-tail)
1057 (,(car var) (if ,args-tail
1060 (rest `((,var ,args-tail)))
1061 (key (cond ((not (consp var))
1062 `((,var (get-key-arg ,(keywordicate var)
1065 (multiple-value-bind (keyword variable)
1066 (if (consp (car var))
1069 (values (keywordicate (car var))
1071 `((,key (get-key-arg1 ',keyword ,args-tail))
1072 (,variable (if (consp ,key)
1076 (multiple-value-bind (keyword variable)
1077 (if (consp (car var))
1080 (values (keywordicate (car var))
1082 `((,key (get-key-arg1 ',keyword ,args-tail))
1084 (,variable (if (consp ,key)
1088 (let ((bindings (mapcan #'process-var lambda-list)))
1089 `(let* ((,args-tail ,args)
1091 (declare (ignorable ,args-tail))
1094 (defun get-key-arg (keyword list)
1095 (loop (when (atom list) (return nil))
1096 (when (eq (car list) keyword) (return (cadr list)))
1097 (setq list (cddr list))))
1099 (defun get-key-arg1 (keyword list)
1100 (loop (when (atom list) (return nil))
1101 (when (eq (car list) keyword) (return (cdr list)))
1102 (setq list (cddr list))))
1104 (defun walk-method-lambda (method-lambda required-parameters env slots calls)
1105 (let ((call-next-method-p nil) ; flag indicating that CALL-NEXT-METHOD
1106 ; should be in the method definition
1107 (closurep nil) ; flag indicating that #'CALL-NEXT-METHOD
1108 ; was seen in the body of a method
1109 (next-method-p-p nil)) ; flag indicating that NEXT-METHOD-P
1110 ; should be in the method definition
1111 (flet ((walk-function (form context env)
1112 (cond ((not (eq context :eval)) form)
1113 ;; FIXME: Jumping to a conclusion from the way it's used
1114 ;; above, perhaps CONTEXT should be called SITUATION
1115 ;; (after the term used in the ANSI specification of
1116 ;; EVAL-WHEN) and given modern ANSI keyword values
1117 ;; like :LOAD-TOPLEVEL.
1118 ((not (listp form)) form)
1119 ((eq (car form) 'call-next-method)
1120 (setq call-next-method-p t)
1122 ((eq (car form) 'next-method-p)
1123 (setq next-method-p-p t)
1125 ((and (eq (car form) 'function)
1126 (cond ((eq (cadr form) 'call-next-method)
1127 (setq call-next-method-p t)
1130 ((eq (cadr form) 'next-method-p)
1131 (setq next-method-p-p t)
1135 ((and (memq (car form)
1136 '(slot-value set-slot-value slot-boundp))
1137 (constantp (caddr form)))
1138 (let ((parameter (can-optimize-access form
1141 (let ((fun (ecase (car form)
1142 (slot-value #'optimize-slot-value)
1143 (set-slot-value #'optimize-set-slot-value)
1144 (slot-boundp #'optimize-slot-boundp))))
1145 (funcall fun slots parameter form))))
1146 ((and (eq (car form) 'apply)
1148 (eq (car (cadr form)) 'function)
1149 (generic-function-name-p (cadr (cadr form))))
1150 (optimize-generic-function-call
1151 form required-parameters env slots calls))
1152 ((generic-function-name-p (car form))
1153 (optimize-generic-function-call
1154 form required-parameters env slots calls))
1155 ((and (eq (car form) 'asv-funcall)
1156 *optimize-asv-funcall-p*)
1158 (reader (push (third form) *asv-readers*))
1159 (writer (push (third form) *asv-writers*))
1160 (boundp (push (third form) *asv-boundps*)))
1161 `(,(second form) ,@(cddddr form)))
1164 (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1165 (values walked-lambda
1168 next-method-p-p)))))
1170 (defun generic-function-name-p (name)
1171 (and (legal-fun-name-p name)
1173 (if (eq *boot-state* 'complete)
1174 (standard-generic-function-p (gdefinition name))
1175 (funcallable-instance-p (gdefinition name)))))
1177 (defvar *method-function-plist* (make-hash-table :test 'eq))
1180 (defvar *mf1cp* nil)
1183 (defvar *mf2cp* nil)
1185 (defun method-function-plist (method-function)
1186 (unless (eq method-function *mf1*)
1187 (rotatef *mf1* *mf2*)
1188 (rotatef *mf1p* *mf2p*)
1189 (rotatef *mf1cp* *mf2cp*))
1190 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1191 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1192 (unless (eq method-function *mf1*)
1193 (setf *mf1* method-function
1195 *mf1p* (gethash method-function *method-function-plist*)))
1198 (defun (setf method-function-plist)
1199 (val method-function)
1200 (unless (eq method-function *mf1*)
1201 (rotatef *mf1* *mf2*)
1202 (rotatef *mf1cp* *mf2cp*)
1203 (rotatef *mf1p* *mf2p*))
1204 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1205 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1206 (setf *mf1* method-function
1210 (defun method-function-get (method-function key &optional default)
1211 (getf (method-function-plist method-function) key default))
1213 (defun (setf method-function-get)
1214 (val method-function key)
1215 (setf (getf (method-function-plist method-function) key) val))
1217 (defun method-function-pv-table (method-function)
1218 (method-function-get method-function :pv-table))
1220 (defun method-function-method (method-function)
1221 (method-function-get method-function :method))
1223 (defun method-function-needs-next-methods-p (method-function)
1224 (method-function-get method-function :needs-next-methods-p t))
1226 (defmacro method-function-closure-generator (method-function)
1227 `(method-function-get ,method-function 'closure-generator))
1229 (defun load-defmethod
1230 (class name quals specls ll initargs &optional pv-table-symbol)
1231 (setq initargs (copy-tree initargs))
1232 (let ((method-spec (or (getf initargs :method-spec)
1233 (make-method-spec name quals specls))))
1234 (setf (getf initargs :method-spec) method-spec)
1235 (load-defmethod-internal class name quals specls
1236 ll initargs pv-table-symbol)))
1238 (defun load-defmethod-internal
1239 (method-class gf-spec qualifiers specializers lambda-list
1240 initargs pv-table-symbol)
1241 (when pv-table-symbol
1242 (setf (getf (getf initargs :plist) :pv-table-symbol)
1244 (when (and (eq *boot-state* 'complete)
1246 (let* ((gf (fdefinition gf-spec))
1247 (method (and (generic-function-p gf)
1250 (parse-specializers specializers)
1253 (sb-kernel::style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1254 gf-spec qualifiers specializers))))
1255 (let ((method (apply #'add-named-method
1256 gf-spec qualifiers specializers lambda-list
1257 :definition-source `((defmethod ,gf-spec
1262 (unless (or (eq method-class 'standard-method)
1263 (eq (find-class method-class nil) (class-of method)))
1264 ;; FIXME: should be STYLE-WARNING?
1265 (format *error-output*
1266 "~&At the time the method with qualifiers ~:S and~%~
1267 specializers ~:S on the generic function ~S~%~
1268 was compiled, the method-class for that generic function was~%~
1269 ~S. But, the method class is now ~S, this~%~
1270 may mean that this method was compiled improperly.~%"
1271 qualifiers specializers gf-spec
1272 method-class (class-name (class-of method))))
1275 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1276 `(method ,gf-spec ,@qualifiers ,unparsed-specializers))
1278 (defun initialize-method-function (initargs &optional return-function-p method)
1279 (let* ((mf (getf initargs :function))
1280 (method-spec (getf initargs :method-spec))
1281 (plist (getf initargs :plist))
1282 (pv-table-symbol (getf plist :pv-table-symbol))
1284 (mff (getf initargs :fast-function)))
1285 (flet ((set-mf-property (p v)
1287 (setf (method-function-get mf p) v))
1289 (setf (method-function-get mff p) v))))
1292 (setq mf (set-fun-name mf method-spec)))
1294 (let ((name `(,(or (get (car method-spec) 'fast-sym)
1295 (setf (get (car method-spec) 'fast-sym)
1296 ;; KLUDGE: If we're going to be
1297 ;; interning private symbols in our
1298 ;; a this way, it would be cleanest
1299 ;; to use a separate package
1300 ;; %PCL-PRIVATE or something, and
1301 ;; failing that, to use a special
1302 ;; symbol prefix denoting privateness.
1304 (intern (format nil "FAST-~A"
1307 ,@(cdr method-spec))))
1308 (set-fun-name mff name)
1310 (set-mf-property :name name)))))
1312 (let ((snl (getf plist :slot-name-lists))
1313 (cl (getf plist :call-list)))
1315 (setq pv-table (intern-pv-table :slot-name-lists snl
1317 (when pv-table (set pv-table-symbol pv-table))
1318 (set-mf-property :pv-table pv-table)))
1319 (loop (when (null plist) (return nil))
1320 (set-mf-property (pop plist) (pop plist)))
1322 (set-mf-property :method method))
1323 (when return-function-p
1324 (or mf (method-function-from-fast-function mff)))))))
1326 (defun analyze-lambda-list (lambda-list)
1327 (flet (;; FIXME: Is this redundant with SB-C::MAKE-KEYWORD-FOR-ARG?
1328 (parse-key-arg (arg)
1330 (if (listp (car arg))
1332 (keywordicate (car arg)))
1333 (keywordicate arg))))
1339 (allow-other-keys-p nil)
1341 (keyword-parameters ())
1343 (dolist (x lambda-list)
1344 (if (memq x lambda-list-keywords)
1346 (&optional (setq state 'optional))
1349 (&allow-other-keys (setq allow-other-keys-p t))
1350 (&rest (setq restp t
1354 (error "encountered the non-standard lambda list keyword ~S"
1357 (required (incf nrequired))
1358 (optional (incf noptional))
1359 (key (push (parse-key-arg x) keywords)
1360 (push x keyword-parameters))
1361 (rest (incf nrest)))))
1362 (when (and restp (zerop nrest))
1363 (error "Error in lambda-list:~%~
1364 After &REST, a DEFGENERIC lambda-list ~
1365 must be followed by at least one variable."))
1366 (values nrequired noptional keysp restp allow-other-keys-p
1368 (reverse keyword-parameters)))))
1370 (defun keyword-spec-name (x)
1371 (let ((key (if (atom x) x (car x))))
1376 (defun ftype-declaration-from-lambda-list (lambda-list name)
1377 (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1378 keywords keyword-parameters)
1379 (analyze-lambda-list lambda-list)
1380 (declare (ignore keyword-parameters))
1381 (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1382 (old-ftype (if (sb-kernel:fun-type-p old) old nil))
1383 (old-restp (and old-ftype (sb-kernel:fun-type-rest old-ftype)))
1384 (old-keys (and old-ftype
1385 (mapcar #'sb-kernel:key-info-name
1386 (sb-kernel:fun-type-keywords
1388 (old-keysp (and old-ftype (sb-kernel:fun-type-keyp old-ftype)))
1389 (old-allowp (and old-ftype
1390 (sb-kernel:fun-type-allowp old-ftype)))
1391 (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1392 `(function ,(append (make-list nrequired :initial-element t)
1393 (when (plusp noptional)
1394 (append '(&optional)
1395 (make-list noptional :initial-element t)))
1396 (when (or restp old-restp)
1398 (when (or keysp old-keysp)
1400 (mapcar (lambda (key)
1403 (when (or allow-other-keys-p old-allowp)
1404 '(&allow-other-keys)))))
1407 (defun defgeneric-declaration (spec lambda-list)
1409 (setq spec (get-setf-fun-name (cadr spec))))
1410 `(ftype ,(ftype-declaration-from-lambda-list lambda-list spec) ,spec))
1412 ;;;; early generic function support
1414 (defvar *!early-generic-functions* ())
1416 (defun ensure-generic-function (fun-name
1420 (declare (ignore environment))
1421 (let ((existing (and (gboundp fun-name)
1422 (gdefinition fun-name))))
1424 (eq *boot-state* 'complete)
1425 (null (generic-function-p existing)))
1426 (generic-clobbers-function fun-name)
1427 (apply #'ensure-generic-function-using-class
1428 existing fun-name all-keys))))
1430 (defun generic-clobbers-function (fun-name)
1431 (error 'simple-program-error
1432 :format-control "~S already names an ordinary function or a macro."
1433 :format-arguments (list fun-name)))
1435 (defvar *sgf-wrapper*
1436 (boot-make-wrapper (early-class-size 'standard-generic-function)
1437 'standard-generic-function))
1439 (defvar *sgf-slots-init*
1440 (mapcar (lambda (canonical-slot)
1441 (if (memq (getf canonical-slot :name) '(arg-info source))
1443 (let ((initfunction (getf canonical-slot :initfunction)))
1445 (funcall initfunction)
1447 (early-collect-inheritance 'standard-generic-function)))
1449 (defvar *sgf-method-class-index*
1450 (!bootstrap-slot-index 'standard-generic-function 'method-class))
1452 (defun early-gf-p (x)
1453 (and (fsc-instance-p x)
1454 (eq (clos-slots-ref (get-slots x) *sgf-method-class-index*)
1457 (defvar *sgf-methods-index*
1458 (!bootstrap-slot-index 'standard-generic-function 'methods))
1460 (defmacro early-gf-methods (gf)
1461 `(clos-slots-ref (get-slots ,gf) *sgf-methods-index*))
1463 (defvar *sgf-arg-info-index*
1464 (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1466 (defmacro early-gf-arg-info (gf)
1467 `(clos-slots-ref (get-slots ,gf) *sgf-arg-info-index*))
1469 (defvar *sgf-dfun-state-index*
1470 (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1472 (defstruct (arg-info
1474 (:constructor make-arg-info ())
1476 (arg-info-lambda-list :no-lambda-list)
1479 arg-info-number-optional
1481 arg-info-keys ;nil no &KEY or &REST allowed
1482 ;(k1 k2 ..) Each method must accept these &KEY arguments.
1483 ;T must have &KEY or &REST
1485 gf-info-simple-accessor-type ; nil, reader, writer, boundp
1486 (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1488 gf-info-static-c-a-m-emf
1489 (gf-info-c-a-m-emf-std-p t)
1492 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1494 (defun arg-info-valid-p (arg-info)
1495 (not (null (arg-info-number-optional arg-info))))
1497 (defun arg-info-applyp (arg-info)
1498 (or (plusp (arg-info-number-optional arg-info))
1499 (arg-info-key/rest-p arg-info)))
1501 (defun arg-info-number-required (arg-info)
1502 (length (arg-info-metatypes arg-info)))
1504 (defun arg-info-nkeys (arg-info)
1505 (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1507 ;;; Keep pages clean by not setting if the value is already the same.
1508 (defmacro esetf (pos val)
1509 (let ((valsym (gensym "value")))
1510 `(let ((,valsym ,val))
1511 (unless (equal ,pos ,valsym)
1512 (setf ,pos ,valsym)))))
1514 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1515 argument-precedence-order)
1516 (let* ((arg-info (if (eq *boot-state* 'complete)
1518 (early-gf-arg-info gf)))
1519 (methods (if (eq *boot-state* 'complete)
1520 (generic-function-methods gf)
1521 (early-gf-methods gf)))
1522 (was-valid-p (integerp (arg-info-number-optional arg-info)))
1523 (first-p (and new-method (null (cdr methods)))))
1524 (when (and (not lambda-list-p) methods)
1525 (setq lambda-list (gf-lambda-list gf)))
1526 (when (or lambda-list-p
1528 (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1529 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1530 (analyze-lambda-list lambda-list)
1531 (when (and methods (not first-p))
1532 (let ((gf-nreq (arg-info-number-required arg-info))
1533 (gf-nopt (arg-info-number-optional arg-info))
1534 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1535 (unless (and (= nreq gf-nreq)
1537 (eq (or keysp restp) gf-key/rest-p))
1538 (error "The lambda-list ~S is incompatible with ~
1539 existing methods of ~S."
1542 (esetf (arg-info-lambda-list arg-info) lambda-list))
1543 (when (or lambda-list-p argument-precedence-order
1544 (null (arg-info-precedence arg-info)))
1545 (esetf (arg-info-precedence arg-info)
1546 (compute-precedence lambda-list nreq
1547 argument-precedence-order)))
1548 (esetf (arg-info-metatypes arg-info) (make-list nreq))
1549 (esetf (arg-info-number-optional arg-info) nopt)
1550 (esetf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1551 (esetf (arg-info-keys arg-info)
1553 (if allow-other-keys-p t keywords)
1554 (arg-info-key/rest-p arg-info)))))
1556 (check-method-arg-info gf arg-info new-method))
1557 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1560 (defun check-method-arg-info (gf arg-info method)
1561 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1562 (analyze-lambda-list (if (consp method)
1563 (early-method-lambda-list method)
1564 (method-lambda-list method)))
1565 (flet ((lose (string &rest args)
1567 "attempt to add the method ~S to the generic function ~S.~%~
1571 (apply #'format nil string args)))
1572 (comparison-description (x y)
1573 (if (> x y) "more" "fewer")))
1574 (let ((gf-nreq (arg-info-number-required arg-info))
1575 (gf-nopt (arg-info-number-optional arg-info))
1576 (gf-key/rest-p (arg-info-key/rest-p arg-info))
1577 (gf-keywords (arg-info-keys arg-info)))
1578 (unless (= nreq gf-nreq)
1580 "the method has ~A required arguments than the generic function."
1581 (comparison-description nreq gf-nreq)))
1582 (unless (= nopt gf-nopt)
1584 "the method has ~A optional arguments than the generic function."
1585 (comparison-description nopt gf-nopt)))
1586 (unless (eq (or keysp restp) gf-key/rest-p)
1588 "The method and generic function differ in whether they accept~%~
1589 &REST or &KEY arguments."))
1590 (when (consp gf-keywords)
1591 (unless (or (and restp (not keysp))
1593 (every (lambda (k) (memq k keywords)) gf-keywords))
1594 (lose "the method does not accept each of the &KEY arguments~%~
1598 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1599 (let* ((existing-p (and methods (cdr methods) new-method))
1600 (nreq (length (arg-info-metatypes arg-info)))
1601 (metatypes (if existing-p
1602 (arg-info-metatypes arg-info)
1604 (type (if existing-p
1605 (gf-info-simple-accessor-type arg-info)
1607 (when (arg-info-valid-p arg-info)
1608 (dolist (method (if new-method (list new-method) methods))
1609 (let* ((specializers (if (or (eq *boot-state* 'complete)
1610 (not (consp method)))
1611 (method-specializers method)
1612 (early-method-specializers method t)))
1613 (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1615 (early-method-class method)))
1616 (new-type (when (and class
1617 (or (not (eq *boot-state* 'complete))
1618 (eq (generic-function-method-combination gf)
1619 *standard-method-combination*)))
1620 (cond ((eq class *the-class-standard-reader-method*)
1622 ((eq class *the-class-standard-writer-method*)
1624 ((eq class *the-class-standard-boundp-method*)
1626 (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1627 (setq type (cond ((null type) new-type)
1628 ((eq type new-type) type)
1630 (esetf (arg-info-metatypes arg-info) metatypes)
1631 (esetf (gf-info-simple-accessor-type arg-info) type)))
1632 (when (or (not was-valid-p) first-p)
1633 (multiple-value-bind (c-a-m-emf std-p)
1636 (compute-applicable-methods-emf gf))
1637 (esetf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1638 (esetf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1639 (unless (gf-info-c-a-m-emf-std-p arg-info)
1640 (esetf (gf-info-simple-accessor-type arg-info) t))))
1642 (let ((name (if (eq *boot-state* 'complete)
1643 (generic-function-name gf)
1644 (!early-gf-name gf))))
1645 (esetf (gf-precompute-dfun-and-emf-p arg-info)
1646 (let* ((sym (if (atom name) name (cadr name)))
1647 (pkg-list (cons *pcl-package*
1648 (package-use-list *pcl-package*))))
1649 (and sym (symbolp sym)
1650 (not (null (memq (symbol-package sym) pkg-list)))
1651 (not (find #\space (symbol-name sym))))))))
1652 (esetf (gf-info-fast-mf-p arg-info)
1653 (or (not (eq *boot-state* 'complete))
1654 (let* ((method-class (generic-function-method-class gf))
1655 (methods (compute-applicable-methods
1656 #'make-method-lambda
1657 (list gf (class-prototype method-class)
1659 (and methods (null (cdr methods))
1660 (let ((specls (method-specializers (car methods))))
1661 (and (classp (car specls))
1662 (eq 'standard-generic-function
1663 (class-name (car specls)))
1664 (classp (cadr specls))
1665 (eq 'standard-method
1666 (class-name (cadr specls)))))))))
1669 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
1671 ;;; The STATIC-SLOTS field of the funcallable instances used as early
1672 ;;; generic functions is used to store the early methods and early
1673 ;;; discriminator code for the early generic function. The static
1674 ;;; slots field of the fins contains a list whose:
1675 ;;; CAR - a list of the early methods on this early gf
1676 ;;; CADR - the early discriminator code for this method
1677 (defun ensure-generic-function-using-class (existing spec &rest keys
1678 &key (lambda-list nil
1681 (declare (ignore keys))
1682 (cond ((and existing (early-gf-p existing))
1684 ((assoc spec *!generic-function-fixups* :test #'equal)
1686 (make-early-gf spec lambda-list lambda-list-p existing)
1687 (error "The function ~S is not already defined." spec)))
1689 (error "~S should be on the list ~S."
1691 '*!generic-function-fixups*))
1693 (pushnew spec *!early-generic-functions* :test #'equal)
1694 (make-early-gf spec lambda-list lambda-list-p))))
1696 (defun make-early-gf (spec &optional lambda-list lambda-list-p function)
1697 (let ((fin (allocate-funcallable-instance *sgf-wrapper* *sgf-slots-init*)))
1698 (set-funcallable-instance-fun
1701 (if (eq spec 'print-object)
1702 #'(sb-kernel:instance-lambda (instance stream)
1703 (print-unreadable-object (instance stream :identity t)
1704 (format stream "std-instance")))
1705 #'(sb-kernel:instance-lambda (&rest args)
1706 (declare (ignore args))
1707 (error "The function of the funcallable-instance ~S~
1708 has not been set." fin)))))
1709 (setf (gdefinition spec) fin)
1710 (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
1711 (!bootstrap-set-slot 'standard-generic-function
1715 (set-fun-name fin spec)
1716 (let ((arg-info (make-arg-info)))
1717 (setf (early-gf-arg-info fin) arg-info)
1719 (proclaim (defgeneric-declaration spec lambda-list))
1720 (set-arg-info fin :lambda-list lambda-list)))
1723 (defun set-dfun (gf &optional dfun cache info)
1725 (setf (cache-owner cache) gf))
1726 (let ((new-state (if (and dfun (or cache info))
1727 (list* dfun cache info)
1729 (if (eq *boot-state* 'complete)
1730 (setf (gf-dfun-state gf) new-state)
1731 (setf (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*)
1735 (defun gf-dfun-cache (gf)
1736 (let ((state (if (eq *boot-state* 'complete)
1738 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1741 (cons (cadr state)))))
1743 (defun gf-dfun-info (gf)
1744 (let ((state (if (eq *boot-state* 'complete)
1746 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1749 (cons (cddr state)))))
1751 (defvar *sgf-name-index*
1752 (!bootstrap-slot-index 'standard-generic-function 'name))
1754 (defun !early-gf-name (gf)
1755 (clos-slots-ref (get-slots gf) *sgf-name-index*))
1757 (defun gf-lambda-list (gf)
1758 (let ((arg-info (if (eq *boot-state* 'complete)
1760 (early-gf-arg-info gf))))
1761 (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
1762 (let ((methods (if (eq *boot-state* 'complete)
1763 (generic-function-methods gf)
1764 (early-gf-methods gf))))
1767 (warn "no way to determine the lambda list for ~S" gf)
1769 (let* ((method (car (last methods)))
1770 (ll (if (consp method)
1771 (early-method-lambda-list method)
1772 (method-lambda-list method)))
1773 (k (member '&key ll)))
1775 (append (ldiff ll (cdr k)) '(&allow-other-keys))
1777 (arg-info-lambda-list arg-info))))
1779 (defmacro real-ensure-gf-internal (gf-class all-keys env)
1781 (cond ((symbolp ,gf-class)
1782 (setq ,gf-class (find-class ,gf-class t ,env)))
1783 ((classp ,gf-class))
1785 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
1786 class nor a symbol that names a class."
1788 (remf ,all-keys :generic-function-class)
1789 (remf ,all-keys :environment)
1790 (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
1791 (unless (eq combin '.shes-not-there.)
1792 (setf (getf ,all-keys :method-combination)
1793 (find-method-combination (class-prototype ,gf-class)
1796 (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
1797 (unless (eq method-class '.shes-not-there.)
1798 (setf (getf ,all-keys :method-class)
1799 (find-class method-class t ,env))))))
1801 (defun real-ensure-gf-using-class--generic-function
1805 &key environment (lambda-list nil lambda-list-p)
1806 (generic-function-class 'standard-generic-function gf-class-p)
1808 (real-ensure-gf-internal generic-function-class all-keys environment)
1809 (unless (or (null gf-class-p)
1810 (eq (class-of existing) generic-function-class))
1811 (change-class existing generic-function-class))
1813 (apply #'reinitialize-instance existing all-keys)
1815 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1817 (defun real-ensure-gf-using-class--null
1821 &key environment (lambda-list nil lambda-list-p)
1822 (generic-function-class 'standard-generic-function)
1824 (declare (ignore existing))
1825 (real-ensure-gf-internal generic-function-class all-keys environment)
1827 (setf (gdefinition fun-name)
1828 (apply #'make-instance generic-function-class
1829 :name fun-name all-keys))
1831 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1833 (defun get-generic-fun-info (gf)
1834 ;; values nreq applyp metatypes nkeys arg-info
1835 (multiple-value-bind (applyp metatypes arg-info)
1836 (let* ((arg-info (if (early-gf-p gf)
1837 (early-gf-arg-info gf)
1839 (metatypes (arg-info-metatypes arg-info)))
1840 (values (arg-info-applyp arg-info)
1843 (values (length metatypes) applyp metatypes
1844 (count-if (lambda (x) (neq x t)) metatypes)
1847 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
1848 &optional slot-name)
1849 (initialize-method-function initargs)
1852 ;; Figure out whether we got class objects or class names as the
1853 ;; specializers and set parsed and unparsed appropriately. If we
1854 ;; got class objects, then we can compute unparsed, but if we got
1855 ;; class names we don't try to compute parsed.
1857 ;; Note that the use of not symbolp in this call to every should be
1858 ;; read as 'classp' we can't use classp itself because it doesn't
1860 (if (every (lambda (s) (not (symbolp s))) specializers)
1861 (setq parsed specializers
1862 unparsed (mapcar (lambda (s)
1863 (if (eq s t) t (class-name s)))
1865 (setq unparsed specializers
1867 (list :early-method ;This is an early method dammit!
1869 (getf initargs :function)
1870 (getf initargs :fast-function)
1872 parsed ;The parsed specializers. This is used
1873 ;by early-method-specializers to cache
1874 ;the parse. Note that this only comes
1875 ;into play when there is more than one
1876 ;early method on an early gf.
1878 (list class ;A list to which real-make-a-method
1879 qualifiers ;can be applied to make a real method
1880 arglist ;corresponding to this early one.
1886 (defun real-make-a-method
1887 (class qualifiers lambda-list specializers initargs doc
1888 &optional slot-name)
1889 (setq specializers (parse-specializers specializers))
1890 (apply #'make-instance class
1891 :qualifiers qualifiers
1892 :lambda-list lambda-list
1893 :specializers specializers
1895 :slot-name slot-name
1899 (defun early-method-function (early-method)
1900 (values (cadr early-method) (caddr early-method)))
1902 (defun early-method-class (early-method)
1903 (find-class (car (fifth early-method))))
1905 (defun early-method-standard-accessor-p (early-method)
1906 (let ((class (first (fifth early-method))))
1907 (or (eq class 'standard-reader-method)
1908 (eq class 'standard-writer-method)
1909 (eq class 'standard-boundp-method))))
1911 (defun early-method-standard-accessor-slot-name (early-method)
1912 (seventh (fifth early-method)))
1914 ;;; Fetch the specializers of an early method. This is basically just
1915 ;;; a simple accessor except that when the second argument is t, this
1916 ;;; converts the specializers from symbols into class objects. The
1917 ;;; class objects are cached in the early method, this makes
1918 ;;; bootstrapping faster because the class objects only have to be
1922 ;;; The second argument should only be passed as T by
1923 ;;; early-lookup-method. This is to implement the rule that only when
1924 ;;; there is more than one early method on a generic function is the
1925 ;;; conversion from class names to class objects done. This
1926 ;;; corresponds to the fact that we are only allowed to have one
1927 ;;; method on any generic function up until the time classes exist.
1928 (defun early-method-specializers (early-method &optional objectsp)
1929 (if (and (listp early-method)
1930 (eq (car early-method) :early-method))
1931 (cond ((eq objectsp t)
1932 (or (fourth early-method)
1933 (setf (fourth early-method)
1934 (mapcar #'find-class (cadddr (fifth early-method))))))
1936 (cadddr (fifth early-method))))
1937 (error "~S is not an early-method." early-method)))
1939 (defun early-method-qualifiers (early-method)
1940 (cadr (fifth early-method)))
1942 (defun early-method-lambda-list (early-method)
1943 (caddr (fifth early-method)))
1945 (defun early-add-named-method (generic-function-name
1950 (let* ((gf (ensure-generic-function generic-function-name))
1952 (dolist (m (early-gf-methods gf))
1953 (when (and (equal (early-method-specializers m) specializers)
1954 (equal (early-method-qualifiers m) qualifiers))
1956 (new (make-a-method 'standard-method
1962 (when existing (remove-method gf existing))
1963 (add-method gf new)))
1965 ;;; This is the early version of ADD-METHOD. Later this will become a
1966 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
1967 ;;; special knowledge about ADD-METHOD.
1968 (defun add-method (generic-function method)
1969 (when (not (fsc-instance-p generic-function))
1970 (error "Early ADD-METHOD didn't get a funcallable instance."))
1971 (when (not (and (listp method) (eq (car method) :early-method)))
1972 (error "Early ADD-METHOD didn't get an early method."))
1973 (push method (early-gf-methods generic-function))
1974 (set-arg-info generic-function :new-method method)
1975 (unless (assoc (!early-gf-name generic-function)
1976 *!generic-function-fixups*
1978 (update-dfun generic-function)))
1980 ;;; This is the early version of REMOVE-METHOD. See comments on
1981 ;;; the early version of ADD-METHOD.
1982 (defun remove-method (generic-function method)
1983 (when (not (fsc-instance-p generic-function))
1984 (error "An early remove-method didn't get a funcallable instance."))
1985 (when (not (and (listp method) (eq (car method) :early-method)))
1986 (error "An early remove-method didn't get an early method."))
1987 (setf (early-gf-methods generic-function)
1988 (remove method (early-gf-methods generic-function)))
1989 (set-arg-info generic-function)
1990 (unless (assoc (!early-gf-name generic-function)
1991 *!generic-function-fixups*
1993 (update-dfun generic-function)))
1995 ;;; This is the early version of GET-METHOD. See comments on the early
1996 ;;; version of ADD-METHOD.
1997 (defun get-method (generic-function qualifiers specializers
1998 &optional (errorp t))
1999 (if (early-gf-p generic-function)
2000 (or (dolist (m (early-gf-methods generic-function))
2001 (when (and (or (equal (early-method-specializers m nil)
2003 (equal (early-method-specializers m t)
2005 (equal (early-method-qualifiers m) qualifiers))
2008 (error "can't get early method")
2010 (real-get-method generic-function qualifiers specializers errorp)))
2012 (defun !fix-early-generic-functions ()
2013 (let ((accessors nil))
2014 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2015 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2016 (dolist (early-gf-spec *!early-generic-functions*)
2017 (when (every #'early-method-standard-accessor-p
2018 (early-gf-methods (gdefinition early-gf-spec)))
2019 (push early-gf-spec accessors)))
2020 (dolist (spec (nconc accessors
2021 '(accessor-method-slot-name
2022 generic-function-methods
2027 slot-definition-location
2028 slot-definition-name
2031 class-precedence-list
2032 slot-boundp-using-class
2033 (setf slot-value-using-class)
2034 slot-value-using-class
2037 funcallable-standard-class-p
2040 (setq *!early-generic-functions*
2042 (delete spec *!early-generic-functions* :test #'equal))))
2044 (dolist (early-gf-spec *!early-generic-functions*)
2045 (/show early-gf-spec)
2046 (let* ((gf (gdefinition early-gf-spec))
2047 (methods (mapcar (lambda (early-method)
2048 (let ((args (copy-list (fifth
2051 (early-method-specializers
2053 (apply #'real-make-a-method args)))
2054 (early-gf-methods gf))))
2055 (setf (generic-function-method-class gf) *the-class-standard-method*)
2056 (setf (generic-function-method-combination gf)
2057 *standard-method-combination*)
2058 (set-methods gf methods)))
2060 (dolist (fn *!early-functions*)
2062 (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2064 (dolist (fixup *!generic-function-fixups*)
2066 (let* ((fspec (car fixup))
2067 (gf (gdefinition fspec))
2068 (methods (mapcar (lambda (method)
2069 (let* ((lambda-list (first method))
2070 (specializers (second method))
2071 (method-fn-name (third method))
2072 (fn-name (or method-fn-name fspec))
2073 (fn (fdefinition fn-name))
2077 (lambda (args next-methods)
2081 `(call ,fn-name)))))
2082 (declare (type function fn))
2083 (make-a-method 'standard-method
2090 (setf (generic-function-method-class gf) *the-class-standard-method*)
2091 (setf (generic-function-method-combination gf)
2092 *standard-method-combination*)
2093 (set-methods gf methods))))
2094 (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2096 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2097 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2098 ;;; is really implemented.
2099 (defun parse-defmethod (cdr-of-form)
2100 (declare (list cdr-of-form))
2101 (let ((name (pop cdr-of-form))
2104 (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2105 (push (pop cdr-of-form) qualifiers)
2106 (return (setq qualifiers (nreverse qualifiers)))))
2107 (setq spec-ll (pop cdr-of-form))
2108 (values name qualifiers spec-ll cdr-of-form)))
2110 (defun parse-specializers (specializers)
2111 (declare (list specializers))
2112 (flet ((parse (spec)
2113 (let ((result (specializer-from-type spec)))
2114 (if (specializerp result)
2117 (error "~S was used as a specializer,~%~
2118 but is not the name of a class."
2120 (error "~S is not a legal specializer." spec))))))
2121 (mapcar #'parse specializers)))
2123 (defun unparse-specializers (specializers-or-method)
2124 (if (listp specializers-or-method)
2125 (flet ((unparse (spec)
2126 (if (specializerp spec)
2127 (let ((type (specializer-type spec)))
2128 (if (and (consp type)
2129 (eq (car type) 'class))
2130 (let* ((class (cadr type))
2131 (class-name (class-name class)))
2132 (if (eq class (find-class class-name nil))
2136 (error "~S is not a legal specializer." spec))))
2137 (mapcar #'unparse specializers-or-method))
2138 (unparse-specializers (method-specializers specializers-or-method))))
2140 (defun parse-method-or-spec (spec &optional (errorp t))
2141 (let (gf method name temp)
2144 gf (method-generic-function method)
2145 temp (and gf (generic-function-name gf))
2148 (make-method-spec temp
2149 (method-qualifiers method)
2150 (unparse-specializers
2151 (method-specializers method))))
2152 (make-symbol (format nil "~S" method))))
2153 (multiple-value-bind (gf-spec quals specls)
2154 (parse-defmethod spec)
2155 (and (setq gf (and (or errorp (gboundp gf-spec))
2156 (gdefinition gf-spec)))
2157 (let ((nreq (compute-discriminating-function-arglist-info gf)))
2158 (setq specls (append (parse-specializers specls)
2159 (make-list (- nreq (length specls))
2163 (setq method (get-method gf quals specls errorp))
2165 (intern-fun-name (make-method-spec gf-spec
2168 (values gf method name)))
2170 (defun extract-parameters (specialized-lambda-list)
2171 (multiple-value-bind (parameters ignore1 ignore2)
2172 (parse-specialized-lambda-list specialized-lambda-list)
2173 (declare (ignore ignore1 ignore2))
2176 (defun extract-lambda-list (specialized-lambda-list)
2177 (multiple-value-bind (ignore1 lambda-list ignore2)
2178 (parse-specialized-lambda-list specialized-lambda-list)
2179 (declare (ignore ignore1 ignore2))
2182 (defun extract-specializer-names (specialized-lambda-list)
2183 (multiple-value-bind (ignore1 ignore2 specializers)
2184 (parse-specialized-lambda-list specialized-lambda-list)
2185 (declare (ignore ignore1 ignore2))
2188 (defun extract-required-parameters (specialized-lambda-list)
2189 (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2190 (parse-specialized-lambda-list specialized-lambda-list)
2191 (declare (ignore ignore1 ignore2 ignore3))
2192 required-parameters))
2194 (defun parse-specialized-lambda-list (arglist &optional post-keyword)
2195 ;;(declare (values parameters lambda-list specializers required-parameters))
2196 (let ((arg (car arglist)))
2197 (cond ((null arglist) (values nil nil nil nil))
2199 (values nil arglist nil))
2200 ((memq arg lambda-list-keywords)
2201 (unless (memq arg '(&optional &rest &key &allow-other-keys &aux))
2202 ;; Now, since we try to conform to ANSI, non-standard
2203 ;; lambda-list-keywords should be treated as errors.
2204 (error 'simple-program-error
2205 :format-control "unrecognized lambda-list keyword ~S ~
2207 :format-arguments (list arg)))
2208 ;; When we are at a lambda-list keyword, the parameters
2209 ;; don't include the lambda-list keyword; the lambda-list
2210 ;; does include the lambda-list keyword; and no
2211 ;; specializers are allowed to follow the lambda-list
2212 ;; keywords (at least for now).
2213 (multiple-value-bind (parameters lambda-list)
2214 (parse-specialized-lambda-list (cdr arglist) t)
2215 (when (eq arg '&rest)
2216 ;; check, if &rest is followed by a var ...
2217 (when (or (null lambda-list)
2218 (memq (car lambda-list) lambda-list-keywords))
2219 (error "Error in lambda-list:~%~
2220 After &REST, a DEFMETHOD lambda-list ~
2221 must be followed by at least one variable.")))
2223 (cons arg lambda-list)
2227 ;; After a lambda-list keyword there can be no specializers.
2228 (multiple-value-bind (parameters lambda-list)
2229 (parse-specialized-lambda-list (cdr arglist) t)
2230 (values (cons (if (listp arg) (car arg) arg) parameters)
2231 (cons arg lambda-list)
2235 (multiple-value-bind (parameters lambda-list specializers required)
2236 (parse-specialized-lambda-list (cdr arglist))
2237 (values (cons (if (listp arg) (car arg) arg) parameters)
2238 (cons (if (listp arg) (car arg) arg) lambda-list)
2239 (cons (if (listp arg) (cadr arg) t) specializers)
2240 (cons (if (listp arg) (car arg) arg) required)))))))
2242 (setq *boot-state* 'early)
2244 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2245 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2246 ;;; walker stuff was only used for implementing stuff like that; maybe
2247 ;;; it's not needed any more? Hunt down what it was used for and see.
2249 (defmacro with-slots (slots instance &body body)
2250 (let ((in (gensym)))
2251 `(let ((,in ,instance))
2252 (declare (ignorable ,in))
2253 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2256 (and (symbolp instance)
2257 `((declare (%variable-rebinding ,in ,instance)))))
2259 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2261 (if (symbolp slot-entry)
2265 (if (symbolp slot-entry)
2267 (cadr slot-entry))))
2269 (slot-value ,in ',slot-name))))
2273 (defmacro with-accessors (slots instance &body body)
2274 (let ((in (gensym)))
2275 `(let ((,in ,instance))
2276 (declare (ignorable ,in))
2277 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2280 (and (symbolp instance)
2281 `((declare (%variable-rebinding ,in ,instance)))))
2283 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2284 (let ((var-name (car slot-entry))
2285 (accessor-name (cadr slot-entry)))
2286 `(,var-name (,accessor-name ,in))))