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 (declaim (notinline make-a-method
73 ensure-generic-function-using-class
77 (defvar *!early-functions*
78 '((make-a-method early-make-a-method
80 (add-named-method early-add-named-method
81 real-add-named-method)
84 ;;; For each of the early functions, arrange to have it point to its
85 ;;; early definition. Do this in a way that makes sure that if we
86 ;;; redefine one of the early definitions the redefinition will take
87 ;;; effect. This makes development easier.
88 (dolist (fns *!early-functions*)
89 (let ((name (car fns))
90 (early-name (cadr fns)))
91 (setf (gdefinition name)
94 (apply (fdefinition early-name) args))
97 ;;; *!GENERIC-FUNCTION-FIXUPS* is used by !FIX-EARLY-GENERIC-FUNCTIONS
98 ;;; to convert the few functions in the bootstrap which are supposed
99 ;;; to be generic functions but can't be early on.
100 (defvar *!generic-function-fixups*
102 ((generic-function method) ;lambda-list
103 (standard-generic-function method) ;specializers
104 real-add-method)) ;method-function
106 ((generic-function method)
107 (standard-generic-function method)
110 ((generic-function qualifiers specializers &optional (errorp t))
111 (standard-generic-function t t)
113 (ensure-generic-function-using-class
114 ((generic-function fun-name
115 &key generic-function-class environment
118 real-ensure-gf-using-class--generic-function)
119 ((generic-function fun-name
120 &key generic-function-class environment
123 real-ensure-gf-using-class--null))
125 ((proto-generic-function proto-method lambda-expression environment)
126 (standard-generic-function standard-method t t)
127 real-make-method-lambda))
128 (make-method-initargs-form
129 ((proto-generic-function proto-method
131 lambda-list environment)
132 (standard-generic-function standard-method t t t)
133 real-make-method-initargs-form))
134 (compute-effective-method
135 ((generic-function combin applicable-methods)
136 (generic-function standard-method-combination t)
137 standard-compute-effective-method))))
139 (defmacro defgeneric (fun-name lambda-list &body options)
140 (declare (type list lambda-list))
141 (unless (legal-fun-name-p fun-name)
142 (error 'simple-program-error
143 :format-control "illegal generic function name ~S"
144 :format-arguments (list fun-name)))
145 (check-gf-lambda-list lambda-list)
148 (flet ((duplicate-option (name)
149 (error 'simple-program-error
150 :format-control "The option ~S appears more than once."
151 :format-arguments (list name)))
152 (expand-method-definition (qab) ; QAB = qualifiers, arglist, body
153 (let* ((arglist-pos (position-if #'listp qab))
154 (arglist (elt qab arglist-pos))
155 (qualifiers (subseq qab 0 arglist-pos))
156 (body (nthcdr (1+ arglist-pos) qab)))
157 `(push (defmethod ,fun-name ,@qualifiers ,arglist ,@body)
158 (generic-function-initial-methods (fdefinition ',fun-name))))))
159 (macrolet ((initarg (key) `(getf initargs ,key)))
160 (dolist (option options)
161 (let ((car-option (car option)))
165 (consp (cadr option))
166 (member (first (cadr option))
167 ;; FIXME: this list is slightly weird.
168 ;; ANSI (on the DEFGENERIC page) in one
169 ;; place allows only OPTIMIZE; in
170 ;; another place gives this list of
171 ;; disallowed declaration specifiers.
172 ;; This seems to be the only place where
173 ;; the FUNCTION declaration is
174 ;; mentioned; TYPE seems to be missing.
175 ;; Very strange. -- CSR, 2002-10-21
176 '(declaration ftype function
177 inline notinline special)))
178 (error 'simple-program-error
179 :format-control "The declaration specifier ~S ~
180 is not allowed inside DEFGENERIC."
181 :format-arguments (list (cadr option))))
182 (push (cadr option) (initarg :declarations)))
184 (when (initarg car-option)
185 (duplicate-option car-option))
186 (unless (symbolp (cadr option))
187 (error 'simple-program-error
188 :format-control "METHOD-COMBINATION name not a ~
190 :format-arguments (list (cadr option))))
191 (setf (initarg car-option)
193 (:argument-precedence-order
194 (let* ((required (parse-lambda-list lambda-list))
195 (supplied (cdr option)))
196 (unless (= (length required) (length supplied))
197 (error 'simple-program-error
198 :format-control "argument count discrepancy in ~
199 :ARGUMENT-PRECEDENCE-ORDER clause."
200 :format-arguments nil))
201 (when (set-difference required supplied)
202 (error 'simple-program-error
203 :format-control "unequal sets for ~
204 :ARGUMENT-PRECEDENCE-ORDER clause: ~
206 :format-arguments (list required supplied)))
207 (setf (initarg car-option)
209 ((:documentation :generic-function-class :method-class)
210 (unless (proper-list-of-length-p option 2)
211 (error "bad list length for ~S" option))
212 (if (initarg car-option)
213 (duplicate-option car-option)
214 (setf (initarg car-option) `',(cadr option))))
216 (push (cdr option) methods))
218 ;; ANSI requires that unsupported things must get a
220 (error 'simple-program-error
221 :format-control "unsupported option ~S"
222 :format-arguments (list option))))))
224 (when (initarg :declarations)
225 (setf (initarg :declarations)
226 `',(initarg :declarations))))
228 (eval-when (:compile-toplevel :load-toplevel :execute)
229 (compile-or-load-defgeneric ',fun-name))
230 (load-defgeneric ',fun-name ',lambda-list ,@initargs)
231 ,@(mapcar #'expand-method-definition methods)
232 (fdefinition ',fun-name)))))
234 (defun compile-or-load-defgeneric (fun-name)
235 (proclaim-as-fun-name fun-name)
236 (note-name-defined fun-name :function)
237 (unless (eq (info :function :where-from fun-name) :declared)
238 (setf (info :function :where-from fun-name) :defined)
239 (setf (info :function :type fun-name)
240 (specifier-type 'function))))
242 (defun load-defgeneric (fun-name lambda-list &rest initargs)
243 (when (fboundp fun-name)
244 (style-warn "redefining ~S in DEFGENERIC" fun-name)
245 (let ((fun (fdefinition fun-name)))
246 (when (generic-function-p fun)
247 (loop for method in (generic-function-initial-methods fun)
248 do (remove-method fun method))
249 (setf (generic-function-initial-methods fun) '()))))
250 (apply #'ensure-generic-function
252 :lambda-list lambda-list
253 :definition-source `((defgeneric ,fun-name) ,*load-pathname*)
256 (define-condition generic-function-lambda-list-error
257 (reference-condition simple-program-error)
259 (:default-initargs :references (list '(:ansi-cl :section (3 4 2)))))
261 (defun check-gf-lambda-list (lambda-list)
262 (flet ((ensure (arg ok)
264 (error 'generic-function-lambda-list-error
266 "~@<invalid ~S ~_in the generic function lambda list ~S~:>"
267 :format-arguments (list arg lambda-list)))))
268 (multiple-value-bind (required optional restp rest keyp keys allowp
269 auxp aux morep more-context more-count)
270 (parse-lambda-list lambda-list)
271 (declare (ignore required)) ; since they're no different in a gf ll
272 (declare (ignore restp rest)) ; since they're no different in a gf ll
273 (declare (ignore allowp)) ; since &ALLOW-OTHER-KEYS is fine either way
274 (declare (ignore aux)) ; since we require AUXP=NIL
275 (declare (ignore more-context more-count)) ; safely ignored unless MOREP
276 ;; no defaults allowed for &OPTIONAL arguments
278 (ensure i (or (symbolp i)
279 (and (consp i) (symbolp (car i)) (null (cdr i))))))
280 ;; no defaults allowed for &KEY arguments
283 (ensure i (or (symbolp i)
285 (or (symbolp (car i))
293 (error "&AUX is not allowed in a generic function lambda list: ~S"
295 ;; Oh, *puhlease*... not specifically as per section 3.4.2 of
296 ;; the ANSI spec, but the CMU CL &MORE extension does not
298 (aver (not morep)))))
300 (defmacro defmethod (&rest args &environment env)
301 (multiple-value-bind (name qualifiers lambda-list body)
302 (parse-defmethod args)
303 (multiple-value-bind (proto-gf proto-method)
304 (prototypes-for-make-method-lambda name)
305 (expand-defmethod name
313 (defun prototypes-for-make-method-lambda (name)
314 (if (not (eq *boot-state* 'complete))
316 (let ((gf? (and (gboundp name)
317 (gdefinition name))))
319 (not (generic-function-p gf?)))
320 (values (class-prototype (find-class 'standard-generic-function))
321 (class-prototype (find-class 'standard-method)))
323 (class-prototype (or (generic-function-method-class gf?)
324 (find-class 'standard-method))))))))
326 ;;; Take a name which is either a generic function name or a list specifying
327 ;;; a SETF generic function (like: (SETF <generic-function-name>)). Return
328 ;;; the prototype instance of the method-class for that generic function.
330 ;;; If there is no generic function by that name, this returns the
331 ;;; default value, the prototype instance of the class
332 ;;; STANDARD-METHOD. This default value is also returned if the spec
333 ;;; names an ordinary function or even a macro. In effect, this leaves
334 ;;; the signalling of the appropriate error until load time.
336 ;;; Note: During bootstrapping, this function is allowed to return NIL.
337 (defun method-prototype-for-gf (name)
338 (let ((gf? (and (gboundp name)
339 (gdefinition name))))
340 (cond ((neq *boot-state* 'complete) nil)
342 (not (generic-function-p gf?))) ; Someone else MIGHT
343 ; error at load time.
344 (class-prototype (find-class 'standard-method)))
346 (class-prototype (or (generic-function-method-class gf?)
347 (find-class 'standard-method)))))))
349 (defun expand-defmethod (name
356 (multiple-value-bind (method-lambda unspecialized-lambda-list specializers)
357 (add-method-declarations name qualifiers lambda-list body env)
358 (multiple-value-bind (method-function-lambda initargs)
359 (make-method-lambda proto-gf proto-method method-lambda env)
360 (let ((initargs-form (make-method-initargs-form proto-gf
362 method-function-lambda
366 ;; Note: We could DECLAIM the ftype of the generic function
367 ;; here, since ANSI specifies that we create it if it does
368 ;; not exist. However, I chose not to, because I think it's
369 ;; more useful to support a style of programming where every
370 ;; generic function has an explicit DEFGENERIC and any typos
371 ;; in DEFMETHODs are warned about. Otherwise
373 ;; (DEFGENERIC FOO-BAR-BLETCH ((X T)))
374 ;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
375 ;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
376 ;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
377 ;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
378 ;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
380 ;; compiles without raising an error and runs without
381 ;; raising an error (since SIMPLE-VECTOR cases fall through
382 ;; to VECTOR) but still doesn't do what was intended. I hate
383 ;; that kind of bug (code which silently gives the wrong
384 ;; answer), so we don't do a DECLAIM here. -- WHN 20000229
385 ,(make-defmethod-form name qualifiers specializers
386 unspecialized-lambda-list
388 (class-name (class-of proto-method))
391 (getf (getf initargs :plist)
392 :pv-table-symbol)))))))
394 (defun interned-symbol-p (x)
395 (and (symbolp x) (symbol-package x)))
397 (defun make-defmethod-form (name qualifiers specializers
398 unspecialized-lambda-list method-class-name
399 initargs-form &optional pv-table-symbol)
402 (if (and (interned-symbol-p (fun-name-block-name name))
403 (every #'interned-symbol-p qualifiers)
406 (and (eq (car s) 'eql)
408 (let ((sv (eval (cadr s))))
409 (or (interned-symbol-p sv)
412 (standard-char-p sv)))))
413 (interned-symbol-p s)))
415 (consp initargs-form)
416 (eq (car initargs-form) 'list*)
417 (memq (cadr initargs-form) '(:function :fast-function))
418 (consp (setq fn (caddr initargs-form)))
419 (eq (car fn) 'function)
420 (consp (setq fn-lambda (cadr fn)))
421 (eq (car fn-lambda) 'lambda))
422 (let* ((specls (mapcar (lambda (specl)
424 `(,(car specl) ,(eval (cadr specl)))
427 (mname `(,(if (eq (cadr initargs-form) :function)
428 'slow-method 'fast-method)
429 ,name ,@qualifiers ,specls)))
431 (defun ,mname ,(cadr fn-lambda)
433 ,(make-defmethod-form-internal
434 name qualifiers `',specls
435 unspecialized-lambda-list method-class-name
436 `(list* ,(cadr initargs-form)
438 ,@(cdddr initargs-form))
440 (make-defmethod-form-internal
442 `(list ,@(mapcar (lambda (specializer)
443 (if (consp specializer)
444 ``(,',(car specializer)
445 ,,(cadr specializer))
448 unspecialized-lambda-list
453 (defun make-defmethod-form-internal
454 (name qualifiers specializers-form unspecialized-lambda-list
455 method-class-name initargs-form &optional pv-table-symbol)
461 ',unspecialized-lambda-list
463 ;; Paper over a bug in KCL by passing the cache-symbol here in
464 ;; addition to in the list. FIXME: We should no longer need to do
465 ;; this, since the CLOS code is now SBCL-specific, and doesn't
466 ;; need to be ported to every buggy compiler in existence.
469 (defmacro make-method-function (method-lambda &environment env)
470 (make-method-function-internal method-lambda env))
472 (defun make-method-function-internal (method-lambda &optional env)
473 (multiple-value-bind (proto-gf proto-method)
474 (prototypes-for-make-method-lambda nil)
475 (multiple-value-bind (method-function-lambda initargs)
476 (make-method-lambda proto-gf proto-method method-lambda env)
477 (make-method-initargs-form proto-gf
479 method-function-lambda
483 (defun add-method-declarations (name qualifiers lambda-list body env)
484 (declare (ignore env))
485 (multiple-value-bind (parameters unspecialized-lambda-list specializers)
486 (parse-specialized-lambda-list lambda-list)
487 (multiple-value-bind (real-body declarations documentation)
489 (values `(lambda ,unspecialized-lambda-list
490 ,@(when documentation `(,documentation))
491 ;; (Old PCL code used a somewhat different style of
492 ;; list for %METHOD-NAME values. Our names use
493 ;; ,@QUALIFIERS instead of ,QUALIFIERS so that the
494 ;; method names look more like what you see in a
497 ;; FIXME: As of sbcl-0.7.0.6, code elsewhere, at
498 ;; least the code to set up named BLOCKs around the
499 ;; bodies of methods, depends on the function's base
500 ;; name being the first element of the %METHOD-NAME
501 ;; list. It would be good to remove this dependency,
502 ;; perhaps by building the BLOCK here, or by using
503 ;; another declaration (e.g. %BLOCK-NAME), so that
504 ;; our method debug names are free to have any format,
505 ;; e.g. (:METHOD PRINT-OBJECT :AROUND (CLOWN T)).
507 ;; Further, as of sbcl-0.7.9.10, the code to
508 ;; implement NO-NEXT-METHOD is coupled to the form of
509 ;; this declaration; see the definition of
510 ;; CALL-NO-NEXT-METHOD (and the passing of
511 ;; METHOD-NAME-DECLARATION arguments around the
512 ;; various CALL-NEXT-METHOD logic).
513 (declare (%method-name (,name
516 (declare (%method-lambda-list ,@lambda-list))
519 unspecialized-lambda-list specializers))))
521 (defun real-make-method-initargs-form (proto-gf proto-method
522 method-lambda initargs env)
523 (declare (ignore proto-gf proto-method))
524 (unless (and (consp method-lambda)
525 (eq (car method-lambda) 'lambda))
526 (error "The METHOD-LAMBDA argument to MAKE-METHOD-FUNCTION, ~S, ~
527 is not a lambda form."
529 (make-method-initargs-form-internal method-lambda initargs env))
531 (unless (fboundp 'make-method-initargs-form)
532 (setf (gdefinition 'make-method-initargs-form)
533 (symbol-function 'real-make-method-initargs-form)))
535 (defun real-make-method-lambda (proto-gf proto-method method-lambda env)
536 (declare (ignore proto-gf proto-method))
537 (make-method-lambda-internal method-lambda env))
539 ;;; a helper function for creating Python-friendly type declarations
540 ;;; in DEFMETHOD forms
541 (defun parameter-specializer-declaration-in-defmethod (parameter specializer)
542 (cond ((and (consp specializer)
543 (eq (car specializer) 'eql))
544 ;; KLUDGE: ANSI, in its wisdom, says that
545 ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at
546 ;; DEFMETHOD expansion time. Thus, although one might think
548 ;; (DEFMETHOD FOO ((X PACKAGE)
551 ;; the PACKAGE and (EQL 12) forms are both parallel type
552 ;; names, they're not, as is made clear when you do
553 ;; (DEFMETHOD FOO ((X PACKAGE)
556 ;; where Y needs to be a symbol named "BAR", not some cons
557 ;; made by (CONS 'QUOTE 'BAR). I.e. when the
558 ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument
559 ;; to be of type (EQL X). It'd be easy to transform one to
560 ;; the other, but it'd be somewhat messier to do so while
561 ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd
562 ;; once. (The new code wouldn't be messy, but it'd require a
563 ;; big transformation of the old code.) So instead we punt.
567 ;; KLUDGE: For some low-level implementation
568 ;; classes, perhaps because of some problems related
569 ;; to the incomplete integration of PCL into SBCL's
570 ;; type system, some specializer classes can't be
571 ;; declared as argument types. E.g.
572 ;; (DEFMETHOD FOO ((X SLOT-OBJECT))
573 ;; (DECLARE (TYPE SLOT-OBJECT X))
576 ;; (DEFSTRUCT BAR A B)
578 ;; perhaps because of the way that STRUCTURE-OBJECT
579 ;; inherits both from SLOT-OBJECT and from
580 ;; SB-KERNEL:INSTANCE. In an effort to sweep such
581 ;; problems under the rug, we exclude these problem
582 ;; cases by blacklisting them here. -- WHN 2001-01-19
585 ((not (eq *boot-state* 'complete))
586 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
587 ;; types which don't match their specializers. (Specifically,
588 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
589 ;; second argument.) Hopefully it only does this kind of
590 ;; weirdness when bootstrapping.. -- WHN 20000610
592 ((var-globally-special-p parameter)
593 ;; KLUDGE: Don't declare types for global special variables
594 ;; -- our rebinding magic for SETQ cases don't work right
597 ;; FIXME: It would be better to detect the SETQ earlier and
598 ;; skip declarations for specials only when needed, not
604 ;; Otherwise, we can usually make Python very happy.
605 (let ((kind (info :type :kind specializer)))
607 ((:primitive) `(type ,specializer ,parameter))
609 (let ((class (find-class specializer nil)))
610 ;; CLASS can be null here if the user has erroneously
611 ;; tried to use a defined type as a specializer; it
612 ;; can be a non-BUILT-IN-CLASS if the user defines a
613 ;; type and calls (SETF FIND-CLASS) in a consistent
615 (when (and class (typep class 'built-in-class))
616 `(type ,specializer ,parameter))))
618 (let ((class (find-class specializer nil)))
621 (if (typep class '(or built-in-class structure-class))
622 `(type ,specializer ,parameter)
623 ;; don't declare CLOS classes as parameters;
624 ;; it's too expensive.
627 ;; we can get here, and still not have a failure
628 ;; case, by doing MOP programming like (PROGN
629 ;; (ENSURE-CLASS 'FOO) (DEFMETHOD BAR ((X FOO))
630 ;; ...)). Best to let the user know we haven't
631 ;; been able to extract enough information:
633 "~@<can't find type for presumed class ~S in ~S.~@:>"
635 'parameter-specializer-declaration-in-defmethod)
637 ((:forthcoming-defclass-type) '(ignorable)))))))
639 (defun make-method-lambda-internal (method-lambda &optional env)
640 (unless (and (consp method-lambda) (eq (car method-lambda) 'lambda))
641 (error "The METHOD-LAMBDA argument to MAKE-METHOD-LAMBDA, ~S, ~
642 is not a lambda form."
644 (multiple-value-bind (real-body declarations documentation)
645 (parse-body (cddr method-lambda))
646 (let* ((name-decl (get-declaration '%method-name declarations))
647 (sll-decl (get-declaration '%method-lambda-list declarations))
648 (method-name (when (consp name-decl) (car name-decl)))
649 (generic-function-name (when method-name (car method-name)))
650 (specialized-lambda-list (or sll-decl (cadr method-lambda))))
651 (multiple-value-bind (parameters lambda-list specializers)
652 (parse-specialized-lambda-list specialized-lambda-list)
653 (let* ((required-parameters
654 (mapcar (lambda (r s) (declare (ignore s)) r)
657 (slots (mapcar #'list required-parameters))
661 ;; These declarations seem to be used by PCL to pass
662 ;; information to itself; when I tried to delete 'em
663 ;; ca. 0.6.10 it didn't work. I'm not sure how
664 ;; they work, but note the (VAR-DECLARATION '%CLASS ..)
665 ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30
667 (mapcar (lambda (a s) (and (symbolp s)
672 ;; These TYPE declarations weren't in the original
673 ;; PCL code, but the Python compiler likes them a
674 ;; lot. (We're telling the compiler about our
675 ;; knowledge of specialized argument types so that
676 ;; it can avoid run-time type dispatch overhead,
677 ;; which can be a huge win for Python.)
679 ;; KLUDGE: when I tried moving these to
680 ;; ADD-METHOD-DECLARATIONS, things broke. No idea
681 ;; why. -- CSR, 2004-06-16
682 ,@(mapcar #'parameter-specializer-declaration-in-defmethod
686 ;; Remove the documentation string and insert the
687 ;; appropriate class declarations. The documentation
688 ;; string is removed to make it easy for us to insert
689 ;; new declarations later, they will just go after the
690 ;; CADR of the method lambda. The class declarations
691 ;; are inserted to communicate the class of the method's
692 ;; arguments to the code walk.
693 `(lambda ,lambda-list
694 ;; The default ignorability of method parameters
695 ;; doesn't seem to be specified by ANSI. PCL had
696 ;; them basically ignorable but was a little
697 ;; inconsistent. E.g. even though the two
698 ;; method definitions
699 ;; (DEFMETHOD FOO ((X T) (Y T)) "Z")
700 ;; (DEFMETHOD FOO ((X T) Y) "Z")
701 ;; are otherwise equivalent, PCL treated Y as
702 ;; ignorable in the first definition but not in the
703 ;; second definition. We make all required
704 ;; parameters ignorable as a way of systematizing
705 ;; the old PCL behavior. -- WHN 2000-11-24
706 (declare (ignorable ,@required-parameters))
709 (block ,(fun-name-block-name generic-function-name)
711 (constant-value-p (and (null (cdr real-body))
712 (constantp (car real-body))))
713 (constant-value (and constant-value-p
714 (eval (car real-body))))
715 (plist (and constant-value-p
716 (or (typep constant-value
717 '(or number character))
718 (and (symbolp constant-value)
719 (symbol-package constant-value)))
720 (list :constant-value constant-value)))
721 (applyp (dolist (p lambda-list nil)
722 (cond ((memq p '(&optional &rest &key))
727 (walked-lambda call-next-method-p closurep
728 next-method-p-p setq-p)
729 (walk-method-lambda method-lambda
734 (multiple-value-bind (walked-lambda-body
736 walked-documentation)
737 (parse-body (cddr walked-lambda))
738 (declare (ignore walked-documentation))
739 (when (or next-method-p-p call-next-method-p)
740 (setq plist (list* :needs-next-methods-p t plist)))
741 (when (some #'cdr slots)
742 (multiple-value-bind (slot-name-lists call-list)
743 (slot-name-lists-from-slots slots calls)
744 (let ((pv-table-symbol (make-symbol "pv-table")))
746 `(,@(when slot-name-lists
747 `(:slot-name-lists ,slot-name-lists))
749 `(:call-list ,call-list))
750 :pv-table-symbol ,pv-table-symbol
752 (setq walked-lambda-body
753 `((pv-binding (,required-parameters
756 ,@walked-lambda-body))))))
757 (when (and (memq '&key lambda-list)
758 (not (memq '&allow-other-keys lambda-list)))
759 (let ((aux (memq '&aux lambda-list)))
760 (setq lambda-list (nconc (ldiff lambda-list aux)
761 (list '&allow-other-keys)
763 (values `(lambda (.method-args. .next-methods.)
764 (simple-lexical-method-functions
765 (,lambda-list .method-args. .next-methods.
768 :next-method-p-p ,next-method-p-p
770 ;; we need to pass this along
771 ;; so that NO-NEXT-METHOD can
772 ;; be given a suitable METHOD
773 ;; argument; we need the
774 ;; QUALIFIERS and SPECIALIZERS
775 ;; inside the declaration to
776 ;; give to FIND-METHOD.
777 :method-name-declaration ,name-decl
780 ,@walked-declarations
781 ,@walked-lambda-body))
784 ,@(when documentation
785 `(:documentation ,documentation)))))))))))
787 (unless (fboundp 'make-method-lambda)
788 (setf (gdefinition 'make-method-lambda)
789 (symbol-function 'real-make-method-lambda)))
791 (defmacro simple-lexical-method-functions ((lambda-list
797 ,method-args ,next-methods
798 (bind-simple-lexical-method-macros (,method-args ,next-methods)
799 (bind-lexical-method-functions (,@lmf-options)
800 (bind-args (,lambda-list ,method-args)
803 (defmacro fast-lexical-method-functions ((lambda-list
809 `(bind-fast-lexical-method-macros (,args ,rest-arg ,next-method-call)
810 (bind-lexical-method-functions (,@lmf-options)
811 (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg)
814 (defmacro bind-simple-lexical-method-macros ((method-args next-methods)
816 `(macrolet ((call-next-method-bind (&body body)
817 `(let ((.next-method. (car ,',next-methods))
818 (,',next-methods (cdr ,',next-methods)))
819 .next-method. ,',next-methods
821 (call-next-method-body (method-name-declaration cnm-args)
823 (funcall (if (std-instance-p .next-method.)
824 (method-function .next-method.)
825 .next-method.) ; for early methods
826 (or ,cnm-args ,',method-args)
828 (apply #'call-no-next-method ',method-name-declaration
829 (or ,cnm-args ,',method-args))))
830 (next-method-p-body ()
831 `(not (null .next-method.)))
832 (with-rebound-original-args ((call-next-method-p setq-p)
834 (declare (ignore call-next-method-p setq-p))
838 (defun call-no-next-method (method-name-declaration &rest args)
839 (destructuring-bind (name) method-name-declaration
840 (destructuring-bind (name &rest qualifiers-and-specializers) name
841 ;; KLUDGE: inefficient traversal, but hey. This should only
842 ;; happen on the slow error path anyway.
843 (let* ((qualifiers (butlast qualifiers-and-specializers))
844 (specializers (car (last qualifiers-and-specializers)))
845 (method (find-method (gdefinition name) qualifiers specializers)))
846 (apply #'no-next-method
847 (method-generic-function method)
851 (defstruct (method-call (:copier nil))
852 (function #'identity :type function)
855 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
857 (defmacro invoke-method-call1 (function args cm-args)
858 `(let ((.function. ,function)
860 (.cm-args. ,cm-args))
861 (if (and .cm-args. (null (cdr .cm-args.)))
862 (funcall .function. .args. (car .cm-args.))
863 (apply .function. .args. .cm-args.))))
865 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
866 `(invoke-method-call1 (method-call-function ,method-call)
868 `(list* ,@required-args+rest-arg)
869 `(list ,@required-args+rest-arg))
870 (method-call-call-method-args ,method-call)))
872 (defstruct (fast-method-call (:copier nil))
873 (function #'identity :type function)
878 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
880 (defmacro fmc-funcall (fn pv-cell next-method-call &rest args)
881 `(funcall ,fn ,pv-cell ,next-method-call ,@args))
883 (defmacro invoke-fast-method-call (method-call &rest required-args+rest-arg)
884 `(fmc-funcall (fast-method-call-function ,method-call)
885 (fast-method-call-pv-cell ,method-call)
886 (fast-method-call-next-method-call ,method-call)
887 ,@required-args+rest-arg))
889 (defstruct (fast-instance-boundp (:copier nil))
890 (index 0 :type fixnum))
892 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
894 (eval-when (:compile-toplevel :load-toplevel :execute)
895 (defvar *allow-emf-call-tracing-p* nil)
896 (defvar *enable-emf-call-tracing-p* #-sb-show nil #+sb-show t))
898 ;;;; effective method functions
900 (defvar *emf-call-trace-size* 200)
901 (defvar *emf-call-trace* nil)
902 (defvar *emf-call-trace-index* 0)
904 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
905 ;;; without explanation. It appears to be intended for debugging, so
906 ;;; it might be useful someday, so I haven't deleted it.
907 ;;; But it isn't documented and isn't used for anything now, so
908 ;;; I've conditionalized it out of the base system. -- WHN 19991213
910 (defun show-emf-call-trace ()
911 (when *emf-call-trace*
912 (let ((j *emf-call-trace-index*)
913 (*enable-emf-call-tracing-p* nil))
914 (format t "~&(The oldest entries are printed first)~%")
915 (dotimes-fixnum (i *emf-call-trace-size*)
916 (let ((ct (aref *emf-call-trace* j)))
917 (when ct (print ct)))
919 (when (= j *emf-call-trace-size*)
922 (defun trace-emf-call-internal (emf format args)
923 (unless *emf-call-trace*
924 (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
925 (setf (aref *emf-call-trace* *emf-call-trace-index*)
926 (list* emf format args))
927 (incf *emf-call-trace-index*)
928 (when (= *emf-call-trace-index* *emf-call-trace-size*)
929 (setq *emf-call-trace-index* 0)))
931 (defmacro trace-emf-call (emf format args)
932 (when *allow-emf-call-tracing-p*
933 `(when *enable-emf-call-tracing-p*
934 (trace-emf-call-internal ,emf ,format ,args))))
936 (defmacro invoke-effective-method-function-fast
937 (emf restp &rest required-args+rest-arg)
939 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
940 (invoke-fast-method-call ,emf ,@required-args+rest-arg)))
942 (defmacro invoke-effective-method-function (emf restp
943 &rest required-args+rest-arg)
944 (unless (constantp restp)
945 (error "The RESTP argument is not constant."))
946 ;; FIXME: The RESTP handling here is confusing and maybe slightly
947 ;; broken if RESTP evaluates to a non-self-evaluating form. E.g. if
948 ;; (INVOKE-EFFECTIVE-METHOD-FUNCTION EMF '(ERROR "gotcha") ...)
949 ;; then TRACE-EMF-CALL-CALL-INTERNAL might die on a gotcha error.
950 (setq restp (eval restp))
952 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
953 (cond ((typep ,emf 'fast-method-call)
954 (invoke-fast-method-call ,emf ,@required-args+rest-arg))
955 ;; "What," you may wonder, "do these next two clauses do?"
956 ;; In that case, you are not a PCL implementor, for they
957 ;; considered this to be self-documenting.:-| Or CSR, for
958 ;; that matter, since he can also figure it out by looking
959 ;; at it without breaking stride. For the rest of us,
960 ;; though: From what the code is doing with .SLOTS. and
961 ;; whatnot, evidently it's implementing SLOT-VALUEish and
962 ;; GET-SLOT-VALUEish things. Then we can reason backwards
963 ;; and conclude that setting EMF to a FIXNUM is an
964 ;; optimized way to represent these slot access operations.
965 ,@(when (and (null restp) (= 1 (length required-args+rest-arg)))
966 `(((typep ,emf 'fixnum)
967 (let* ((.slots. (get-slots-or-nil
968 ,(car required-args+rest-arg)))
969 (value (when .slots. (clos-slots-ref .slots. ,emf))))
970 (if (eq value +slot-unbound+)
971 (slot-unbound-internal ,(car required-args+rest-arg)
974 ,@(when (and (null restp) (= 2 (length required-args+rest-arg)))
975 `(((typep ,emf 'fixnum)
976 (let ((.new-value. ,(car required-args+rest-arg))
977 (.slots. (get-slots-or-nil
978 ,(cadr required-args+rest-arg))))
980 (setf (clos-slots-ref .slots. ,emf) .new-value.))))))
981 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
982 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
983 ;; there was no explanation and presumably the code is 10+
984 ;; years stale, I simply deleted it. -- WHN)
988 (invoke-method-call ,emf ,restp ,@required-args+rest-arg))
991 `(apply (the function ,emf) ,@required-args+rest-arg)
992 `(funcall (the function ,emf)
993 ,@required-args+rest-arg))))))))
995 (defun invoke-emf (emf args)
996 (trace-emf-call emf t args)
999 (let* ((arg-info (fast-method-call-arg-info emf))
1000 (restp (cdr arg-info))
1001 (nreq (car arg-info)))
1003 (let* ((rest-args (nthcdr nreq args))
1004 (req-args (ldiff args rest-args)))
1005 (apply (fast-method-call-function emf)
1006 (fast-method-call-pv-cell emf)
1007 (fast-method-call-next-method-call emf)
1008 (nconc req-args (list rest-args))))
1011 (invoke-fast-method-call emf)
1012 (error "wrong number of args")))
1015 (invoke-fast-method-call emf (car args))
1016 (error "wrong number of args")))
1019 (invoke-fast-method-call emf (car args) (cadr args))
1020 (error "wrong number of args")))
1022 (apply (fast-method-call-function emf)
1023 (fast-method-call-pv-cell emf)
1024 (fast-method-call-next-method-call emf)
1027 (apply (method-call-function emf)
1029 (method-call-call-method-args emf)))
1031 (cond ((null args) (error "1 or 2 args were expected."))
1033 (let* ((slots (get-slots (car args)))
1034 (value (clos-slots-ref slots emf)))
1035 (if (eq value +slot-unbound+)
1036 (slot-unbound-internal (car args) emf)
1039 (setf (clos-slots-ref (get-slots (cadr args)) emf)
1041 (t (error "1 or 2 args were expected."))))
1042 (fast-instance-boundp
1043 (if (or (null args) (cdr args))
1044 (error "1 arg was expected.")
1045 (let ((slots (get-slots (car args))))
1046 (not (eq (clos-slots-ref slots
1047 (fast-instance-boundp-index emf))
1052 (defmacro bind-fast-lexical-method-macros ((args rest-arg next-method-call)
1054 (let* ((all-params (append args (when rest-arg (list rest-arg))))
1055 (rebindings (mapcar (lambda (x) (list x x)) all-params)))
1056 `(macrolet ((narrowed-emf (emf)
1057 ;; INVOKE-EFFECTIVE-METHOD-FUNCTION has code in it to
1058 ;; dispatch on the possibility that EMF might be of
1059 ;; type FIXNUM (as an optimized representation of a
1060 ;; slot accessor). But as far as I (WHN 2002-06-11)
1061 ;; can tell, it's impossible for such a representation
1062 ;; to end up as .NEXT-METHOD-CALL. By reassuring
1063 ;; INVOKE-E-M-F that when called from this context
1064 ;; it needn't worry about the FIXNUM case, we can
1065 ;; keep those cases from being compiled, which is
1066 ;; good both because it saves bytes and because it
1067 ;; avoids annoying type mismatch compiler warnings.
1069 ;; KLUDGE: In sbcl-0.7.4.29, the compiler's type
1070 ;; system isn't smart enough about NOT and
1071 ;; intersection types to benefit from a (NOT FIXNUM)
1072 ;; declaration here. -- WHN 2002-06-12 (FIXME: maybe
1073 ;; it is now... -- CSR, 2003-06-07)
1075 ;; FIXME: Might the FUNCTION type be omittable here,
1076 ;; leaving only METHOD-CALLs? Failing that, could this
1077 ;; be documented somehow? (It'd be nice if the types
1078 ;; involved could be understood without solving the
1079 ;; halting problem.)
1080 `(the (or function method-call fast-method-call)
1082 (call-next-method-bind (&body body)
1084 (call-next-method-body (method-name-declaration cnm-args)
1085 `(if ,',next-method-call
1087 ;; This declaration suppresses a "deleting
1088 ;; unreachable code" note for the following IF
1089 ;; when REST-ARG is NIL. It is not nice for
1090 ;; debugging SBCL itself, but at least it
1091 ;; keeps us from annoying users.
1092 (declare (optimize (inhibit-warnings 3)))
1093 (if (and (null ',rest-arg)
1095 (eq (car cnm-args) 'list))
1096 `(invoke-effective-method-function
1097 (narrowed-emf ,',next-method-call)
1100 (let ((call `(invoke-effective-method-function
1101 (narrowed-emf ,',next-method-call)
1102 ,',(not (null rest-arg))
1104 ,@',(when rest-arg `(,rest-arg)))))
1106 (bind-args ((,@',args
1108 `(&rest ,rest-arg)))
1113 ;; As above, this declaration suppresses code
1115 (declare (optimize (inhibit-warnings 3)))
1116 (if (and (null ',rest-arg)
1118 (eq (car cnm-args) 'list))
1119 `(call-no-next-method ',method-name-declaration
1121 `(call-no-next-method ',method-name-declaration
1125 (next-method-p-body ()
1126 `(not (null ,',next-method-call)))
1127 (with-rebound-original-args ((cnm-p setq-p) &body body)
1128 (if (or cnm-p setq-p)
1130 (declare (ignorable ,@',all-params))
1135 (defmacro bind-lexical-method-functions
1136 ((&key call-next-method-p next-method-p-p setq-p
1137 closurep applyp method-name-declaration)
1139 (cond ((and (null call-next-method-p) (null next-method-p-p)
1140 (null closurep) (null applyp) (null setq-p))
1143 `(call-next-method-bind
1144 (flet (,@(and call-next-method-p
1145 `((call-next-method (&rest cnm-args)
1146 (call-next-method-body
1147 ,method-name-declaration
1149 ,@(and next-method-p-p
1151 (next-method-p-body)))))
1152 (with-rebound-original-args (,call-next-method-p ,setq-p)
1155 (defmacro bind-args ((lambda-list args) &body body)
1156 (let ((args-tail '.args-tail.)
1159 (flet ((process-var (var)
1160 (if (memq var lambda-list-keywords)
1163 (&optional (setq state 'optional))
1164 (&key (setq state 'key))
1166 (&rest (setq state 'rest))
1167 (&aux (setq state 'aux))
1170 "encountered the non-standard lambda list keyword ~S"
1174 (required `((,var (pop ,args-tail))))
1175 (optional (cond ((not (consp var))
1176 `((,var (when ,args-tail
1177 (pop ,args-tail)))))
1179 `((,(car var) (if ,args-tail
1183 `((,(caddr var) ,args-tail)
1184 (,(car var) (if ,args-tail
1187 (rest `((,var ,args-tail)))
1188 (key (cond ((not (consp var))
1190 (get-key-arg-tail ,(keywordicate var)
1193 (multiple-value-bind (keyword variable)
1194 (if (consp (car var))
1197 (values (keywordicate (car var))
1199 `((,key (get-key-arg-tail ',keyword
1205 (multiple-value-bind (keyword variable)
1206 (if (consp (car var))
1209 (values (keywordicate (car var))
1211 `((,key (get-key-arg-tail ',keyword
1218 (let ((bindings (mapcan #'process-var lambda-list)))
1219 `(let* ((,args-tail ,args)
1222 ,@(when (eq state 'optional)
1223 `((unless (null ,args-tail)
1224 (error 'simple-program-error
1225 :format-control "surplus arguments: ~S"
1226 :format-arguments (list ,args-tail)))))))
1227 (declare (ignorable ,args-tail .dummy0.))
1230 (defun get-key-arg-tail (keyword list)
1231 (loop for (key . tail) on list by #'cddr
1233 ;; FIXME: Do we want to export this symbol? Or maybe use an
1234 ;; (ERROR 'SIMPLE-PROGRAM-ERROR) form?
1235 (sb-c::%odd-key-args-error)
1236 when (eq key keyword)
1239 (defun walk-method-lambda (method-lambda required-parameters env slots calls)
1240 (let ((call-next-method-p nil) ; flag indicating that CALL-NEXT-METHOD
1241 ; should be in the method definition
1242 (closurep nil) ; flag indicating that #'CALL-NEXT-METHOD
1243 ; was seen in the body of a method
1244 (next-method-p-p nil) ; flag indicating that NEXT-METHOD-P
1245 ; should be in the method definition
1247 (flet ((walk-function (form context env)
1248 (cond ((not (eq context :eval)) form)
1249 ;; FIXME: Jumping to a conclusion from the way it's used
1250 ;; above, perhaps CONTEXT should be called SITUATION
1251 ;; (after the term used in the ANSI specification of
1252 ;; EVAL-WHEN) and given modern ANSI keyword values
1253 ;; like :LOAD-TOPLEVEL.
1254 ((not (listp form)) form)
1255 ((eq (car form) 'call-next-method)
1256 (setq call-next-method-p t)
1258 ((eq (car form) 'next-method-p)
1259 (setq next-method-p-p t)
1261 ((memq (car form) '(setq multiple-value-setq))
1262 ;; FIXME: this is possibly a little strong as
1263 ;; conditions go. Ideally we would want to detect
1264 ;; which, if any, of the method parameters are
1265 ;; being set, and communicate that information to
1266 ;; e.g. SPLIT-DECLARATIONS. However, the brute
1267 ;; force method doesn't really cost much; a little
1268 ;; loss of discrimination over IGNORED variables
1269 ;; should be all. -- CSR, 2004-07-01
1272 ((and (eq (car form) 'function)
1273 (cond ((eq (cadr form) 'call-next-method)
1274 (setq call-next-method-p t)
1277 ((eq (cadr form) 'next-method-p)
1278 (setq next-method-p-p t)
1282 ((and (memq (car form)
1283 '(slot-value set-slot-value slot-boundp))
1284 (constantp (caddr form)))
1285 (let ((parameter (can-optimize-access form
1288 (let ((fun (ecase (car form)
1289 (slot-value #'optimize-slot-value)
1290 (set-slot-value #'optimize-set-slot-value)
1291 (slot-boundp #'optimize-slot-boundp))))
1292 (funcall fun slots parameter form))))
1293 ((and (eq (car form) 'apply)
1295 (eq (car (cadr form)) 'function)
1296 (generic-function-name-p (cadr (cadr form))))
1297 (optimize-generic-function-call
1298 form required-parameters env slots calls))
1299 ((generic-function-name-p (car form))
1300 (optimize-generic-function-call
1301 form required-parameters env slots calls))
1304 (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1305 (values walked-lambda
1311 (defun generic-function-name-p (name)
1312 (and (legal-fun-name-p name)
1314 (if (eq *boot-state* 'complete)
1315 (standard-generic-function-p (gdefinition name))
1316 (funcallable-instance-p (gdefinition name)))))
1318 (defvar *method-function-plist* (make-hash-table :test 'eq))
1321 (defvar *mf1cp* nil)
1324 (defvar *mf2cp* nil)
1326 (defun method-function-plist (method-function)
1327 (unless (eq method-function *mf1*)
1328 (rotatef *mf1* *mf2*)
1329 (rotatef *mf1p* *mf2p*)
1330 (rotatef *mf1cp* *mf2cp*))
1331 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1332 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1333 (unless (eq method-function *mf1*)
1334 (setf *mf1* method-function
1336 *mf1p* (gethash method-function *method-function-plist*)))
1339 (defun (setf method-function-plist)
1340 (val method-function)
1341 (unless (eq method-function *mf1*)
1342 (rotatef *mf1* *mf2*)
1343 (rotatef *mf1cp* *mf2cp*)
1344 (rotatef *mf1p* *mf2p*))
1345 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1346 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1347 (setf *mf1* method-function
1351 (defun method-function-get (method-function key &optional default)
1352 (getf (method-function-plist method-function) key default))
1354 (defun (setf method-function-get)
1355 (val method-function key)
1356 (setf (getf (method-function-plist method-function) key) val))
1358 (defun method-function-pv-table (method-function)
1359 (method-function-get method-function :pv-table))
1361 (defun method-function-method (method-function)
1362 (method-function-get method-function :method))
1364 (defun method-function-needs-next-methods-p (method-function)
1365 (method-function-get method-function :needs-next-methods-p t))
1367 (defmacro method-function-closure-generator (method-function)
1368 `(method-function-get ,method-function 'closure-generator))
1370 (defun load-defmethod
1371 (class name quals specls ll initargs &optional pv-table-symbol)
1372 (setq initargs (copy-tree initargs))
1373 (let ((method-spec (or (getf initargs :method-spec)
1374 (make-method-spec name quals specls))))
1375 (setf (getf initargs :method-spec) method-spec)
1376 (load-defmethod-internal class name quals specls
1377 ll initargs pv-table-symbol)))
1379 (defun load-defmethod-internal
1380 (method-class gf-spec qualifiers specializers lambda-list
1381 initargs pv-table-symbol)
1382 (when pv-table-symbol
1383 (setf (getf (getf initargs :plist) :pv-table-symbol)
1385 (when (and (eq *boot-state* 'complete)
1387 (let* ((gf (fdefinition gf-spec))
1388 (method (and (generic-function-p gf)
1389 (generic-function-methods gf)
1392 (parse-specializers specializers)
1395 (style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1396 gf-spec qualifiers specializers))))
1397 (let ((method (apply #'add-named-method
1398 gf-spec qualifiers specializers lambda-list
1399 :definition-source `((defmethod ,gf-spec
1404 (unless (or (eq method-class 'standard-method)
1405 (eq (find-class method-class nil) (class-of method)))
1406 ;; FIXME: should be STYLE-WARNING?
1407 (format *error-output*
1408 "~&At the time the method with qualifiers ~:S and~%~
1409 specializers ~:S on the generic function ~S~%~
1410 was compiled, the method-class for that generic function was~%~
1411 ~S. But, the method class is now ~S, this~%~
1412 may mean that this method was compiled improperly.~%"
1413 qualifiers specializers gf-spec
1414 method-class (class-name (class-of method))))
1417 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1418 `(slow-method ,gf-spec ,@qualifiers ,unparsed-specializers))
1420 (defun initialize-method-function (initargs &optional return-function-p method)
1421 (let* ((mf (getf initargs :function))
1422 (method-spec (getf initargs :method-spec))
1423 (plist (getf initargs :plist))
1424 (pv-table-symbol (getf plist :pv-table-symbol))
1426 (mff (getf initargs :fast-function)))
1427 (flet ((set-mf-property (p v)
1429 (setf (method-function-get mf p) v))
1431 (setf (method-function-get mff p) v))))
1434 (setq mf (set-fun-name mf method-spec)))
1436 (let ((name `(fast-method ,@(cdr method-spec))))
1437 (set-fun-name mff name)
1439 (set-mf-property :name name)))))
1441 (let ((snl (getf plist :slot-name-lists))
1442 (cl (getf plist :call-list)))
1444 (setq pv-table (intern-pv-table :slot-name-lists snl
1446 (when pv-table (set pv-table-symbol pv-table))
1447 (set-mf-property :pv-table pv-table)))
1448 (loop (when (null plist) (return nil))
1449 (set-mf-property (pop plist) (pop plist)))
1451 (set-mf-property :method method))
1452 (when return-function-p
1453 (or mf (method-function-from-fast-function mff)))))))
1455 (defun analyze-lambda-list (lambda-list)
1456 (flet (;; FIXME: Is this redundant with SB-C::MAKE-KEYWORD-FOR-ARG?
1457 (parse-key-arg (arg)
1459 (if (listp (car arg))
1461 (keywordicate (car arg)))
1462 (keywordicate arg))))
1468 (allow-other-keys-p nil)
1470 (keyword-parameters ())
1472 (dolist (x lambda-list)
1473 (if (memq x lambda-list-keywords)
1475 (&optional (setq state 'optional))
1478 (&allow-other-keys (setq allow-other-keys-p t))
1479 (&rest (setq restp t
1483 (error "encountered the non-standard lambda list keyword ~S"
1486 (required (incf nrequired))
1487 (optional (incf noptional))
1488 (key (push (parse-key-arg x) keywords)
1489 (push x keyword-parameters))
1490 (rest (incf nrest)))))
1491 (when (and restp (zerop nrest))
1492 (error "Error in lambda-list:~%~
1493 After &REST, a DEFGENERIC lambda-list ~
1494 must be followed by at least one variable."))
1495 (values nrequired noptional keysp restp allow-other-keys-p
1497 (reverse keyword-parameters)))))
1499 (defun keyword-spec-name (x)
1500 (let ((key (if (atom x) x (car x))))
1505 (defun ftype-declaration-from-lambda-list (lambda-list name)
1506 (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1507 keywords keyword-parameters)
1508 (analyze-lambda-list lambda-list)
1509 (declare (ignore keyword-parameters))
1510 (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1511 (old-ftype (if (fun-type-p old) old nil))
1512 (old-restp (and old-ftype (fun-type-rest old-ftype)))
1513 (old-keys (and old-ftype
1514 (mapcar #'key-info-name
1517 (old-keysp (and old-ftype (fun-type-keyp old-ftype)))
1518 (old-allowp (and old-ftype
1519 (fun-type-allowp old-ftype)))
1520 (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1521 `(function ,(append (make-list nrequired :initial-element t)
1522 (when (plusp noptional)
1523 (append '(&optional)
1524 (make-list noptional :initial-element t)))
1525 (when (or restp old-restp)
1527 (when (or keysp old-keysp)
1529 (mapcar (lambda (key)
1532 (when (or allow-other-keys-p old-allowp)
1533 '(&allow-other-keys)))))
1536 (defun defgeneric-declaration (spec lambda-list)
1537 `(ftype ,(ftype-declaration-from-lambda-list lambda-list spec) ,spec))
1539 ;;;; early generic function support
1541 (defvar *!early-generic-functions* ())
1543 (defun ensure-generic-function (fun-name
1547 (declare (ignore environment))
1548 (let ((existing (and (gboundp fun-name)
1549 (gdefinition fun-name))))
1551 (eq *boot-state* 'complete)
1552 (null (generic-function-p existing)))
1553 (generic-clobbers-function fun-name)
1554 (apply #'ensure-generic-function-using-class
1555 existing fun-name all-keys))))
1557 (defun generic-clobbers-function (fun-name)
1558 (error 'simple-program-error
1559 :format-control "~S already names an ordinary function or a macro."
1560 :format-arguments (list fun-name)))
1562 (defvar *sgf-wrapper*
1563 (boot-make-wrapper (early-class-size 'standard-generic-function)
1564 'standard-generic-function))
1566 (defvar *sgf-slots-init*
1567 (mapcar (lambda (canonical-slot)
1568 (if (memq (getf canonical-slot :name) '(arg-info source))
1570 (let ((initfunction (getf canonical-slot :initfunction)))
1572 (funcall initfunction)
1574 (early-collect-inheritance 'standard-generic-function)))
1576 (defvar *sgf-method-class-index*
1577 (!bootstrap-slot-index 'standard-generic-function 'method-class))
1579 (defun early-gf-p (x)
1580 (and (fsc-instance-p x)
1581 (eq (clos-slots-ref (get-slots x) *sgf-method-class-index*)
1584 (defvar *sgf-methods-index*
1585 (!bootstrap-slot-index 'standard-generic-function 'methods))
1587 (defmacro early-gf-methods (gf)
1588 `(clos-slots-ref (get-slots ,gf) *sgf-methods-index*))
1590 (defvar *sgf-arg-info-index*
1591 (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1593 (defmacro early-gf-arg-info (gf)
1594 `(clos-slots-ref (get-slots ,gf) *sgf-arg-info-index*))
1596 (defvar *sgf-dfun-state-index*
1597 (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1599 (defstruct (arg-info
1601 (:constructor make-arg-info ())
1603 (arg-info-lambda-list :no-lambda-list)
1606 arg-info-number-optional
1608 arg-info-keys ;nil no &KEY or &REST allowed
1609 ;(k1 k2 ..) Each method must accept these &KEY arguments.
1610 ;T must have &KEY or &REST
1612 gf-info-simple-accessor-type ; nil, reader, writer, boundp
1613 (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1615 gf-info-static-c-a-m-emf
1616 (gf-info-c-a-m-emf-std-p t)
1619 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1621 (defun arg-info-valid-p (arg-info)
1622 (not (null (arg-info-number-optional arg-info))))
1624 (defun arg-info-applyp (arg-info)
1625 (or (plusp (arg-info-number-optional arg-info))
1626 (arg-info-key/rest-p arg-info)))
1628 (defun arg-info-number-required (arg-info)
1629 (length (arg-info-metatypes arg-info)))
1631 (defun arg-info-nkeys (arg-info)
1632 (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1634 (defun create-gf-lambda-list (lambda-list)
1635 ;;; Create a gf lambda list from a method lambda list
1636 (loop for x in lambda-list
1637 collect (if (consp x) (list (car x)) x)
1638 if (eq x '&key) do (loop-finish)))
1640 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1641 argument-precedence-order)
1642 (let* ((arg-info (if (eq *boot-state* 'complete)
1644 (early-gf-arg-info gf)))
1645 (methods (if (eq *boot-state* 'complete)
1646 (generic-function-methods gf)
1647 (early-gf-methods gf)))
1648 (was-valid-p (integerp (arg-info-number-optional arg-info)))
1649 (first-p (and new-method (null (cdr methods)))))
1650 (when (and (not lambda-list-p) methods)
1651 (setq lambda-list (gf-lambda-list gf)))
1652 (when (or lambda-list-p
1654 (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1655 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1656 (analyze-lambda-list lambda-list)
1657 (when (and methods (not first-p))
1658 (let ((gf-nreq (arg-info-number-required arg-info))
1659 (gf-nopt (arg-info-number-optional arg-info))
1660 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1661 (unless (and (= nreq gf-nreq)
1663 (eq (or keysp restp) gf-key/rest-p))
1664 (error "The lambda-list ~S is incompatible with ~
1665 existing methods of ~S."
1667 (setf (arg-info-lambda-list arg-info)
1670 (create-gf-lambda-list lambda-list)))
1671 (when (or lambda-list-p argument-precedence-order
1672 (null (arg-info-precedence arg-info)))
1673 (setf (arg-info-precedence arg-info)
1674 (compute-precedence lambda-list nreq argument-precedence-order)))
1675 (setf (arg-info-metatypes arg-info) (make-list nreq))
1676 (setf (arg-info-number-optional arg-info) nopt)
1677 (setf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1678 (setf (arg-info-keys arg-info)
1680 (if allow-other-keys-p t keywords)
1681 (arg-info-key/rest-p arg-info)))))
1683 (check-method-arg-info gf arg-info new-method))
1684 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1687 (defun check-method-arg-info (gf arg-info method)
1688 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1689 (analyze-lambda-list (if (consp method)
1690 (early-method-lambda-list method)
1691 (method-lambda-list method)))
1692 (flet ((lose (string &rest args)
1693 (error 'simple-program-error
1694 :format-control "~@<attempt to add the method~2I~_~S~I~_~
1695 to the generic function~2I~_~S;~I~_~
1697 :format-arguments (list method gf string args)))
1698 (comparison-description (x y)
1699 (if (> x y) "more" "fewer")))
1700 (let ((gf-nreq (arg-info-number-required arg-info))
1701 (gf-nopt (arg-info-number-optional arg-info))
1702 (gf-key/rest-p (arg-info-key/rest-p arg-info))
1703 (gf-keywords (arg-info-keys arg-info)))
1704 (unless (= nreq gf-nreq)
1706 "the method has ~A required arguments than the generic function."
1707 (comparison-description nreq gf-nreq)))
1708 (unless (= nopt gf-nopt)
1710 "the method has ~A optional arguments than the generic function."
1711 (comparison-description nopt gf-nopt)))
1712 (unless (eq (or keysp restp) gf-key/rest-p)
1714 "the method and generic function differ in whether they accept~_~
1715 &REST or &KEY arguments."))
1716 (when (consp gf-keywords)
1717 (unless (or (and restp (not keysp))
1719 (every (lambda (k) (memq k keywords)) gf-keywords))
1720 (lose "the method does not accept each of the &KEY arguments~2I~_~
1724 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1725 (let* ((existing-p (and methods (cdr methods) new-method))
1726 (nreq (length (arg-info-metatypes arg-info)))
1727 (metatypes (if existing-p
1728 (arg-info-metatypes arg-info)
1730 (type (if existing-p
1731 (gf-info-simple-accessor-type arg-info)
1733 (when (arg-info-valid-p arg-info)
1734 (dolist (method (if new-method (list new-method) methods))
1735 (let* ((specializers (if (or (eq *boot-state* 'complete)
1736 (not (consp method)))
1737 (method-specializers method)
1738 (early-method-specializers method t)))
1739 (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1741 (early-method-class method)))
1742 (new-type (when (and class
1743 (or (not (eq *boot-state* 'complete))
1744 (eq (generic-function-method-combination gf)
1745 *standard-method-combination*)))
1746 (cond ((eq class *the-class-standard-reader-method*)
1748 ((eq class *the-class-standard-writer-method*)
1750 ((eq class *the-class-standard-boundp-method*)
1752 (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1753 (setq type (cond ((null type) new-type)
1754 ((eq type new-type) type)
1756 (setf (arg-info-metatypes arg-info) metatypes)
1757 (setf (gf-info-simple-accessor-type arg-info) type)))
1758 (when (or (not was-valid-p) first-p)
1759 (multiple-value-bind (c-a-m-emf std-p)
1762 (compute-applicable-methods-emf gf))
1763 (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1764 (setf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1765 (unless (gf-info-c-a-m-emf-std-p arg-info)
1766 (setf (gf-info-simple-accessor-type arg-info) t))))
1768 (let ((name (if (eq *boot-state* 'complete)
1769 (generic-function-name gf)
1770 (!early-gf-name gf))))
1771 (setf (gf-precompute-dfun-and-emf-p arg-info)
1775 *internal-pcl-generalized-fun-name-symbols*))
1777 (t (let* ((symbol (fun-name-block-name name))
1778 (package (symbol-package symbol)))
1779 (and (or (eq package *pcl-package*)
1780 (memq package (package-use-list *pcl-package*)))
1781 ;; FIXME: this test will eventually be
1782 ;; superseded by the *internal-pcl...* test,
1783 ;; above. While we are in a process of
1784 ;; transition, however, it should probably
1786 (not (find #\Space (symbol-name symbol))))))))))
1787 (setf (gf-info-fast-mf-p arg-info)
1788 (or (not (eq *boot-state* 'complete))
1789 (let* ((method-class (generic-function-method-class gf))
1790 (methods (compute-applicable-methods
1791 #'make-method-lambda
1792 (list gf (class-prototype method-class)
1794 (and methods (null (cdr methods))
1795 (let ((specls (method-specializers (car methods))))
1796 (and (classp (car specls))
1797 (eq 'standard-generic-function
1798 (class-name (car specls)))
1799 (classp (cadr specls))
1800 (eq 'standard-method
1801 (class-name (cadr specls)))))))))
1804 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
1806 ;;; The STATIC-SLOTS field of the funcallable instances used as early
1807 ;;; generic functions is used to store the early methods and early
1808 ;;; discriminator code for the early generic function. The static
1809 ;;; slots field of the fins contains a list whose:
1810 ;;; CAR - a list of the early methods on this early gf
1811 ;;; CADR - the early discriminator code for this method
1812 (defun ensure-generic-function-using-class (existing spec &rest keys
1813 &key (lambda-list nil
1815 argument-precedence-order
1817 (declare (ignore keys))
1818 (cond ((and existing (early-gf-p existing))
1820 (set-arg-info existing :lambda-list lambda-list))
1822 ((assoc spec *!generic-function-fixups* :test #'equal)
1824 (make-early-gf spec lambda-list lambda-list-p existing
1825 argument-precedence-order)
1826 (error "The function ~S is not already defined." spec)))
1828 (error "~S should be on the list ~S."
1830 '*!generic-function-fixups*))
1832 (pushnew spec *!early-generic-functions* :test #'equal)
1833 (make-early-gf spec lambda-list lambda-list-p nil
1834 argument-precedence-order))))
1836 (defun make-early-gf (spec &optional lambda-list lambda-list-p
1837 function argument-precedence-order)
1838 (let ((fin (allocate-funcallable-instance *sgf-wrapper* *sgf-slots-init*)))
1839 (set-funcallable-instance-function
1842 (if (eq spec 'print-object)
1843 #'(instance-lambda (instance stream)
1844 (print-unreadable-object (instance stream :identity t)
1845 (format stream "std-instance")))
1846 #'(instance-lambda (&rest args)
1847 (declare (ignore args))
1848 (error "The function of the funcallable-instance ~S~
1849 has not been set." fin)))))
1850 (setf (gdefinition spec) fin)
1851 (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
1852 (!bootstrap-set-slot 'standard-generic-function
1856 (set-fun-name fin spec)
1857 (let ((arg-info (make-arg-info)))
1858 (setf (early-gf-arg-info fin) arg-info)
1860 (proclaim (defgeneric-declaration spec lambda-list))
1861 (if argument-precedence-order
1863 :lambda-list lambda-list
1864 :argument-precedence-order argument-precedence-order)
1865 (set-arg-info fin :lambda-list lambda-list))))
1868 (defun set-dfun (gf &optional dfun cache info)
1870 (setf (cache-owner cache) gf))
1871 (let ((new-state (if (and dfun (or cache info))
1872 (list* dfun cache info)
1874 (if (eq *boot-state* 'complete)
1875 (setf (gf-dfun-state gf) new-state)
1876 (setf (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*)
1880 (defun gf-dfun-cache (gf)
1881 (let ((state (if (eq *boot-state* 'complete)
1883 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1886 (cons (cadr state)))))
1888 (defun gf-dfun-info (gf)
1889 (let ((state (if (eq *boot-state* 'complete)
1891 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1894 (cons (cddr state)))))
1896 (defvar *sgf-name-index*
1897 (!bootstrap-slot-index 'standard-generic-function 'name))
1899 (defun !early-gf-name (gf)
1900 (clos-slots-ref (get-slots gf) *sgf-name-index*))
1902 (defun gf-lambda-list (gf)
1903 (let ((arg-info (if (eq *boot-state* 'complete)
1905 (early-gf-arg-info gf))))
1906 (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
1907 (let ((methods (if (eq *boot-state* 'complete)
1908 (generic-function-methods gf)
1909 (early-gf-methods gf))))
1912 (warn "no way to determine the lambda list for ~S" gf)
1914 (let* ((method (car (last methods)))
1915 (ll (if (consp method)
1916 (early-method-lambda-list method)
1917 (method-lambda-list method))))
1918 (create-gf-lambda-list ll))))
1919 (arg-info-lambda-list arg-info))))
1921 (defmacro real-ensure-gf-internal (gf-class all-keys env)
1923 (cond ((symbolp ,gf-class)
1924 (setq ,gf-class (find-class ,gf-class t ,env)))
1925 ((classp ,gf-class))
1927 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
1928 class nor a symbol that names a class."
1930 (remf ,all-keys :generic-function-class)
1931 (remf ,all-keys :environment)
1932 (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
1933 (unless (eq combin '.shes-not-there.)
1934 (setf (getf ,all-keys :method-combination)
1935 (find-method-combination (class-prototype ,gf-class)
1938 (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
1939 (unless (eq method-class '.shes-not-there.)
1940 (setf (getf ,all-keys :method-class)
1941 (find-class method-class t ,env))))))
1943 (defun real-ensure-gf-using-class--generic-function
1947 &key environment (lambda-list nil lambda-list-p)
1948 (generic-function-class 'standard-generic-function gf-class-p)
1950 (real-ensure-gf-internal generic-function-class all-keys environment)
1951 (unless (or (null gf-class-p)
1952 (eq (class-of existing) generic-function-class))
1953 (change-class existing generic-function-class))
1955 (apply #'reinitialize-instance existing all-keys)
1957 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1959 (defun real-ensure-gf-using-class--null
1963 &key environment (lambda-list nil lambda-list-p)
1964 (generic-function-class 'standard-generic-function)
1966 (declare (ignore existing))
1967 (real-ensure-gf-internal generic-function-class all-keys environment)
1969 (setf (gdefinition fun-name)
1970 (apply #'make-instance generic-function-class
1971 :name fun-name all-keys))
1973 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1975 (defun get-generic-fun-info (gf)
1976 ;; values nreq applyp metatypes nkeys arg-info
1977 (multiple-value-bind (applyp metatypes arg-info)
1978 (let* ((arg-info (if (early-gf-p gf)
1979 (early-gf-arg-info gf)
1981 (metatypes (arg-info-metatypes arg-info)))
1982 (values (arg-info-applyp arg-info)
1985 (values (length metatypes) applyp metatypes
1986 (count-if (lambda (x) (neq x t)) metatypes)
1989 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
1990 &optional slot-name)
1991 (initialize-method-function initargs)
1994 ;; Figure out whether we got class objects or class names as the
1995 ;; specializers and set parsed and unparsed appropriately. If we
1996 ;; got class objects, then we can compute unparsed, but if we got
1997 ;; class names we don't try to compute parsed.
1999 ;; Note that the use of not symbolp in this call to every should be
2000 ;; read as 'classp' we can't use classp itself because it doesn't
2002 (if (every (lambda (s) (not (symbolp s))) specializers)
2003 (setq parsed specializers
2004 unparsed (mapcar (lambda (s)
2005 (if (eq s t) t (class-name s)))
2007 (setq unparsed specializers
2009 (list :early-method ;This is an early method dammit!
2011 (getf initargs :function)
2012 (getf initargs :fast-function)
2014 parsed ;The parsed specializers. This is used
2015 ;by early-method-specializers to cache
2016 ;the parse. Note that this only comes
2017 ;into play when there is more than one
2018 ;early method on an early gf.
2020 (list class ;A list to which real-make-a-method
2021 qualifiers ;can be applied to make a real method
2022 arglist ;corresponding to this early one.
2028 (defun real-make-a-method
2029 (class qualifiers lambda-list specializers initargs doc
2030 &optional slot-name)
2031 (setq specializers (parse-specializers specializers))
2032 (apply #'make-instance class
2033 :qualifiers qualifiers
2034 :lambda-list lambda-list
2035 :specializers specializers
2037 :slot-name slot-name
2041 (defun early-method-function (early-method)
2042 (values (cadr early-method) (caddr early-method)))
2044 (defun early-method-class (early-method)
2045 (find-class (car (fifth early-method))))
2047 (defun early-method-standard-accessor-p (early-method)
2048 (let ((class (first (fifth early-method))))
2049 (or (eq class 'standard-reader-method)
2050 (eq class 'standard-writer-method)
2051 (eq class 'standard-boundp-method))))
2053 (defun early-method-standard-accessor-slot-name (early-method)
2054 (seventh (fifth early-method)))
2056 ;;; Fetch the specializers of an early method. This is basically just
2057 ;;; a simple accessor except that when the second argument is t, this
2058 ;;; converts the specializers from symbols into class objects. The
2059 ;;; class objects are cached in the early method, this makes
2060 ;;; bootstrapping faster because the class objects only have to be
2064 ;;; The second argument should only be passed as T by
2065 ;;; early-lookup-method. This is to implement the rule that only when
2066 ;;; there is more than one early method on a generic function is the
2067 ;;; conversion from class names to class objects done. This
2068 ;;; corresponds to the fact that we are only allowed to have one
2069 ;;; method on any generic function up until the time classes exist.
2070 (defun early-method-specializers (early-method &optional objectsp)
2071 (if (and (listp early-method)
2072 (eq (car early-method) :early-method))
2073 (cond ((eq objectsp t)
2074 (or (fourth early-method)
2075 (setf (fourth early-method)
2076 (mapcar #'find-class (cadddr (fifth early-method))))))
2078 (cadddr (fifth early-method))))
2079 (error "~S is not an early-method." early-method)))
2081 (defun early-method-qualifiers (early-method)
2082 (cadr (fifth early-method)))
2084 (defun early-method-lambda-list (early-method)
2085 (caddr (fifth early-method)))
2087 (defun early-add-named-method (generic-function-name
2092 (let* ((gf (ensure-generic-function generic-function-name))
2094 (dolist (m (early-gf-methods gf))
2095 (when (and (equal (early-method-specializers m) specializers)
2096 (equal (early-method-qualifiers m) qualifiers))
2098 (new (make-a-method 'standard-method
2104 (when existing (remove-method gf existing))
2105 (add-method gf new)))
2107 ;;; This is the early version of ADD-METHOD. Later this will become a
2108 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2109 ;;; special knowledge about ADD-METHOD.
2110 (defun add-method (generic-function method)
2111 (when (not (fsc-instance-p generic-function))
2112 (error "Early ADD-METHOD didn't get a funcallable instance."))
2113 (when (not (and (listp method) (eq (car method) :early-method)))
2114 (error "Early ADD-METHOD didn't get an early method."))
2115 (push method (early-gf-methods generic-function))
2116 (set-arg-info generic-function :new-method method)
2117 (unless (assoc (!early-gf-name generic-function)
2118 *!generic-function-fixups*
2120 (update-dfun generic-function)))
2122 ;;; This is the early version of REMOVE-METHOD. See comments on
2123 ;;; the early version of ADD-METHOD.
2124 (defun remove-method (generic-function method)
2125 (when (not (fsc-instance-p generic-function))
2126 (error "An early remove-method didn't get a funcallable instance."))
2127 (when (not (and (listp method) (eq (car method) :early-method)))
2128 (error "An early remove-method didn't get an early method."))
2129 (setf (early-gf-methods generic-function)
2130 (remove method (early-gf-methods generic-function)))
2131 (set-arg-info generic-function)
2132 (unless (assoc (!early-gf-name generic-function)
2133 *!generic-function-fixups*
2135 (update-dfun generic-function)))
2137 ;;; This is the early version of GET-METHOD. See comments on the early
2138 ;;; version of ADD-METHOD.
2139 (defun get-method (generic-function qualifiers specializers
2140 &optional (errorp t))
2141 (if (early-gf-p generic-function)
2142 (or (dolist (m (early-gf-methods generic-function))
2143 (when (and (or (equal (early-method-specializers m nil)
2145 (equal (early-method-specializers m t)
2147 (equal (early-method-qualifiers m) qualifiers))
2150 (error "can't get early method")
2152 (real-get-method generic-function qualifiers specializers errorp)))
2154 (defun !fix-early-generic-functions ()
2155 (let ((accessors nil))
2156 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2157 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2158 (dolist (early-gf-spec *!early-generic-functions*)
2159 (when (every #'early-method-standard-accessor-p
2160 (early-gf-methods (gdefinition early-gf-spec)))
2161 (push early-gf-spec accessors)))
2162 (dolist (spec (nconc accessors
2163 '(accessor-method-slot-name
2164 generic-function-methods
2169 slot-definition-location
2170 slot-definition-name
2173 class-precedence-list
2174 slot-boundp-using-class
2175 (setf slot-value-using-class)
2176 slot-value-using-class
2179 funcallable-standard-class-p
2182 (setq *!early-generic-functions*
2184 (delete spec *!early-generic-functions* :test #'equal))))
2186 (dolist (early-gf-spec *!early-generic-functions*)
2187 (/show early-gf-spec)
2188 (let* ((gf (gdefinition early-gf-spec))
2189 (methods (mapcar (lambda (early-method)
2190 (let ((args (copy-list (fifth
2193 (early-method-specializers
2195 (apply #'real-make-a-method args)))
2196 (early-gf-methods gf))))
2197 (setf (generic-function-method-class gf) *the-class-standard-method*)
2198 (setf (generic-function-method-combination gf)
2199 *standard-method-combination*)
2200 (set-methods gf methods)))
2202 (dolist (fn *!early-functions*)
2204 (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2206 (dolist (fixup *!generic-function-fixups*)
2208 (let* ((fspec (car fixup))
2209 (gf (gdefinition fspec))
2210 (methods (mapcar (lambda (method)
2211 (let* ((lambda-list (first method))
2212 (specializers (second method))
2213 (method-fn-name (third method))
2214 (fn-name (or method-fn-name fspec))
2215 (fn (fdefinition fn-name))
2219 (lambda (args next-methods)
2223 `(call ,fn-name)))))
2224 (declare (type function fn))
2225 (make-a-method 'standard-method
2232 (setf (generic-function-method-class gf) *the-class-standard-method*)
2233 (setf (generic-function-method-combination gf)
2234 *standard-method-combination*)
2235 (set-methods gf methods))))
2236 (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2238 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2239 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2240 ;;; is really implemented.
2241 (defun parse-defmethod (cdr-of-form)
2242 (declare (list cdr-of-form))
2243 (let ((name (pop cdr-of-form))
2246 (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2247 (push (pop cdr-of-form) qualifiers)
2248 (return (setq qualifiers (nreverse qualifiers)))))
2249 (setq spec-ll (pop cdr-of-form))
2250 (values name qualifiers spec-ll cdr-of-form)))
2252 (defun parse-specializers (specializers)
2253 (declare (list specializers))
2254 (flet ((parse (spec)
2255 (let ((result (specializer-from-type spec)))
2256 (if (specializerp result)
2259 (error "~S was used as a specializer,~%~
2260 but is not the name of a class."
2262 (error "~S is not a legal specializer." spec))))))
2263 (mapcar #'parse specializers)))
2265 (defun unparse-specializers (specializers-or-method)
2266 (if (listp specializers-or-method)
2267 (flet ((unparse (spec)
2268 (if (specializerp spec)
2269 (let ((type (specializer-type spec)))
2270 (if (and (consp type)
2271 (eq (car type) 'class))
2272 (let* ((class (cadr type))
2273 (class-name (class-name class)))
2274 (if (eq class (find-class class-name nil))
2278 (error "~S is not a legal specializer." spec))))
2279 (mapcar #'unparse specializers-or-method))
2280 (unparse-specializers (method-specializers specializers-or-method))))
2282 (defun parse-method-or-spec (spec &optional (errorp t))
2283 (let (gf method name temp)
2286 gf (method-generic-function method)
2287 temp (and gf (generic-function-name gf))
2289 (make-method-spec temp
2290 (method-qualifiers method)
2291 (unparse-specializers
2292 (method-specializers method)))
2293 (make-symbol (format nil "~S" method))))
2294 (multiple-value-bind (gf-spec quals specls)
2295 (parse-defmethod spec)
2296 (and (setq gf (and (or errorp (gboundp gf-spec))
2297 (gdefinition gf-spec)))
2298 (let ((nreq (compute-discriminating-function-arglist-info gf)))
2299 (setq specls (append (parse-specializers specls)
2300 (make-list (- nreq (length specls))
2304 (setq method (get-method gf quals specls errorp))
2307 gf-spec quals (unparse-specializers specls))))))))
2308 (values gf method name)))
2310 (defun extract-parameters (specialized-lambda-list)
2311 (multiple-value-bind (parameters ignore1 ignore2)
2312 (parse-specialized-lambda-list specialized-lambda-list)
2313 (declare (ignore ignore1 ignore2))
2316 (defun extract-lambda-list (specialized-lambda-list)
2317 (multiple-value-bind (ignore1 lambda-list ignore2)
2318 (parse-specialized-lambda-list specialized-lambda-list)
2319 (declare (ignore ignore1 ignore2))
2322 (defun extract-specializer-names (specialized-lambda-list)
2323 (multiple-value-bind (ignore1 ignore2 specializers)
2324 (parse-specialized-lambda-list specialized-lambda-list)
2325 (declare (ignore ignore1 ignore2))
2328 (defun extract-required-parameters (specialized-lambda-list)
2329 (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2330 (parse-specialized-lambda-list specialized-lambda-list)
2331 (declare (ignore ignore1 ignore2 ignore3))
2332 required-parameters))
2334 (define-condition specialized-lambda-list-error
2335 (reference-condition simple-program-error)
2337 (:default-initargs :references (list '(:ansi-cl :section (3 4 3)))))
2339 (defun parse-specialized-lambda-list
2341 &optional supplied-keywords (allowed-keywords '(&optional &rest &key &aux))
2342 &aux (specialized-lambda-list-keywords
2343 '(&optional &rest &key &allow-other-keys &aux)))
2344 (let ((arg (car arglist)))
2345 (cond ((null arglist) (values nil nil nil nil))
2347 (values nil arglist nil nil))
2348 ((memq arg lambda-list-keywords)
2349 ;; non-standard lambda-list-keywords are errors.
2350 (unless (memq arg specialized-lambda-list-keywords)
2351 (error 'specialized-lambda-list-error
2352 :format-control "unknown specialized-lambda-list ~
2354 :format-arguments (list arg)))
2355 ;; no multiple &rest x &rest bla specifying
2356 (when (memq arg supplied-keywords)
2357 (error 'specialized-lambda-list-error
2358 :format-control "multiple occurrence of ~
2359 specialized-lambda-list keyword ~S~%"
2360 :format-arguments (list arg)))
2361 ;; And no placing &key in front of &optional, either.
2362 (unless (memq arg allowed-keywords)
2363 (error 'specialized-lambda-list-error
2364 :format-control "misplaced specialized-lambda-list ~
2366 :format-arguments (list arg)))
2367 ;; When we are at a lambda-list keyword, the parameters
2368 ;; don't include the lambda-list keyword; the lambda-list
2369 ;; does include the lambda-list keyword; and no
2370 ;; specializers are allowed to follow the lambda-list
2371 ;; keywords (at least for now).
2372 (multiple-value-bind (parameters lambda-list)
2373 (parse-specialized-lambda-list (cdr arglist)
2374 (cons arg supplied-keywords)
2376 (cons '&allow-other-keys
2377 (cdr (member arg allowed-keywords)))
2378 (cdr (member arg allowed-keywords))))
2379 (when (and (eq arg '&rest)
2380 (or (null lambda-list)
2381 (memq (car lambda-list)
2382 specialized-lambda-list-keywords)
2383 (not (or (null (cadr lambda-list))
2384 (memq (cadr lambda-list)
2385 specialized-lambda-list-keywords)))))
2386 (error 'specialized-lambda-list-error
2388 "in a specialized-lambda-list, excactly one ~
2389 variable must follow &REST.~%"
2390 :format-arguments nil))
2392 (cons arg lambda-list)
2396 ;; After a lambda-list keyword there can be no specializers.
2397 (multiple-value-bind (parameters lambda-list)
2398 (parse-specialized-lambda-list (cdr arglist)
2401 (values (cons (if (listp arg) (car arg) arg) parameters)
2402 (cons arg lambda-list)
2406 (multiple-value-bind (parameters lambda-list specializers required)
2407 (parse-specialized-lambda-list (cdr arglist))
2408 (values (cons (if (listp arg) (car arg) arg) parameters)
2409 (cons (if (listp arg) (car arg) arg) lambda-list)
2410 (cons (if (listp arg) (cadr arg) t) specializers)
2411 (cons (if (listp arg) (car arg) arg) required)))))))
2413 (setq *boot-state* 'early)
2415 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2416 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2417 ;;; walker stuff was only used for implementing stuff like that; maybe
2418 ;;; it's not needed any more? Hunt down what it was used for and see.
2420 (defmacro with-slots (slots instance &body body)
2421 (let ((in (gensym)))
2422 `(let ((,in ,instance))
2423 (declare (ignorable ,in))
2424 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2427 (and (symbolp instance)
2428 `((declare (%variable-rebinding ,in ,instance)))))
2430 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2432 (if (symbolp slot-entry)
2436 (if (symbolp slot-entry)
2438 (cadr slot-entry))))
2440 (slot-value ,in ',slot-name))))
2444 (defmacro with-accessors (slots instance &body body)
2445 (let ((in (gensym)))
2446 `(let ((,in ,instance))
2447 (declare (ignorable ,in))
2448 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2451 (and (symbolp instance)
2452 `((declare (%variable-rebinding ,in ,instance)))))
2454 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2455 (let ((var-name (car slot-entry))
2456 (accessor-name (cadr slot-entry)))
2457 `(,var-name (,accessor-name ,in))))