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)))
175 (consp (cadr option))
176 (member (first (cadr option))
177 ;; FIXME: this list is slightly weird.
178 ;; ANSI (on the DEFGENERIC page) in one
179 ;; place allows only OPTIMIZE; in
180 ;; another place gives this list of
181 ;; disallowed declaration specifiers.
182 ;; This seems to be the only place where
183 ;; the FUNCTION declaration is
184 ;; mentioned; TYPE seems to be missing.
185 ;; Very strange. -- CSR, 2002-10-21
186 '(declaration ftype function
187 inline notinline special)))
188 (error 'simple-program-error
189 :format-control "The declaration specifier ~S ~
190 is not allowed inside DEFGENERIC."
191 :format-arguments (list (cadr option))))
192 (push (cadr option) (initarg :declarations)))
193 ((:argument-precedence-order :method-combination)
194 (if (initarg car-option)
195 (duplicate-option car-option)
196 (setf (initarg car-option)
198 ((:documentation :generic-function-class :method-class)
199 (unless (proper-list-of-length-p option 2)
200 (error "bad list length for ~S" option))
201 (if (initarg car-option)
202 (duplicate-option car-option)
203 (setf (initarg car-option) `',(cadr option))))
205 (push (cdr option) methods))
207 ;; ANSI requires that unsupported things must get a
209 (error 'simple-program-error
210 :format-control "unsupported option ~S"
211 :format-arguments (list option))))))
213 (when (initarg :declarations)
214 (setf (initarg :declarations)
215 `',(initarg :declarations))))
217 (eval-when (:compile-toplevel :load-toplevel :execute)
218 (compile-or-load-defgeneric ',fun-name))
219 (load-defgeneric ',fun-name ',lambda-list ,@initargs)
220 ,@(mapcar #'expand-method-definition methods)
223 (defun compile-or-load-defgeneric (fun-name)
224 (proclaim-as-fun-name fun-name)
225 (note-name-defined fun-name :function)
226 (unless (eq (info :function :where-from fun-name) :declared)
227 (setf (info :function :where-from fun-name) :defined)
228 (setf (info :function :type fun-name)
229 (specifier-type 'function))))
231 (defun load-defgeneric (fun-name lambda-list &rest initargs)
232 (when (fboundp fun-name)
233 (style-warn "redefining ~S in DEFGENERIC" fun-name)
234 (let ((fun (fdefinition fun-name)))
235 (when (generic-function-p fun)
236 (loop for method in (generic-function-initial-methods fun)
237 do (remove-method fun method))
238 (setf (generic-function-initial-methods fun) '()))))
239 (apply #'ensure-generic-function
241 :lambda-list lambda-list
242 :definition-source `((defgeneric ,fun-name) ,*load-pathname*)
245 ;;; As per section 3.4.2 of the ANSI spec, generic function lambda
246 ;;; lists have some special limitations, which we check here.
247 (defun check-gf-lambda-list (lambda-list)
248 (flet ((ensure (arg ok)
251 ;; (s/invalid/non-ANSI-conforming/ because the old PCL
252 ;; implementation allowed this, so people got used to
253 ;; it, and maybe this phrasing will help them to guess
254 ;; why their program which worked under PCL no longer works.)
255 "~@<non-ANSI-conforming argument ~S ~_in the generic function lambda list ~S~:>"
257 (multiple-value-bind (required optional restp rest keyp keys allowp
258 auxp aux morep more-context more-count)
259 (parse-lambda-list lambda-list)
260 (declare (ignore required)) ; since they're no different in a gf ll
261 (declare (ignore restp rest)) ; since they're no different in a gf ll
262 (declare (ignore allowp)) ; since &ALLOW-OTHER-KEYS is fine either way
263 (declare (ignore aux)) ; since we require AUXP=NIL
264 (declare (ignore more-context more-count)) ; safely ignored unless MOREP
265 ;; no defaults allowed for &OPTIONAL arguments
267 (ensure i (or (symbolp i)
268 (and (consp i) (symbolp (car i)) (null (cdr i))))))
269 ;; no defaults allowed for &KEY arguments
272 (ensure i (or (symbolp i)
274 (or (symbolp (car i))
282 (error "&AUX is not allowed in a generic function lambda list: ~S"
284 ;; Oh, *puhlease*... not specifically as per section 3.4.2 of
285 ;; the ANSI spec, but the CMU CL &MORE extension does not
287 (aver (not morep)))))
289 (defmacro defmethod (&rest args &environment env)
290 (multiple-value-bind (name qualifiers lambda-list body)
291 (parse-defmethod args)
292 (multiple-value-bind (proto-gf proto-method)
293 (prototypes-for-make-method-lambda name)
294 (expand-defmethod name
302 (defun prototypes-for-make-method-lambda (name)
303 (if (not (eq *boot-state* 'complete))
305 (let ((gf? (and (gboundp name)
306 (gdefinition name))))
308 (not (generic-function-p gf?)))
309 (values (class-prototype (find-class 'standard-generic-function))
310 (class-prototype (find-class 'standard-method)))
312 (class-prototype (or (generic-function-method-class gf?)
313 (find-class 'standard-method))))))))
315 ;;; Take a name which is either a generic function name or a list specifying
316 ;;; a SETF generic function (like: (SETF <generic-function-name>)). Return
317 ;;; the prototype instance of the method-class for that generic function.
319 ;;; If there is no generic function by that name, this returns the
320 ;;; default value, the prototype instance of the class
321 ;;; STANDARD-METHOD. This default value is also returned if the spec
322 ;;; names an ordinary function or even a macro. In effect, this leaves
323 ;;; the signalling of the appropriate error until load time.
325 ;;; Note: During bootstrapping, this function is allowed to return NIL.
326 (defun method-prototype-for-gf (name)
327 (let ((gf? (and (gboundp name)
328 (gdefinition name))))
329 (cond ((neq *boot-state* 'complete) nil)
331 (not (generic-function-p gf?))) ; Someone else MIGHT
332 ; error at load time.
333 (class-prototype (find-class 'standard-method)))
335 (class-prototype (or (generic-function-method-class gf?)
336 (find-class 'standard-method)))))))
338 (defun expand-defmethod (name
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 function
356 ;; here, since ANSI specifies that we create it if it does
357 ;; not exist. However, I chose not to, because I think it's
358 ;; more useful to support a style of programming where every
359 ;; generic function has an explicit DEFGENERIC and any typos
360 ;; in DEFMETHODs are 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)) ..)
369 ;; compiles without raising an error and runs without
370 ;; raising an error (since SIMPLE-VECTOR cases fall through
371 ;; to VECTOR) but still doesn't do what was intended. I hate
372 ;; that kind of bug (code which silently gives the wrong
373 ;; answer), so we don't do a DECLAIM here. -- WHN 20000229
374 ,(make-defmethod-form name qualifiers specializers
375 unspecialized-lambda-list
377 (class-name (class-of proto-method))
380 (getf (getf initargs :plist)
381 :pv-table-symbol)))))))
383 (defun interned-symbol-p (x)
384 (and (symbolp x) (symbol-package x)))
386 (defun make-defmethod-form (name qualifiers specializers
387 unspecialized-lambda-list method-class-name
388 initargs-form &optional pv-table-symbol)
391 (if (and (interned-symbol-p (fun-name-block-name name))
392 (every #'interned-symbol-p qualifiers)
395 (and (eq (car s) 'eql)
397 (let ((sv (eval (cadr s))))
398 (or (interned-symbol-p sv)
401 (standard-char-p sv)))))
402 (interned-symbol-p s)))
404 (consp initargs-form)
405 (eq (car initargs-form) 'list*)
406 (memq (cadr initargs-form) '(:function :fast-function))
407 (consp (setq fn (caddr initargs-form)))
408 (eq (car fn) 'function)
409 (consp (setq fn-lambda (cadr fn)))
410 (eq (car fn-lambda) 'lambda))
411 (let* ((specls (mapcar (lambda (specl)
413 `(,(car specl) ,(eval (cadr specl)))
416 (mname `(,(if (eq (cadr initargs-form) :function)
417 'method 'fast-method)
418 ,name ,@qualifiers ,specls))
419 (mname-sym (intern (let ((*print-pretty* nil)
420 ;; (We bind *PACKAGE* to
421 ;; KEYWORD here as a way to
422 ;; force symbols to be printed
423 ;; with explicit package
425 (*package* *keyword-package*))
426 (format nil "~S" mname)))))
428 (defun ,mname-sym ,(cadr fn-lambda)
430 ,(make-defmethod-form-internal
431 name qualifiers `',specls
432 unspecialized-lambda-list method-class-name
433 `(list* ,(cadr initargs-form)
435 ,@(cdddr initargs-form))
437 (make-defmethod-form-internal
439 `(list ,@(mapcar (lambda (specializer)
440 (if (consp specializer)
441 ``(,',(car specializer)
442 ,,(cadr specializer))
445 unspecialized-lambda-list
450 (defun make-defmethod-form-internal
451 (name qualifiers specializers-form unspecialized-lambda-list
452 method-class-name initargs-form &optional pv-table-symbol)
458 ',unspecialized-lambda-list
460 ;; Paper over a bug in KCL by passing the cache-symbol here in
461 ;; addition to in the list. FIXME: We should no longer need to do
462 ;; this, since the CLOS code is now SBCL-specific, and doesn't
463 ;; need to be ported to every buggy compiler in existence.
466 (defmacro make-method-function (method-lambda &environment env)
467 (make-method-function-internal method-lambda env))
469 (defun make-method-function-internal (method-lambda &optional env)
470 (multiple-value-bind (proto-gf proto-method)
471 (prototypes-for-make-method-lambda nil)
472 (multiple-value-bind (method-function-lambda initargs)
473 (make-method-lambda proto-gf proto-method method-lambda env)
474 (make-method-initargs-form proto-gf
476 method-function-lambda
480 (defun add-method-declarations (name qualifiers lambda-list body env)
481 (multiple-value-bind (parameters unspecialized-lambda-list specializers)
482 (parse-specialized-lambda-list lambda-list)
483 (declare (ignore parameters))
484 (multiple-value-bind (real-body declarations documentation)
485 (parse-body body env)
486 (values `(lambda ,unspecialized-lambda-list
487 ,@(when documentation `(,documentation))
488 ;; (Old PCL code used a somewhat different style of
489 ;; list for %METHOD-NAME values. Our names use
490 ;; ,@QUALIFIERS instead of ,QUALIFIERS so that the
491 ;; method names look more like what you see in a
494 ;; FIXME: As of sbcl-0.7.0.6, code elsewhere, at
495 ;; least the code to set up named BLOCKs around the
496 ;; bodies of methods, depends on the function's base
497 ;; name being the first element of the %METHOD-NAME
498 ;; list. It would be good to remove this dependency,
499 ;; perhaps by building the BLOCK here, or by using
500 ;; another declaration (e.g. %BLOCK-NAME), so that
501 ;; our method debug names are free to have any format,
502 ;; e.g. (:METHOD PRINT-OBJECT :AROUND (CLOWN T)).
504 ;; Further, as of sbcl-0.7.9.10, the code to
505 ;; implement NO-NEXT-METHOD is coupled to the form of
506 ;; this declaration; see the definition of
507 ;; CALL-NO-NEXT-METHOD (and the passing of
508 ;; METHOD-NAME-DECLARATION arguments around the
509 ;; various CALL-NEXT-METHOD logic).
510 (declare (%method-name (,name
513 (declare (%method-lambda-list ,@lambda-list))
516 unspecialized-lambda-list specializers))))
518 (defun real-make-method-initargs-form (proto-gf proto-method
519 method-lambda initargs env)
520 (declare (ignore proto-gf proto-method))
521 (unless (and (consp method-lambda)
522 (eq (car method-lambda) 'lambda))
523 (error "The METHOD-LAMBDA argument to MAKE-METHOD-FUNCTION, ~S, ~
524 is not a lambda form."
526 (make-method-initargs-form-internal method-lambda initargs env))
528 (unless (fboundp 'make-method-initargs-form)
529 (setf (gdefinition 'make-method-initargs-form)
530 (symbol-function 'real-make-method-initargs-form)))
532 (defun real-make-method-lambda (proto-gf proto-method method-lambda env)
533 (declare (ignore proto-gf proto-method))
534 (make-method-lambda-internal method-lambda env))
536 ;;; a helper function for creating Python-friendly type declarations
537 ;;; in DEFMETHOD forms
538 (defun parameter-specializer-declaration-in-defmethod (parameter specializer)
539 (cond ((and (consp specializer)
540 (eq (car specializer) 'eql))
541 ;; KLUDGE: ANSI, in its wisdom, says that
542 ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at
543 ;; DEFMETHOD expansion time. Thus, although one might think
545 ;; (DEFMETHOD FOO ((X PACKAGE)
548 ;; the PACKAGE and (EQL 12) forms are both parallel type
549 ;; names, they're not, as is made clear when you do
550 ;; (DEFMETHOD FOO ((X PACKAGE)
553 ;; where Y needs to be a symbol named "BAR", not some cons
554 ;; made by (CONS 'QUOTE 'BAR). I.e. when the
555 ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument
556 ;; to be of type (EQL X). It'd be easy to transform one to
557 ;; the other, but it'd be somewhat messier to do so while
558 ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd
559 ;; once. (The new code wouldn't be messy, but it'd require a
560 ;; big transformation of the old code.) So instead we punt.
564 ;; KLUDGE: For some low-level implementation
565 ;; classes, perhaps because of some problems related
566 ;; to the incomplete integration of PCL into SBCL's
567 ;; type system, some specializer classes can't be
568 ;; declared as argument types. E.g.
569 ;; (DEFMETHOD FOO ((X SLOT-OBJECT))
570 ;; (DECLARE (TYPE SLOT-OBJECT X))
573 ;; (DEFSTRUCT BAR A B)
575 ;; perhaps because of the way that STRUCTURE-OBJECT
576 ;; inherits both from SLOT-OBJECT and from
577 ;; SB-KERNEL:INSTANCE. In an effort to sweep such
578 ;; problems under the rug, we exclude these problem
579 ;; cases by blacklisting them here. -- WHN 2001-01-19
582 ((not (eq *boot-state* 'complete))
583 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
584 ;; types which don't match their specializers. (Specifically,
585 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
586 ;; second argument.) Hopefully it only does this kind of
587 ;; weirdness when bootstrapping.. -- WHN 20000610
590 ;; Otherwise, we can usually make Python very happy.
591 (let ((type (info :type :kind specializer)))
593 ((:primitive :defined :instance :forthcoming-defclass-type)
594 `(type ,specializer ,parameter))
596 (let ((class (find-class specializer nil)))
598 `(type ,(class-name class) ,parameter)
600 ;; we can get here, and still not have a failure
601 ;; case, by doing MOP programming like (PROGN
602 ;; (ENSURE-CLASS 'FOO) (DEFMETHOD BAR ((X FOO))
603 ;; ...)). Best to let the user know we haven't
604 ;; been able to extract enough information:
606 "~@<can't find type for presumed class ~S in ~S.~@:>"
608 'parameter-specializer-declaration-in-defmethod)
609 '(ignorable))))))))))
611 (defun make-method-lambda-internal (method-lambda &optional env)
612 (unless (and (consp method-lambda) (eq (car method-lambda) 'lambda))
613 (error "The METHOD-LAMBDA argument to MAKE-METHOD-LAMBDA, ~S, ~
614 is not a lambda form."
616 (multiple-value-bind (real-body declarations documentation)
617 (parse-body (cddr method-lambda) env)
618 (let* ((name-decl (get-declaration '%method-name declarations))
619 (sll-decl (get-declaration '%method-lambda-list declarations))
620 (method-name (when (consp name-decl) (car name-decl)))
621 (generic-function-name (when method-name (car method-name)))
622 (specialized-lambda-list (or sll-decl (cadr method-lambda))))
623 (multiple-value-bind (parameters lambda-list specializers)
624 (parse-specialized-lambda-list specialized-lambda-list)
625 (let* ((required-parameters
626 (mapcar (lambda (r s) (declare (ignore s)) r)
629 (slots (mapcar #'list required-parameters))
633 ;; These declarations seem to be used by PCL to pass
634 ;; information to itself; when I tried to delete 'em
635 ;; ca. 0.6.10 it didn't work. I'm not sure how
636 ;; they work, but note the (VAR-DECLARATION '%CLASS ..)
637 ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30
639 (mapcar (lambda (a s) (and (symbolp s)
644 ;; These TYPE declarations weren't in the original
645 ;; PCL code, but the Python compiler likes them a
646 ;; lot. (We're telling the compiler about our
647 ;; knowledge of specialized argument types so that
648 ;; it can avoid run-time type dispatch overhead,
649 ;; which can be a huge win for Python.)
651 ;; FIXME: Perhaps these belong in
652 ;; ADD-METHOD-DECLARATIONS instead of here?
653 ,@(mapcar #'parameter-specializer-declaration-in-defmethod
657 ;; Remove the documentation string and insert the
658 ;; appropriate class declarations. The documentation
659 ;; string is removed to make it easy for us to insert
660 ;; new declarations later, they will just go after the
661 ;; CADR of the method lambda. The class declarations
662 ;; are inserted to communicate the class of the method's
663 ;; arguments to the code walk.
664 `(lambda ,lambda-list
665 ;; The default ignorability of method parameters
666 ;; doesn't seem to be specified by ANSI. PCL had
667 ;; them basically ignorable but was a little
668 ;; inconsistent. E.g. even though the two
669 ;; method definitions
670 ;; (DEFMETHOD FOO ((X T) (Y T)) "Z")
671 ;; (DEFMETHOD FOO ((X T) Y) "Z")
672 ;; are otherwise equivalent, PCL treated Y as
673 ;; ignorable in the first definition but not in the
674 ;; second definition. We make all required
675 ;; parameters ignorable as a way of systematizing
676 ;; the old PCL behavior. -- WHN 2000-11-24
677 (declare (ignorable ,@required-parameters))
680 (block ,(fun-name-block-name generic-function-name)
682 (constant-value-p (and (null (cdr real-body))
683 (constantp (car real-body))))
684 (constant-value (and constant-value-p
685 (eval (car real-body))))
686 (plist (and constant-value-p
687 (or (typep constant-value
688 '(or number character))
689 (and (symbolp constant-value)
690 (symbol-package constant-value)))
691 (list :constant-value constant-value)))
692 (applyp (dolist (p lambda-list nil)
693 (cond ((memq p '(&optional &rest &key))
698 (walked-lambda call-next-method-p closurep next-method-p-p)
699 (walk-method-lambda method-lambda
704 (multiple-value-bind (walked-lambda-body
706 walked-documentation)
707 (parse-body (cddr walked-lambda) env)
708 (declare (ignore walked-documentation))
709 (when (or next-method-p-p call-next-method-p)
710 (setq plist (list* :needs-next-methods-p t plist)))
711 (when (some #'cdr slots)
712 (multiple-value-bind (slot-name-lists call-list)
713 (slot-name-lists-from-slots slots calls)
714 (let ((pv-table-symbol (make-symbol "pv-table")))
716 `(,@(when slot-name-lists
717 `(:slot-name-lists ,slot-name-lists))
719 `(:call-list ,call-list))
720 :pv-table-symbol ,pv-table-symbol
722 (setq walked-lambda-body
723 `((pv-binding (,required-parameters
726 ,@walked-lambda-body))))))
727 (when (and (memq '&key lambda-list)
728 (not (memq '&allow-other-keys lambda-list)))
729 (let ((aux (memq '&aux lambda-list)))
730 (setq lambda-list (nconc (ldiff lambda-list aux)
731 (list '&allow-other-keys)
733 (values `(lambda (.method-args. .next-methods.)
734 (simple-lexical-method-functions
735 (,lambda-list .method-args. .next-methods.
738 :next-method-p-p ,next-method-p-p
739 ;; we need to pass this along
740 ;; so that NO-NEXT-METHOD can
741 ;; be given a suitable METHOD
742 ;; argument; we need the
743 ;; QUALIFIERS and SPECIALIZERS
744 ;; inside the declaration to
745 ;; give to FIND-METHOD.
746 :method-name-declaration ,name-decl
749 ,@walked-declarations
750 ,@walked-lambda-body))
753 ,@(when documentation
754 `(:documentation ,documentation)))))))))))
756 (unless (fboundp 'make-method-lambda)
757 (setf (gdefinition 'make-method-lambda)
758 (symbol-function 'real-make-method-lambda)))
760 (defmacro simple-lexical-method-functions ((lambda-list
766 ,method-args ,next-methods
767 (bind-simple-lexical-method-macros (,method-args ,next-methods)
768 (bind-lexical-method-functions (,@lmf-options)
769 (bind-args (,lambda-list ,method-args)
772 (defmacro fast-lexical-method-functions ((lambda-list
778 `(bind-fast-lexical-method-macros (,args ,rest-arg ,next-method-call)
779 (bind-lexical-method-functions (,@lmf-options)
780 (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg)
783 (defmacro bind-simple-lexical-method-macros ((method-args next-methods)
785 `(macrolet ((call-next-method-bind (&body body)
786 `(let ((.next-method. (car ,',next-methods))
787 (,',next-methods (cdr ,',next-methods)))
788 .next-method. ,',next-methods
790 (call-next-method-body (method-name-declaration cnm-args)
792 (funcall (if (std-instance-p .next-method.)
793 (method-function .next-method.)
794 .next-method.) ; for early methods
795 (or ,cnm-args ,',method-args)
797 (apply #'call-no-next-method ',method-name-declaration
798 (or ,cnm-args ,',method-args))))
799 (next-method-p-body ()
800 `(not (null .next-method.))))
803 (defun call-no-next-method (method-name-declaration &rest args)
804 (destructuring-bind (name) method-name-declaration
805 (destructuring-bind (name &rest qualifiers-and-specializers) name
806 ;; KLUDGE: inefficient traversal, but hey. This should only
807 ;; happen on the slow error path anyway.
808 (let* ((qualifiers (butlast qualifiers-and-specializers))
809 (specializers (car (last qualifiers-and-specializers)))
810 (method (find-method (gdefinition name) qualifiers specializers)))
811 (apply #'no-next-method
812 (method-generic-function method)
816 (defstruct (method-call (:copier nil))
817 (function #'identity :type function)
820 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
822 (defmacro invoke-method-call1 (function args cm-args)
823 `(let ((.function. ,function)
825 (.cm-args. ,cm-args))
826 (if (and .cm-args. (null (cdr .cm-args.)))
827 (funcall .function. .args. (car .cm-args.))
828 (apply .function. .args. .cm-args.))))
830 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
831 `(invoke-method-call1 (method-call-function ,method-call)
833 `(list* ,@required-args+rest-arg)
834 `(list ,@required-args+rest-arg))
835 (method-call-call-method-args ,method-call)))
837 (defstruct (fast-method-call (:copier nil))
838 (function #'identity :type function)
843 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
845 (defmacro fmc-funcall (fn pv-cell next-method-call &rest args)
846 `(funcall ,fn ,pv-cell ,next-method-call ,@args))
848 (defmacro invoke-fast-method-call (method-call &rest required-args+rest-arg)
849 `(fmc-funcall (fast-method-call-function ,method-call)
850 (fast-method-call-pv-cell ,method-call)
851 (fast-method-call-next-method-call ,method-call)
852 ,@required-args+rest-arg))
854 (defstruct (fast-instance-boundp (:copier nil))
855 (index 0 :type fixnum))
857 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
859 (eval-when (:compile-toplevel :load-toplevel :execute)
860 (defvar *allow-emf-call-tracing-p* nil)
861 (defvar *enable-emf-call-tracing-p* #-sb-show nil #+sb-show t))
863 ;;;; effective method functions
865 (defvar *emf-call-trace-size* 200)
866 (defvar *emf-call-trace* nil)
867 (defvar *emf-call-trace-index* 0)
869 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
870 ;;; without explanation. It appears to be intended for debugging, so
871 ;;; it might be useful someday, so I haven't deleted it.
872 ;;; But it isn't documented and isn't used for anything now, so
873 ;;; I've conditionalized it out of the base system. -- WHN 19991213
875 (defun show-emf-call-trace ()
876 (when *emf-call-trace*
877 (let ((j *emf-call-trace-index*)
878 (*enable-emf-call-tracing-p* nil))
879 (format t "~&(The oldest entries are printed first)~%")
880 (dotimes-fixnum (i *emf-call-trace-size*)
881 (let ((ct (aref *emf-call-trace* j)))
882 (when ct (print ct)))
884 (when (= j *emf-call-trace-size*)
887 (defun trace-emf-call-internal (emf format args)
888 (unless *emf-call-trace*
889 (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
890 (setf (aref *emf-call-trace* *emf-call-trace-index*)
891 (list* emf format args))
892 (incf *emf-call-trace-index*)
893 (when (= *emf-call-trace-index* *emf-call-trace-size*)
894 (setq *emf-call-trace-index* 0)))
896 (defmacro trace-emf-call (emf format args)
897 (when *allow-emf-call-tracing-p*
898 `(when *enable-emf-call-tracing-p*
899 (trace-emf-call-internal ,emf ,format ,args))))
901 (defmacro invoke-effective-method-function-fast
902 (emf restp &rest required-args+rest-arg)
904 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
905 (invoke-fast-method-call ,emf ,@required-args+rest-arg)))
907 (defmacro invoke-effective-method-function (emf restp
908 &rest required-args+rest-arg)
909 (unless (constantp restp)
910 (error "The RESTP argument is not constant."))
911 ;; FIXME: The RESTP handling here is confusing and maybe slightly
912 ;; broken if RESTP evaluates to a non-self-evaluating form. E.g. if
913 ;; (INVOKE-EFFECTIVE-METHOD-FUNCTION EMF '(ERROR "gotcha") ...)
914 ;; then TRACE-EMF-CALL-CALL-INTERNAL might die on a gotcha error.
915 (setq restp (eval restp))
917 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
918 (cond ((typep ,emf 'fast-method-call)
919 (invoke-fast-method-call ,emf ,@required-args+rest-arg))
920 ;; "What," you may wonder, "do these next two clauses do?"
921 ;; In that case, you are not a PCL implementor, for they
922 ;; considered this to be self-documenting.:-| Or CSR, for
923 ;; that matter, since he can also figure it out by looking
924 ;; at it without breaking stride. For the rest of us,
925 ;; though: From what the code is doing with .SLOTS. and
926 ;; whatnot, evidently it's implementing SLOT-VALUEish and
927 ;; GET-SLOT-VALUEish things. Then we can reason backwards
928 ;; and conclude that setting EMF to a FIXNUM is an
929 ;; optimized way to represent these slot access operations.
930 ,@(when (and (null restp) (= 1 (length required-args+rest-arg)))
931 `(((typep ,emf 'fixnum)
932 (let* ((.slots. (get-slots-or-nil
933 ,(car required-args+rest-arg)))
934 (value (when .slots. (clos-slots-ref .slots. ,emf))))
935 (if (eq value +slot-unbound+)
936 (slot-unbound-internal ,(car required-args+rest-arg)
939 ,@(when (and (null restp) (= 2 (length required-args+rest-arg)))
940 `(((typep ,emf 'fixnum)
941 (let ((.new-value. ,(car required-args+rest-arg))
942 (.slots. (get-slots-or-nil
943 ,(cadr required-args+rest-arg))))
945 (setf (clos-slots-ref .slots. ,emf) .new-value.))))))
946 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
947 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
948 ;; there was no explanation and presumably the code is 10+
949 ;; years stale, I simply deleted it. -- WHN)
953 (invoke-method-call ,emf ,restp ,@required-args+rest-arg))
956 `(apply (the function ,emf) ,@required-args+rest-arg)
957 `(funcall (the function ,emf)
958 ,@required-args+rest-arg))))))))
960 (defun invoke-emf (emf args)
961 (trace-emf-call emf t args)
964 (let* ((arg-info (fast-method-call-arg-info emf))
965 (restp (cdr arg-info))
966 (nreq (car arg-info)))
968 (let* ((rest-args (nthcdr nreq args))
969 (req-args (ldiff args rest-args)))
970 (apply (fast-method-call-function emf)
971 (fast-method-call-pv-cell emf)
972 (fast-method-call-next-method-call emf)
973 (nconc req-args (list rest-args))))
976 (invoke-fast-method-call emf)
977 (error "wrong number of args")))
980 (invoke-fast-method-call emf (car args))
981 (error "wrong number of args")))
984 (invoke-fast-method-call emf (car args) (cadr args))
985 (error "wrong number of args")))
987 (apply (fast-method-call-function emf)
988 (fast-method-call-pv-cell emf)
989 (fast-method-call-next-method-call emf)
992 (apply (method-call-function emf)
994 (method-call-call-method-args emf)))
996 (cond ((null args) (error "1 or 2 args were expected."))
998 (let* ((slots (get-slots (car args)))
999 (value (clos-slots-ref slots emf)))
1000 (if (eq value +slot-unbound+)
1001 (slot-unbound-internal (car args) emf)
1004 (setf (clos-slots-ref (get-slots (cadr args)) emf)
1006 (t (error "1 or 2 args were expected."))))
1007 (fast-instance-boundp
1008 (if (or (null args) (cdr args))
1009 (error "1 arg was expected.")
1010 (let ((slots (get-slots (car args))))
1011 (not (eq (clos-slots-ref slots
1012 (fast-instance-boundp-index emf))
1017 (defmacro bind-fast-lexical-method-macros ((args rest-arg next-method-call)
1019 `(macrolet ((narrowed-emf (emf)
1020 ;; INVOKE-EFFECTIVE-METHOD-FUNCTION has code in it to
1021 ;; dispatch on the possibility that EMF might be of
1022 ;; type FIXNUM (as an optimized representation of a
1023 ;; slot accessor). But as far as I (WHN 2002-06-11)
1024 ;; can tell, it's impossible for such a representation
1025 ;; to end up as .NEXT-METHOD-CALL. By reassuring
1026 ;; INVOKE-E-M-F that when called from this context
1027 ;; it needn't worry about the FIXNUM case, we can
1028 ;; keep those cases from being compiled, which is
1029 ;; good both because it saves bytes and because it
1030 ;; avoids annoying type mismatch compiler warnings.
1032 ;; KLUDGE: In sbcl-0.7.4.29, the compiler's type
1033 ;; system isn't smart enough about NOT and intersection
1034 ;; types to benefit from a (NOT FIXNUM) declaration
1035 ;; here. -- WHN 2002-06-12
1037 ;; FIXME: Might the FUNCTION type be omittable here,
1038 ;; leaving only METHOD-CALLs? Failing that, could this
1039 ;; be documented somehow? (It'd be nice if the types
1040 ;; involved could be understood without solving the
1041 ;; halting problem.)
1042 `(the (or function method-call fast-method-call)
1044 (call-next-method-bind (&body body)
1046 (call-next-method-body (method-name-declaration cnm-args)
1047 `(if ,',next-method-call
1049 ;; This declaration suppresses a "deleting
1050 ;; unreachable code" note for the following IF when
1051 ;; REST-ARG is NIL. It is not nice for debugging
1052 ;; SBCL itself, but at least it keeps us from
1054 (declare (optimize (inhibit-warnings 3)))
1055 (if (and (null ',rest-arg)
1057 (eq (car cnm-args) 'list))
1058 `(invoke-effective-method-function
1059 (narrowed-emf ,',next-method-call)
1062 (let ((call `(invoke-effective-method-function
1063 (narrowed-emf ,',next-method-call)
1064 ,',(not (null rest-arg))
1066 ,@',(when rest-arg `(,rest-arg)))))
1068 (bind-args ((,@',args
1070 `(&rest ,rest-arg)))
1075 ;; As above, this declaration suppresses code
1077 (declare (optimize (inhibit-warnings 3)))
1078 (if (and (null ',rest-arg)
1080 (eq (car cnm-args) 'list))
1081 `(call-no-next-method ',method-name-declaration
1083 `(call-no-next-method ',method-name-declaration
1087 (next-method-p-body ()
1088 `(not (null ,',next-method-call))))
1091 (defmacro bind-lexical-method-functions
1092 ((&key call-next-method-p next-method-p-p
1093 closurep applyp method-name-declaration)
1095 (cond ((and (null call-next-method-p) (null next-method-p-p)
1100 `(call-next-method-bind
1101 (flet (,@(and call-next-method-p
1102 `((call-next-method (&rest cnm-args)
1103 (call-next-method-body
1104 ,method-name-declaration
1106 ,@(and next-method-p-p
1108 (next-method-p-body)))))
1111 (defmacro bind-args ((lambda-list args) &body body)
1112 (let ((args-tail '.args-tail.)
1115 (flet ((process-var (var)
1116 (if (memq var lambda-list-keywords)
1119 (&optional (setq state 'optional))
1120 (&key (setq state 'key))
1122 (&rest (setq state 'rest))
1123 (&aux (setq state 'aux))
1126 "encountered the non-standard lambda list keyword ~S"
1130 (required `((,var (pop ,args-tail))))
1131 (optional (cond ((not (consp var))
1132 `((,var (when ,args-tail
1133 (pop ,args-tail)))))
1135 `((,(car var) (if ,args-tail
1139 `((,(caddr var) ,args-tail)
1140 (,(car var) (if ,args-tail
1143 (rest `((,var ,args-tail)))
1144 (key (cond ((not (consp var))
1146 (get-key-arg-tail ,(keywordicate var)
1149 (multiple-value-bind (keyword variable)
1150 (if (consp (car var))
1153 (values (keywordicate (car var))
1155 `((,key (get-key-arg-tail ',keyword
1161 (multiple-value-bind (keyword variable)
1162 (if (consp (car var))
1165 (values (keywordicate (car var))
1167 `((,key (get-key-arg-tail ',keyword
1174 (let ((bindings (mapcan #'process-var lambda-list)))
1175 `(let* ((,args-tail ,args)
1177 (declare (ignorable ,args-tail))
1180 (defun get-key-arg-tail (keyword list)
1181 (loop for (key . tail) on list by #'cddr
1183 ;; FIXME: Do we want to export this symbol? Or maybe use an
1184 ;; (ERROR 'SIMPLE-PROGRAM-ERROR) form?
1185 (sb-c::%odd-key-args-error)
1186 when (eq key keyword)
1189 (defun walk-method-lambda (method-lambda required-parameters env slots calls)
1190 (let ((call-next-method-p nil) ; flag indicating that CALL-NEXT-METHOD
1191 ; should be in the method definition
1192 (closurep nil) ; flag indicating that #'CALL-NEXT-METHOD
1193 ; was seen in the body of a method
1194 (next-method-p-p nil)) ; flag indicating that NEXT-METHOD-P
1195 ; should be in the method definition
1196 (flet ((walk-function (form context env)
1197 (cond ((not (eq context :eval)) form)
1198 ;; FIXME: Jumping to a conclusion from the way it's used
1199 ;; above, perhaps CONTEXT should be called SITUATION
1200 ;; (after the term used in the ANSI specification of
1201 ;; EVAL-WHEN) and given modern ANSI keyword values
1202 ;; like :LOAD-TOPLEVEL.
1203 ((not (listp form)) form)
1204 ((eq (car form) 'call-next-method)
1205 (setq call-next-method-p t)
1207 ((eq (car form) 'next-method-p)
1208 (setq next-method-p-p t)
1210 ((and (eq (car form) 'function)
1211 (cond ((eq (cadr form) 'call-next-method)
1212 (setq call-next-method-p t)
1215 ((eq (cadr form) 'next-method-p)
1216 (setq next-method-p-p t)
1220 ((and (memq (car form)
1221 '(slot-value set-slot-value slot-boundp))
1222 (constantp (caddr form)))
1223 (let ((parameter (can-optimize-access form
1226 (let ((fun (ecase (car form)
1227 (slot-value #'optimize-slot-value)
1228 (set-slot-value #'optimize-set-slot-value)
1229 (slot-boundp #'optimize-slot-boundp))))
1230 (funcall fun slots parameter form))))
1231 ((and (eq (car form) 'apply)
1233 (eq (car (cadr form)) 'function)
1234 (generic-function-name-p (cadr (cadr form))))
1235 (optimize-generic-function-call
1236 form required-parameters env slots calls))
1237 ((generic-function-name-p (car form))
1238 (optimize-generic-function-call
1239 form required-parameters env slots calls))
1242 (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1243 (values walked-lambda
1246 next-method-p-p)))))
1248 (defun generic-function-name-p (name)
1249 (and (legal-fun-name-p name)
1251 (if (eq *boot-state* 'complete)
1252 (standard-generic-function-p (gdefinition name))
1253 (funcallable-instance-p (gdefinition name)))))
1255 (defvar *method-function-plist* (make-hash-table :test 'eq))
1258 (defvar *mf1cp* nil)
1261 (defvar *mf2cp* nil)
1263 (defun method-function-plist (method-function)
1264 (unless (eq method-function *mf1*)
1265 (rotatef *mf1* *mf2*)
1266 (rotatef *mf1p* *mf2p*)
1267 (rotatef *mf1cp* *mf2cp*))
1268 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1269 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1270 (unless (eq method-function *mf1*)
1271 (setf *mf1* method-function
1273 *mf1p* (gethash method-function *method-function-plist*)))
1276 (defun (setf method-function-plist)
1277 (val method-function)
1278 (unless (eq method-function *mf1*)
1279 (rotatef *mf1* *mf2*)
1280 (rotatef *mf1cp* *mf2cp*)
1281 (rotatef *mf1p* *mf2p*))
1282 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1283 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1284 (setf *mf1* method-function
1288 (defun method-function-get (method-function key &optional default)
1289 (getf (method-function-plist method-function) key default))
1291 (defun (setf method-function-get)
1292 (val method-function key)
1293 (setf (getf (method-function-plist method-function) key) val))
1295 (defun method-function-pv-table (method-function)
1296 (method-function-get method-function :pv-table))
1298 (defun method-function-method (method-function)
1299 (method-function-get method-function :method))
1301 (defun method-function-needs-next-methods-p (method-function)
1302 (method-function-get method-function :needs-next-methods-p t))
1304 (defmacro method-function-closure-generator (method-function)
1305 `(method-function-get ,method-function 'closure-generator))
1307 (defun load-defmethod
1308 (class name quals specls ll initargs &optional pv-table-symbol)
1309 (setq initargs (copy-tree initargs))
1310 (let ((method-spec (or (getf initargs :method-spec)
1311 (make-method-spec name quals specls))))
1312 (setf (getf initargs :method-spec) method-spec)
1313 (load-defmethod-internal class name quals specls
1314 ll initargs pv-table-symbol)))
1316 (defun load-defmethod-internal
1317 (method-class gf-spec qualifiers specializers lambda-list
1318 initargs pv-table-symbol)
1319 (when pv-table-symbol
1320 (setf (getf (getf initargs :plist) :pv-table-symbol)
1322 (when (and (eq *boot-state* 'complete)
1324 (let* ((gf (fdefinition gf-spec))
1325 (method (and (generic-function-p gf)
1328 (parse-specializers specializers)
1331 (style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1332 gf-spec qualifiers specializers))))
1333 (let ((method (apply #'add-named-method
1334 gf-spec qualifiers specializers lambda-list
1335 :definition-source `((defmethod ,gf-spec
1340 (unless (or (eq method-class 'standard-method)
1341 (eq (find-class method-class nil) (class-of method)))
1342 ;; FIXME: should be STYLE-WARNING?
1343 (format *error-output*
1344 "~&At the time the method with qualifiers ~:S and~%~
1345 specializers ~:S on the generic function ~S~%~
1346 was compiled, the method-class for that generic function was~%~
1347 ~S. But, the method class is now ~S, this~%~
1348 may mean that this method was compiled improperly.~%"
1349 qualifiers specializers gf-spec
1350 method-class (class-name (class-of method))))
1353 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1354 `(method ,gf-spec ,@qualifiers ,unparsed-specializers))
1356 (defun initialize-method-function (initargs &optional return-function-p method)
1357 (let* ((mf (getf initargs :function))
1358 (method-spec (getf initargs :method-spec))
1359 (plist (getf initargs :plist))
1360 (pv-table-symbol (getf plist :pv-table-symbol))
1362 (mff (getf initargs :fast-function)))
1363 (flet ((set-mf-property (p v)
1365 (setf (method-function-get mf p) v))
1367 (setf (method-function-get mff p) v))))
1370 (setq mf (set-fun-name mf method-spec)))
1372 (let ((name `(,(or (get (car method-spec) 'fast-sym)
1373 (setf (get (car method-spec) 'fast-sym)
1374 ;; KLUDGE: If we're going to be
1375 ;; interning private symbols in our
1376 ;; a this way, it would be cleanest
1377 ;; to use a separate package
1378 ;; %PCL-PRIVATE or something, and
1379 ;; failing that, to use a special
1380 ;; symbol prefix denoting privateness.
1382 (intern (format nil "FAST-~A"
1385 ,@(cdr method-spec))))
1386 (set-fun-name mff name)
1388 (set-mf-property :name name)))))
1390 (let ((snl (getf plist :slot-name-lists))
1391 (cl (getf plist :call-list)))
1393 (setq pv-table (intern-pv-table :slot-name-lists snl
1395 (when pv-table (set pv-table-symbol pv-table))
1396 (set-mf-property :pv-table pv-table)))
1397 (loop (when (null plist) (return nil))
1398 (set-mf-property (pop plist) (pop plist)))
1400 (set-mf-property :method method))
1401 (when return-function-p
1402 (or mf (method-function-from-fast-function mff)))))))
1404 (defun analyze-lambda-list (lambda-list)
1405 (flet (;; FIXME: Is this redundant with SB-C::MAKE-KEYWORD-FOR-ARG?
1406 (parse-key-arg (arg)
1408 (if (listp (car arg))
1410 (keywordicate (car arg)))
1411 (keywordicate arg))))
1417 (allow-other-keys-p nil)
1419 (keyword-parameters ())
1421 (dolist (x lambda-list)
1422 (if (memq x lambda-list-keywords)
1424 (&optional (setq state 'optional))
1427 (&allow-other-keys (setq allow-other-keys-p t))
1428 (&rest (setq restp t
1432 (error "encountered the non-standard lambda list keyword ~S"
1435 (required (incf nrequired))
1436 (optional (incf noptional))
1437 (key (push (parse-key-arg x) keywords)
1438 (push x keyword-parameters))
1439 (rest (incf nrest)))))
1440 (when (and restp (zerop nrest))
1441 (error "Error in lambda-list:~%~
1442 After &REST, a DEFGENERIC lambda-list ~
1443 must be followed by at least one variable."))
1444 (values nrequired noptional keysp restp allow-other-keys-p
1446 (reverse keyword-parameters)))))
1448 (defun keyword-spec-name (x)
1449 (let ((key (if (atom x) x (car x))))
1454 (defun ftype-declaration-from-lambda-list (lambda-list name)
1455 (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1456 keywords keyword-parameters)
1457 (analyze-lambda-list lambda-list)
1458 (declare (ignore keyword-parameters))
1459 (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1460 (old-ftype (if (fun-type-p old) old nil))
1461 (old-restp (and old-ftype (fun-type-rest old-ftype)))
1462 (old-keys (and old-ftype
1463 (mapcar #'key-info-name
1466 (old-keysp (and old-ftype (fun-type-keyp old-ftype)))
1467 (old-allowp (and old-ftype
1468 (fun-type-allowp old-ftype)))
1469 (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1470 `(function ,(append (make-list nrequired :initial-element t)
1471 (when (plusp noptional)
1472 (append '(&optional)
1473 (make-list noptional :initial-element t)))
1474 (when (or restp old-restp)
1476 (when (or keysp old-keysp)
1478 (mapcar (lambda (key)
1481 (when (or allow-other-keys-p old-allowp)
1482 '(&allow-other-keys)))))
1485 (defun defgeneric-declaration (spec lambda-list)
1487 (setq spec (get-setf-fun-name (cadr spec))))
1488 `(ftype ,(ftype-declaration-from-lambda-list lambda-list spec) ,spec))
1490 ;;;; early generic function support
1492 (defvar *!early-generic-functions* ())
1494 (defun ensure-generic-function (fun-name
1498 (declare (ignore environment))
1499 (let ((existing (and (gboundp fun-name)
1500 (gdefinition fun-name))))
1502 (eq *boot-state* 'complete)
1503 (null (generic-function-p existing)))
1504 (generic-clobbers-function fun-name)
1505 (apply #'ensure-generic-function-using-class
1506 existing fun-name all-keys))))
1508 (defun generic-clobbers-function (fun-name)
1509 (error 'simple-program-error
1510 :format-control "~S already names an ordinary function or a macro."
1511 :format-arguments (list fun-name)))
1513 (defvar *sgf-wrapper*
1514 (boot-make-wrapper (early-class-size 'standard-generic-function)
1515 'standard-generic-function))
1517 (defvar *sgf-slots-init*
1518 (mapcar (lambda (canonical-slot)
1519 (if (memq (getf canonical-slot :name) '(arg-info source))
1521 (let ((initfunction (getf canonical-slot :initfunction)))
1523 (funcall initfunction)
1525 (early-collect-inheritance 'standard-generic-function)))
1527 (defvar *sgf-method-class-index*
1528 (!bootstrap-slot-index 'standard-generic-function 'method-class))
1530 (defun early-gf-p (x)
1531 (and (fsc-instance-p x)
1532 (eq (clos-slots-ref (get-slots x) *sgf-method-class-index*)
1535 (defvar *sgf-methods-index*
1536 (!bootstrap-slot-index 'standard-generic-function 'methods))
1538 (defmacro early-gf-methods (gf)
1539 `(clos-slots-ref (get-slots ,gf) *sgf-methods-index*))
1541 (defvar *sgf-arg-info-index*
1542 (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1544 (defmacro early-gf-arg-info (gf)
1545 `(clos-slots-ref (get-slots ,gf) *sgf-arg-info-index*))
1547 (defvar *sgf-dfun-state-index*
1548 (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1550 (defstruct (arg-info
1552 (:constructor make-arg-info ())
1554 (arg-info-lambda-list :no-lambda-list)
1557 arg-info-number-optional
1559 arg-info-keys ;nil no &KEY or &REST allowed
1560 ;(k1 k2 ..) Each method must accept these &KEY arguments.
1561 ;T must have &KEY or &REST
1563 gf-info-simple-accessor-type ; nil, reader, writer, boundp
1564 (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1566 gf-info-static-c-a-m-emf
1567 (gf-info-c-a-m-emf-std-p t)
1570 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1572 (defun arg-info-valid-p (arg-info)
1573 (not (null (arg-info-number-optional arg-info))))
1575 (defun arg-info-applyp (arg-info)
1576 (or (plusp (arg-info-number-optional arg-info))
1577 (arg-info-key/rest-p arg-info)))
1579 (defun arg-info-number-required (arg-info)
1580 (length (arg-info-metatypes arg-info)))
1582 (defun arg-info-nkeys (arg-info)
1583 (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1585 ;;; Keep pages clean by not setting if the value is already the same.
1586 (defmacro esetf (pos val)
1587 (let ((valsym (gensym "value")))
1588 `(let ((,valsym ,val))
1589 (unless (equal ,pos ,valsym)
1590 (setf ,pos ,valsym)))))
1592 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1593 argument-precedence-order)
1594 (let* ((arg-info (if (eq *boot-state* 'complete)
1596 (early-gf-arg-info gf)))
1597 (methods (if (eq *boot-state* 'complete)
1598 (generic-function-methods gf)
1599 (early-gf-methods gf)))
1600 (was-valid-p (integerp (arg-info-number-optional arg-info)))
1601 (first-p (and new-method (null (cdr methods)))))
1602 (when (and (not lambda-list-p) methods)
1603 (setq lambda-list (gf-lambda-list gf)))
1604 (when (or lambda-list-p
1606 (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1607 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1608 (analyze-lambda-list lambda-list)
1609 (when (and methods (not first-p))
1610 (let ((gf-nreq (arg-info-number-required arg-info))
1611 (gf-nopt (arg-info-number-optional arg-info))
1612 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1613 (unless (and (= nreq gf-nreq)
1615 (eq (or keysp restp) gf-key/rest-p))
1616 (error "The lambda-list ~S is incompatible with ~
1617 existing methods of ~S."
1620 (esetf (arg-info-lambda-list arg-info) lambda-list))
1621 (when (or lambda-list-p argument-precedence-order
1622 (null (arg-info-precedence arg-info)))
1623 (esetf (arg-info-precedence arg-info)
1624 (compute-precedence lambda-list nreq
1625 argument-precedence-order)))
1626 (esetf (arg-info-metatypes arg-info) (make-list nreq))
1627 (esetf (arg-info-number-optional arg-info) nopt)
1628 (esetf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1629 (esetf (arg-info-keys arg-info)
1631 (if allow-other-keys-p t keywords)
1632 (arg-info-key/rest-p arg-info)))))
1634 (check-method-arg-info gf arg-info new-method))
1635 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1638 (defun check-method-arg-info (gf arg-info method)
1639 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1640 (analyze-lambda-list (if (consp method)
1641 (early-method-lambda-list method)
1642 (method-lambda-list method)))
1643 (flet ((lose (string &rest args)
1644 (error 'simple-program-error
1645 :format-control "~@<attempt to add the method~2I~_~S~I~_~
1646 to the generic function~2I~_~S;~I~_~
1648 :format-arguments (list method gf string args)))
1649 (comparison-description (x y)
1650 (if (> x y) "more" "fewer")))
1651 (let ((gf-nreq (arg-info-number-required arg-info))
1652 (gf-nopt (arg-info-number-optional arg-info))
1653 (gf-key/rest-p (arg-info-key/rest-p arg-info))
1654 (gf-keywords (arg-info-keys arg-info)))
1655 (unless (= nreq gf-nreq)
1657 "the method has ~A required arguments than the generic function."
1658 (comparison-description nreq gf-nreq)))
1659 (unless (= nopt gf-nopt)
1661 "the method has ~A optional arguments than the generic function."
1662 (comparison-description nopt gf-nopt)))
1663 (unless (eq (or keysp restp) gf-key/rest-p)
1665 "the method and generic function differ in whether they accept~_~
1666 &REST or &KEY arguments."))
1667 (when (consp gf-keywords)
1668 (unless (or (and restp (not keysp))
1670 (every (lambda (k) (memq k keywords)) gf-keywords))
1671 (lose "the method does not accept each of the &KEY arguments~2I~_~
1675 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1676 (let* ((existing-p (and methods (cdr methods) new-method))
1677 (nreq (length (arg-info-metatypes arg-info)))
1678 (metatypes (if existing-p
1679 (arg-info-metatypes arg-info)
1681 (type (if existing-p
1682 (gf-info-simple-accessor-type arg-info)
1684 (when (arg-info-valid-p arg-info)
1685 (dolist (method (if new-method (list new-method) methods))
1686 (let* ((specializers (if (or (eq *boot-state* 'complete)
1687 (not (consp method)))
1688 (method-specializers method)
1689 (early-method-specializers method t)))
1690 (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1692 (early-method-class method)))
1693 (new-type (when (and class
1694 (or (not (eq *boot-state* 'complete))
1695 (eq (generic-function-method-combination gf)
1696 *standard-method-combination*)))
1697 (cond ((eq class *the-class-standard-reader-method*)
1699 ((eq class *the-class-standard-writer-method*)
1701 ((eq class *the-class-standard-boundp-method*)
1703 (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1704 (setq type (cond ((null type) new-type)
1705 ((eq type new-type) type)
1707 (esetf (arg-info-metatypes arg-info) metatypes)
1708 (esetf (gf-info-simple-accessor-type arg-info) type)))
1709 (when (or (not was-valid-p) first-p)
1710 (multiple-value-bind (c-a-m-emf std-p)
1713 (compute-applicable-methods-emf gf))
1714 (esetf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1715 (esetf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1716 (unless (gf-info-c-a-m-emf-std-p arg-info)
1717 (esetf (gf-info-simple-accessor-type arg-info) t))))
1719 (let ((name (if (eq *boot-state* 'complete)
1720 (generic-function-name gf)
1721 (!early-gf-name gf))))
1722 (esetf (gf-precompute-dfun-and-emf-p arg-info)
1723 (let* ((sym (if (atom name) name (cadr name)))
1724 (pkg-list (cons *pcl-package*
1725 (package-use-list *pcl-package*))))
1726 ;; FIXME: given the presence of generalized function
1727 ;; names, this test is broken. A little
1728 ;; reverse-engineering suggests that this was intended
1729 ;; to prevent precompilation of things on some
1730 ;; PCL-internal automatically-constructed functions
1731 ;; like the old "~A~A standard class ~A reader"
1732 ;; functions. When the CADR of SB-PCL::SLOT-ACCESSOR
1733 ;; generalized functions was *, this test returned T,
1734 ;; not NIL, and an error was signalled in
1735 ;; MAKE-ACCESSOR-TABLE for (DEFUN FOO (X) (SLOT-VALUE X
1736 ;; 'ASLDKJ)). Whether the right thing to do is to fix
1737 ;; MAKE-ACCESSOR-TABLE so that it can work in the
1738 ;; presence of slot names that have no classes, or to
1739 ;; restore this test to something more obvious, I don't
1740 ;; know. -- CSR, 2003-02-14
1741 (and sym (symbolp sym)
1742 (not (null (memq (symbol-package sym) pkg-list)))
1743 (not (find #\space (symbol-name sym))))))))
1744 (esetf (gf-info-fast-mf-p arg-info)
1745 (or (not (eq *boot-state* 'complete))
1746 (let* ((method-class (generic-function-method-class gf))
1747 (methods (compute-applicable-methods
1748 #'make-method-lambda
1749 (list gf (class-prototype method-class)
1751 (and methods (null (cdr methods))
1752 (let ((specls (method-specializers (car methods))))
1753 (and (classp (car specls))
1754 (eq 'standard-generic-function
1755 (class-name (car specls)))
1756 (classp (cadr specls))
1757 (eq 'standard-method
1758 (class-name (cadr specls)))))))))
1761 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
1763 ;;; The STATIC-SLOTS field of the funcallable instances used as early
1764 ;;; generic functions is used to store the early methods and early
1765 ;;; discriminator code for the early generic function. The static
1766 ;;; slots field of the fins contains a list whose:
1767 ;;; CAR - a list of the early methods on this early gf
1768 ;;; CADR - the early discriminator code for this method
1769 (defun ensure-generic-function-using-class (existing spec &rest keys
1770 &key (lambda-list nil
1772 argument-precedence-order
1774 (declare (ignore keys))
1775 (cond ((and existing (early-gf-p existing))
1777 ((assoc spec *!generic-function-fixups* :test #'equal)
1779 (make-early-gf spec lambda-list lambda-list-p existing
1780 argument-precedence-order)
1781 (error "The function ~S is not already defined." spec)))
1783 (error "~S should be on the list ~S."
1785 '*!generic-function-fixups*))
1787 (pushnew spec *!early-generic-functions* :test #'equal)
1788 (make-early-gf spec lambda-list lambda-list-p nil
1789 argument-precedence-order))))
1791 (defun make-early-gf (spec &optional lambda-list lambda-list-p
1792 function argument-precedence-order)
1793 (let ((fin (allocate-funcallable-instance *sgf-wrapper* *sgf-slots-init*)))
1794 (set-funcallable-instance-function
1797 (if (eq spec 'print-object)
1798 #'(instance-lambda (instance stream)
1799 (print-unreadable-object (instance stream :identity t)
1800 (format stream "std-instance")))
1801 #'(instance-lambda (&rest args)
1802 (declare (ignore args))
1803 (error "The function of the funcallable-instance ~S~
1804 has not been set." fin)))))
1805 (setf (gdefinition spec) fin)
1806 (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
1807 (!bootstrap-set-slot 'standard-generic-function
1811 (set-fun-name fin spec)
1812 (let ((arg-info (make-arg-info)))
1813 (setf (early-gf-arg-info fin) arg-info)
1815 (proclaim (defgeneric-declaration spec lambda-list))
1816 (if argument-precedence-order
1818 :lambda-list lambda-list
1819 :argument-precedence-order argument-precedence-order)
1820 (set-arg-info fin :lambda-list lambda-list))))
1823 (defun set-dfun (gf &optional dfun cache info)
1825 (setf (cache-owner cache) gf))
1826 (let ((new-state (if (and dfun (or cache info))
1827 (list* dfun cache info)
1829 (if (eq *boot-state* 'complete)
1830 (setf (gf-dfun-state gf) new-state)
1831 (setf (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*)
1835 (defun gf-dfun-cache (gf)
1836 (let ((state (if (eq *boot-state* 'complete)
1838 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1841 (cons (cadr state)))))
1843 (defun gf-dfun-info (gf)
1844 (let ((state (if (eq *boot-state* 'complete)
1846 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1849 (cons (cddr state)))))
1851 (defvar *sgf-name-index*
1852 (!bootstrap-slot-index 'standard-generic-function 'name))
1854 (defun !early-gf-name (gf)
1855 (clos-slots-ref (get-slots gf) *sgf-name-index*))
1857 (defun gf-lambda-list (gf)
1858 (let ((arg-info (if (eq *boot-state* 'complete)
1860 (early-gf-arg-info gf))))
1861 (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
1862 (let ((methods (if (eq *boot-state* 'complete)
1863 (generic-function-methods gf)
1864 (early-gf-methods gf))))
1867 (warn "no way to determine the lambda list for ~S" gf)
1869 (let* ((method (car (last methods)))
1870 (ll (if (consp method)
1871 (early-method-lambda-list method)
1872 (method-lambda-list method)))
1873 (k (member '&key ll)))
1875 (append (ldiff ll (cdr k)) '(&allow-other-keys))
1877 (arg-info-lambda-list arg-info))))
1879 (defmacro real-ensure-gf-internal (gf-class all-keys env)
1881 (cond ((symbolp ,gf-class)
1882 (setq ,gf-class (find-class ,gf-class t ,env)))
1883 ((classp ,gf-class))
1885 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
1886 class nor a symbol that names a class."
1888 (remf ,all-keys :generic-function-class)
1889 (remf ,all-keys :environment)
1890 (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
1891 (unless (eq combin '.shes-not-there.)
1892 (setf (getf ,all-keys :method-combination)
1893 (find-method-combination (class-prototype ,gf-class)
1896 (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
1897 (unless (eq method-class '.shes-not-there.)
1898 (setf (getf ,all-keys :method-class)
1899 (find-class method-class t ,env))))))
1901 (defun real-ensure-gf-using-class--generic-function
1905 &key environment (lambda-list nil lambda-list-p)
1906 (generic-function-class 'standard-generic-function gf-class-p)
1908 (real-ensure-gf-internal generic-function-class all-keys environment)
1909 (unless (or (null gf-class-p)
1910 (eq (class-of existing) generic-function-class))
1911 (change-class existing generic-function-class))
1913 (apply #'reinitialize-instance existing all-keys)
1915 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1917 (defun real-ensure-gf-using-class--null
1921 &key environment (lambda-list nil lambda-list-p)
1922 (generic-function-class 'standard-generic-function)
1924 (declare (ignore existing))
1925 (real-ensure-gf-internal generic-function-class all-keys environment)
1927 (setf (gdefinition fun-name)
1928 (apply #'make-instance generic-function-class
1929 :name fun-name all-keys))
1931 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1933 (defun get-generic-fun-info (gf)
1934 ;; values nreq applyp metatypes nkeys arg-info
1935 (multiple-value-bind (applyp metatypes arg-info)
1936 (let* ((arg-info (if (early-gf-p gf)
1937 (early-gf-arg-info gf)
1939 (metatypes (arg-info-metatypes arg-info)))
1940 (values (arg-info-applyp arg-info)
1943 (values (length metatypes) applyp metatypes
1944 (count-if (lambda (x) (neq x t)) metatypes)
1947 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
1948 &optional slot-name)
1949 (initialize-method-function initargs)
1952 ;; Figure out whether we got class objects or class names as the
1953 ;; specializers and set parsed and unparsed appropriately. If we
1954 ;; got class objects, then we can compute unparsed, but if we got
1955 ;; class names we don't try to compute parsed.
1957 ;; Note that the use of not symbolp in this call to every should be
1958 ;; read as 'classp' we can't use classp itself because it doesn't
1960 (if (every (lambda (s) (not (symbolp s))) specializers)
1961 (setq parsed specializers
1962 unparsed (mapcar (lambda (s)
1963 (if (eq s t) t (class-name s)))
1965 (setq unparsed specializers
1967 (list :early-method ;This is an early method dammit!
1969 (getf initargs :function)
1970 (getf initargs :fast-function)
1972 parsed ;The parsed specializers. This is used
1973 ;by early-method-specializers to cache
1974 ;the parse. Note that this only comes
1975 ;into play when there is more than one
1976 ;early method on an early gf.
1978 (list class ;A list to which real-make-a-method
1979 qualifiers ;can be applied to make a real method
1980 arglist ;corresponding to this early one.
1986 (defun real-make-a-method
1987 (class qualifiers lambda-list specializers initargs doc
1988 &optional slot-name)
1989 (setq specializers (parse-specializers specializers))
1990 (apply #'make-instance class
1991 :qualifiers qualifiers
1992 :lambda-list lambda-list
1993 :specializers specializers
1995 :slot-name slot-name
1999 (defun early-method-function (early-method)
2000 (values (cadr early-method) (caddr early-method)))
2002 (defun early-method-class (early-method)
2003 (find-class (car (fifth early-method))))
2005 (defun early-method-standard-accessor-p (early-method)
2006 (let ((class (first (fifth early-method))))
2007 (or (eq class 'standard-reader-method)
2008 (eq class 'standard-writer-method)
2009 (eq class 'standard-boundp-method))))
2011 (defun early-method-standard-accessor-slot-name (early-method)
2012 (seventh (fifth early-method)))
2014 ;;; Fetch the specializers of an early method. This is basically just
2015 ;;; a simple accessor except that when the second argument is t, this
2016 ;;; converts the specializers from symbols into class objects. The
2017 ;;; class objects are cached in the early method, this makes
2018 ;;; bootstrapping faster because the class objects only have to be
2022 ;;; The second argument should only be passed as T by
2023 ;;; early-lookup-method. This is to implement the rule that only when
2024 ;;; there is more than one early method on a generic function is the
2025 ;;; conversion from class names to class objects done. This
2026 ;;; corresponds to the fact that we are only allowed to have one
2027 ;;; method on any generic function up until the time classes exist.
2028 (defun early-method-specializers (early-method &optional objectsp)
2029 (if (and (listp early-method)
2030 (eq (car early-method) :early-method))
2031 (cond ((eq objectsp t)
2032 (or (fourth early-method)
2033 (setf (fourth early-method)
2034 (mapcar #'find-class (cadddr (fifth early-method))))))
2036 (cadddr (fifth early-method))))
2037 (error "~S is not an early-method." early-method)))
2039 (defun early-method-qualifiers (early-method)
2040 (cadr (fifth early-method)))
2042 (defun early-method-lambda-list (early-method)
2043 (caddr (fifth early-method)))
2045 (defun early-add-named-method (generic-function-name
2050 (let* ((gf (ensure-generic-function generic-function-name))
2052 (dolist (m (early-gf-methods gf))
2053 (when (and (equal (early-method-specializers m) specializers)
2054 (equal (early-method-qualifiers m) qualifiers))
2056 (new (make-a-method 'standard-method
2062 (when existing (remove-method gf existing))
2063 (add-method gf new)))
2065 ;;; This is the early version of ADD-METHOD. Later this will become a
2066 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2067 ;;; special knowledge about ADD-METHOD.
2068 (defun add-method (generic-function method)
2069 (when (not (fsc-instance-p generic-function))
2070 (error "Early ADD-METHOD didn't get a funcallable instance."))
2071 (when (not (and (listp method) (eq (car method) :early-method)))
2072 (error "Early ADD-METHOD didn't get an early method."))
2073 (push method (early-gf-methods generic-function))
2074 (set-arg-info generic-function :new-method method)
2075 (unless (assoc (!early-gf-name generic-function)
2076 *!generic-function-fixups*
2078 (update-dfun generic-function)))
2080 ;;; This is the early version of REMOVE-METHOD. See comments on
2081 ;;; the early version of ADD-METHOD.
2082 (defun remove-method (generic-function method)
2083 (when (not (fsc-instance-p generic-function))
2084 (error "An early remove-method didn't get a funcallable instance."))
2085 (when (not (and (listp method) (eq (car method) :early-method)))
2086 (error "An early remove-method didn't get an early method."))
2087 (setf (early-gf-methods generic-function)
2088 (remove method (early-gf-methods generic-function)))
2089 (set-arg-info generic-function)
2090 (unless (assoc (!early-gf-name generic-function)
2091 *!generic-function-fixups*
2093 (update-dfun generic-function)))
2095 ;;; This is the early version of GET-METHOD. See comments on the early
2096 ;;; version of ADD-METHOD.
2097 (defun get-method (generic-function qualifiers specializers
2098 &optional (errorp t))
2099 (if (early-gf-p generic-function)
2100 (or (dolist (m (early-gf-methods generic-function))
2101 (when (and (or (equal (early-method-specializers m nil)
2103 (equal (early-method-specializers m t)
2105 (equal (early-method-qualifiers m) qualifiers))
2108 (error "can't get early method")
2110 (real-get-method generic-function qualifiers specializers errorp)))
2112 (defun !fix-early-generic-functions ()
2113 (let ((accessors nil))
2114 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2115 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2116 (dolist (early-gf-spec *!early-generic-functions*)
2117 (when (every #'early-method-standard-accessor-p
2118 (early-gf-methods (gdefinition early-gf-spec)))
2119 (push early-gf-spec accessors)))
2120 (dolist (spec (nconc accessors
2121 '(accessor-method-slot-name
2122 generic-function-methods
2127 slot-definition-location
2128 slot-definition-name
2131 class-precedence-list
2132 slot-boundp-using-class
2133 (setf slot-value-using-class)
2134 slot-value-using-class
2137 funcallable-standard-class-p
2140 (setq *!early-generic-functions*
2142 (delete spec *!early-generic-functions* :test #'equal))))
2144 (dolist (early-gf-spec *!early-generic-functions*)
2145 (/show early-gf-spec)
2146 (let* ((gf (gdefinition early-gf-spec))
2147 (methods (mapcar (lambda (early-method)
2148 (let ((args (copy-list (fifth
2151 (early-method-specializers
2153 (apply #'real-make-a-method args)))
2154 (early-gf-methods gf))))
2155 (setf (generic-function-method-class gf) *the-class-standard-method*)
2156 (setf (generic-function-method-combination gf)
2157 *standard-method-combination*)
2158 (set-methods gf methods)))
2160 (dolist (fn *!early-functions*)
2162 (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2164 (dolist (fixup *!generic-function-fixups*)
2166 (let* ((fspec (car fixup))
2167 (gf (gdefinition fspec))
2168 (methods (mapcar (lambda (method)
2169 (let* ((lambda-list (first method))
2170 (specializers (second method))
2171 (method-fn-name (third method))
2172 (fn-name (or method-fn-name fspec))
2173 (fn (fdefinition fn-name))
2177 (lambda (args next-methods)
2181 `(call ,fn-name)))))
2182 (declare (type function fn))
2183 (make-a-method 'standard-method
2190 (setf (generic-function-method-class gf) *the-class-standard-method*)
2191 (setf (generic-function-method-combination gf)
2192 *standard-method-combination*)
2193 (set-methods gf methods))))
2194 (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2196 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2197 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2198 ;;; is really implemented.
2199 (defun parse-defmethod (cdr-of-form)
2200 (declare (list cdr-of-form))
2201 (let ((name (pop cdr-of-form))
2204 (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2205 (push (pop cdr-of-form) qualifiers)
2206 (return (setq qualifiers (nreverse qualifiers)))))
2207 (setq spec-ll (pop cdr-of-form))
2208 (values name qualifiers spec-ll cdr-of-form)))
2210 (defun parse-specializers (specializers)
2211 (declare (list specializers))
2212 (flet ((parse (spec)
2213 (let ((result (specializer-from-type spec)))
2214 (if (specializerp result)
2217 (error "~S was used as a specializer,~%~
2218 but is not the name of a class."
2220 (error "~S is not a legal specializer." spec))))))
2221 (mapcar #'parse specializers)))
2223 (defun unparse-specializers (specializers-or-method)
2224 (if (listp specializers-or-method)
2225 (flet ((unparse (spec)
2226 (if (specializerp spec)
2227 (let ((type (specializer-type spec)))
2228 (if (and (consp type)
2229 (eq (car type) 'class))
2230 (let* ((class (cadr type))
2231 (class-name (class-name class)))
2232 (if (eq class (find-class class-name nil))
2236 (error "~S is not a legal specializer." spec))))
2237 (mapcar #'unparse specializers-or-method))
2238 (unparse-specializers (method-specializers specializers-or-method))))
2240 (defun parse-method-or-spec (spec &optional (errorp t))
2241 (let (gf method name temp)
2244 gf (method-generic-function method)
2245 temp (and gf (generic-function-name gf))
2248 (make-method-spec temp
2249 (method-qualifiers method)
2250 (unparse-specializers
2251 (method-specializers method))))
2252 (make-symbol (format nil "~S" method))))
2253 (multiple-value-bind (gf-spec quals specls)
2254 (parse-defmethod spec)
2255 (and (setq gf (and (or errorp (gboundp gf-spec))
2256 (gdefinition gf-spec)))
2257 (let ((nreq (compute-discriminating-function-arglist-info gf)))
2258 (setq specls (append (parse-specializers specls)
2259 (make-list (- nreq (length specls))
2263 (setq method (get-method gf quals specls errorp))
2265 (intern-fun-name (make-method-spec gf-spec
2268 (values gf method name)))
2270 (defun extract-parameters (specialized-lambda-list)
2271 (multiple-value-bind (parameters ignore1 ignore2)
2272 (parse-specialized-lambda-list specialized-lambda-list)
2273 (declare (ignore ignore1 ignore2))
2276 (defun extract-lambda-list (specialized-lambda-list)
2277 (multiple-value-bind (ignore1 lambda-list ignore2)
2278 (parse-specialized-lambda-list specialized-lambda-list)
2279 (declare (ignore ignore1 ignore2))
2282 (defun extract-specializer-names (specialized-lambda-list)
2283 (multiple-value-bind (ignore1 ignore2 specializers)
2284 (parse-specialized-lambda-list specialized-lambda-list)
2285 (declare (ignore ignore1 ignore2))
2288 (defun extract-required-parameters (specialized-lambda-list)
2289 (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2290 (parse-specialized-lambda-list specialized-lambda-list)
2291 (declare (ignore ignore1 ignore2 ignore3))
2292 required-parameters))
2294 (defun parse-specialized-lambda-list (arglist &optional post-keyword)
2295 ;;(declare (values parameters lambda-list specializers required-parameters))
2296 (let ((arg (car arglist)))
2297 (cond ((null arglist) (values nil nil nil nil))
2299 (values nil arglist nil))
2300 ((memq arg lambda-list-keywords)
2301 (unless (memq arg '(&optional &rest &key &allow-other-keys &aux))
2302 ;; Now, since we try to conform to ANSI, non-standard
2303 ;; lambda-list-keywords should be treated as errors.
2304 (error 'simple-program-error
2305 :format-control "unrecognized lambda-list keyword ~S ~
2307 :format-arguments (list arg)))
2308 ;; When we are at a lambda-list keyword, the parameters
2309 ;; don't include the lambda-list keyword; the lambda-list
2310 ;; does include the lambda-list keyword; and no
2311 ;; specializers are allowed to follow the lambda-list
2312 ;; keywords (at least for now).
2313 (multiple-value-bind (parameters lambda-list)
2314 (parse-specialized-lambda-list (cdr arglist) t)
2315 (when (eq arg '&rest)
2316 ;; check, if &rest is followed by a var ...
2317 (when (or (null lambda-list)
2318 (memq (car lambda-list) lambda-list-keywords))
2319 (error "Error in lambda-list:~%~
2320 After &REST, a DEFMETHOD lambda-list ~
2321 must be followed by at least one variable.")))
2323 (cons arg lambda-list)
2327 ;; After a lambda-list keyword there can be no specializers.
2328 (multiple-value-bind (parameters lambda-list)
2329 (parse-specialized-lambda-list (cdr arglist) t)
2330 (values (cons (if (listp arg) (car arg) arg) parameters)
2331 (cons arg lambda-list)
2335 (multiple-value-bind (parameters lambda-list specializers required)
2336 (parse-specialized-lambda-list (cdr arglist))
2337 (values (cons (if (listp arg) (car arg) arg) parameters)
2338 (cons (if (listp arg) (car arg) arg) lambda-list)
2339 (cons (if (listp arg) (cadr arg) t) specializers)
2340 (cons (if (listp arg) (car arg) arg) required)))))))
2342 (setq *boot-state* 'early)
2344 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2345 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2346 ;;; walker stuff was only used for implementing stuff like that; maybe
2347 ;;; it's not needed any more? Hunt down what it was used for and see.
2349 (defmacro with-slots (slots instance &body body)
2350 (let ((in (gensym)))
2351 `(let ((,in ,instance))
2352 (declare (ignorable ,in))
2353 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2356 (and (symbolp instance)
2357 `((declare (%variable-rebinding ,in ,instance)))))
2359 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2361 (if (symbolp slot-entry)
2365 (if (symbolp slot-entry)
2367 (cadr slot-entry))))
2369 (slot-value ,in ',slot-name))))
2373 (defmacro with-accessors (slots instance &body body)
2374 (let ((in (gensym)))
2375 `(let ((,in ,instance))
2376 (declare (ignorable ,in))
2377 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2380 (and (symbolp instance)
2381 `((declare (%variable-rebinding ,in ,instance)))))
2383 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2384 (let ((var-name (car slot-entry))
2385 (accessor-name (cadr slot-entry)))
2386 `(,var-name (,accessor-name ,in))))