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)))
164 (check-gf-lambda-list lambda-list)
167 (flet ((duplicate-option (name)
168 (error 'simple-program-error
169 :format-control "The option ~S appears more than once."
170 :format-arguments (list name)))
171 (expand-method-definition (qab) ; QAB = qualifiers, arglist, body
172 (let* ((arglist-pos (position-if #'listp qab))
173 (arglist (elt qab arglist-pos))
174 (qualifiers (subseq qab 0 arglist-pos))
175 (body (nthcdr (1+ arglist-pos) qab)))
176 `(push (defmethod ,fun-name ,@qualifiers ,arglist ,@body)
177 (generic-function-initial-methods #',fun-name)))))
178 (macrolet ((initarg (key) `(getf initargs ,key)))
179 (dolist (option options)
180 (let ((car-option (car option)))
183 (push (cdr option) (initarg :declarations)))
184 ((:argument-precedence-order :method-combination)
185 (if (initarg car-option)
186 (duplicate-option car-option)
187 (setf (initarg car-option)
189 ((:documentation :generic-function-class :method-class)
190 (unless (proper-list-of-length-p option 2)
191 (error "bad list length for ~S" option))
192 (if (initarg car-option)
193 (duplicate-option car-option)
194 (setf (initarg car-option) `',(cadr option))))
196 (push (cdr option) methods))
198 ;; ANSI requires that unsupported things must get a
200 (error 'simple-program-error
201 :format-control "unsupported option ~S"
202 :format-arguments (list option))))))
204 (when (initarg :declarations)
205 (setf (initarg :declarations)
206 `',(initarg :declarations))))
208 (eval-when (:compile-toplevel :load-toplevel :execute)
209 (compile-or-load-defgeneric ',fun-name))
210 (load-defgeneric ',fun-name ',lambda-list ,@initargs)
211 ,@(mapcar #'expand-method-definition methods)
214 (defun compile-or-load-defgeneric (fun-name)
215 (sb-kernel:proclaim-as-fun-name fun-name)
216 (sb-kernel:note-name-defined fun-name :function)
217 (unless (eq (info :function :where-from fun-name) :declared)
218 (setf (info :function :where-from fun-name) :defined)
219 (setf (info :function :type fun-name)
220 (sb-kernel:specifier-type 'function))))
222 (defun load-defgeneric (fun-name lambda-list &rest initargs)
223 (when (fboundp fun-name)
224 (sb-kernel::style-warn "redefining ~S in DEFGENERIC" fun-name)
225 (let ((fun (fdefinition fun-name)))
226 (when (generic-function-p fun)
227 (loop for method in (generic-function-initial-methods fun)
228 do (remove-method fun method))
229 (setf (generic-function-initial-methods fun) '()))))
230 (apply #'ensure-generic-function
232 :lambda-list lambda-list
233 :definition-source `((defgeneric ,fun-name) ,*load-truename*)
236 ;;; As per section 3.4.2 of the ANSI spec, generic function lambda
237 ;;; lists have a number of limitations, which we check here.
238 (defun check-gf-lambda-list (lambda-list)
239 (macrolet ((ensure (condition)
241 (error "Invalid argument ~S in the generic function lambda list ~S."
243 (process-lambda-list lambda-list
244 (&required (ensure (symbolp it)))
245 (&optional (ensure (or (symbolp it)
246 (and (consp it) (symbolp (car it)) (null (cdr it))))))
247 (&rest (ensure (symbolp it)))
248 (&key (ensure (or (symbolp it)
250 (or (symbolp (car it))
251 (and (consp (car it))
256 ((&aux (error "&AUX is not allowed in the generic function lambda list ~S."
259 (defmacro defmethod (&rest args &environment env)
260 (multiple-value-bind (name qualifiers lambda-list body)
261 (parse-defmethod args)
262 (multiple-value-bind (proto-gf proto-method)
263 (prototypes-for-make-method-lambda name)
264 (expand-defmethod name
272 (defun prototypes-for-make-method-lambda (name)
273 (if (not (eq *boot-state* 'complete))
275 (let ((gf? (and (gboundp name)
276 (gdefinition name))))
278 (not (generic-function-p gf?)))
279 (values (class-prototype (find-class 'standard-generic-function))
280 (class-prototype (find-class 'standard-method)))
282 (class-prototype (or (generic-function-method-class gf?)
283 (find-class 'standard-method))))))))
285 ;;; Take a name which is either a generic function name or a list specifying
286 ;;; a SETF generic function (like: (SETF <generic-function-name>)). Return
287 ;;; the prototype instance of the method-class for that generic function.
289 ;;; If there is no generic function by that name, this returns the
290 ;;; default value, the prototype instance of the class
291 ;;; STANDARD-METHOD. This default value is also returned if the spec
292 ;;; names an ordinary function or even a macro. In effect, this leaves
293 ;;; the signalling of the appropriate error until load time.
295 ;;; Note: During bootstrapping, this function is allowed to return NIL.
296 (defun method-prototype-for-gf (name)
297 (let ((gf? (and (gboundp name)
298 (gdefinition name))))
299 (cond ((neq *boot-state* 'complete) nil)
301 (not (generic-function-p gf?))) ; Someone else MIGHT
302 ; error at load time.
303 (class-prototype (find-class 'standard-method)))
305 (class-prototype (or (generic-function-method-class gf?)
306 (find-class 'standard-method)))))))
308 (defvar *optimize-asv-funcall-p* nil)
309 (defvar *asv-readers*)
310 (defvar *asv-writers*)
311 (defvar *asv-boundps*)
313 (defun expand-defmethod (name
320 (let ((*make-instance-function-keys* nil)
321 (*optimize-asv-funcall-p* t)
322 (*asv-readers* nil) (*asv-writers* nil) (*asv-boundps* nil))
323 (declare (special *make-instance-function-keys*))
324 (multiple-value-bind (method-lambda unspecialized-lambda-list specializers)
325 (add-method-declarations name qualifiers lambda-list body env)
326 (multiple-value-bind (method-function-lambda initargs)
327 (make-method-lambda proto-gf proto-method method-lambda env)
328 (let ((initargs-form (make-method-initargs-form proto-gf
330 method-function-lambda
334 ;; Note: We could DECLAIM the ftype of the generic
335 ;; function here, since ANSI specifies that we create it
336 ;; if it does not exist. However, I chose not to, because
337 ;; I think it's more useful to support a style of
338 ;; programming where every generic function has an
339 ;; explicit DEFGENERIC and any typos in DEFMETHODs are
340 ;; warned about. Otherwise
341 ;; (DEFGENERIC FOO-BAR-BLETCH ((X T)))
342 ;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
343 ;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
344 ;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
345 ;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
346 ;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
347 ;; compiles without raising an error and runs without
348 ;; raising an error (since SIMPLE-VECTOR cases fall
349 ;; through to VECTOR) but still doesn't do what was
350 ;; intended. I hate that kind of bug (code which silently
351 ;; gives the wrong answer), so we don't do a DECLAIM
352 ;; here. -- WHN 20000229
353 ,@(when *make-instance-function-keys*
354 `((get-make-instance-functions
355 ',*make-instance-function-keys*)))
356 ,@(when (or *asv-readers* *asv-writers* *asv-boundps*)
357 `((initialize-internal-slot-gfs*
358 ',*asv-readers* ',*asv-writers* ',*asv-boundps*)))
359 ,(make-defmethod-form name qualifiers specializers
360 unspecialized-lambda-list
362 (class-name (class-of proto-method))
365 (getf (getf initargs :plist)
366 :pv-table-symbol))))))))
368 (defun interned-symbol-p (x)
369 (and (symbolp x) (symbol-package x)))
371 (defun make-defmethod-form (name qualifiers specializers
372 unspecialized-lambda-list method-class-name
373 initargs-form &optional pv-table-symbol)
376 (if (and (interned-symbol-p (fun-name-block-name name))
377 (every #'interned-symbol-p qualifiers)
380 (and (eq (car s) 'eql)
382 (let ((sv (eval (cadr s))))
383 (or (interned-symbol-p sv)
386 (standard-char-p sv)))))
387 (interned-symbol-p s)))
389 (consp initargs-form)
390 (eq (car initargs-form) 'list*)
391 (memq (cadr initargs-form) '(:function :fast-function))
392 (consp (setq fn (caddr initargs-form)))
393 (eq (car fn) 'function)
394 (consp (setq fn-lambda (cadr fn)))
395 (eq (car fn-lambda) 'lambda))
396 (let* ((specls (mapcar (lambda (specl)
398 `(,(car specl) ,(eval (cadr specl)))
401 (mname `(,(if (eq (cadr initargs-form) :function)
402 'method 'fast-method)
403 ,name ,@qualifiers ,specls))
404 (mname-sym (intern (let ((*print-pretty* nil)
405 ;; (We bind *PACKAGE* to
406 ;; KEYWORD here as a way to
407 ;; force symbols to be printed
408 ;; with explicit package
410 (*package* *keyword-package*))
411 (format nil "~S" mname)))))
413 (defun ,mname-sym ,(cadr fn-lambda)
415 ,(make-defmethod-form-internal
416 name qualifiers `',specls
417 unspecialized-lambda-list method-class-name
418 `(list* ,(cadr initargs-form)
420 ,@(cdddr initargs-form))
422 (make-defmethod-form-internal
424 `(list ,@(mapcar (lambda (specializer)
425 (if (consp specializer)
426 ``(,',(car specializer)
427 ,,(cadr specializer))
430 unspecialized-lambda-list
435 (defun make-defmethod-form-internal
436 (name qualifiers specializers-form unspecialized-lambda-list
437 method-class-name initargs-form &optional pv-table-symbol)
443 ',unspecialized-lambda-list
445 ;; Paper over a bug in KCL by passing the cache-symbol here in
446 ;; addition to in the list. FIXME: We should no longer need to do
447 ;; this, since the CLOS code is now SBCL-specific, and doesn't
448 ;; need to be ported to every buggy compiler in existence.
451 (defmacro make-method-function (method-lambda &environment env)
452 (make-method-function-internal method-lambda env))
454 (defun make-method-function-internal (method-lambda &optional env)
455 (multiple-value-bind (proto-gf proto-method)
456 (prototypes-for-make-method-lambda nil)
457 (multiple-value-bind (method-function-lambda initargs)
458 (make-method-lambda proto-gf proto-method method-lambda env)
459 (make-method-initargs-form proto-gf
461 method-function-lambda
465 (defun add-method-declarations (name qualifiers lambda-list body env)
466 (multiple-value-bind (parameters unspecialized-lambda-list specializers)
467 (parse-specialized-lambda-list lambda-list)
468 (declare (ignore parameters))
469 (multiple-value-bind (real-body declarations documentation)
470 (parse-body body env)
471 (values `(lambda ,unspecialized-lambda-list
472 ,@(when documentation `(,documentation))
473 ;; (Old PCL code used a somewhat different style of
474 ;; list for %METHOD-NAME values. Our names use
475 ;; ,@QUALIFIERS instead of ,QUALIFIERS so that the
476 ;; method names look more like what you see in a
479 ;; FIXME: As of sbcl-0.7.0.6, code elsewhere, at
480 ;; least the code to set up named BLOCKs around the
481 ;; bodies of methods, depends on the function's base
482 ;; name being the first element of the %METHOD-NAME
483 ;; list. It would be good to remove this dependency,
484 ;; perhaps by building the BLOCK here, or by using
485 ;; another declaration (e.g. %BLOCK-NAME), so that
486 ;; our method debug names are free to have any format,
487 ;; e.g. (:METHOD PRINT-OBJECT :AROUND (CLOWN T)).
488 (declare (%method-name (,name
491 (declare (%method-lambda-list ,@lambda-list))
494 unspecialized-lambda-list specializers))))
496 (defun real-make-method-initargs-form (proto-gf proto-method
497 method-lambda initargs env)
498 (declare (ignore proto-gf proto-method))
499 (unless (and (consp method-lambda)
500 (eq (car method-lambda) 'lambda))
501 (error "The METHOD-LAMBDA argument to MAKE-METHOD-FUNCTION, ~S, ~
502 is not a lambda form."
504 (make-method-initargs-form-internal method-lambda initargs env))
506 (unless (fboundp 'make-method-initargs-form)
507 (setf (gdefinition 'make-method-initargs-form)
508 (symbol-function 'real-make-method-initargs-form)))
510 (defun real-make-method-lambda (proto-gf proto-method method-lambda env)
511 (declare (ignore proto-gf proto-method))
512 (make-method-lambda-internal method-lambda env))
514 ;;; a helper function for creating Python-friendly type declarations
515 ;;; in DEFMETHOD forms
516 (defun parameter-specializer-declaration-in-defmethod (parameter specializer)
517 (cond ((and (consp specializer)
518 (eq (car specializer) 'eql))
519 ;; KLUDGE: ANSI, in its wisdom, says that
520 ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at
521 ;; DEFMETHOD expansion time. Thus, although one might think
523 ;; (DEFMETHOD FOO ((X PACKAGE)
526 ;; the PACKAGE and (EQL 12) forms are both parallel type
527 ;; names, they're not, as is made clear when you do
528 ;; (DEFMETHOD FOO ((X PACKAGE)
531 ;; where Y needs to be a symbol named "BAR", not some cons
532 ;; made by (CONS 'QUOTE 'BAR). I.e. when the
533 ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument
534 ;; to be of type (EQL X). It'd be easy to transform one to
535 ;; the other, but it'd be somewhat messier to do so while
536 ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd
537 ;; once. (The new code wouldn't be messy, but it'd require a
538 ;; big transformation of the old code.) So instead we punt.
542 ;; KLUDGE: For some low-level implementation
543 ;; classes, perhaps because of some problems related
544 ;; to the incomplete integration of PCL into SBCL's
545 ;; type system, some specializer classes can't be
546 ;; declared as argument types. E.g.
547 ;; (DEFMETHOD FOO ((X SLOT-OBJECT))
548 ;; (DECLARE (TYPE SLOT-OBJECT X))
551 ;; (DEFSTRUCT BAR A B)
553 ;; perhaps because of the way that STRUCTURE-OBJECT
554 ;; inherits both from SLOT-OBJECT and from
555 ;; SB-KERNEL:INSTANCE. In an effort to sweep such
556 ;; problems under the rug, we exclude these problem
557 ;; cases by blacklisting them here. -- WHN 2001-01-19
560 ((not (eq *boot-state* 'complete))
561 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
562 ;; types which don't match their specializers. (Specifically,
563 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
564 ;; second argument.) Hopefully it only does this kind of
565 ;; weirdness when bootstrapping.. -- WHN 20000610
568 ;; Otherwise, we can make Python very happy.
569 `(type ,specializer ,parameter))))
571 (defun make-method-lambda-internal (method-lambda &optional env)
572 (unless (and (consp method-lambda) (eq (car method-lambda) 'lambda))
573 (error "The METHOD-LAMBDA argument to MAKE-METHOD-LAMBDA, ~S, ~
574 is not a lambda form."
576 (multiple-value-bind (real-body declarations documentation)
577 (parse-body (cddr method-lambda) env)
578 (let* ((name-decl (get-declaration '%method-name declarations))
579 (sll-decl (get-declaration '%method-lambda-list declarations))
580 (method-name (when (consp name-decl) (car name-decl)))
581 (generic-function-name (when method-name (car method-name)))
582 (specialized-lambda-list (or sll-decl (cadr method-lambda))))
583 (multiple-value-bind (parameters lambda-list specializers)
584 (parse-specialized-lambda-list specialized-lambda-list)
585 (let* ((required-parameters
586 (mapcar (lambda (r s) (declare (ignore s)) r)
589 (slots (mapcar #'list required-parameters))
593 ;; These declarations seem to be used by PCL to pass
594 ;; information to itself; when I tried to delete 'em
595 ;; ca. 0.6.10 it didn't work. I'm not sure how
596 ;; they work, but note the (VAR-DECLARATION '%CLASS ..)
597 ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30
599 (mapcar (lambda (a s) (and (symbolp s)
604 ;; These TYPE declarations weren't in the original
605 ;; PCL code, but the Python compiler likes them a
606 ;; lot. (We're telling the compiler about our
607 ;; knowledge of specialized argument types so that
608 ;; it can avoid run-time type dispatch overhead,
609 ;; which can be a huge win for Python.)
611 ;; FIXME: Perhaps these belong in
612 ;; ADD-METHOD-DECLARATIONS instead of here?
613 ,@(mapcar #'parameter-specializer-declaration-in-defmethod
617 ;; Remove the documentation string and insert the
618 ;; appropriate class declarations. The documentation
619 ;; string is removed to make it easy for us to insert
620 ;; new declarations later, they will just go after the
621 ;; CADR of the method lambda. The class declarations
622 ;; are inserted to communicate the class of the method's
623 ;; arguments to the code walk.
624 `(lambda ,lambda-list
625 ;; The default ignorability of method parameters
626 ;; doesn't seem to be specified by ANSI. PCL had
627 ;; them basically ignorable but was a little
628 ;; inconsistent. E.g. even though the two
629 ;; method definitions
630 ;; (DEFMETHOD FOO ((X T) (Y T)) "Z")
631 ;; (DEFMETHOD FOO ((X T) Y) "Z")
632 ;; are otherwise equivalent, PCL treated Y as
633 ;; ignorable in the first definition but not in the
634 ;; second definition. We make all required
635 ;; parameters ignorable as a way of systematizing
636 ;; the old PCL behavior. -- WHN 2000-11-24
637 (declare (ignorable ,@required-parameters))
640 (block ,(fun-name-block-name generic-function-name)
642 (constant-value-p (and (null (cdr real-body))
643 (constantp (car real-body))))
644 (constant-value (and constant-value-p
645 (eval (car real-body))))
646 (plist (and constant-value-p
647 (or (typep constant-value
648 '(or number character))
649 (and (symbolp constant-value)
650 (symbol-package constant-value)))
651 (list :constant-value constant-value)))
652 (applyp (dolist (p lambda-list nil)
653 (cond ((memq p '(&optional &rest &key))
658 (walked-lambda call-next-method-p closurep next-method-p-p)
659 (walk-method-lambda method-lambda
664 (multiple-value-bind (walked-lambda-body
666 walked-documentation)
667 (parse-body (cddr walked-lambda) env)
668 (declare (ignore walked-documentation))
669 (when (or next-method-p-p call-next-method-p)
670 (setq plist (list* :needs-next-methods-p t plist)))
671 (when (some #'cdr slots)
672 (multiple-value-bind (slot-name-lists call-list)
673 (slot-name-lists-from-slots slots calls)
674 (let ((pv-table-symbol (make-symbol "pv-table")))
676 `(,@(when slot-name-lists
677 `(:slot-name-lists ,slot-name-lists))
679 `(:call-list ,call-list))
680 :pv-table-symbol ,pv-table-symbol
682 (setq walked-lambda-body
683 `((pv-binding (,required-parameters
686 ,@walked-lambda-body))))))
687 (when (and (memq '&key lambda-list)
688 (not (memq '&allow-other-keys lambda-list)))
689 (let ((aux (memq '&aux lambda-list)))
690 (setq lambda-list (nconc (ldiff lambda-list aux)
691 (list '&allow-other-keys)
693 (values `(lambda (.method-args. .next-methods.)
694 (simple-lexical-method-functions
695 (,lambda-list .method-args. .next-methods.
698 :next-method-p-p ,next-method-p-p
701 ,@walked-declarations
702 ,@walked-lambda-body))
705 ,@(when documentation
706 `(:documentation ,documentation)))))))))))
708 (unless (fboundp 'make-method-lambda)
709 (setf (gdefinition 'make-method-lambda)
710 (symbol-function 'real-make-method-lambda)))
712 (defmacro simple-lexical-method-functions ((lambda-list
718 ,method-args ,next-methods
719 (bind-simple-lexical-method-macros (,method-args ,next-methods)
720 (bind-lexical-method-functions (,@lmf-options)
721 (bind-args (,lambda-list ,method-args)
724 (defmacro fast-lexical-method-functions ((lambda-list
730 `(bind-fast-lexical-method-macros (,args ,rest-arg ,next-method-call)
731 (bind-lexical-method-functions (,@lmf-options)
732 (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg)
735 (defmacro bind-simple-lexical-method-macros ((method-args next-methods)
737 `(macrolet ((call-next-method-bind (&body body)
738 `(let ((.next-method. (car ,',next-methods))
739 (,',next-methods (cdr ,',next-methods)))
740 .next-method. ,',next-methods
742 (call-next-method-body (cnm-args)
744 (funcall (if (std-instance-p .next-method.)
745 (method-function .next-method.)
746 .next-method.) ; for early methods
747 (or ,cnm-args ,',method-args)
749 (error "no next method")))
750 (next-method-p-body ()
751 `(not (null .next-method.))))
754 (defstruct (method-call (:copier nil))
755 (function #'identity :type function)
758 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
760 (defmacro invoke-method-call1 (function args cm-args)
761 `(let ((.function. ,function)
763 (.cm-args. ,cm-args))
764 (if (and .cm-args. (null (cdr .cm-args.)))
765 (funcall .function. .args. (car .cm-args.))
766 (apply .function. .args. .cm-args.))))
768 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
769 `(invoke-method-call1 (method-call-function ,method-call)
771 `(list* ,@required-args+rest-arg)
772 `(list ,@required-args+rest-arg))
773 (method-call-call-method-args ,method-call)))
775 (defstruct (fast-method-call (:copier nil))
776 (function #'identity :type function)
781 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
783 (defmacro fmc-funcall (fn pv-cell next-method-call &rest args)
784 `(funcall ,fn ,pv-cell ,next-method-call ,@args))
786 (defmacro invoke-fast-method-call (method-call &rest required-args+rest-arg)
787 `(fmc-funcall (fast-method-call-function ,method-call)
788 (fast-method-call-pv-cell ,method-call)
789 (fast-method-call-next-method-call ,method-call)
790 ,@required-args+rest-arg))
792 (defstruct (fast-instance-boundp (:copier nil))
793 (index 0 :type fixnum))
795 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
797 (eval-when (:compile-toplevel :load-toplevel :execute)
798 (defvar *allow-emf-call-tracing-p* nil)
799 (defvar *enable-emf-call-tracing-p* #-sb-show nil #+sb-show t))
801 ;;;; effective method functions
803 (defvar *emf-call-trace-size* 200)
804 (defvar *emf-call-trace* nil)
805 (defvar *emf-call-trace-index* 0)
807 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
808 ;;; without explanation. It appears to be intended for debugging, so
809 ;;; it might be useful someday, so I haven't deleted it.
810 ;;; But it isn't documented and isn't used for anything now, so
811 ;;; I've conditionalized it out of the base system. -- WHN 19991213
813 (defun show-emf-call-trace ()
814 (when *emf-call-trace*
815 (let ((j *emf-call-trace-index*)
816 (*enable-emf-call-tracing-p* nil))
817 (format t "~&(The oldest entries are printed first)~%")
818 (dotimes-fixnum (i *emf-call-trace-size*)
819 (let ((ct (aref *emf-call-trace* j)))
820 (when ct (print ct)))
822 (when (= j *emf-call-trace-size*)
825 (defun trace-emf-call-internal (emf format args)
826 (unless *emf-call-trace*
827 (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
828 (setf (aref *emf-call-trace* *emf-call-trace-index*)
829 (list* emf format args))
830 (incf *emf-call-trace-index*)
831 (when (= *emf-call-trace-index* *emf-call-trace-size*)
832 (setq *emf-call-trace-index* 0)))
834 (defmacro trace-emf-call (emf format args)
835 (when *allow-emf-call-tracing-p*
836 `(when *enable-emf-call-tracing-p*
837 (trace-emf-call-internal ,emf ,format ,args))))
839 (defmacro invoke-effective-method-function-fast
840 (emf restp &rest required-args+rest-arg)
842 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
843 (invoke-fast-method-call ,emf ,@required-args+rest-arg)))
845 (defmacro invoke-effective-method-function (emf restp
846 &rest required-args+rest-arg)
847 (unless (constantp restp)
848 (error "The RESTP argument is not constant."))
849 ;; FIXME: The RESTP handling here is confusing and maybe slightly
850 ;; broken if RESTP evaluates to a non-self-evaluating form. E.g. if
851 ;; (INVOKE-EFFECTIVE-METHOD-FUNCTION EMF '(ERROR "gotcha") ...)
852 ;; then TRACE-EMF-CALL-CALL-INTERNAL might die on a gotcha error.
853 (setq restp (eval restp))
855 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
856 (cond ((typep ,emf 'fast-method-call)
857 (invoke-fast-method-call ,emf ,@required-args+rest-arg))
858 ;; "What," you may wonder, "do these next two clauses do?"
859 ;; In that case, you are not a PCL implementor, for they
860 ;; considered this to be self-documenting.:-| Or CSR, for
861 ;; that matter, since he can also figure it out by looking
862 ;; at it without breaking stride. For the rest of us,
863 ;; though: From what the code is doing with .SLOTS. and
864 ;; whatnot, evidently it's implementing SLOT-VALUEish and
865 ;; GET-SLOT-VALUEish things. Then we can reason backwards
866 ;; and conclude that setting EMF to a FIXNUM is an
867 ;; optimized way to represent these slot access operations.
868 ,@(when (and (null restp) (= 1 (length required-args+rest-arg)))
869 `(((typep ,emf 'fixnum)
870 (let* ((.slots. (get-slots-or-nil
871 ,(car required-args+rest-arg)))
872 (value (when .slots. (clos-slots-ref .slots. ,emf))))
873 (if (eq value +slot-unbound+)
874 (slot-unbound-internal ,(car required-args+rest-arg)
877 ,@(when (and (null restp) (= 2 (length required-args+rest-arg)))
878 `(((typep ,emf 'fixnum)
879 (let ((.new-value. ,(car required-args+rest-arg))
880 (.slots. (get-slots-or-nil
881 ,(car required-args+rest-arg))))
883 (setf (clos-slots-ref .slots. ,emf) .new-value.))))))
884 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
885 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
886 ;; there was no explanation and presumably the code is 10+
887 ;; years stale, I simply deleted it. -- WHN)
891 (invoke-method-call ,emf ,restp ,@required-args+rest-arg))
894 `(apply (the function ,emf) ,@required-args+rest-arg)
895 `(funcall (the function ,emf)
896 ,@required-args+rest-arg))))))))
898 (defun invoke-emf (emf args)
899 (trace-emf-call emf t args)
902 (let* ((arg-info (fast-method-call-arg-info emf))
903 (restp (cdr arg-info))
904 (nreq (car arg-info)))
906 (let* ((rest-args (nthcdr nreq args))
907 (req-args (ldiff args rest-args)))
908 (apply (fast-method-call-function emf)
909 (fast-method-call-pv-cell emf)
910 (fast-method-call-next-method-call emf)
911 (nconc req-args (list rest-args))))
914 (invoke-fast-method-call emf)
915 (error "wrong number of args")))
918 (invoke-fast-method-call emf (car args))
919 (error "wrong number of args")))
922 (invoke-fast-method-call emf (car args) (cadr args))
923 (error "wrong number of args")))
925 (apply (fast-method-call-function emf)
926 (fast-method-call-pv-cell emf)
927 (fast-method-call-next-method-call emf)
930 (apply (method-call-function emf)
932 (method-call-call-method-args emf)))
934 (cond ((null args) (error "1 or 2 args were expected."))
936 (let* ((slots (get-slots (car args)))
937 (value (clos-slots-ref slots emf)))
938 (if (eq value +slot-unbound+)
939 (slot-unbound-internal (car args) emf)
942 (setf (clos-slots-ref (get-slots (cadr args)) emf)
944 (t (error "1 or 2 args were expected."))))
945 (fast-instance-boundp
946 (if (or (null args) (cdr args))
947 (error "1 arg was expected.")
948 (let ((slots (get-slots (car args))))
949 (not (eq (clos-slots-ref slots
950 (fast-instance-boundp-index emf))
955 ;; KLUDGE: A comment from the original PCL said "This can be improved alot."
956 (defun gf-make-function-from-emf (gf emf)
958 (fast-method-call (let* ((arg-info (gf-arg-info gf))
959 (nreq (arg-info-number-required arg-info))
960 (restp (arg-info-applyp arg-info)))
962 (trace-emf-call emf t args)
963 (apply (fast-method-call-function emf)
964 (fast-method-call-pv-cell emf)
965 (fast-method-call-next-method-call emf)
967 (let* ((rest-args (nthcdr nreq args))
968 (req-args (ldiff args
970 (nconc req-args rest-args))
972 (method-call (lambda (&rest args)
973 (trace-emf-call emf t args)
974 (apply (method-call-function emf)
976 (method-call-call-method-args emf))))
979 (defmacro bind-fast-lexical-method-macros ((args rest-arg next-method-call)
981 `(macrolet ((narrowed-emf (emf)
982 ;; INVOKE-EFFECTIVE-METHOD-FUNCTION has code in it to
983 ;; dispatch on the possibility that EMF might be of
984 ;; type FIXNUM (as an optimized representation of a
985 ;; slot accessor). But as far as I (WHN 2002-06-11)
986 ;; can tell, it's impossible for such a representation
987 ;; to end up as .NEXT-METHOD-CALL. By reassuring
988 ;; INVOKE-E-M-F that when called from this context
989 ;; it needn't worry about the FIXNUM case, we can
990 ;; keep those cases from being compiled, which is
991 ;; good both because it saves bytes and because it
992 ;; avoids annoying type mismatch compiler warnings.
994 ;; KLUDGE: In sbcl-0.7.4.29, the compiler's type
995 ;; system isn't smart enough about NOT and intersection
996 ;; types to benefit from a (NOT FIXNUM) declaration
997 ;; here. -- WHN 2002-06-12
999 ;; FIXME: Might the FUNCTION type be omittable here,
1000 ;; leaving only METHOD-CALLs? Failing that, could this
1001 ;; be documented somehow? (It'd be nice if the types
1002 ;; involved could be understood without solving the
1003 ;; halting problem.)
1004 `(the (or function method-call fast-method-call)
1006 (call-next-method-bind (&body body)
1008 (call-next-method-body (cnm-args)
1009 `(if ,',next-method-call
1011 ;; This declaration suppresses a "deleting
1012 ;; unreachable code" note for the following IF when
1013 ;; REST-ARG is NIL. It is not nice for debugging
1014 ;; SBCL itself, but at least it keeps us from
1016 (declare (optimize (inhibit-warnings 3)))
1017 (if (and (null ',rest-arg)
1019 (eq (car cnm-args) 'list))
1020 `(invoke-effective-method-function
1021 (narrowed-emf ,',next-method-call)
1024 (let ((call `(invoke-effective-method-function
1025 (narrowed-emf ,',next-method-call)
1026 ,',(not (null rest-arg))
1028 ,@',(when rest-arg `(,rest-arg)))))
1030 (bind-args ((,@',args
1032 `(&rest ,rest-arg)))
1036 (error "no next method")))
1037 (next-method-p-body ()
1038 `(not (null ,',next-method-call))))
1041 (defmacro bind-lexical-method-functions
1042 ((&key call-next-method-p next-method-p-p closurep applyp)
1044 (cond ((and (null call-next-method-p) (null next-method-p-p)
1048 ((and (null closurep)
1050 ;; OK to use MACROLET, and all args are mandatory
1051 ;; (else APPLYP would be true).
1052 `(call-next-method-bind
1053 (macrolet ((call-next-method (&rest cnm-args)
1054 `(call-next-method-body ,(when cnm-args
1055 `(list ,@cnm-args))))
1057 `(next-method-p-body)))
1060 `(call-next-method-bind
1061 (flet (,@(and call-next-method-p
1062 '((call-next-method (&rest cnm-args)
1063 (call-next-method-body cnm-args))))
1064 ,@(and next-method-p-p
1066 (next-method-p-body)))))
1069 (defmacro bind-args ((lambda-list args) &body body)
1070 (let ((args-tail '.args-tail.)
1073 (flet ((process-var (var)
1074 (if (memq var lambda-list-keywords)
1077 (&optional (setq state 'optional))
1078 (&key (setq state 'key))
1080 (&rest (setq state 'rest))
1081 (&aux (setq state 'aux))
1084 "encountered the non-standard lambda list keyword ~S"
1088 (required `((,var (pop ,args-tail))))
1089 (optional (cond ((not (consp var))
1090 `((,var (when ,args-tail
1091 (pop ,args-tail)))))
1093 `((,(car var) (if ,args-tail
1097 `((,(caddr var) ,args-tail)
1098 (,(car var) (if ,args-tail
1101 (rest `((,var ,args-tail)))
1102 (key (cond ((not (consp var))
1103 `((,var (get-key-arg ,(keywordicate var)
1106 (multiple-value-bind (keyword variable)
1107 (if (consp (car var))
1110 (values (keywordicate (car var))
1112 `((,key (get-key-arg1 ',keyword ,args-tail))
1113 (,variable (if (consp ,key)
1117 (multiple-value-bind (keyword variable)
1118 (if (consp (car var))
1121 (values (keywordicate (car var))
1123 `((,key (get-key-arg1 ',keyword ,args-tail))
1125 (,variable (if (consp ,key)
1129 (let ((bindings (mapcan #'process-var lambda-list)))
1130 `(let* ((,args-tail ,args)
1132 (declare (ignorable ,args-tail))
1135 (defun get-key-arg (keyword list)
1136 (loop (when (atom list) (return nil))
1137 (when (eq (car list) keyword) (return (cadr list)))
1138 (setq list (cddr list))))
1140 (defun get-key-arg1 (keyword list)
1141 (loop (when (atom list) (return nil))
1142 (when (eq (car list) keyword) (return (cdr list)))
1143 (setq list (cddr list))))
1145 (defun walk-method-lambda (method-lambda required-parameters env slots calls)
1146 (let ((call-next-method-p nil) ; flag indicating that CALL-NEXT-METHOD
1147 ; should be in the method definition
1148 (closurep nil) ; flag indicating that #'CALL-NEXT-METHOD
1149 ; was seen in the body of a method
1150 (next-method-p-p nil)) ; flag indicating that NEXT-METHOD-P
1151 ; should be in the method definition
1152 (flet ((walk-function (form context env)
1153 (cond ((not (eq context :eval)) form)
1154 ;; FIXME: Jumping to a conclusion from the way it's used
1155 ;; above, perhaps CONTEXT should be called SITUATION
1156 ;; (after the term used in the ANSI specification of
1157 ;; EVAL-WHEN) and given modern ANSI keyword values
1158 ;; like :LOAD-TOPLEVEL.
1159 ((not (listp form)) form)
1160 ((eq (car form) 'call-next-method)
1161 (setq call-next-method-p t)
1163 ((eq (car form) 'next-method-p)
1164 (setq next-method-p-p t)
1166 ((and (eq (car form) 'function)
1167 (cond ((eq (cadr form) 'call-next-method)
1168 (setq call-next-method-p t)
1171 ((eq (cadr form) 'next-method-p)
1172 (setq next-method-p-p t)
1176 ((and (memq (car form)
1177 '(slot-value set-slot-value slot-boundp))
1178 (constantp (caddr form)))
1179 (let ((parameter (can-optimize-access form
1182 (let ((fun (ecase (car form)
1183 (slot-value #'optimize-slot-value)
1184 (set-slot-value #'optimize-set-slot-value)
1185 (slot-boundp #'optimize-slot-boundp))))
1186 (funcall fun slots parameter form))))
1187 ((and (eq (car form) 'apply)
1189 (eq (car (cadr form)) 'function)
1190 (generic-function-name-p (cadr (cadr form))))
1191 (optimize-generic-function-call
1192 form required-parameters env slots calls))
1193 ((generic-function-name-p (car form))
1194 (optimize-generic-function-call
1195 form required-parameters env slots calls))
1196 ((and (eq (car form) 'asv-funcall)
1197 *optimize-asv-funcall-p*)
1199 (reader (push (third form) *asv-readers*))
1200 (writer (push (third form) *asv-writers*))
1201 (boundp (push (third form) *asv-boundps*)))
1202 `(,(second form) ,@(cddddr form)))
1205 (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1206 (values walked-lambda
1209 next-method-p-p)))))
1211 (defun generic-function-name-p (name)
1212 (and (legal-fun-name-p name)
1214 (if (eq *boot-state* 'complete)
1215 (standard-generic-function-p (gdefinition name))
1216 (funcallable-instance-p (gdefinition name)))))
1218 (defvar *method-function-plist* (make-hash-table :test 'eq))
1221 (defvar *mf1cp* nil)
1224 (defvar *mf2cp* nil)
1226 (defun method-function-plist (method-function)
1227 (unless (eq method-function *mf1*)
1228 (rotatef *mf1* *mf2*)
1229 (rotatef *mf1p* *mf2p*)
1230 (rotatef *mf1cp* *mf2cp*))
1231 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1232 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1233 (unless (eq method-function *mf1*)
1234 (setf *mf1* method-function
1236 *mf1p* (gethash method-function *method-function-plist*)))
1239 (defun (setf method-function-plist)
1240 (val method-function)
1241 (unless (eq method-function *mf1*)
1242 (rotatef *mf1* *mf2*)
1243 (rotatef *mf1cp* *mf2cp*)
1244 (rotatef *mf1p* *mf2p*))
1245 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1246 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1247 (setf *mf1* method-function
1251 (defun method-function-get (method-function key &optional default)
1252 (getf (method-function-plist method-function) key default))
1254 (defun (setf method-function-get)
1255 (val method-function key)
1256 (setf (getf (method-function-plist method-function) key) val))
1258 (defun method-function-pv-table (method-function)
1259 (method-function-get method-function :pv-table))
1261 (defun method-function-method (method-function)
1262 (method-function-get method-function :method))
1264 (defun method-function-needs-next-methods-p (method-function)
1265 (method-function-get method-function :needs-next-methods-p t))
1267 (defmacro method-function-closure-generator (method-function)
1268 `(method-function-get ,method-function 'closure-generator))
1270 (defun load-defmethod
1271 (class name quals specls ll initargs &optional pv-table-symbol)
1272 (setq initargs (copy-tree initargs))
1273 (let ((method-spec (or (getf initargs :method-spec)
1274 (make-method-spec name quals specls))))
1275 (setf (getf initargs :method-spec) method-spec)
1276 (load-defmethod-internal class name quals specls
1277 ll initargs pv-table-symbol)))
1279 (defun load-defmethod-internal
1280 (method-class gf-spec qualifiers specializers lambda-list
1281 initargs pv-table-symbol)
1282 (when pv-table-symbol
1283 (setf (getf (getf initargs :plist) :pv-table-symbol)
1285 (when (and (eq *boot-state* 'complete)
1287 (let* ((gf (fdefinition gf-spec))
1288 (method (and (generic-function-p gf)
1291 (parse-specializers specializers)
1294 (sb-kernel::style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1295 gf-spec qualifiers specializers))))
1296 (let ((method (apply #'add-named-method
1297 gf-spec qualifiers specializers lambda-list
1298 :definition-source `((defmethod ,gf-spec
1303 (unless (or (eq method-class 'standard-method)
1304 (eq (find-class method-class nil) (class-of method)))
1305 ;; FIXME: should be STYLE-WARNING?
1306 (format *error-output*
1307 "~&At the time the method with qualifiers ~:S and~%~
1308 specializers ~:S on the generic function ~S~%~
1309 was compiled, the method-class for that generic function was~%~
1310 ~S. But, the method class is now ~S, this~%~
1311 may mean that this method was compiled improperly.~%"
1312 qualifiers specializers gf-spec
1313 method-class (class-name (class-of method))))
1316 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1317 `(method ,gf-spec ,@qualifiers ,unparsed-specializers))
1319 (defun initialize-method-function (initargs &optional return-function-p method)
1320 (let* ((mf (getf initargs :function))
1321 (method-spec (getf initargs :method-spec))
1322 (plist (getf initargs :plist))
1323 (pv-table-symbol (getf plist :pv-table-symbol))
1325 (mff (getf initargs :fast-function)))
1326 (flet ((set-mf-property (p v)
1328 (setf (method-function-get mf p) v))
1330 (setf (method-function-get mff p) v))))
1333 (setq mf (set-fun-name mf method-spec)))
1335 (let ((name `(,(or (get (car method-spec) 'fast-sym)
1336 (setf (get (car method-spec) 'fast-sym)
1337 ;; KLUDGE: If we're going to be
1338 ;; interning private symbols in our
1339 ;; a this way, it would be cleanest
1340 ;; to use a separate package
1341 ;; %PCL-PRIVATE or something, and
1342 ;; failing that, to use a special
1343 ;; symbol prefix denoting privateness.
1345 (intern (format nil "FAST-~A"
1348 ,@(cdr method-spec))))
1349 (set-fun-name mff name)
1351 (set-mf-property :name name)))))
1353 (let ((snl (getf plist :slot-name-lists))
1354 (cl (getf plist :call-list)))
1356 (setq pv-table (intern-pv-table :slot-name-lists snl
1358 (when pv-table (set pv-table-symbol pv-table))
1359 (set-mf-property :pv-table pv-table)))
1360 (loop (when (null plist) (return nil))
1361 (set-mf-property (pop plist) (pop plist)))
1363 (set-mf-property :method method))
1364 (when return-function-p
1365 (or mf (method-function-from-fast-function mff)))))))
1367 (defun analyze-lambda-list (lambda-list)
1368 (flet (;; FIXME: Is this redundant with SB-C::MAKE-KEYWORD-FOR-ARG?
1369 (parse-key-arg (arg)
1371 (if (listp (car arg))
1373 (keywordicate (car arg)))
1374 (keywordicate arg))))
1380 (allow-other-keys-p nil)
1382 (keyword-parameters ())
1384 (dolist (x lambda-list)
1385 (if (memq x lambda-list-keywords)
1387 (&optional (setq state 'optional))
1390 (&allow-other-keys (setq allow-other-keys-p t))
1391 (&rest (setq restp t
1395 (error "encountered the non-standard lambda list keyword ~S"
1398 (required (incf nrequired))
1399 (optional (incf noptional))
1400 (key (push (parse-key-arg x) keywords)
1401 (push x keyword-parameters))
1402 (rest (incf nrest)))))
1403 (when (and restp (zerop nrest))
1404 (error "Error in lambda-list:~%~
1405 After &REST, a DEFGENERIC lambda-list ~
1406 must be followed by at least one variable."))
1407 (values nrequired noptional keysp restp allow-other-keys-p
1409 (reverse keyword-parameters)))))
1411 (defun keyword-spec-name (x)
1412 (let ((key (if (atom x) x (car x))))
1417 (defun ftype-declaration-from-lambda-list (lambda-list name)
1418 (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1419 keywords keyword-parameters)
1420 (analyze-lambda-list lambda-list)
1421 (declare (ignore keyword-parameters))
1422 (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1423 (old-ftype (if (sb-kernel:fun-type-p old) old nil))
1424 (old-restp (and old-ftype (sb-kernel:fun-type-rest old-ftype)))
1425 (old-keys (and old-ftype
1426 (mapcar #'sb-kernel:key-info-name
1427 (sb-kernel:fun-type-keywords
1429 (old-keysp (and old-ftype (sb-kernel:fun-type-keyp old-ftype)))
1430 (old-allowp (and old-ftype
1431 (sb-kernel:fun-type-allowp old-ftype)))
1432 (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1433 `(function ,(append (make-list nrequired :initial-element t)
1434 (when (plusp noptional)
1435 (append '(&optional)
1436 (make-list noptional :initial-element t)))
1437 (when (or restp old-restp)
1439 (when (or keysp old-keysp)
1441 (mapcar (lambda (key)
1444 (when (or allow-other-keys-p old-allowp)
1445 '(&allow-other-keys)))))
1448 (defun defgeneric-declaration (spec lambda-list)
1450 (setq spec (get-setf-fun-name (cadr spec))))
1451 `(ftype ,(ftype-declaration-from-lambda-list lambda-list spec) ,spec))
1453 ;;;; early generic function support
1455 (defvar *!early-generic-functions* ())
1457 (defun ensure-generic-function (fun-name
1461 (declare (ignore environment))
1462 (let ((existing (and (gboundp fun-name)
1463 (gdefinition fun-name))))
1465 (eq *boot-state* 'complete)
1466 (null (generic-function-p existing)))
1467 (generic-clobbers-function fun-name)
1468 (apply #'ensure-generic-function-using-class
1469 existing fun-name all-keys))))
1471 (defun generic-clobbers-function (fun-name)
1472 (error 'simple-program-error
1473 :format-control "~S already names an ordinary function or a macro."
1474 :format-arguments (list fun-name)))
1476 (defvar *sgf-wrapper*
1477 (boot-make-wrapper (early-class-size 'standard-generic-function)
1478 'standard-generic-function))
1480 (defvar *sgf-slots-init*
1481 (mapcar (lambda (canonical-slot)
1482 (if (memq (getf canonical-slot :name) '(arg-info source))
1484 (let ((initfunction (getf canonical-slot :initfunction)))
1486 (funcall initfunction)
1488 (early-collect-inheritance 'standard-generic-function)))
1490 (defvar *sgf-method-class-index*
1491 (!bootstrap-slot-index 'standard-generic-function 'method-class))
1493 (defun early-gf-p (x)
1494 (and (fsc-instance-p x)
1495 (eq (clos-slots-ref (get-slots x) *sgf-method-class-index*)
1498 (defvar *sgf-methods-index*
1499 (!bootstrap-slot-index 'standard-generic-function 'methods))
1501 (defmacro early-gf-methods (gf)
1502 `(clos-slots-ref (get-slots ,gf) *sgf-methods-index*))
1504 (defvar *sgf-arg-info-index*
1505 (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1507 (defmacro early-gf-arg-info (gf)
1508 `(clos-slots-ref (get-slots ,gf) *sgf-arg-info-index*))
1510 (defvar *sgf-dfun-state-index*
1511 (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1513 (defstruct (arg-info
1515 (:constructor make-arg-info ())
1517 (arg-info-lambda-list :no-lambda-list)
1520 arg-info-number-optional
1522 arg-info-keys ;nil no &KEY or &REST allowed
1523 ;(k1 k2 ..) Each method must accept these &KEY arguments.
1524 ;T must have &KEY or &REST
1526 gf-info-simple-accessor-type ; nil, reader, writer, boundp
1527 (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1529 gf-info-static-c-a-m-emf
1530 (gf-info-c-a-m-emf-std-p t)
1533 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1535 (defun arg-info-valid-p (arg-info)
1536 (not (null (arg-info-number-optional arg-info))))
1538 (defun arg-info-applyp (arg-info)
1539 (or (plusp (arg-info-number-optional arg-info))
1540 (arg-info-key/rest-p arg-info)))
1542 (defun arg-info-number-required (arg-info)
1543 (length (arg-info-metatypes arg-info)))
1545 (defun arg-info-nkeys (arg-info)
1546 (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1548 ;;; Keep pages clean by not setting if the value is already the same.
1549 (defmacro esetf (pos val)
1550 (let ((valsym (gensym "value")))
1551 `(let ((,valsym ,val))
1552 (unless (equal ,pos ,valsym)
1553 (setf ,pos ,valsym)))))
1555 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1556 argument-precedence-order)
1557 (let* ((arg-info (if (eq *boot-state* 'complete)
1559 (early-gf-arg-info gf)))
1560 (methods (if (eq *boot-state* 'complete)
1561 (generic-function-methods gf)
1562 (early-gf-methods gf)))
1563 (was-valid-p (integerp (arg-info-number-optional arg-info)))
1564 (first-p (and new-method (null (cdr methods)))))
1565 (when (and (not lambda-list-p) methods)
1566 (setq lambda-list (gf-lambda-list gf)))
1567 (when (or lambda-list-p
1569 (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1570 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1571 (analyze-lambda-list lambda-list)
1572 (when (and methods (not first-p))
1573 (let ((gf-nreq (arg-info-number-required arg-info))
1574 (gf-nopt (arg-info-number-optional arg-info))
1575 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1576 (unless (and (= nreq gf-nreq)
1578 (eq (or keysp restp) gf-key/rest-p))
1579 (error "The lambda-list ~S is incompatible with ~
1580 existing methods of ~S."
1583 (esetf (arg-info-lambda-list arg-info) lambda-list))
1584 (when (or lambda-list-p argument-precedence-order
1585 (null (arg-info-precedence arg-info)))
1586 (esetf (arg-info-precedence arg-info)
1587 (compute-precedence lambda-list nreq
1588 argument-precedence-order)))
1589 (esetf (arg-info-metatypes arg-info) (make-list nreq))
1590 (esetf (arg-info-number-optional arg-info) nopt)
1591 (esetf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1592 (esetf (arg-info-keys arg-info)
1594 (if allow-other-keys-p t keywords)
1595 (arg-info-key/rest-p arg-info)))))
1597 (check-method-arg-info gf arg-info new-method))
1598 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1601 (defun check-method-arg-info (gf arg-info method)
1602 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1603 (analyze-lambda-list (if (consp method)
1604 (early-method-lambda-list method)
1605 (method-lambda-list method)))
1606 (flet ((lose (string &rest args)
1608 "attempt to add the method ~S to the generic function ~S.~%~
1612 (apply #'format nil string args)))
1613 (comparison-description (x y)
1614 (if (> x y) "more" "fewer")))
1615 (let ((gf-nreq (arg-info-number-required arg-info))
1616 (gf-nopt (arg-info-number-optional arg-info))
1617 (gf-key/rest-p (arg-info-key/rest-p arg-info))
1618 (gf-keywords (arg-info-keys arg-info)))
1619 (unless (= nreq gf-nreq)
1621 "the method has ~A required arguments than the generic function."
1622 (comparison-description nreq gf-nreq)))
1623 (unless (= nopt gf-nopt)
1625 "the method has ~A optional arguments than the generic function."
1626 (comparison-description nopt gf-nopt)))
1627 (unless (eq (or keysp restp) gf-key/rest-p)
1629 "The method and generic function differ in whether they accept~%~
1630 &REST or &KEY arguments."))
1631 (when (consp gf-keywords)
1632 (unless (or (and restp (not keysp))
1634 (every (lambda (k) (memq k keywords)) gf-keywords))
1635 (lose "the method does not accept each of the &KEY arguments~%~
1639 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1640 (let* ((existing-p (and methods (cdr methods) new-method))
1641 (nreq (length (arg-info-metatypes arg-info)))
1642 (metatypes (if existing-p
1643 (arg-info-metatypes arg-info)
1645 (type (if existing-p
1646 (gf-info-simple-accessor-type arg-info)
1648 (when (arg-info-valid-p arg-info)
1649 (dolist (method (if new-method (list new-method) methods))
1650 (let* ((specializers (if (or (eq *boot-state* 'complete)
1651 (not (consp method)))
1652 (method-specializers method)
1653 (early-method-specializers method t)))
1654 (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1656 (early-method-class method)))
1657 (new-type (when (and class
1658 (or (not (eq *boot-state* 'complete))
1659 (eq (generic-function-method-combination gf)
1660 *standard-method-combination*)))
1661 (cond ((eq class *the-class-standard-reader-method*)
1663 ((eq class *the-class-standard-writer-method*)
1665 ((eq class *the-class-standard-boundp-method*)
1667 (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1668 (setq type (cond ((null type) new-type)
1669 ((eq type new-type) type)
1671 (esetf (arg-info-metatypes arg-info) metatypes)
1672 (esetf (gf-info-simple-accessor-type arg-info) type)))
1673 (when (or (not was-valid-p) first-p)
1674 (multiple-value-bind (c-a-m-emf std-p)
1677 (compute-applicable-methods-emf gf))
1678 (esetf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1679 (esetf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1680 (unless (gf-info-c-a-m-emf-std-p arg-info)
1681 (esetf (gf-info-simple-accessor-type arg-info) t))))
1683 (let ((name (if (eq *boot-state* 'complete)
1684 (generic-function-name gf)
1685 (!early-gf-name gf))))
1686 (esetf (gf-precompute-dfun-and-emf-p arg-info)
1687 (let* ((sym (if (atom name) name (cadr name)))
1688 (pkg-list (cons *pcl-package*
1689 (package-use-list *pcl-package*))))
1690 (and sym (symbolp sym)
1691 (not (null (memq (symbol-package sym) pkg-list)))
1692 (not (find #\space (symbol-name sym))))))))
1693 (esetf (gf-info-fast-mf-p arg-info)
1694 (or (not (eq *boot-state* 'complete))
1695 (let* ((method-class (generic-function-method-class gf))
1696 (methods (compute-applicable-methods
1697 #'make-method-lambda
1698 (list gf (class-prototype method-class)
1700 (and methods (null (cdr methods))
1701 (let ((specls (method-specializers (car methods))))
1702 (and (classp (car specls))
1703 (eq 'standard-generic-function
1704 (class-name (car specls)))
1705 (classp (cadr specls))
1706 (eq 'standard-method
1707 (class-name (cadr specls)))))))))
1710 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
1712 ;;; The STATIC-SLOTS field of the funcallable instances used as early
1713 ;;; generic functions is used to store the early methods and early
1714 ;;; discriminator code for the early generic function. The static
1715 ;;; slots field of the fins contains a list whose:
1716 ;;; CAR - a list of the early methods on this early gf
1717 ;;; CADR - the early discriminator code for this method
1718 (defun ensure-generic-function-using-class (existing spec &rest keys
1719 &key (lambda-list nil
1722 (declare (ignore keys))
1723 (cond ((and existing (early-gf-p existing))
1725 ((assoc spec *!generic-function-fixups* :test #'equal)
1727 (make-early-gf spec lambda-list lambda-list-p existing)
1728 (error "The function ~S is not already defined." spec)))
1730 (error "~S should be on the list ~S."
1732 '*!generic-function-fixups*))
1734 (pushnew spec *!early-generic-functions* :test #'equal)
1735 (make-early-gf spec lambda-list lambda-list-p))))
1737 (defun make-early-gf (spec &optional lambda-list lambda-list-p function)
1738 (let ((fin (allocate-funcallable-instance *sgf-wrapper* *sgf-slots-init*)))
1739 (set-funcallable-instance-fun
1742 (if (eq spec 'print-object)
1743 #'(sb-kernel:instance-lambda (instance stream)
1744 (print-unreadable-object (instance stream :identity t)
1745 (format stream "std-instance")))
1746 #'(sb-kernel:instance-lambda (&rest args)
1747 (declare (ignore args))
1748 (error "The function of the funcallable-instance ~S~
1749 has not been set." fin)))))
1750 (setf (gdefinition spec) fin)
1751 (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
1752 (!bootstrap-set-slot 'standard-generic-function
1756 (set-fun-name fin spec)
1757 (let ((arg-info (make-arg-info)))
1758 (setf (early-gf-arg-info fin) arg-info)
1760 (proclaim (defgeneric-declaration spec lambda-list))
1761 (set-arg-info fin :lambda-list lambda-list)))
1764 (defun set-dfun (gf &optional dfun cache info)
1766 (setf (cache-owner cache) gf))
1767 (let ((new-state (if (and dfun (or cache info))
1768 (list* dfun cache info)
1770 (if (eq *boot-state* 'complete)
1771 (setf (gf-dfun-state gf) new-state)
1772 (setf (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*)
1776 (defun gf-dfun-cache (gf)
1777 (let ((state (if (eq *boot-state* 'complete)
1779 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1782 (cons (cadr state)))))
1784 (defun gf-dfun-info (gf)
1785 (let ((state (if (eq *boot-state* 'complete)
1787 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1790 (cons (cddr state)))))
1792 (defvar *sgf-name-index*
1793 (!bootstrap-slot-index 'standard-generic-function 'name))
1795 (defun !early-gf-name (gf)
1796 (clos-slots-ref (get-slots gf) *sgf-name-index*))
1798 (defun gf-lambda-list (gf)
1799 (let ((arg-info (if (eq *boot-state* 'complete)
1801 (early-gf-arg-info gf))))
1802 (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
1803 (let ((methods (if (eq *boot-state* 'complete)
1804 (generic-function-methods gf)
1805 (early-gf-methods gf))))
1808 (warn "no way to determine the lambda list for ~S" gf)
1810 (let* ((method (car (last methods)))
1811 (ll (if (consp method)
1812 (early-method-lambda-list method)
1813 (method-lambda-list method)))
1814 (k (member '&key ll)))
1816 (append (ldiff ll (cdr k)) '(&allow-other-keys))
1818 (arg-info-lambda-list arg-info))))
1820 (defmacro real-ensure-gf-internal (gf-class all-keys env)
1822 (cond ((symbolp ,gf-class)
1823 (setq ,gf-class (find-class ,gf-class t ,env)))
1824 ((classp ,gf-class))
1826 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
1827 class nor a symbol that names a class."
1829 (remf ,all-keys :generic-function-class)
1830 (remf ,all-keys :environment)
1831 (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
1832 (unless (eq combin '.shes-not-there.)
1833 (setf (getf ,all-keys :method-combination)
1834 (find-method-combination (class-prototype ,gf-class)
1837 (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
1838 (unless (eq method-class '.shes-not-there.)
1839 (setf (getf ,all-keys :method-class)
1840 (find-class method-class t ,env))))))
1842 (defun real-ensure-gf-using-class--generic-function
1846 &key environment (lambda-list nil lambda-list-p)
1847 (generic-function-class 'standard-generic-function gf-class-p)
1849 (real-ensure-gf-internal generic-function-class all-keys environment)
1850 (unless (or (null gf-class-p)
1851 (eq (class-of existing) generic-function-class))
1852 (change-class existing generic-function-class))
1854 (apply #'reinitialize-instance existing all-keys)
1856 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1858 (defun real-ensure-gf-using-class--null
1862 &key environment (lambda-list nil lambda-list-p)
1863 (generic-function-class 'standard-generic-function)
1865 (declare (ignore existing))
1866 (real-ensure-gf-internal generic-function-class all-keys environment)
1868 (setf (gdefinition fun-name)
1869 (apply #'make-instance generic-function-class
1870 :name fun-name all-keys))
1872 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1874 (defun get-generic-fun-info (gf)
1875 ;; values nreq applyp metatypes nkeys arg-info
1876 (multiple-value-bind (applyp metatypes arg-info)
1877 (let* ((arg-info (if (early-gf-p gf)
1878 (early-gf-arg-info gf)
1880 (metatypes (arg-info-metatypes arg-info)))
1881 (values (arg-info-applyp arg-info)
1884 (values (length metatypes) applyp metatypes
1885 (count-if (lambda (x) (neq x t)) metatypes)
1888 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
1889 &optional slot-name)
1890 (initialize-method-function initargs)
1893 ;; Figure out whether we got class objects or class names as the
1894 ;; specializers and set parsed and unparsed appropriately. If we
1895 ;; got class objects, then we can compute unparsed, but if we got
1896 ;; class names we don't try to compute parsed.
1898 ;; Note that the use of not symbolp in this call to every should be
1899 ;; read as 'classp' we can't use classp itself because it doesn't
1901 (if (every (lambda (s) (not (symbolp s))) specializers)
1902 (setq parsed specializers
1903 unparsed (mapcar (lambda (s)
1904 (if (eq s t) t (class-name s)))
1906 (setq unparsed specializers
1908 (list :early-method ;This is an early method dammit!
1910 (getf initargs :function)
1911 (getf initargs :fast-function)
1913 parsed ;The parsed specializers. This is used
1914 ;by early-method-specializers to cache
1915 ;the parse. Note that this only comes
1916 ;into play when there is more than one
1917 ;early method on an early gf.
1919 (list class ;A list to which real-make-a-method
1920 qualifiers ;can be applied to make a real method
1921 arglist ;corresponding to this early one.
1927 (defun real-make-a-method
1928 (class qualifiers lambda-list specializers initargs doc
1929 &optional slot-name)
1930 (setq specializers (parse-specializers specializers))
1931 (apply #'make-instance class
1932 :qualifiers qualifiers
1933 :lambda-list lambda-list
1934 :specializers specializers
1936 :slot-name slot-name
1940 (defun early-method-function (early-method)
1941 (values (cadr early-method) (caddr early-method)))
1943 (defun early-method-class (early-method)
1944 (find-class (car (fifth early-method))))
1946 (defun early-method-standard-accessor-p (early-method)
1947 (let ((class (first (fifth early-method))))
1948 (or (eq class 'standard-reader-method)
1949 (eq class 'standard-writer-method)
1950 (eq class 'standard-boundp-method))))
1952 (defun early-method-standard-accessor-slot-name (early-method)
1953 (seventh (fifth early-method)))
1955 ;;; Fetch the specializers of an early method. This is basically just
1956 ;;; a simple accessor except that when the second argument is t, this
1957 ;;; converts the specializers from symbols into class objects. The
1958 ;;; class objects are cached in the early method, this makes
1959 ;;; bootstrapping faster because the class objects only have to be
1963 ;;; The second argument should only be passed as T by
1964 ;;; early-lookup-method. This is to implement the rule that only when
1965 ;;; there is more than one early method on a generic function is the
1966 ;;; conversion from class names to class objects done. This
1967 ;;; corresponds to the fact that we are only allowed to have one
1968 ;;; method on any generic function up until the time classes exist.
1969 (defun early-method-specializers (early-method &optional objectsp)
1970 (if (and (listp early-method)
1971 (eq (car early-method) :early-method))
1972 (cond ((eq objectsp t)
1973 (or (fourth early-method)
1974 (setf (fourth early-method)
1975 (mapcar #'find-class (cadddr (fifth early-method))))))
1977 (cadddr (fifth early-method))))
1978 (error "~S is not an early-method." early-method)))
1980 (defun early-method-qualifiers (early-method)
1981 (cadr (fifth early-method)))
1983 (defun early-method-lambda-list (early-method)
1984 (caddr (fifth early-method)))
1986 (defun early-add-named-method (generic-function-name
1991 (let* ((gf (ensure-generic-function generic-function-name))
1993 (dolist (m (early-gf-methods gf))
1994 (when (and (equal (early-method-specializers m) specializers)
1995 (equal (early-method-qualifiers m) qualifiers))
1997 (new (make-a-method 'standard-method
2003 (when existing (remove-method gf existing))
2004 (add-method gf new)))
2006 ;;; This is the early version of ADD-METHOD. Later this will become a
2007 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2008 ;;; special knowledge about ADD-METHOD.
2009 (defun add-method (generic-function method)
2010 (when (not (fsc-instance-p generic-function))
2011 (error "Early ADD-METHOD didn't get a funcallable instance."))
2012 (when (not (and (listp method) (eq (car method) :early-method)))
2013 (error "Early ADD-METHOD didn't get an early method."))
2014 (push method (early-gf-methods generic-function))
2015 (set-arg-info generic-function :new-method method)
2016 (unless (assoc (!early-gf-name generic-function)
2017 *!generic-function-fixups*
2019 (update-dfun generic-function)))
2021 ;;; This is the early version of REMOVE-METHOD. See comments on
2022 ;;; the early version of ADD-METHOD.
2023 (defun remove-method (generic-function method)
2024 (when (not (fsc-instance-p generic-function))
2025 (error "An early remove-method didn't get a funcallable instance."))
2026 (when (not (and (listp method) (eq (car method) :early-method)))
2027 (error "An early remove-method didn't get an early method."))
2028 (setf (early-gf-methods generic-function)
2029 (remove method (early-gf-methods generic-function)))
2030 (set-arg-info generic-function)
2031 (unless (assoc (!early-gf-name generic-function)
2032 *!generic-function-fixups*
2034 (update-dfun generic-function)))
2036 ;;; This is the early version of GET-METHOD. See comments on the early
2037 ;;; version of ADD-METHOD.
2038 (defun get-method (generic-function qualifiers specializers
2039 &optional (errorp t))
2040 (if (early-gf-p generic-function)
2041 (or (dolist (m (early-gf-methods generic-function))
2042 (when (and (or (equal (early-method-specializers m nil)
2044 (equal (early-method-specializers m t)
2046 (equal (early-method-qualifiers m) qualifiers))
2049 (error "can't get early method")
2051 (real-get-method generic-function qualifiers specializers errorp)))
2053 (defun !fix-early-generic-functions ()
2054 (let ((accessors nil))
2055 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2056 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2057 (dolist (early-gf-spec *!early-generic-functions*)
2058 (when (every #'early-method-standard-accessor-p
2059 (early-gf-methods (gdefinition early-gf-spec)))
2060 (push early-gf-spec accessors)))
2061 (dolist (spec (nconc accessors
2062 '(accessor-method-slot-name
2063 generic-function-methods
2068 slot-definition-location
2069 slot-definition-name
2072 class-precedence-list
2073 slot-boundp-using-class
2074 (setf slot-value-using-class)
2075 slot-value-using-class
2078 funcallable-standard-class-p
2081 (setq *!early-generic-functions*
2083 (delete spec *!early-generic-functions* :test #'equal))))
2085 (dolist (early-gf-spec *!early-generic-functions*)
2086 (/show early-gf-spec)
2087 (let* ((gf (gdefinition early-gf-spec))
2088 (methods (mapcar (lambda (early-method)
2089 (let ((args (copy-list (fifth
2092 (early-method-specializers
2094 (apply #'real-make-a-method args)))
2095 (early-gf-methods gf))))
2096 (setf (generic-function-method-class gf) *the-class-standard-method*)
2097 (setf (generic-function-method-combination gf)
2098 *standard-method-combination*)
2099 (set-methods gf methods)))
2101 (dolist (fn *!early-functions*)
2103 (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2105 (dolist (fixup *!generic-function-fixups*)
2107 (let* ((fspec (car fixup))
2108 (gf (gdefinition fspec))
2109 (methods (mapcar (lambda (method)
2110 (let* ((lambda-list (first method))
2111 (specializers (second method))
2112 (method-fn-name (third method))
2113 (fn-name (or method-fn-name fspec))
2114 (fn (fdefinition fn-name))
2118 (lambda (args next-methods)
2122 `(call ,fn-name)))))
2123 (declare (type function fn))
2124 (make-a-method 'standard-method
2131 (setf (generic-function-method-class gf) *the-class-standard-method*)
2132 (setf (generic-function-method-combination gf)
2133 *standard-method-combination*)
2134 (set-methods gf methods))))
2135 (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2137 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2138 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2139 ;;; is really implemented.
2140 (defun parse-defmethod (cdr-of-form)
2141 (declare (list cdr-of-form))
2142 (let ((name (pop cdr-of-form))
2145 (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2146 (push (pop cdr-of-form) qualifiers)
2147 (return (setq qualifiers (nreverse qualifiers)))))
2148 (setq spec-ll (pop cdr-of-form))
2149 (values name qualifiers spec-ll cdr-of-form)))
2151 (defun parse-specializers (specializers)
2152 (declare (list specializers))
2153 (flet ((parse (spec)
2154 (let ((result (specializer-from-type spec)))
2155 (if (specializerp result)
2158 (error "~S was used as a specializer,~%~
2159 but is not the name of a class."
2161 (error "~S is not a legal specializer." spec))))))
2162 (mapcar #'parse specializers)))
2164 (defun unparse-specializers (specializers-or-method)
2165 (if (listp specializers-or-method)
2166 (flet ((unparse (spec)
2167 (if (specializerp spec)
2168 (let ((type (specializer-type spec)))
2169 (if (and (consp type)
2170 (eq (car type) 'class))
2171 (let* ((class (cadr type))
2172 (class-name (class-name class)))
2173 (if (eq class (find-class class-name nil))
2177 (error "~S is not a legal specializer." spec))))
2178 (mapcar #'unparse specializers-or-method))
2179 (unparse-specializers (method-specializers specializers-or-method))))
2181 (defun parse-method-or-spec (spec &optional (errorp t))
2182 (let (gf method name temp)
2185 gf (method-generic-function method)
2186 temp (and gf (generic-function-name gf))
2189 (make-method-spec temp
2190 (method-qualifiers method)
2191 (unparse-specializers
2192 (method-specializers method))))
2193 (make-symbol (format nil "~S" method))))
2194 (multiple-value-bind (gf-spec quals specls)
2195 (parse-defmethod spec)
2196 (and (setq gf (and (or errorp (gboundp gf-spec))
2197 (gdefinition gf-spec)))
2198 (let ((nreq (compute-discriminating-function-arglist-info gf)))
2199 (setq specls (append (parse-specializers specls)
2200 (make-list (- nreq (length specls))
2204 (setq method (get-method gf quals specls errorp))
2206 (intern-fun-name (make-method-spec gf-spec
2209 (values gf method name)))
2211 (defun extract-parameters (specialized-lambda-list)
2212 (multiple-value-bind (parameters ignore1 ignore2)
2213 (parse-specialized-lambda-list specialized-lambda-list)
2214 (declare (ignore ignore1 ignore2))
2217 (defun extract-lambda-list (specialized-lambda-list)
2218 (multiple-value-bind (ignore1 lambda-list ignore2)
2219 (parse-specialized-lambda-list specialized-lambda-list)
2220 (declare (ignore ignore1 ignore2))
2223 (defun extract-specializer-names (specialized-lambda-list)
2224 (multiple-value-bind (ignore1 ignore2 specializers)
2225 (parse-specialized-lambda-list specialized-lambda-list)
2226 (declare (ignore ignore1 ignore2))
2229 (defun extract-required-parameters (specialized-lambda-list)
2230 (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2231 (parse-specialized-lambda-list specialized-lambda-list)
2232 (declare (ignore ignore1 ignore2 ignore3))
2233 required-parameters))
2235 (defun parse-specialized-lambda-list (arglist &optional post-keyword)
2236 ;;(declare (values parameters lambda-list specializers required-parameters))
2237 (let ((arg (car arglist)))
2238 (cond ((null arglist) (values nil nil nil nil))
2240 (values nil arglist nil))
2241 ((memq arg lambda-list-keywords)
2242 (unless (memq arg '(&optional &rest &key &allow-other-keys &aux))
2243 ;; Now, since we try to conform to ANSI, non-standard
2244 ;; lambda-list-keywords should be treated as errors.
2245 (error 'simple-program-error
2246 :format-control "unrecognized lambda-list keyword ~S ~
2248 :format-arguments (list arg)))
2249 ;; When we are at a lambda-list keyword, the parameters
2250 ;; don't include the lambda-list keyword; the lambda-list
2251 ;; does include the lambda-list keyword; and no
2252 ;; specializers are allowed to follow the lambda-list
2253 ;; keywords (at least for now).
2254 (multiple-value-bind (parameters lambda-list)
2255 (parse-specialized-lambda-list (cdr arglist) t)
2256 (when (eq arg '&rest)
2257 ;; check, if &rest is followed by a var ...
2258 (when (or (null lambda-list)
2259 (memq (car lambda-list) lambda-list-keywords))
2260 (error "Error in lambda-list:~%~
2261 After &REST, a DEFMETHOD lambda-list ~
2262 must be followed by at least one variable.")))
2264 (cons arg lambda-list)
2268 ;; After a lambda-list keyword there can be no specializers.
2269 (multiple-value-bind (parameters lambda-list)
2270 (parse-specialized-lambda-list (cdr arglist) t)
2271 (values (cons (if (listp arg) (car arg) arg) parameters)
2272 (cons arg lambda-list)
2276 (multiple-value-bind (parameters lambda-list specializers required)
2277 (parse-specialized-lambda-list (cdr arglist))
2278 (values (cons (if (listp arg) (car arg) arg) parameters)
2279 (cons (if (listp arg) (car arg) arg) lambda-list)
2280 (cons (if (listp arg) (cadr arg) t) specializers)
2281 (cons (if (listp arg) (car arg) arg) required)))))))
2283 (setq *boot-state* 'early)
2285 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2286 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2287 ;;; walker stuff was only used for implementing stuff like that; maybe
2288 ;;; it's not needed any more? Hunt down what it was used for and see.
2290 (defmacro with-slots (slots instance &body body)
2291 (let ((in (gensym)))
2292 `(let ((,in ,instance))
2293 (declare (ignorable ,in))
2294 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2297 (and (symbolp instance)
2298 `((declare (%variable-rebinding ,in ,instance)))))
2300 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2302 (if (symbolp slot-entry)
2306 (if (symbolp slot-entry)
2308 (cadr slot-entry))))
2310 (slot-value ,in ',slot-name))))
2314 (defmacro with-accessors (slots instance &body body)
2315 (let ((in (gensym)))
2316 `(let ((,in ,instance))
2317 (declare (ignorable ,in))
2318 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2321 (and (symbolp instance)
2322 `((declare (%variable-rebinding ,in ,instance)))))
2324 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2325 (let ((var-name (car slot-entry))
2326 (accessor-name (cadr slot-entry)))
2327 `(,var-name (,accessor-name ,in))))