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 some special limitations, which we check here.
238 (defun check-gf-lambda-list (lambda-list)
239 (flet ((ensure (arg ok)
242 ;; (s/invalid/non-ANSI-conforming/ because the old PCL
243 ;; implementation allowed this, so people got used to
244 ;; it, and maybe this phrasing will help them to guess
245 ;; why their program which worked under PCL no longer works.)
246 "~@<non-ANSI-conforming argument ~S ~_in the generic function lambda list ~S~:>"
248 (multiple-value-bind (required optional restp rest keyp keys allowp
249 auxp aux morep more-context more-count)
250 (parse-lambda-list lambda-list)
251 (declare (ignore required)) ; since they're no different in a gf ll
252 (declare (ignore restp rest)) ; since they're no different in a gf ll
253 (declare (ignore allowp)) ; since &ALLOW-OTHER-KEYS is fine either way
254 (declare (ignore aux)) ; since we require AUXP=NIL
255 (declare (ignore more-context more-count)) ; safely ignored unless MOREP
256 ;; no defaults allowed for &OPTIONAL arguments
258 (ensure i (or (symbolp i)
259 (and (consp i) (symbolp (car i)) (null (cdr i))))))
260 ;; no defaults allowed for &KEY arguments
263 (ensure i (or (symbolp i)
265 (or (symbolp (car i))
273 (error "&AUX is not allowed in a generic function lambda list: ~S"
275 ;; Oh, *puhlease*... not specifically as per section 3.4.2 of
276 ;; the ANSI spec, but the CMU CL &MORE extension does not
278 (aver (not morep)))))
280 (defmacro defmethod (&rest args &environment env)
281 (multiple-value-bind (name qualifiers lambda-list body)
282 (parse-defmethod args)
283 (multiple-value-bind (proto-gf proto-method)
284 (prototypes-for-make-method-lambda name)
285 (expand-defmethod name
293 (defun prototypes-for-make-method-lambda (name)
294 (if (not (eq *boot-state* 'complete))
296 (let ((gf? (and (gboundp name)
297 (gdefinition name))))
299 (not (generic-function-p gf?)))
300 (values (class-prototype (find-class 'standard-generic-function))
301 (class-prototype (find-class 'standard-method)))
303 (class-prototype (or (generic-function-method-class gf?)
304 (find-class 'standard-method))))))))
306 ;;; Take a name which is either a generic function name or a list specifying
307 ;;; a SETF generic function (like: (SETF <generic-function-name>)). Return
308 ;;; the prototype instance of the method-class for that generic function.
310 ;;; If there is no generic function by that name, this returns the
311 ;;; default value, the prototype instance of the class
312 ;;; STANDARD-METHOD. This default value is also returned if the spec
313 ;;; names an ordinary function or even a macro. In effect, this leaves
314 ;;; the signalling of the appropriate error until load time.
316 ;;; Note: During bootstrapping, this function is allowed to return NIL.
317 (defun method-prototype-for-gf (name)
318 (let ((gf? (and (gboundp name)
319 (gdefinition name))))
320 (cond ((neq *boot-state* 'complete) nil)
322 (not (generic-function-p gf?))) ; Someone else MIGHT
323 ; error at load time.
324 (class-prototype (find-class 'standard-method)))
326 (class-prototype (or (generic-function-method-class gf?)
327 (find-class 'standard-method)))))))
329 (defvar *optimize-asv-funcall-p* nil)
330 (defvar *asv-readers*)
331 (defvar *asv-writers*)
332 (defvar *asv-boundps*)
334 (defun expand-defmethod (name
341 (let ((*make-instance-function-keys* nil)
342 (*optimize-asv-funcall-p* t)
343 (*asv-readers* nil) (*asv-writers* nil) (*asv-boundps* nil))
344 (declare (special *make-instance-function-keys*))
345 (multiple-value-bind (method-lambda unspecialized-lambda-list specializers)
346 (add-method-declarations name qualifiers lambda-list body env)
347 (multiple-value-bind (method-function-lambda initargs)
348 (make-method-lambda proto-gf proto-method method-lambda env)
349 (let ((initargs-form (make-method-initargs-form proto-gf
351 method-function-lambda
355 ;; Note: We could DECLAIM the ftype of the generic
356 ;; function here, since ANSI specifies that we create it
357 ;; if it does not exist. However, I chose not to, because
358 ;; I think it's more useful to support a style of
359 ;; programming where every generic function has an
360 ;; explicit DEFGENERIC and any typos in DEFMETHODs are
361 ;; warned about. Otherwise
362 ;; (DEFGENERIC FOO-BAR-BLETCH ((X T)))
363 ;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
364 ;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
365 ;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
366 ;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
367 ;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
368 ;; compiles without raising an error and runs without
369 ;; raising an error (since SIMPLE-VECTOR cases fall
370 ;; through to VECTOR) but still doesn't do what was
371 ;; intended. I hate that kind of bug (code which silently
372 ;; gives the wrong answer), so we don't do a DECLAIM
373 ;; here. -- WHN 20000229
374 ,@(when *make-instance-function-keys*
375 `((get-make-instance-functions
376 ',*make-instance-function-keys*)))
377 ,@(when (or *asv-readers* *asv-writers* *asv-boundps*)
378 `((initialize-internal-slot-gfs*
379 ',*asv-readers* ',*asv-writers* ',*asv-boundps*)))
380 ,(make-defmethod-form name qualifiers specializers
381 unspecialized-lambda-list
383 (class-name (class-of proto-method))
386 (getf (getf initargs :plist)
387 :pv-table-symbol))))))))
389 (defun interned-symbol-p (x)
390 (and (symbolp x) (symbol-package x)))
392 (defun make-defmethod-form (name qualifiers specializers
393 unspecialized-lambda-list method-class-name
394 initargs-form &optional pv-table-symbol)
397 (if (and (interned-symbol-p (fun-name-block-name name))
398 (every #'interned-symbol-p qualifiers)
401 (and (eq (car s) 'eql)
403 (let ((sv (eval (cadr s))))
404 (or (interned-symbol-p sv)
407 (standard-char-p sv)))))
408 (interned-symbol-p s)))
410 (consp initargs-form)
411 (eq (car initargs-form) 'list*)
412 (memq (cadr initargs-form) '(:function :fast-function))
413 (consp (setq fn (caddr initargs-form)))
414 (eq (car fn) 'function)
415 (consp (setq fn-lambda (cadr fn)))
416 (eq (car fn-lambda) 'lambda))
417 (let* ((specls (mapcar (lambda (specl)
419 `(,(car specl) ,(eval (cadr specl)))
422 (mname `(,(if (eq (cadr initargs-form) :function)
423 'method 'fast-method)
424 ,name ,@qualifiers ,specls))
425 (mname-sym (intern (let ((*print-pretty* nil)
426 ;; (We bind *PACKAGE* to
427 ;; KEYWORD here as a way to
428 ;; force symbols to be printed
429 ;; with explicit package
431 (*package* *keyword-package*))
432 (format nil "~S" mname)))))
434 (defun ,mname-sym ,(cadr fn-lambda)
436 ,(make-defmethod-form-internal
437 name qualifiers `',specls
438 unspecialized-lambda-list method-class-name
439 `(list* ,(cadr initargs-form)
441 ,@(cdddr initargs-form))
443 (make-defmethod-form-internal
445 `(list ,@(mapcar (lambda (specializer)
446 (if (consp specializer)
447 ``(,',(car specializer)
448 ,,(cadr specializer))
451 unspecialized-lambda-list
456 (defun make-defmethod-form-internal
457 (name qualifiers specializers-form unspecialized-lambda-list
458 method-class-name initargs-form &optional pv-table-symbol)
464 ',unspecialized-lambda-list
466 ;; Paper over a bug in KCL by passing the cache-symbol here in
467 ;; addition to in the list. FIXME: We should no longer need to do
468 ;; this, since the CLOS code is now SBCL-specific, and doesn't
469 ;; need to be ported to every buggy compiler in existence.
472 (defmacro make-method-function (method-lambda &environment env)
473 (make-method-function-internal method-lambda env))
475 (defun make-method-function-internal (method-lambda &optional env)
476 (multiple-value-bind (proto-gf proto-method)
477 (prototypes-for-make-method-lambda nil)
478 (multiple-value-bind (method-function-lambda initargs)
479 (make-method-lambda proto-gf proto-method method-lambda env)
480 (make-method-initargs-form proto-gf
482 method-function-lambda
486 (defun add-method-declarations (name qualifiers lambda-list body env)
487 (multiple-value-bind (parameters unspecialized-lambda-list specializers)
488 (parse-specialized-lambda-list lambda-list)
489 (declare (ignore parameters))
490 (multiple-value-bind (real-body declarations documentation)
491 (parse-body body env)
492 (values `(lambda ,unspecialized-lambda-list
493 ,@(when documentation `(,documentation))
494 ;; (Old PCL code used a somewhat different style of
495 ;; list for %METHOD-NAME values. Our names use
496 ;; ,@QUALIFIERS instead of ,QUALIFIERS so that the
497 ;; method names look more like what you see in a
500 ;; FIXME: As of sbcl-0.7.0.6, code elsewhere, at
501 ;; least the code to set up named BLOCKs around the
502 ;; bodies of methods, depends on the function's base
503 ;; name being the first element of the %METHOD-NAME
504 ;; list. It would be good to remove this dependency,
505 ;; perhaps by building the BLOCK here, or by using
506 ;; another declaration (e.g. %BLOCK-NAME), so that
507 ;; our method debug names are free to have any format,
508 ;; e.g. (:METHOD PRINT-OBJECT :AROUND (CLOWN T)).
509 (declare (%method-name (,name
512 (declare (%method-lambda-list ,@lambda-list))
515 unspecialized-lambda-list specializers))))
517 (defun real-make-method-initargs-form (proto-gf proto-method
518 method-lambda initargs env)
519 (declare (ignore proto-gf proto-method))
520 (unless (and (consp method-lambda)
521 (eq (car method-lambda) 'lambda))
522 (error "The METHOD-LAMBDA argument to MAKE-METHOD-FUNCTION, ~S, ~
523 is not a lambda form."
525 (make-method-initargs-form-internal method-lambda initargs env))
527 (unless (fboundp 'make-method-initargs-form)
528 (setf (gdefinition 'make-method-initargs-form)
529 (symbol-function 'real-make-method-initargs-form)))
531 (defun real-make-method-lambda (proto-gf proto-method method-lambda env)
532 (declare (ignore proto-gf proto-method))
533 (make-method-lambda-internal method-lambda env))
535 ;;; a helper function for creating Python-friendly type declarations
536 ;;; in DEFMETHOD forms
537 (defun parameter-specializer-declaration-in-defmethod (parameter specializer)
538 (cond ((and (consp specializer)
539 (eq (car specializer) 'eql))
540 ;; KLUDGE: ANSI, in its wisdom, says that
541 ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at
542 ;; DEFMETHOD expansion time. Thus, although one might think
544 ;; (DEFMETHOD FOO ((X PACKAGE)
547 ;; the PACKAGE and (EQL 12) forms are both parallel type
548 ;; names, they're not, as is made clear when you do
549 ;; (DEFMETHOD FOO ((X PACKAGE)
552 ;; where Y needs to be a symbol named "BAR", not some cons
553 ;; made by (CONS 'QUOTE 'BAR). I.e. when the
554 ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument
555 ;; to be of type (EQL X). It'd be easy to transform one to
556 ;; the other, but it'd be somewhat messier to do so while
557 ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd
558 ;; once. (The new code wouldn't be messy, but it'd require a
559 ;; big transformation of the old code.) So instead we punt.
563 ;; KLUDGE: For some low-level implementation
564 ;; classes, perhaps because of some problems related
565 ;; to the incomplete integration of PCL into SBCL's
566 ;; type system, some specializer classes can't be
567 ;; declared as argument types. E.g.
568 ;; (DEFMETHOD FOO ((X SLOT-OBJECT))
569 ;; (DECLARE (TYPE SLOT-OBJECT X))
572 ;; (DEFSTRUCT BAR A B)
574 ;; perhaps because of the way that STRUCTURE-OBJECT
575 ;; inherits both from SLOT-OBJECT and from
576 ;; SB-KERNEL:INSTANCE. In an effort to sweep such
577 ;; problems under the rug, we exclude these problem
578 ;; cases by blacklisting them here. -- WHN 2001-01-19
581 ((not (eq *boot-state* 'complete))
582 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
583 ;; types which don't match their specializers. (Specifically,
584 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
585 ;; second argument.) Hopefully it only does this kind of
586 ;; weirdness when bootstrapping.. -- WHN 20000610
589 ;; Otherwise, we can make Python very happy.
590 `(type ,specializer ,parameter))))
592 (defun make-method-lambda-internal (method-lambda &optional env)
593 (unless (and (consp method-lambda) (eq (car method-lambda) 'lambda))
594 (error "The METHOD-LAMBDA argument to MAKE-METHOD-LAMBDA, ~S, ~
595 is not a lambda form."
597 (multiple-value-bind (real-body declarations documentation)
598 (parse-body (cddr method-lambda) env)
599 (let* ((name-decl (get-declaration '%method-name declarations))
600 (sll-decl (get-declaration '%method-lambda-list declarations))
601 (method-name (when (consp name-decl) (car name-decl)))
602 (generic-function-name (when method-name (car method-name)))
603 (specialized-lambda-list (or sll-decl (cadr method-lambda))))
604 (multiple-value-bind (parameters lambda-list specializers)
605 (parse-specialized-lambda-list specialized-lambda-list)
606 (let* ((required-parameters
607 (mapcar (lambda (r s) (declare (ignore s)) r)
610 (slots (mapcar #'list required-parameters))
614 ;; These declarations seem to be used by PCL to pass
615 ;; information to itself; when I tried to delete 'em
616 ;; ca. 0.6.10 it didn't work. I'm not sure how
617 ;; they work, but note the (VAR-DECLARATION '%CLASS ..)
618 ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30
620 (mapcar (lambda (a s) (and (symbolp s)
625 ;; These TYPE declarations weren't in the original
626 ;; PCL code, but the Python compiler likes them a
627 ;; lot. (We're telling the compiler about our
628 ;; knowledge of specialized argument types so that
629 ;; it can avoid run-time type dispatch overhead,
630 ;; which can be a huge win for Python.)
632 ;; FIXME: Perhaps these belong in
633 ;; ADD-METHOD-DECLARATIONS instead of here?
634 ,@(mapcar #'parameter-specializer-declaration-in-defmethod
638 ;; Remove the documentation string and insert the
639 ;; appropriate class declarations. The documentation
640 ;; string is removed to make it easy for us to insert
641 ;; new declarations later, they will just go after the
642 ;; CADR of the method lambda. The class declarations
643 ;; are inserted to communicate the class of the method's
644 ;; arguments to the code walk.
645 `(lambda ,lambda-list
646 ;; The default ignorability of method parameters
647 ;; doesn't seem to be specified by ANSI. PCL had
648 ;; them basically ignorable but was a little
649 ;; inconsistent. E.g. even though the two
650 ;; method definitions
651 ;; (DEFMETHOD FOO ((X T) (Y T)) "Z")
652 ;; (DEFMETHOD FOO ((X T) Y) "Z")
653 ;; are otherwise equivalent, PCL treated Y as
654 ;; ignorable in the first definition but not in the
655 ;; second definition. We make all required
656 ;; parameters ignorable as a way of systematizing
657 ;; the old PCL behavior. -- WHN 2000-11-24
658 (declare (ignorable ,@required-parameters))
661 (block ,(fun-name-block-name generic-function-name)
663 (constant-value-p (and (null (cdr real-body))
664 (constantp (car real-body))))
665 (constant-value (and constant-value-p
666 (eval (car real-body))))
667 (plist (and constant-value-p
668 (or (typep constant-value
669 '(or number character))
670 (and (symbolp constant-value)
671 (symbol-package constant-value)))
672 (list :constant-value constant-value)))
673 (applyp (dolist (p lambda-list nil)
674 (cond ((memq p '(&optional &rest &key))
679 (walked-lambda call-next-method-p closurep next-method-p-p)
680 (walk-method-lambda method-lambda
685 (multiple-value-bind (walked-lambda-body
687 walked-documentation)
688 (parse-body (cddr walked-lambda) env)
689 (declare (ignore walked-documentation))
690 (when (or next-method-p-p call-next-method-p)
691 (setq plist (list* :needs-next-methods-p t plist)))
692 (when (some #'cdr slots)
693 (multiple-value-bind (slot-name-lists call-list)
694 (slot-name-lists-from-slots slots calls)
695 (let ((pv-table-symbol (make-symbol "pv-table")))
697 `(,@(when slot-name-lists
698 `(:slot-name-lists ,slot-name-lists))
700 `(:call-list ,call-list))
701 :pv-table-symbol ,pv-table-symbol
703 (setq walked-lambda-body
704 `((pv-binding (,required-parameters
707 ,@walked-lambda-body))))))
708 (when (and (memq '&key lambda-list)
709 (not (memq '&allow-other-keys lambda-list)))
710 (let ((aux (memq '&aux lambda-list)))
711 (setq lambda-list (nconc (ldiff lambda-list aux)
712 (list '&allow-other-keys)
714 (values `(lambda (.method-args. .next-methods.)
715 (simple-lexical-method-functions
716 (,lambda-list .method-args. .next-methods.
719 :next-method-p-p ,next-method-p-p
722 ,@walked-declarations
723 ,@walked-lambda-body))
726 ,@(when documentation
727 `(:documentation ,documentation)))))))))))
729 (unless (fboundp 'make-method-lambda)
730 (setf (gdefinition 'make-method-lambda)
731 (symbol-function 'real-make-method-lambda)))
733 (defmacro simple-lexical-method-functions ((lambda-list
739 ,method-args ,next-methods
740 (bind-simple-lexical-method-macros (,method-args ,next-methods)
741 (bind-lexical-method-functions (,@lmf-options)
742 (bind-args (,lambda-list ,method-args)
745 (defmacro fast-lexical-method-functions ((lambda-list
751 `(bind-fast-lexical-method-macros (,args ,rest-arg ,next-method-call)
752 (bind-lexical-method-functions (,@lmf-options)
753 (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg)
756 (defmacro bind-simple-lexical-method-macros ((method-args next-methods)
758 `(macrolet ((call-next-method-bind (&body body)
759 `(let ((.next-method. (car ,',next-methods))
760 (,',next-methods (cdr ,',next-methods)))
761 .next-method. ,',next-methods
763 (call-next-method-body (cnm-args)
765 (funcall (if (std-instance-p .next-method.)
766 (method-function .next-method.)
767 .next-method.) ; for early methods
768 (or ,cnm-args ,',method-args)
770 (error "no next method")))
771 (next-method-p-body ()
772 `(not (null .next-method.))))
775 (defstruct (method-call (:copier nil))
776 (function #'identity :type function)
779 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
781 (defmacro invoke-method-call1 (function args cm-args)
782 `(let ((.function. ,function)
784 (.cm-args. ,cm-args))
785 (if (and .cm-args. (null (cdr .cm-args.)))
786 (funcall .function. .args. (car .cm-args.))
787 (apply .function. .args. .cm-args.))))
789 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
790 `(invoke-method-call1 (method-call-function ,method-call)
792 `(list* ,@required-args+rest-arg)
793 `(list ,@required-args+rest-arg))
794 (method-call-call-method-args ,method-call)))
796 (defstruct (fast-method-call (:copier nil))
797 (function #'identity :type function)
802 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
804 (defmacro fmc-funcall (fn pv-cell next-method-call &rest args)
805 `(funcall ,fn ,pv-cell ,next-method-call ,@args))
807 (defmacro invoke-fast-method-call (method-call &rest required-args+rest-arg)
808 `(fmc-funcall (fast-method-call-function ,method-call)
809 (fast-method-call-pv-cell ,method-call)
810 (fast-method-call-next-method-call ,method-call)
811 ,@required-args+rest-arg))
813 (defstruct (fast-instance-boundp (:copier nil))
814 (index 0 :type fixnum))
816 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
818 (eval-when (:compile-toplevel :load-toplevel :execute)
819 (defvar *allow-emf-call-tracing-p* nil)
820 (defvar *enable-emf-call-tracing-p* #-sb-show nil #+sb-show t))
822 ;;;; effective method functions
824 (defvar *emf-call-trace-size* 200)
825 (defvar *emf-call-trace* nil)
826 (defvar *emf-call-trace-index* 0)
828 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
829 ;;; without explanation. It appears to be intended for debugging, so
830 ;;; it might be useful someday, so I haven't deleted it.
831 ;;; But it isn't documented and isn't used for anything now, so
832 ;;; I've conditionalized it out of the base system. -- WHN 19991213
834 (defun show-emf-call-trace ()
835 (when *emf-call-trace*
836 (let ((j *emf-call-trace-index*)
837 (*enable-emf-call-tracing-p* nil))
838 (format t "~&(The oldest entries are printed first)~%")
839 (dotimes-fixnum (i *emf-call-trace-size*)
840 (let ((ct (aref *emf-call-trace* j)))
841 (when ct (print ct)))
843 (when (= j *emf-call-trace-size*)
846 (defun trace-emf-call-internal (emf format args)
847 (unless *emf-call-trace*
848 (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
849 (setf (aref *emf-call-trace* *emf-call-trace-index*)
850 (list* emf format args))
851 (incf *emf-call-trace-index*)
852 (when (= *emf-call-trace-index* *emf-call-trace-size*)
853 (setq *emf-call-trace-index* 0)))
855 (defmacro trace-emf-call (emf format args)
856 (when *allow-emf-call-tracing-p*
857 `(when *enable-emf-call-tracing-p*
858 (trace-emf-call-internal ,emf ,format ,args))))
860 (defmacro invoke-effective-method-function-fast
861 (emf restp &rest required-args+rest-arg)
863 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
864 (invoke-fast-method-call ,emf ,@required-args+rest-arg)))
866 (defmacro invoke-effective-method-function (emf restp
867 &rest required-args+rest-arg)
868 (unless (constantp restp)
869 (error "The RESTP argument is not constant."))
870 ;; FIXME: The RESTP handling here is confusing and maybe slightly
871 ;; broken if RESTP evaluates to a non-self-evaluating form. E.g. if
872 ;; (INVOKE-EFFECTIVE-METHOD-FUNCTION EMF '(ERROR "gotcha") ...)
873 ;; then TRACE-EMF-CALL-CALL-INTERNAL might die on a gotcha error.
874 (setq restp (eval restp))
876 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
877 (cond ((typep ,emf 'fast-method-call)
878 (invoke-fast-method-call ,emf ,@required-args+rest-arg))
879 ;; "What," you may wonder, "do these next two clauses do?"
880 ;; In that case, you are not a PCL implementor, for they
881 ;; considered this to be self-documenting.:-| Or CSR, for
882 ;; that matter, since he can also figure it out by looking
883 ;; at it without breaking stride. For the rest of us,
884 ;; though: From what the code is doing with .SLOTS. and
885 ;; whatnot, evidently it's implementing SLOT-VALUEish and
886 ;; GET-SLOT-VALUEish things. Then we can reason backwards
887 ;; and conclude that setting EMF to a FIXNUM is an
888 ;; optimized way to represent these slot access operations.
889 ,@(when (and (null restp) (= 1 (length required-args+rest-arg)))
890 `(((typep ,emf 'fixnum)
891 (let* ((.slots. (get-slots-or-nil
892 ,(car required-args+rest-arg)))
893 (value (when .slots. (clos-slots-ref .slots. ,emf))))
894 (if (eq value +slot-unbound+)
895 (slot-unbound-internal ,(car required-args+rest-arg)
898 ,@(when (and (null restp) (= 2 (length required-args+rest-arg)))
899 `(((typep ,emf 'fixnum)
900 (let ((.new-value. ,(car required-args+rest-arg))
901 (.slots. (get-slots-or-nil
902 ,(car required-args+rest-arg))))
904 (setf (clos-slots-ref .slots. ,emf) .new-value.))))))
905 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
906 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
907 ;; there was no explanation and presumably the code is 10+
908 ;; years stale, I simply deleted it. -- WHN)
912 (invoke-method-call ,emf ,restp ,@required-args+rest-arg))
915 `(apply (the function ,emf) ,@required-args+rest-arg)
916 `(funcall (the function ,emf)
917 ,@required-args+rest-arg))))))))
919 (defun invoke-emf (emf args)
920 (trace-emf-call emf t args)
923 (let* ((arg-info (fast-method-call-arg-info emf))
924 (restp (cdr arg-info))
925 (nreq (car arg-info)))
927 (let* ((rest-args (nthcdr nreq args))
928 (req-args (ldiff args rest-args)))
929 (apply (fast-method-call-function emf)
930 (fast-method-call-pv-cell emf)
931 (fast-method-call-next-method-call emf)
932 (nconc req-args (list rest-args))))
935 (invoke-fast-method-call emf)
936 (error "wrong number of args")))
939 (invoke-fast-method-call emf (car args))
940 (error "wrong number of args")))
943 (invoke-fast-method-call emf (car args) (cadr args))
944 (error "wrong number of args")))
946 (apply (fast-method-call-function emf)
947 (fast-method-call-pv-cell emf)
948 (fast-method-call-next-method-call emf)
951 (apply (method-call-function emf)
953 (method-call-call-method-args emf)))
955 (cond ((null args) (error "1 or 2 args were expected."))
957 (let* ((slots (get-slots (car args)))
958 (value (clos-slots-ref slots emf)))
959 (if (eq value +slot-unbound+)
960 (slot-unbound-internal (car args) emf)
963 (setf (clos-slots-ref (get-slots (cadr args)) emf)
965 (t (error "1 or 2 args were expected."))))
966 (fast-instance-boundp
967 (if (or (null args) (cdr args))
968 (error "1 arg was expected.")
969 (let ((slots (get-slots (car args))))
970 (not (eq (clos-slots-ref slots
971 (fast-instance-boundp-index emf))
976 (defmacro bind-fast-lexical-method-macros ((args rest-arg next-method-call)
978 `(macrolet ((narrowed-emf (emf)
979 ;; INVOKE-EFFECTIVE-METHOD-FUNCTION has code in it to
980 ;; dispatch on the possibility that EMF might be of
981 ;; type FIXNUM (as an optimized representation of a
982 ;; slot accessor). But as far as I (WHN 2002-06-11)
983 ;; can tell, it's impossible for such a representation
984 ;; to end up as .NEXT-METHOD-CALL. By reassuring
985 ;; INVOKE-E-M-F that when called from this context
986 ;; it needn't worry about the FIXNUM case, we can
987 ;; keep those cases from being compiled, which is
988 ;; good both because it saves bytes and because it
989 ;; avoids annoying type mismatch compiler warnings.
991 ;; KLUDGE: In sbcl-0.7.4.29, the compiler's type
992 ;; system isn't smart enough about NOT and intersection
993 ;; types to benefit from a (NOT FIXNUM) declaration
994 ;; here. -- WHN 2002-06-12
996 ;; FIXME: Might the FUNCTION type be omittable here,
997 ;; leaving only METHOD-CALLs? Failing that, could this
998 ;; be documented somehow? (It'd be nice if the types
999 ;; involved could be understood without solving the
1000 ;; halting problem.)
1001 `(the (or function method-call fast-method-call)
1003 (call-next-method-bind (&body body)
1005 (call-next-method-body (cnm-args)
1006 `(if ,',next-method-call
1008 ;; This declaration suppresses a "deleting
1009 ;; unreachable code" note for the following IF when
1010 ;; REST-ARG is NIL. It is not nice for debugging
1011 ;; SBCL itself, but at least it keeps us from
1013 (declare (optimize (inhibit-warnings 3)))
1014 (if (and (null ',rest-arg)
1016 (eq (car cnm-args) 'list))
1017 `(invoke-effective-method-function
1018 (narrowed-emf ,',next-method-call)
1021 (let ((call `(invoke-effective-method-function
1022 (narrowed-emf ,',next-method-call)
1023 ,',(not (null rest-arg))
1025 ,@',(when rest-arg `(,rest-arg)))))
1027 (bind-args ((,@',args
1029 `(&rest ,rest-arg)))
1033 (error "no next method")))
1034 (next-method-p-body ()
1035 `(not (null ,',next-method-call))))
1038 (defmacro bind-lexical-method-functions
1039 ((&key call-next-method-p next-method-p-p closurep applyp)
1041 (cond ((and (null call-next-method-p) (null next-method-p-p)
1045 ((and (null closurep)
1047 ;; OK to use MACROLET, and all args are mandatory
1048 ;; (else APPLYP would be true).
1049 `(call-next-method-bind
1050 (macrolet ((call-next-method (&rest cnm-args)
1051 `(call-next-method-body ,(when cnm-args
1052 `(list ,@cnm-args))))
1054 `(next-method-p-body)))
1057 `(call-next-method-bind
1058 (flet (,@(and call-next-method-p
1059 '((call-next-method (&rest cnm-args)
1060 (call-next-method-body cnm-args))))
1061 ,@(and next-method-p-p
1063 (next-method-p-body)))))
1066 (defmacro bind-args ((lambda-list args) &body body)
1067 (let ((args-tail '.args-tail.)
1070 (flet ((process-var (var)
1071 (if (memq var lambda-list-keywords)
1074 (&optional (setq state 'optional))
1075 (&key (setq state 'key))
1077 (&rest (setq state 'rest))
1078 (&aux (setq state 'aux))
1081 "encountered the non-standard lambda list keyword ~S"
1085 (required `((,var (pop ,args-tail))))
1086 (optional (cond ((not (consp var))
1087 `((,var (when ,args-tail
1088 (pop ,args-tail)))))
1090 `((,(car var) (if ,args-tail
1094 `((,(caddr var) ,args-tail)
1095 (,(car var) (if ,args-tail
1098 (rest `((,var ,args-tail)))
1099 (key (cond ((not (consp var))
1100 `((,var (get-key-arg ,(keywordicate var)
1103 (multiple-value-bind (keyword variable)
1104 (if (consp (car var))
1107 (values (keywordicate (car var))
1109 `((,key (get-key-arg1 ',keyword ,args-tail))
1110 (,variable (if (consp ,key)
1114 (multiple-value-bind (keyword variable)
1115 (if (consp (car var))
1118 (values (keywordicate (car var))
1120 `((,key (get-key-arg1 ',keyword ,args-tail))
1122 (,variable (if (consp ,key)
1126 (let ((bindings (mapcan #'process-var lambda-list)))
1127 `(let* ((,args-tail ,args)
1129 (declare (ignorable ,args-tail))
1132 (defun get-key-arg (keyword list)
1133 (loop (when (atom list) (return nil))
1134 (when (eq (car list) keyword) (return (cadr list)))
1135 (setq list (cddr list))))
1137 (defun get-key-arg1 (keyword list)
1138 (loop (when (atom list) (return nil))
1139 (when (eq (car list) keyword) (return (cdr list)))
1140 (setq list (cddr list))))
1142 (defun walk-method-lambda (method-lambda required-parameters env slots calls)
1143 (let ((call-next-method-p nil) ; flag indicating that CALL-NEXT-METHOD
1144 ; should be in the method definition
1145 (closurep nil) ; flag indicating that #'CALL-NEXT-METHOD
1146 ; was seen in the body of a method
1147 (next-method-p-p nil)) ; flag indicating that NEXT-METHOD-P
1148 ; should be in the method definition
1149 (flet ((walk-function (form context env)
1150 (cond ((not (eq context :eval)) form)
1151 ;; FIXME: Jumping to a conclusion from the way it's used
1152 ;; above, perhaps CONTEXT should be called SITUATION
1153 ;; (after the term used in the ANSI specification of
1154 ;; EVAL-WHEN) and given modern ANSI keyword values
1155 ;; like :LOAD-TOPLEVEL.
1156 ((not (listp form)) form)
1157 ((eq (car form) 'call-next-method)
1158 (setq call-next-method-p t)
1160 ((eq (car form) 'next-method-p)
1161 (setq next-method-p-p t)
1163 ((and (eq (car form) 'function)
1164 (cond ((eq (cadr form) 'call-next-method)
1165 (setq call-next-method-p t)
1168 ((eq (cadr form) 'next-method-p)
1169 (setq next-method-p-p t)
1173 ((and (memq (car form)
1174 '(slot-value set-slot-value slot-boundp))
1175 (constantp (caddr form)))
1176 (let ((parameter (can-optimize-access form
1179 (let ((fun (ecase (car form)
1180 (slot-value #'optimize-slot-value)
1181 (set-slot-value #'optimize-set-slot-value)
1182 (slot-boundp #'optimize-slot-boundp))))
1183 (funcall fun slots parameter form))))
1184 ((and (eq (car form) 'apply)
1186 (eq (car (cadr form)) 'function)
1187 (generic-function-name-p (cadr (cadr form))))
1188 (optimize-generic-function-call
1189 form required-parameters env slots calls))
1190 ((generic-function-name-p (car form))
1191 (optimize-generic-function-call
1192 form required-parameters env slots calls))
1193 ((and (eq (car form) 'asv-funcall)
1194 *optimize-asv-funcall-p*)
1196 (reader (push (third form) *asv-readers*))
1197 (writer (push (third form) *asv-writers*))
1198 (boundp (push (third form) *asv-boundps*)))
1199 `(,(second form) ,@(cddddr form)))
1202 (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1203 (values walked-lambda
1206 next-method-p-p)))))
1208 (defun generic-function-name-p (name)
1209 (and (legal-fun-name-p name)
1211 (if (eq *boot-state* 'complete)
1212 (standard-generic-function-p (gdefinition name))
1213 (funcallable-instance-p (gdefinition name)))))
1215 (defvar *method-function-plist* (make-hash-table :test 'eq))
1218 (defvar *mf1cp* nil)
1221 (defvar *mf2cp* nil)
1223 (defun method-function-plist (method-function)
1224 (unless (eq method-function *mf1*)
1225 (rotatef *mf1* *mf2*)
1226 (rotatef *mf1p* *mf2p*)
1227 (rotatef *mf1cp* *mf2cp*))
1228 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1229 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1230 (unless (eq method-function *mf1*)
1231 (setf *mf1* method-function
1233 *mf1p* (gethash method-function *method-function-plist*)))
1236 (defun (setf method-function-plist)
1237 (val method-function)
1238 (unless (eq method-function *mf1*)
1239 (rotatef *mf1* *mf2*)
1240 (rotatef *mf1cp* *mf2cp*)
1241 (rotatef *mf1p* *mf2p*))
1242 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1243 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1244 (setf *mf1* method-function
1248 (defun method-function-get (method-function key &optional default)
1249 (getf (method-function-plist method-function) key default))
1251 (defun (setf method-function-get)
1252 (val method-function key)
1253 (setf (getf (method-function-plist method-function) key) val))
1255 (defun method-function-pv-table (method-function)
1256 (method-function-get method-function :pv-table))
1258 (defun method-function-method (method-function)
1259 (method-function-get method-function :method))
1261 (defun method-function-needs-next-methods-p (method-function)
1262 (method-function-get method-function :needs-next-methods-p t))
1264 (defmacro method-function-closure-generator (method-function)
1265 `(method-function-get ,method-function 'closure-generator))
1267 (defun load-defmethod
1268 (class name quals specls ll initargs &optional pv-table-symbol)
1269 (setq initargs (copy-tree initargs))
1270 (let ((method-spec (or (getf initargs :method-spec)
1271 (make-method-spec name quals specls))))
1272 (setf (getf initargs :method-spec) method-spec)
1273 (load-defmethod-internal class name quals specls
1274 ll initargs pv-table-symbol)))
1276 (defun load-defmethod-internal
1277 (method-class gf-spec qualifiers specializers lambda-list
1278 initargs pv-table-symbol)
1279 (when pv-table-symbol
1280 (setf (getf (getf initargs :plist) :pv-table-symbol)
1282 (when (and (eq *boot-state* 'complete)
1284 (let* ((gf (fdefinition gf-spec))
1285 (method (and (generic-function-p gf)
1288 (parse-specializers specializers)
1291 (sb-kernel::style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1292 gf-spec qualifiers specializers))))
1293 (let ((method (apply #'add-named-method
1294 gf-spec qualifiers specializers lambda-list
1295 :definition-source `((defmethod ,gf-spec
1300 (unless (or (eq method-class 'standard-method)
1301 (eq (find-class method-class nil) (class-of method)))
1302 ;; FIXME: should be STYLE-WARNING?
1303 (format *error-output*
1304 "~&At the time the method with qualifiers ~:S and~%~
1305 specializers ~:S on the generic function ~S~%~
1306 was compiled, the method-class for that generic function was~%~
1307 ~S. But, the method class is now ~S, this~%~
1308 may mean that this method was compiled improperly.~%"
1309 qualifiers specializers gf-spec
1310 method-class (class-name (class-of method))))
1313 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1314 `(method ,gf-spec ,@qualifiers ,unparsed-specializers))
1316 (defun initialize-method-function (initargs &optional return-function-p method)
1317 (let* ((mf (getf initargs :function))
1318 (method-spec (getf initargs :method-spec))
1319 (plist (getf initargs :plist))
1320 (pv-table-symbol (getf plist :pv-table-symbol))
1322 (mff (getf initargs :fast-function)))
1323 (flet ((set-mf-property (p v)
1325 (setf (method-function-get mf p) v))
1327 (setf (method-function-get mff p) v))))
1330 (setq mf (set-fun-name mf method-spec)))
1332 (let ((name `(,(or (get (car method-spec) 'fast-sym)
1333 (setf (get (car method-spec) 'fast-sym)
1334 ;; KLUDGE: If we're going to be
1335 ;; interning private symbols in our
1336 ;; a this way, it would be cleanest
1337 ;; to use a separate package
1338 ;; %PCL-PRIVATE or something, and
1339 ;; failing that, to use a special
1340 ;; symbol prefix denoting privateness.
1342 (intern (format nil "FAST-~A"
1345 ,@(cdr method-spec))))
1346 (set-fun-name mff name)
1348 (set-mf-property :name name)))))
1350 (let ((snl (getf plist :slot-name-lists))
1351 (cl (getf plist :call-list)))
1353 (setq pv-table (intern-pv-table :slot-name-lists snl
1355 (when pv-table (set pv-table-symbol pv-table))
1356 (set-mf-property :pv-table pv-table)))
1357 (loop (when (null plist) (return nil))
1358 (set-mf-property (pop plist) (pop plist)))
1360 (set-mf-property :method method))
1361 (when return-function-p
1362 (or mf (method-function-from-fast-function mff)))))))
1364 (defun analyze-lambda-list (lambda-list)
1365 (flet (;; FIXME: Is this redundant with SB-C::MAKE-KEYWORD-FOR-ARG?
1366 (parse-key-arg (arg)
1368 (if (listp (car arg))
1370 (keywordicate (car arg)))
1371 (keywordicate arg))))
1377 (allow-other-keys-p nil)
1379 (keyword-parameters ())
1381 (dolist (x lambda-list)
1382 (if (memq x lambda-list-keywords)
1384 (&optional (setq state 'optional))
1387 (&allow-other-keys (setq allow-other-keys-p t))
1388 (&rest (setq restp t
1392 (error "encountered the non-standard lambda list keyword ~S"
1395 (required (incf nrequired))
1396 (optional (incf noptional))
1397 (key (push (parse-key-arg x) keywords)
1398 (push x keyword-parameters))
1399 (rest (incf nrest)))))
1400 (when (and restp (zerop nrest))
1401 (error "Error in lambda-list:~%~
1402 After &REST, a DEFGENERIC lambda-list ~
1403 must be followed by at least one variable."))
1404 (values nrequired noptional keysp restp allow-other-keys-p
1406 (reverse keyword-parameters)))))
1408 (defun keyword-spec-name (x)
1409 (let ((key (if (atom x) x (car x))))
1414 (defun ftype-declaration-from-lambda-list (lambda-list name)
1415 (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1416 keywords keyword-parameters)
1417 (analyze-lambda-list lambda-list)
1418 (declare (ignore keyword-parameters))
1419 (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1420 (old-ftype (if (sb-kernel:fun-type-p old) old nil))
1421 (old-restp (and old-ftype (sb-kernel:fun-type-rest old-ftype)))
1422 (old-keys (and old-ftype
1423 (mapcar #'sb-kernel:key-info-name
1424 (sb-kernel:fun-type-keywords
1426 (old-keysp (and old-ftype (sb-kernel:fun-type-keyp old-ftype)))
1427 (old-allowp (and old-ftype
1428 (sb-kernel:fun-type-allowp old-ftype)))
1429 (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1430 `(function ,(append (make-list nrequired :initial-element t)
1431 (when (plusp noptional)
1432 (append '(&optional)
1433 (make-list noptional :initial-element t)))
1434 (when (or restp old-restp)
1436 (when (or keysp old-keysp)
1438 (mapcar (lambda (key)
1441 (when (or allow-other-keys-p old-allowp)
1442 '(&allow-other-keys)))))
1445 (defun defgeneric-declaration (spec lambda-list)
1447 (setq spec (get-setf-fun-name (cadr spec))))
1448 `(ftype ,(ftype-declaration-from-lambda-list lambda-list spec) ,spec))
1450 ;;;; early generic function support
1452 (defvar *!early-generic-functions* ())
1454 (defun ensure-generic-function (fun-name
1458 (declare (ignore environment))
1459 (let ((existing (and (gboundp fun-name)
1460 (gdefinition fun-name))))
1462 (eq *boot-state* 'complete)
1463 (null (generic-function-p existing)))
1464 (generic-clobbers-function fun-name)
1465 (apply #'ensure-generic-function-using-class
1466 existing fun-name all-keys))))
1468 (defun generic-clobbers-function (fun-name)
1469 (error 'simple-program-error
1470 :format-control "~S already names an ordinary function or a macro."
1471 :format-arguments (list fun-name)))
1473 (defvar *sgf-wrapper*
1474 (boot-make-wrapper (early-class-size 'standard-generic-function)
1475 'standard-generic-function))
1477 (defvar *sgf-slots-init*
1478 (mapcar (lambda (canonical-slot)
1479 (if (memq (getf canonical-slot :name) '(arg-info source))
1481 (let ((initfunction (getf canonical-slot :initfunction)))
1483 (funcall initfunction)
1485 (early-collect-inheritance 'standard-generic-function)))
1487 (defvar *sgf-method-class-index*
1488 (!bootstrap-slot-index 'standard-generic-function 'method-class))
1490 (defun early-gf-p (x)
1491 (and (fsc-instance-p x)
1492 (eq (clos-slots-ref (get-slots x) *sgf-method-class-index*)
1495 (defvar *sgf-methods-index*
1496 (!bootstrap-slot-index 'standard-generic-function 'methods))
1498 (defmacro early-gf-methods (gf)
1499 `(clos-slots-ref (get-slots ,gf) *sgf-methods-index*))
1501 (defvar *sgf-arg-info-index*
1502 (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1504 (defmacro early-gf-arg-info (gf)
1505 `(clos-slots-ref (get-slots ,gf) *sgf-arg-info-index*))
1507 (defvar *sgf-dfun-state-index*
1508 (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1510 (defstruct (arg-info
1512 (:constructor make-arg-info ())
1514 (arg-info-lambda-list :no-lambda-list)
1517 arg-info-number-optional
1519 arg-info-keys ;nil no &KEY or &REST allowed
1520 ;(k1 k2 ..) Each method must accept these &KEY arguments.
1521 ;T must have &KEY or &REST
1523 gf-info-simple-accessor-type ; nil, reader, writer, boundp
1524 (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1526 gf-info-static-c-a-m-emf
1527 (gf-info-c-a-m-emf-std-p t)
1530 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1532 (defun arg-info-valid-p (arg-info)
1533 (not (null (arg-info-number-optional arg-info))))
1535 (defun arg-info-applyp (arg-info)
1536 (or (plusp (arg-info-number-optional arg-info))
1537 (arg-info-key/rest-p arg-info)))
1539 (defun arg-info-number-required (arg-info)
1540 (length (arg-info-metatypes arg-info)))
1542 (defun arg-info-nkeys (arg-info)
1543 (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1545 ;;; Keep pages clean by not setting if the value is already the same.
1546 (defmacro esetf (pos val)
1547 (let ((valsym (gensym "value")))
1548 `(let ((,valsym ,val))
1549 (unless (equal ,pos ,valsym)
1550 (setf ,pos ,valsym)))))
1552 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1553 argument-precedence-order)
1554 (let* ((arg-info (if (eq *boot-state* 'complete)
1556 (early-gf-arg-info gf)))
1557 (methods (if (eq *boot-state* 'complete)
1558 (generic-function-methods gf)
1559 (early-gf-methods gf)))
1560 (was-valid-p (integerp (arg-info-number-optional arg-info)))
1561 (first-p (and new-method (null (cdr methods)))))
1562 (when (and (not lambda-list-p) methods)
1563 (setq lambda-list (gf-lambda-list gf)))
1564 (when (or lambda-list-p
1566 (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1567 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1568 (analyze-lambda-list lambda-list)
1569 (when (and methods (not first-p))
1570 (let ((gf-nreq (arg-info-number-required arg-info))
1571 (gf-nopt (arg-info-number-optional arg-info))
1572 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1573 (unless (and (= nreq gf-nreq)
1575 (eq (or keysp restp) gf-key/rest-p))
1576 (error "The lambda-list ~S is incompatible with ~
1577 existing methods of ~S."
1580 (esetf (arg-info-lambda-list arg-info) lambda-list))
1581 (when (or lambda-list-p argument-precedence-order
1582 (null (arg-info-precedence arg-info)))
1583 (esetf (arg-info-precedence arg-info)
1584 (compute-precedence lambda-list nreq
1585 argument-precedence-order)))
1586 (esetf (arg-info-metatypes arg-info) (make-list nreq))
1587 (esetf (arg-info-number-optional arg-info) nopt)
1588 (esetf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1589 (esetf (arg-info-keys arg-info)
1591 (if allow-other-keys-p t keywords)
1592 (arg-info-key/rest-p arg-info)))))
1594 (check-method-arg-info gf arg-info new-method))
1595 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1598 (defun check-method-arg-info (gf arg-info method)
1599 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1600 (analyze-lambda-list (if (consp method)
1601 (early-method-lambda-list method)
1602 (method-lambda-list method)))
1603 (flet ((lose (string &rest args)
1605 "attempt to add the method ~S to the generic function ~S.~%~
1609 (apply #'format nil string args)))
1610 (comparison-description (x y)
1611 (if (> x y) "more" "fewer")))
1612 (let ((gf-nreq (arg-info-number-required arg-info))
1613 (gf-nopt (arg-info-number-optional arg-info))
1614 (gf-key/rest-p (arg-info-key/rest-p arg-info))
1615 (gf-keywords (arg-info-keys arg-info)))
1616 (unless (= nreq gf-nreq)
1618 "the method has ~A required arguments than the generic function."
1619 (comparison-description nreq gf-nreq)))
1620 (unless (= nopt gf-nopt)
1622 "the method has ~A optional arguments than the generic function."
1623 (comparison-description nopt gf-nopt)))
1624 (unless (eq (or keysp restp) gf-key/rest-p)
1626 "The method and generic function differ in whether they accept~%~
1627 &REST or &KEY arguments."))
1628 (when (consp gf-keywords)
1629 (unless (or (and restp (not keysp))
1631 (every (lambda (k) (memq k keywords)) gf-keywords))
1632 (lose "the method does not accept each of the &KEY arguments~%~
1636 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1637 (let* ((existing-p (and methods (cdr methods) new-method))
1638 (nreq (length (arg-info-metatypes arg-info)))
1639 (metatypes (if existing-p
1640 (arg-info-metatypes arg-info)
1642 (type (if existing-p
1643 (gf-info-simple-accessor-type arg-info)
1645 (when (arg-info-valid-p arg-info)
1646 (dolist (method (if new-method (list new-method) methods))
1647 (let* ((specializers (if (or (eq *boot-state* 'complete)
1648 (not (consp method)))
1649 (method-specializers method)
1650 (early-method-specializers method t)))
1651 (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1653 (early-method-class method)))
1654 (new-type (when (and class
1655 (or (not (eq *boot-state* 'complete))
1656 (eq (generic-function-method-combination gf)
1657 *standard-method-combination*)))
1658 (cond ((eq class *the-class-standard-reader-method*)
1660 ((eq class *the-class-standard-writer-method*)
1662 ((eq class *the-class-standard-boundp-method*)
1664 (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1665 (setq type (cond ((null type) new-type)
1666 ((eq type new-type) type)
1668 (esetf (arg-info-metatypes arg-info) metatypes)
1669 (esetf (gf-info-simple-accessor-type arg-info) type)))
1670 (when (or (not was-valid-p) first-p)
1671 (multiple-value-bind (c-a-m-emf std-p)
1674 (compute-applicable-methods-emf gf))
1675 (esetf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1676 (esetf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1677 (unless (gf-info-c-a-m-emf-std-p arg-info)
1678 (esetf (gf-info-simple-accessor-type arg-info) t))))
1680 (let ((name (if (eq *boot-state* 'complete)
1681 (generic-function-name gf)
1682 (!early-gf-name gf))))
1683 (esetf (gf-precompute-dfun-and-emf-p arg-info)
1684 (let* ((sym (if (atom name) name (cadr name)))
1685 (pkg-list (cons *pcl-package*
1686 (package-use-list *pcl-package*))))
1687 (and sym (symbolp sym)
1688 (not (null (memq (symbol-package sym) pkg-list)))
1689 (not (find #\space (symbol-name sym))))))))
1690 (esetf (gf-info-fast-mf-p arg-info)
1691 (or (not (eq *boot-state* 'complete))
1692 (let* ((method-class (generic-function-method-class gf))
1693 (methods (compute-applicable-methods
1694 #'make-method-lambda
1695 (list gf (class-prototype method-class)
1697 (and methods (null (cdr methods))
1698 (let ((specls (method-specializers (car methods))))
1699 (and (classp (car specls))
1700 (eq 'standard-generic-function
1701 (class-name (car specls)))
1702 (classp (cadr specls))
1703 (eq 'standard-method
1704 (class-name (cadr specls)))))))))
1707 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
1709 ;;; The STATIC-SLOTS field of the funcallable instances used as early
1710 ;;; generic functions is used to store the early methods and early
1711 ;;; discriminator code for the early generic function. The static
1712 ;;; slots field of the fins contains a list whose:
1713 ;;; CAR - a list of the early methods on this early gf
1714 ;;; CADR - the early discriminator code for this method
1715 (defun ensure-generic-function-using-class (existing spec &rest keys
1716 &key (lambda-list nil
1719 (declare (ignore keys))
1720 (cond ((and existing (early-gf-p existing))
1722 ((assoc spec *!generic-function-fixups* :test #'equal)
1724 (make-early-gf spec lambda-list lambda-list-p existing)
1725 (error "The function ~S is not already defined." spec)))
1727 (error "~S should be on the list ~S."
1729 '*!generic-function-fixups*))
1731 (pushnew spec *!early-generic-functions* :test #'equal)
1732 (make-early-gf spec lambda-list lambda-list-p))))
1734 (defun make-early-gf (spec &optional lambda-list lambda-list-p function)
1735 (let ((fin (allocate-funcallable-instance *sgf-wrapper* *sgf-slots-init*)))
1736 (set-funcallable-instance-fun
1739 (if (eq spec 'print-object)
1740 #'(sb-kernel:instance-lambda (instance stream)
1741 (print-unreadable-object (instance stream :identity t)
1742 (format stream "std-instance")))
1743 #'(sb-kernel:instance-lambda (&rest args)
1744 (declare (ignore args))
1745 (error "The function of the funcallable-instance ~S~
1746 has not been set." fin)))))
1747 (setf (gdefinition spec) fin)
1748 (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
1749 (!bootstrap-set-slot 'standard-generic-function
1753 (set-fun-name fin spec)
1754 (let ((arg-info (make-arg-info)))
1755 (setf (early-gf-arg-info fin) arg-info)
1757 (proclaim (defgeneric-declaration spec lambda-list))
1758 (set-arg-info fin :lambda-list lambda-list)))
1761 (defun set-dfun (gf &optional dfun cache info)
1763 (setf (cache-owner cache) gf))
1764 (let ((new-state (if (and dfun (or cache info))
1765 (list* dfun cache info)
1767 (if (eq *boot-state* 'complete)
1768 (setf (gf-dfun-state gf) new-state)
1769 (setf (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*)
1773 (defun gf-dfun-cache (gf)
1774 (let ((state (if (eq *boot-state* 'complete)
1776 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1779 (cons (cadr state)))))
1781 (defun gf-dfun-info (gf)
1782 (let ((state (if (eq *boot-state* 'complete)
1784 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1787 (cons (cddr state)))))
1789 (defvar *sgf-name-index*
1790 (!bootstrap-slot-index 'standard-generic-function 'name))
1792 (defun !early-gf-name (gf)
1793 (clos-slots-ref (get-slots gf) *sgf-name-index*))
1795 (defun gf-lambda-list (gf)
1796 (let ((arg-info (if (eq *boot-state* 'complete)
1798 (early-gf-arg-info gf))))
1799 (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
1800 (let ((methods (if (eq *boot-state* 'complete)
1801 (generic-function-methods gf)
1802 (early-gf-methods gf))))
1805 (warn "no way to determine the lambda list for ~S" gf)
1807 (let* ((method (car (last methods)))
1808 (ll (if (consp method)
1809 (early-method-lambda-list method)
1810 (method-lambda-list method)))
1811 (k (member '&key ll)))
1813 (append (ldiff ll (cdr k)) '(&allow-other-keys))
1815 (arg-info-lambda-list arg-info))))
1817 (defmacro real-ensure-gf-internal (gf-class all-keys env)
1819 (cond ((symbolp ,gf-class)
1820 (setq ,gf-class (find-class ,gf-class t ,env)))
1821 ((classp ,gf-class))
1823 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
1824 class nor a symbol that names a class."
1826 (remf ,all-keys :generic-function-class)
1827 (remf ,all-keys :environment)
1828 (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
1829 (unless (eq combin '.shes-not-there.)
1830 (setf (getf ,all-keys :method-combination)
1831 (find-method-combination (class-prototype ,gf-class)
1834 (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
1835 (unless (eq method-class '.shes-not-there.)
1836 (setf (getf ,all-keys :method-class)
1837 (find-class method-class t ,env))))))
1839 (defun real-ensure-gf-using-class--generic-function
1843 &key environment (lambda-list nil lambda-list-p)
1844 (generic-function-class 'standard-generic-function gf-class-p)
1846 (real-ensure-gf-internal generic-function-class all-keys environment)
1847 (unless (or (null gf-class-p)
1848 (eq (class-of existing) generic-function-class))
1849 (change-class existing generic-function-class))
1851 (apply #'reinitialize-instance existing all-keys)
1853 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1855 (defun real-ensure-gf-using-class--null
1859 &key environment (lambda-list nil lambda-list-p)
1860 (generic-function-class 'standard-generic-function)
1862 (declare (ignore existing))
1863 (real-ensure-gf-internal generic-function-class all-keys environment)
1865 (setf (gdefinition fun-name)
1866 (apply #'make-instance generic-function-class
1867 :name fun-name all-keys))
1869 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1871 (defun get-generic-fun-info (gf)
1872 ;; values nreq applyp metatypes nkeys arg-info
1873 (multiple-value-bind (applyp metatypes arg-info)
1874 (let* ((arg-info (if (early-gf-p gf)
1875 (early-gf-arg-info gf)
1877 (metatypes (arg-info-metatypes arg-info)))
1878 (values (arg-info-applyp arg-info)
1881 (values (length metatypes) applyp metatypes
1882 (count-if (lambda (x) (neq x t)) metatypes)
1885 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
1886 &optional slot-name)
1887 (initialize-method-function initargs)
1890 ;; Figure out whether we got class objects or class names as the
1891 ;; specializers and set parsed and unparsed appropriately. If we
1892 ;; got class objects, then we can compute unparsed, but if we got
1893 ;; class names we don't try to compute parsed.
1895 ;; Note that the use of not symbolp in this call to every should be
1896 ;; read as 'classp' we can't use classp itself because it doesn't
1898 (if (every (lambda (s) (not (symbolp s))) specializers)
1899 (setq parsed specializers
1900 unparsed (mapcar (lambda (s)
1901 (if (eq s t) t (class-name s)))
1903 (setq unparsed specializers
1905 (list :early-method ;This is an early method dammit!
1907 (getf initargs :function)
1908 (getf initargs :fast-function)
1910 parsed ;The parsed specializers. This is used
1911 ;by early-method-specializers to cache
1912 ;the parse. Note that this only comes
1913 ;into play when there is more than one
1914 ;early method on an early gf.
1916 (list class ;A list to which real-make-a-method
1917 qualifiers ;can be applied to make a real method
1918 arglist ;corresponding to this early one.
1924 (defun real-make-a-method
1925 (class qualifiers lambda-list specializers initargs doc
1926 &optional slot-name)
1927 (setq specializers (parse-specializers specializers))
1928 (apply #'make-instance class
1929 :qualifiers qualifiers
1930 :lambda-list lambda-list
1931 :specializers specializers
1933 :slot-name slot-name
1937 (defun early-method-function (early-method)
1938 (values (cadr early-method) (caddr early-method)))
1940 (defun early-method-class (early-method)
1941 (find-class (car (fifth early-method))))
1943 (defun early-method-standard-accessor-p (early-method)
1944 (let ((class (first (fifth early-method))))
1945 (or (eq class 'standard-reader-method)
1946 (eq class 'standard-writer-method)
1947 (eq class 'standard-boundp-method))))
1949 (defun early-method-standard-accessor-slot-name (early-method)
1950 (seventh (fifth early-method)))
1952 ;;; Fetch the specializers of an early method. This is basically just
1953 ;;; a simple accessor except that when the second argument is t, this
1954 ;;; converts the specializers from symbols into class objects. The
1955 ;;; class objects are cached in the early method, this makes
1956 ;;; bootstrapping faster because the class objects only have to be
1960 ;;; The second argument should only be passed as T by
1961 ;;; early-lookup-method. This is to implement the rule that only when
1962 ;;; there is more than one early method on a generic function is the
1963 ;;; conversion from class names to class objects done. This
1964 ;;; corresponds to the fact that we are only allowed to have one
1965 ;;; method on any generic function up until the time classes exist.
1966 (defun early-method-specializers (early-method &optional objectsp)
1967 (if (and (listp early-method)
1968 (eq (car early-method) :early-method))
1969 (cond ((eq objectsp t)
1970 (or (fourth early-method)
1971 (setf (fourth early-method)
1972 (mapcar #'find-class (cadddr (fifth early-method))))))
1974 (cadddr (fifth early-method))))
1975 (error "~S is not an early-method." early-method)))
1977 (defun early-method-qualifiers (early-method)
1978 (cadr (fifth early-method)))
1980 (defun early-method-lambda-list (early-method)
1981 (caddr (fifth early-method)))
1983 (defun early-add-named-method (generic-function-name
1988 (let* ((gf (ensure-generic-function generic-function-name))
1990 (dolist (m (early-gf-methods gf))
1991 (when (and (equal (early-method-specializers m) specializers)
1992 (equal (early-method-qualifiers m) qualifiers))
1994 (new (make-a-method 'standard-method
2000 (when existing (remove-method gf existing))
2001 (add-method gf new)))
2003 ;;; This is the early version of ADD-METHOD. Later this will become a
2004 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2005 ;;; special knowledge about ADD-METHOD.
2006 (defun add-method (generic-function method)
2007 (when (not (fsc-instance-p generic-function))
2008 (error "Early ADD-METHOD didn't get a funcallable instance."))
2009 (when (not (and (listp method) (eq (car method) :early-method)))
2010 (error "Early ADD-METHOD didn't get an early method."))
2011 (push method (early-gf-methods generic-function))
2012 (set-arg-info generic-function :new-method method)
2013 (unless (assoc (!early-gf-name generic-function)
2014 *!generic-function-fixups*
2016 (update-dfun generic-function)))
2018 ;;; This is the early version of REMOVE-METHOD. See comments on
2019 ;;; the early version of ADD-METHOD.
2020 (defun remove-method (generic-function method)
2021 (when (not (fsc-instance-p generic-function))
2022 (error "An early remove-method didn't get a funcallable instance."))
2023 (when (not (and (listp method) (eq (car method) :early-method)))
2024 (error "An early remove-method didn't get an early method."))
2025 (setf (early-gf-methods generic-function)
2026 (remove method (early-gf-methods generic-function)))
2027 (set-arg-info generic-function)
2028 (unless (assoc (!early-gf-name generic-function)
2029 *!generic-function-fixups*
2031 (update-dfun generic-function)))
2033 ;;; This is the early version of GET-METHOD. See comments on the early
2034 ;;; version of ADD-METHOD.
2035 (defun get-method (generic-function qualifiers specializers
2036 &optional (errorp t))
2037 (if (early-gf-p generic-function)
2038 (or (dolist (m (early-gf-methods generic-function))
2039 (when (and (or (equal (early-method-specializers m nil)
2041 (equal (early-method-specializers m t)
2043 (equal (early-method-qualifiers m) qualifiers))
2046 (error "can't get early method")
2048 (real-get-method generic-function qualifiers specializers errorp)))
2050 (defun !fix-early-generic-functions ()
2051 (let ((accessors nil))
2052 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2053 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2054 (dolist (early-gf-spec *!early-generic-functions*)
2055 (when (every #'early-method-standard-accessor-p
2056 (early-gf-methods (gdefinition early-gf-spec)))
2057 (push early-gf-spec accessors)))
2058 (dolist (spec (nconc accessors
2059 '(accessor-method-slot-name
2060 generic-function-methods
2065 slot-definition-location
2066 slot-definition-name
2069 class-precedence-list
2070 slot-boundp-using-class
2071 (setf slot-value-using-class)
2072 slot-value-using-class
2075 funcallable-standard-class-p
2078 (setq *!early-generic-functions*
2080 (delete spec *!early-generic-functions* :test #'equal))))
2082 (dolist (early-gf-spec *!early-generic-functions*)
2083 (/show early-gf-spec)
2084 (let* ((gf (gdefinition early-gf-spec))
2085 (methods (mapcar (lambda (early-method)
2086 (let ((args (copy-list (fifth
2089 (early-method-specializers
2091 (apply #'real-make-a-method args)))
2092 (early-gf-methods gf))))
2093 (setf (generic-function-method-class gf) *the-class-standard-method*)
2094 (setf (generic-function-method-combination gf)
2095 *standard-method-combination*)
2096 (set-methods gf methods)))
2098 (dolist (fn *!early-functions*)
2100 (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2102 (dolist (fixup *!generic-function-fixups*)
2104 (let* ((fspec (car fixup))
2105 (gf (gdefinition fspec))
2106 (methods (mapcar (lambda (method)
2107 (let* ((lambda-list (first method))
2108 (specializers (second method))
2109 (method-fn-name (third method))
2110 (fn-name (or method-fn-name fspec))
2111 (fn (fdefinition fn-name))
2115 (lambda (args next-methods)
2119 `(call ,fn-name)))))
2120 (declare (type function fn))
2121 (make-a-method 'standard-method
2128 (setf (generic-function-method-class gf) *the-class-standard-method*)
2129 (setf (generic-function-method-combination gf)
2130 *standard-method-combination*)
2131 (set-methods gf methods))))
2132 (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2134 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2135 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2136 ;;; is really implemented.
2137 (defun parse-defmethod (cdr-of-form)
2138 (declare (list cdr-of-form))
2139 (let ((name (pop cdr-of-form))
2142 (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2143 (push (pop cdr-of-form) qualifiers)
2144 (return (setq qualifiers (nreverse qualifiers)))))
2145 (setq spec-ll (pop cdr-of-form))
2146 (values name qualifiers spec-ll cdr-of-form)))
2148 (defun parse-specializers (specializers)
2149 (declare (list specializers))
2150 (flet ((parse (spec)
2151 (let ((result (specializer-from-type spec)))
2152 (if (specializerp result)
2155 (error "~S was used as a specializer,~%~
2156 but is not the name of a class."
2158 (error "~S is not a legal specializer." spec))))))
2159 (mapcar #'parse specializers)))
2161 (defun unparse-specializers (specializers-or-method)
2162 (if (listp specializers-or-method)
2163 (flet ((unparse (spec)
2164 (if (specializerp spec)
2165 (let ((type (specializer-type spec)))
2166 (if (and (consp type)
2167 (eq (car type) 'class))
2168 (let* ((class (cadr type))
2169 (class-name (class-name class)))
2170 (if (eq class (find-class class-name nil))
2174 (error "~S is not a legal specializer." spec))))
2175 (mapcar #'unparse specializers-or-method))
2176 (unparse-specializers (method-specializers specializers-or-method))))
2178 (defun parse-method-or-spec (spec &optional (errorp t))
2179 (let (gf method name temp)
2182 gf (method-generic-function method)
2183 temp (and gf (generic-function-name gf))
2186 (make-method-spec temp
2187 (method-qualifiers method)
2188 (unparse-specializers
2189 (method-specializers method))))
2190 (make-symbol (format nil "~S" method))))
2191 (multiple-value-bind (gf-spec quals specls)
2192 (parse-defmethod spec)
2193 (and (setq gf (and (or errorp (gboundp gf-spec))
2194 (gdefinition gf-spec)))
2195 (let ((nreq (compute-discriminating-function-arglist-info gf)))
2196 (setq specls (append (parse-specializers specls)
2197 (make-list (- nreq (length specls))
2201 (setq method (get-method gf quals specls errorp))
2203 (intern-fun-name (make-method-spec gf-spec
2206 (values gf method name)))
2208 (defun extract-parameters (specialized-lambda-list)
2209 (multiple-value-bind (parameters ignore1 ignore2)
2210 (parse-specialized-lambda-list specialized-lambda-list)
2211 (declare (ignore ignore1 ignore2))
2214 (defun extract-lambda-list (specialized-lambda-list)
2215 (multiple-value-bind (ignore1 lambda-list ignore2)
2216 (parse-specialized-lambda-list specialized-lambda-list)
2217 (declare (ignore ignore1 ignore2))
2220 (defun extract-specializer-names (specialized-lambda-list)
2221 (multiple-value-bind (ignore1 ignore2 specializers)
2222 (parse-specialized-lambda-list specialized-lambda-list)
2223 (declare (ignore ignore1 ignore2))
2226 (defun extract-required-parameters (specialized-lambda-list)
2227 (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2228 (parse-specialized-lambda-list specialized-lambda-list)
2229 (declare (ignore ignore1 ignore2 ignore3))
2230 required-parameters))
2232 (defun parse-specialized-lambda-list (arglist &optional post-keyword)
2233 ;;(declare (values parameters lambda-list specializers required-parameters))
2234 (let ((arg (car arglist)))
2235 (cond ((null arglist) (values nil nil nil nil))
2237 (values nil arglist nil))
2238 ((memq arg lambda-list-keywords)
2239 (unless (memq arg '(&optional &rest &key &allow-other-keys &aux))
2240 ;; Now, since we try to conform to ANSI, non-standard
2241 ;; lambda-list-keywords should be treated as errors.
2242 (error 'simple-program-error
2243 :format-control "unrecognized lambda-list keyword ~S ~
2245 :format-arguments (list arg)))
2246 ;; When we are at a lambda-list keyword, the parameters
2247 ;; don't include the lambda-list keyword; the lambda-list
2248 ;; does include the lambda-list keyword; and no
2249 ;; specializers are allowed to follow the lambda-list
2250 ;; keywords (at least for now).
2251 (multiple-value-bind (parameters lambda-list)
2252 (parse-specialized-lambda-list (cdr arglist) t)
2253 (when (eq arg '&rest)
2254 ;; check, if &rest is followed by a var ...
2255 (when (or (null lambda-list)
2256 (memq (car lambda-list) lambda-list-keywords))
2257 (error "Error in lambda-list:~%~
2258 After &REST, a DEFMETHOD lambda-list ~
2259 must be followed by at least one variable.")))
2261 (cons arg lambda-list)
2265 ;; After a lambda-list keyword there can be no specializers.
2266 (multiple-value-bind (parameters lambda-list)
2267 (parse-specialized-lambda-list (cdr arglist) t)
2268 (values (cons (if (listp arg) (car arg) arg) parameters)
2269 (cons arg lambda-list)
2273 (multiple-value-bind (parameters lambda-list specializers required)
2274 (parse-specialized-lambda-list (cdr arglist))
2275 (values (cons (if (listp arg) (car arg) arg) parameters)
2276 (cons (if (listp arg) (car arg) arg) lambda-list)
2277 (cons (if (listp arg) (cadr arg) t) specializers)
2278 (cons (if (listp arg) (car arg) arg) required)))))))
2280 (setq *boot-state* 'early)
2282 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2283 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2284 ;;; walker stuff was only used for implementing stuff like that; maybe
2285 ;;; it's not needed any more? Hunt down what it was used for and see.
2287 (defmacro with-slots (slots instance &body body)
2288 (let ((in (gensym)))
2289 `(let ((,in ,instance))
2290 (declare (ignorable ,in))
2291 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2294 (and (symbolp instance)
2295 `((declare (%variable-rebinding ,in ,instance)))))
2297 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2299 (if (symbolp slot-entry)
2303 (if (symbolp slot-entry)
2305 (cadr slot-entry))))
2307 (slot-value ,in ',slot-name))))
2311 (defmacro with-accessors (slots instance &body body)
2312 (let ((in (gensym)))
2313 `(let ((,in ,instance))
2314 (declare (ignorable ,in))
2315 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2318 (and (symbolp instance)
2319 `((declare (%variable-rebinding ,in ,instance)))))
2321 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2322 (let ((var-name (car slot-entry))
2323 (accessor-name (cadr slot-entry)))
2324 `(,var-name (,accessor-name ,in))))