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 (fdefinition ',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)))
194 (when (initarg car-option)
195 (duplicate-option car-option))
196 (unless (symbolp (cadr option))
197 (error 'simple-program-error
198 :format-control "METHOD-COMBINATION name not a ~
200 :format-arguments (list (cadr option))))
201 (setf (initarg car-option)
203 (:argument-precedence-order
204 (let* ((required (parse-lambda-list lambda-list))
205 (supplied (cdr option)))
206 (unless (= (length required) (length supplied))
207 (error 'simple-program-error
208 :format-control "argument count discrepancy in ~
209 :ARGUMENT-PRECEDENCE-ORDER clause."
210 :format-arguments nil))
211 (when (set-difference required supplied)
212 (error 'simple-program-error
213 :format-control "unequal sets for ~
214 :ARGUMENT-PRECEDENCE-ORDER clause: ~
216 :format-arguments (list required supplied)))
217 (setf (initarg car-option)
219 ((:documentation :generic-function-class :method-class)
220 (unless (proper-list-of-length-p option 2)
221 (error "bad list length for ~S" option))
222 (if (initarg car-option)
223 (duplicate-option car-option)
224 (setf (initarg car-option) `',(cadr option))))
226 (push (cdr option) methods))
228 ;; ANSI requires that unsupported things must get a
230 (error 'simple-program-error
231 :format-control "unsupported option ~S"
232 :format-arguments (list option))))))
234 (when (initarg :declarations)
235 (setf (initarg :declarations)
236 `',(initarg :declarations))))
238 (eval-when (:compile-toplevel :load-toplevel :execute)
239 (compile-or-load-defgeneric ',fun-name))
240 (load-defgeneric ',fun-name ',lambda-list ,@initargs)
241 ,@(mapcar #'expand-method-definition methods)
242 (fdefinition ',fun-name)))))
244 (defun compile-or-load-defgeneric (fun-name)
245 (proclaim-as-fun-name fun-name)
246 (note-name-defined fun-name :function)
247 (unless (eq (info :function :where-from fun-name) :declared)
248 (setf (info :function :where-from fun-name) :defined)
249 (setf (info :function :type fun-name)
250 (specifier-type 'function))))
252 (defun load-defgeneric (fun-name lambda-list &rest initargs)
253 (when (fboundp fun-name)
254 (style-warn "redefining ~S in DEFGENERIC" fun-name)
255 (let ((fun (fdefinition fun-name)))
256 (when (generic-function-p fun)
257 (loop for method in (generic-function-initial-methods fun)
258 do (remove-method fun method))
259 (setf (generic-function-initial-methods fun) '()))))
260 (apply #'ensure-generic-function
262 :lambda-list lambda-list
263 :definition-source `((defgeneric ,fun-name) ,*load-pathname*)
266 (define-condition generic-function-lambda-list-error
267 (reference-condition simple-program-error)
269 (:default-initargs :references (list '(:ansi-cl :section (3 4 2)))))
271 (defun check-gf-lambda-list (lambda-list)
272 (flet ((ensure (arg ok)
274 (error 'generic-function-lambda-list-error
276 "~@<invalid ~S ~_in the generic function lambda list ~S~:>"
277 :format-arguments (list arg lambda-list)))))
278 (multiple-value-bind (required optional restp rest keyp keys allowp
279 auxp aux morep more-context more-count)
280 (parse-lambda-list lambda-list)
281 (declare (ignore required)) ; since they're no different in a gf ll
282 (declare (ignore restp rest)) ; since they're no different in a gf ll
283 (declare (ignore allowp)) ; since &ALLOW-OTHER-KEYS is fine either way
284 (declare (ignore aux)) ; since we require AUXP=NIL
285 (declare (ignore more-context more-count)) ; safely ignored unless MOREP
286 ;; no defaults allowed for &OPTIONAL arguments
288 (ensure i (or (symbolp i)
289 (and (consp i) (symbolp (car i)) (null (cdr i))))))
290 ;; no defaults allowed for &KEY arguments
293 (ensure i (or (symbolp i)
295 (or (symbolp (car i))
303 (error "&AUX is not allowed in a generic function lambda list: ~S"
305 ;; Oh, *puhlease*... not specifically as per section 3.4.2 of
306 ;; the ANSI spec, but the CMU CL &MORE extension does not
308 (aver (not morep)))))
310 (defmacro defmethod (&rest args &environment env)
311 (multiple-value-bind (name qualifiers lambda-list body)
312 (parse-defmethod args)
313 (multiple-value-bind (proto-gf proto-method)
314 (prototypes-for-make-method-lambda name)
315 (expand-defmethod name
323 (defun prototypes-for-make-method-lambda (name)
324 (if (not (eq *boot-state* 'complete))
326 (let ((gf? (and (gboundp name)
327 (gdefinition name))))
329 (not (generic-function-p gf?)))
330 (values (class-prototype (find-class 'standard-generic-function))
331 (class-prototype (find-class 'standard-method)))
333 (class-prototype (or (generic-function-method-class gf?)
334 (find-class 'standard-method))))))))
336 ;;; Take a name which is either a generic function name or a list specifying
337 ;;; a SETF generic function (like: (SETF <generic-function-name>)). Return
338 ;;; the prototype instance of the method-class for that generic function.
340 ;;; If there is no generic function by that name, this returns the
341 ;;; default value, the prototype instance of the class
342 ;;; STANDARD-METHOD. This default value is also returned if the spec
343 ;;; names an ordinary function or even a macro. In effect, this leaves
344 ;;; the signalling of the appropriate error until load time.
346 ;;; Note: During bootstrapping, this function is allowed to return NIL.
347 (defun method-prototype-for-gf (name)
348 (let ((gf? (and (gboundp name)
349 (gdefinition name))))
350 (cond ((neq *boot-state* 'complete) nil)
352 (not (generic-function-p gf?))) ; Someone else MIGHT
353 ; error at load time.
354 (class-prototype (find-class 'standard-method)))
356 (class-prototype (or (generic-function-method-class gf?)
357 (find-class 'standard-method)))))))
359 (defun expand-defmethod (name
366 (multiple-value-bind (method-lambda unspecialized-lambda-list specializers)
367 (add-method-declarations name qualifiers lambda-list body env)
368 (multiple-value-bind (method-function-lambda initargs)
369 (make-method-lambda proto-gf proto-method method-lambda env)
370 (let ((initargs-form (make-method-initargs-form proto-gf
372 method-function-lambda
376 ;; Note: We could DECLAIM the ftype of the generic function
377 ;; here, since ANSI specifies that we create it if it does
378 ;; not exist. However, I chose not to, because I think it's
379 ;; more useful to support a style of programming where every
380 ;; generic function has an explicit DEFGENERIC and any typos
381 ;; in DEFMETHODs are warned about. Otherwise
383 ;; (DEFGENERIC FOO-BAR-BLETCH ((X T)))
384 ;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
385 ;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
386 ;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
387 ;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
388 ;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
390 ;; compiles without raising an error and runs without
391 ;; raising an error (since SIMPLE-VECTOR cases fall through
392 ;; to VECTOR) but still doesn't do what was intended. I hate
393 ;; that kind of bug (code which silently gives the wrong
394 ;; answer), so we don't do a DECLAIM here. -- WHN 20000229
395 ,(make-defmethod-form name qualifiers specializers
396 unspecialized-lambda-list
398 (class-name (class-of proto-method))
401 (getf (getf initargs :plist)
402 :pv-table-symbol)))))))
404 (defun interned-symbol-p (x)
405 (and (symbolp x) (symbol-package x)))
407 (defun make-defmethod-form (name qualifiers specializers
408 unspecialized-lambda-list method-class-name
409 initargs-form &optional pv-table-symbol)
412 (if (and (interned-symbol-p (fun-name-block-name name))
413 (every #'interned-symbol-p qualifiers)
416 (and (eq (car s) 'eql)
418 (let ((sv (eval (cadr s))))
419 (or (interned-symbol-p sv)
422 (standard-char-p sv)))))
423 (interned-symbol-p s)))
425 (consp initargs-form)
426 (eq (car initargs-form) 'list*)
427 (memq (cadr initargs-form) '(:function :fast-function))
428 (consp (setq fn (caddr initargs-form)))
429 (eq (car fn) 'function)
430 (consp (setq fn-lambda (cadr fn)))
431 (eq (car fn-lambda) 'lambda))
432 (let* ((specls (mapcar (lambda (specl)
434 `(,(car specl) ,(eval (cadr specl)))
437 (mname `(,(if (eq (cadr initargs-form) :function)
438 'slow-method 'fast-method)
439 ,name ,@qualifiers ,specls)))
441 (defun ,mname ,(cadr fn-lambda)
443 ,(make-defmethod-form-internal
444 name qualifiers `',specls
445 unspecialized-lambda-list method-class-name
446 `(list* ,(cadr initargs-form)
448 ,@(cdddr initargs-form))
450 (make-defmethod-form-internal
452 `(list ,@(mapcar (lambda (specializer)
453 (if (consp specializer)
454 ``(,',(car specializer)
455 ,,(cadr specializer))
458 unspecialized-lambda-list
463 (defun make-defmethod-form-internal
464 (name qualifiers specializers-form unspecialized-lambda-list
465 method-class-name initargs-form &optional pv-table-symbol)
471 ',unspecialized-lambda-list
473 ;; Paper over a bug in KCL by passing the cache-symbol here in
474 ;; addition to in the list. FIXME: We should no longer need to do
475 ;; this, since the CLOS code is now SBCL-specific, and doesn't
476 ;; need to be ported to every buggy compiler in existence.
479 (defmacro make-method-function (method-lambda &environment env)
480 (make-method-function-internal method-lambda env))
482 (defun make-method-function-internal (method-lambda &optional env)
483 (multiple-value-bind (proto-gf proto-method)
484 (prototypes-for-make-method-lambda nil)
485 (multiple-value-bind (method-function-lambda initargs)
486 (make-method-lambda proto-gf proto-method method-lambda env)
487 (make-method-initargs-form proto-gf
489 method-function-lambda
493 (defun add-method-declarations (name qualifiers lambda-list body env)
494 (declare (ignore env))
495 (multiple-value-bind (parameters unspecialized-lambda-list specializers)
496 (parse-specialized-lambda-list lambda-list)
497 (multiple-value-bind (real-body declarations documentation)
499 (values `(lambda ,unspecialized-lambda-list
500 ,@(when documentation `(,documentation))
501 ;; (Old PCL code used a somewhat different style of
502 ;; list for %METHOD-NAME values. Our names use
503 ;; ,@QUALIFIERS instead of ,QUALIFIERS so that the
504 ;; method names look more like what you see in a
507 ;; FIXME: As of sbcl-0.7.0.6, code elsewhere, at
508 ;; least the code to set up named BLOCKs around the
509 ;; bodies of methods, depends on the function's base
510 ;; name being the first element of the %METHOD-NAME
511 ;; list. It would be good to remove this dependency,
512 ;; perhaps by building the BLOCK here, or by using
513 ;; another declaration (e.g. %BLOCK-NAME), so that
514 ;; our method debug names are free to have any format,
515 ;; e.g. (:METHOD PRINT-OBJECT :AROUND (CLOWN T)).
517 ;; Further, as of sbcl-0.7.9.10, the code to
518 ;; implement NO-NEXT-METHOD is coupled to the form of
519 ;; this declaration; see the definition of
520 ;; CALL-NO-NEXT-METHOD (and the passing of
521 ;; METHOD-NAME-DECLARATION arguments around the
522 ;; various CALL-NEXT-METHOD logic).
523 (declare (%method-name (,name
526 (declare (%method-lambda-list ,@lambda-list))
529 unspecialized-lambda-list specializers))))
531 (defun real-make-method-initargs-form (proto-gf proto-method
532 method-lambda initargs env)
533 (declare (ignore proto-gf proto-method))
534 (unless (and (consp method-lambda)
535 (eq (car method-lambda) 'lambda))
536 (error "The METHOD-LAMBDA argument to MAKE-METHOD-FUNCTION, ~S, ~
537 is not a lambda form."
539 (make-method-initargs-form-internal method-lambda initargs env))
541 (unless (fboundp 'make-method-initargs-form)
542 (setf (gdefinition 'make-method-initargs-form)
543 (symbol-function 'real-make-method-initargs-form)))
545 (defun real-make-method-lambda (proto-gf proto-method method-lambda env)
546 (declare (ignore proto-gf proto-method))
547 (make-method-lambda-internal method-lambda env))
549 ;;; a helper function for creating Python-friendly type declarations
550 ;;; in DEFMETHOD forms
551 (defun parameter-specializer-declaration-in-defmethod (parameter specializer)
552 (cond ((and (consp specializer)
553 (eq (car specializer) 'eql))
554 ;; KLUDGE: ANSI, in its wisdom, says that
555 ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at
556 ;; DEFMETHOD expansion time. Thus, although one might think
558 ;; (DEFMETHOD FOO ((X PACKAGE)
561 ;; the PACKAGE and (EQL 12) forms are both parallel type
562 ;; names, they're not, as is made clear when you do
563 ;; (DEFMETHOD FOO ((X PACKAGE)
566 ;; where Y needs to be a symbol named "BAR", not some cons
567 ;; made by (CONS 'QUOTE 'BAR). I.e. when the
568 ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument
569 ;; to be of type (EQL X). It'd be easy to transform one to
570 ;; the other, but it'd be somewhat messier to do so while
571 ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd
572 ;; once. (The new code wouldn't be messy, but it'd require a
573 ;; big transformation of the old code.) So instead we punt.
577 ;; KLUDGE: For some low-level implementation
578 ;; classes, perhaps because of some problems related
579 ;; to the incomplete integration of PCL into SBCL's
580 ;; type system, some specializer classes can't be
581 ;; declared as argument types. E.g.
582 ;; (DEFMETHOD FOO ((X SLOT-OBJECT))
583 ;; (DECLARE (TYPE SLOT-OBJECT X))
586 ;; (DEFSTRUCT BAR A B)
588 ;; perhaps because of the way that STRUCTURE-OBJECT
589 ;; inherits both from SLOT-OBJECT and from
590 ;; SB-KERNEL:INSTANCE. In an effort to sweep such
591 ;; problems under the rug, we exclude these problem
592 ;; cases by blacklisting them here. -- WHN 2001-01-19
595 ((not (eq *boot-state* 'complete))
596 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
597 ;; types which don't match their specializers. (Specifically,
598 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
599 ;; second argument.) Hopefully it only does this kind of
600 ;; weirdness when bootstrapping.. -- WHN 20000610
602 ((var-globally-special-p parameter)
603 ;; KLUDGE: Don't declare types for global special variables
604 ;; -- our rebinding magic for SETQ cases don't work right
607 ;; FIXME: It would be better to detect the SETQ earlier and
608 ;; skip declarations for specials only when needed, not
614 ;; Otherwise, we can usually make Python very happy.
615 (let ((type (info :type :kind specializer)))
617 ((:primitive) `(type ,specializer ,parameter))
619 (let ((class (find-class specializer nil)))
622 (if (typep class '(or built-in-class structure-class))
623 `(type ,specializer ,parameter)
624 ;; don't declare CLOS classes as parameters;
625 ;; it's too expensive.
628 ;; we can get here, and still not have a failure
629 ;; case, by doing MOP programming like (PROGN
630 ;; (ENSURE-CLASS 'FOO) (DEFMETHOD BAR ((X FOO))
631 ;; ...)). Best to let the user know we haven't
632 ;; been able to extract enough information:
634 "~@<can't find type for presumed class ~S in ~S.~@:>"
636 'parameter-specializer-declaration-in-defmethod)
638 ((:forthcoming-defclass-type) '(ignorable)))))))
640 (defun make-method-lambda-internal (method-lambda &optional env)
641 (unless (and (consp method-lambda) (eq (car method-lambda) 'lambda))
642 (error "The METHOD-LAMBDA argument to MAKE-METHOD-LAMBDA, ~S, ~
643 is not a lambda form."
645 (multiple-value-bind (real-body declarations documentation)
646 (parse-body (cddr method-lambda))
647 (let* ((name-decl (get-declaration '%method-name declarations))
648 (sll-decl (get-declaration '%method-lambda-list declarations))
649 (method-name (when (consp name-decl) (car name-decl)))
650 (generic-function-name (when method-name (car method-name)))
651 (specialized-lambda-list (or sll-decl (cadr method-lambda))))
652 (multiple-value-bind (parameters lambda-list specializers)
653 (parse-specialized-lambda-list specialized-lambda-list)
654 (let* ((required-parameters
655 (mapcar (lambda (r s) (declare (ignore s)) r)
658 (slots (mapcar #'list required-parameters))
662 ;; These declarations seem to be used by PCL to pass
663 ;; information to itself; when I tried to delete 'em
664 ;; ca. 0.6.10 it didn't work. I'm not sure how
665 ;; they work, but note the (VAR-DECLARATION '%CLASS ..)
666 ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30
668 (mapcar (lambda (a s) (and (symbolp s)
673 ;; These TYPE declarations weren't in the original
674 ;; PCL code, but the Python compiler likes them a
675 ;; lot. (We're telling the compiler about our
676 ;; knowledge of specialized argument types so that
677 ;; it can avoid run-time type dispatch overhead,
678 ;; which can be a huge win for Python.)
680 ;; KLUDGE: when I tried moving these to
681 ;; ADD-METHOD-DECLARATIONS, things broke. No idea
682 ;; why. -- CSR, 2004-06-16
683 ,@(mapcar #'parameter-specializer-declaration-in-defmethod
687 ;; Remove the documentation string and insert the
688 ;; appropriate class declarations. The documentation
689 ;; string is removed to make it easy for us to insert
690 ;; new declarations later, they will just go after the
691 ;; CADR of the method lambda. The class declarations
692 ;; are inserted to communicate the class of the method's
693 ;; arguments to the code walk.
694 `(lambda ,lambda-list
695 ;; The default ignorability of method parameters
696 ;; doesn't seem to be specified by ANSI. PCL had
697 ;; them basically ignorable but was a little
698 ;; inconsistent. E.g. even though the two
699 ;; method definitions
700 ;; (DEFMETHOD FOO ((X T) (Y T)) "Z")
701 ;; (DEFMETHOD FOO ((X T) Y) "Z")
702 ;; are otherwise equivalent, PCL treated Y as
703 ;; ignorable in the first definition but not in the
704 ;; second definition. We make all required
705 ;; parameters ignorable as a way of systematizing
706 ;; the old PCL behavior. -- WHN 2000-11-24
707 (declare (ignorable ,@required-parameters))
710 (block ,(fun-name-block-name generic-function-name)
712 (constant-value-p (and (null (cdr real-body))
713 (constantp (car real-body))))
714 (constant-value (and constant-value-p
715 (eval (car real-body))))
716 (plist (and constant-value-p
717 (or (typep constant-value
718 '(or number character))
719 (and (symbolp constant-value)
720 (symbol-package constant-value)))
721 (list :constant-value constant-value)))
722 (applyp (dolist (p lambda-list nil)
723 (cond ((memq p '(&optional &rest &key))
728 (walked-lambda call-next-method-p closurep
729 next-method-p-p setq-p)
730 (walk-method-lambda method-lambda
735 (multiple-value-bind (walked-lambda-body
737 walked-documentation)
738 (parse-body (cddr walked-lambda))
739 (declare (ignore walked-documentation))
740 (when (or next-method-p-p call-next-method-p)
741 (setq plist (list* :needs-next-methods-p t plist)))
742 (when (some #'cdr slots)
743 (multiple-value-bind (slot-name-lists call-list)
744 (slot-name-lists-from-slots slots calls)
745 (let ((pv-table-symbol (make-symbol "pv-table")))
747 `(,@(when slot-name-lists
748 `(:slot-name-lists ,slot-name-lists))
750 `(:call-list ,call-list))
751 :pv-table-symbol ,pv-table-symbol
753 (setq walked-lambda-body
754 `((pv-binding (,required-parameters
757 ,@walked-lambda-body))))))
758 (when (and (memq '&key lambda-list)
759 (not (memq '&allow-other-keys lambda-list)))
760 (let ((aux (memq '&aux lambda-list)))
761 (setq lambda-list (nconc (ldiff lambda-list aux)
762 (list '&allow-other-keys)
764 (values `(lambda (.method-args. .next-methods.)
765 (simple-lexical-method-functions
766 (,lambda-list .method-args. .next-methods.
769 :next-method-p-p ,next-method-p-p
771 ;; we need to pass this along
772 ;; so that NO-NEXT-METHOD can
773 ;; be given a suitable METHOD
774 ;; argument; we need the
775 ;; QUALIFIERS and SPECIALIZERS
776 ;; inside the declaration to
777 ;; give to FIND-METHOD.
778 :method-name-declaration ,name-decl
781 ,@walked-declarations
782 ,@walked-lambda-body))
785 ,@(when documentation
786 `(:documentation ,documentation)))))))))))
788 (unless (fboundp 'make-method-lambda)
789 (setf (gdefinition 'make-method-lambda)
790 (symbol-function 'real-make-method-lambda)))
792 (defmacro simple-lexical-method-functions ((lambda-list
798 ,method-args ,next-methods
799 (bind-simple-lexical-method-macros (,method-args ,next-methods)
800 (bind-lexical-method-functions (,@lmf-options)
801 (bind-args (,lambda-list ,method-args)
804 (defmacro fast-lexical-method-functions ((lambda-list
810 `(bind-fast-lexical-method-macros (,args ,rest-arg ,next-method-call)
811 (bind-lexical-method-functions (,@lmf-options)
812 (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg)
815 (defmacro bind-simple-lexical-method-macros ((method-args next-methods)
817 `(macrolet ((call-next-method-bind (&body body)
818 `(let ((.next-method. (car ,',next-methods))
819 (,',next-methods (cdr ,',next-methods)))
820 .next-method. ,',next-methods
822 (call-next-method-body (method-name-declaration cnm-args)
824 (funcall (if (std-instance-p .next-method.)
825 (method-function .next-method.)
826 .next-method.) ; for early methods
827 (or ,cnm-args ,',method-args)
829 (apply #'call-no-next-method ',method-name-declaration
830 (or ,cnm-args ,',method-args))))
831 (next-method-p-body ()
832 `(not (null .next-method.)))
833 (with-rebound-original-args ((call-next-method-p setq-p)
835 (declare (ignore call-next-method-p setq-p))
839 (defun call-no-next-method (method-name-declaration &rest args)
840 (destructuring-bind (name) method-name-declaration
841 (destructuring-bind (name &rest qualifiers-and-specializers) name
842 ;; KLUDGE: inefficient traversal, but hey. This should only
843 ;; happen on the slow error path anyway.
844 (let* ((qualifiers (butlast qualifiers-and-specializers))
845 (specializers (car (last qualifiers-and-specializers)))
846 (method (find-method (gdefinition name) qualifiers specializers)))
847 (apply #'no-next-method
848 (method-generic-function method)
852 (defstruct (method-call (:copier nil))
853 (function #'identity :type function)
856 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
858 (defmacro invoke-method-call1 (function args cm-args)
859 `(let ((.function. ,function)
861 (.cm-args. ,cm-args))
862 (if (and .cm-args. (null (cdr .cm-args.)))
863 (funcall .function. .args. (car .cm-args.))
864 (apply .function. .args. .cm-args.))))
866 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
867 `(invoke-method-call1 (method-call-function ,method-call)
869 `(list* ,@required-args+rest-arg)
870 `(list ,@required-args+rest-arg))
871 (method-call-call-method-args ,method-call)))
873 (defstruct (fast-method-call (:copier nil))
874 (function #'identity :type function)
879 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
881 (defmacro fmc-funcall (fn pv-cell next-method-call &rest args)
882 `(funcall ,fn ,pv-cell ,next-method-call ,@args))
884 (defmacro invoke-fast-method-call (method-call &rest required-args+rest-arg)
885 `(fmc-funcall (fast-method-call-function ,method-call)
886 (fast-method-call-pv-cell ,method-call)
887 (fast-method-call-next-method-call ,method-call)
888 ,@required-args+rest-arg))
890 (defstruct (fast-instance-boundp (:copier nil))
891 (index 0 :type fixnum))
893 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
895 (eval-when (:compile-toplevel :load-toplevel :execute)
896 (defvar *allow-emf-call-tracing-p* nil)
897 (defvar *enable-emf-call-tracing-p* #-sb-show nil #+sb-show t))
899 ;;;; effective method functions
901 (defvar *emf-call-trace-size* 200)
902 (defvar *emf-call-trace* nil)
903 (defvar *emf-call-trace-index* 0)
905 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
906 ;;; without explanation. It appears to be intended for debugging, so
907 ;;; it might be useful someday, so I haven't deleted it.
908 ;;; But it isn't documented and isn't used for anything now, so
909 ;;; I've conditionalized it out of the base system. -- WHN 19991213
911 (defun show-emf-call-trace ()
912 (when *emf-call-trace*
913 (let ((j *emf-call-trace-index*)
914 (*enable-emf-call-tracing-p* nil))
915 (format t "~&(The oldest entries are printed first)~%")
916 (dotimes-fixnum (i *emf-call-trace-size*)
917 (let ((ct (aref *emf-call-trace* j)))
918 (when ct (print ct)))
920 (when (= j *emf-call-trace-size*)
923 (defun trace-emf-call-internal (emf format args)
924 (unless *emf-call-trace*
925 (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
926 (setf (aref *emf-call-trace* *emf-call-trace-index*)
927 (list* emf format args))
928 (incf *emf-call-trace-index*)
929 (when (= *emf-call-trace-index* *emf-call-trace-size*)
930 (setq *emf-call-trace-index* 0)))
932 (defmacro trace-emf-call (emf format args)
933 (when *allow-emf-call-tracing-p*
934 `(when *enable-emf-call-tracing-p*
935 (trace-emf-call-internal ,emf ,format ,args))))
937 (defmacro invoke-effective-method-function-fast
938 (emf restp &rest required-args+rest-arg)
940 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
941 (invoke-fast-method-call ,emf ,@required-args+rest-arg)))
943 (defmacro invoke-effective-method-function (emf restp
944 &rest required-args+rest-arg)
945 (unless (constantp restp)
946 (error "The RESTP argument is not constant."))
947 ;; FIXME: The RESTP handling here is confusing and maybe slightly
948 ;; broken if RESTP evaluates to a non-self-evaluating form. E.g. if
949 ;; (INVOKE-EFFECTIVE-METHOD-FUNCTION EMF '(ERROR "gotcha") ...)
950 ;; then TRACE-EMF-CALL-CALL-INTERNAL might die on a gotcha error.
951 (setq restp (eval restp))
953 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
954 (cond ((typep ,emf 'fast-method-call)
955 (invoke-fast-method-call ,emf ,@required-args+rest-arg))
956 ;; "What," you may wonder, "do these next two clauses do?"
957 ;; In that case, you are not a PCL implementor, for they
958 ;; considered this to be self-documenting.:-| Or CSR, for
959 ;; that matter, since he can also figure it out by looking
960 ;; at it without breaking stride. For the rest of us,
961 ;; though: From what the code is doing with .SLOTS. and
962 ;; whatnot, evidently it's implementing SLOT-VALUEish and
963 ;; GET-SLOT-VALUEish things. Then we can reason backwards
964 ;; and conclude that setting EMF to a FIXNUM is an
965 ;; optimized way to represent these slot access operations.
966 ,@(when (and (null restp) (= 1 (length required-args+rest-arg)))
967 `(((typep ,emf 'fixnum)
968 (let* ((.slots. (get-slots-or-nil
969 ,(car required-args+rest-arg)))
970 (value (when .slots. (clos-slots-ref .slots. ,emf))))
971 (if (eq value +slot-unbound+)
972 (slot-unbound-internal ,(car required-args+rest-arg)
975 ,@(when (and (null restp) (= 2 (length required-args+rest-arg)))
976 `(((typep ,emf 'fixnum)
977 (let ((.new-value. ,(car required-args+rest-arg))
978 (.slots. (get-slots-or-nil
979 ,(cadr required-args+rest-arg))))
981 (setf (clos-slots-ref .slots. ,emf) .new-value.))))))
982 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
983 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
984 ;; there was no explanation and presumably the code is 10+
985 ;; years stale, I simply deleted it. -- WHN)
989 (invoke-method-call ,emf ,restp ,@required-args+rest-arg))
992 `(apply (the function ,emf) ,@required-args+rest-arg)
993 `(funcall (the function ,emf)
994 ,@required-args+rest-arg))))))))
996 (defun invoke-emf (emf args)
997 (trace-emf-call emf t args)
1000 (let* ((arg-info (fast-method-call-arg-info emf))
1001 (restp (cdr arg-info))
1002 (nreq (car arg-info)))
1004 (let* ((rest-args (nthcdr nreq args))
1005 (req-args (ldiff args rest-args)))
1006 (apply (fast-method-call-function emf)
1007 (fast-method-call-pv-cell emf)
1008 (fast-method-call-next-method-call emf)
1009 (nconc req-args (list rest-args))))
1012 (invoke-fast-method-call emf)
1013 (error "wrong number of args")))
1016 (invoke-fast-method-call emf (car args))
1017 (error "wrong number of args")))
1020 (invoke-fast-method-call emf (car args) (cadr args))
1021 (error "wrong number of args")))
1023 (apply (fast-method-call-function emf)
1024 (fast-method-call-pv-cell emf)
1025 (fast-method-call-next-method-call emf)
1028 (apply (method-call-function emf)
1030 (method-call-call-method-args emf)))
1032 (cond ((null args) (error "1 or 2 args were expected."))
1034 (let* ((slots (get-slots (car args)))
1035 (value (clos-slots-ref slots emf)))
1036 (if (eq value +slot-unbound+)
1037 (slot-unbound-internal (car args) emf)
1040 (setf (clos-slots-ref (get-slots (cadr args)) emf)
1042 (t (error "1 or 2 args were expected."))))
1043 (fast-instance-boundp
1044 (if (or (null args) (cdr args))
1045 (error "1 arg was expected.")
1046 (let ((slots (get-slots (car args))))
1047 (not (eq (clos-slots-ref slots
1048 (fast-instance-boundp-index emf))
1053 (defmacro bind-fast-lexical-method-macros ((args rest-arg next-method-call)
1055 (let* ((all-params (append args (when rest-arg (list rest-arg))))
1056 (rebindings (mapcar (lambda (x) (list x x)) all-params)))
1057 `(macrolet ((narrowed-emf (emf)
1058 ;; INVOKE-EFFECTIVE-METHOD-FUNCTION has code in it to
1059 ;; dispatch on the possibility that EMF might be of
1060 ;; type FIXNUM (as an optimized representation of a
1061 ;; slot accessor). But as far as I (WHN 2002-06-11)
1062 ;; can tell, it's impossible for such a representation
1063 ;; to end up as .NEXT-METHOD-CALL. By reassuring
1064 ;; INVOKE-E-M-F that when called from this context
1065 ;; it needn't worry about the FIXNUM case, we can
1066 ;; keep those cases from being compiled, which is
1067 ;; good both because it saves bytes and because it
1068 ;; avoids annoying type mismatch compiler warnings.
1070 ;; KLUDGE: In sbcl-0.7.4.29, the compiler's type
1071 ;; system isn't smart enough about NOT and
1072 ;; intersection types to benefit from a (NOT FIXNUM)
1073 ;; declaration here. -- WHN 2002-06-12 (FIXME: maybe
1074 ;; it is now... -- CSR, 2003-06-07)
1076 ;; FIXME: Might the FUNCTION type be omittable here,
1077 ;; leaving only METHOD-CALLs? Failing that, could this
1078 ;; be documented somehow? (It'd be nice if the types
1079 ;; involved could be understood without solving the
1080 ;; halting problem.)
1081 `(the (or function method-call fast-method-call)
1083 (call-next-method-bind (&body body)
1085 (call-next-method-body (method-name-declaration cnm-args)
1086 `(if ,',next-method-call
1088 ;; This declaration suppresses a "deleting
1089 ;; unreachable code" note for the following IF
1090 ;; when REST-ARG is NIL. It is not nice for
1091 ;; debugging SBCL itself, but at least it
1092 ;; keeps us from annoying users.
1093 (declare (optimize (inhibit-warnings 3)))
1094 (if (and (null ',rest-arg)
1096 (eq (car cnm-args) 'list))
1097 `(invoke-effective-method-function
1098 (narrowed-emf ,',next-method-call)
1101 (let ((call `(invoke-effective-method-function
1102 (narrowed-emf ,',next-method-call)
1103 ,',(not (null rest-arg))
1105 ,@',(when rest-arg `(,rest-arg)))))
1107 (bind-args ((,@',args
1109 `(&rest ,rest-arg)))
1114 ;; As above, this declaration suppresses code
1116 (declare (optimize (inhibit-warnings 3)))
1117 (if (and (null ',rest-arg)
1119 (eq (car cnm-args) 'list))
1120 `(call-no-next-method ',method-name-declaration
1122 `(call-no-next-method ',method-name-declaration
1126 (next-method-p-body ()
1127 `(not (null ,',next-method-call)))
1128 (with-rebound-original-args ((cnm-p setq-p) &body body)
1129 (if (or cnm-p setq-p)
1131 (declare (ignorable ,@',all-params))
1136 (defmacro bind-lexical-method-functions
1137 ((&key call-next-method-p next-method-p-p setq-p
1138 closurep applyp method-name-declaration)
1140 (cond ((and (null call-next-method-p) (null next-method-p-p)
1141 (null closurep) (null applyp) (null setq-p))
1144 `(call-next-method-bind
1145 (flet (,@(and call-next-method-p
1146 `((call-next-method (&rest cnm-args)
1147 (call-next-method-body
1148 ,method-name-declaration
1150 ,@(and next-method-p-p
1152 (next-method-p-body)))))
1153 (with-rebound-original-args (,call-next-method-p ,setq-p)
1156 (defmacro bind-args ((lambda-list args) &body body)
1157 (let ((args-tail '.args-tail.)
1160 (flet ((process-var (var)
1161 (if (memq var lambda-list-keywords)
1164 (&optional (setq state 'optional))
1165 (&key (setq state 'key))
1167 (&rest (setq state 'rest))
1168 (&aux (setq state 'aux))
1171 "encountered the non-standard lambda list keyword ~S"
1175 (required `((,var (pop ,args-tail))))
1176 (optional (cond ((not (consp var))
1177 `((,var (when ,args-tail
1178 (pop ,args-tail)))))
1180 `((,(car var) (if ,args-tail
1184 `((,(caddr var) ,args-tail)
1185 (,(car var) (if ,args-tail
1188 (rest `((,var ,args-tail)))
1189 (key (cond ((not (consp var))
1191 (get-key-arg-tail ,(keywordicate var)
1194 (multiple-value-bind (keyword variable)
1195 (if (consp (car var))
1198 (values (keywordicate (car var))
1200 `((,key (get-key-arg-tail ',keyword
1206 (multiple-value-bind (keyword variable)
1207 (if (consp (car var))
1210 (values (keywordicate (car var))
1212 `((,key (get-key-arg-tail ',keyword
1219 (let ((bindings (mapcan #'process-var lambda-list)))
1220 `(let* ((,args-tail ,args)
1223 ,@(when (eq state 'optional)
1224 `((unless (null ,args-tail)
1225 (error 'simple-program-error
1226 :format-control "surplus arguments: ~S"
1227 :format-arguments (list ,args-tail)))))))
1228 (declare (ignorable ,args-tail .dummy0.))
1231 (defun get-key-arg-tail (keyword list)
1232 (loop for (key . tail) on list by #'cddr
1234 ;; FIXME: Do we want to export this symbol? Or maybe use an
1235 ;; (ERROR 'SIMPLE-PROGRAM-ERROR) form?
1236 (sb-c::%odd-key-args-error)
1237 when (eq key keyword)
1240 (defun walk-method-lambda (method-lambda required-parameters env slots calls)
1241 (let ((call-next-method-p nil) ; flag indicating that CALL-NEXT-METHOD
1242 ; should be in the method definition
1243 (closurep nil) ; flag indicating that #'CALL-NEXT-METHOD
1244 ; was seen in the body of a method
1245 (next-method-p-p nil) ; flag indicating that NEXT-METHOD-P
1246 ; should be in the method definition
1248 (flet ((walk-function (form context env)
1249 (cond ((not (eq context :eval)) form)
1250 ;; FIXME: Jumping to a conclusion from the way it's used
1251 ;; above, perhaps CONTEXT should be called SITUATION
1252 ;; (after the term used in the ANSI specification of
1253 ;; EVAL-WHEN) and given modern ANSI keyword values
1254 ;; like :LOAD-TOPLEVEL.
1255 ((not (listp form)) form)
1256 ((eq (car form) 'call-next-method)
1257 (setq call-next-method-p t)
1259 ((eq (car form) 'next-method-p)
1260 (setq next-method-p-p t)
1262 ((eq (car form) 'setq)
1263 ;; FIXME: this is possibly a little strong as
1264 ;; conditions go. Ideally we would want to detect
1265 ;; which, if any, of the method parameters are
1266 ;; being set, and communicate that information to
1267 ;; e.g. SPLIT-DECLARATIONS. However, the brute
1268 ;; force method doesn't really cost much; a little
1269 ;; loss of discrimination over IGNORED variables
1270 ;; should be all. -- CSR, 2004-07-01
1273 ((and (eq (car form) 'function)
1274 (cond ((eq (cadr form) 'call-next-method)
1275 (setq call-next-method-p t)
1278 ((eq (cadr form) 'next-method-p)
1279 (setq next-method-p-p t)
1283 ((and (memq (car form)
1284 '(slot-value set-slot-value slot-boundp))
1285 (constantp (caddr form)))
1286 (let ((parameter (can-optimize-access form
1289 (let ((fun (ecase (car form)
1290 (slot-value #'optimize-slot-value)
1291 (set-slot-value #'optimize-set-slot-value)
1292 (slot-boundp #'optimize-slot-boundp))))
1293 (funcall fun slots parameter form))))
1294 ((and (eq (car form) 'apply)
1296 (eq (car (cadr form)) 'function)
1297 (generic-function-name-p (cadr (cadr form))))
1298 (optimize-generic-function-call
1299 form required-parameters env slots calls))
1300 ((generic-function-name-p (car form))
1301 (optimize-generic-function-call
1302 form required-parameters env slots calls))
1305 (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1306 (values walked-lambda
1312 (defun generic-function-name-p (name)
1313 (and (legal-fun-name-p name)
1315 (if (eq *boot-state* 'complete)
1316 (standard-generic-function-p (gdefinition name))
1317 (funcallable-instance-p (gdefinition name)))))
1319 (defvar *method-function-plist* (make-hash-table :test 'eq))
1322 (defvar *mf1cp* nil)
1325 (defvar *mf2cp* nil)
1327 (defun method-function-plist (method-function)
1328 (unless (eq method-function *mf1*)
1329 (rotatef *mf1* *mf2*)
1330 (rotatef *mf1p* *mf2p*)
1331 (rotatef *mf1cp* *mf2cp*))
1332 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1333 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1334 (unless (eq method-function *mf1*)
1335 (setf *mf1* method-function
1337 *mf1p* (gethash method-function *method-function-plist*)))
1340 (defun (setf method-function-plist)
1341 (val method-function)
1342 (unless (eq method-function *mf1*)
1343 (rotatef *mf1* *mf2*)
1344 (rotatef *mf1cp* *mf2cp*)
1345 (rotatef *mf1p* *mf2p*))
1346 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1347 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1348 (setf *mf1* method-function
1352 (defun method-function-get (method-function key &optional default)
1353 (getf (method-function-plist method-function) key default))
1355 (defun (setf method-function-get)
1356 (val method-function key)
1357 (setf (getf (method-function-plist method-function) key) val))
1359 (defun method-function-pv-table (method-function)
1360 (method-function-get method-function :pv-table))
1362 (defun method-function-method (method-function)
1363 (method-function-get method-function :method))
1365 (defun method-function-needs-next-methods-p (method-function)
1366 (method-function-get method-function :needs-next-methods-p t))
1368 (defmacro method-function-closure-generator (method-function)
1369 `(method-function-get ,method-function 'closure-generator))
1371 (defun load-defmethod
1372 (class name quals specls ll initargs &optional pv-table-symbol)
1373 (setq initargs (copy-tree initargs))
1374 (let ((method-spec (or (getf initargs :method-spec)
1375 (make-method-spec name quals specls))))
1376 (setf (getf initargs :method-spec) method-spec)
1377 (load-defmethod-internal class name quals specls
1378 ll initargs pv-table-symbol)))
1380 (defun load-defmethod-internal
1381 (method-class gf-spec qualifiers specializers lambda-list
1382 initargs pv-table-symbol)
1383 (when pv-table-symbol
1384 (setf (getf (getf initargs :plist) :pv-table-symbol)
1386 (when (and (eq *boot-state* 'complete)
1388 (let* ((gf (fdefinition gf-spec))
1389 (method (and (generic-function-p gf)
1390 (generic-function-methods gf)
1393 (parse-specializers specializers)
1396 (style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1397 gf-spec qualifiers specializers))))
1398 (let ((method (apply #'add-named-method
1399 gf-spec qualifiers specializers lambda-list
1400 :definition-source `((defmethod ,gf-spec
1405 (unless (or (eq method-class 'standard-method)
1406 (eq (find-class method-class nil) (class-of method)))
1407 ;; FIXME: should be STYLE-WARNING?
1408 (format *error-output*
1409 "~&At the time the method with qualifiers ~:S and~%~
1410 specializers ~:S on the generic function ~S~%~
1411 was compiled, the method-class for that generic function was~%~
1412 ~S. But, the method class is now ~S, this~%~
1413 may mean that this method was compiled improperly.~%"
1414 qualifiers specializers gf-spec
1415 method-class (class-name (class-of method))))
1418 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1419 `(slow-method ,gf-spec ,@qualifiers ,unparsed-specializers))
1421 (defun initialize-method-function (initargs &optional return-function-p method)
1422 (let* ((mf (getf initargs :function))
1423 (method-spec (getf initargs :method-spec))
1424 (plist (getf initargs :plist))
1425 (pv-table-symbol (getf plist :pv-table-symbol))
1427 (mff (getf initargs :fast-function)))
1428 (flet ((set-mf-property (p v)
1430 (setf (method-function-get mf p) v))
1432 (setf (method-function-get mff p) v))))
1435 (setq mf (set-fun-name mf method-spec)))
1437 (let ((name `(fast-method ,@(cdr method-spec))))
1438 (set-fun-name mff name)
1440 (set-mf-property :name name)))))
1442 (let ((snl (getf plist :slot-name-lists))
1443 (cl (getf plist :call-list)))
1445 (setq pv-table (intern-pv-table :slot-name-lists snl
1447 (when pv-table (set pv-table-symbol pv-table))
1448 (set-mf-property :pv-table pv-table)))
1449 (loop (when (null plist) (return nil))
1450 (set-mf-property (pop plist) (pop plist)))
1452 (set-mf-property :method method))
1453 (when return-function-p
1454 (or mf (method-function-from-fast-function mff)))))))
1456 (defun analyze-lambda-list (lambda-list)
1457 (flet (;; FIXME: Is this redundant with SB-C::MAKE-KEYWORD-FOR-ARG?
1458 (parse-key-arg (arg)
1460 (if (listp (car arg))
1462 (keywordicate (car arg)))
1463 (keywordicate arg))))
1469 (allow-other-keys-p nil)
1471 (keyword-parameters ())
1473 (dolist (x lambda-list)
1474 (if (memq x lambda-list-keywords)
1476 (&optional (setq state 'optional))
1479 (&allow-other-keys (setq allow-other-keys-p t))
1480 (&rest (setq restp t
1484 (error "encountered the non-standard lambda list keyword ~S"
1487 (required (incf nrequired))
1488 (optional (incf noptional))
1489 (key (push (parse-key-arg x) keywords)
1490 (push x keyword-parameters))
1491 (rest (incf nrest)))))
1492 (when (and restp (zerop nrest))
1493 (error "Error in lambda-list:~%~
1494 After &REST, a DEFGENERIC lambda-list ~
1495 must be followed by at least one variable."))
1496 (values nrequired noptional keysp restp allow-other-keys-p
1498 (reverse keyword-parameters)))))
1500 (defun keyword-spec-name (x)
1501 (let ((key (if (atom x) x (car x))))
1506 (defun ftype-declaration-from-lambda-list (lambda-list name)
1507 (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1508 keywords keyword-parameters)
1509 (analyze-lambda-list lambda-list)
1510 (declare (ignore keyword-parameters))
1511 (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1512 (old-ftype (if (fun-type-p old) old nil))
1513 (old-restp (and old-ftype (fun-type-rest old-ftype)))
1514 (old-keys (and old-ftype
1515 (mapcar #'key-info-name
1518 (old-keysp (and old-ftype (fun-type-keyp old-ftype)))
1519 (old-allowp (and old-ftype
1520 (fun-type-allowp old-ftype)))
1521 (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1522 `(function ,(append (make-list nrequired :initial-element t)
1523 (when (plusp noptional)
1524 (append '(&optional)
1525 (make-list noptional :initial-element t)))
1526 (when (or restp old-restp)
1528 (when (or keysp old-keysp)
1530 (mapcar (lambda (key)
1533 (when (or allow-other-keys-p old-allowp)
1534 '(&allow-other-keys)))))
1537 (defun defgeneric-declaration (spec lambda-list)
1538 `(ftype ,(ftype-declaration-from-lambda-list lambda-list spec) ,spec))
1540 ;;;; early generic function support
1542 (defvar *!early-generic-functions* ())
1544 (defun ensure-generic-function (fun-name
1548 (declare (ignore environment))
1549 (let ((existing (and (gboundp fun-name)
1550 (gdefinition fun-name))))
1552 (eq *boot-state* 'complete)
1553 (null (generic-function-p existing)))
1554 (generic-clobbers-function fun-name)
1555 (apply #'ensure-generic-function-using-class
1556 existing fun-name all-keys))))
1558 (defun generic-clobbers-function (fun-name)
1559 (error 'simple-program-error
1560 :format-control "~S already names an ordinary function or a macro."
1561 :format-arguments (list fun-name)))
1563 (defvar *sgf-wrapper*
1564 (boot-make-wrapper (early-class-size 'standard-generic-function)
1565 'standard-generic-function))
1567 (defvar *sgf-slots-init*
1568 (mapcar (lambda (canonical-slot)
1569 (if (memq (getf canonical-slot :name) '(arg-info source))
1571 (let ((initfunction (getf canonical-slot :initfunction)))
1573 (funcall initfunction)
1575 (early-collect-inheritance 'standard-generic-function)))
1577 (defvar *sgf-method-class-index*
1578 (!bootstrap-slot-index 'standard-generic-function 'method-class))
1580 (defun early-gf-p (x)
1581 (and (fsc-instance-p x)
1582 (eq (clos-slots-ref (get-slots x) *sgf-method-class-index*)
1585 (defvar *sgf-methods-index*
1586 (!bootstrap-slot-index 'standard-generic-function 'methods))
1588 (defmacro early-gf-methods (gf)
1589 `(clos-slots-ref (get-slots ,gf) *sgf-methods-index*))
1591 (defvar *sgf-arg-info-index*
1592 (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1594 (defmacro early-gf-arg-info (gf)
1595 `(clos-slots-ref (get-slots ,gf) *sgf-arg-info-index*))
1597 (defvar *sgf-dfun-state-index*
1598 (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1600 (defstruct (arg-info
1602 (:constructor make-arg-info ())
1604 (arg-info-lambda-list :no-lambda-list)
1607 arg-info-number-optional
1609 arg-info-keys ;nil no &KEY or &REST allowed
1610 ;(k1 k2 ..) Each method must accept these &KEY arguments.
1611 ;T must have &KEY or &REST
1613 gf-info-simple-accessor-type ; nil, reader, writer, boundp
1614 (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1616 gf-info-static-c-a-m-emf
1617 (gf-info-c-a-m-emf-std-p t)
1620 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1622 (defun arg-info-valid-p (arg-info)
1623 (not (null (arg-info-number-optional arg-info))))
1625 (defun arg-info-applyp (arg-info)
1626 (or (plusp (arg-info-number-optional arg-info))
1627 (arg-info-key/rest-p arg-info)))
1629 (defun arg-info-number-required (arg-info)
1630 (length (arg-info-metatypes arg-info)))
1632 (defun arg-info-nkeys (arg-info)
1633 (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1635 ;;; Keep pages clean by not setting if the value is already the same.
1636 (defmacro esetf (pos val)
1637 (with-unique-names (valsym)
1638 `(let ((,valsym ,val))
1639 (unless (equal ,pos ,valsym)
1640 (setf ,pos ,valsym)))))
1642 (defun create-gf-lambda-list (lambda-list)
1643 ;;; Create a gf lambda list from a method lambda list
1644 (loop for x in lambda-list
1645 collect (if (consp x) (list (car x)) x)
1646 if (eq x '&key) do (loop-finish)))
1648 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1649 argument-precedence-order)
1650 (let* ((arg-info (if (eq *boot-state* 'complete)
1652 (early-gf-arg-info gf)))
1653 (methods (if (eq *boot-state* 'complete)
1654 (generic-function-methods gf)
1655 (early-gf-methods gf)))
1656 (was-valid-p (integerp (arg-info-number-optional arg-info)))
1657 (first-p (and new-method (null (cdr methods)))))
1658 (when (and (not lambda-list-p) methods)
1659 (setq lambda-list (gf-lambda-list gf)))
1660 (when (or lambda-list-p
1662 (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1663 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1664 (analyze-lambda-list lambda-list)
1665 (when (and methods (not first-p))
1666 (let ((gf-nreq (arg-info-number-required arg-info))
1667 (gf-nopt (arg-info-number-optional arg-info))
1668 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1669 (unless (and (= nreq gf-nreq)
1671 (eq (or keysp restp) gf-key/rest-p))
1672 (error "The lambda-list ~S is incompatible with ~
1673 existing methods of ~S."
1675 (esetf (arg-info-lambda-list arg-info)
1678 (create-gf-lambda-list lambda-list)))
1679 (when (or lambda-list-p argument-precedence-order
1680 (null (arg-info-precedence arg-info)))
1681 (esetf (arg-info-precedence arg-info)
1682 (compute-precedence lambda-list nreq
1683 argument-precedence-order)))
1684 (esetf (arg-info-metatypes arg-info) (make-list nreq))
1685 (esetf (arg-info-number-optional arg-info) nopt)
1686 (esetf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1687 (esetf (arg-info-keys arg-info)
1689 (if allow-other-keys-p t keywords)
1690 (arg-info-key/rest-p arg-info)))))
1692 (check-method-arg-info gf arg-info new-method))
1693 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1696 (defun check-method-arg-info (gf arg-info method)
1697 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1698 (analyze-lambda-list (if (consp method)
1699 (early-method-lambda-list method)
1700 (method-lambda-list method)))
1701 (flet ((lose (string &rest args)
1702 (error 'simple-program-error
1703 :format-control "~@<attempt to add the method~2I~_~S~I~_~
1704 to the generic function~2I~_~S;~I~_~
1706 :format-arguments (list method gf string args)))
1707 (comparison-description (x y)
1708 (if (> x y) "more" "fewer")))
1709 (let ((gf-nreq (arg-info-number-required arg-info))
1710 (gf-nopt (arg-info-number-optional arg-info))
1711 (gf-key/rest-p (arg-info-key/rest-p arg-info))
1712 (gf-keywords (arg-info-keys arg-info)))
1713 (unless (= nreq gf-nreq)
1715 "the method has ~A required arguments than the generic function."
1716 (comparison-description nreq gf-nreq)))
1717 (unless (= nopt gf-nopt)
1719 "the method has ~A optional arguments than the generic function."
1720 (comparison-description nopt gf-nopt)))
1721 (unless (eq (or keysp restp) gf-key/rest-p)
1723 "the method and generic function differ in whether they accept~_~
1724 &REST or &KEY arguments."))
1725 (when (consp gf-keywords)
1726 (unless (or (and restp (not keysp))
1728 (every (lambda (k) (memq k keywords)) gf-keywords))
1729 (lose "the method does not accept each of the &KEY arguments~2I~_~
1733 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1734 (let* ((existing-p (and methods (cdr methods) new-method))
1735 (nreq (length (arg-info-metatypes arg-info)))
1736 (metatypes (if existing-p
1737 (arg-info-metatypes arg-info)
1739 (type (if existing-p
1740 (gf-info-simple-accessor-type arg-info)
1742 (when (arg-info-valid-p arg-info)
1743 (dolist (method (if new-method (list new-method) methods))
1744 (let* ((specializers (if (or (eq *boot-state* 'complete)
1745 (not (consp method)))
1746 (method-specializers method)
1747 (early-method-specializers method t)))
1748 (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1750 (early-method-class method)))
1751 (new-type (when (and class
1752 (or (not (eq *boot-state* 'complete))
1753 (eq (generic-function-method-combination gf)
1754 *standard-method-combination*)))
1755 (cond ((eq class *the-class-standard-reader-method*)
1757 ((eq class *the-class-standard-writer-method*)
1759 ((eq class *the-class-standard-boundp-method*)
1761 (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1762 (setq type (cond ((null type) new-type)
1763 ((eq type new-type) type)
1765 (esetf (arg-info-metatypes arg-info) metatypes)
1766 (esetf (gf-info-simple-accessor-type arg-info) type)))
1767 (when (or (not was-valid-p) first-p)
1768 (multiple-value-bind (c-a-m-emf std-p)
1771 (compute-applicable-methods-emf gf))
1772 (esetf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1773 (esetf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1774 (unless (gf-info-c-a-m-emf-std-p arg-info)
1775 (esetf (gf-info-simple-accessor-type arg-info) t))))
1777 (let ((name (if (eq *boot-state* 'complete)
1778 (generic-function-name gf)
1779 (!early-gf-name gf))))
1780 (esetf (gf-precompute-dfun-and-emf-p arg-info)
1784 *internal-pcl-generalized-fun-name-symbols*))
1786 (t (let* ((symbol (fun-name-block-name name))
1787 (package (symbol-package symbol)))
1788 (and (or (eq package *pcl-package*)
1789 (memq package (package-use-list *pcl-package*)))
1790 ;; FIXME: this test will eventually be
1791 ;; superseded by the *internal-pcl...* test,
1792 ;; above. While we are in a process of
1793 ;; transition, however, it should probably
1795 (not (find #\Space (symbol-name symbol))))))))))
1796 (esetf (gf-info-fast-mf-p arg-info)
1797 (or (not (eq *boot-state* 'complete))
1798 (let* ((method-class (generic-function-method-class gf))
1799 (methods (compute-applicable-methods
1800 #'make-method-lambda
1801 (list gf (class-prototype method-class)
1803 (and methods (null (cdr methods))
1804 (let ((specls (method-specializers (car methods))))
1805 (and (classp (car specls))
1806 (eq 'standard-generic-function
1807 (class-name (car specls)))
1808 (classp (cadr specls))
1809 (eq 'standard-method
1810 (class-name (cadr specls)))))))))
1813 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
1815 ;;; The STATIC-SLOTS field of the funcallable instances used as early
1816 ;;; generic functions is used to store the early methods and early
1817 ;;; discriminator code for the early generic function. The static
1818 ;;; slots field of the fins contains a list whose:
1819 ;;; CAR - a list of the early methods on this early gf
1820 ;;; CADR - the early discriminator code for this method
1821 (defun ensure-generic-function-using-class (existing spec &rest keys
1822 &key (lambda-list nil
1824 argument-precedence-order
1826 (declare (ignore keys))
1827 (cond ((and existing (early-gf-p existing))
1829 (set-arg-info existing :lambda-list lambda-list))
1831 ((assoc spec *!generic-function-fixups* :test #'equal)
1833 (make-early-gf spec lambda-list lambda-list-p existing
1834 argument-precedence-order)
1835 (error "The function ~S is not already defined." spec)))
1837 (error "~S should be on the list ~S."
1839 '*!generic-function-fixups*))
1841 (pushnew spec *!early-generic-functions* :test #'equal)
1842 (make-early-gf spec lambda-list lambda-list-p nil
1843 argument-precedence-order))))
1845 (defun make-early-gf (spec &optional lambda-list lambda-list-p
1846 function argument-precedence-order)
1847 (let ((fin (allocate-funcallable-instance *sgf-wrapper* *sgf-slots-init*)))
1848 (set-funcallable-instance-function
1851 (if (eq spec 'print-object)
1852 #'(instance-lambda (instance stream)
1853 (print-unreadable-object (instance stream :identity t)
1854 (format stream "std-instance")))
1855 #'(instance-lambda (&rest args)
1856 (declare (ignore args))
1857 (error "The function of the funcallable-instance ~S~
1858 has not been set." fin)))))
1859 (setf (gdefinition spec) fin)
1860 (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
1861 (!bootstrap-set-slot 'standard-generic-function
1865 (set-fun-name fin spec)
1866 (let ((arg-info (make-arg-info)))
1867 (setf (early-gf-arg-info fin) arg-info)
1869 (proclaim (defgeneric-declaration spec lambda-list))
1870 (if argument-precedence-order
1872 :lambda-list lambda-list
1873 :argument-precedence-order argument-precedence-order)
1874 (set-arg-info fin :lambda-list lambda-list))))
1877 (defun set-dfun (gf &optional dfun cache info)
1879 (setf (cache-owner cache) gf))
1880 (let ((new-state (if (and dfun (or cache info))
1881 (list* dfun cache info)
1883 (if (eq *boot-state* 'complete)
1884 (setf (gf-dfun-state gf) new-state)
1885 (setf (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*)
1889 (defun gf-dfun-cache (gf)
1890 (let ((state (if (eq *boot-state* 'complete)
1892 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1895 (cons (cadr state)))))
1897 (defun gf-dfun-info (gf)
1898 (let ((state (if (eq *boot-state* 'complete)
1900 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1903 (cons (cddr state)))))
1905 (defvar *sgf-name-index*
1906 (!bootstrap-slot-index 'standard-generic-function 'name))
1908 (defun !early-gf-name (gf)
1909 (clos-slots-ref (get-slots gf) *sgf-name-index*))
1911 (defun gf-lambda-list (gf)
1912 (let ((arg-info (if (eq *boot-state* 'complete)
1914 (early-gf-arg-info gf))))
1915 (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
1916 (let ((methods (if (eq *boot-state* 'complete)
1917 (generic-function-methods gf)
1918 (early-gf-methods gf))))
1921 (warn "no way to determine the lambda list for ~S" gf)
1923 (let* ((method (car (last methods)))
1924 (ll (if (consp method)
1925 (early-method-lambda-list method)
1926 (method-lambda-list method))))
1927 (create-gf-lambda-list ll))))
1928 (arg-info-lambda-list arg-info))))
1930 (defmacro real-ensure-gf-internal (gf-class all-keys env)
1932 (cond ((symbolp ,gf-class)
1933 (setq ,gf-class (find-class ,gf-class t ,env)))
1934 ((classp ,gf-class))
1936 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
1937 class nor a symbol that names a class."
1939 (remf ,all-keys :generic-function-class)
1940 (remf ,all-keys :environment)
1941 (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
1942 (unless (eq combin '.shes-not-there.)
1943 (setf (getf ,all-keys :method-combination)
1944 (find-method-combination (class-prototype ,gf-class)
1947 (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
1948 (unless (eq method-class '.shes-not-there.)
1949 (setf (getf ,all-keys :method-class)
1950 (find-class method-class t ,env))))))
1952 (defun real-ensure-gf-using-class--generic-function
1956 &key environment (lambda-list nil lambda-list-p)
1957 (generic-function-class 'standard-generic-function gf-class-p)
1959 (real-ensure-gf-internal generic-function-class all-keys environment)
1960 (unless (or (null gf-class-p)
1961 (eq (class-of existing) generic-function-class))
1962 (change-class existing generic-function-class))
1964 (apply #'reinitialize-instance existing all-keys)
1966 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1968 (defun real-ensure-gf-using-class--null
1972 &key environment (lambda-list nil lambda-list-p)
1973 (generic-function-class 'standard-generic-function)
1975 (declare (ignore existing))
1976 (real-ensure-gf-internal generic-function-class all-keys environment)
1978 (setf (gdefinition fun-name)
1979 (apply #'make-instance generic-function-class
1980 :name fun-name all-keys))
1982 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1984 (defun get-generic-fun-info (gf)
1985 ;; values nreq applyp metatypes nkeys arg-info
1986 (multiple-value-bind (applyp metatypes arg-info)
1987 (let* ((arg-info (if (early-gf-p gf)
1988 (early-gf-arg-info gf)
1990 (metatypes (arg-info-metatypes arg-info)))
1991 (values (arg-info-applyp arg-info)
1994 (values (length metatypes) applyp metatypes
1995 (count-if (lambda (x) (neq x t)) metatypes)
1998 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
1999 &optional slot-name)
2000 (initialize-method-function initargs)
2003 ;; Figure out whether we got class objects or class names as the
2004 ;; specializers and set parsed and unparsed appropriately. If we
2005 ;; got class objects, then we can compute unparsed, but if we got
2006 ;; class names we don't try to compute parsed.
2008 ;; Note that the use of not symbolp in this call to every should be
2009 ;; read as 'classp' we can't use classp itself because it doesn't
2011 (if (every (lambda (s) (not (symbolp s))) specializers)
2012 (setq parsed specializers
2013 unparsed (mapcar (lambda (s)
2014 (if (eq s t) t (class-name s)))
2016 (setq unparsed specializers
2018 (list :early-method ;This is an early method dammit!
2020 (getf initargs :function)
2021 (getf initargs :fast-function)
2023 parsed ;The parsed specializers. This is used
2024 ;by early-method-specializers to cache
2025 ;the parse. Note that this only comes
2026 ;into play when there is more than one
2027 ;early method on an early gf.
2029 (list class ;A list to which real-make-a-method
2030 qualifiers ;can be applied to make a real method
2031 arglist ;corresponding to this early one.
2037 (defun real-make-a-method
2038 (class qualifiers lambda-list specializers initargs doc
2039 &optional slot-name)
2040 (setq specializers (parse-specializers specializers))
2041 (apply #'make-instance class
2042 :qualifiers qualifiers
2043 :lambda-list lambda-list
2044 :specializers specializers
2046 :slot-name slot-name
2050 (defun early-method-function (early-method)
2051 (values (cadr early-method) (caddr early-method)))
2053 (defun early-method-class (early-method)
2054 (find-class (car (fifth early-method))))
2056 (defun early-method-standard-accessor-p (early-method)
2057 (let ((class (first (fifth early-method))))
2058 (or (eq class 'standard-reader-method)
2059 (eq class 'standard-writer-method)
2060 (eq class 'standard-boundp-method))))
2062 (defun early-method-standard-accessor-slot-name (early-method)
2063 (seventh (fifth early-method)))
2065 ;;; Fetch the specializers of an early method. This is basically just
2066 ;;; a simple accessor except that when the second argument is t, this
2067 ;;; converts the specializers from symbols into class objects. The
2068 ;;; class objects are cached in the early method, this makes
2069 ;;; bootstrapping faster because the class objects only have to be
2073 ;;; The second argument should only be passed as T by
2074 ;;; early-lookup-method. This is to implement the rule that only when
2075 ;;; there is more than one early method on a generic function is the
2076 ;;; conversion from class names to class objects done. This
2077 ;;; corresponds to the fact that we are only allowed to have one
2078 ;;; method on any generic function up until the time classes exist.
2079 (defun early-method-specializers (early-method &optional objectsp)
2080 (if (and (listp early-method)
2081 (eq (car early-method) :early-method))
2082 (cond ((eq objectsp t)
2083 (or (fourth early-method)
2084 (setf (fourth early-method)
2085 (mapcar #'find-class (cadddr (fifth early-method))))))
2087 (cadddr (fifth early-method))))
2088 (error "~S is not an early-method." early-method)))
2090 (defun early-method-qualifiers (early-method)
2091 (cadr (fifth early-method)))
2093 (defun early-method-lambda-list (early-method)
2094 (caddr (fifth early-method)))
2096 (defun early-add-named-method (generic-function-name
2101 (let* ((gf (ensure-generic-function generic-function-name))
2103 (dolist (m (early-gf-methods gf))
2104 (when (and (equal (early-method-specializers m) specializers)
2105 (equal (early-method-qualifiers m) qualifiers))
2107 (new (make-a-method 'standard-method
2113 (when existing (remove-method gf existing))
2114 (add-method gf new)))
2116 ;;; This is the early version of ADD-METHOD. Later this will become a
2117 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2118 ;;; special knowledge about ADD-METHOD.
2119 (defun add-method (generic-function method)
2120 (when (not (fsc-instance-p generic-function))
2121 (error "Early ADD-METHOD didn't get a funcallable instance."))
2122 (when (not (and (listp method) (eq (car method) :early-method)))
2123 (error "Early ADD-METHOD didn't get an early method."))
2124 (push method (early-gf-methods generic-function))
2125 (set-arg-info generic-function :new-method method)
2126 (unless (assoc (!early-gf-name generic-function)
2127 *!generic-function-fixups*
2129 (update-dfun generic-function)))
2131 ;;; This is the early version of REMOVE-METHOD. See comments on
2132 ;;; the early version of ADD-METHOD.
2133 (defun remove-method (generic-function method)
2134 (when (not (fsc-instance-p generic-function))
2135 (error "An early remove-method didn't get a funcallable instance."))
2136 (when (not (and (listp method) (eq (car method) :early-method)))
2137 (error "An early remove-method didn't get an early method."))
2138 (setf (early-gf-methods generic-function)
2139 (remove method (early-gf-methods generic-function)))
2140 (set-arg-info generic-function)
2141 (unless (assoc (!early-gf-name generic-function)
2142 *!generic-function-fixups*
2144 (update-dfun generic-function)))
2146 ;;; This is the early version of GET-METHOD. See comments on the early
2147 ;;; version of ADD-METHOD.
2148 (defun get-method (generic-function qualifiers specializers
2149 &optional (errorp t))
2150 (if (early-gf-p generic-function)
2151 (or (dolist (m (early-gf-methods generic-function))
2152 (when (and (or (equal (early-method-specializers m nil)
2154 (equal (early-method-specializers m t)
2156 (equal (early-method-qualifiers m) qualifiers))
2159 (error "can't get early method")
2161 (real-get-method generic-function qualifiers specializers errorp)))
2163 (defun !fix-early-generic-functions ()
2164 (let ((accessors nil))
2165 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2166 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2167 (dolist (early-gf-spec *!early-generic-functions*)
2168 (when (every #'early-method-standard-accessor-p
2169 (early-gf-methods (gdefinition early-gf-spec)))
2170 (push early-gf-spec accessors)))
2171 (dolist (spec (nconc accessors
2172 '(accessor-method-slot-name
2173 generic-function-methods
2178 slot-definition-location
2179 slot-definition-name
2182 class-precedence-list
2183 slot-boundp-using-class
2184 (setf slot-value-using-class)
2185 slot-value-using-class
2188 funcallable-standard-class-p
2191 (setq *!early-generic-functions*
2193 (delete spec *!early-generic-functions* :test #'equal))))
2195 (dolist (early-gf-spec *!early-generic-functions*)
2196 (/show early-gf-spec)
2197 (let* ((gf (gdefinition early-gf-spec))
2198 (methods (mapcar (lambda (early-method)
2199 (let ((args (copy-list (fifth
2202 (early-method-specializers
2204 (apply #'real-make-a-method args)))
2205 (early-gf-methods gf))))
2206 (setf (generic-function-method-class gf) *the-class-standard-method*)
2207 (setf (generic-function-method-combination gf)
2208 *standard-method-combination*)
2209 (set-methods gf methods)))
2211 (dolist (fn *!early-functions*)
2213 (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2215 (dolist (fixup *!generic-function-fixups*)
2217 (let* ((fspec (car fixup))
2218 (gf (gdefinition fspec))
2219 (methods (mapcar (lambda (method)
2220 (let* ((lambda-list (first method))
2221 (specializers (second method))
2222 (method-fn-name (third method))
2223 (fn-name (or method-fn-name fspec))
2224 (fn (fdefinition fn-name))
2228 (lambda (args next-methods)
2232 `(call ,fn-name)))))
2233 (declare (type function fn))
2234 (make-a-method 'standard-method
2241 (setf (generic-function-method-class gf) *the-class-standard-method*)
2242 (setf (generic-function-method-combination gf)
2243 *standard-method-combination*)
2244 (set-methods gf methods))))
2245 (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2247 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2248 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2249 ;;; is really implemented.
2250 (defun parse-defmethod (cdr-of-form)
2251 (declare (list cdr-of-form))
2252 (let ((name (pop cdr-of-form))
2255 (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2256 (push (pop cdr-of-form) qualifiers)
2257 (return (setq qualifiers (nreverse qualifiers)))))
2258 (setq spec-ll (pop cdr-of-form))
2259 (values name qualifiers spec-ll cdr-of-form)))
2261 (defun parse-specializers (specializers)
2262 (declare (list specializers))
2263 (flet ((parse (spec)
2264 (let ((result (specializer-from-type spec)))
2265 (if (specializerp result)
2268 (error "~S was used as a specializer,~%~
2269 but is not the name of a class."
2271 (error "~S is not a legal specializer." spec))))))
2272 (mapcar #'parse specializers)))
2274 (defun unparse-specializers (specializers-or-method)
2275 (if (listp specializers-or-method)
2276 (flet ((unparse (spec)
2277 (if (specializerp spec)
2278 (let ((type (specializer-type spec)))
2279 (if (and (consp type)
2280 (eq (car type) 'class))
2281 (let* ((class (cadr type))
2282 (class-name (class-name class)))
2283 (if (eq class (find-class class-name nil))
2287 (error "~S is not a legal specializer." spec))))
2288 (mapcar #'unparse specializers-or-method))
2289 (unparse-specializers (method-specializers specializers-or-method))))
2291 (defun parse-method-or-spec (spec &optional (errorp t))
2292 (let (gf method name temp)
2295 gf (method-generic-function method)
2296 temp (and gf (generic-function-name gf))
2298 (make-method-spec temp
2299 (method-qualifiers method)
2300 (unparse-specializers
2301 (method-specializers method)))
2302 (make-symbol (format nil "~S" method))))
2303 (multiple-value-bind (gf-spec quals specls)
2304 (parse-defmethod spec)
2305 (and (setq gf (and (or errorp (gboundp gf-spec))
2306 (gdefinition gf-spec)))
2307 (let ((nreq (compute-discriminating-function-arglist-info gf)))
2308 (setq specls (append (parse-specializers specls)
2309 (make-list (- nreq (length specls))
2313 (setq method (get-method gf quals specls errorp))
2316 gf-spec quals (unparse-specializers specls))))))))
2317 (values gf method name)))
2319 (defun extract-parameters (specialized-lambda-list)
2320 (multiple-value-bind (parameters ignore1 ignore2)
2321 (parse-specialized-lambda-list specialized-lambda-list)
2322 (declare (ignore ignore1 ignore2))
2325 (defun extract-lambda-list (specialized-lambda-list)
2326 (multiple-value-bind (ignore1 lambda-list ignore2)
2327 (parse-specialized-lambda-list specialized-lambda-list)
2328 (declare (ignore ignore1 ignore2))
2331 (defun extract-specializer-names (specialized-lambda-list)
2332 (multiple-value-bind (ignore1 ignore2 specializers)
2333 (parse-specialized-lambda-list specialized-lambda-list)
2334 (declare (ignore ignore1 ignore2))
2337 (defun extract-required-parameters (specialized-lambda-list)
2338 (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2339 (parse-specialized-lambda-list specialized-lambda-list)
2340 (declare (ignore ignore1 ignore2 ignore3))
2341 required-parameters))
2343 (define-condition specialized-lambda-list-error
2344 (reference-condition simple-program-error)
2346 (:default-initargs :references (list '(:ansi-cl :section (3 4 3)))))
2348 (defun parse-specialized-lambda-list
2350 &optional supplied-keywords (allowed-keywords '(&optional &rest &key &aux))
2351 &aux (specialized-lambda-list-keywords
2352 '(&optional &rest &key &allow-other-keys &aux)))
2353 (let ((arg (car arglist)))
2354 (cond ((null arglist) (values nil nil nil nil))
2356 (values nil arglist nil nil))
2357 ((memq arg lambda-list-keywords)
2358 ;; non-standard lambda-list-keywords are errors.
2359 (unless (memq arg specialized-lambda-list-keywords)
2360 (error 'specialized-lambda-list-error
2361 :format-control "unknown specialized-lambda-list ~
2363 :format-arguments (list arg)))
2364 ;; no multiple &rest x &rest bla specifying
2365 (when (memq arg supplied-keywords)
2366 (error 'specialized-lambda-list-error
2367 :format-control "multiple occurrence of ~
2368 specialized-lambda-list keyword ~S~%"
2369 :format-arguments (list arg)))
2370 ;; And no placing &key in front of &optional, either.
2371 (unless (memq arg allowed-keywords)
2372 (error 'specialized-lambda-list-error
2373 :format-control "misplaced specialized-lambda-list ~
2375 :format-arguments (list arg)))
2376 ;; When we are at a lambda-list keyword, the parameters
2377 ;; don't include the lambda-list keyword; the lambda-list
2378 ;; does include the lambda-list keyword; and no
2379 ;; specializers are allowed to follow the lambda-list
2380 ;; keywords (at least for now).
2381 (multiple-value-bind (parameters lambda-list)
2382 (parse-specialized-lambda-list (cdr arglist)
2383 (cons arg supplied-keywords)
2385 (cons '&allow-other-keys
2386 (cdr (member arg allowed-keywords)))
2387 (cdr (member arg allowed-keywords))))
2388 (when (and (eq arg '&rest)
2389 (or (null lambda-list)
2390 (memq (car lambda-list)
2391 specialized-lambda-list-keywords)
2392 (not (or (null (cadr lambda-list))
2393 (memq (cadr lambda-list)
2394 specialized-lambda-list-keywords)))))
2395 (error 'specialized-lambda-list-error
2397 "in a specialized-lambda-list, excactly one ~
2398 variable must follow &REST.~%"
2399 :format-arguments nil))
2401 (cons arg lambda-list)
2405 ;; After a lambda-list keyword there can be no specializers.
2406 (multiple-value-bind (parameters lambda-list)
2407 (parse-specialized-lambda-list (cdr arglist)
2410 (values (cons (if (listp arg) (car arg) arg) parameters)
2411 (cons arg lambda-list)
2415 (multiple-value-bind (parameters lambda-list specializers required)
2416 (parse-specialized-lambda-list (cdr arglist))
2417 (values (cons (if (listp arg) (car arg) arg) parameters)
2418 (cons (if (listp arg) (car arg) arg) lambda-list)
2419 (cons (if (listp arg) (cadr arg) t) specializers)
2420 (cons (if (listp arg) (car arg) arg) required)))))))
2422 (setq *boot-state* 'early)
2424 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2425 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2426 ;;; walker stuff was only used for implementing stuff like that; maybe
2427 ;;; it's not needed any more? Hunt down what it was used for and see.
2429 (defmacro with-slots (slots instance &body body)
2430 (let ((in (gensym)))
2431 `(let ((,in ,instance))
2432 (declare (ignorable ,in))
2433 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2436 (and (symbolp instance)
2437 `((declare (%variable-rebinding ,in ,instance)))))
2439 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2441 (if (symbolp slot-entry)
2445 (if (symbolp slot-entry)
2447 (cadr slot-entry))))
2449 (slot-value ,in ',slot-name))))
2453 (defmacro with-accessors (slots instance &body body)
2454 (let ((in (gensym)))
2455 `(let ((,in ,instance))
2456 (declare (ignorable ,in))
2457 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2460 (and (symbolp instance)
2461 `((declare (%variable-rebinding ,in ,instance)))))
2463 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2464 (let ((var-name (car slot-entry))
2465 (accessor-name (cadr slot-entry)))
2466 `(,var-name (,accessor-name ,in))))