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 (check-cnm-args-body (&environment env method-name-declaration cnm-args)
822 (if (safe-code-p env)
823 `(%check-cnm-args ,cnm-args ,',method-args ',method-name-declaration)
825 (call-next-method-body (method-name-declaration cnm-args)
827 (funcall (if (std-instance-p .next-method.)
828 (method-function .next-method.)
829 .next-method.) ; for early methods
830 (or ,cnm-args ,',method-args)
832 (apply #'call-no-next-method ',method-name-declaration
833 (or ,cnm-args ,',method-args))))
834 (next-method-p-body ()
835 `(not (null .next-method.)))
836 (with-rebound-original-args ((call-next-method-p setq-p)
838 (declare (ignore call-next-method-p setq-p))
842 (defun call-no-next-method (method-name-declaration &rest args)
843 (destructuring-bind (name) method-name-declaration
844 (destructuring-bind (name &rest qualifiers-and-specializers) name
845 ;; KLUDGE: inefficient traversal, but hey. This should only
846 ;; happen on the slow error path anyway.
847 (let* ((qualifiers (butlast qualifiers-and-specializers))
848 (specializers (car (last qualifiers-and-specializers)))
849 (method (find-method (gdefinition name) qualifiers specializers)))
850 (apply #'no-next-method
851 (method-generic-function method)
855 (defstruct (method-call (:copier nil))
856 (function #'identity :type function)
859 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
861 (defmacro invoke-method-call1 (function args cm-args)
862 `(let ((.function. ,function)
864 (.cm-args. ,cm-args))
865 (if (and .cm-args. (null (cdr .cm-args.)))
866 (funcall .function. .args. (car .cm-args.))
867 (apply .function. .args. .cm-args.))))
869 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
870 `(invoke-method-call1 (method-call-function ,method-call)
872 `(list* ,@required-args+rest-arg)
873 `(list ,@required-args+rest-arg))
874 (method-call-call-method-args ,method-call)))
876 (defstruct (fast-method-call (:copier nil))
877 (function #'identity :type function)
882 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
884 (defmacro fmc-funcall (fn pv-cell next-method-call &rest args)
885 `(funcall ,fn ,pv-cell ,next-method-call ,@args))
887 (defmacro invoke-fast-method-call (method-call &rest required-args+rest-arg)
888 `(fmc-funcall (fast-method-call-function ,method-call)
889 (fast-method-call-pv-cell ,method-call)
890 (fast-method-call-next-method-call ,method-call)
891 ,@required-args+rest-arg))
893 (defstruct (fast-instance-boundp (:copier nil))
894 (index 0 :type fixnum))
896 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
898 (eval-when (:compile-toplevel :load-toplevel :execute)
899 (defvar *allow-emf-call-tracing-p* nil)
900 (defvar *enable-emf-call-tracing-p* #-sb-show nil #+sb-show t))
902 ;;;; effective method functions
904 (defvar *emf-call-trace-size* 200)
905 (defvar *emf-call-trace* nil)
906 (defvar *emf-call-trace-index* 0)
908 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
909 ;;; without explanation. It appears to be intended for debugging, so
910 ;;; it might be useful someday, so I haven't deleted it.
911 ;;; But it isn't documented and isn't used for anything now, so
912 ;;; I've conditionalized it out of the base system. -- WHN 19991213
914 (defun show-emf-call-trace ()
915 (when *emf-call-trace*
916 (let ((j *emf-call-trace-index*)
917 (*enable-emf-call-tracing-p* nil))
918 (format t "~&(The oldest entries are printed first)~%")
919 (dotimes-fixnum (i *emf-call-trace-size*)
920 (let ((ct (aref *emf-call-trace* j)))
921 (when ct (print ct)))
923 (when (= j *emf-call-trace-size*)
926 (defun trace-emf-call-internal (emf format args)
927 (unless *emf-call-trace*
928 (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
929 (setf (aref *emf-call-trace* *emf-call-trace-index*)
930 (list* emf format args))
931 (incf *emf-call-trace-index*)
932 (when (= *emf-call-trace-index* *emf-call-trace-size*)
933 (setq *emf-call-trace-index* 0)))
935 (defmacro trace-emf-call (emf format args)
936 (when *allow-emf-call-tracing-p*
937 `(when *enable-emf-call-tracing-p*
938 (trace-emf-call-internal ,emf ,format ,args))))
940 (defmacro invoke-effective-method-function-fast
941 (emf restp &rest required-args+rest-arg)
943 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
944 (invoke-fast-method-call ,emf ,@required-args+rest-arg)))
946 (defmacro invoke-effective-method-function (emf restp
947 &rest required-args+rest-arg)
948 (unless (constantp restp)
949 (error "The RESTP argument is not constant."))
950 ;; FIXME: The RESTP handling here is confusing and maybe slightly
951 ;; broken if RESTP evaluates to a non-self-evaluating form. E.g. if
952 ;; (INVOKE-EFFECTIVE-METHOD-FUNCTION EMF '(ERROR "gotcha") ...)
953 ;; then TRACE-EMF-CALL-CALL-INTERNAL might die on a gotcha error.
954 (setq restp (eval restp))
956 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
957 (cond ((typep ,emf 'fast-method-call)
958 (invoke-fast-method-call ,emf ,@required-args+rest-arg))
959 ;; "What," you may wonder, "do these next two clauses do?"
960 ;; In that case, you are not a PCL implementor, for they
961 ;; considered this to be self-documenting.:-| Or CSR, for
962 ;; that matter, since he can also figure it out by looking
963 ;; at it without breaking stride. For the rest of us,
964 ;; though: From what the code is doing with .SLOTS. and
965 ;; whatnot, evidently it's implementing SLOT-VALUEish and
966 ;; GET-SLOT-VALUEish things. Then we can reason backwards
967 ;; and conclude that setting EMF to a FIXNUM is an
968 ;; optimized way to represent these slot access operations.
969 ,@(when (and (null restp) (= 1 (length required-args+rest-arg)))
970 `(((typep ,emf 'fixnum)
971 (let* ((.slots. (get-slots-or-nil
972 ,(car required-args+rest-arg)))
973 (value (when .slots. (clos-slots-ref .slots. ,emf))))
974 (if (eq value +slot-unbound+)
975 (slot-unbound-internal ,(car required-args+rest-arg)
978 ,@(when (and (null restp) (= 2 (length required-args+rest-arg)))
979 `(((typep ,emf 'fixnum)
980 (let ((.new-value. ,(car required-args+rest-arg))
981 (.slots. (get-slots-or-nil
982 ,(cadr required-args+rest-arg))))
984 (setf (clos-slots-ref .slots. ,emf) .new-value.))))))
985 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
986 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
987 ;; there was no explanation and presumably the code is 10+
988 ;; years stale, I simply deleted it. -- WHN)
992 (invoke-method-call ,emf ,restp ,@required-args+rest-arg))
995 `(apply (the function ,emf) ,@required-args+rest-arg)
996 `(funcall (the function ,emf)
997 ,@required-args+rest-arg))))))))
999 (defun invoke-emf (emf args)
1000 (trace-emf-call emf t args)
1003 (let* ((arg-info (fast-method-call-arg-info emf))
1004 (restp (cdr arg-info))
1005 (nreq (car arg-info)))
1007 (let* ((rest-args (nthcdr nreq args))
1008 (req-args (ldiff args rest-args)))
1009 (apply (fast-method-call-function emf)
1010 (fast-method-call-pv-cell emf)
1011 (fast-method-call-next-method-call emf)
1012 (nconc req-args (list rest-args))))
1015 (invoke-fast-method-call emf)
1016 (error 'simple-program-error
1017 :format-control "invalid number of arguments: 0"
1018 :format-arguments nil)))
1021 (invoke-fast-method-call emf (car args))
1022 (error 'simple-program-error
1023 :format-control "invalid number of arguments: 1"
1024 :format-arguments nil)))
1027 (invoke-fast-method-call emf (car args) (cadr args))
1028 (error 'simple-program-error
1029 :format-control "invalid number of arguments: 2"
1030 :format-arguments nil)))
1032 (apply (fast-method-call-function emf)
1033 (fast-method-call-pv-cell emf)
1034 (fast-method-call-next-method-call emf)
1037 (apply (method-call-function emf)
1039 (method-call-call-method-args emf)))
1042 (error 'simple-program-error
1043 :format-control "invalid number of arguments: 0"
1044 :format-arguments nil))
1046 (let* ((slots (get-slots (car args)))
1047 (value (clos-slots-ref slots emf)))
1048 (if (eq value +slot-unbound+)
1049 (slot-unbound-internal (car args) emf)
1052 (setf (clos-slots-ref (get-slots (cadr args)) emf)
1054 (t (error 'simple-program-error
1055 :format-control "invalid number of arguments"
1056 :format-arguments nil))))
1057 (fast-instance-boundp
1058 (if (or (null args) (cdr args))
1059 (error 'simple-program-error
1060 :format-control "invalid number of arguments"
1061 :format-arguments nil)
1062 (let ((slots (get-slots (car args))))
1063 (not (eq (clos-slots-ref slots (fast-instance-boundp-index emf))
1068 (defmacro bind-fast-lexical-method-macros ((args rest-arg next-method-call)
1071 (let* ((all-params (append args (when rest-arg (list rest-arg))))
1072 (rebindings (mapcar (lambda (x) (list x x)) all-params)))
1073 `(macrolet ((narrowed-emf (emf)
1074 ;; INVOKE-EFFECTIVE-METHOD-FUNCTION has code in it to
1075 ;; dispatch on the possibility that EMF might be of
1076 ;; type FIXNUM (as an optimized representation of a
1077 ;; slot accessor). But as far as I (WHN 2002-06-11)
1078 ;; can tell, it's impossible for such a representation
1079 ;; to end up as .NEXT-METHOD-CALL. By reassuring
1080 ;; INVOKE-E-M-F that when called from this context
1081 ;; it needn't worry about the FIXNUM case, we can
1082 ;; keep those cases from being compiled, which is
1083 ;; good both because it saves bytes and because it
1084 ;; avoids annoying type mismatch compiler warnings.
1086 ;; KLUDGE: In sbcl-0.7.4.29, the compiler's type
1087 ;; system isn't smart enough about NOT and
1088 ;; intersection types to benefit from a (NOT FIXNUM)
1089 ;; declaration here. -- WHN 2002-06-12 (FIXME: maybe
1090 ;; it is now... -- CSR, 2003-06-07)
1092 ;; FIXME: Might the FUNCTION type be omittable here,
1093 ;; leaving only METHOD-CALLs? Failing that, could this
1094 ;; be documented somehow? (It'd be nice if the types
1095 ;; involved could be understood without solving the
1096 ;; halting problem.)
1097 `(the (or function method-call fast-method-call)
1099 (call-next-method-bind (&body body)
1101 (check-cnm-args-body (&environment env method-name-declaration cnm-args)
1102 (if (safe-code-p env)
1103 `(%check-cnm-args ,cnm-args (list ,@',args)
1104 ',method-name-declaration)
1106 (call-next-method-body (method-name-declaration cnm-args)
1107 `(if ,',next-method-call
1109 ;; This declaration suppresses a "deleting
1110 ;; unreachable code" note for the following IF
1111 ;; when REST-ARG is NIL. It is not nice for
1112 ;; debugging SBCL itself, but at least it
1113 ;; keeps us from annoying users.
1114 (declare (optimize (inhibit-warnings 3)))
1115 (if (and (null ',rest-arg)
1117 (eq (car cnm-args) 'list))
1118 `(invoke-effective-method-function
1119 (narrowed-emf ,',next-method-call)
1122 (let ((call `(invoke-effective-method-function
1123 (narrowed-emf ,',next-method-call)
1124 ,',(not (null rest-arg))
1126 ,@',(when rest-arg `(,rest-arg)))))
1128 (bind-args ((,@',args
1130 `(&rest ,rest-arg)))
1135 ;; As above, this declaration suppresses code
1137 (declare (optimize (inhibit-warnings 3)))
1138 (if (and (null ',rest-arg)
1140 (eq (car cnm-args) 'list))
1141 `(call-no-next-method ',method-name-declaration
1143 `(call-no-next-method ',method-name-declaration
1147 (next-method-p-body ()
1148 `(not (null ,',next-method-call)))
1149 (with-rebound-original-args ((cnm-p setq-p) &body body)
1150 (if (or cnm-p setq-p)
1152 (declare (ignorable ,@',all-params))
1157 (defmacro bind-lexical-method-functions
1158 ((&key call-next-method-p next-method-p-p setq-p
1159 closurep applyp method-name-declaration)
1161 (cond ((and (null call-next-method-p) (null next-method-p-p)
1162 (null closurep) (null applyp) (null setq-p))
1165 `(call-next-method-bind
1166 (flet (,@(and call-next-method-p
1167 `((call-next-method (&rest cnm-args)
1168 (check-cnm-args-body ,method-name-declaration cnm-args)
1169 (call-next-method-body ,method-name-declaration cnm-args))))
1170 ,@(and next-method-p-p
1172 (next-method-p-body)))))
1173 (with-rebound-original-args (,call-next-method-p ,setq-p)
1176 ;;; CMUCL comment (Gerd Moellmann):
1178 ;;; The standard says it's an error if CALL-NEXT-METHOD is called with
1179 ;;; arguments, and the set of methods applicable to those arguments is
1180 ;;; different from the set of methods applicable to the original
1181 ;;; method arguments. (According to Barry Margolin, this rule was
1182 ;;; probably added to ensure that before and around methods are always
1183 ;;; run before primary methods.)
1185 ;;; This could be optimized for the case that the generic function
1186 ;;; doesn't have hairy methods, does have standard method combination,
1187 ;;; is a standard generic function, there are no methods defined on it
1188 ;;; for COMPUTE-APPLICABLE-METHODS and probably a lot more of such
1189 ;;; preconditions. That looks hairy and is probably not worth it,
1190 ;;; because this check will never be fast.
1191 (defun %check-cnm-args (cnm-args orig-args method-name-declaration)
1193 (let* ((gf (fdefinition (caar method-name-declaration)))
1194 (omethods (compute-applicable-methods gf orig-args))
1195 (nmethods (compute-applicable-methods gf cnm-args)))
1196 (unless (equal omethods nmethods)
1197 (error "~@<The set of methods ~S applicable to argument~P ~
1198 ~{~S~^, ~} to call-next-method is different from ~
1199 the set of methods ~S applicable to the original ~
1200 method argument~P ~{~S~^, ~}.~@:>"
1201 nmethods (length cnm-args) cnm-args omethods
1202 (length orig-args) orig-args)))))
1204 (defmacro bind-args ((lambda-list args) &body body)
1205 (let ((args-tail '.args-tail.)
1208 (flet ((process-var (var)
1209 (if (memq var lambda-list-keywords)
1212 (&optional (setq state 'optional))
1213 (&key (setq state 'key))
1215 (&rest (setq state 'rest))
1216 (&aux (setq state 'aux))
1219 "encountered the non-standard lambda list keyword ~S"
1223 (required `((,var (pop ,args-tail))))
1224 (optional (cond ((not (consp var))
1225 `((,var (when ,args-tail
1226 (pop ,args-tail)))))
1228 `((,(car var) (if ,args-tail
1232 `((,(caddr var) ,args-tail)
1233 (,(car var) (if ,args-tail
1236 (rest `((,var ,args-tail)))
1237 (key (cond ((not (consp var))
1239 (get-key-arg-tail ,(keywordicate var)
1242 (multiple-value-bind (keyword variable)
1243 (if (consp (car var))
1246 (values (keywordicate (car var))
1248 `((,key (get-key-arg-tail ',keyword
1254 (multiple-value-bind (keyword variable)
1255 (if (consp (car var))
1258 (values (keywordicate (car var))
1260 `((,key (get-key-arg-tail ',keyword
1267 (let ((bindings (mapcan #'process-var lambda-list)))
1268 `(let* ((,args-tail ,args)
1271 ,@(when (eq state 'optional)
1272 `((unless (null ,args-tail)
1273 (error 'simple-program-error
1274 :format-control "surplus arguments: ~S"
1275 :format-arguments (list ,args-tail)))))))
1276 (declare (ignorable ,args-tail .dummy0.))
1279 (defun get-key-arg-tail (keyword list)
1280 (loop for (key . tail) on list by #'cddr
1282 ;; FIXME: Do we want to export this symbol? Or maybe use an
1283 ;; (ERROR 'SIMPLE-PROGRAM-ERROR) form?
1284 (sb-c::%odd-key-args-error)
1285 when (eq key keyword)
1288 (defun walk-method-lambda (method-lambda required-parameters env slots calls)
1289 (let ((call-next-method-p nil) ; flag indicating that CALL-NEXT-METHOD
1290 ; should be in the method definition
1291 (closurep nil) ; flag indicating that #'CALL-NEXT-METHOD
1292 ; was seen in the body of a method
1293 (next-method-p-p nil) ; flag indicating that NEXT-METHOD-P
1294 ; should be in the method definition
1296 (flet ((walk-function (form context env)
1297 (cond ((not (eq context :eval)) form)
1298 ;; FIXME: Jumping to a conclusion from the way it's used
1299 ;; above, perhaps CONTEXT should be called SITUATION
1300 ;; (after the term used in the ANSI specification of
1301 ;; EVAL-WHEN) and given modern ANSI keyword values
1302 ;; like :LOAD-TOPLEVEL.
1303 ((not (listp form)) form)
1304 ((eq (car form) 'call-next-method)
1305 (setq call-next-method-p t)
1307 ((eq (car form) 'next-method-p)
1308 (setq next-method-p-p t)
1310 ((memq (car form) '(setq multiple-value-setq))
1311 ;; FIXME: this is possibly a little strong as
1312 ;; conditions go. Ideally we would want to detect
1313 ;; which, if any, of the method parameters are
1314 ;; being set, and communicate that information to
1315 ;; e.g. SPLIT-DECLARATIONS. However, the brute
1316 ;; force method doesn't really cost much; a little
1317 ;; loss of discrimination over IGNORED variables
1318 ;; should be all. -- CSR, 2004-07-01
1321 ((and (eq (car form) 'function)
1322 (cond ((eq (cadr form) 'call-next-method)
1323 (setq call-next-method-p t)
1326 ((eq (cadr form) 'next-method-p)
1327 (setq next-method-p-p t)
1331 ((and (memq (car form)
1332 '(slot-value set-slot-value slot-boundp))
1333 (constantp (caddr form)))
1334 (let ((parameter (can-optimize-access form
1337 (let ((fun (ecase (car form)
1338 (slot-value #'optimize-slot-value)
1339 (set-slot-value #'optimize-set-slot-value)
1340 (slot-boundp #'optimize-slot-boundp))))
1341 (funcall fun slots parameter form))))
1342 ((and (eq (car form) 'apply)
1344 (eq (car (cadr form)) 'function)
1345 (generic-function-name-p (cadr (cadr form))))
1346 (optimize-generic-function-call
1347 form required-parameters env slots calls))
1348 ((generic-function-name-p (car form))
1349 (optimize-generic-function-call
1350 form required-parameters env slots calls))
1353 (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1354 (values walked-lambda
1360 (defun generic-function-name-p (name)
1361 (and (legal-fun-name-p name)
1363 (if (eq *boot-state* 'complete)
1364 (standard-generic-function-p (gdefinition name))
1365 (funcallable-instance-p (gdefinition name)))))
1367 (defvar *method-function-plist* (make-hash-table :test 'eq))
1370 (defvar *mf1cp* nil)
1373 (defvar *mf2cp* nil)
1375 (defun method-function-plist (method-function)
1376 (unless (eq method-function *mf1*)
1377 (rotatef *mf1* *mf2*)
1378 (rotatef *mf1p* *mf2p*)
1379 (rotatef *mf1cp* *mf2cp*))
1380 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1381 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1382 (unless (eq method-function *mf1*)
1383 (setf *mf1* method-function
1385 *mf1p* (gethash method-function *method-function-plist*)))
1388 (defun (setf method-function-plist)
1389 (val method-function)
1390 (unless (eq method-function *mf1*)
1391 (rotatef *mf1* *mf2*)
1392 (rotatef *mf1cp* *mf2cp*)
1393 (rotatef *mf1p* *mf2p*))
1394 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1395 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1396 (setf *mf1* method-function
1400 (defun method-function-get (method-function key &optional default)
1401 (getf (method-function-plist method-function) key default))
1403 (defun (setf method-function-get)
1404 (val method-function key)
1405 (setf (getf (method-function-plist method-function) key) val))
1407 (defun method-function-pv-table (method-function)
1408 (method-function-get method-function :pv-table))
1410 (defun method-function-method (method-function)
1411 (method-function-get method-function :method))
1413 (defun method-function-needs-next-methods-p (method-function)
1414 (method-function-get method-function :needs-next-methods-p t))
1416 (defmacro method-function-closure-generator (method-function)
1417 `(method-function-get ,method-function 'closure-generator))
1419 (defun load-defmethod
1420 (class name quals specls ll initargs &optional pv-table-symbol)
1421 (setq initargs (copy-tree initargs))
1422 (let ((method-spec (or (getf initargs :method-spec)
1423 (make-method-spec name quals specls))))
1424 (setf (getf initargs :method-spec) method-spec)
1425 (load-defmethod-internal class name quals specls
1426 ll initargs pv-table-symbol)))
1428 (defun load-defmethod-internal
1429 (method-class gf-spec qualifiers specializers lambda-list
1430 initargs pv-table-symbol)
1431 (when pv-table-symbol
1432 (setf (getf (getf initargs :plist) :pv-table-symbol)
1434 (when (and (eq *boot-state* 'complete)
1436 (let* ((gf (fdefinition gf-spec))
1437 (method (and (generic-function-p gf)
1438 (generic-function-methods gf)
1441 (parse-specializers specializers)
1444 (style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1445 gf-spec qualifiers specializers))))
1446 (let ((method (apply #'add-named-method
1447 gf-spec qualifiers specializers lambda-list
1448 :definition-source `((defmethod ,gf-spec
1453 (unless (or (eq method-class 'standard-method)
1454 (eq (find-class method-class nil) (class-of method)))
1455 ;; FIXME: should be STYLE-WARNING?
1456 (format *error-output*
1457 "~&At the time the method with qualifiers ~:S and~%~
1458 specializers ~:S on the generic function ~S~%~
1459 was compiled, the method-class for that generic function was~%~
1460 ~S. But, the method class is now ~S, this~%~
1461 may mean that this method was compiled improperly.~%"
1462 qualifiers specializers gf-spec
1463 method-class (class-name (class-of method))))
1466 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1467 `(slow-method ,gf-spec ,@qualifiers ,unparsed-specializers))
1469 (defun initialize-method-function (initargs &optional return-function-p method)
1470 (let* ((mf (getf initargs :function))
1471 (method-spec (getf initargs :method-spec))
1472 (plist (getf initargs :plist))
1473 (pv-table-symbol (getf plist :pv-table-symbol))
1475 (mff (getf initargs :fast-function)))
1476 (flet ((set-mf-property (p v)
1478 (setf (method-function-get mf p) v))
1480 (setf (method-function-get mff p) v))))
1483 (setq mf (set-fun-name mf method-spec)))
1485 (let ((name `(fast-method ,@(cdr method-spec))))
1486 (set-fun-name mff name)
1488 (set-mf-property :name name)))))
1490 (let ((snl (getf plist :slot-name-lists))
1491 (cl (getf plist :call-list)))
1493 (setq pv-table (intern-pv-table :slot-name-lists snl
1495 (when pv-table (set pv-table-symbol pv-table))
1496 (set-mf-property :pv-table pv-table)))
1497 (loop (when (null plist) (return nil))
1498 (set-mf-property (pop plist) (pop plist)))
1500 (set-mf-property :method method))
1501 (when return-function-p
1502 (or mf (method-function-from-fast-function mff)))))))
1504 (defun analyze-lambda-list (lambda-list)
1505 (flet (;; FIXME: Is this redundant with SB-C::MAKE-KEYWORD-FOR-ARG?
1506 (parse-key-arg (arg)
1508 (if (listp (car arg))
1510 (keywordicate (car arg)))
1511 (keywordicate arg))))
1517 (allow-other-keys-p nil)
1519 (keyword-parameters ())
1521 (dolist (x lambda-list)
1522 (if (memq x lambda-list-keywords)
1524 (&optional (setq state 'optional))
1527 (&allow-other-keys (setq allow-other-keys-p t))
1528 (&rest (setq restp t
1532 (error "encountered the non-standard lambda list keyword ~S"
1535 (required (incf nrequired))
1536 (optional (incf noptional))
1537 (key (push (parse-key-arg x) keywords)
1538 (push x keyword-parameters))
1539 (rest (incf nrest)))))
1540 (when (and restp (zerop nrest))
1541 (error "Error in lambda-list:~%~
1542 After &REST, a DEFGENERIC lambda-list ~
1543 must be followed by at least one variable."))
1544 (values nrequired noptional keysp restp allow-other-keys-p
1546 (reverse keyword-parameters)))))
1548 (defun keyword-spec-name (x)
1549 (let ((key (if (atom x) x (car x))))
1554 (defun ftype-declaration-from-lambda-list (lambda-list name)
1555 (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1556 keywords keyword-parameters)
1557 (analyze-lambda-list lambda-list)
1558 (declare (ignore keyword-parameters))
1559 (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1560 (old-ftype (if (fun-type-p old) old nil))
1561 (old-restp (and old-ftype (fun-type-rest old-ftype)))
1562 (old-keys (and old-ftype
1563 (mapcar #'key-info-name
1566 (old-keysp (and old-ftype (fun-type-keyp old-ftype)))
1567 (old-allowp (and old-ftype
1568 (fun-type-allowp old-ftype)))
1569 (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1570 `(function ,(append (make-list nrequired :initial-element t)
1571 (when (plusp noptional)
1572 (append '(&optional)
1573 (make-list noptional :initial-element t)))
1574 (when (or restp old-restp)
1576 (when (or keysp old-keysp)
1578 (mapcar (lambda (key)
1581 (when (or allow-other-keys-p old-allowp)
1582 '(&allow-other-keys)))))
1585 (defun defgeneric-declaration (spec lambda-list)
1586 `(ftype ,(ftype-declaration-from-lambda-list lambda-list spec) ,spec))
1588 ;;;; early generic function support
1590 (defvar *!early-generic-functions* ())
1592 (defun ensure-generic-function (fun-name
1596 (declare (ignore environment))
1597 (let ((existing (and (gboundp fun-name)
1598 (gdefinition fun-name))))
1600 (eq *boot-state* 'complete)
1601 (null (generic-function-p existing)))
1602 (generic-clobbers-function fun-name)
1603 (apply #'ensure-generic-function-using-class
1604 existing fun-name all-keys))))
1606 (defun generic-clobbers-function (fun-name)
1607 (error 'simple-program-error
1608 :format-control "~S already names an ordinary function or a macro."
1609 :format-arguments (list fun-name)))
1611 (defvar *sgf-wrapper*
1612 (boot-make-wrapper (early-class-size 'standard-generic-function)
1613 'standard-generic-function))
1615 (defvar *sgf-slots-init*
1616 (mapcar (lambda (canonical-slot)
1617 (if (memq (getf canonical-slot :name) '(arg-info source))
1619 (let ((initfunction (getf canonical-slot :initfunction)))
1621 (funcall initfunction)
1623 (early-collect-inheritance 'standard-generic-function)))
1625 (defvar *sgf-method-class-index*
1626 (!bootstrap-slot-index 'standard-generic-function 'method-class))
1628 (defun early-gf-p (x)
1629 (and (fsc-instance-p x)
1630 (eq (clos-slots-ref (get-slots x) *sgf-method-class-index*)
1633 (defvar *sgf-methods-index*
1634 (!bootstrap-slot-index 'standard-generic-function 'methods))
1636 (defmacro early-gf-methods (gf)
1637 `(clos-slots-ref (get-slots ,gf) *sgf-methods-index*))
1639 (defvar *sgf-arg-info-index*
1640 (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1642 (defmacro early-gf-arg-info (gf)
1643 `(clos-slots-ref (get-slots ,gf) *sgf-arg-info-index*))
1645 (defvar *sgf-dfun-state-index*
1646 (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1648 (defstruct (arg-info
1650 (:constructor make-arg-info ())
1652 (arg-info-lambda-list :no-lambda-list)
1655 arg-info-number-optional
1657 arg-info-keys ;nil no &KEY or &REST allowed
1658 ;(k1 k2 ..) Each method must accept these &KEY arguments.
1659 ;T must have &KEY or &REST
1661 gf-info-simple-accessor-type ; nil, reader, writer, boundp
1662 (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1664 gf-info-static-c-a-m-emf
1665 (gf-info-c-a-m-emf-std-p t)
1668 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1670 (defun arg-info-valid-p (arg-info)
1671 (not (null (arg-info-number-optional arg-info))))
1673 (defun arg-info-applyp (arg-info)
1674 (or (plusp (arg-info-number-optional arg-info))
1675 (arg-info-key/rest-p arg-info)))
1677 (defun arg-info-number-required (arg-info)
1678 (length (arg-info-metatypes arg-info)))
1680 (defun arg-info-nkeys (arg-info)
1681 (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1683 (defun create-gf-lambda-list (lambda-list)
1684 ;;; Create a gf lambda list from a method lambda list
1685 (loop for x in lambda-list
1686 collect (if (consp x) (list (car x)) x)
1687 if (eq x '&key) do (loop-finish)))
1689 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1690 argument-precedence-order)
1691 (let* ((arg-info (if (eq *boot-state* 'complete)
1693 (early-gf-arg-info gf)))
1694 (methods (if (eq *boot-state* 'complete)
1695 (generic-function-methods gf)
1696 (early-gf-methods gf)))
1697 (was-valid-p (integerp (arg-info-number-optional arg-info)))
1698 (first-p (and new-method (null (cdr methods)))))
1699 (when (and (not lambda-list-p) methods)
1700 (setq lambda-list (gf-lambda-list gf)))
1701 (when (or lambda-list-p
1703 (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1704 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1705 (analyze-lambda-list lambda-list)
1706 (when (and methods (not first-p))
1707 (let ((gf-nreq (arg-info-number-required arg-info))
1708 (gf-nopt (arg-info-number-optional arg-info))
1709 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1710 (unless (and (= nreq gf-nreq)
1712 (eq (or keysp restp) gf-key/rest-p))
1713 (error "The lambda-list ~S is incompatible with ~
1714 existing methods of ~S."
1716 (setf (arg-info-lambda-list arg-info)
1719 (create-gf-lambda-list lambda-list)))
1720 (when (or lambda-list-p argument-precedence-order
1721 (null (arg-info-precedence arg-info)))
1722 (setf (arg-info-precedence arg-info)
1723 (compute-precedence lambda-list nreq argument-precedence-order)))
1724 (setf (arg-info-metatypes arg-info) (make-list nreq))
1725 (setf (arg-info-number-optional arg-info) nopt)
1726 (setf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1727 (setf (arg-info-keys arg-info)
1729 (if allow-other-keys-p t keywords)
1730 (arg-info-key/rest-p arg-info)))))
1732 (check-method-arg-info gf arg-info new-method))
1733 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1736 (defun check-method-arg-info (gf arg-info method)
1737 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1738 (analyze-lambda-list (if (consp method)
1739 (early-method-lambda-list method)
1740 (method-lambda-list method)))
1741 (flet ((lose (string &rest args)
1742 (error 'simple-program-error
1743 :format-control "~@<attempt to add the method~2I~_~S~I~_~
1744 to the generic function~2I~_~S;~I~_~
1746 :format-arguments (list method gf string args)))
1747 (comparison-description (x y)
1748 (if (> x y) "more" "fewer")))
1749 (let ((gf-nreq (arg-info-number-required arg-info))
1750 (gf-nopt (arg-info-number-optional arg-info))
1751 (gf-key/rest-p (arg-info-key/rest-p arg-info))
1752 (gf-keywords (arg-info-keys arg-info)))
1753 (unless (= nreq gf-nreq)
1755 "the method has ~A required arguments than the generic function."
1756 (comparison-description nreq gf-nreq)))
1757 (unless (= nopt gf-nopt)
1759 "the method has ~A optional arguments than the generic function."
1760 (comparison-description nopt gf-nopt)))
1761 (unless (eq (or keysp restp) gf-key/rest-p)
1763 "the method and generic function differ in whether they accept~_~
1764 &REST or &KEY arguments."))
1765 (when (consp gf-keywords)
1766 (unless (or (and restp (not keysp))
1768 (every (lambda (k) (memq k keywords)) gf-keywords))
1769 (lose "the method does not accept each of the &KEY arguments~2I~_~
1773 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1774 (let* ((existing-p (and methods (cdr methods) new-method))
1775 (nreq (length (arg-info-metatypes arg-info)))
1776 (metatypes (if existing-p
1777 (arg-info-metatypes arg-info)
1779 (type (if existing-p
1780 (gf-info-simple-accessor-type arg-info)
1782 (when (arg-info-valid-p arg-info)
1783 (dolist (method (if new-method (list new-method) methods))
1784 (let* ((specializers (if (or (eq *boot-state* 'complete)
1785 (not (consp method)))
1786 (method-specializers method)
1787 (early-method-specializers method t)))
1788 (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1790 (early-method-class method)))
1791 (new-type (when (and class
1792 (or (not (eq *boot-state* 'complete))
1793 (eq (generic-function-method-combination gf)
1794 *standard-method-combination*)))
1795 (cond ((eq class *the-class-standard-reader-method*)
1797 ((eq class *the-class-standard-writer-method*)
1799 ((eq class *the-class-standard-boundp-method*)
1801 (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1802 (setq type (cond ((null type) new-type)
1803 ((eq type new-type) type)
1805 (setf (arg-info-metatypes arg-info) metatypes)
1806 (setf (gf-info-simple-accessor-type arg-info) type)))
1807 (when (or (not was-valid-p) first-p)
1808 (multiple-value-bind (c-a-m-emf std-p)
1811 (compute-applicable-methods-emf gf))
1812 (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1813 (setf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1814 (unless (gf-info-c-a-m-emf-std-p arg-info)
1815 (setf (gf-info-simple-accessor-type arg-info) t))))
1817 (let ((name (if (eq *boot-state* 'complete)
1818 (generic-function-name gf)
1819 (!early-gf-name gf))))
1820 (setf (gf-precompute-dfun-and-emf-p arg-info)
1824 *internal-pcl-generalized-fun-name-symbols*))
1826 (t (let* ((symbol (fun-name-block-name name))
1827 (package (symbol-package symbol)))
1828 (and (or (eq package *pcl-package*)
1829 (memq package (package-use-list *pcl-package*)))
1830 ;; FIXME: this test will eventually be
1831 ;; superseded by the *internal-pcl...* test,
1832 ;; above. While we are in a process of
1833 ;; transition, however, it should probably
1835 (not (find #\Space (symbol-name symbol))))))))))
1836 (setf (gf-info-fast-mf-p arg-info)
1837 (or (not (eq *boot-state* 'complete))
1838 (let* ((method-class (generic-function-method-class gf))
1839 (methods (compute-applicable-methods
1840 #'make-method-lambda
1841 (list gf (class-prototype method-class)
1843 (and methods (null (cdr methods))
1844 (let ((specls (method-specializers (car methods))))
1845 (and (classp (car specls))
1846 (eq 'standard-generic-function
1847 (class-name (car specls)))
1848 (classp (cadr specls))
1849 (eq 'standard-method
1850 (class-name (cadr specls)))))))))
1853 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
1855 ;;; The STATIC-SLOTS field of the funcallable instances used as early
1856 ;;; generic functions is used to store the early methods and early
1857 ;;; discriminator code for the early generic function. The static
1858 ;;; slots field of the fins contains a list whose:
1859 ;;; CAR - a list of the early methods on this early gf
1860 ;;; CADR - the early discriminator code for this method
1861 (defun ensure-generic-function-using-class (existing spec &rest keys
1862 &key (lambda-list nil
1864 argument-precedence-order
1866 (declare (ignore keys))
1867 (cond ((and existing (early-gf-p existing))
1869 (set-arg-info existing :lambda-list lambda-list))
1871 ((assoc spec *!generic-function-fixups* :test #'equal)
1873 (make-early-gf spec lambda-list lambda-list-p existing
1874 argument-precedence-order)
1875 (error "The function ~S is not already defined." spec)))
1877 (error "~S should be on the list ~S."
1879 '*!generic-function-fixups*))
1881 (pushnew spec *!early-generic-functions* :test #'equal)
1882 (make-early-gf spec lambda-list lambda-list-p nil
1883 argument-precedence-order))))
1885 (defun make-early-gf (spec &optional lambda-list lambda-list-p
1886 function argument-precedence-order)
1887 (let ((fin (allocate-funcallable-instance *sgf-wrapper* *sgf-slots-init*)))
1888 (set-funcallable-instance-function
1891 (if (eq spec 'print-object)
1892 #'(lambda (instance stream)
1893 (print-unreadable-object (instance stream :identity t)
1894 (format stream "std-instance")))
1895 #'(lambda (&rest args)
1896 (declare (ignore args))
1897 (error "The function of the funcallable-instance ~S~
1898 has not been set." fin)))))
1899 (setf (gdefinition spec) fin)
1900 (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
1901 (!bootstrap-set-slot 'standard-generic-function
1905 (set-fun-name fin spec)
1906 (let ((arg-info (make-arg-info)))
1907 (setf (early-gf-arg-info fin) arg-info)
1909 (proclaim (defgeneric-declaration spec lambda-list))
1910 (if argument-precedence-order
1912 :lambda-list lambda-list
1913 :argument-precedence-order argument-precedence-order)
1914 (set-arg-info fin :lambda-list lambda-list))))
1917 (defun set-dfun (gf &optional dfun cache info)
1919 (setf (cache-owner cache) gf))
1920 (let ((new-state (if (and dfun (or cache info))
1921 (list* dfun cache info)
1923 (if (eq *boot-state* 'complete)
1924 (setf (gf-dfun-state gf) new-state)
1925 (setf (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*)
1929 (defun gf-dfun-cache (gf)
1930 (let ((state (if (eq *boot-state* 'complete)
1932 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1935 (cons (cadr state)))))
1937 (defun gf-dfun-info (gf)
1938 (let ((state (if (eq *boot-state* 'complete)
1940 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1943 (cons (cddr state)))))
1945 (defvar *sgf-name-index*
1946 (!bootstrap-slot-index 'standard-generic-function 'name))
1948 (defun !early-gf-name (gf)
1949 (clos-slots-ref (get-slots gf) *sgf-name-index*))
1951 (defun gf-lambda-list (gf)
1952 (let ((arg-info (if (eq *boot-state* 'complete)
1954 (early-gf-arg-info gf))))
1955 (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
1956 (let ((methods (if (eq *boot-state* 'complete)
1957 (generic-function-methods gf)
1958 (early-gf-methods gf))))
1961 (warn "no way to determine the lambda list for ~S" gf)
1963 (let* ((method (car (last methods)))
1964 (ll (if (consp method)
1965 (early-method-lambda-list method)
1966 (method-lambda-list method))))
1967 (create-gf-lambda-list ll))))
1968 (arg-info-lambda-list arg-info))))
1970 (defmacro real-ensure-gf-internal (gf-class all-keys env)
1972 (cond ((symbolp ,gf-class)
1973 (setq ,gf-class (find-class ,gf-class t ,env)))
1974 ((classp ,gf-class))
1976 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
1977 class nor a symbol that names a class."
1979 (remf ,all-keys :generic-function-class)
1980 (remf ,all-keys :environment)
1981 (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
1982 (unless (eq combin '.shes-not-there.)
1983 (setf (getf ,all-keys :method-combination)
1984 (find-method-combination (class-prototype ,gf-class)
1987 (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
1988 (unless (eq method-class '.shes-not-there.)
1989 (setf (getf ,all-keys :method-class)
1990 (cond ((classp method-class)
1992 (t (find-class method-class t ,env))))))))
1994 (defun real-ensure-gf-using-class--generic-function
1998 &key environment (lambda-list nil lambda-list-p)
1999 (generic-function-class 'standard-generic-function gf-class-p)
2001 (real-ensure-gf-internal generic-function-class all-keys environment)
2002 (unless (or (null gf-class-p)
2003 (eq (class-of existing) generic-function-class))
2004 (change-class existing generic-function-class))
2006 (apply #'reinitialize-instance existing all-keys)
2008 (proclaim (defgeneric-declaration fun-name lambda-list)))))
2010 (defun real-ensure-gf-using-class--null
2014 &key environment (lambda-list nil lambda-list-p)
2015 (generic-function-class 'standard-generic-function)
2017 (declare (ignore existing))
2018 (real-ensure-gf-internal generic-function-class all-keys environment)
2020 (setf (gdefinition fun-name)
2021 (apply #'make-instance generic-function-class
2022 :name fun-name all-keys))
2024 (proclaim (defgeneric-declaration fun-name lambda-list)))))
2026 (defun get-generic-fun-info (gf)
2027 ;; values nreq applyp metatypes nkeys arg-info
2028 (multiple-value-bind (applyp metatypes arg-info)
2029 (let* ((arg-info (if (early-gf-p gf)
2030 (early-gf-arg-info gf)
2032 (metatypes (arg-info-metatypes arg-info)))
2033 (values (arg-info-applyp arg-info)
2036 (values (length metatypes) applyp metatypes
2037 (count-if (lambda (x) (neq x t)) metatypes)
2040 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
2041 &optional slot-name)
2042 (initialize-method-function initargs)
2045 ;; Figure out whether we got class objects or class names as the
2046 ;; specializers and set parsed and unparsed appropriately. If we
2047 ;; got class objects, then we can compute unparsed, but if we got
2048 ;; class names we don't try to compute parsed.
2050 ;; Note that the use of not symbolp in this call to every should be
2051 ;; read as 'classp' we can't use classp itself because it doesn't
2053 (if (every (lambda (s) (not (symbolp s))) specializers)
2054 (setq parsed specializers
2055 unparsed (mapcar (lambda (s)
2056 (if (eq s t) t (class-name s)))
2058 (setq unparsed specializers
2060 (list :early-method ;This is an early method dammit!
2062 (getf initargs :function)
2063 (getf initargs :fast-function)
2065 parsed ;The parsed specializers. This is used
2066 ;by early-method-specializers to cache
2067 ;the parse. Note that this only comes
2068 ;into play when there is more than one
2069 ;early method on an early gf.
2071 (list class ;A list to which real-make-a-method
2072 qualifiers ;can be applied to make a real method
2073 arglist ;corresponding to this early one.
2079 (defun real-make-a-method
2080 (class qualifiers lambda-list specializers initargs doc
2081 &optional slot-name)
2082 (setq specializers (parse-specializers specializers))
2083 (apply #'make-instance class
2084 :qualifiers qualifiers
2085 :lambda-list lambda-list
2086 :specializers specializers
2088 :slot-name slot-name
2092 (defun early-method-function (early-method)
2093 (values (cadr early-method) (caddr early-method)))
2095 (defun early-method-class (early-method)
2096 (find-class (car (fifth early-method))))
2098 (defun early-method-standard-accessor-p (early-method)
2099 (let ((class (first (fifth early-method))))
2100 (or (eq class 'standard-reader-method)
2101 (eq class 'standard-writer-method)
2102 (eq class 'standard-boundp-method))))
2104 (defun early-method-standard-accessor-slot-name (early-method)
2105 (seventh (fifth early-method)))
2107 ;;; Fetch the specializers of an early method. This is basically just
2108 ;;; a simple accessor except that when the second argument is t, this
2109 ;;; converts the specializers from symbols into class objects. The
2110 ;;; class objects are cached in the early method, this makes
2111 ;;; bootstrapping faster because the class objects only have to be
2115 ;;; The second argument should only be passed as T by
2116 ;;; early-lookup-method. This is to implement the rule that only when
2117 ;;; there is more than one early method on a generic function is the
2118 ;;; conversion from class names to class objects done. This
2119 ;;; corresponds to the fact that we are only allowed to have one
2120 ;;; method on any generic function up until the time classes exist.
2121 (defun early-method-specializers (early-method &optional objectsp)
2122 (if (and (listp early-method)
2123 (eq (car early-method) :early-method))
2124 (cond ((eq objectsp t)
2125 (or (fourth early-method)
2126 (setf (fourth early-method)
2127 (mapcar #'find-class (cadddr (fifth early-method))))))
2129 (cadddr (fifth early-method))))
2130 (error "~S is not an early-method." early-method)))
2132 (defun early-method-qualifiers (early-method)
2133 (cadr (fifth early-method)))
2135 (defun early-method-lambda-list (early-method)
2136 (caddr (fifth early-method)))
2138 (defun early-add-named-method (generic-function-name
2143 (let* ((gf (ensure-generic-function generic-function-name))
2145 (dolist (m (early-gf-methods gf))
2146 (when (and (equal (early-method-specializers m) specializers)
2147 (equal (early-method-qualifiers m) qualifiers))
2149 (new (make-a-method 'standard-method
2155 (when existing (remove-method gf existing))
2156 (add-method gf new)))
2158 ;;; This is the early version of ADD-METHOD. Later this will become a
2159 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2160 ;;; special knowledge about ADD-METHOD.
2161 (defun add-method (generic-function method)
2162 (when (not (fsc-instance-p generic-function))
2163 (error "Early ADD-METHOD didn't get a funcallable instance."))
2164 (when (not (and (listp method) (eq (car method) :early-method)))
2165 (error "Early ADD-METHOD didn't get an early method."))
2166 (push method (early-gf-methods generic-function))
2167 (set-arg-info generic-function :new-method method)
2168 (unless (assoc (!early-gf-name generic-function)
2169 *!generic-function-fixups*
2171 (update-dfun generic-function)))
2173 ;;; This is the early version of REMOVE-METHOD. See comments on
2174 ;;; the early version of ADD-METHOD.
2175 (defun remove-method (generic-function method)
2176 (when (not (fsc-instance-p generic-function))
2177 (error "An early remove-method didn't get a funcallable instance."))
2178 (when (not (and (listp method) (eq (car method) :early-method)))
2179 (error "An early remove-method didn't get an early method."))
2180 (setf (early-gf-methods generic-function)
2181 (remove method (early-gf-methods generic-function)))
2182 (set-arg-info generic-function)
2183 (unless (assoc (!early-gf-name generic-function)
2184 *!generic-function-fixups*
2186 (update-dfun generic-function)))
2188 ;;; This is the early version of GET-METHOD. See comments on the early
2189 ;;; version of ADD-METHOD.
2190 (defun get-method (generic-function qualifiers specializers
2191 &optional (errorp t))
2192 (if (early-gf-p generic-function)
2193 (or (dolist (m (early-gf-methods generic-function))
2194 (when (and (or (equal (early-method-specializers m nil)
2196 (equal (early-method-specializers m t)
2198 (equal (early-method-qualifiers m) qualifiers))
2201 (error "can't get early method")
2203 (real-get-method generic-function qualifiers specializers errorp)))
2205 (defun !fix-early-generic-functions ()
2206 (let ((accessors nil))
2207 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2208 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2209 (dolist (early-gf-spec *!early-generic-functions*)
2210 (when (every #'early-method-standard-accessor-p
2211 (early-gf-methods (gdefinition early-gf-spec)))
2212 (push early-gf-spec accessors)))
2213 (dolist (spec (nconc accessors
2214 '(accessor-method-slot-name
2215 generic-function-methods
2220 slot-definition-location
2221 slot-definition-name
2224 class-precedence-list
2225 slot-boundp-using-class
2226 (setf slot-value-using-class)
2227 slot-value-using-class
2230 funcallable-standard-class-p
2233 (setq *!early-generic-functions*
2235 (delete spec *!early-generic-functions* :test #'equal))))
2237 (dolist (early-gf-spec *!early-generic-functions*)
2238 (/show early-gf-spec)
2239 (let* ((gf (gdefinition early-gf-spec))
2240 (methods (mapcar (lambda (early-method)
2241 (let ((args (copy-list (fifth
2244 (early-method-specializers
2246 (apply #'real-make-a-method args)))
2247 (early-gf-methods gf))))
2248 (setf (generic-function-method-class gf) *the-class-standard-method*)
2249 (setf (generic-function-method-combination gf)
2250 *standard-method-combination*)
2251 (set-methods gf methods)))
2253 (dolist (fn *!early-functions*)
2255 (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2257 (dolist (fixup *!generic-function-fixups*)
2259 (let* ((fspec (car fixup))
2260 (gf (gdefinition fspec))
2261 (methods (mapcar (lambda (method)
2262 (let* ((lambda-list (first method))
2263 (specializers (second method))
2264 (method-fn-name (third method))
2265 (fn-name (or method-fn-name fspec))
2266 (fn (fdefinition fn-name))
2270 (lambda (args next-methods)
2274 `(call ,fn-name)))))
2275 (declare (type function fn))
2276 (make-a-method 'standard-method
2283 (setf (generic-function-method-class gf) *the-class-standard-method*)
2284 (setf (generic-function-method-combination gf)
2285 *standard-method-combination*)
2286 (set-methods gf methods))))
2287 (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2289 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2290 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2291 ;;; is really implemented.
2292 (defun parse-defmethod (cdr-of-form)
2293 (declare (list cdr-of-form))
2294 (let ((name (pop cdr-of-form))
2297 (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2298 (push (pop cdr-of-form) qualifiers)
2299 (return (setq qualifiers (nreverse qualifiers)))))
2300 (setq spec-ll (pop cdr-of-form))
2301 (values name qualifiers spec-ll cdr-of-form)))
2303 (defun parse-specializers (specializers)
2304 (declare (list specializers))
2305 (flet ((parse (spec)
2306 (let ((result (specializer-from-type spec)))
2307 (if (specializerp result)
2310 (error "~S was used as a specializer,~%~
2311 but is not the name of a class."
2313 (error "~S is not a legal specializer." spec))))))
2314 (mapcar #'parse specializers)))
2316 (defun unparse-specializers (specializers-or-method)
2317 (if (listp specializers-or-method)
2318 (flet ((unparse (spec)
2319 (if (specializerp spec)
2320 (let ((type (specializer-type spec)))
2321 (if (and (consp type)
2322 (eq (car type) 'class))
2323 (let* ((class (cadr type))
2324 (class-name (class-name class)))
2325 (if (eq class (find-class class-name nil))
2329 (error "~S is not a legal specializer." spec))))
2330 (mapcar #'unparse specializers-or-method))
2331 (unparse-specializers (method-specializers specializers-or-method))))
2333 (defun parse-method-or-spec (spec &optional (errorp t))
2334 (let (gf method name temp)
2337 gf (method-generic-function method)
2338 temp (and gf (generic-function-name gf))
2340 (make-method-spec temp
2341 (method-qualifiers method)
2342 (unparse-specializers
2343 (method-specializers method)))
2344 (make-symbol (format nil "~S" method))))
2345 (multiple-value-bind (gf-spec quals specls)
2346 (parse-defmethod spec)
2347 (and (setq gf (and (or errorp (gboundp gf-spec))
2348 (gdefinition gf-spec)))
2349 (let ((nreq (compute-discriminating-function-arglist-info gf)))
2350 (setq specls (append (parse-specializers specls)
2351 (make-list (- nreq (length specls))
2355 (setq method (get-method gf quals specls errorp))
2358 gf-spec quals (unparse-specializers specls))))))))
2359 (values gf method name)))
2361 (defun extract-parameters (specialized-lambda-list)
2362 (multiple-value-bind (parameters ignore1 ignore2)
2363 (parse-specialized-lambda-list specialized-lambda-list)
2364 (declare (ignore ignore1 ignore2))
2367 (defun extract-lambda-list (specialized-lambda-list)
2368 (multiple-value-bind (ignore1 lambda-list ignore2)
2369 (parse-specialized-lambda-list specialized-lambda-list)
2370 (declare (ignore ignore1 ignore2))
2373 (defun extract-specializer-names (specialized-lambda-list)
2374 (multiple-value-bind (ignore1 ignore2 specializers)
2375 (parse-specialized-lambda-list specialized-lambda-list)
2376 (declare (ignore ignore1 ignore2))
2379 (defun extract-required-parameters (specialized-lambda-list)
2380 (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2381 (parse-specialized-lambda-list specialized-lambda-list)
2382 (declare (ignore ignore1 ignore2 ignore3))
2383 required-parameters))
2385 (define-condition specialized-lambda-list-error
2386 (reference-condition simple-program-error)
2388 (:default-initargs :references (list '(:ansi-cl :section (3 4 3)))))
2390 (defun parse-specialized-lambda-list
2392 &optional supplied-keywords (allowed-keywords '(&optional &rest &key &aux))
2393 &aux (specialized-lambda-list-keywords
2394 '(&optional &rest &key &allow-other-keys &aux)))
2395 (let ((arg (car arglist)))
2396 (cond ((null arglist) (values nil nil nil nil))
2398 (values nil arglist nil nil))
2399 ((memq arg lambda-list-keywords)
2400 ;; non-standard lambda-list-keywords are errors.
2401 (unless (memq arg specialized-lambda-list-keywords)
2402 (error 'specialized-lambda-list-error
2403 :format-control "unknown specialized-lambda-list ~
2405 :format-arguments (list arg)))
2406 ;; no multiple &rest x &rest bla specifying
2407 (when (memq arg supplied-keywords)
2408 (error 'specialized-lambda-list-error
2409 :format-control "multiple occurrence of ~
2410 specialized-lambda-list keyword ~S~%"
2411 :format-arguments (list arg)))
2412 ;; And no placing &key in front of &optional, either.
2413 (unless (memq arg allowed-keywords)
2414 (error 'specialized-lambda-list-error
2415 :format-control "misplaced specialized-lambda-list ~
2417 :format-arguments (list arg)))
2418 ;; When we are at a lambda-list keyword, the parameters
2419 ;; don't include the lambda-list keyword; the lambda-list
2420 ;; does include the lambda-list keyword; and no
2421 ;; specializers are allowed to follow the lambda-list
2422 ;; keywords (at least for now).
2423 (multiple-value-bind (parameters lambda-list)
2424 (parse-specialized-lambda-list (cdr arglist)
2425 (cons arg supplied-keywords)
2427 (cons '&allow-other-keys
2428 (cdr (member arg allowed-keywords)))
2429 (cdr (member arg allowed-keywords))))
2430 (when (and (eq arg '&rest)
2431 (or (null lambda-list)
2432 (memq (car lambda-list)
2433 specialized-lambda-list-keywords)
2434 (not (or (null (cadr lambda-list))
2435 (memq (cadr lambda-list)
2436 specialized-lambda-list-keywords)))))
2437 (error 'specialized-lambda-list-error
2439 "in a specialized-lambda-list, excactly one ~
2440 variable must follow &REST.~%"
2441 :format-arguments nil))
2443 (cons arg lambda-list)
2447 ;; After a lambda-list keyword there can be no specializers.
2448 (multiple-value-bind (parameters lambda-list)
2449 (parse-specialized-lambda-list (cdr arglist)
2452 (values (cons (if (listp arg) (car arg) arg) parameters)
2453 (cons arg lambda-list)
2457 (multiple-value-bind (parameters lambda-list specializers required)
2458 (parse-specialized-lambda-list (cdr arglist))
2459 (values (cons (if (listp arg) (car arg) arg) parameters)
2460 (cons (if (listp arg) (car arg) arg) lambda-list)
2461 (cons (if (listp arg) (cadr arg) t) specializers)
2462 (cons (if (listp arg) (car arg) arg) required)))))))
2464 (setq *boot-state* 'early)
2466 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2467 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2468 ;;; walker stuff was only used for implementing stuff like that; maybe
2469 ;;; it's not needed any more? Hunt down what it was used for and see.
2471 (defmacro with-slots (slots instance &body body)
2472 (let ((in (gensym)))
2473 `(let ((,in ,instance))
2474 (declare (ignorable ,in))
2475 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2478 (and (symbolp instance)
2479 `((declare (%variable-rebinding ,in ,instance)))))
2481 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2483 (if (symbolp slot-entry)
2487 (if (symbolp slot-entry)
2489 (cadr slot-entry))))
2491 (slot-value ,in ',slot-name))))
2495 (defmacro with-accessors (slots instance &body body)
2496 (let ((in (gensym)))
2497 `(let ((,in ,instance))
2498 (declare (ignorable ,in))
2499 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2502 (and (symbolp instance)
2503 `((declare (%variable-rebinding ,in ,instance)))))
2505 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2506 (let ((var-name (car slot-entry))
2507 (accessor-name (cadr slot-entry)))
2508 `(,var-name (,accessor-name ,in))))