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 (declaim (notinline make-a-method
83 ensure-generic-function-using-class
87 (defvar *!early-functions*
88 '((make-a-method early-make-a-method
90 (add-named-method early-add-named-method
91 real-add-named-method)
94 ;;; For each of the early functions, arrange to have it point to its
95 ;;; early definition. Do this in a way that makes sure that if we
96 ;;; redefine one of the early definitions the redefinition will take
97 ;;; effect. This makes development easier.
98 (dolist (fns *!early-functions*)
99 (let ((name (car fns))
100 (early-name (cadr fns)))
101 (setf (gdefinition name)
104 (apply (fdefinition early-name) args))
107 ;;; *!GENERIC-FUNCTION-FIXUPS* is used by !FIX-EARLY-GENERIC-FUNCTIONS
108 ;;; to convert the few functions in the bootstrap which are supposed
109 ;;; to be generic functions but can't be early on.
110 (defvar *!generic-function-fixups*
112 ((generic-function method) ;lambda-list
113 (standard-generic-function method) ;specializers
114 real-add-method)) ;method-function
116 ((generic-function method)
117 (standard-generic-function method)
120 ((generic-function qualifiers specializers &optional (errorp t))
121 (standard-generic-function t t)
123 (ensure-generic-function-using-class
124 ((generic-function fun-name
125 &key generic-function-class environment
128 real-ensure-gf-using-class--generic-function)
129 ((generic-function fun-name
130 &key generic-function-class environment
133 real-ensure-gf-using-class--null))
135 ((proto-generic-function proto-method lambda-expression environment)
136 (standard-generic-function standard-method t t)
137 real-make-method-lambda))
138 (make-method-initargs-form
139 ((proto-generic-function proto-method
141 lambda-list environment)
142 (standard-generic-function standard-method t t t)
143 real-make-method-initargs-form))
144 (compute-effective-method
145 ((generic-function combin applicable-methods)
146 (generic-function standard-method-combination t)
147 standard-compute-effective-method))))
149 (defmacro defgeneric (fun-name lambda-list &body options)
150 (declare (type list lambda-list))
151 (unless (legal-fun-name-p fun-name)
152 (error 'simple-program-error
153 :format-control "illegal generic function name ~S"
154 :format-arguments (list fun-name)))
155 (check-gf-lambda-list lambda-list)
158 (flet ((duplicate-option (name)
159 (error 'simple-program-error
160 :format-control "The option ~S appears more than once."
161 :format-arguments (list name)))
162 (expand-method-definition (qab) ; QAB = qualifiers, arglist, body
163 (let* ((arglist-pos (position-if #'listp qab))
164 (arglist (elt qab arglist-pos))
165 (qualifiers (subseq qab 0 arglist-pos))
166 (body (nthcdr (1+ arglist-pos) qab)))
167 `(push (defmethod ,fun-name ,@qualifiers ,arglist ,@body)
168 (generic-function-initial-methods #',fun-name)))))
169 (macrolet ((initarg (key) `(getf initargs ,key)))
170 (dolist (option options)
171 (let ((car-option (car option)))
174 (push (cdr option) (initarg :declarations)))
175 ((:argument-precedence-order :method-combination)
176 (if (initarg car-option)
177 (duplicate-option car-option)
178 (setf (initarg car-option)
180 ((:documentation :generic-function-class :method-class)
181 (unless (proper-list-of-length-p option 2)
182 (error "bad list length for ~S" option))
183 (if (initarg car-option)
184 (duplicate-option car-option)
185 (setf (initarg car-option) `',(cadr option))))
187 (push (cdr option) methods))
189 ;; ANSI requires that unsupported things must get a
191 (error 'simple-program-error
192 :format-control "unsupported option ~S"
193 :format-arguments (list option))))))
195 (when (initarg :declarations)
196 (setf (initarg :declarations)
197 `',(initarg :declarations))))
199 (eval-when (:compile-toplevel :load-toplevel :execute)
200 (compile-or-load-defgeneric ',fun-name))
201 (load-defgeneric ',fun-name ',lambda-list ,@initargs)
202 ,@(mapcar #'expand-method-definition methods)
205 (defun compile-or-load-defgeneric (fun-name)
206 (sb-kernel:proclaim-as-fun-name fun-name)
207 (sb-kernel:note-name-defined fun-name :function)
208 (unless (eq (info :function :where-from fun-name) :declared)
209 (setf (info :function :where-from fun-name) :defined)
210 (setf (info :function :type fun-name)
211 (sb-kernel:specifier-type 'function))))
213 (defun load-defgeneric (fun-name lambda-list &rest initargs)
214 (when (fboundp fun-name)
215 (sb-kernel::style-warn "redefining ~S in DEFGENERIC" fun-name)
216 (let ((fun (fdefinition fun-name)))
217 (when (generic-function-p fun)
218 (loop for method in (generic-function-initial-methods fun)
219 do (remove-method fun method))
220 (setf (generic-function-initial-methods fun) '()))))
221 (apply #'ensure-generic-function
223 :lambda-list lambda-list
224 :definition-source `((defgeneric ,fun-name) ,*load-truename*)
227 ;;; As per section 3.4.2 of the ANSI spec, generic function lambda
228 ;;; lists have some special limitations, which we check here.
229 (defun check-gf-lambda-list (lambda-list)
230 (flet ((ensure (arg ok)
233 ;; (s/invalid/non-ANSI-conforming/ because the old PCL
234 ;; implementation allowed this, so people got used to
235 ;; it, and maybe this phrasing will help them to guess
236 ;; why their program which worked under PCL no longer works.)
237 "~@<non-ANSI-conforming argument ~S ~_in the generic function lambda list ~S~:>"
239 (multiple-value-bind (required optional restp rest keyp keys allowp
240 auxp aux morep more-context more-count)
241 (parse-lambda-list lambda-list)
242 (declare (ignore required)) ; since they're no different in a gf ll
243 (declare (ignore restp rest)) ; since they're no different in a gf ll
244 (declare (ignore allowp)) ; since &ALLOW-OTHER-KEYS is fine either way
245 (declare (ignore aux)) ; since we require AUXP=NIL
246 (declare (ignore more-context more-count)) ; safely ignored unless MOREP
247 ;; no defaults allowed for &OPTIONAL arguments
249 (ensure i (or (symbolp i)
250 (and (consp i) (symbolp (car i)) (null (cdr i))))))
251 ;; no defaults allowed for &KEY arguments
254 (ensure i (or (symbolp i)
256 (or (symbolp (car i))
264 (error "&AUX is not allowed in a generic function lambda list: ~S"
266 ;; Oh, *puhlease*... not specifically as per section 3.4.2 of
267 ;; the ANSI spec, but the CMU CL &MORE extension does not
269 (aver (not morep)))))
271 (defmacro defmethod (&rest args &environment env)
272 (multiple-value-bind (name qualifiers lambda-list body)
273 (parse-defmethod args)
274 (multiple-value-bind (proto-gf proto-method)
275 (prototypes-for-make-method-lambda name)
276 (expand-defmethod name
284 (defun prototypes-for-make-method-lambda (name)
285 (if (not (eq *boot-state* 'complete))
287 (let ((gf? (and (gboundp name)
288 (gdefinition name))))
290 (not (generic-function-p gf?)))
291 (values (class-prototype (find-class 'standard-generic-function))
292 (class-prototype (find-class 'standard-method)))
294 (class-prototype (or (generic-function-method-class gf?)
295 (find-class 'standard-method))))))))
297 ;;; Take a name which is either a generic function name or a list specifying
298 ;;; a SETF generic function (like: (SETF <generic-function-name>)). Return
299 ;;; the prototype instance of the method-class for that generic function.
301 ;;; If there is no generic function by that name, this returns the
302 ;;; default value, the prototype instance of the class
303 ;;; STANDARD-METHOD. This default value is also returned if the spec
304 ;;; names an ordinary function or even a macro. In effect, this leaves
305 ;;; the signalling of the appropriate error until load time.
307 ;;; Note: During bootstrapping, this function is allowed to return NIL.
308 (defun method-prototype-for-gf (name)
309 (let ((gf? (and (gboundp name)
310 (gdefinition name))))
311 (cond ((neq *boot-state* 'complete) nil)
313 (not (generic-function-p gf?))) ; Someone else MIGHT
314 ; error at load time.
315 (class-prototype (find-class 'standard-method)))
317 (class-prototype (or (generic-function-method-class gf?)
318 (find-class 'standard-method)))))))
320 (defvar *optimize-asv-funcall-p* nil)
321 (defvar *asv-readers*)
322 (defvar *asv-writers*)
323 (defvar *asv-boundps*)
325 (defun expand-defmethod (name
332 (let ((*make-instance-function-keys* nil)
333 (*optimize-asv-funcall-p* t)
334 (*asv-readers* nil) (*asv-writers* nil) (*asv-boundps* nil))
335 (declare (special *make-instance-function-keys*))
336 (multiple-value-bind (method-lambda unspecialized-lambda-list specializers)
337 (add-method-declarations name qualifiers lambda-list body env)
338 (multiple-value-bind (method-function-lambda initargs)
339 (make-method-lambda proto-gf proto-method method-lambda env)
340 (let ((initargs-form (make-method-initargs-form proto-gf
342 method-function-lambda
346 ;; Note: We could DECLAIM the ftype of the generic
347 ;; function here, since ANSI specifies that we create it
348 ;; if it does not exist. However, I chose not to, because
349 ;; I think it's more useful to support a style of
350 ;; programming where every generic function has an
351 ;; explicit DEFGENERIC and any typos in DEFMETHODs are
352 ;; warned about. Otherwise
353 ;; (DEFGENERIC FOO-BAR-BLETCH ((X T)))
354 ;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
355 ;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
356 ;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
357 ;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
358 ;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
359 ;; compiles without raising an error and runs without
360 ;; raising an error (since SIMPLE-VECTOR cases fall
361 ;; through to VECTOR) but still doesn't do what was
362 ;; intended. I hate that kind of bug (code which silently
363 ;; gives the wrong answer), so we don't do a DECLAIM
364 ;; here. -- WHN 20000229
365 ,@(when *make-instance-function-keys*
366 `((get-make-instance-functions
367 ',*make-instance-function-keys*)))
368 ,@(when (or *asv-readers* *asv-writers* *asv-boundps*)
369 `((initialize-internal-slot-gfs*
370 ',*asv-readers* ',*asv-writers* ',*asv-boundps*)))
371 ,(make-defmethod-form name qualifiers specializers
372 unspecialized-lambda-list
374 (class-name (class-of proto-method))
377 (getf (getf initargs :plist)
378 :pv-table-symbol))))))))
380 (defun interned-symbol-p (x)
381 (and (symbolp x) (symbol-package x)))
383 (defun make-defmethod-form (name qualifiers specializers
384 unspecialized-lambda-list method-class-name
385 initargs-form &optional pv-table-symbol)
388 (if (and (interned-symbol-p (fun-name-block-name name))
389 (every #'interned-symbol-p qualifiers)
392 (and (eq (car s) 'eql)
394 (let ((sv (eval (cadr s))))
395 (or (interned-symbol-p sv)
398 (standard-char-p sv)))))
399 (interned-symbol-p s)))
401 (consp initargs-form)
402 (eq (car initargs-form) 'list*)
403 (memq (cadr initargs-form) '(:function :fast-function))
404 (consp (setq fn (caddr initargs-form)))
405 (eq (car fn) 'function)
406 (consp (setq fn-lambda (cadr fn)))
407 (eq (car fn-lambda) 'lambda))
408 (let* ((specls (mapcar (lambda (specl)
410 `(,(car specl) ,(eval (cadr specl)))
413 (mname `(,(if (eq (cadr initargs-form) :function)
414 'method 'fast-method)
415 ,name ,@qualifiers ,specls))
416 (mname-sym (intern (let ((*print-pretty* nil)
417 ;; (We bind *PACKAGE* to
418 ;; KEYWORD here as a way to
419 ;; force symbols to be printed
420 ;; with explicit package
422 (*package* *keyword-package*))
423 (format nil "~S" mname)))))
425 (defun ,mname-sym ,(cadr fn-lambda)
427 ,(make-defmethod-form-internal
428 name qualifiers `',specls
429 unspecialized-lambda-list method-class-name
430 `(list* ,(cadr initargs-form)
432 ,@(cdddr initargs-form))
434 (make-defmethod-form-internal
436 `(list ,@(mapcar (lambda (specializer)
437 (if (consp specializer)
438 ``(,',(car specializer)
439 ,,(cadr specializer))
442 unspecialized-lambda-list
447 (defun make-defmethod-form-internal
448 (name qualifiers specializers-form unspecialized-lambda-list
449 method-class-name initargs-form &optional pv-table-symbol)
455 ',unspecialized-lambda-list
457 ;; Paper over a bug in KCL by passing the cache-symbol here in
458 ;; addition to in the list. FIXME: We should no longer need to do
459 ;; this, since the CLOS code is now SBCL-specific, and doesn't
460 ;; need to be ported to every buggy compiler in existence.
463 (defmacro make-method-function (method-lambda &environment env)
464 (make-method-function-internal method-lambda env))
466 (defun make-method-function-internal (method-lambda &optional env)
467 (multiple-value-bind (proto-gf proto-method)
468 (prototypes-for-make-method-lambda nil)
469 (multiple-value-bind (method-function-lambda initargs)
470 (make-method-lambda proto-gf proto-method method-lambda env)
471 (make-method-initargs-form proto-gf
473 method-function-lambda
477 (defun add-method-declarations (name qualifiers lambda-list body env)
478 (multiple-value-bind (parameters unspecialized-lambda-list specializers)
479 (parse-specialized-lambda-list lambda-list)
480 (declare (ignore parameters))
481 (multiple-value-bind (real-body declarations documentation)
482 (parse-body body env)
483 (values `(lambda ,unspecialized-lambda-list
484 ,@(when documentation `(,documentation))
485 ;; (Old PCL code used a somewhat different style of
486 ;; list for %METHOD-NAME values. Our names use
487 ;; ,@QUALIFIERS instead of ,QUALIFIERS so that the
488 ;; method names look more like what you see in a
491 ;; FIXME: As of sbcl-0.7.0.6, code elsewhere, at
492 ;; least the code to set up named BLOCKs around the
493 ;; bodies of methods, depends on the function's base
494 ;; name being the first element of the %METHOD-NAME
495 ;; list. It would be good to remove this dependency,
496 ;; perhaps by building the BLOCK here, or by using
497 ;; another declaration (e.g. %BLOCK-NAME), so that
498 ;; our method debug names are free to have any format,
499 ;; e.g. (:METHOD PRINT-OBJECT :AROUND (CLOWN T)).
500 (declare (%method-name (,name
503 (declare (%method-lambda-list ,@lambda-list))
506 unspecialized-lambda-list specializers))))
508 (defun real-make-method-initargs-form (proto-gf proto-method
509 method-lambda initargs env)
510 (declare (ignore proto-gf proto-method))
511 (unless (and (consp method-lambda)
512 (eq (car method-lambda) 'lambda))
513 (error "The METHOD-LAMBDA argument to MAKE-METHOD-FUNCTION, ~S, ~
514 is not a lambda form."
516 (make-method-initargs-form-internal method-lambda initargs env))
518 (unless (fboundp 'make-method-initargs-form)
519 (setf (gdefinition 'make-method-initargs-form)
520 (symbol-function 'real-make-method-initargs-form)))
522 (defun real-make-method-lambda (proto-gf proto-method method-lambda env)
523 (declare (ignore proto-gf proto-method))
524 (make-method-lambda-internal method-lambda env))
526 ;;; a helper function for creating Python-friendly type declarations
527 ;;; in DEFMETHOD forms
528 (defun parameter-specializer-declaration-in-defmethod (parameter specializer)
529 (cond ((and (consp specializer)
530 (eq (car specializer) 'eql))
531 ;; KLUDGE: ANSI, in its wisdom, says that
532 ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at
533 ;; DEFMETHOD expansion time. Thus, although one might think
535 ;; (DEFMETHOD FOO ((X PACKAGE)
538 ;; the PACKAGE and (EQL 12) forms are both parallel type
539 ;; names, they're not, as is made clear when you do
540 ;; (DEFMETHOD FOO ((X PACKAGE)
543 ;; where Y needs to be a symbol named "BAR", not some cons
544 ;; made by (CONS 'QUOTE 'BAR). I.e. when the
545 ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument
546 ;; to be of type (EQL X). It'd be easy to transform one to
547 ;; the other, but it'd be somewhat messier to do so while
548 ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd
549 ;; once. (The new code wouldn't be messy, but it'd require a
550 ;; big transformation of the old code.) So instead we punt.
554 ;; KLUDGE: For some low-level implementation
555 ;; classes, perhaps because of some problems related
556 ;; to the incomplete integration of PCL into SBCL's
557 ;; type system, some specializer classes can't be
558 ;; declared as argument types. E.g.
559 ;; (DEFMETHOD FOO ((X SLOT-OBJECT))
560 ;; (DECLARE (TYPE SLOT-OBJECT X))
563 ;; (DEFSTRUCT BAR A B)
565 ;; perhaps because of the way that STRUCTURE-OBJECT
566 ;; inherits both from SLOT-OBJECT and from
567 ;; SB-KERNEL:INSTANCE. In an effort to sweep such
568 ;; problems under the rug, we exclude these problem
569 ;; cases by blacklisting them here. -- WHN 2001-01-19
572 ((not (eq *boot-state* 'complete))
573 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
574 ;; types which don't match their specializers. (Specifically,
575 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
576 ;; second argument.) Hopefully it only does this kind of
577 ;; weirdness when bootstrapping.. -- WHN 20000610
580 ;; Otherwise, we can make Python very happy.
581 `(type ,specializer ,parameter))))
583 (defun make-method-lambda-internal (method-lambda &optional env)
584 (unless (and (consp method-lambda) (eq (car method-lambda) 'lambda))
585 (error "The METHOD-LAMBDA argument to MAKE-METHOD-LAMBDA, ~S, ~
586 is not a lambda form."
588 (multiple-value-bind (real-body declarations documentation)
589 (parse-body (cddr method-lambda) env)
590 (let* ((name-decl (get-declaration '%method-name declarations))
591 (sll-decl (get-declaration '%method-lambda-list declarations))
592 (method-name (when (consp name-decl) (car name-decl)))
593 (generic-function-name (when method-name (car method-name)))
594 (specialized-lambda-list (or sll-decl (cadr method-lambda))))
595 (multiple-value-bind (parameters lambda-list specializers)
596 (parse-specialized-lambda-list specialized-lambda-list)
597 (let* ((required-parameters
598 (mapcar (lambda (r s) (declare (ignore s)) r)
601 (slots (mapcar #'list required-parameters))
605 ;; These declarations seem to be used by PCL to pass
606 ;; information to itself; when I tried to delete 'em
607 ;; ca. 0.6.10 it didn't work. I'm not sure how
608 ;; they work, but note the (VAR-DECLARATION '%CLASS ..)
609 ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30
611 (mapcar (lambda (a s) (and (symbolp s)
616 ;; These TYPE declarations weren't in the original
617 ;; PCL code, but the Python compiler likes them a
618 ;; lot. (We're telling the compiler about our
619 ;; knowledge of specialized argument types so that
620 ;; it can avoid run-time type dispatch overhead,
621 ;; which can be a huge win for Python.)
623 ;; FIXME: Perhaps these belong in
624 ;; ADD-METHOD-DECLARATIONS instead of here?
625 ,@(mapcar #'parameter-specializer-declaration-in-defmethod
629 ;; Remove the documentation string and insert the
630 ;; appropriate class declarations. The documentation
631 ;; string is removed to make it easy for us to insert
632 ;; new declarations later, they will just go after the
633 ;; CADR of the method lambda. The class declarations
634 ;; are inserted to communicate the class of the method's
635 ;; arguments to the code walk.
636 `(lambda ,lambda-list
637 ;; The default ignorability of method parameters
638 ;; doesn't seem to be specified by ANSI. PCL had
639 ;; them basically ignorable but was a little
640 ;; inconsistent. E.g. even though the two
641 ;; method definitions
642 ;; (DEFMETHOD FOO ((X T) (Y T)) "Z")
643 ;; (DEFMETHOD FOO ((X T) Y) "Z")
644 ;; are otherwise equivalent, PCL treated Y as
645 ;; ignorable in the first definition but not in the
646 ;; second definition. We make all required
647 ;; parameters ignorable as a way of systematizing
648 ;; the old PCL behavior. -- WHN 2000-11-24
649 (declare (ignorable ,@required-parameters))
652 (block ,(fun-name-block-name generic-function-name)
654 (constant-value-p (and (null (cdr real-body))
655 (constantp (car real-body))))
656 (constant-value (and constant-value-p
657 (eval (car real-body))))
658 (plist (and constant-value-p
659 (or (typep constant-value
660 '(or number character))
661 (and (symbolp constant-value)
662 (symbol-package constant-value)))
663 (list :constant-value constant-value)))
664 (applyp (dolist (p lambda-list nil)
665 (cond ((memq p '(&optional &rest &key))
670 (walked-lambda call-next-method-p closurep next-method-p-p)
671 (walk-method-lambda method-lambda
676 (multiple-value-bind (walked-lambda-body
678 walked-documentation)
679 (parse-body (cddr walked-lambda) env)
680 (declare (ignore walked-documentation))
681 (when (or next-method-p-p call-next-method-p)
682 (setq plist (list* :needs-next-methods-p t plist)))
683 (when (some #'cdr slots)
684 (multiple-value-bind (slot-name-lists call-list)
685 (slot-name-lists-from-slots slots calls)
686 (let ((pv-table-symbol (make-symbol "pv-table")))
688 `(,@(when slot-name-lists
689 `(:slot-name-lists ,slot-name-lists))
691 `(:call-list ,call-list))
692 :pv-table-symbol ,pv-table-symbol
694 (setq walked-lambda-body
695 `((pv-binding (,required-parameters
698 ,@walked-lambda-body))))))
699 (when (and (memq '&key lambda-list)
700 (not (memq '&allow-other-keys lambda-list)))
701 (let ((aux (memq '&aux lambda-list)))
702 (setq lambda-list (nconc (ldiff lambda-list aux)
703 (list '&allow-other-keys)
705 (values `(lambda (.method-args. .next-methods.)
706 (simple-lexical-method-functions
707 (,lambda-list .method-args. .next-methods.
710 :next-method-p-p ,next-method-p-p
713 ,@walked-declarations
714 ,@walked-lambda-body))
717 ,@(when documentation
718 `(:documentation ,documentation)))))))))))
720 (unless (fboundp 'make-method-lambda)
721 (setf (gdefinition 'make-method-lambda)
722 (symbol-function 'real-make-method-lambda)))
724 (defmacro simple-lexical-method-functions ((lambda-list
730 ,method-args ,next-methods
731 (bind-simple-lexical-method-macros (,method-args ,next-methods)
732 (bind-lexical-method-functions (,@lmf-options)
733 (bind-args (,lambda-list ,method-args)
736 (defmacro fast-lexical-method-functions ((lambda-list
742 `(bind-fast-lexical-method-macros (,args ,rest-arg ,next-method-call)
743 (bind-lexical-method-functions (,@lmf-options)
744 (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg)
747 (defmacro bind-simple-lexical-method-macros ((method-args next-methods)
749 `(macrolet ((call-next-method-bind (&body body)
750 `(let ((.next-method. (car ,',next-methods))
751 (,',next-methods (cdr ,',next-methods)))
752 .next-method. ,',next-methods
754 (call-next-method-body (cnm-args)
756 (funcall (if (std-instance-p .next-method.)
757 (method-function .next-method.)
758 .next-method.) ; for early methods
759 (or ,cnm-args ,',method-args)
761 (error "no next method")))
762 (next-method-p-body ()
763 `(not (null .next-method.))))
766 (defstruct (method-call (:copier nil))
767 (function #'identity :type function)
770 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
772 (defmacro invoke-method-call1 (function args cm-args)
773 `(let ((.function. ,function)
775 (.cm-args. ,cm-args))
776 (if (and .cm-args. (null (cdr .cm-args.)))
777 (funcall .function. .args. (car .cm-args.))
778 (apply .function. .args. .cm-args.))))
780 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
781 `(invoke-method-call1 (method-call-function ,method-call)
783 `(list* ,@required-args+rest-arg)
784 `(list ,@required-args+rest-arg))
785 (method-call-call-method-args ,method-call)))
787 (defstruct (fast-method-call (:copier nil))
788 (function #'identity :type function)
793 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
795 (defmacro fmc-funcall (fn pv-cell next-method-call &rest args)
796 `(funcall ,fn ,pv-cell ,next-method-call ,@args))
798 (defmacro invoke-fast-method-call (method-call &rest required-args+rest-arg)
799 `(fmc-funcall (fast-method-call-function ,method-call)
800 (fast-method-call-pv-cell ,method-call)
801 (fast-method-call-next-method-call ,method-call)
802 ,@required-args+rest-arg))
804 (defstruct (fast-instance-boundp (:copier nil))
805 (index 0 :type fixnum))
807 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
809 (eval-when (:compile-toplevel :load-toplevel :execute)
810 (defvar *allow-emf-call-tracing-p* nil)
811 (defvar *enable-emf-call-tracing-p* #-sb-show nil #+sb-show t))
813 ;;;; effective method functions
815 (defvar *emf-call-trace-size* 200)
816 (defvar *emf-call-trace* nil)
817 (defvar *emf-call-trace-index* 0)
819 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
820 ;;; without explanation. It appears to be intended for debugging, so
821 ;;; it might be useful someday, so I haven't deleted it.
822 ;;; But it isn't documented and isn't used for anything now, so
823 ;;; I've conditionalized it out of the base system. -- WHN 19991213
825 (defun show-emf-call-trace ()
826 (when *emf-call-trace*
827 (let ((j *emf-call-trace-index*)
828 (*enable-emf-call-tracing-p* nil))
829 (format t "~&(The oldest entries are printed first)~%")
830 (dotimes-fixnum (i *emf-call-trace-size*)
831 (let ((ct (aref *emf-call-trace* j)))
832 (when ct (print ct)))
834 (when (= j *emf-call-trace-size*)
837 (defun trace-emf-call-internal (emf format args)
838 (unless *emf-call-trace*
839 (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
840 (setf (aref *emf-call-trace* *emf-call-trace-index*)
841 (list* emf format args))
842 (incf *emf-call-trace-index*)
843 (when (= *emf-call-trace-index* *emf-call-trace-size*)
844 (setq *emf-call-trace-index* 0)))
846 (defmacro trace-emf-call (emf format args)
847 (when *allow-emf-call-tracing-p*
848 `(when *enable-emf-call-tracing-p*
849 (trace-emf-call-internal ,emf ,format ,args))))
851 (defmacro invoke-effective-method-function-fast
852 (emf restp &rest required-args+rest-arg)
854 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
855 (invoke-fast-method-call ,emf ,@required-args+rest-arg)))
857 (defmacro invoke-effective-method-function (emf restp
858 &rest required-args+rest-arg)
859 (unless (constantp restp)
860 (error "The RESTP argument is not constant."))
861 ;; FIXME: The RESTP handling here is confusing and maybe slightly
862 ;; broken if RESTP evaluates to a non-self-evaluating form. E.g. if
863 ;; (INVOKE-EFFECTIVE-METHOD-FUNCTION EMF '(ERROR "gotcha") ...)
864 ;; then TRACE-EMF-CALL-CALL-INTERNAL might die on a gotcha error.
865 (setq restp (eval restp))
867 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
868 (cond ((typep ,emf 'fast-method-call)
869 (invoke-fast-method-call ,emf ,@required-args+rest-arg))
870 ;; "What," you may wonder, "do these next two clauses do?"
871 ;; In that case, you are not a PCL implementor, for they
872 ;; considered this to be self-documenting.:-| Or CSR, for
873 ;; that matter, since he can also figure it out by looking
874 ;; at it without breaking stride. For the rest of us,
875 ;; though: From what the code is doing with .SLOTS. and
876 ;; whatnot, evidently it's implementing SLOT-VALUEish and
877 ;; GET-SLOT-VALUEish things. Then we can reason backwards
878 ;; and conclude that setting EMF to a FIXNUM is an
879 ;; optimized way to represent these slot access operations.
880 ,@(when (and (null restp) (= 1 (length required-args+rest-arg)))
881 `(((typep ,emf 'fixnum)
882 (let* ((.slots. (get-slots-or-nil
883 ,(car required-args+rest-arg)))
884 (value (when .slots. (clos-slots-ref .slots. ,emf))))
885 (if (eq value +slot-unbound+)
886 (slot-unbound-internal ,(car required-args+rest-arg)
889 ,@(when (and (null restp) (= 2 (length required-args+rest-arg)))
890 `(((typep ,emf 'fixnum)
891 (let ((.new-value. ,(car required-args+rest-arg))
892 (.slots. (get-slots-or-nil
893 ,(car required-args+rest-arg))))
895 (setf (clos-slots-ref .slots. ,emf) .new-value.))))))
896 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
897 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
898 ;; there was no explanation and presumably the code is 10+
899 ;; years stale, I simply deleted it. -- WHN)
903 (invoke-method-call ,emf ,restp ,@required-args+rest-arg))
906 `(apply (the function ,emf) ,@required-args+rest-arg)
907 `(funcall (the function ,emf)
908 ,@required-args+rest-arg))))))))
910 (defun invoke-emf (emf args)
911 (trace-emf-call emf t args)
914 (let* ((arg-info (fast-method-call-arg-info emf))
915 (restp (cdr arg-info))
916 (nreq (car arg-info)))
918 (let* ((rest-args (nthcdr nreq args))
919 (req-args (ldiff args rest-args)))
920 (apply (fast-method-call-function emf)
921 (fast-method-call-pv-cell emf)
922 (fast-method-call-next-method-call emf)
923 (nconc req-args (list rest-args))))
926 (invoke-fast-method-call emf)
927 (error "wrong number of args")))
930 (invoke-fast-method-call emf (car args))
931 (error "wrong number of args")))
934 (invoke-fast-method-call emf (car args) (cadr args))
935 (error "wrong number of args")))
937 (apply (fast-method-call-function emf)
938 (fast-method-call-pv-cell emf)
939 (fast-method-call-next-method-call emf)
942 (apply (method-call-function emf)
944 (method-call-call-method-args emf)))
946 (cond ((null args) (error "1 or 2 args were expected."))
948 (let* ((slots (get-slots (car args)))
949 (value (clos-slots-ref slots emf)))
950 (if (eq value +slot-unbound+)
951 (slot-unbound-internal (car args) emf)
954 (setf (clos-slots-ref (get-slots (cadr args)) emf)
956 (t (error "1 or 2 args were expected."))))
957 (fast-instance-boundp
958 (if (or (null args) (cdr args))
959 (error "1 arg was expected.")
960 (let ((slots (get-slots (car args))))
961 (not (eq (clos-slots-ref slots
962 (fast-instance-boundp-index emf))
967 (defmacro bind-fast-lexical-method-macros ((args rest-arg next-method-call)
969 `(macrolet ((narrowed-emf (emf)
970 ;; INVOKE-EFFECTIVE-METHOD-FUNCTION has code in it to
971 ;; dispatch on the possibility that EMF might be of
972 ;; type FIXNUM (as an optimized representation of a
973 ;; slot accessor). But as far as I (WHN 2002-06-11)
974 ;; can tell, it's impossible for such a representation
975 ;; to end up as .NEXT-METHOD-CALL. By reassuring
976 ;; INVOKE-E-M-F that when called from this context
977 ;; it needn't worry about the FIXNUM case, we can
978 ;; keep those cases from being compiled, which is
979 ;; good both because it saves bytes and because it
980 ;; avoids annoying type mismatch compiler warnings.
982 ;; KLUDGE: In sbcl-0.7.4.29, the compiler's type
983 ;; system isn't smart enough about NOT and intersection
984 ;; types to benefit from a (NOT FIXNUM) declaration
985 ;; here. -- WHN 2002-06-12
987 ;; FIXME: Might the FUNCTION type be omittable here,
988 ;; leaving only METHOD-CALLs? Failing that, could this
989 ;; be documented somehow? (It'd be nice if the types
990 ;; involved could be understood without solving the
992 `(the (or function method-call fast-method-call)
994 (call-next-method-bind (&body body)
996 (call-next-method-body (cnm-args)
997 `(if ,',next-method-call
999 ;; This declaration suppresses a "deleting
1000 ;; unreachable code" note for the following IF when
1001 ;; REST-ARG is NIL. It is not nice for debugging
1002 ;; SBCL itself, but at least it keeps us from
1004 (declare (optimize (inhibit-warnings 3)))
1005 (if (and (null ',rest-arg)
1007 (eq (car cnm-args) 'list))
1008 `(invoke-effective-method-function
1009 (narrowed-emf ,',next-method-call)
1012 (let ((call `(invoke-effective-method-function
1013 (narrowed-emf ,',next-method-call)
1014 ,',(not (null rest-arg))
1016 ,@',(when rest-arg `(,rest-arg)))))
1018 (bind-args ((,@',args
1020 `(&rest ,rest-arg)))
1024 (error "no next method")))
1025 (next-method-p-body ()
1026 `(not (null ,',next-method-call))))
1029 (defmacro bind-lexical-method-functions
1030 ((&key call-next-method-p next-method-p-p closurep applyp)
1032 (cond ((and (null call-next-method-p) (null next-method-p-p)
1036 ((and (null closurep)
1038 ;; OK to use MACROLET, and all args are mandatory
1039 ;; (else APPLYP would be true).
1040 `(call-next-method-bind
1041 (macrolet ((call-next-method (&rest cnm-args)
1042 `(call-next-method-body ,(when cnm-args
1043 `(list ,@cnm-args))))
1045 `(next-method-p-body)))
1048 `(call-next-method-bind
1049 (flet (,@(and call-next-method-p
1050 '((call-next-method (&rest cnm-args)
1051 (call-next-method-body cnm-args))))
1052 ,@(and next-method-p-p
1054 (next-method-p-body)))))
1057 (defmacro bind-args ((lambda-list args) &body body)
1058 (let ((args-tail '.args-tail.)
1061 (flet ((process-var (var)
1062 (if (memq var lambda-list-keywords)
1065 (&optional (setq state 'optional))
1066 (&key (setq state 'key))
1068 (&rest (setq state 'rest))
1069 (&aux (setq state 'aux))
1072 "encountered the non-standard lambda list keyword ~S"
1076 (required `((,var (pop ,args-tail))))
1077 (optional (cond ((not (consp var))
1078 `((,var (when ,args-tail
1079 (pop ,args-tail)))))
1081 `((,(car var) (if ,args-tail
1085 `((,(caddr var) ,args-tail)
1086 (,(car var) (if ,args-tail
1089 (rest `((,var ,args-tail)))
1090 (key (cond ((not (consp var))
1091 `((,var (get-key-arg ,(keywordicate var)
1094 (multiple-value-bind (keyword variable)
1095 (if (consp (car var))
1098 (values (keywordicate (car var))
1100 `((,key (get-key-arg1 ',keyword ,args-tail))
1101 (,variable (if (consp ,key)
1105 (multiple-value-bind (keyword variable)
1106 (if (consp (car var))
1109 (values (keywordicate (car var))
1111 `((,key (get-key-arg1 ',keyword ,args-tail))
1113 (,variable (if (consp ,key)
1117 (let ((bindings (mapcan #'process-var lambda-list)))
1118 `(let* ((,args-tail ,args)
1120 (declare (ignorable ,args-tail))
1123 (defun get-key-arg (keyword list)
1124 (loop (when (atom list) (return nil))
1125 (when (eq (car list) keyword) (return (cadr list)))
1126 (setq list (cddr list))))
1128 (defun get-key-arg1 (keyword list)
1129 (loop (when (atom list) (return nil))
1130 (when (eq (car list) keyword) (return (cdr list)))
1131 (setq list (cddr list))))
1133 (defun walk-method-lambda (method-lambda required-parameters env slots calls)
1134 (let ((call-next-method-p nil) ; flag indicating that CALL-NEXT-METHOD
1135 ; should be in the method definition
1136 (closurep nil) ; flag indicating that #'CALL-NEXT-METHOD
1137 ; was seen in the body of a method
1138 (next-method-p-p nil)) ; flag indicating that NEXT-METHOD-P
1139 ; should be in the method definition
1140 (flet ((walk-function (form context env)
1141 (cond ((not (eq context :eval)) form)
1142 ;; FIXME: Jumping to a conclusion from the way it's used
1143 ;; above, perhaps CONTEXT should be called SITUATION
1144 ;; (after the term used in the ANSI specification of
1145 ;; EVAL-WHEN) and given modern ANSI keyword values
1146 ;; like :LOAD-TOPLEVEL.
1147 ((not (listp form)) form)
1148 ((eq (car form) 'call-next-method)
1149 (setq call-next-method-p t)
1151 ((eq (car form) 'next-method-p)
1152 (setq next-method-p-p t)
1154 ((and (eq (car form) 'function)
1155 (cond ((eq (cadr form) 'call-next-method)
1156 (setq call-next-method-p t)
1159 ((eq (cadr form) 'next-method-p)
1160 (setq next-method-p-p t)
1164 ((and (memq (car form)
1165 '(slot-value set-slot-value slot-boundp))
1166 (constantp (caddr form)))
1167 (let ((parameter (can-optimize-access form
1170 (let ((fun (ecase (car form)
1171 (slot-value #'optimize-slot-value)
1172 (set-slot-value #'optimize-set-slot-value)
1173 (slot-boundp #'optimize-slot-boundp))))
1174 (funcall fun slots parameter form))))
1175 ((and (eq (car form) 'apply)
1177 (eq (car (cadr form)) 'function)
1178 (generic-function-name-p (cadr (cadr form))))
1179 (optimize-generic-function-call
1180 form required-parameters env slots calls))
1181 ((generic-function-name-p (car form))
1182 (optimize-generic-function-call
1183 form required-parameters env slots calls))
1184 ((and (eq (car form) 'asv-funcall)
1185 *optimize-asv-funcall-p*)
1187 (reader (push (third form) *asv-readers*))
1188 (writer (push (third form) *asv-writers*))
1189 (boundp (push (third form) *asv-boundps*)))
1190 `(,(second form) ,@(cddddr form)))
1193 (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1194 (values walked-lambda
1197 next-method-p-p)))))
1199 (defun generic-function-name-p (name)
1200 (and (legal-fun-name-p name)
1202 (if (eq *boot-state* 'complete)
1203 (standard-generic-function-p (gdefinition name))
1204 (funcallable-instance-p (gdefinition name)))))
1206 (defvar *method-function-plist* (make-hash-table :test 'eq))
1209 (defvar *mf1cp* nil)
1212 (defvar *mf2cp* nil)
1214 (defun method-function-plist (method-function)
1215 (unless (eq method-function *mf1*)
1216 (rotatef *mf1* *mf2*)
1217 (rotatef *mf1p* *mf2p*)
1218 (rotatef *mf1cp* *mf2cp*))
1219 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1220 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1221 (unless (eq method-function *mf1*)
1222 (setf *mf1* method-function
1224 *mf1p* (gethash method-function *method-function-plist*)))
1227 (defun (setf method-function-plist)
1228 (val method-function)
1229 (unless (eq method-function *mf1*)
1230 (rotatef *mf1* *mf2*)
1231 (rotatef *mf1cp* *mf2cp*)
1232 (rotatef *mf1p* *mf2p*))
1233 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1234 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1235 (setf *mf1* method-function
1239 (defun method-function-get (method-function key &optional default)
1240 (getf (method-function-plist method-function) key default))
1242 (defun (setf method-function-get)
1243 (val method-function key)
1244 (setf (getf (method-function-plist method-function) key) val))
1246 (defun method-function-pv-table (method-function)
1247 (method-function-get method-function :pv-table))
1249 (defun method-function-method (method-function)
1250 (method-function-get method-function :method))
1252 (defun method-function-needs-next-methods-p (method-function)
1253 (method-function-get method-function :needs-next-methods-p t))
1255 (defmacro method-function-closure-generator (method-function)
1256 `(method-function-get ,method-function 'closure-generator))
1258 (defun load-defmethod
1259 (class name quals specls ll initargs &optional pv-table-symbol)
1260 (setq initargs (copy-tree initargs))
1261 (let ((method-spec (or (getf initargs :method-spec)
1262 (make-method-spec name quals specls))))
1263 (setf (getf initargs :method-spec) method-spec)
1264 (load-defmethod-internal class name quals specls
1265 ll initargs pv-table-symbol)))
1267 (defun load-defmethod-internal
1268 (method-class gf-spec qualifiers specializers lambda-list
1269 initargs pv-table-symbol)
1270 (when pv-table-symbol
1271 (setf (getf (getf initargs :plist) :pv-table-symbol)
1273 (when (and (eq *boot-state* 'complete)
1275 (let* ((gf (fdefinition gf-spec))
1276 (method (and (generic-function-p gf)
1279 (parse-specializers specializers)
1282 (sb-kernel::style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1283 gf-spec qualifiers specializers))))
1284 (let ((method (apply #'add-named-method
1285 gf-spec qualifiers specializers lambda-list
1286 :definition-source `((defmethod ,gf-spec
1291 (unless (or (eq method-class 'standard-method)
1292 (eq (find-class method-class nil) (class-of method)))
1293 ;; FIXME: should be STYLE-WARNING?
1294 (format *error-output*
1295 "~&At the time the method with qualifiers ~:S and~%~
1296 specializers ~:S on the generic function ~S~%~
1297 was compiled, the method-class for that generic function was~%~
1298 ~S. But, the method class is now ~S, this~%~
1299 may mean that this method was compiled improperly.~%"
1300 qualifiers specializers gf-spec
1301 method-class (class-name (class-of method))))
1304 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1305 `(method ,gf-spec ,@qualifiers ,unparsed-specializers))
1307 (defun initialize-method-function (initargs &optional return-function-p method)
1308 (let* ((mf (getf initargs :function))
1309 (method-spec (getf initargs :method-spec))
1310 (plist (getf initargs :plist))
1311 (pv-table-symbol (getf plist :pv-table-symbol))
1313 (mff (getf initargs :fast-function)))
1314 (flet ((set-mf-property (p v)
1316 (setf (method-function-get mf p) v))
1318 (setf (method-function-get mff p) v))))
1321 (setq mf (set-fun-name mf method-spec)))
1323 (let ((name `(,(or (get (car method-spec) 'fast-sym)
1324 (setf (get (car method-spec) 'fast-sym)
1325 ;; KLUDGE: If we're going to be
1326 ;; interning private symbols in our
1327 ;; a this way, it would be cleanest
1328 ;; to use a separate package
1329 ;; %PCL-PRIVATE or something, and
1330 ;; failing that, to use a special
1331 ;; symbol prefix denoting privateness.
1333 (intern (format nil "FAST-~A"
1336 ,@(cdr method-spec))))
1337 (set-fun-name mff name)
1339 (set-mf-property :name name)))))
1341 (let ((snl (getf plist :slot-name-lists))
1342 (cl (getf plist :call-list)))
1344 (setq pv-table (intern-pv-table :slot-name-lists snl
1346 (when pv-table (set pv-table-symbol pv-table))
1347 (set-mf-property :pv-table pv-table)))
1348 (loop (when (null plist) (return nil))
1349 (set-mf-property (pop plist) (pop plist)))
1351 (set-mf-property :method method))
1352 (when return-function-p
1353 (or mf (method-function-from-fast-function mff)))))))
1355 (defun analyze-lambda-list (lambda-list)
1356 (flet (;; FIXME: Is this redundant with SB-C::MAKE-KEYWORD-FOR-ARG?
1357 (parse-key-arg (arg)
1359 (if (listp (car arg))
1361 (keywordicate (car arg)))
1362 (keywordicate arg))))
1368 (allow-other-keys-p nil)
1370 (keyword-parameters ())
1372 (dolist (x lambda-list)
1373 (if (memq x lambda-list-keywords)
1375 (&optional (setq state 'optional))
1378 (&allow-other-keys (setq allow-other-keys-p t))
1379 (&rest (setq restp t
1383 (error "encountered the non-standard lambda list keyword ~S"
1386 (required (incf nrequired))
1387 (optional (incf noptional))
1388 (key (push (parse-key-arg x) keywords)
1389 (push x keyword-parameters))
1390 (rest (incf nrest)))))
1391 (when (and restp (zerop nrest))
1392 (error "Error in lambda-list:~%~
1393 After &REST, a DEFGENERIC lambda-list ~
1394 must be followed by at least one variable."))
1395 (values nrequired noptional keysp restp allow-other-keys-p
1397 (reverse keyword-parameters)))))
1399 (defun keyword-spec-name (x)
1400 (let ((key (if (atom x) x (car x))))
1405 (defun ftype-declaration-from-lambda-list (lambda-list name)
1406 (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1407 keywords keyword-parameters)
1408 (analyze-lambda-list lambda-list)
1409 (declare (ignore keyword-parameters))
1410 (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1411 (old-ftype (if (sb-kernel:fun-type-p old) old nil))
1412 (old-restp (and old-ftype (sb-kernel:fun-type-rest old-ftype)))
1413 (old-keys (and old-ftype
1414 (mapcar #'sb-kernel:key-info-name
1415 (sb-kernel:fun-type-keywords
1417 (old-keysp (and old-ftype (sb-kernel:fun-type-keyp old-ftype)))
1418 (old-allowp (and old-ftype
1419 (sb-kernel:fun-type-allowp old-ftype)))
1420 (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1421 `(function ,(append (make-list nrequired :initial-element t)
1422 (when (plusp noptional)
1423 (append '(&optional)
1424 (make-list noptional :initial-element t)))
1425 (when (or restp old-restp)
1427 (when (or keysp old-keysp)
1429 (mapcar (lambda (key)
1432 (when (or allow-other-keys-p old-allowp)
1433 '(&allow-other-keys)))))
1436 (defun defgeneric-declaration (spec lambda-list)
1438 (setq spec (get-setf-fun-name (cadr spec))))
1439 `(ftype ,(ftype-declaration-from-lambda-list lambda-list spec) ,spec))
1441 ;;;; early generic function support
1443 (defvar *!early-generic-functions* ())
1445 (defun ensure-generic-function (fun-name
1449 (declare (ignore environment))
1450 (let ((existing (and (gboundp fun-name)
1451 (gdefinition fun-name))))
1453 (eq *boot-state* 'complete)
1454 (null (generic-function-p existing)))
1455 (generic-clobbers-function fun-name)
1456 (apply #'ensure-generic-function-using-class
1457 existing fun-name all-keys))))
1459 (defun generic-clobbers-function (fun-name)
1460 (error 'simple-program-error
1461 :format-control "~S already names an ordinary function or a macro."
1462 :format-arguments (list fun-name)))
1464 (defvar *sgf-wrapper*
1465 (boot-make-wrapper (early-class-size 'standard-generic-function)
1466 'standard-generic-function))
1468 (defvar *sgf-slots-init*
1469 (mapcar (lambda (canonical-slot)
1470 (if (memq (getf canonical-slot :name) '(arg-info source))
1472 (let ((initfunction (getf canonical-slot :initfunction)))
1474 (funcall initfunction)
1476 (early-collect-inheritance 'standard-generic-function)))
1478 (defvar *sgf-method-class-index*
1479 (!bootstrap-slot-index 'standard-generic-function 'method-class))
1481 (defun early-gf-p (x)
1482 (and (fsc-instance-p x)
1483 (eq (clos-slots-ref (get-slots x) *sgf-method-class-index*)
1486 (defvar *sgf-methods-index*
1487 (!bootstrap-slot-index 'standard-generic-function 'methods))
1489 (defmacro early-gf-methods (gf)
1490 `(clos-slots-ref (get-slots ,gf) *sgf-methods-index*))
1492 (defvar *sgf-arg-info-index*
1493 (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1495 (defmacro early-gf-arg-info (gf)
1496 `(clos-slots-ref (get-slots ,gf) *sgf-arg-info-index*))
1498 (defvar *sgf-dfun-state-index*
1499 (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1501 (defstruct (arg-info
1503 (:constructor make-arg-info ())
1505 (arg-info-lambda-list :no-lambda-list)
1508 arg-info-number-optional
1510 arg-info-keys ;nil no &KEY or &REST allowed
1511 ;(k1 k2 ..) Each method must accept these &KEY arguments.
1512 ;T must have &KEY or &REST
1514 gf-info-simple-accessor-type ; nil, reader, writer, boundp
1515 (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1517 gf-info-static-c-a-m-emf
1518 (gf-info-c-a-m-emf-std-p t)
1521 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1523 (defun arg-info-valid-p (arg-info)
1524 (not (null (arg-info-number-optional arg-info))))
1526 (defun arg-info-applyp (arg-info)
1527 (or (plusp (arg-info-number-optional arg-info))
1528 (arg-info-key/rest-p arg-info)))
1530 (defun arg-info-number-required (arg-info)
1531 (length (arg-info-metatypes arg-info)))
1533 (defun arg-info-nkeys (arg-info)
1534 (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1536 ;;; Keep pages clean by not setting if the value is already the same.
1537 (defmacro esetf (pos val)
1538 (let ((valsym (gensym "value")))
1539 `(let ((,valsym ,val))
1540 (unless (equal ,pos ,valsym)
1541 (setf ,pos ,valsym)))))
1543 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1544 argument-precedence-order)
1545 (let* ((arg-info (if (eq *boot-state* 'complete)
1547 (early-gf-arg-info gf)))
1548 (methods (if (eq *boot-state* 'complete)
1549 (generic-function-methods gf)
1550 (early-gf-methods gf)))
1551 (was-valid-p (integerp (arg-info-number-optional arg-info)))
1552 (first-p (and new-method (null (cdr methods)))))
1553 (when (and (not lambda-list-p) methods)
1554 (setq lambda-list (gf-lambda-list gf)))
1555 (when (or lambda-list-p
1557 (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1558 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1559 (analyze-lambda-list lambda-list)
1560 (when (and methods (not first-p))
1561 (let ((gf-nreq (arg-info-number-required arg-info))
1562 (gf-nopt (arg-info-number-optional arg-info))
1563 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1564 (unless (and (= nreq gf-nreq)
1566 (eq (or keysp restp) gf-key/rest-p))
1567 (error "The lambda-list ~S is incompatible with ~
1568 existing methods of ~S."
1571 (esetf (arg-info-lambda-list arg-info) lambda-list))
1572 (when (or lambda-list-p argument-precedence-order
1573 (null (arg-info-precedence arg-info)))
1574 (esetf (arg-info-precedence arg-info)
1575 (compute-precedence lambda-list nreq
1576 argument-precedence-order)))
1577 (esetf (arg-info-metatypes arg-info) (make-list nreq))
1578 (esetf (arg-info-number-optional arg-info) nopt)
1579 (esetf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1580 (esetf (arg-info-keys arg-info)
1582 (if allow-other-keys-p t keywords)
1583 (arg-info-key/rest-p arg-info)))))
1585 (check-method-arg-info gf arg-info new-method))
1586 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1589 (defun check-method-arg-info (gf arg-info method)
1590 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1591 (analyze-lambda-list (if (consp method)
1592 (early-method-lambda-list method)
1593 (method-lambda-list method)))
1594 (flet ((lose (string &rest args)
1596 "attempt to add the method ~S to the generic function ~S.~%~
1600 (apply #'format nil string args)))
1601 (comparison-description (x y)
1602 (if (> x y) "more" "fewer")))
1603 (let ((gf-nreq (arg-info-number-required arg-info))
1604 (gf-nopt (arg-info-number-optional arg-info))
1605 (gf-key/rest-p (arg-info-key/rest-p arg-info))
1606 (gf-keywords (arg-info-keys arg-info)))
1607 (unless (= nreq gf-nreq)
1609 "the method has ~A required arguments than the generic function."
1610 (comparison-description nreq gf-nreq)))
1611 (unless (= nopt gf-nopt)
1613 "the method has ~A optional arguments than the generic function."
1614 (comparison-description nopt gf-nopt)))
1615 (unless (eq (or keysp restp) gf-key/rest-p)
1617 "The method and generic function differ in whether they accept~%~
1618 &REST or &KEY arguments."))
1619 (when (consp gf-keywords)
1620 (unless (or (and restp (not keysp))
1622 (every (lambda (k) (memq k keywords)) gf-keywords))
1623 (lose "the method does not accept each of the &KEY arguments~%~
1627 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1628 (let* ((existing-p (and methods (cdr methods) new-method))
1629 (nreq (length (arg-info-metatypes arg-info)))
1630 (metatypes (if existing-p
1631 (arg-info-metatypes arg-info)
1633 (type (if existing-p
1634 (gf-info-simple-accessor-type arg-info)
1636 (when (arg-info-valid-p arg-info)
1637 (dolist (method (if new-method (list new-method) methods))
1638 (let* ((specializers (if (or (eq *boot-state* 'complete)
1639 (not (consp method)))
1640 (method-specializers method)
1641 (early-method-specializers method t)))
1642 (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1644 (early-method-class method)))
1645 (new-type (when (and class
1646 (or (not (eq *boot-state* 'complete))
1647 (eq (generic-function-method-combination gf)
1648 *standard-method-combination*)))
1649 (cond ((eq class *the-class-standard-reader-method*)
1651 ((eq class *the-class-standard-writer-method*)
1653 ((eq class *the-class-standard-boundp-method*)
1655 (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1656 (setq type (cond ((null type) new-type)
1657 ((eq type new-type) type)
1659 (esetf (arg-info-metatypes arg-info) metatypes)
1660 (esetf (gf-info-simple-accessor-type arg-info) type)))
1661 (when (or (not was-valid-p) first-p)
1662 (multiple-value-bind (c-a-m-emf std-p)
1665 (compute-applicable-methods-emf gf))
1666 (esetf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1667 (esetf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1668 (unless (gf-info-c-a-m-emf-std-p arg-info)
1669 (esetf (gf-info-simple-accessor-type arg-info) t))))
1671 (let ((name (if (eq *boot-state* 'complete)
1672 (generic-function-name gf)
1673 (!early-gf-name gf))))
1674 (esetf (gf-precompute-dfun-and-emf-p arg-info)
1675 (let* ((sym (if (atom name) name (cadr name)))
1676 (pkg-list (cons *pcl-package*
1677 (package-use-list *pcl-package*))))
1678 (and sym (symbolp sym)
1679 (not (null (memq (symbol-package sym) pkg-list)))
1680 (not (find #\space (symbol-name sym))))))))
1681 (esetf (gf-info-fast-mf-p arg-info)
1682 (or (not (eq *boot-state* 'complete))
1683 (let* ((method-class (generic-function-method-class gf))
1684 (methods (compute-applicable-methods
1685 #'make-method-lambda
1686 (list gf (class-prototype method-class)
1688 (and methods (null (cdr methods))
1689 (let ((specls (method-specializers (car methods))))
1690 (and (classp (car specls))
1691 (eq 'standard-generic-function
1692 (class-name (car specls)))
1693 (classp (cadr specls))
1694 (eq 'standard-method
1695 (class-name (cadr specls)))))))))
1698 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
1700 ;;; The STATIC-SLOTS field of the funcallable instances used as early
1701 ;;; generic functions is used to store the early methods and early
1702 ;;; discriminator code for the early generic function. The static
1703 ;;; slots field of the fins contains a list whose:
1704 ;;; CAR - a list of the early methods on this early gf
1705 ;;; CADR - the early discriminator code for this method
1706 (defun ensure-generic-function-using-class (existing spec &rest keys
1707 &key (lambda-list nil
1710 (declare (ignore keys))
1711 (cond ((and existing (early-gf-p existing))
1713 ((assoc spec *!generic-function-fixups* :test #'equal)
1715 (make-early-gf spec lambda-list lambda-list-p existing)
1716 (error "The function ~S is not already defined." spec)))
1718 (error "~S should be on the list ~S."
1720 '*!generic-function-fixups*))
1722 (pushnew spec *!early-generic-functions* :test #'equal)
1723 (make-early-gf spec lambda-list lambda-list-p))))
1725 (defun make-early-gf (spec &optional lambda-list lambda-list-p function)
1726 (let ((fin (allocate-funcallable-instance *sgf-wrapper* *sgf-slots-init*)))
1727 (set-funcallable-instance-fun
1730 (if (eq spec 'print-object)
1731 #'(sb-kernel:instance-lambda (instance stream)
1732 (print-unreadable-object (instance stream :identity t)
1733 (format stream "std-instance")))
1734 #'(sb-kernel:instance-lambda (&rest args)
1735 (declare (ignore args))
1736 (error "The function of the funcallable-instance ~S~
1737 has not been set." fin)))))
1738 (setf (gdefinition spec) fin)
1739 (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
1740 (!bootstrap-set-slot 'standard-generic-function
1744 (set-fun-name fin spec)
1745 (let ((arg-info (make-arg-info)))
1746 (setf (early-gf-arg-info fin) arg-info)
1748 (proclaim (defgeneric-declaration spec lambda-list))
1749 (set-arg-info fin :lambda-list lambda-list)))
1752 (defun set-dfun (gf &optional dfun cache info)
1754 (setf (cache-owner cache) gf))
1755 (let ((new-state (if (and dfun (or cache info))
1756 (list* dfun cache info)
1758 (if (eq *boot-state* 'complete)
1759 (setf (gf-dfun-state gf) new-state)
1760 (setf (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*)
1764 (defun gf-dfun-cache (gf)
1765 (let ((state (if (eq *boot-state* 'complete)
1767 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1770 (cons (cadr state)))))
1772 (defun gf-dfun-info (gf)
1773 (let ((state (if (eq *boot-state* 'complete)
1775 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1778 (cons (cddr state)))))
1780 (defvar *sgf-name-index*
1781 (!bootstrap-slot-index 'standard-generic-function 'name))
1783 (defun !early-gf-name (gf)
1784 (clos-slots-ref (get-slots gf) *sgf-name-index*))
1786 (defun gf-lambda-list (gf)
1787 (let ((arg-info (if (eq *boot-state* 'complete)
1789 (early-gf-arg-info gf))))
1790 (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
1791 (let ((methods (if (eq *boot-state* 'complete)
1792 (generic-function-methods gf)
1793 (early-gf-methods gf))))
1796 (warn "no way to determine the lambda list for ~S" gf)
1798 (let* ((method (car (last methods)))
1799 (ll (if (consp method)
1800 (early-method-lambda-list method)
1801 (method-lambda-list method)))
1802 (k (member '&key ll)))
1804 (append (ldiff ll (cdr k)) '(&allow-other-keys))
1806 (arg-info-lambda-list arg-info))))
1808 (defmacro real-ensure-gf-internal (gf-class all-keys env)
1810 (cond ((symbolp ,gf-class)
1811 (setq ,gf-class (find-class ,gf-class t ,env)))
1812 ((classp ,gf-class))
1814 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
1815 class nor a symbol that names a class."
1817 (remf ,all-keys :generic-function-class)
1818 (remf ,all-keys :environment)
1819 (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
1820 (unless (eq combin '.shes-not-there.)
1821 (setf (getf ,all-keys :method-combination)
1822 (find-method-combination (class-prototype ,gf-class)
1825 (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
1826 (unless (eq method-class '.shes-not-there.)
1827 (setf (getf ,all-keys :method-class)
1828 (find-class method-class t ,env))))))
1830 (defun real-ensure-gf-using-class--generic-function
1834 &key environment (lambda-list nil lambda-list-p)
1835 (generic-function-class 'standard-generic-function gf-class-p)
1837 (real-ensure-gf-internal generic-function-class all-keys environment)
1838 (unless (or (null gf-class-p)
1839 (eq (class-of existing) generic-function-class))
1840 (change-class existing generic-function-class))
1842 (apply #'reinitialize-instance existing all-keys)
1844 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1846 (defun real-ensure-gf-using-class--null
1850 &key environment (lambda-list nil lambda-list-p)
1851 (generic-function-class 'standard-generic-function)
1853 (declare (ignore existing))
1854 (real-ensure-gf-internal generic-function-class all-keys environment)
1856 (setf (gdefinition fun-name)
1857 (apply #'make-instance generic-function-class
1858 :name fun-name all-keys))
1860 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1862 (defun get-generic-fun-info (gf)
1863 ;; values nreq applyp metatypes nkeys arg-info
1864 (multiple-value-bind (applyp metatypes arg-info)
1865 (let* ((arg-info (if (early-gf-p gf)
1866 (early-gf-arg-info gf)
1868 (metatypes (arg-info-metatypes arg-info)))
1869 (values (arg-info-applyp arg-info)
1872 (values (length metatypes) applyp metatypes
1873 (count-if (lambda (x) (neq x t)) metatypes)
1876 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
1877 &optional slot-name)
1878 (initialize-method-function initargs)
1881 ;; Figure out whether we got class objects or class names as the
1882 ;; specializers and set parsed and unparsed appropriately. If we
1883 ;; got class objects, then we can compute unparsed, but if we got
1884 ;; class names we don't try to compute parsed.
1886 ;; Note that the use of not symbolp in this call to every should be
1887 ;; read as 'classp' we can't use classp itself because it doesn't
1889 (if (every (lambda (s) (not (symbolp s))) specializers)
1890 (setq parsed specializers
1891 unparsed (mapcar (lambda (s)
1892 (if (eq s t) t (class-name s)))
1894 (setq unparsed specializers
1896 (list :early-method ;This is an early method dammit!
1898 (getf initargs :function)
1899 (getf initargs :fast-function)
1901 parsed ;The parsed specializers. This is used
1902 ;by early-method-specializers to cache
1903 ;the parse. Note that this only comes
1904 ;into play when there is more than one
1905 ;early method on an early gf.
1907 (list class ;A list to which real-make-a-method
1908 qualifiers ;can be applied to make a real method
1909 arglist ;corresponding to this early one.
1915 (defun real-make-a-method
1916 (class qualifiers lambda-list specializers initargs doc
1917 &optional slot-name)
1918 (setq specializers (parse-specializers specializers))
1919 (apply #'make-instance class
1920 :qualifiers qualifiers
1921 :lambda-list lambda-list
1922 :specializers specializers
1924 :slot-name slot-name
1928 (defun early-method-function (early-method)
1929 (values (cadr early-method) (caddr early-method)))
1931 (defun early-method-class (early-method)
1932 (find-class (car (fifth early-method))))
1934 (defun early-method-standard-accessor-p (early-method)
1935 (let ((class (first (fifth early-method))))
1936 (or (eq class 'standard-reader-method)
1937 (eq class 'standard-writer-method)
1938 (eq class 'standard-boundp-method))))
1940 (defun early-method-standard-accessor-slot-name (early-method)
1941 (seventh (fifth early-method)))
1943 ;;; Fetch the specializers of an early method. This is basically just
1944 ;;; a simple accessor except that when the second argument is t, this
1945 ;;; converts the specializers from symbols into class objects. The
1946 ;;; class objects are cached in the early method, this makes
1947 ;;; bootstrapping faster because the class objects only have to be
1951 ;;; The second argument should only be passed as T by
1952 ;;; early-lookup-method. This is to implement the rule that only when
1953 ;;; there is more than one early method on a generic function is the
1954 ;;; conversion from class names to class objects done. This
1955 ;;; corresponds to the fact that we are only allowed to have one
1956 ;;; method on any generic function up until the time classes exist.
1957 (defun early-method-specializers (early-method &optional objectsp)
1958 (if (and (listp early-method)
1959 (eq (car early-method) :early-method))
1960 (cond ((eq objectsp t)
1961 (or (fourth early-method)
1962 (setf (fourth early-method)
1963 (mapcar #'find-class (cadddr (fifth early-method))))))
1965 (cadddr (fifth early-method))))
1966 (error "~S is not an early-method." early-method)))
1968 (defun early-method-qualifiers (early-method)
1969 (cadr (fifth early-method)))
1971 (defun early-method-lambda-list (early-method)
1972 (caddr (fifth early-method)))
1974 (defun early-add-named-method (generic-function-name
1979 (let* ((gf (ensure-generic-function generic-function-name))
1981 (dolist (m (early-gf-methods gf))
1982 (when (and (equal (early-method-specializers m) specializers)
1983 (equal (early-method-qualifiers m) qualifiers))
1985 (new (make-a-method 'standard-method
1991 (when existing (remove-method gf existing))
1992 (add-method gf new)))
1994 ;;; This is the early version of ADD-METHOD. Later this will become a
1995 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
1996 ;;; special knowledge about ADD-METHOD.
1997 (defun add-method (generic-function method)
1998 (when (not (fsc-instance-p generic-function))
1999 (error "Early ADD-METHOD didn't get a funcallable instance."))
2000 (when (not (and (listp method) (eq (car method) :early-method)))
2001 (error "Early ADD-METHOD didn't get an early method."))
2002 (push method (early-gf-methods generic-function))
2003 (set-arg-info generic-function :new-method method)
2004 (unless (assoc (!early-gf-name generic-function)
2005 *!generic-function-fixups*
2007 (update-dfun generic-function)))
2009 ;;; This is the early version of REMOVE-METHOD. See comments on
2010 ;;; the early version of ADD-METHOD.
2011 (defun remove-method (generic-function method)
2012 (when (not (fsc-instance-p generic-function))
2013 (error "An early remove-method didn't get a funcallable instance."))
2014 (when (not (and (listp method) (eq (car method) :early-method)))
2015 (error "An early remove-method didn't get an early method."))
2016 (setf (early-gf-methods generic-function)
2017 (remove method (early-gf-methods generic-function)))
2018 (set-arg-info generic-function)
2019 (unless (assoc (!early-gf-name generic-function)
2020 *!generic-function-fixups*
2022 (update-dfun generic-function)))
2024 ;;; This is the early version of GET-METHOD. See comments on the early
2025 ;;; version of ADD-METHOD.
2026 (defun get-method (generic-function qualifiers specializers
2027 &optional (errorp t))
2028 (if (early-gf-p generic-function)
2029 (or (dolist (m (early-gf-methods generic-function))
2030 (when (and (or (equal (early-method-specializers m nil)
2032 (equal (early-method-specializers m t)
2034 (equal (early-method-qualifiers m) qualifiers))
2037 (error "can't get early method")
2039 (real-get-method generic-function qualifiers specializers errorp)))
2041 (defun !fix-early-generic-functions ()
2042 (let ((accessors nil))
2043 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2044 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2045 (dolist (early-gf-spec *!early-generic-functions*)
2046 (when (every #'early-method-standard-accessor-p
2047 (early-gf-methods (gdefinition early-gf-spec)))
2048 (push early-gf-spec accessors)))
2049 (dolist (spec (nconc accessors
2050 '(accessor-method-slot-name
2051 generic-function-methods
2056 slot-definition-location
2057 slot-definition-name
2060 class-precedence-list
2061 slot-boundp-using-class
2062 (setf slot-value-using-class)
2063 slot-value-using-class
2066 funcallable-standard-class-p
2069 (setq *!early-generic-functions*
2071 (delete spec *!early-generic-functions* :test #'equal))))
2073 (dolist (early-gf-spec *!early-generic-functions*)
2074 (/show early-gf-spec)
2075 (let* ((gf (gdefinition early-gf-spec))
2076 (methods (mapcar (lambda (early-method)
2077 (let ((args (copy-list (fifth
2080 (early-method-specializers
2082 (apply #'real-make-a-method args)))
2083 (early-gf-methods gf))))
2084 (setf (generic-function-method-class gf) *the-class-standard-method*)
2085 (setf (generic-function-method-combination gf)
2086 *standard-method-combination*)
2087 (set-methods gf methods)))
2089 (dolist (fn *!early-functions*)
2091 (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2093 (dolist (fixup *!generic-function-fixups*)
2095 (let* ((fspec (car fixup))
2096 (gf (gdefinition fspec))
2097 (methods (mapcar (lambda (method)
2098 (let* ((lambda-list (first method))
2099 (specializers (second method))
2100 (method-fn-name (third method))
2101 (fn-name (or method-fn-name fspec))
2102 (fn (fdefinition fn-name))
2106 (lambda (args next-methods)
2110 `(call ,fn-name)))))
2111 (declare (type function fn))
2112 (make-a-method 'standard-method
2119 (setf (generic-function-method-class gf) *the-class-standard-method*)
2120 (setf (generic-function-method-combination gf)
2121 *standard-method-combination*)
2122 (set-methods gf methods))))
2123 (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2125 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2126 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2127 ;;; is really implemented.
2128 (defun parse-defmethod (cdr-of-form)
2129 (declare (list cdr-of-form))
2130 (let ((name (pop cdr-of-form))
2133 (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2134 (push (pop cdr-of-form) qualifiers)
2135 (return (setq qualifiers (nreverse qualifiers)))))
2136 (setq spec-ll (pop cdr-of-form))
2137 (values name qualifiers spec-ll cdr-of-form)))
2139 (defun parse-specializers (specializers)
2140 (declare (list specializers))
2141 (flet ((parse (spec)
2142 (let ((result (specializer-from-type spec)))
2143 (if (specializerp result)
2146 (error "~S was used as a specializer,~%~
2147 but is not the name of a class."
2149 (error "~S is not a legal specializer." spec))))))
2150 (mapcar #'parse specializers)))
2152 (defun unparse-specializers (specializers-or-method)
2153 (if (listp specializers-or-method)
2154 (flet ((unparse (spec)
2155 (if (specializerp spec)
2156 (let ((type (specializer-type spec)))
2157 (if (and (consp type)
2158 (eq (car type) 'class))
2159 (let* ((class (cadr type))
2160 (class-name (class-name class)))
2161 (if (eq class (find-class class-name nil))
2165 (error "~S is not a legal specializer." spec))))
2166 (mapcar #'unparse specializers-or-method))
2167 (unparse-specializers (method-specializers specializers-or-method))))
2169 (defun parse-method-or-spec (spec &optional (errorp t))
2170 (let (gf method name temp)
2173 gf (method-generic-function method)
2174 temp (and gf (generic-function-name gf))
2177 (make-method-spec temp
2178 (method-qualifiers method)
2179 (unparse-specializers
2180 (method-specializers method))))
2181 (make-symbol (format nil "~S" method))))
2182 (multiple-value-bind (gf-spec quals specls)
2183 (parse-defmethod spec)
2184 (and (setq gf (and (or errorp (gboundp gf-spec))
2185 (gdefinition gf-spec)))
2186 (let ((nreq (compute-discriminating-function-arglist-info gf)))
2187 (setq specls (append (parse-specializers specls)
2188 (make-list (- nreq (length specls))
2192 (setq method (get-method gf quals specls errorp))
2194 (intern-fun-name (make-method-spec gf-spec
2197 (values gf method name)))
2199 (defun extract-parameters (specialized-lambda-list)
2200 (multiple-value-bind (parameters ignore1 ignore2)
2201 (parse-specialized-lambda-list specialized-lambda-list)
2202 (declare (ignore ignore1 ignore2))
2205 (defun extract-lambda-list (specialized-lambda-list)
2206 (multiple-value-bind (ignore1 lambda-list ignore2)
2207 (parse-specialized-lambda-list specialized-lambda-list)
2208 (declare (ignore ignore1 ignore2))
2211 (defun extract-specializer-names (specialized-lambda-list)
2212 (multiple-value-bind (ignore1 ignore2 specializers)
2213 (parse-specialized-lambda-list specialized-lambda-list)
2214 (declare (ignore ignore1 ignore2))
2217 (defun extract-required-parameters (specialized-lambda-list)
2218 (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2219 (parse-specialized-lambda-list specialized-lambda-list)
2220 (declare (ignore ignore1 ignore2 ignore3))
2221 required-parameters))
2223 (defun parse-specialized-lambda-list (arglist &optional post-keyword)
2224 ;;(declare (values parameters lambda-list specializers required-parameters))
2225 (let ((arg (car arglist)))
2226 (cond ((null arglist) (values nil nil nil nil))
2228 (values nil arglist nil))
2229 ((memq arg lambda-list-keywords)
2230 (unless (memq arg '(&optional &rest &key &allow-other-keys &aux))
2231 ;; Now, since we try to conform to ANSI, non-standard
2232 ;; lambda-list-keywords should be treated as errors.
2233 (error 'simple-program-error
2234 :format-control "unrecognized lambda-list keyword ~S ~
2236 :format-arguments (list arg)))
2237 ;; When we are at a lambda-list keyword, the parameters
2238 ;; don't include the lambda-list keyword; the lambda-list
2239 ;; does include the lambda-list keyword; and no
2240 ;; specializers are allowed to follow the lambda-list
2241 ;; keywords (at least for now).
2242 (multiple-value-bind (parameters lambda-list)
2243 (parse-specialized-lambda-list (cdr arglist) t)
2244 (when (eq arg '&rest)
2245 ;; check, if &rest is followed by a var ...
2246 (when (or (null lambda-list)
2247 (memq (car lambda-list) lambda-list-keywords))
2248 (error "Error in lambda-list:~%~
2249 After &REST, a DEFMETHOD lambda-list ~
2250 must be followed by at least one variable.")))
2252 (cons arg lambda-list)
2256 ;; After a lambda-list keyword there can be no specializers.
2257 (multiple-value-bind (parameters lambda-list)
2258 (parse-specialized-lambda-list (cdr arglist) t)
2259 (values (cons (if (listp arg) (car arg) arg) parameters)
2260 (cons arg lambda-list)
2264 (multiple-value-bind (parameters lambda-list specializers required)
2265 (parse-specialized-lambda-list (cdr arglist))
2266 (values (cons (if (listp arg) (car arg) arg) parameters)
2267 (cons (if (listp arg) (car arg) arg) lambda-list)
2268 (cons (if (listp arg) (cadr arg) t) specializers)
2269 (cons (if (listp arg) (car arg) arg) required)))))))
2271 (setq *boot-state* 'early)
2273 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2274 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2275 ;;; walker stuff was only used for implementing stuff like that; maybe
2276 ;;; it's not needed any more? Hunt down what it was used for and see.
2278 (defmacro with-slots (slots instance &body body)
2279 (let ((in (gensym)))
2280 `(let ((,in ,instance))
2281 (declare (ignorable ,in))
2282 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2285 (and (symbolp instance)
2286 `((declare (%variable-rebinding ,in ,instance)))))
2288 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2290 (if (symbolp slot-entry)
2294 (if (symbolp slot-entry)
2296 (cadr slot-entry))))
2298 (slot-value ,in ',slot-name))))
2302 (defmacro with-accessors (slots instance &body body)
2303 (let ((in (gensym)))
2304 `(let ((,in ,instance))
2305 (declare (ignorable ,in))
2306 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2309 (and (symbolp instance)
2310 `((declare (%variable-rebinding ,in ,instance)))))
2312 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2313 (let ((var-name (car slot-entry))
2314 (accessor-name (cadr slot-entry)))
2315 `(,var-name (,accessor-name ,in))))