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 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: SB-KERNEL::PCL-CHECK-WRAPPER-VALIDITY-HOOK shouldn't be a
72 ;;; separate function. Instead, we should define a simple placeholder
73 ;;; version of SB-PCL:CHECK-WRAPPER-VALIDITY where
74 ;;; SB-KERNEL::PCL-CHECK-WRAPPER-VALIDITY is defined now, then just
75 ;;; let the later real PCL DEFUN of SB-PCL:CHECK-WRAPPER-VALIDITY
77 (setf (symbol-function 'sb-kernel::pcl-check-wrapper-validity-hook)
78 #'check-wrapper-validity)
80 (declaim (notinline make-a-method
82 ensure-generic-function-using-class
87 (defvar *early-functions*
88 '((make-a-method early-make-a-method
90 (add-named-method early-add-named-method
91 real-add-named-method)
94 ;;; For each of the early functions, arrange to have it point to its early
95 ;;; definition. Do this in a way that makes sure that if we redefine one
96 ;;; of the early definitions the redefinition will take effect. This makes
97 ;;; development easier.
99 ;;; The function which generates the redirection closure is pulled out into
100 ;;; a separate piece of code because of a bug in ExCL which causes this not
101 ;;; to work if it is inlined.
102 ;;; FIXME: We no longer need to worry about ExCL now, so we could unscrew this.
103 (eval-when (:load-toplevel :execute)
105 (defun redirect-early-function-internal (real early)
106 (setf (gdefinition real)
108 #'(lambda (&rest args)
109 (apply (the function (symbol-function early)) args))
112 (dolist (fns *early-functions*)
113 (let ((name (car fns))
114 (early-name (cadr fns)))
115 (redirect-early-function-internal name early-name)))
119 ;;; *GENERIC-FUNCTION-FIXUPS* is used by fix-early-generic-functions to
120 ;;; convert the few functions in the bootstrap which are supposed to be
121 ;;; generic functions but can't be early on.
122 (defvar *generic-function-fixups*
124 ((generic-function method) ;lambda-list
125 (standard-generic-function method) ;specializers
126 real-add-method)) ;method-function
128 ((generic-function method)
129 (standard-generic-function method)
132 ((generic-function qualifiers specializers &optional (errorp t))
133 (standard-generic-function t t)
135 (ensure-generic-function-using-class
136 ((generic-function function-name
137 &key generic-function-class environment
140 real-ensure-gf-using-class--generic-function)
141 ((generic-function function-name
142 &key generic-function-class environment
145 real-ensure-gf-using-class--null))
147 ((proto-generic-function proto-method lambda-expression environment)
148 (standard-generic-function standard-method t t)
149 real-make-method-lambda))
150 (make-method-initargs-form
151 ((proto-generic-function proto-method
153 lambda-list environment)
154 (standard-generic-function standard-method t t t)
155 real-make-method-initargs-form))
156 (compute-effective-method
157 ((generic-function combin applicable-methods)
158 (generic-function standard-method-combination t)
159 standard-compute-effective-method))))
161 (defmacro defgeneric (function-name lambda-list &body options)
162 (expand-defgeneric function-name lambda-list options))
164 (defun expand-defgeneric (function-name lambda-list options)
165 (when (listp function-name)
166 (do-standard-defsetf-1 (sb-int:function-name-block-name function-name)))
169 (flet ((duplicate-option (name)
170 (error 'sb-kernel:simple-program-error
171 :format-control "The option ~S appears more than once."
172 :format-arguments (list name)))
173 (expand-method-definition (qab) ; QAB = qualifiers, arglist, body
174 (let* ((arglist-pos (position-if #'listp qab))
175 (arglist (elt qab arglist-pos))
176 (qualifiers (subseq qab 0 arglist-pos))
177 (body (nthcdr (1+ arglist-pos) qab)))
178 (when (not (equal (cadr (getf initargs :method-combination))
180 (error "bad method specification in DEFGENERIC ~A~%~
181 -- qualifier mismatch for lambda list ~A"
182 function-name arglist))
183 `(defmethod ,function-name ,@qualifiers ,arglist ,@body))))
184 (macrolet ((initarg (key) `(getf initargs ,key)))
185 (dolist (option options)
186 (let ((car-option (car option)))
189 (push (cdr option) (initarg :declarations)))
190 ((:argument-precedence-order :method-combination)
191 (if (initarg car-option)
192 (duplicate-option car-option)
193 (setf (initarg car-option)
195 ((:documentation :generic-function-class :method-class)
196 (unless (sb-int:proper-list-of-length-p option 2)
197 (error "bad list length for ~S" option))
198 (if (initarg car-option)
199 (duplicate-option car-option)
200 (setf (initarg car-option) `',(cadr option))))
202 (push (cdr option) methods))
204 ;; ANSI requires that unsupported things must get a
206 (error 'sb-kernel:simple-program-error
207 :format-control "unsupported option ~S"
208 :format-arguments (list option))))))
210 (when (initarg :declarations)
211 (setf (initarg :declarations)
212 `',(initarg :declarations))))
214 (eval-when (:compile-toplevel :load-toplevel :execute)
215 (compile-or-load-defgeneric ',function-name))
216 ,(make-top-level-form
217 `(defgeneric ,function-name)
219 `(load-defgeneric ',function-name ',lambda-list ,@initargs))
220 ,@(mapcar #'expand-method-definition methods)
221 `,(function ,function-name)))))
223 (defun compile-or-load-defgeneric (function-name)
224 (sb-kernel:proclaim-as-function-name function-name)
225 (sb-kernel:note-name-defined function-name :function)
226 (unless (eq (sb-int:info :function :where-from function-name) :declared)
227 (setf (sb-int:info :function :where-from function-name) :defined)
228 (setf (sb-int:info :function :type function-name)
229 (sb-kernel:specifier-type 'function))))
231 (defun load-defgeneric (function-name lambda-list &rest initargs)
232 (when (listp function-name)
233 (do-standard-defsetf-1 (cadr function-name)))
234 (when (fboundp function-name)
235 (sb-kernel::style-warn "redefining ~S in DEFGENERIC" function-name))
236 (apply #'ensure-generic-function
238 :lambda-list lambda-list
239 :definition-source `((defgeneric ,function-name)
243 (defmacro defmethod (&rest args &environment env)
244 (declare (arglist name
246 specialized-lambda-list
248 (multiple-value-bind (name qualifiers lambda-list body)
249 (parse-defmethod args)
250 (multiple-value-bind (proto-gf proto-method)
251 (prototypes-for-make-method-lambda name)
252 (expand-defmethod name
260 (defun prototypes-for-make-method-lambda (name)
261 (if (not (eq *boot-state* 'complete))
263 (let ((gf? (and (gboundp name)
264 (gdefinition name))))
266 (not (generic-function-p gf?)))
267 (values (class-prototype (find-class 'standard-generic-function))
268 (class-prototype (find-class 'standard-method)))
270 (class-prototype (or (generic-function-method-class gf?)
271 (find-class 'standard-method))))))))
273 ;;; takes a name which is either a generic function name or a list specifying
274 ;;; a setf generic function (like: (SETF <generic-function-name>)). Returns
275 ;;; the prototype instance of the method-class for that generic function.
277 ;;; If there is no generic function by that name, this returns the default
278 ;;; value, the prototype instance of the class STANDARD-METHOD. This default
279 ;;; value is also returned if the spec names an ordinary function or even a
280 ;;; macro. In effect, this leaves the signalling of the appropriate error
283 ;;; Note: During bootstrapping, this function is allowed to return NIL.
284 (defun method-prototype-for-gf (name)
285 (let ((gf? (and (gboundp name)
286 (gdefinition name))))
287 (cond ((neq *boot-state* 'complete) nil)
289 (not (generic-function-p gf?))) ; Someone else MIGHT
290 ; error at load time.
291 (class-prototype (find-class 'standard-method)))
293 (class-prototype (or (generic-function-method-class gf?)
294 (find-class 'standard-method)))))))
296 (defvar *optimize-asv-funcall-p* nil)
297 (defvar *asv-readers*)
298 (defvar *asv-writers*)
299 (defvar *asv-boundps*)
301 (defun expand-defmethod (name
309 (do-standard-defsetf-1 (cadr name)))
310 (let ((*make-instance-function-keys* nil)
311 (*optimize-asv-funcall-p* t)
312 (*asv-readers* nil) (*asv-writers* nil) (*asv-boundps* nil))
313 (declare (special *make-instance-function-keys*))
314 (multiple-value-bind (method-lambda unspecialized-lambda-list specializers)
315 (add-method-declarations name qualifiers lambda-list body env)
316 (multiple-value-bind (method-function-lambda initargs)
317 (make-method-lambda proto-gf proto-method method-lambda env)
318 (let ((initargs-form (make-method-initargs-form proto-gf
320 method-function-lambda
324 ;; Note: We could DECLAIM the type of the generic
325 ;; function here, since ANSI specifies that we create it
326 ;; if it does not exist. However, I chose not to, because
327 ;; I think it's more useful to support a style of
328 ;; programming where every generic function has an
329 ;; explicit DEFGENERIC and any typos in DEFMETHODs are
330 ;; warned about. Otherwise
331 ;; (DEFGENERIC FOO-BAR-BLETCH ((X T)))
332 ;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
333 ;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
334 ;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
335 ;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
336 ;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
337 ;; compiles without raising an error and runs without
338 ;; raising an error (since SIMPLE-VECTOR cases fall
339 ;; through to VECTOR) but still doesn't do what was
340 ;; intended. I hate that kind of bug (code which silently
341 ;; gives the wrong answer), so we don't do a DECLAIM
342 ;; here. -- WHN 20000229
343 ,@(when *make-instance-function-keys*
344 `((get-make-instance-functions
345 ',*make-instance-function-keys*)))
346 ,@(when (or *asv-readers* *asv-writers* *asv-boundps*)
347 `((initialize-internal-slot-gfs*
348 ',*asv-readers* ',*asv-writers* ',*asv-boundps*)))
349 ,(make-defmethod-form name qualifiers specializers
350 unspecialized-lambda-list
352 (class-name (class-of proto-method))
355 (getf (getf initargs ':plist)
356 ':pv-table-symbol))))))))
358 (defun interned-symbol-p (x)
359 (and (symbolp x) (symbol-package x)))
361 (defun make-defmethod-form (name qualifiers specializers
362 unspecialized-lambda-list method-class-name
363 initargs-form &optional pv-table-symbol)
366 (if (and (interned-symbol-p (sb-int:function-name-block-name name))
367 (every #'interned-symbol-p qualifiers)
370 (and (eq (car s) 'eql)
372 (let ((sv (eval (cadr s))))
373 (or (interned-symbol-p sv)
376 (standard-char-p sv)))))
377 (interned-symbol-p s)))
379 (consp initargs-form)
380 (eq (car initargs-form) 'list*)
381 (memq (cadr initargs-form) '(:function :fast-function))
382 (consp (setq fn (caddr initargs-form)))
383 (eq (car fn) 'function)
384 (consp (setq fn-lambda (cadr fn)))
385 (eq (car fn-lambda) 'lambda))
386 (let* ((specls (mapcar (lambda (specl)
388 `(,(car specl) ,(eval (cadr specl)))
391 (mname `(,(if (eq (cadr initargs-form) ':function)
392 'method 'fast-method)
393 ,name ,@qualifiers ,specls))
394 (mname-sym (intern (let ((*print-pretty* nil))
395 (format nil "~S" mname)))))
396 `(eval-when ,*defmethod-times*
397 (defun ,mname-sym ,(cadr fn-lambda)
399 ,(make-defmethod-form-internal
400 name qualifiers `',specls
401 unspecialized-lambda-list method-class-name
402 `(list* ,(cadr initargs-form)
404 ,@(cdddr initargs-form))
407 `(defmethod ,name ,@qualifiers ,specializers)
409 (make-defmethod-form-internal
411 `(list ,@(mapcar #'(lambda (specializer)
412 (if (consp specializer)
413 ``(,',(car specializer)
414 ,,(cadr specializer))
417 unspecialized-lambda-list method-class-name
421 (defun make-defmethod-form-internal
422 (name qualifiers specializers-form unspecialized-lambda-list
423 method-class-name initargs-form &optional pv-table-symbol)
429 ',unspecialized-lambda-list
431 ;; Paper over a bug in KCL by passing the cache-symbol here in addition to
432 ;; in the list. FIXME: We should no longer need to do this.
435 (defmacro make-method-function (method-lambda &environment env)
436 (make-method-function-internal method-lambda env))
438 (defun make-method-function-internal (method-lambda &optional env)
439 (multiple-value-bind (proto-gf proto-method)
440 (prototypes-for-make-method-lambda nil)
441 (multiple-value-bind (method-function-lambda initargs)
442 (make-method-lambda proto-gf proto-method method-lambda env)
443 (make-method-initargs-form proto-gf
445 method-function-lambda
449 (defun add-method-declarations (name qualifiers lambda-list body env)
450 (multiple-value-bind (parameters unspecialized-lambda-list specializers)
451 (parse-specialized-lambda-list lambda-list)
452 (declare (ignore parameters))
453 (multiple-value-bind (documentation declarations real-body)
454 (extract-declarations body env)
455 (values `(lambda ,unspecialized-lambda-list
456 ,@(when documentation `(,documentation))
457 (declare (method-name ,(list name qualifiers specializers)))
458 (declare (method-lambda-list ,@lambda-list))
461 unspecialized-lambda-list specializers))))
463 (defun real-make-method-initargs-form (proto-gf proto-method
464 method-lambda initargs env)
465 (declare (ignore proto-gf proto-method))
466 (unless (and (consp method-lambda) (eq (car method-lambda) 'lambda))
467 (error "The METHOD-LAMBDA argument to MAKE-METHOD-FUNCTION, ~S,~
468 is not a lambda form."
470 (make-method-initargs-form-internal method-lambda initargs env))
472 (unless (fboundp 'make-method-initargs-form)
473 (setf (gdefinition 'make-method-initargs-form)
474 (symbol-function 'real-make-method-initargs-form)))
476 (defun real-make-method-lambda (proto-gf proto-method method-lambda env)
477 (declare (ignore proto-gf proto-method))
478 (make-method-lambda-internal method-lambda env))
480 (defun make-method-lambda-internal (method-lambda &optional env)
481 (unless (and (consp method-lambda) (eq (car method-lambda) 'lambda))
482 (error "The METHOD-LAMBDA argument to MAKE-METHOD-LAMBDA, ~S,~
483 is not a lambda form."
485 (multiple-value-bind (documentation declarations real-body)
486 (extract-declarations (cddr method-lambda) env)
487 (let* ((name-decl (get-declaration 'method-name declarations))
488 (sll-decl (get-declaration 'method-lambda-list declarations))
489 (method-name (when (consp name-decl) (car name-decl)))
490 (generic-function-name (when method-name (car method-name)))
491 (specialized-lambda-list (or sll-decl (cadr method-lambda))))
492 (multiple-value-bind (parameters lambda-list specializers)
493 (parse-specialized-lambda-list specialized-lambda-list)
494 (let* ((required-parameters
495 (mapcar #'(lambda (r s) (declare (ignore s)) r)
498 (slots (mapcar #'list required-parameters))
500 (parameters-to-reference
501 (make-parameter-references specialized-lambda-list
508 ;; FIXME: Are these (DECLARE (SB-PCL::CLASS FOO BAR))
509 ;; declarations used for anything any more?
511 (mapcar (lambda (a s) (and (symbolp s)
516 ;; These TYPE declarations weren't in the original
517 ;; PCL code, but Python likes them a lot. (We're
518 ;; telling the compiler about our knowledge of
519 ;; specialized argument types so that it can avoid
520 ;; run-time type overhead, which can be a big win
522 ,@(mapcar (lambda (a s)
523 (cond ((and (consp s)
525 ;; KLUDGE: ANSI, in its wisdom, says
526 ;; that EQL-SPECIALIZER-FORMs in EQL
527 ;; specializers are evaluated at
528 ;; DEFMETHOD expansion time. Thus,
529 ;; although one might think that in
530 ;; (DEFMETHOD FOO ((X PACKAGE)
533 ;; the PACKAGE and (EQL 12) forms are
534 ;; both parallel type names, they're
535 ;; not, as is made clear when you do
536 ;; (DEFMETHOD FOO ((X PACKAGE)
539 ;; where Y needs to be a symbol
540 ;; named "BAR", not some cons made by
541 ;; (CONS 'QUOTE 'BAR). I.e. when
542 ;; the EQL-SPECIALIZER-FORM is (EQL 'X),
543 ;; it requires an argument to be of
544 ;; type (EQL X). It'd be easy to transform
545 ;; one to the other, but it'd be somewhat
546 ;; messier to do so while ensuring that
547 ;; the EQL-SPECIALIZER-FORM is only
548 ;; EVAL'd once. (The new code wouldn't
549 ;; be messy, but it'd require a big
550 ;; transformation of the old code.)
551 ;; So instead we punt. -- WHN 20000610
553 ((not (eq *boot-state* 'complete))
554 ;; KLUDGE: PCL, in its wisdom,
555 ;; sometimes calls methods with
556 ;; types which don't match their
557 ;; specializers. (Specifically, it calls
558 ;; ENSURE-CLASS-USING-CLASS (T NULL)
559 ;; with a non-NULL second argument.)
560 ;; Hopefully it only does this kind
561 ;; of weirdness when bootstrapping..
565 ;; Otherwise, we can make Python
571 ;; Remove the documentation string and insert the
572 ;; appropriate class declarations. The documentation
573 ;; string is removed to make it easy for us to insert
574 ;; new declarations later, they will just go after the
575 ;; cadr of the method lambda. The class declarations
576 ;; are inserted to communicate the class of the method's
577 ;; arguments to the code walk.
578 `(lambda ,lambda-list
581 (declare (ignorable ,@parameters-to-reference))
583 ;; FIXME: should become FUNCTION-NAME-BLOCK-NAME
584 (block ,(if (listp generic-function-name)
585 (cadr generic-function-name)
586 generic-function-name)
588 (constant-value-p (and (null (cdr real-body))
589 (constantp (car real-body))))
590 (constant-value (and constant-value-p
591 (eval (car real-body))))
592 ;; FIXME: This can become a bare AND (no IF), just like
593 ;; the expression for CONSTANT-VALUE just above.
594 (plist (if (and constant-value-p
595 (or (typep constant-value
596 '(or number character))
597 (and (symbolp constant-value)
598 (symbol-package constant-value))))
599 (list :constant-value constant-value)
601 (applyp (dolist (p lambda-list nil)
602 (cond ((memq p '(&optional &rest &key))
607 (walked-lambda call-next-method-p closurep next-method-p-p)
608 (walk-method-lambda method-lambda
614 (ignore walked-declarations walked-lambda-body)
615 (extract-declarations (cddr walked-lambda))
616 (declare (ignore ignore))
617 (when (or next-method-p-p call-next-method-p)
618 (setq plist (list* :needs-next-methods-p 't plist)))
619 (when (some #'cdr slots)
620 (multiple-value-bind (slot-name-lists call-list)
621 (slot-name-lists-from-slots slots calls)
622 (let ((pv-table-symbol (make-symbol "pv-table")))
624 `(,@(when slot-name-lists
625 `(:slot-name-lists ,slot-name-lists))
627 `(:call-list ,call-list))
628 :pv-table-symbol ,pv-table-symbol
630 (setq walked-lambda-body
631 `((pv-binding (,required-parameters
634 ,@walked-lambda-body))))))
635 (when (and (memq '&key lambda-list)
636 (not (memq '&allow-other-keys lambda-list)))
637 (let ((aux (memq '&aux lambda-list)))
638 (setq lambda-list (nconc (ldiff lambda-list aux)
639 (list '&allow-other-keys)
641 (values `(lambda (.method-args. .next-methods.)
642 (simple-lexical-method-functions
643 (,lambda-list .method-args. .next-methods.
646 :next-method-p-p ,next-method-p-p
649 ,@walked-declarations
650 ,@walked-lambda-body))
653 ,@(when documentation
654 `(:documentation ,documentation)))))))))))
656 (unless (fboundp 'make-method-lambda)
657 (setf (gdefinition 'make-method-lambda)
658 (symbol-function 'real-make-method-lambda)))
660 (defmacro simple-lexical-method-functions ((lambda-list
666 ,method-args ,next-methods
667 (bind-simple-lexical-method-macros (,method-args ,next-methods)
668 (bind-lexical-method-functions (,@lmf-options)
669 (bind-args (,lambda-list ,method-args)
672 (defmacro fast-lexical-method-functions ((lambda-list
678 `(bind-fast-lexical-method-macros (,args ,rest-arg ,next-method-call)
679 (bind-lexical-method-functions (,@lmf-options)
680 (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg)
683 (defmacro bind-simple-lexical-method-macros ((method-args next-methods)
685 `(macrolet ((call-next-method-bind (&body body)
686 `(let ((.next-method. (car ,',next-methods))
687 (,',next-methods (cdr ,',next-methods)))
688 .next-method. ,',next-methods
690 (call-next-method-body (cnm-args)
692 (funcall (if (std-instance-p .next-method.)
693 (method-function .next-method.)
694 .next-method.) ; for early methods
695 (or ,cnm-args ,',method-args)
697 (error "no next method")))
698 (next-method-p-body ()
699 `(not (null .next-method.))))
702 (defstruct method-call
703 (function #'identity :type function)
706 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
708 (defmacro invoke-method-call1 (function args cm-args)
709 `(let ((.function. ,function)
711 (.cm-args. ,cm-args))
712 (if (and .cm-args. (null (cdr .cm-args.)))
713 (funcall .function. .args. (car .cm-args.))
714 (apply .function. .args. .cm-args.))))
716 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
717 `(invoke-method-call1 (method-call-function ,method-call)
719 `(list* ,@required-args+rest-arg)
720 `(list ,@required-args+rest-arg))
721 (method-call-call-method-args ,method-call)))
723 (defstruct fast-method-call
724 (function #'identity :type function)
729 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
731 (defmacro fmc-funcall (fn pv-cell next-method-call &rest args)
732 `(funcall ,fn ,pv-cell ,next-method-call ,@args))
734 (defmacro invoke-fast-method-call (method-call &rest required-args+rest-arg)
735 `(fmc-funcall (fast-method-call-function ,method-call)
736 (fast-method-call-pv-cell ,method-call)
737 (fast-method-call-next-method-call ,method-call)
738 ,@required-args+rest-arg))
740 (defstruct fast-instance-boundp
741 (index 0 :type fixnum))
743 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
745 (eval-when (:compile-toplevel :load-toplevel :execute)
747 (defvar *allow-emf-call-tracing-p* nil)
748 (defvar *enable-emf-call-tracing-p* #-testing nil #+testing t)
752 ;;;; effective method functions
754 (defvar *emf-call-trace-size* 200)
755 (defvar *emf-call-trace* nil)
756 (defvar *emf-call-trace-index* 0)
758 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
759 ;;; without explanation. It appears to be intended for debugging, so
760 ;;; it might be useful someday, so I haven't deleted it.
761 ;;; But it isn't documented and isn't used for anything now, so
762 ;;; I've conditionalized it out of the base system. -- WHN 19991213
764 (defun show-emf-call-trace ()
765 (when *emf-call-trace*
766 (let ((j *emf-call-trace-index*)
767 (*enable-emf-call-tracing-p* nil))
768 (format t "~&(The oldest entries are printed first)~%")
769 (dotimes-fixnum (i *emf-call-trace-size*)
770 (let ((ct (aref *emf-call-trace* j)))
771 (when ct (print ct)))
773 (when (= j *emf-call-trace-size*)
776 (defun trace-emf-call-internal (emf format args)
777 (unless *emf-call-trace*
778 (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
779 (setf (aref *emf-call-trace* *emf-call-trace-index*)
780 (list* emf format args))
781 (incf *emf-call-trace-index*)
782 (when (= *emf-call-trace-index* *emf-call-trace-size*)
783 (setq *emf-call-trace-index* 0)))
785 (defmacro trace-emf-call (emf format args)
786 (when *allow-emf-call-tracing-p*
787 `(when *enable-emf-call-tracing-p*
788 (trace-emf-call-internal ,emf ,format ,args))))
790 (defmacro invoke-effective-method-function-fast
791 (emf restp &rest required-args+rest-arg)
793 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
794 (invoke-fast-method-call ,emf ,@required-args+rest-arg)))
796 (defmacro invoke-effective-method-function (emf restp
797 &rest required-args+rest-arg)
798 (unless (constantp restp)
799 (error "The RESTP argument is not constant."))
800 (setq restp (eval restp))
802 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
803 (cond ((typep ,emf 'fast-method-call)
804 (invoke-fast-method-call ,emf ,@required-args+rest-arg))
805 ,@(when (and (null restp) (= 1 (length required-args+rest-arg)))
806 `(((typep ,emf 'fixnum)
807 (let* ((.slots. (get-slots-or-nil
808 ,(car required-args+rest-arg)))
809 (value (when .slots. (%instance-ref .slots. ,emf))))
810 (if (eq value ',*slot-unbound*)
811 (slot-unbound-internal ,(car required-args+rest-arg)
814 ,@(when (and (null restp) (= 2 (length required-args+rest-arg)))
815 `(((typep ,emf 'fixnum)
816 (let ((.new-value. ,(car required-args+rest-arg))
817 (.slots. (get-slots-or-nil
818 ,(car required-args+rest-arg))))
819 (when .slots. ; just to avoid compiler warnings
820 (setf (%instance-ref .slots. ,emf) .new-value.))))))
822 ,@(when (and (null restp) (= 1 (length required-args+rest-arg)))
823 `(((typep ,emf 'fast-instance-boundp)
824 (let ((.slots. (get-slots-or-nil
825 ,(car required-args+rest-arg))))
827 (not (eq (%instance-ref
828 .slots. (fast-instance-boundp-index ,emf))
829 ',*slot-unbound*)))))))
834 (invoke-method-call ,emf ,restp ,@required-args+rest-arg))
837 `(apply (the function ,emf) ,@required-args+rest-arg)
838 `(funcall (the function ,emf)
839 ,@required-args+rest-arg))))))))
841 (defun invoke-emf (emf args)
842 (trace-emf-call emf t args)
845 (let* ((arg-info (fast-method-call-arg-info emf))
846 (restp (cdr arg-info))
847 (nreq (car arg-info)))
849 (let* ((rest-args (nthcdr nreq args))
850 (req-args (ldiff args rest-args)))
851 (apply (fast-method-call-function emf)
852 (fast-method-call-pv-cell emf)
853 (fast-method-call-next-method-call emf)
854 (nconc req-args (list rest-args))))
857 (invoke-fast-method-call emf)
858 (error "wrong number of args")))
861 (invoke-fast-method-call emf (car args))
862 (error "wrong number of args")))
865 (invoke-fast-method-call emf (car args) (cadr args))
866 (error "wrong number of args")))
868 (apply (fast-method-call-function emf)
869 (fast-method-call-pv-cell emf)
870 (fast-method-call-next-method-call emf)
873 (apply (method-call-function emf)
875 (method-call-call-method-args emf)))
877 (cond ((null args) (error "1 or 2 args were expected."))
879 (let ((value (%instance-ref (get-slots (car args)) emf)))
880 (if (eq value *slot-unbound*)
881 (slot-unbound-internal (car args) emf)
884 (setf (%instance-ref (get-slots (cadr args)) emf)
886 (t (error "1 or 2 args were expected."))))
887 (fast-instance-boundp
888 (if (or (null args) (cdr args))
889 (error "1 arg was expected.")
890 (not (eq (%instance-ref (get-slots (car args))
891 (fast-instance-boundp-index emf))
896 ;; KLUDGE: A comment from the original PCL said "This can be improved alot."
897 (defun gf-make-function-from-emf (gf emf)
899 (fast-method-call (let* ((arg-info (gf-arg-info gf))
900 (nreq (arg-info-number-required arg-info))
901 (restp (arg-info-applyp arg-info)))
902 #'(lambda (&rest args)
903 (trace-emf-call emf t args)
904 (apply (fast-method-call-function emf)
905 (fast-method-call-pv-cell emf)
906 (fast-method-call-next-method-call emf)
908 (let* ((rest-args (nthcdr nreq args))
909 (req-args (ldiff args
911 (nconc req-args rest-args))
913 (method-call #'(lambda (&rest args)
914 (trace-emf-call emf t args)
915 (apply (method-call-function emf)
917 (method-call-call-method-args emf))))
920 (defmacro bind-fast-lexical-method-macros ((args rest-arg next-method-call)
922 `(macrolet ((call-next-method-bind (&body body)
924 (call-next-method-body (cnm-args)
925 `(if ,',next-method-call
926 ,(if (and (null ',rest-arg)
928 (eq (car cnm-args) 'list))
929 `(invoke-effective-method-function
930 ,',next-method-call nil
932 (let ((call `(invoke-effective-method-function
934 ,',(not (null rest-arg))
936 ,@',(when rest-arg `(,rest-arg)))))
938 (bind-args ((,@',args
944 (error "no next method")))
945 (next-method-p-body ()
946 `(not (null ,',next-method-call))))
949 (defmacro bind-lexical-method-functions
950 ((&key call-next-method-p next-method-p-p closurep applyp)
952 (cond ((and (null call-next-method-p) (null next-method-p-p)
956 ((and (null closurep)
958 ;; OK to use MACROLET, and all args are mandatory
959 ;; (else APPLYP would be true).
960 `(call-next-method-bind
961 (macrolet ((call-next-method (&rest cnm-args)
962 `(call-next-method-body ,(when cnm-args
963 `(list ,@cnm-args))))
965 `(next-method-p-body)))
968 `(call-next-method-bind
969 (flet (,@(and call-next-method-p
970 '((call-next-method (&rest cnm-args)
971 (call-next-method-body cnm-args))))
972 ,@(and next-method-p-p
974 (next-method-p-body)))))
977 (defmacro bind-args ((lambda-list args) &body body)
978 (let ((args-tail '.args-tail.)
981 (flet ((process-var (var)
982 (if (memq var lambda-list-keywords)
985 (&optional (setq state 'optional))
986 (&key (setq state 'key))
988 (&rest (setq state 'rest))
989 (&aux (setq state 'aux))
991 (error "encountered the non-standard lambda list keyword ~S"
995 (required `((,var (pop ,args-tail))))
996 (optional (cond ((not (consp var))
997 `((,var (when ,args-tail
1000 `((,(car var) (if ,args-tail
1004 `((,(caddr var) ,args-tail)
1005 (,(car var) (if ,args-tail
1008 (rest `((,var ,args-tail)))
1009 (key (cond ((not (consp var))
1010 `((,var (get-key-arg ,(make-keyword var)
1013 (multiple-value-bind (keyword variable)
1014 (if (consp (car var))
1017 (values (make-keyword (car var))
1019 `((,key (get-key-arg1 ,keyword ,args-tail))
1020 (,variable (if (consp ,key)
1024 (multiple-value-bind (keyword variable)
1025 (if (consp (car var))
1028 (values (make-keyword (car var))
1030 `((,key (get-key-arg1 ,keyword ,args-tail))
1032 (,variable (if (consp ,key)
1036 (let ((bindings (mapcan #'process-var lambda-list)))
1037 `(let* ((,args-tail ,args)
1039 (declare (ignorable ,args-tail))
1042 (defun get-key-arg (keyword list)
1043 (loop (when (atom list) (return nil))
1044 (when (eq (car list) keyword) (return (cadr list)))
1045 (setq list (cddr list))))
1047 (defun get-key-arg1 (keyword list)
1048 (loop (when (atom list) (return nil))
1049 (when (eq (car list) keyword) (return (cdr list)))
1050 (setq list (cddr list))))
1052 (defun walk-method-lambda (method-lambda required-parameters env slots calls)
1053 (let ((call-next-method-p nil) ; flag indicating that CALL-NEXT-METHOD
1054 ; should be in the method definition
1055 (closurep nil) ; flag indicating that #'CALL-NEXT-METHOD
1056 ; was seen in the body of a method
1057 (next-method-p-p nil)) ; flag indicating that NEXT-METHOD-P
1058 ; should be in the method definition
1059 (flet ((walk-function (form context env)
1060 (cond ((not (eq context ':eval)) form)
1061 ;; FIXME: Jumping to a conclusion from the way it's used
1062 ;; above, perhaps CONTEXT should be called SITUATION
1063 ;; (after the term used in the ANSI specification of
1064 ;; EVAL-WHEN) and given modern ANSI keyword values
1065 ;; like :LOAD-TOPLEVEL.
1066 ((not (listp form)) form)
1067 ((eq (car form) 'call-next-method)
1068 (setq call-next-method-p 't)
1070 ((eq (car form) 'next-method-p)
1071 (setq next-method-p-p 't)
1073 ((and (eq (car form) 'function)
1074 (cond ((eq (cadr form) 'call-next-method)
1075 (setq call-next-method-p 't)
1078 ((eq (cadr form) 'next-method-p)
1079 (setq next-method-p-p 't)
1083 (;; FIXME: should be MEMQ or FIND :TEST #'EQ
1084 (and (or (eq (car form) 'slot-value)
1085 (eq (car form) 'set-slot-value)
1086 (eq (car form) 'slot-boundp))
1087 (constantp (caddr form)))
1088 (let ((parameter (can-optimize-access form
1092 ;; (LET ((FUN (ECASE (CAR FORM) ..)))
1093 ;; (FUNCALL FUN SLOTS PARAMETER FORM))
1096 (optimize-slot-value slots parameter form))
1098 (optimize-set-slot-value slots parameter form))
1100 (optimize-slot-boundp slots parameter form)))))
1101 ((and (eq (car form) 'apply)
1103 (eq (car (cadr form)) 'function)
1104 (generic-function-name-p (cadr (cadr form))))
1105 (optimize-generic-function-call
1106 form required-parameters env slots calls))
1107 ((generic-function-name-p (car form))
1108 (optimize-generic-function-call
1109 form required-parameters env slots calls))
1110 ((and (eq (car form) 'asv-funcall)
1111 *optimize-asv-funcall-p*)
1113 (reader (push (third form) *asv-readers*))
1114 (writer (push (third form) *asv-writers*))
1115 (boundp (push (third form) *asv-boundps*)))
1116 `(,(second form) ,@(cddddr form)))
1119 (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1120 (values walked-lambda
1123 next-method-p-p)))))
1125 (defun generic-function-name-p (name)
1126 (and (sb-int:legal-function-name-p name)
1128 (if (eq *boot-state* 'complete)
1129 (standard-generic-function-p (gdefinition name))
1130 (funcallable-instance-p (gdefinition name)))))
1132 (defun make-parameter-references (specialized-lambda-list
1137 (flet ((ignoredp (symbol)
1138 (dolist (decl (cdar declarations))
1139 (when (and (eq (car decl) 'ignore)
1140 (memq symbol (cdr decl)))
1142 (gathering ((references (collecting)))
1143 (iterate ((s (list-elements specialized-lambda-list))
1144 (p (list-elements required-parameters)))
1146 (cond ((not (listp s)))
1148 (warn "In DEFMETHOD ~S, there is a~%~
1149 redundant IGNORE declaration for the parameter ~S."
1154 (gather (car s) references)))))))
1156 (defvar *method-function-plist* (make-hash-table :test 'eq))
1159 (defvar *mf1cp* nil)
1162 (defvar *mf2cp* nil)
1164 (defun method-function-plist (method-function)
1165 (unless (eq method-function *mf1*)
1166 (rotatef *mf1* *mf2*)
1167 (rotatef *mf1p* *mf2p*)
1168 (rotatef *mf1cp* *mf2cp*))
1169 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1170 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1171 (unless (eq method-function *mf1*)
1172 (setf *mf1* method-function
1174 *mf1p* (gethash method-function *method-function-plist*)))
1177 (defun #-setf SETF\ SB-PCL\ METHOD-FUNCTION-PLIST
1178 #+setf (setf method-function-plist)
1179 (val method-function)
1180 (unless (eq method-function *mf1*)
1181 (rotatef *mf1* *mf2*)
1182 (rotatef *mf1cp* *mf2cp*)
1183 (rotatef *mf1p* *mf2p*))
1184 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1185 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1186 (setf *mf1* method-function
1190 (defun method-function-get (method-function key &optional default)
1191 (getf (method-function-plist method-function) key default))
1193 (defun #-setf SETF\ SB-PCL\ METHOD-FUNCTION-GET
1194 #+setf (setf method-function-get)
1195 (val method-function key)
1196 (setf (getf (method-function-plist method-function) key) val))
1198 (defun method-function-pv-table (method-function)
1199 (method-function-get method-function :pv-table))
1201 (defun method-function-method (method-function)
1202 (method-function-get method-function :method))
1204 (defun method-function-needs-next-methods-p (method-function)
1205 (method-function-get method-function :needs-next-methods-p t))
1207 (defmacro method-function-closure-generator (method-function)
1208 `(method-function-get ,method-function 'closure-generator))
1210 (defun load-defmethod
1211 (class name quals specls ll initargs &optional pv-table-symbol)
1212 (when (listp name) (do-standard-defsetf-1 (cadr name)))
1213 (setq initargs (copy-tree initargs))
1214 (let ((method-spec (or (getf initargs ':method-spec)
1215 (make-method-spec name quals specls))))
1216 (setf (getf initargs ':method-spec) method-spec)
1217 (record-definition 'method method-spec)
1218 (load-defmethod-internal class name quals specls
1219 ll initargs pv-table-symbol)))
1221 (defun load-defmethod-internal
1222 (method-class gf-spec qualifiers specializers lambda-list
1223 initargs pv-table-symbol)
1224 (when (listp gf-spec) (do-standard-defsetf-1 (cadr gf-spec)))
1225 (when pv-table-symbol
1226 (setf (getf (getf initargs ':plist) :pv-table-symbol)
1228 ;; FIXME: It seems as though I should be able to get this to work.
1229 ;; But it keeps on screwing up PCL bootstrapping.
1231 (when (and (eq *boot-state* 'complete)
1233 (let* ((gf (symbol-function gf-spec))
1234 (method (and (generic-function-p gf)
1237 (mapcar #'find-class specializers)
1240 (sb-kernel::style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1241 gf-spec qualifiers specializers))))
1242 (let ((method (apply #'add-named-method
1243 gf-spec qualifiers specializers lambda-list
1244 :definition-source `((defmethod ,gf-spec
1249 (unless (or (eq method-class 'standard-method)
1250 (eq (find-class method-class nil) (class-of method)))
1251 ;; FIXME: should be STYLE-WARNING?
1252 (format *error-output*
1253 "~&At the time the method with qualifiers ~:S and~%~
1254 specializers ~:S on the generic function ~S~%~
1255 was compiled, the method-class for that generic function was~%~
1256 ~S. But, the method class is now ~S, this~%~
1257 may mean that this method was compiled improperly.~%"
1258 qualifiers specializers gf-spec
1259 method-class (class-name (class-of method))))
1262 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1263 `(method ,gf-spec ,@qualifiers ,unparsed-specializers))
1265 (defun initialize-method-function (initargs &optional return-function-p method)
1266 (let* ((mf (getf initargs ':function))
1267 (method-spec (getf initargs ':method-spec))
1268 (plist (getf initargs ':plist))
1269 (pv-table-symbol (getf plist ':pv-table-symbol))
1271 (mff (getf initargs ':fast-function)))
1272 (flet ((set-mf-property (p v)
1274 (setf (method-function-get mf p) v))
1276 (setf (method-function-get mff p) v))))
1279 (setq mf (set-function-name mf method-spec)))
1281 (let ((name `(,(or (get (car method-spec) 'fast-sym)
1282 (setf (get (car method-spec) 'fast-sym)
1283 ;; KLUDGE: If we're going to be
1284 ;; interning private symbols in our
1285 ;; a this way, it would be cleanest
1286 ;; to use a separate package
1287 ;; %PCL-PRIVATE or something, and
1288 ;; failing that, to use a special
1289 ;; symbol prefix denoting privateness.
1291 (intern (format nil "FAST-~A"
1294 ,@(cdr method-spec))))
1295 (set-function-name mff name)
1297 (set-mf-property :name name)))))
1299 (let ((snl (getf plist :slot-name-lists))
1300 (cl (getf plist :call-list)))
1302 (setq pv-table (intern-pv-table :slot-name-lists snl
1304 (when pv-table (set pv-table-symbol pv-table))
1305 (set-mf-property :pv-table pv-table)))
1306 (loop (when (null plist) (return nil))
1307 (set-mf-property (pop plist) (pop plist)))
1309 (set-mf-property :method method))
1310 (when return-function-p
1311 (or mf (method-function-from-fast-function mff)))))))
1313 (defun analyze-lambda-list (lambda-list)
1314 ;;(declare (values nrequired noptional keysp restp allow-other-keys-p
1315 ;; keywords keyword-parameters))
1316 (flet ((parse-keyword-argument (arg)
1318 (if (listp (car arg))
1320 (make-keyword (car arg)))
1321 (make-keyword arg))))
1326 (allow-other-keys-p nil)
1328 (keyword-parameters ())
1330 (dolist (x lambda-list)
1331 (if (memq x lambda-list-keywords)
1333 (&optional (setq state 'optional))
1334 (&key (setq keysp 't
1336 (&allow-other-keys (setq allow-other-keys-p 't))
1337 (&rest (setq restp 't
1341 (error "encountered the non-standard lambda list keyword ~S" x)))
1343 (required (incf nrequired))
1344 (optional (incf noptional))
1345 (key (push (parse-keyword-argument x) keywords)
1346 (push x keyword-parameters))
1348 (values nrequired noptional keysp restp allow-other-keys-p
1350 (reverse keyword-parameters)))))
1352 (defun keyword-spec-name (x)
1353 (let ((key (if (atom x) x (car x))))
1355 (intern (symbol-name key) *keyword-package*)
1358 (defun ftype-declaration-from-lambda-list (lambda-list name)
1359 (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1360 keywords keyword-parameters)
1361 (analyze-lambda-list lambda-list)
1362 (declare (ignore keyword-parameters))
1363 (let* ((old (sb-c::info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1364 (old-ftype (if (sb-c::function-type-p old) old nil))
1365 (old-restp (and old-ftype (sb-c::function-type-rest old-ftype)))
1366 (old-keys (and old-ftype
1367 (mapcar #'sb-c::key-info-name
1368 (sb-c::function-type-keywords old-ftype))))
1369 (old-keysp (and old-ftype (sb-c::function-type-keyp old-ftype)))
1370 (old-allowp (and old-ftype (sb-c::function-type-allowp old-ftype)))
1371 (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1372 `(function ,(append (make-list nrequired :initial-element 't)
1373 (when (plusp noptional)
1374 (append '(&optional)
1375 (make-list noptional :initial-element 't)))
1376 (when (or restp old-restp)
1378 (when (or keysp old-keysp)
1380 (mapcar #'(lambda (key)
1383 (when (or allow-other-keys-p old-allowp)
1384 '(&allow-other-keys)))))
1387 (defun defgeneric-declaration (spec lambda-list)
1389 (setq spec (get-setf-function-name (cadr spec))))
1390 `(ftype ,(ftype-declaration-from-lambda-list lambda-list spec) ,spec))
1392 ;;;; early generic function support
1394 (defvar *early-generic-functions* ())
1396 (defun ensure-generic-function (function-name
1400 (declare (ignore environment))
1401 (let ((existing (and (gboundp function-name)
1402 (gdefinition function-name))))
1404 (eq *boot-state* 'complete)
1405 (null (generic-function-p existing)))
1406 (generic-clobbers-function function-name)
1407 (apply #'ensure-generic-function-using-class
1408 existing function-name all-keys))))
1410 (defun generic-clobbers-function (function-name)
1411 (error 'sb-kernel:simple-program-error
1413 "~S already names an ordinary function or a macro."
1414 :format-arguments (list function-name)))
1416 (defvar *sgf-wrapper*
1417 (boot-make-wrapper (early-class-size 'standard-generic-function)
1418 'standard-generic-function))
1420 (defvar *sgf-slots-init*
1421 (mapcar #'(lambda (canonical-slot)
1422 (if (memq (getf canonical-slot :name) '(arg-info source))
1424 (let ((initfunction (getf canonical-slot :initfunction)))
1426 (funcall initfunction)
1428 (early-collect-inheritance 'standard-generic-function)))
1430 (defvar *sgf-method-class-index*
1431 (bootstrap-slot-index 'standard-generic-function 'method-class))
1433 (defun early-gf-p (x)
1434 (and (fsc-instance-p x)
1435 (eq (instance-ref (get-slots x) *sgf-method-class-index*)
1438 (defvar *sgf-methods-index*
1439 (bootstrap-slot-index 'standard-generic-function 'methods))
1441 (defmacro early-gf-methods (gf)
1442 `(instance-ref (get-slots ,gf) *sgf-methods-index*))
1444 (defvar *sgf-arg-info-index*
1445 (bootstrap-slot-index 'standard-generic-function 'arg-info))
1447 (defmacro early-gf-arg-info (gf)
1448 `(instance-ref (get-slots ,gf) *sgf-arg-info-index*))
1450 (defvar *sgf-dfun-state-index*
1451 (bootstrap-slot-index 'standard-generic-function 'dfun-state))
1453 (defstruct (arg-info
1455 (:constructor make-arg-info ()))
1456 (arg-info-lambda-list :no-lambda-list)
1459 arg-info-number-optional
1461 arg-info-keywords ;nil no keyword or rest allowed
1462 ;(k1 k2 ..) each method must accept these keyword arguments
1463 ;T must have &key or &rest
1465 gf-info-simple-accessor-type ; nil, reader, writer, boundp
1466 (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1468 gf-info-static-c-a-m-emf
1469 (gf-info-c-a-m-emf-std-p t)
1472 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1474 (defun arg-info-valid-p (arg-info)
1475 (not (null (arg-info-number-optional arg-info))))
1477 (defun arg-info-applyp (arg-info)
1478 (or (plusp (arg-info-number-optional arg-info))
1479 (arg-info-key/rest-p arg-info)))
1481 (defun arg-info-number-required (arg-info)
1482 (length (arg-info-metatypes arg-info)))
1484 (defun arg-info-nkeys (arg-info)
1485 (count-if #'(lambda (x) (neq x 't)) (arg-info-metatypes arg-info)))
1487 ;;; Keep pages clean by not setting if the value is already the same.
1488 (defmacro esetf (pos val)
1489 (let ((valsym (gensym "value")))
1490 `(let ((,valsym ,val))
1491 (unless (equal ,pos ,valsym)
1492 (setf ,pos ,valsym)))))
1494 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1495 argument-precedence-order)
1496 (let* ((arg-info (if (eq *boot-state* 'complete)
1498 (early-gf-arg-info gf)))
1499 (methods (if (eq *boot-state* 'complete)
1500 (generic-function-methods gf)
1501 (early-gf-methods gf)))
1502 (was-valid-p (integerp (arg-info-number-optional arg-info)))
1503 (first-p (and new-method (null (cdr methods)))))
1504 (when (and (not lambda-list-p) methods)
1505 (setq lambda-list (gf-lambda-list gf)))
1506 (when (or lambda-list-p
1508 (eq (arg-info-lambda-list arg-info) ':no-lambda-list)))
1509 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1510 (analyze-lambda-list lambda-list)
1511 (when (and methods (not first-p))
1512 (let ((gf-nreq (arg-info-number-required arg-info))
1513 (gf-nopt (arg-info-number-optional arg-info))
1514 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1515 (unless (and (= nreq gf-nreq)
1517 (eq (or keysp restp) gf-key/rest-p))
1518 (error "The lambda-list ~S is incompatible with ~
1519 existing methods of ~S."
1522 (esetf (arg-info-lambda-list arg-info) lambda-list))
1523 (when (or lambda-list-p argument-precedence-order
1524 (null (arg-info-precedence arg-info)))
1525 (esetf (arg-info-precedence arg-info)
1526 (compute-precedence lambda-list nreq
1527 argument-precedence-order)))
1528 (esetf (arg-info-metatypes arg-info) (make-list nreq))
1529 (esetf (arg-info-number-optional arg-info) nopt)
1530 (esetf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1531 (esetf (arg-info-keywords arg-info)
1533 (if allow-other-keys-p t keywords)
1534 (arg-info-key/rest-p arg-info)))))
1536 (check-method-arg-info gf arg-info new-method))
1537 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1540 (defun check-method-arg-info (gf arg-info method)
1541 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1542 (analyze-lambda-list (if (consp method)
1543 (early-method-lambda-list method)
1544 (method-lambda-list method)))
1545 (flet ((lose (string &rest args)
1547 "attempt to add the method ~S to the generic function ~S.~%~
1551 (apply #'format nil string args)))
1553 (if (> x y) "more" "fewer")))
1554 (let ((gf-nreq (arg-info-number-required arg-info))
1555 (gf-nopt (arg-info-number-optional arg-info))
1556 (gf-key/rest-p (arg-info-key/rest-p arg-info))
1557 (gf-keywords (arg-info-keywords arg-info)))
1558 (unless (= nreq gf-nreq)
1560 "the method has ~A required arguments than the generic function."
1561 (compare nreq gf-nreq)))
1562 (unless (= nopt gf-nopt)
1564 "the method has ~S optional arguments than the generic function."
1565 (compare nopt gf-nopt)))
1566 (unless (eq (or keysp restp) gf-key/rest-p)
1568 "The method and generic function differ in whether they accept~%~
1569 &REST or &KEY arguments."))
1570 (when (consp gf-keywords)
1571 (unless (or (and restp (not keysp))
1573 (every #'(lambda (k) (memq k keywords)) gf-keywords))
1574 (lose "the method does not accept each of the keyword arguments~%~
1578 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1579 (let* ((existing-p (and methods (cdr methods) new-method))
1580 (nreq (length (arg-info-metatypes arg-info)))
1581 (metatypes (if existing-p
1582 (arg-info-metatypes arg-info)
1584 (type (if existing-p
1585 (gf-info-simple-accessor-type arg-info)
1587 (when (arg-info-valid-p arg-info)
1588 (dolist (method (if new-method (list new-method) methods))
1589 (let* ((specializers (if (or (eq *boot-state* 'complete)
1590 (not (consp method)))
1591 (method-specializers method)
1592 (early-method-specializers method t)))
1593 (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1595 (early-method-class method)))
1596 (new-type (when (and class
1597 (or (not (eq *boot-state* 'complete))
1598 (eq (generic-function-method-combination gf)
1599 *standard-method-combination*)))
1600 (cond ((eq class *the-class-standard-reader-method*)
1602 ((eq class *the-class-standard-writer-method*)
1604 ((eq class *the-class-standard-boundp-method*)
1606 (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1607 (setq type (cond ((null type) new-type)
1608 ((eq type new-type) type)
1610 (esetf (arg-info-metatypes arg-info) metatypes)
1611 (esetf (gf-info-simple-accessor-type arg-info) type)))
1612 (when (or (not was-valid-p) first-p)
1613 (multiple-value-bind (c-a-m-emf std-p)
1616 (compute-applicable-methods-emf gf))
1617 (esetf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1618 (esetf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1619 (unless (gf-info-c-a-m-emf-std-p arg-info)
1620 (esetf (gf-info-simple-accessor-type arg-info) t))))
1622 (let ((name (if (eq *boot-state* 'complete)
1623 (generic-function-name gf)
1624 (early-gf-name gf))))
1625 (esetf (gf-precompute-dfun-and-emf-p arg-info)
1626 (let* ((sym (if (atom name) name (cadr name)))
1627 (pkg-list (cons *pcl-package*
1628 (package-use-list *pcl-package*))))
1629 (and sym (symbolp sym)
1630 (not (null (memq (symbol-package sym) pkg-list)))
1631 (not (find #\space (symbol-name sym))))))))
1632 (esetf (gf-info-fast-mf-p arg-info)
1633 (or (not (eq *boot-state* 'complete))
1634 (let* ((method-class (generic-function-method-class gf))
1635 (methods (compute-applicable-methods
1636 #'make-method-lambda
1637 (list gf (class-prototype method-class)
1639 (and methods (null (cdr methods))
1640 (let ((specls (method-specializers (car methods))))
1641 (and (classp (car specls))
1642 (eq 'standard-generic-function
1643 (class-name (car specls)))
1644 (classp (cadr specls))
1645 (eq 'standard-method
1646 (class-name (cadr specls)))))))))
1649 ;;; This is the early definition of ensure-generic-function-using-class.
1651 ;;; The static-slots field of the funcallable instances used as early generic
1652 ;;; functions is used to store the early methods and early discriminator code
1653 ;;; for the early generic function. The static slots field of the fins
1654 ;;; contains a list whose:
1655 ;;; CAR - a list of the early methods on this early gf
1656 ;;; CADR - the early discriminator code for this method
1657 (defun ensure-generic-function-using-class (existing spec &rest keys
1658 &key (lambda-list nil lambda-list-p)
1660 (declare (ignore keys))
1661 (cond ((and existing (early-gf-p existing))
1663 ((assoc spec *generic-function-fixups* :test #'equal)
1665 (make-early-gf spec lambda-list lambda-list-p existing)
1666 (error "The function ~S is not already defined." spec)))
1668 (error "~S should be on the list ~S."
1670 '*generic-function-fixups*))
1672 (pushnew spec *early-generic-functions* :test #'equal)
1673 (make-early-gf spec lambda-list lambda-list-p))))
1675 (defun make-early-gf (spec &optional lambda-list lambda-list-p function)
1676 (let ((fin (allocate-funcallable-instance *sgf-wrapper* *sgf-slots-init*)))
1677 (set-funcallable-instance-function
1680 (if (eq spec 'print-object)
1681 #'(sb-kernel:instance-lambda (instance stream)
1682 (print-unreadable-object (instance stream :identity t)
1683 (format stream "std-instance")))
1684 #'(sb-kernel:instance-lambda (&rest args)
1685 (declare (ignore args))
1686 (error "The function of the funcallable-instance ~S~
1687 has not been set." fin)))))
1688 (setf (gdefinition spec) fin)
1689 (bootstrap-set-slot 'standard-generic-function fin 'name spec)
1690 (bootstrap-set-slot 'standard-generic-function fin 'source *load-truename*)
1691 (set-function-name fin spec)
1692 (let ((arg-info (make-arg-info)))
1693 (setf (early-gf-arg-info fin) arg-info)
1695 (proclaim (defgeneric-declaration spec lambda-list))
1696 (set-arg-info fin :lambda-list lambda-list)))
1699 (defun set-dfun (gf &optional dfun cache info)
1701 (setf (cache-owner cache) gf))
1702 (let ((new-state (if (and dfun (or cache info))
1703 (list* dfun cache info)
1705 (if (eq *boot-state* 'complete)
1706 (setf (gf-dfun-state gf) new-state)
1707 (setf (instance-ref (get-slots gf) *sgf-dfun-state-index*) new-state)))
1710 (defun gf-dfun-cache (gf)
1711 (let ((state (if (eq *boot-state* 'complete)
1713 (instance-ref (get-slots gf) *sgf-dfun-state-index*))))
1716 (cons (cadr state)))))
1718 (defun gf-dfun-info (gf)
1719 (let ((state (if (eq *boot-state* 'complete)
1721 (instance-ref (get-slots gf) *sgf-dfun-state-index*))))
1724 (cons (cddr state)))))
1726 (defvar *sgf-name-index*
1727 (bootstrap-slot-index 'standard-generic-function 'name))
1729 (defun early-gf-name (gf)
1730 (instance-ref (get-slots gf) *sgf-name-index*))
1732 (defun gf-lambda-list (gf)
1733 (let ((arg-info (if (eq *boot-state* 'complete)
1735 (early-gf-arg-info gf))))
1736 (if (eq ':no-lambda-list (arg-info-lambda-list arg-info))
1737 (let ((methods (if (eq *boot-state* 'complete)
1738 (generic-function-methods gf)
1739 (early-gf-methods gf))))
1742 (warn "no way to determine the lambda list for ~S" gf)
1744 (let* ((method (car (last methods)))
1745 (ll (if (consp method)
1746 (early-method-lambda-list method)
1747 (method-lambda-list method)))
1748 (k (member '&key ll)))
1750 (append (ldiff ll (cdr k)) '(&allow-other-keys))
1752 (arg-info-lambda-list arg-info))))
1754 (defmacro real-ensure-gf-internal (gf-class all-keys env)
1756 (cond ((symbolp ,gf-class)
1757 (setq ,gf-class (find-class ,gf-class t ,env)))
1758 ((classp ,gf-class))
1760 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
1761 class nor a symbol that names a class."
1763 (remf ,all-keys :generic-function-class)
1764 (remf ,all-keys :environment)
1765 (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
1766 (unless (eq combin '.shes-not-there.)
1767 (setf (getf ,all-keys :method-combination)
1768 (find-method-combination (class-prototype ,gf-class)
1772 (defun real-ensure-gf-using-class--generic-function
1776 &key environment (lambda-list nil lambda-list-p)
1777 (generic-function-class 'standard-generic-function gf-class-p)
1779 (real-ensure-gf-internal generic-function-class all-keys environment)
1780 (unless (or (null gf-class-p)
1781 (eq (class-of existing) generic-function-class))
1782 (change-class existing generic-function-class))
1784 (apply #'reinitialize-instance existing all-keys)
1786 (proclaim (defgeneric-declaration function-name lambda-list)))))
1788 (defun real-ensure-gf-using-class--null
1792 &key environment (lambda-list nil lambda-list-p)
1793 (generic-function-class 'standard-generic-function)
1795 (declare (ignore existing))
1796 (real-ensure-gf-internal generic-function-class all-keys environment)
1798 (setf (gdefinition function-name)
1799 (apply #'make-instance generic-function-class
1800 :name function-name all-keys))
1802 (proclaim (defgeneric-declaration function-name lambda-list)))))
1804 (defun get-generic-function-info (gf)
1805 ;; values nreq applyp metatypes nkeys arg-info
1806 (multiple-value-bind (applyp metatypes arg-info)
1807 (let* ((arg-info (if (early-gf-p gf)
1808 (early-gf-arg-info gf)
1810 (metatypes (arg-info-metatypes arg-info)))
1811 (values (arg-info-applyp arg-info)
1814 (values (length metatypes) applyp metatypes
1815 (count-if #'(lambda (x) (neq x 't)) metatypes)
1818 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
1819 &optional slot-name)
1820 (initialize-method-function initargs)
1823 ;; Figure out whether we got class objects or class names as the
1824 ;; specializers and set parsed and unparsed appropriately. If we
1825 ;; got class objects, then we can compute unparsed, but if we got
1826 ;; class names we don't try to compute parsed.
1828 ;; Note that the use of not symbolp in this call to every should be
1829 ;; read as 'classp' we can't use classp itself because it doesn't
1831 (if (every #'(lambda (s) (not (symbolp s))) specializers)
1832 (setq parsed specializers
1833 unparsed (mapcar #'(lambda (s)
1834 (if (eq s 't) 't (class-name s)))
1836 (setq unparsed specializers
1838 (list :early-method ;This is an early method dammit!
1840 (getf initargs ':function)
1841 (getf initargs ':fast-function)
1843 parsed ;The parsed specializers. This is used
1844 ;by early-method-specializers to cache
1845 ;the parse. Note that this only comes
1846 ;into play when there is more than one
1847 ;early method on an early gf.
1849 (list class ;A list to which real-make-a-method
1850 qualifiers ;can be applied to make a real method
1851 arglist ;corresponding to this early one.
1857 (defun real-make-a-method
1858 (class qualifiers lambda-list specializers initargs doc
1859 &optional slot-name)
1860 (setq specializers (parse-specializers specializers))
1861 (apply #'make-instance class
1862 :qualifiers qualifiers
1863 :lambda-list lambda-list
1864 :specializers specializers
1866 :slot-name slot-name
1870 (defun early-method-function (early-method)
1871 (values (cadr early-method) (caddr early-method)))
1873 (defun early-method-class (early-method)
1874 (find-class (car (fifth early-method))))
1876 (defun early-method-standard-accessor-p (early-method)
1877 (let ((class (first (fifth early-method))))
1878 (or (eq class 'standard-reader-method)
1879 (eq class 'standard-writer-method)
1880 (eq class 'standard-boundp-method))))
1882 (defun early-method-standard-accessor-slot-name (early-method)
1883 (seventh (fifth early-method)))
1885 ;;; Fetch the specializers of an early method. This is basically just a
1886 ;;; simple accessor except that when the second argument is t, this converts
1887 ;;; the specializers from symbols into class objects. The class objects
1888 ;;; are cached in the early method, this makes bootstrapping faster because
1889 ;;; the class objects only have to be computed once.
1891 ;;; the second argument should only be passed as T by early-lookup-method.
1892 ;;; this is to implement the rule that only when there is more than one
1893 ;;; early method on a generic function is the conversion from class names
1894 ;;; to class objects done.
1895 ;;; the corresponds to the fact that we are only allowed to have one method
1896 ;;; on any generic function up until the time classes exist.
1897 (defun early-method-specializers (early-method &optional objectsp)
1898 (if (and (listp early-method)
1899 (eq (car early-method) :early-method))
1900 (cond ((eq objectsp 't)
1901 (or (fourth early-method)
1902 (setf (fourth early-method)
1903 (mapcar #'find-class (cadddr (fifth early-method))))))
1905 (cadddr (fifth early-method))))
1906 (error "~S is not an early-method." early-method)))
1908 (defun early-method-qualifiers (early-method)
1909 (cadr (fifth early-method)))
1911 (defun early-method-lambda-list (early-method)
1912 (caddr (fifth early-method)))
1914 (defun early-add-named-method (generic-function-name
1919 (let* ((gf (ensure-generic-function generic-function-name))
1921 (dolist (m (early-gf-methods gf))
1922 (when (and (equal (early-method-specializers m) specializers)
1923 (equal (early-method-qualifiers m) qualifiers))
1925 (new (make-a-method 'standard-method
1931 (when existing (remove-method gf existing))
1932 (add-method gf new)))
1934 ;;; This is the early version of add-method. Later this will become a
1935 ;;; generic function. See fix-early-generic-functions which has special
1936 ;;; knowledge about add-method.
1937 (defun add-method (generic-function method)
1938 (when (not (fsc-instance-p generic-function))
1939 (error "Early add-method didn't get a funcallable instance."))
1940 (when (not (and (listp method) (eq (car method) :early-method)))
1941 (error "Early add-method didn't get an early method."))
1942 (push method (early-gf-methods generic-function))
1943 (set-arg-info generic-function :new-method method)
1944 (unless (assoc (early-gf-name generic-function) *generic-function-fixups*
1946 (update-dfun generic-function)))
1948 ;;; This is the early version of REMOVE-METHOD..
1949 (defun remove-method (generic-function method)
1950 (when (not (fsc-instance-p generic-function))
1951 (error "An early remove-method didn't get a funcallable instance."))
1952 (when (not (and (listp method) (eq (car method) :early-method)))
1953 (error "An early remove-method didn't get an early method."))
1954 (setf (early-gf-methods generic-function)
1955 (remove method (early-gf-methods generic-function)))
1956 (set-arg-info generic-function)
1957 (unless (assoc (early-gf-name generic-function) *generic-function-fixups*
1959 (update-dfun generic-function)))
1961 ;;; ..and the early version of GET-METHOD.
1962 (defun get-method (generic-function qualifiers specializers
1963 &optional (errorp t))
1964 (if (early-gf-p generic-function)
1965 (or (dolist (m (early-gf-methods generic-function))
1966 (when (and (or (equal (early-method-specializers m nil)
1968 (equal (early-method-specializers m 't)
1970 (equal (early-method-qualifiers m) qualifiers))
1973 (error "can't get early method")
1975 (real-get-method generic-function qualifiers specializers errorp)))
1977 (defvar *fegf-debug-p* nil)
1979 (defun fix-early-generic-functions (&optional (noisyp *fegf-debug-p*))
1980 (let ((accessors nil))
1981 ;; Rearrange *EARLY-GENERIC-FUNCTIONS* to speed up
1982 ;; FIX-EARLY-GENERIC-FUNCTIONS.
1983 (dolist (early-gf-spec *early-generic-functions*)
1984 (when (every #'early-method-standard-accessor-p
1985 (early-gf-methods (gdefinition early-gf-spec)))
1986 (push early-gf-spec accessors)))
1987 (dolist (spec (nconc accessors
1988 '(accessor-method-slot-name
1989 generic-function-methods
1994 slot-definition-location
1995 slot-definition-name
1998 class-precedence-list
1999 slot-boundp-using-class
2000 (setf slot-value-using-class)
2001 slot-value-using-class
2004 funcallable-standard-class-p
2006 (setq *early-generic-functions*
2007 (cons spec (delete spec *early-generic-functions* :test #'equal))))
2009 (dolist (early-gf-spec *early-generic-functions*)
2010 (when noisyp (format t "~&~S..." early-gf-spec))
2011 (let* ((gf (gdefinition early-gf-spec))
2012 (methods (mapcar #'(lambda (early-method)
2013 (let ((args (copy-list (fifth
2016 (early-method-specializers
2018 (apply #'real-make-a-method args)))
2019 (early-gf-methods gf))))
2020 (setf (generic-function-method-class gf) *the-class-standard-method*)
2021 (setf (generic-function-method-combination gf)
2022 *standard-method-combination*)
2023 (set-methods gf methods)))
2025 (dolist (fns *early-functions*)
2026 (setf (gdefinition (car fns)) (symbol-function (caddr fns))))
2028 (dolist (fixup *generic-function-fixups*)
2029 (let* ((fspec (car fixup))
2030 (gf (gdefinition fspec))
2031 (methods (mapcar #'(lambda (method)
2032 (let* ((lambda-list (first method))
2033 (specializers (second method))
2034 (method-fn-name (third method))
2035 (fn-name (or method-fn-name fspec))
2036 (fn (symbol-function fn-name))
2040 #'(lambda (args next-methods)
2044 `(call ,fn-name)))))
2045 (declare (type function fn))
2046 (make-a-method 'standard-method
2053 (setf (generic-function-method-class gf) *the-class-standard-method*)
2054 (setf (generic-function-method-combination gf)
2055 *standard-method-combination*)
2056 (set-methods gf methods)))))
2058 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument into
2059 ;;; the 'real' arguments. This is where the syntax of DEFMETHOD is really
2061 (defun parse-defmethod (cdr-of-form)
2062 ;;(declare (values name qualifiers specialized-lambda-list body))
2063 (let ((name (pop cdr-of-form))
2066 (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2067 (push (pop cdr-of-form) qualifiers)
2068 (return (setq qualifiers (nreverse qualifiers)))))
2069 (setq spec-ll (pop cdr-of-form))
2070 (values name qualifiers spec-ll cdr-of-form)))
2072 (defun parse-specializers (specializers)
2073 (flet ((parse (spec)
2074 (let ((result (specializer-from-type spec)))
2075 (if (specializerp result)
2078 (error "~S was used as a specializer,~%~
2079 but is not the name of a class."
2081 (error "~S is not a legal specializer." spec))))))
2082 (mapcar #'parse specializers)))
2084 (defun unparse-specializers (specializers-or-method)
2085 (if (listp specializers-or-method)
2086 (flet ((unparse (spec)
2087 (if (specializerp spec)
2088 (let ((type (specializer-type spec)))
2089 (if (and (consp type)
2090 (eq (car type) 'class))
2091 (let* ((class (cadr type))
2092 (class-name (class-name class)))
2093 (if (eq class (find-class class-name nil))
2097 (error "~S is not a legal specializer." spec))))
2098 (mapcar #'unparse specializers-or-method))
2099 (unparse-specializers (method-specializers specializers-or-method))))
2101 (defun parse-method-or-spec (spec &optional (errorp t))
2102 ;;(declare (values generic-function method method-name))
2103 (let (gf method name temp)
2106 gf (method-generic-function method)
2107 temp (and gf (generic-function-name gf))
2109 (intern-function-name
2110 (make-method-spec temp
2111 (method-qualifiers method)
2112 (unparse-specializers
2113 (method-specializers method))))
2114 (make-symbol (format nil "~S" method))))
2115 (multiple-value-bind (gf-spec quals specls)
2116 (parse-defmethod spec)
2117 (and (setq gf (and (or errorp (gboundp gf-spec))
2118 (gdefinition gf-spec)))
2119 (let ((nreq (compute-discriminating-function-arglist-info gf)))
2120 (setq specls (append (parse-specializers specls)
2121 (make-list (- nreq (length specls))
2125 (setq method (get-method gf quals specls errorp))
2127 (intern-function-name (make-method-spec gf-spec
2130 (values gf method name)))
2132 (defun extract-parameters (specialized-lambda-list)
2133 (multiple-value-bind (parameters ignore1 ignore2)
2134 (parse-specialized-lambda-list specialized-lambda-list)
2135 (declare (ignore ignore1 ignore2))
2138 (defun extract-lambda-list (specialized-lambda-list)
2139 (multiple-value-bind (ignore1 lambda-list ignore2)
2140 (parse-specialized-lambda-list specialized-lambda-list)
2141 (declare (ignore ignore1 ignore2))
2144 (defun extract-specializer-names (specialized-lambda-list)
2145 (multiple-value-bind (ignore1 ignore2 specializers)
2146 (parse-specialized-lambda-list specialized-lambda-list)
2147 (declare (ignore ignore1 ignore2))
2150 (defun extract-required-parameters (specialized-lambda-list)
2151 (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2152 (parse-specialized-lambda-list specialized-lambda-list)
2153 (declare (ignore ignore1 ignore2 ignore3))
2154 required-parameters))
2156 (defun parse-specialized-lambda-list (arglist &optional post-keyword)
2157 ;;(declare (values parameters lambda-list specializers required-parameters))
2158 (let ((arg (car arglist)))
2159 (cond ((null arglist) (values nil nil nil nil))
2161 (values nil arglist nil))
2162 ((memq arg lambda-list-keywords)
2163 (unless (memq arg '(&optional &rest &key &allow-other-keys &aux))
2164 ;; Warn about non-standard lambda-list-keywords, but then
2165 ;; go on to treat them like a standard lambda-list-keyword
2166 ;; what with the warning its probably ok.
2168 ;; FIXME: This shouldn't happen now that this is maintained
2169 ;; as part of SBCL, should it? Perhaps this is now
2170 ;; "internal error: unrecognized lambda-list keyword ~S"?
2171 (warn "Unrecognized lambda-list keyword ~S in arglist.~%~
2172 Assuming that the symbols following it are parameters,~%~
2173 and not allowing any parameter specializers to follow~%~
2176 ;; When we are at a lambda-list keyword, the parameters don't
2177 ;; include the lambda-list keyword; the lambda-list does include
2178 ;; the lambda-list keyword; and no specializers are allowed to
2179 ;; follow the lambda-list keywords (at least for now).
2180 (multiple-value-bind (parameters lambda-list)
2181 (parse-specialized-lambda-list (cdr arglist) t)
2183 (cons arg lambda-list)
2187 ;; After a lambda-list keyword there can be no specializers.
2188 (multiple-value-bind (parameters lambda-list)
2189 (parse-specialized-lambda-list (cdr arglist) t)
2190 (values (cons (if (listp arg) (car arg) arg) parameters)
2191 (cons arg lambda-list)
2195 (multiple-value-bind (parameters lambda-list specializers required)
2196 (parse-specialized-lambda-list (cdr arglist))
2197 (values (cons (if (listp arg) (car arg) arg) parameters)
2198 (cons (if (listp arg) (car arg) arg) lambda-list)
2199 (cons (if (listp arg) (cadr arg) 't) specializers)
2200 (cons (if (listp arg) (car arg) arg) required)))))))
2202 (eval-when (:load-toplevel :execute)
2203 (setq *boot-state* 'early))
2205 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET which used
2206 ;;; %WALKER stuff. That suggests to me that maybe the code walker stuff was
2207 ;;; only used for implementing stuff like that; maybe it's not needed any more?
2208 ;;; Hunt down what it was used for and see.
2210 (defmacro with-slots (slots instance &body body)
2211 (let ((in (gensym)))
2212 `(let ((,in ,instance))
2213 (declare (ignorable ,in))
2214 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2217 (and (symbolp instance)
2218 `((declare (variable-rebinding ,in ,instance)))))
2220 (symbol-macrolet ,(mapcar #'(lambda (slot-entry)
2221 (let ((variable-name
2222 (if (symbolp slot-entry)
2226 (if (symbolp slot-entry)
2228 (cadr slot-entry))))
2230 (slot-value ,in ',slot-name))))
2234 (defmacro with-accessors (slots instance &body body)
2235 (let ((in (gensym)))
2236 `(let ((,in ,instance))
2237 (declare (ignorable ,in))
2238 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2241 (and (symbolp instance)
2242 `((declare (variable-rebinding ,in ,instance)))))
2244 (symbol-macrolet ,(mapcar #'(lambda (slot-entry)
2245 (let ((variable-name (car slot-entry))
2246 (accessor-name (cadr slot-entry)))
2248 (,accessor-name ,in))))