1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; This software is derived from software originally released by Xerox
5 ;;;; Corporation. Copyright and release statements follow. Later modifications
6 ;;;; to the software are in the public domain and are provided with
7 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
10 ;;;; copyright information from original PCL sources:
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
15 ;;;; Use and copying of this software and preparation of derivative works based
16 ;;;; upon this software are permitted. Any distribution of this software or
17 ;;;; derivative works must comply with all applicable United States export
20 ;;;; This software is made available AS IS, and Xerox Corporation makes no
21 ;;;; warranty about the software, its performance or its conformity to any
28 The CommonLoops evaluator is meta-circular.
30 Most of the code in PCL is methods on generic functions, including
31 most of the code that actually implements generic functions and method
34 So, we have a classic bootstrapping problem. The solution to this is
35 to first get a cheap implementation of generic functions running,
36 these are called early generic functions. These early generic
37 functions and the corresponding early methods and early method lookup
38 are used to get enough of the system running that it is possible to
39 create real generic functions and methods and implement real method
40 lookup. At that point (done in the file FIXUP) the function
41 !FIX-EARLY-GENERIC-FUNCTIONS is called to convert all the early generic
42 functions to real generic functions.
44 The cheap generic functions are built using the same
45 FUNCALLABLE-INSTANCE objects that real generic functions are made out of.
46 This means that as PCL is being bootstrapped, the cheap generic
47 function objects which are being created are the same objects which
48 will later be real generic functions. This is good because:
49 - we don't cons garbage structure, and
50 - we can keep pointers to the cheap generic function objects
51 during booting because those pointers will still point to
52 the right object after the generic functions are all fixed up.
54 This file defines the DEFMETHOD macro and the mechanism used to expand
55 it. This includes the mechanism for processing the body of a method.
56 DEFMETHOD basically expands into a call to LOAD-DEFMETHOD, which
57 basically calls ADD-METHOD to add the method to the generic function.
58 These expansions can be loaded either during bootstrapping or when PCL
59 is fully up and running.
61 An important effect of this arrangement is it means we can compile
62 files with DEFMETHOD forms in them in a completely running PCL, but
63 then load those files back in during bootstrapping. This makes
64 development easier. It also means there is only one set of code for
65 processing DEFMETHOD. Bootstrapping works by being sure to have
66 LOAD-METHOD be careful to call only primitives which work during
71 ;;; FIXME: As of sbcl-0.6.9.10, PCL still uses this nonstandard type
72 ;;; of declaration internally. It would be good to figure out how to
73 ;;; get rid of it, or failing that, (1) document why it's needed and
74 ;;; (2) use a private symbol with a forbidding name which suggests
75 ;;; it's not to be messed with by the user (e.g. SB-PCL:%CLASS)
76 ;;; instead of the too-inviting CLASS. (I tried just deleting the
77 ;;; declarations in MAKE-METHOD-LAMBDA-INTERNAL ca. sbcl-0.6.9.10, but
78 ;;; then things break.)
79 (declaim (declaration class))
81 (declaim (notinline make-a-method
83 ensure-generic-function-using-class
87 (defvar *!early-functions*
88 '((make-a-method early-make-a-method
90 (add-named-method early-add-named-method
91 real-add-named-method)
94 ;;; For each of the early functions, arrange to have it point to its
95 ;;; early definition. Do this in a way that makes sure that if we
96 ;;; redefine one of the early definitions the redefinition will take
97 ;;; effect. This makes development easier.
98 (dolist (fns *!early-functions*)
99 (let ((name (car fns))
100 (early-name (cadr fns)))
101 (setf (gdefinition name)
104 (apply (fdefinition early-name) args))
107 ;;; *!GENERIC-FUNCTION-FIXUPS* is used by !FIX-EARLY-GENERIC-FUNCTIONS
108 ;;; to convert the few functions in the bootstrap which are supposed
109 ;;; to be generic functions but can't be early on.
110 (defvar *!generic-function-fixups*
112 ((generic-function method) ;lambda-list
113 (standard-generic-function method) ;specializers
114 real-add-method)) ;method-function
116 ((generic-function method)
117 (standard-generic-function method)
120 ((generic-function qualifiers specializers &optional (errorp t))
121 (standard-generic-function t t)
123 (ensure-generic-function-using-class
124 ((generic-function fun-name
125 &key generic-function-class environment
128 real-ensure-gf-using-class--generic-function)
129 ((generic-function fun-name
130 &key generic-function-class environment
133 real-ensure-gf-using-class--null))
135 ((proto-generic-function proto-method lambda-expression environment)
136 (standard-generic-function standard-method t t)
137 real-make-method-lambda))
138 (make-method-initargs-form
139 ((proto-generic-function proto-method
141 lambda-list environment)
142 (standard-generic-function standard-method t t t)
143 real-make-method-initargs-form))
144 (compute-effective-method
145 ((generic-function combin applicable-methods)
146 (generic-function standard-method-combination t)
147 standard-compute-effective-method))))
149 (defmacro defgeneric (fun-name lambda-list &body options)
150 (declare (type list lambda-list))
151 (unless (legal-fun-name-p fun-name)
152 (error 'simple-program-error
153 :format-control "illegal generic function name ~S"
154 :format-arguments (list fun-name)))
155 (check-gf-lambda-list lambda-list)
158 (flet ((duplicate-option (name)
159 (error 'simple-program-error
160 :format-control "The option ~S appears more than once."
161 :format-arguments (list name)))
162 (expand-method-definition (qab) ; QAB = qualifiers, arglist, body
163 (let* ((arglist-pos (position-if #'listp qab))
164 (arglist (elt qab arglist-pos))
165 (qualifiers (subseq qab 0 arglist-pos))
166 (body (nthcdr (1+ arglist-pos) qab)))
167 `(push (defmethod ,fun-name ,@qualifiers ,arglist ,@body)
168 (generic-function-initial-methods #',fun-name)))))
169 (macrolet ((initarg (key) `(getf initargs ,key)))
170 (dolist (option options)
171 (let ((car-option (car option)))
175 (consp (cadr option))
176 (member (first (cadr option))
177 ;; FIXME: this list is slightly weird.
178 ;; ANSI (on the DEFGENERIC page) in one
179 ;; place allows only OPTIMIZE; in
180 ;; another place gives this list of
181 ;; disallowed declaration specifiers.
182 ;; This seems to be the only place where
183 ;; the FUNCTION declaration is
184 ;; mentioned; TYPE seems to be missing.
185 ;; Very strange. -- CSR, 2002-10-21
186 '(declaration ftype function
187 inline notinline special)))
188 (error 'simple-program-error
189 :format-control "The declaration specifier ~S ~
190 is not allowed inside DEFGENERIC."
191 :format-arguments (list (cadr option))))
192 (push (cadr option) (initarg :declarations)))
194 (when (initarg car-option)
195 (duplicate-option car-option))
196 (unless (symbolp (cadr option))
197 (error 'simple-program-error
198 :format-control "METHOD-COMBINATION name not a ~
200 :format-arguments (list (cadr option))))
201 (setf (initarg car-option)
203 (:argument-precedence-order
204 (let* ((required (parse-lambda-list lambda-list))
205 (supplied (cdr option)))
206 (unless (= (length required) (length supplied))
207 (error 'simple-program-error
208 :format-control "argument count discrepancy in ~
209 :ARGUMENT-PRECEDENCE-ORDER clause."
210 :format-arguments nil))
211 (when (set-difference required supplied)
212 (error 'simple-program-error
213 :format-control "unequal sets for ~
214 :ARGUMENT-PRECEDENCE-ORDER clause: ~
216 :format-arguments (list required supplied)))
217 (setf (initarg car-option)
219 ((:documentation :generic-function-class :method-class)
220 (unless (proper-list-of-length-p option 2)
221 (error "bad list length for ~S" option))
222 (if (initarg car-option)
223 (duplicate-option car-option)
224 (setf (initarg car-option) `',(cadr option))))
226 (push (cdr option) methods))
228 ;; ANSI requires that unsupported things must get a
230 (error 'simple-program-error
231 :format-control "unsupported option ~S"
232 :format-arguments (list option))))))
234 (when (initarg :declarations)
235 (setf (initarg :declarations)
236 `',(initarg :declarations))))
238 (eval-when (:compile-toplevel :load-toplevel :execute)
239 (compile-or-load-defgeneric ',fun-name))
240 (load-defgeneric ',fun-name ',lambda-list ,@initargs)
241 ,@(mapcar #'expand-method-definition methods)
244 (defun compile-or-load-defgeneric (fun-name)
245 (proclaim-as-fun-name fun-name)
246 (note-name-defined fun-name :function)
247 (unless (eq (info :function :where-from fun-name) :declared)
248 (setf (info :function :where-from fun-name) :defined)
249 (setf (info :function :type fun-name)
250 (specifier-type 'function))))
252 (defun load-defgeneric (fun-name lambda-list &rest initargs)
253 (when (fboundp fun-name)
254 (style-warn "redefining ~S in DEFGENERIC" fun-name)
255 (let ((fun (fdefinition fun-name)))
256 (when (generic-function-p fun)
257 (loop for method in (generic-function-initial-methods fun)
258 do (remove-method fun method))
259 (setf (generic-function-initial-methods fun) '()))))
260 (apply #'ensure-generic-function
262 :lambda-list lambda-list
263 :definition-source `((defgeneric ,fun-name) ,*load-pathname*)
266 ;;; As per section 3.4.2 of the ANSI spec, generic function lambda
267 ;;; lists have some special limitations, which we check here.
268 (defun check-gf-lambda-list (lambda-list)
269 (flet ((ensure (arg ok)
272 ;; (s/invalid/non-ANSI-conforming/ because the old PCL
273 ;; implementation allowed this, so people got used to
274 ;; it, and maybe this phrasing will help them to guess
275 ;; why their program which worked under PCL no longer works.)
276 "~@<non-ANSI-conforming argument ~S ~_in the generic function lambda list ~S~:>"
278 (multiple-value-bind (required optional restp rest keyp keys allowp
279 auxp aux morep more-context more-count)
280 (parse-lambda-list lambda-list)
281 (declare (ignore required)) ; since they're no different in a gf ll
282 (declare (ignore restp rest)) ; since they're no different in a gf ll
283 (declare (ignore allowp)) ; since &ALLOW-OTHER-KEYS is fine either way
284 (declare (ignore aux)) ; since we require AUXP=NIL
285 (declare (ignore more-context more-count)) ; safely ignored unless MOREP
286 ;; no defaults allowed for &OPTIONAL arguments
288 (ensure i (or (symbolp i)
289 (and (consp i) (symbolp (car i)) (null (cdr i))))))
290 ;; no defaults allowed for &KEY arguments
293 (ensure i (or (symbolp i)
295 (or (symbolp (car i))
303 (error "&AUX is not allowed in a generic function lambda list: ~S"
305 ;; Oh, *puhlease*... not specifically as per section 3.4.2 of
306 ;; the ANSI spec, but the CMU CL &MORE extension does not
308 (aver (not morep)))))
310 (defmacro defmethod (&rest args &environment env)
311 (multiple-value-bind (name qualifiers lambda-list body)
312 (parse-defmethod args)
313 (multiple-value-bind (proto-gf proto-method)
314 (prototypes-for-make-method-lambda name)
315 (expand-defmethod name
323 (defun prototypes-for-make-method-lambda (name)
324 (if (not (eq *boot-state* 'complete))
326 (let ((gf? (and (gboundp name)
327 (gdefinition name))))
329 (not (generic-function-p gf?)))
330 (values (class-prototype (find-class 'standard-generic-function))
331 (class-prototype (find-class 'standard-method)))
333 (class-prototype (or (generic-function-method-class gf?)
334 (find-class 'standard-method))))))))
336 ;;; Take a name which is either a generic function name or a list specifying
337 ;;; a SETF generic function (like: (SETF <generic-function-name>)). Return
338 ;;; the prototype instance of the method-class for that generic function.
340 ;;; If there is no generic function by that name, this returns the
341 ;;; default value, the prototype instance of the class
342 ;;; STANDARD-METHOD. This default value is also returned if the spec
343 ;;; names an ordinary function or even a macro. In effect, this leaves
344 ;;; the signalling of the appropriate error until load time.
346 ;;; Note: During bootstrapping, this function is allowed to return NIL.
347 (defun method-prototype-for-gf (name)
348 (let ((gf? (and (gboundp name)
349 (gdefinition name))))
350 (cond ((neq *boot-state* 'complete) nil)
352 (not (generic-function-p gf?))) ; Someone else MIGHT
353 ; error at load time.
354 (class-prototype (find-class 'standard-method)))
356 (class-prototype (or (generic-function-method-class gf?)
357 (find-class 'standard-method)))))))
359 (defun expand-defmethod (name
366 (multiple-value-bind (method-lambda unspecialized-lambda-list specializers)
367 (add-method-declarations name qualifiers lambda-list body env)
368 (multiple-value-bind (method-function-lambda initargs)
369 (make-method-lambda proto-gf proto-method method-lambda env)
370 (let ((initargs-form (make-method-initargs-form proto-gf
372 method-function-lambda
376 ;; Note: We could DECLAIM the ftype of the generic function
377 ;; here, since ANSI specifies that we create it if it does
378 ;; not exist. However, I chose not to, because I think it's
379 ;; more useful to support a style of programming where every
380 ;; generic function has an explicit DEFGENERIC and any typos
381 ;; in DEFMETHODs are warned about. Otherwise
383 ;; (DEFGENERIC FOO-BAR-BLETCH ((X T)))
384 ;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
385 ;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
386 ;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
387 ;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
388 ;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
390 ;; compiles without raising an error and runs without
391 ;; raising an error (since SIMPLE-VECTOR cases fall through
392 ;; to VECTOR) but still doesn't do what was intended. I hate
393 ;; that kind of bug (code which silently gives the wrong
394 ;; answer), so we don't do a DECLAIM here. -- WHN 20000229
395 ,(make-defmethod-form name qualifiers specializers
396 unspecialized-lambda-list
398 (class-name (class-of proto-method))
401 (getf (getf initargs :plist)
402 :pv-table-symbol)))))))
404 (defun interned-symbol-p (x)
405 (and (symbolp x) (symbol-package x)))
407 (defun make-defmethod-form (name qualifiers specializers
408 unspecialized-lambda-list method-class-name
409 initargs-form &optional pv-table-symbol)
412 (if (and (interned-symbol-p (fun-name-block-name name))
413 (every #'interned-symbol-p qualifiers)
416 (and (eq (car s) 'eql)
418 (let ((sv (eval (cadr s))))
419 (or (interned-symbol-p sv)
422 (standard-char-p sv)))))
423 (interned-symbol-p s)))
425 (consp initargs-form)
426 (eq (car initargs-form) 'list*)
427 (memq (cadr initargs-form) '(:function :fast-function))
428 (consp (setq fn (caddr initargs-form)))
429 (eq (car fn) 'function)
430 (consp (setq fn-lambda (cadr fn)))
431 (eq (car fn-lambda) 'lambda))
432 (let* ((specls (mapcar (lambda (specl)
434 `(,(car specl) ,(eval (cadr specl)))
437 (mname `(,(if (eq (cadr initargs-form) :function)
438 'method 'fast-method)
439 ,name ,@qualifiers ,specls))
440 (mname-sym (intern (let ((*print-pretty* nil)
441 ;; (We bind *PACKAGE* to
442 ;; KEYWORD here as a way to
443 ;; force symbols to be printed
444 ;; with explicit package
446 (*package* *keyword-package*))
447 (format nil "~S" mname)))))
449 (defun ,mname-sym ,(cadr fn-lambda)
451 ,(make-defmethod-form-internal
452 name qualifiers `',specls
453 unspecialized-lambda-list method-class-name
454 `(list* ,(cadr initargs-form)
456 ,@(cdddr initargs-form))
458 (make-defmethod-form-internal
460 `(list ,@(mapcar (lambda (specializer)
461 (if (consp specializer)
462 ``(,',(car specializer)
463 ,,(cadr specializer))
466 unspecialized-lambda-list
471 (defun make-defmethod-form-internal
472 (name qualifiers specializers-form unspecialized-lambda-list
473 method-class-name initargs-form &optional pv-table-symbol)
479 ',unspecialized-lambda-list
481 ;; Paper over a bug in KCL by passing the cache-symbol here in
482 ;; addition to in the list. FIXME: We should no longer need to do
483 ;; this, since the CLOS code is now SBCL-specific, and doesn't
484 ;; need to be ported to every buggy compiler in existence.
487 (defmacro make-method-function (method-lambda &environment env)
488 (make-method-function-internal method-lambda env))
490 (defun make-method-function-internal (method-lambda &optional env)
491 (multiple-value-bind (proto-gf proto-method)
492 (prototypes-for-make-method-lambda nil)
493 (multiple-value-bind (method-function-lambda initargs)
494 (make-method-lambda proto-gf proto-method method-lambda env)
495 (make-method-initargs-form proto-gf
497 method-function-lambda
501 (defun add-method-declarations (name qualifiers lambda-list body env)
502 (multiple-value-bind (parameters unspecialized-lambda-list specializers)
503 (parse-specialized-lambda-list lambda-list)
504 (declare (ignore parameters))
505 (multiple-value-bind (real-body declarations documentation)
506 (parse-body body env)
507 (values `(lambda ,unspecialized-lambda-list
508 ,@(when documentation `(,documentation))
509 ;; (Old PCL code used a somewhat different style of
510 ;; list for %METHOD-NAME values. Our names use
511 ;; ,@QUALIFIERS instead of ,QUALIFIERS so that the
512 ;; method names look more like what you see in a
515 ;; FIXME: As of sbcl-0.7.0.6, code elsewhere, at
516 ;; least the code to set up named BLOCKs around the
517 ;; bodies of methods, depends on the function's base
518 ;; name being the first element of the %METHOD-NAME
519 ;; list. It would be good to remove this dependency,
520 ;; perhaps by building the BLOCK here, or by using
521 ;; another declaration (e.g. %BLOCK-NAME), so that
522 ;; our method debug names are free to have any format,
523 ;; e.g. (:METHOD PRINT-OBJECT :AROUND (CLOWN T)).
525 ;; Further, as of sbcl-0.7.9.10, the code to
526 ;; implement NO-NEXT-METHOD is coupled to the form of
527 ;; this declaration; see the definition of
528 ;; CALL-NO-NEXT-METHOD (and the passing of
529 ;; METHOD-NAME-DECLARATION arguments around the
530 ;; various CALL-NEXT-METHOD logic).
531 (declare (%method-name (,name
534 (declare (%method-lambda-list ,@lambda-list))
537 unspecialized-lambda-list specializers))))
539 (defun real-make-method-initargs-form (proto-gf proto-method
540 method-lambda initargs env)
541 (declare (ignore proto-gf proto-method))
542 (unless (and (consp method-lambda)
543 (eq (car method-lambda) 'lambda))
544 (error "The METHOD-LAMBDA argument to MAKE-METHOD-FUNCTION, ~S, ~
545 is not a lambda form."
547 (make-method-initargs-form-internal method-lambda initargs env))
549 (unless (fboundp 'make-method-initargs-form)
550 (setf (gdefinition 'make-method-initargs-form)
551 (symbol-function 'real-make-method-initargs-form)))
553 (defun real-make-method-lambda (proto-gf proto-method method-lambda env)
554 (declare (ignore proto-gf proto-method))
555 (make-method-lambda-internal method-lambda env))
557 ;;; a helper function for creating Python-friendly type declarations
558 ;;; in DEFMETHOD forms
559 (defun parameter-specializer-declaration-in-defmethod (parameter specializer)
560 (cond ((and (consp specializer)
561 (eq (car specializer) 'eql))
562 ;; KLUDGE: ANSI, in its wisdom, says that
563 ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at
564 ;; DEFMETHOD expansion time. Thus, although one might think
566 ;; (DEFMETHOD FOO ((X PACKAGE)
569 ;; the PACKAGE and (EQL 12) forms are both parallel type
570 ;; names, they're not, as is made clear when you do
571 ;; (DEFMETHOD FOO ((X PACKAGE)
574 ;; where Y needs to be a symbol named "BAR", not some cons
575 ;; made by (CONS 'QUOTE 'BAR). I.e. when the
576 ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument
577 ;; to be of type (EQL X). It'd be easy to transform one to
578 ;; the other, but it'd be somewhat messier to do so while
579 ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd
580 ;; once. (The new code wouldn't be messy, but it'd require a
581 ;; big transformation of the old code.) So instead we punt.
585 ;; KLUDGE: For some low-level implementation
586 ;; classes, perhaps because of some problems related
587 ;; to the incomplete integration of PCL into SBCL's
588 ;; type system, some specializer classes can't be
589 ;; declared as argument types. E.g.
590 ;; (DEFMETHOD FOO ((X SLOT-OBJECT))
591 ;; (DECLARE (TYPE SLOT-OBJECT X))
594 ;; (DEFSTRUCT BAR A B)
596 ;; perhaps because of the way that STRUCTURE-OBJECT
597 ;; inherits both from SLOT-OBJECT and from
598 ;; SB-KERNEL:INSTANCE. In an effort to sweep such
599 ;; problems under the rug, we exclude these problem
600 ;; cases by blacklisting them here. -- WHN 2001-01-19
603 ((not (eq *boot-state* 'complete))
604 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
605 ;; types which don't match their specializers. (Specifically,
606 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
607 ;; second argument.) Hopefully it only does this kind of
608 ;; weirdness when bootstrapping.. -- WHN 20000610
611 ;; Otherwise, we can usually make Python very happy.
612 (let ((type (info :type :kind specializer)))
614 ((:primitive :defined :instance :forthcoming-defclass-type)
615 `(type ,specializer ,parameter))
617 (let ((class (find-class specializer nil)))
619 `(type ,(class-name class) ,parameter)
621 ;; we can get here, and still not have a failure
622 ;; case, by doing MOP programming like (PROGN
623 ;; (ENSURE-CLASS 'FOO) (DEFMETHOD BAR ((X FOO))
624 ;; ...)). Best to let the user know we haven't
625 ;; been able to extract enough information:
627 "~@<can't find type for presumed class ~S in ~S.~@:>"
629 'parameter-specializer-declaration-in-defmethod)
630 '(ignorable))))))))))
632 (defun make-method-lambda-internal (method-lambda &optional env)
633 (unless (and (consp method-lambda) (eq (car method-lambda) 'lambda))
634 (error "The METHOD-LAMBDA argument to MAKE-METHOD-LAMBDA, ~S, ~
635 is not a lambda form."
637 (multiple-value-bind (real-body declarations documentation)
638 (parse-body (cddr method-lambda) env)
639 (let* ((name-decl (get-declaration '%method-name declarations))
640 (sll-decl (get-declaration '%method-lambda-list declarations))
641 (method-name (when (consp name-decl) (car name-decl)))
642 (generic-function-name (when method-name (car method-name)))
643 (specialized-lambda-list (or sll-decl (cadr method-lambda))))
644 (multiple-value-bind (parameters lambda-list specializers)
645 (parse-specialized-lambda-list specialized-lambda-list)
646 (let* ((required-parameters
647 (mapcar (lambda (r s) (declare (ignore s)) r)
650 (slots (mapcar #'list required-parameters))
654 ;; These declarations seem to be used by PCL to pass
655 ;; information to itself; when I tried to delete 'em
656 ;; ca. 0.6.10 it didn't work. I'm not sure how
657 ;; they work, but note the (VAR-DECLARATION '%CLASS ..)
658 ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30
660 (mapcar (lambda (a s) (and (symbolp s)
665 ;; These TYPE declarations weren't in the original
666 ;; PCL code, but the Python compiler likes them a
667 ;; lot. (We're telling the compiler about our
668 ;; knowledge of specialized argument types so that
669 ;; it can avoid run-time type dispatch overhead,
670 ;; which can be a huge win for Python.)
672 ;; FIXME: Perhaps these belong in
673 ;; ADD-METHOD-DECLARATIONS instead of here?
674 ,@(mapcar #'parameter-specializer-declaration-in-defmethod
678 ;; Remove the documentation string and insert the
679 ;; appropriate class declarations. The documentation
680 ;; string is removed to make it easy for us to insert
681 ;; new declarations later, they will just go after the
682 ;; CADR of the method lambda. The class declarations
683 ;; are inserted to communicate the class of the method's
684 ;; arguments to the code walk.
685 `(lambda ,lambda-list
686 ;; The default ignorability of method parameters
687 ;; doesn't seem to be specified by ANSI. PCL had
688 ;; them basically ignorable but was a little
689 ;; inconsistent. E.g. even though the two
690 ;; method definitions
691 ;; (DEFMETHOD FOO ((X T) (Y T)) "Z")
692 ;; (DEFMETHOD FOO ((X T) Y) "Z")
693 ;; are otherwise equivalent, PCL treated Y as
694 ;; ignorable in the first definition but not in the
695 ;; second definition. We make all required
696 ;; parameters ignorable as a way of systematizing
697 ;; the old PCL behavior. -- WHN 2000-11-24
698 (declare (ignorable ,@required-parameters))
701 (block ,(fun-name-block-name generic-function-name)
703 (constant-value-p (and (null (cdr real-body))
704 (constantp (car real-body))))
705 (constant-value (and constant-value-p
706 (eval (car real-body))))
707 (plist (and constant-value-p
708 (or (typep constant-value
709 '(or number character))
710 (and (symbolp constant-value)
711 (symbol-package constant-value)))
712 (list :constant-value constant-value)))
713 (applyp (dolist (p lambda-list nil)
714 (cond ((memq p '(&optional &rest &key))
719 (walked-lambda call-next-method-p closurep next-method-p-p)
720 (walk-method-lambda method-lambda
725 (multiple-value-bind (walked-lambda-body
727 walked-documentation)
728 (parse-body (cddr walked-lambda) env)
729 (declare (ignore walked-documentation))
730 (when (or next-method-p-p call-next-method-p)
731 (setq plist (list* :needs-next-methods-p t plist)))
732 (when (some #'cdr slots)
733 (multiple-value-bind (slot-name-lists call-list)
734 (slot-name-lists-from-slots slots calls)
735 (let ((pv-table-symbol (make-symbol "pv-table")))
737 `(,@(when slot-name-lists
738 `(:slot-name-lists ,slot-name-lists))
740 `(:call-list ,call-list))
741 :pv-table-symbol ,pv-table-symbol
743 (setq walked-lambda-body
744 `((pv-binding (,required-parameters
747 ,@walked-lambda-body))))))
748 (when (and (memq '&key lambda-list)
749 (not (memq '&allow-other-keys lambda-list)))
750 (let ((aux (memq '&aux lambda-list)))
751 (setq lambda-list (nconc (ldiff lambda-list aux)
752 (list '&allow-other-keys)
754 (values `(lambda (.method-args. .next-methods.)
755 (simple-lexical-method-functions
756 (,lambda-list .method-args. .next-methods.
759 :next-method-p-p ,next-method-p-p
760 ;; we need to pass this along
761 ;; so that NO-NEXT-METHOD can
762 ;; be given a suitable METHOD
763 ;; argument; we need the
764 ;; QUALIFIERS and SPECIALIZERS
765 ;; inside the declaration to
766 ;; give to FIND-METHOD.
767 :method-name-declaration ,name-decl
770 ,@walked-declarations
771 ,@walked-lambda-body))
774 ,@(when documentation
775 `(:documentation ,documentation)))))))))))
777 (unless (fboundp 'make-method-lambda)
778 (setf (gdefinition 'make-method-lambda)
779 (symbol-function 'real-make-method-lambda)))
781 (defmacro simple-lexical-method-functions ((lambda-list
787 ,method-args ,next-methods
788 (bind-simple-lexical-method-macros (,method-args ,next-methods)
789 (bind-lexical-method-functions (,@lmf-options)
790 (bind-args (,lambda-list ,method-args)
793 (defmacro fast-lexical-method-functions ((lambda-list
799 `(bind-fast-lexical-method-macros (,args ,rest-arg ,next-method-call)
800 (bind-lexical-method-functions (,@lmf-options)
801 (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg)
804 (defmacro bind-simple-lexical-method-macros ((method-args next-methods)
806 `(macrolet ((call-next-method-bind (&body body)
807 `(let ((.next-method. (car ,',next-methods))
808 (,',next-methods (cdr ,',next-methods)))
809 .next-method. ,',next-methods
811 (call-next-method-body (method-name-declaration cnm-args)
813 (funcall (if (std-instance-p .next-method.)
814 (method-function .next-method.)
815 .next-method.) ; for early methods
816 (or ,cnm-args ,',method-args)
818 (apply #'call-no-next-method ',method-name-declaration
819 (or ,cnm-args ,',method-args))))
820 (next-method-p-body ()
821 `(not (null .next-method.))))
824 (defun call-no-next-method (method-name-declaration &rest args)
825 (destructuring-bind (name) method-name-declaration
826 (destructuring-bind (name &rest qualifiers-and-specializers) name
827 ;; KLUDGE: inefficient traversal, but hey. This should only
828 ;; happen on the slow error path anyway.
829 (let* ((qualifiers (butlast qualifiers-and-specializers))
830 (specializers (car (last qualifiers-and-specializers)))
831 (method (find-method (gdefinition name) qualifiers specializers)))
832 (apply #'no-next-method
833 (method-generic-function method)
837 (defstruct (method-call (:copier nil))
838 (function #'identity :type function)
841 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
843 (defmacro invoke-method-call1 (function args cm-args)
844 `(let ((.function. ,function)
846 (.cm-args. ,cm-args))
847 (if (and .cm-args. (null (cdr .cm-args.)))
848 (funcall .function. .args. (car .cm-args.))
849 (apply .function. .args. .cm-args.))))
851 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
852 `(invoke-method-call1 (method-call-function ,method-call)
854 `(list* ,@required-args+rest-arg)
855 `(list ,@required-args+rest-arg))
856 (method-call-call-method-args ,method-call)))
858 (defstruct (fast-method-call (:copier nil))
859 (function #'identity :type function)
864 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
866 (defmacro fmc-funcall (fn pv-cell next-method-call &rest args)
867 `(funcall ,fn ,pv-cell ,next-method-call ,@args))
869 (defmacro invoke-fast-method-call (method-call &rest required-args+rest-arg)
870 `(fmc-funcall (fast-method-call-function ,method-call)
871 (fast-method-call-pv-cell ,method-call)
872 (fast-method-call-next-method-call ,method-call)
873 ,@required-args+rest-arg))
875 (defstruct (fast-instance-boundp (:copier nil))
876 (index 0 :type fixnum))
878 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
880 (eval-when (:compile-toplevel :load-toplevel :execute)
881 (defvar *allow-emf-call-tracing-p* nil)
882 (defvar *enable-emf-call-tracing-p* #-sb-show nil #+sb-show t))
884 ;;;; effective method functions
886 (defvar *emf-call-trace-size* 200)
887 (defvar *emf-call-trace* nil)
888 (defvar *emf-call-trace-index* 0)
890 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
891 ;;; without explanation. It appears to be intended for debugging, so
892 ;;; it might be useful someday, so I haven't deleted it.
893 ;;; But it isn't documented and isn't used for anything now, so
894 ;;; I've conditionalized it out of the base system. -- WHN 19991213
896 (defun show-emf-call-trace ()
897 (when *emf-call-trace*
898 (let ((j *emf-call-trace-index*)
899 (*enable-emf-call-tracing-p* nil))
900 (format t "~&(The oldest entries are printed first)~%")
901 (dotimes-fixnum (i *emf-call-trace-size*)
902 (let ((ct (aref *emf-call-trace* j)))
903 (when ct (print ct)))
905 (when (= j *emf-call-trace-size*)
908 (defun trace-emf-call-internal (emf format args)
909 (unless *emf-call-trace*
910 (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
911 (setf (aref *emf-call-trace* *emf-call-trace-index*)
912 (list* emf format args))
913 (incf *emf-call-trace-index*)
914 (when (= *emf-call-trace-index* *emf-call-trace-size*)
915 (setq *emf-call-trace-index* 0)))
917 (defmacro trace-emf-call (emf format args)
918 (when *allow-emf-call-tracing-p*
919 `(when *enable-emf-call-tracing-p*
920 (trace-emf-call-internal ,emf ,format ,args))))
922 (defmacro invoke-effective-method-function-fast
923 (emf restp &rest required-args+rest-arg)
925 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
926 (invoke-fast-method-call ,emf ,@required-args+rest-arg)))
928 (defmacro invoke-effective-method-function (emf restp
929 &rest required-args+rest-arg)
930 (unless (constantp restp)
931 (error "The RESTP argument is not constant."))
932 ;; FIXME: The RESTP handling here is confusing and maybe slightly
933 ;; broken if RESTP evaluates to a non-self-evaluating form. E.g. if
934 ;; (INVOKE-EFFECTIVE-METHOD-FUNCTION EMF '(ERROR "gotcha") ...)
935 ;; then TRACE-EMF-CALL-CALL-INTERNAL might die on a gotcha error.
936 (setq restp (eval restp))
938 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
939 (cond ((typep ,emf 'fast-method-call)
940 (invoke-fast-method-call ,emf ,@required-args+rest-arg))
941 ;; "What," you may wonder, "do these next two clauses do?"
942 ;; In that case, you are not a PCL implementor, for they
943 ;; considered this to be self-documenting.:-| Or CSR, for
944 ;; that matter, since he can also figure it out by looking
945 ;; at it without breaking stride. For the rest of us,
946 ;; though: From what the code is doing with .SLOTS. and
947 ;; whatnot, evidently it's implementing SLOT-VALUEish and
948 ;; GET-SLOT-VALUEish things. Then we can reason backwards
949 ;; and conclude that setting EMF to a FIXNUM is an
950 ;; optimized way to represent these slot access operations.
951 ,@(when (and (null restp) (= 1 (length required-args+rest-arg)))
952 `(((typep ,emf 'fixnum)
953 (let* ((.slots. (get-slots-or-nil
954 ,(car required-args+rest-arg)))
955 (value (when .slots. (clos-slots-ref .slots. ,emf))))
956 (if (eq value +slot-unbound+)
957 (slot-unbound-internal ,(car required-args+rest-arg)
960 ,@(when (and (null restp) (= 2 (length required-args+rest-arg)))
961 `(((typep ,emf 'fixnum)
962 (let ((.new-value. ,(car required-args+rest-arg))
963 (.slots. (get-slots-or-nil
964 ,(cadr required-args+rest-arg))))
966 (setf (clos-slots-ref .slots. ,emf) .new-value.))))))
967 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
968 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
969 ;; there was no explanation and presumably the code is 10+
970 ;; years stale, I simply deleted it. -- WHN)
974 (invoke-method-call ,emf ,restp ,@required-args+rest-arg))
977 `(apply (the function ,emf) ,@required-args+rest-arg)
978 `(funcall (the function ,emf)
979 ,@required-args+rest-arg))))))))
981 (defun invoke-emf (emf args)
982 (trace-emf-call emf t args)
985 (let* ((arg-info (fast-method-call-arg-info emf))
986 (restp (cdr arg-info))
987 (nreq (car arg-info)))
989 (let* ((rest-args (nthcdr nreq args))
990 (req-args (ldiff args rest-args)))
991 (apply (fast-method-call-function emf)
992 (fast-method-call-pv-cell emf)
993 (fast-method-call-next-method-call emf)
994 (nconc req-args (list rest-args))))
997 (invoke-fast-method-call emf)
998 (error "wrong number of args")))
1001 (invoke-fast-method-call emf (car args))
1002 (error "wrong number of args")))
1005 (invoke-fast-method-call emf (car args) (cadr args))
1006 (error "wrong number of args")))
1008 (apply (fast-method-call-function emf)
1009 (fast-method-call-pv-cell emf)
1010 (fast-method-call-next-method-call emf)
1013 (apply (method-call-function emf)
1015 (method-call-call-method-args emf)))
1017 (cond ((null args) (error "1 or 2 args were expected."))
1019 (let* ((slots (get-slots (car args)))
1020 (value (clos-slots-ref slots emf)))
1021 (if (eq value +slot-unbound+)
1022 (slot-unbound-internal (car args) emf)
1025 (setf (clos-slots-ref (get-slots (cadr args)) emf)
1027 (t (error "1 or 2 args were expected."))))
1028 (fast-instance-boundp
1029 (if (or (null args) (cdr args))
1030 (error "1 arg was expected.")
1031 (let ((slots (get-slots (car args))))
1032 (not (eq (clos-slots-ref slots
1033 (fast-instance-boundp-index emf))
1038 (defmacro bind-fast-lexical-method-macros ((args rest-arg next-method-call)
1040 `(macrolet ((narrowed-emf (emf)
1041 ;; INVOKE-EFFECTIVE-METHOD-FUNCTION has code in it to
1042 ;; dispatch on the possibility that EMF might be of
1043 ;; type FIXNUM (as an optimized representation of a
1044 ;; slot accessor). But as far as I (WHN 2002-06-11)
1045 ;; can tell, it's impossible for such a representation
1046 ;; to end up as .NEXT-METHOD-CALL. By reassuring
1047 ;; INVOKE-E-M-F that when called from this context
1048 ;; it needn't worry about the FIXNUM case, we can
1049 ;; keep those cases from being compiled, which is
1050 ;; good both because it saves bytes and because it
1051 ;; avoids annoying type mismatch compiler warnings.
1053 ;; KLUDGE: In sbcl-0.7.4.29, the compiler's type
1054 ;; system isn't smart enough about NOT and intersection
1055 ;; types to benefit from a (NOT FIXNUM) declaration
1056 ;; here. -- WHN 2002-06-12
1058 ;; FIXME: Might the FUNCTION type be omittable here,
1059 ;; leaving only METHOD-CALLs? Failing that, could this
1060 ;; be documented somehow? (It'd be nice if the types
1061 ;; involved could be understood without solving the
1062 ;; halting problem.)
1063 `(the (or function method-call fast-method-call)
1065 (call-next-method-bind (&body body)
1067 (call-next-method-body (method-name-declaration cnm-args)
1068 `(if ,',next-method-call
1070 ;; This declaration suppresses a "deleting
1071 ;; unreachable code" note for the following IF when
1072 ;; REST-ARG is NIL. It is not nice for debugging
1073 ;; SBCL itself, but at least it keeps us from
1075 (declare (optimize (inhibit-warnings 3)))
1076 (if (and (null ',rest-arg)
1078 (eq (car cnm-args) 'list))
1079 `(invoke-effective-method-function
1080 (narrowed-emf ,',next-method-call)
1083 (let ((call `(invoke-effective-method-function
1084 (narrowed-emf ,',next-method-call)
1085 ,',(not (null rest-arg))
1087 ,@',(when rest-arg `(,rest-arg)))))
1089 (bind-args ((,@',args
1091 `(&rest ,rest-arg)))
1096 ;; As above, this declaration suppresses code
1098 (declare (optimize (inhibit-warnings 3)))
1099 (if (and (null ',rest-arg)
1101 (eq (car cnm-args) 'list))
1102 `(call-no-next-method ',method-name-declaration
1104 `(call-no-next-method ',method-name-declaration
1108 (next-method-p-body ()
1109 `(not (null ,',next-method-call))))
1112 (defmacro bind-lexical-method-functions
1113 ((&key call-next-method-p next-method-p-p
1114 closurep applyp method-name-declaration)
1116 (cond ((and (null call-next-method-p) (null next-method-p-p)
1121 `(call-next-method-bind
1122 (flet (,@(and call-next-method-p
1123 `((call-next-method (&rest cnm-args)
1124 (call-next-method-body
1125 ,method-name-declaration
1127 ,@(and next-method-p-p
1129 (next-method-p-body)))))
1132 (defmacro bind-args ((lambda-list args) &body body)
1133 (let ((args-tail '.args-tail.)
1136 (flet ((process-var (var)
1137 (if (memq var lambda-list-keywords)
1140 (&optional (setq state 'optional))
1141 (&key (setq state 'key))
1143 (&rest (setq state 'rest))
1144 (&aux (setq state 'aux))
1147 "encountered the non-standard lambda list keyword ~S"
1151 (required `((,var (pop ,args-tail))))
1152 (optional (cond ((not (consp var))
1153 `((,var (when ,args-tail
1154 (pop ,args-tail)))))
1156 `((,(car var) (if ,args-tail
1160 `((,(caddr var) ,args-tail)
1161 (,(car var) (if ,args-tail
1164 (rest `((,var ,args-tail)))
1165 (key (cond ((not (consp var))
1167 (get-key-arg-tail ,(keywordicate var)
1170 (multiple-value-bind (keyword variable)
1171 (if (consp (car var))
1174 (values (keywordicate (car var))
1176 `((,key (get-key-arg-tail ',keyword
1182 (multiple-value-bind (keyword variable)
1183 (if (consp (car var))
1186 (values (keywordicate (car var))
1188 `((,key (get-key-arg-tail ',keyword
1195 (let ((bindings (mapcan #'process-var lambda-list)))
1196 `(let* ((,args-tail ,args)
1199 ,@(when (eq state 'optional)
1200 `((unless (null ,args-tail)
1201 (error 'simple-program-error
1202 :format-control "surplus arguments: ~S"
1203 :format-arguments (list ,args-tail)))))))
1204 (declare (ignorable ,args-tail .dummy0.))
1207 (defun get-key-arg-tail (keyword list)
1208 (loop for (key . tail) on list by #'cddr
1210 ;; FIXME: Do we want to export this symbol? Or maybe use an
1211 ;; (ERROR 'SIMPLE-PROGRAM-ERROR) form?
1212 (sb-c::%odd-key-args-error)
1213 when (eq key keyword)
1216 (defun walk-method-lambda (method-lambda required-parameters env slots calls)
1217 (let ((call-next-method-p nil) ; flag indicating that CALL-NEXT-METHOD
1218 ; should be in the method definition
1219 (closurep nil) ; flag indicating that #'CALL-NEXT-METHOD
1220 ; was seen in the body of a method
1221 (next-method-p-p nil)) ; flag indicating that NEXT-METHOD-P
1222 ; should be in the method definition
1223 (flet ((walk-function (form context env)
1224 (cond ((not (eq context :eval)) form)
1225 ;; FIXME: Jumping to a conclusion from the way it's used
1226 ;; above, perhaps CONTEXT should be called SITUATION
1227 ;; (after the term used in the ANSI specification of
1228 ;; EVAL-WHEN) and given modern ANSI keyword values
1229 ;; like :LOAD-TOPLEVEL.
1230 ((not (listp form)) form)
1231 ((eq (car form) 'call-next-method)
1232 (setq call-next-method-p t)
1234 ((eq (car form) 'next-method-p)
1235 (setq next-method-p-p t)
1237 ((and (eq (car form) 'function)
1238 (cond ((eq (cadr form) 'call-next-method)
1239 (setq call-next-method-p t)
1242 ((eq (cadr form) 'next-method-p)
1243 (setq next-method-p-p t)
1247 ((and (memq (car form)
1248 '(slot-value set-slot-value slot-boundp))
1249 (constantp (caddr form)))
1250 (let ((parameter (can-optimize-access form
1253 (let ((fun (ecase (car form)
1254 (slot-value #'optimize-slot-value)
1255 (set-slot-value #'optimize-set-slot-value)
1256 (slot-boundp #'optimize-slot-boundp))))
1257 (funcall fun slots parameter form))))
1258 ((and (eq (car form) 'apply)
1260 (eq (car (cadr form)) 'function)
1261 (generic-function-name-p (cadr (cadr form))))
1262 (optimize-generic-function-call
1263 form required-parameters env slots calls))
1264 ((generic-function-name-p (car form))
1265 (optimize-generic-function-call
1266 form required-parameters env slots calls))
1269 (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1270 (values walked-lambda
1273 next-method-p-p)))))
1275 (defun generic-function-name-p (name)
1276 (and (legal-fun-name-p name)
1278 (if (eq *boot-state* 'complete)
1279 (standard-generic-function-p (gdefinition name))
1280 (funcallable-instance-p (gdefinition name)))))
1282 (defvar *method-function-plist* (make-hash-table :test 'eq))
1285 (defvar *mf1cp* nil)
1288 (defvar *mf2cp* nil)
1290 (defun method-function-plist (method-function)
1291 (unless (eq method-function *mf1*)
1292 (rotatef *mf1* *mf2*)
1293 (rotatef *mf1p* *mf2p*)
1294 (rotatef *mf1cp* *mf2cp*))
1295 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1296 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1297 (unless (eq method-function *mf1*)
1298 (setf *mf1* method-function
1300 *mf1p* (gethash method-function *method-function-plist*)))
1303 (defun (setf method-function-plist)
1304 (val method-function)
1305 (unless (eq method-function *mf1*)
1306 (rotatef *mf1* *mf2*)
1307 (rotatef *mf1cp* *mf2cp*)
1308 (rotatef *mf1p* *mf2p*))
1309 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1310 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1311 (setf *mf1* method-function
1315 (defun method-function-get (method-function key &optional default)
1316 (getf (method-function-plist method-function) key default))
1318 (defun (setf method-function-get)
1319 (val method-function key)
1320 (setf (getf (method-function-plist method-function) key) val))
1322 (defun method-function-pv-table (method-function)
1323 (method-function-get method-function :pv-table))
1325 (defun method-function-method (method-function)
1326 (method-function-get method-function :method))
1328 (defun method-function-needs-next-methods-p (method-function)
1329 (method-function-get method-function :needs-next-methods-p t))
1331 (defmacro method-function-closure-generator (method-function)
1332 `(method-function-get ,method-function 'closure-generator))
1334 (defun load-defmethod
1335 (class name quals specls ll initargs &optional pv-table-symbol)
1336 (setq initargs (copy-tree initargs))
1337 (let ((method-spec (or (getf initargs :method-spec)
1338 (make-method-spec name quals specls))))
1339 (setf (getf initargs :method-spec) method-spec)
1340 (load-defmethod-internal class name quals specls
1341 ll initargs pv-table-symbol)))
1343 (defun load-defmethod-internal
1344 (method-class gf-spec qualifiers specializers lambda-list
1345 initargs pv-table-symbol)
1346 (when pv-table-symbol
1347 (setf (getf (getf initargs :plist) :pv-table-symbol)
1349 (when (and (eq *boot-state* 'complete)
1351 (let* ((gf (fdefinition gf-spec))
1352 (method (and (generic-function-p gf)
1355 (parse-specializers specializers)
1358 (style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1359 gf-spec qualifiers specializers))))
1360 (let ((method (apply #'add-named-method
1361 gf-spec qualifiers specializers lambda-list
1362 :definition-source `((defmethod ,gf-spec
1367 (unless (or (eq method-class 'standard-method)
1368 (eq (find-class method-class nil) (class-of method)))
1369 ;; FIXME: should be STYLE-WARNING?
1370 (format *error-output*
1371 "~&At the time the method with qualifiers ~:S and~%~
1372 specializers ~:S on the generic function ~S~%~
1373 was compiled, the method-class for that generic function was~%~
1374 ~S. But, the method class is now ~S, this~%~
1375 may mean that this method was compiled improperly.~%"
1376 qualifiers specializers gf-spec
1377 method-class (class-name (class-of method))))
1380 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1381 `(method ,gf-spec ,@qualifiers ,unparsed-specializers))
1383 (defun initialize-method-function (initargs &optional return-function-p method)
1384 (let* ((mf (getf initargs :function))
1385 (method-spec (getf initargs :method-spec))
1386 (plist (getf initargs :plist))
1387 (pv-table-symbol (getf plist :pv-table-symbol))
1389 (mff (getf initargs :fast-function)))
1390 (flet ((set-mf-property (p v)
1392 (setf (method-function-get mf p) v))
1394 (setf (method-function-get mff p) v))))
1397 (setq mf (set-fun-name mf method-spec)))
1399 (let ((name `(,(or (get (car method-spec) 'fast-sym)
1400 (setf (get (car method-spec) 'fast-sym)
1401 ;; KLUDGE: If we're going to be
1402 ;; interning private symbols in our
1403 ;; a this way, it would be cleanest
1404 ;; to use a separate package
1405 ;; %PCL-PRIVATE or something, and
1406 ;; failing that, to use a special
1407 ;; symbol prefix denoting privateness.
1409 (intern (format nil "FAST-~A"
1412 ,@(cdr method-spec))))
1413 (set-fun-name mff name)
1415 (set-mf-property :name name)))))
1417 (let ((snl (getf plist :slot-name-lists))
1418 (cl (getf plist :call-list)))
1420 (setq pv-table (intern-pv-table :slot-name-lists snl
1422 (when pv-table (set pv-table-symbol pv-table))
1423 (set-mf-property :pv-table pv-table)))
1424 (loop (when (null plist) (return nil))
1425 (set-mf-property (pop plist) (pop plist)))
1427 (set-mf-property :method method))
1428 (when return-function-p
1429 (or mf (method-function-from-fast-function mff)))))))
1431 (defun analyze-lambda-list (lambda-list)
1432 (flet (;; FIXME: Is this redundant with SB-C::MAKE-KEYWORD-FOR-ARG?
1433 (parse-key-arg (arg)
1435 (if (listp (car arg))
1437 (keywordicate (car arg)))
1438 (keywordicate arg))))
1444 (allow-other-keys-p nil)
1446 (keyword-parameters ())
1448 (dolist (x lambda-list)
1449 (if (memq x lambda-list-keywords)
1451 (&optional (setq state 'optional))
1454 (&allow-other-keys (setq allow-other-keys-p t))
1455 (&rest (setq restp t
1459 (error "encountered the non-standard lambda list keyword ~S"
1462 (required (incf nrequired))
1463 (optional (incf noptional))
1464 (key (push (parse-key-arg x) keywords)
1465 (push x keyword-parameters))
1466 (rest (incf nrest)))))
1467 (when (and restp (zerop nrest))
1468 (error "Error in lambda-list:~%~
1469 After &REST, a DEFGENERIC lambda-list ~
1470 must be followed by at least one variable."))
1471 (values nrequired noptional keysp restp allow-other-keys-p
1473 (reverse keyword-parameters)))))
1475 (defun keyword-spec-name (x)
1476 (let ((key (if (atom x) x (car x))))
1481 (defun ftype-declaration-from-lambda-list (lambda-list name)
1482 (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1483 keywords keyword-parameters)
1484 (analyze-lambda-list lambda-list)
1485 (declare (ignore keyword-parameters))
1486 (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1487 (old-ftype (if (fun-type-p old) old nil))
1488 (old-restp (and old-ftype (fun-type-rest old-ftype)))
1489 (old-keys (and old-ftype
1490 (mapcar #'key-info-name
1493 (old-keysp (and old-ftype (fun-type-keyp old-ftype)))
1494 (old-allowp (and old-ftype
1495 (fun-type-allowp old-ftype)))
1496 (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1497 `(function ,(append (make-list nrequired :initial-element t)
1498 (when (plusp noptional)
1499 (append '(&optional)
1500 (make-list noptional :initial-element t)))
1501 (when (or restp old-restp)
1503 (when (or keysp old-keysp)
1505 (mapcar (lambda (key)
1508 (when (or allow-other-keys-p old-allowp)
1509 '(&allow-other-keys)))))
1512 (defun defgeneric-declaration (spec lambda-list)
1514 (setq spec (get-setf-fun-name (cadr spec))))
1515 `(ftype ,(ftype-declaration-from-lambda-list lambda-list spec) ,spec))
1517 ;;;; early generic function support
1519 (defvar *!early-generic-functions* ())
1521 (defun ensure-generic-function (fun-name
1525 (declare (ignore environment))
1526 (let ((existing (and (gboundp fun-name)
1527 (gdefinition fun-name))))
1529 (eq *boot-state* 'complete)
1530 (null (generic-function-p existing)))
1531 (generic-clobbers-function fun-name)
1532 (apply #'ensure-generic-function-using-class
1533 existing fun-name all-keys))))
1535 (defun generic-clobbers-function (fun-name)
1536 (error 'simple-program-error
1537 :format-control "~S already names an ordinary function or a macro."
1538 :format-arguments (list fun-name)))
1540 (defvar *sgf-wrapper*
1541 (boot-make-wrapper (early-class-size 'standard-generic-function)
1542 'standard-generic-function))
1544 (defvar *sgf-slots-init*
1545 (mapcar (lambda (canonical-slot)
1546 (if (memq (getf canonical-slot :name) '(arg-info source))
1548 (let ((initfunction (getf canonical-slot :initfunction)))
1550 (funcall initfunction)
1552 (early-collect-inheritance 'standard-generic-function)))
1554 (defvar *sgf-method-class-index*
1555 (!bootstrap-slot-index 'standard-generic-function 'method-class))
1557 (defun early-gf-p (x)
1558 (and (fsc-instance-p x)
1559 (eq (clos-slots-ref (get-slots x) *sgf-method-class-index*)
1562 (defvar *sgf-methods-index*
1563 (!bootstrap-slot-index 'standard-generic-function 'methods))
1565 (defmacro early-gf-methods (gf)
1566 `(clos-slots-ref (get-slots ,gf) *sgf-methods-index*))
1568 (defvar *sgf-arg-info-index*
1569 (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1571 (defmacro early-gf-arg-info (gf)
1572 `(clos-slots-ref (get-slots ,gf) *sgf-arg-info-index*))
1574 (defvar *sgf-dfun-state-index*
1575 (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1577 (defstruct (arg-info
1579 (:constructor make-arg-info ())
1581 (arg-info-lambda-list :no-lambda-list)
1584 arg-info-number-optional
1586 arg-info-keys ;nil no &KEY or &REST allowed
1587 ;(k1 k2 ..) Each method must accept these &KEY arguments.
1588 ;T must have &KEY or &REST
1590 gf-info-simple-accessor-type ; nil, reader, writer, boundp
1591 (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1593 gf-info-static-c-a-m-emf
1594 (gf-info-c-a-m-emf-std-p t)
1597 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1599 (defun arg-info-valid-p (arg-info)
1600 (not (null (arg-info-number-optional arg-info))))
1602 (defun arg-info-applyp (arg-info)
1603 (or (plusp (arg-info-number-optional arg-info))
1604 (arg-info-key/rest-p arg-info)))
1606 (defun arg-info-number-required (arg-info)
1607 (length (arg-info-metatypes arg-info)))
1609 (defun arg-info-nkeys (arg-info)
1610 (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1612 ;;; Keep pages clean by not setting if the value is already the same.
1613 (defmacro esetf (pos val)
1614 (with-unique-names (valsym)
1615 `(let ((,valsym ,val))
1616 (unless (equal ,pos ,valsym)
1617 (setf ,pos ,valsym)))))
1619 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1620 argument-precedence-order)
1621 (let* ((arg-info (if (eq *boot-state* 'complete)
1623 (early-gf-arg-info gf)))
1624 (methods (if (eq *boot-state* 'complete)
1625 (generic-function-methods gf)
1626 (early-gf-methods gf)))
1627 (was-valid-p (integerp (arg-info-number-optional arg-info)))
1628 (first-p (and new-method (null (cdr methods)))))
1629 (when (and (not lambda-list-p) methods)
1630 (setq lambda-list (gf-lambda-list gf)))
1631 (when (or lambda-list-p
1633 (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1634 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1635 (analyze-lambda-list lambda-list)
1636 (when (and methods (not first-p))
1637 (let ((gf-nreq (arg-info-number-required arg-info))
1638 (gf-nopt (arg-info-number-optional arg-info))
1639 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1640 (unless (and (= nreq gf-nreq)
1642 (eq (or keysp restp) gf-key/rest-p))
1643 (error "The lambda-list ~S is incompatible with ~
1644 existing methods of ~S."
1647 (esetf (arg-info-lambda-list arg-info) lambda-list))
1648 (when (or lambda-list-p argument-precedence-order
1649 (null (arg-info-precedence arg-info)))
1650 (esetf (arg-info-precedence arg-info)
1651 (compute-precedence lambda-list nreq
1652 argument-precedence-order)))
1653 (esetf (arg-info-metatypes arg-info) (make-list nreq))
1654 (esetf (arg-info-number-optional arg-info) nopt)
1655 (esetf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1656 (esetf (arg-info-keys arg-info)
1658 (if allow-other-keys-p t keywords)
1659 (arg-info-key/rest-p arg-info)))))
1661 (check-method-arg-info gf arg-info new-method))
1662 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1665 (defun check-method-arg-info (gf arg-info method)
1666 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1667 (analyze-lambda-list (if (consp method)
1668 (early-method-lambda-list method)
1669 (method-lambda-list method)))
1670 (flet ((lose (string &rest args)
1671 (error 'simple-program-error
1672 :format-control "~@<attempt to add the method~2I~_~S~I~_~
1673 to the generic function~2I~_~S;~I~_~
1675 :format-arguments (list method gf string args)))
1676 (comparison-description (x y)
1677 (if (> x y) "more" "fewer")))
1678 (let ((gf-nreq (arg-info-number-required arg-info))
1679 (gf-nopt (arg-info-number-optional arg-info))
1680 (gf-key/rest-p (arg-info-key/rest-p arg-info))
1681 (gf-keywords (arg-info-keys arg-info)))
1682 (unless (= nreq gf-nreq)
1684 "the method has ~A required arguments than the generic function."
1685 (comparison-description nreq gf-nreq)))
1686 (unless (= nopt gf-nopt)
1688 "the method has ~A optional arguments than the generic function."
1689 (comparison-description nopt gf-nopt)))
1690 (unless (eq (or keysp restp) gf-key/rest-p)
1692 "the method and generic function differ in whether they accept~_~
1693 &REST or &KEY arguments."))
1694 (when (consp gf-keywords)
1695 (unless (or (and restp (not keysp))
1697 (every (lambda (k) (memq k keywords)) gf-keywords))
1698 (lose "the method does not accept each of the &KEY arguments~2I~_~
1702 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1703 (let* ((existing-p (and methods (cdr methods) new-method))
1704 (nreq (length (arg-info-metatypes arg-info)))
1705 (metatypes (if existing-p
1706 (arg-info-metatypes arg-info)
1708 (type (if existing-p
1709 (gf-info-simple-accessor-type arg-info)
1711 (when (arg-info-valid-p arg-info)
1712 (dolist (method (if new-method (list new-method) methods))
1713 (let* ((specializers (if (or (eq *boot-state* 'complete)
1714 (not (consp method)))
1715 (method-specializers method)
1716 (early-method-specializers method t)))
1717 (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1719 (early-method-class method)))
1720 (new-type (when (and class
1721 (or (not (eq *boot-state* 'complete))
1722 (eq (generic-function-method-combination gf)
1723 *standard-method-combination*)))
1724 (cond ((eq class *the-class-standard-reader-method*)
1726 ((eq class *the-class-standard-writer-method*)
1728 ((eq class *the-class-standard-boundp-method*)
1730 (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1731 (setq type (cond ((null type) new-type)
1732 ((eq type new-type) type)
1734 (esetf (arg-info-metatypes arg-info) metatypes)
1735 (esetf (gf-info-simple-accessor-type arg-info) type)))
1736 (when (or (not was-valid-p) first-p)
1737 (multiple-value-bind (c-a-m-emf std-p)
1740 (compute-applicable-methods-emf gf))
1741 (esetf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1742 (esetf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1743 (unless (gf-info-c-a-m-emf-std-p arg-info)
1744 (esetf (gf-info-simple-accessor-type arg-info) t))))
1746 (let ((name (if (eq *boot-state* 'complete)
1747 (generic-function-name gf)
1748 (!early-gf-name gf))))
1749 (esetf (gf-precompute-dfun-and-emf-p arg-info)
1753 *internal-pcl-generalized-fun-name-symbols*))
1755 (t (let* ((symbol (fun-name-block-name name))
1756 (package (symbol-package symbol)))
1757 (and (or (eq package *pcl-package*)
1758 (memq package (package-use-list *pcl-package*)))
1759 ;; FIXME: this test will eventually be
1760 ;; superseded by the *internal-pcl...* test,
1761 ;; above. While we are in a process of
1762 ;; transition, however, it should probably
1764 (not (find #\Space (symbol-name symbol))))))))))
1765 (esetf (gf-info-fast-mf-p arg-info)
1766 (or (not (eq *boot-state* 'complete))
1767 (let* ((method-class (generic-function-method-class gf))
1768 (methods (compute-applicable-methods
1769 #'make-method-lambda
1770 (list gf (class-prototype method-class)
1772 (and methods (null (cdr methods))
1773 (let ((specls (method-specializers (car methods))))
1774 (and (classp (car specls))
1775 (eq 'standard-generic-function
1776 (class-name (car specls)))
1777 (classp (cadr specls))
1778 (eq 'standard-method
1779 (class-name (cadr specls)))))))))
1782 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
1784 ;;; The STATIC-SLOTS field of the funcallable instances used as early
1785 ;;; generic functions is used to store the early methods and early
1786 ;;; discriminator code for the early generic function. The static
1787 ;;; slots field of the fins contains a list whose:
1788 ;;; CAR - a list of the early methods on this early gf
1789 ;;; CADR - the early discriminator code for this method
1790 (defun ensure-generic-function-using-class (existing spec &rest keys
1791 &key (lambda-list nil
1793 argument-precedence-order
1795 (declare (ignore keys))
1796 (cond ((and existing (early-gf-p existing))
1798 ((assoc spec *!generic-function-fixups* :test #'equal)
1800 (make-early-gf spec lambda-list lambda-list-p existing
1801 argument-precedence-order)
1802 (error "The function ~S is not already defined." spec)))
1804 (error "~S should be on the list ~S."
1806 '*!generic-function-fixups*))
1808 (pushnew spec *!early-generic-functions* :test #'equal)
1809 (make-early-gf spec lambda-list lambda-list-p nil
1810 argument-precedence-order))))
1812 (defun make-early-gf (spec &optional lambda-list lambda-list-p
1813 function argument-precedence-order)
1814 (let ((fin (allocate-funcallable-instance *sgf-wrapper* *sgf-slots-init*)))
1815 (set-funcallable-instance-function
1818 (if (eq spec 'print-object)
1819 #'(instance-lambda (instance stream)
1820 (print-unreadable-object (instance stream :identity t)
1821 (format stream "std-instance")))
1822 #'(instance-lambda (&rest args)
1823 (declare (ignore args))
1824 (error "The function of the funcallable-instance ~S~
1825 has not been set." fin)))))
1826 (setf (gdefinition spec) fin)
1827 (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
1828 (!bootstrap-set-slot 'standard-generic-function
1832 (set-fun-name fin spec)
1833 (let ((arg-info (make-arg-info)))
1834 (setf (early-gf-arg-info fin) arg-info)
1836 (proclaim (defgeneric-declaration spec lambda-list))
1837 (if argument-precedence-order
1839 :lambda-list lambda-list
1840 :argument-precedence-order argument-precedence-order)
1841 (set-arg-info fin :lambda-list lambda-list))))
1844 (defun set-dfun (gf &optional dfun cache info)
1846 (setf (cache-owner cache) gf))
1847 (let ((new-state (if (and dfun (or cache info))
1848 (list* dfun cache info)
1850 (if (eq *boot-state* 'complete)
1851 (setf (gf-dfun-state gf) new-state)
1852 (setf (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*)
1856 (defun gf-dfun-cache (gf)
1857 (let ((state (if (eq *boot-state* 'complete)
1859 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1862 (cons (cadr state)))))
1864 (defun gf-dfun-info (gf)
1865 (let ((state (if (eq *boot-state* 'complete)
1867 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1870 (cons (cddr state)))))
1872 (defvar *sgf-name-index*
1873 (!bootstrap-slot-index 'standard-generic-function 'name))
1875 (defun !early-gf-name (gf)
1876 (clos-slots-ref (get-slots gf) *sgf-name-index*))
1878 (defun gf-lambda-list (gf)
1879 (let ((arg-info (if (eq *boot-state* 'complete)
1881 (early-gf-arg-info gf))))
1882 (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
1883 (let ((methods (if (eq *boot-state* 'complete)
1884 (generic-function-methods gf)
1885 (early-gf-methods gf))))
1888 (warn "no way to determine the lambda list for ~S" gf)
1890 (let* ((method (car (last methods)))
1891 (ll (if (consp method)
1892 (early-method-lambda-list method)
1893 (method-lambda-list method)))
1894 (k (member '&key ll)))
1896 (append (ldiff ll (cdr k)) '(&allow-other-keys))
1898 (arg-info-lambda-list arg-info))))
1900 (defmacro real-ensure-gf-internal (gf-class all-keys env)
1902 (cond ((symbolp ,gf-class)
1903 (setq ,gf-class (find-class ,gf-class t ,env)))
1904 ((classp ,gf-class))
1906 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
1907 class nor a symbol that names a class."
1909 (remf ,all-keys :generic-function-class)
1910 (remf ,all-keys :environment)
1911 (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
1912 (unless (eq combin '.shes-not-there.)
1913 (setf (getf ,all-keys :method-combination)
1914 (find-method-combination (class-prototype ,gf-class)
1917 (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
1918 (unless (eq method-class '.shes-not-there.)
1919 (setf (getf ,all-keys :method-class)
1920 (find-class method-class t ,env))))))
1922 (defun real-ensure-gf-using-class--generic-function
1926 &key environment (lambda-list nil lambda-list-p)
1927 (generic-function-class 'standard-generic-function gf-class-p)
1929 (real-ensure-gf-internal generic-function-class all-keys environment)
1930 (unless (or (null gf-class-p)
1931 (eq (class-of existing) generic-function-class))
1932 (change-class existing generic-function-class))
1934 (apply #'reinitialize-instance existing all-keys)
1936 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1938 (defun real-ensure-gf-using-class--null
1942 &key environment (lambda-list nil lambda-list-p)
1943 (generic-function-class 'standard-generic-function)
1945 (declare (ignore existing))
1946 (real-ensure-gf-internal generic-function-class all-keys environment)
1948 (setf (gdefinition fun-name)
1949 (apply #'make-instance generic-function-class
1950 :name fun-name all-keys))
1952 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1954 (defun get-generic-fun-info (gf)
1955 ;; values nreq applyp metatypes nkeys arg-info
1956 (multiple-value-bind (applyp metatypes arg-info)
1957 (let* ((arg-info (if (early-gf-p gf)
1958 (early-gf-arg-info gf)
1960 (metatypes (arg-info-metatypes arg-info)))
1961 (values (arg-info-applyp arg-info)
1964 (values (length metatypes) applyp metatypes
1965 (count-if (lambda (x) (neq x t)) metatypes)
1968 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
1969 &optional slot-name)
1970 (initialize-method-function initargs)
1973 ;; Figure out whether we got class objects or class names as the
1974 ;; specializers and set parsed and unparsed appropriately. If we
1975 ;; got class objects, then we can compute unparsed, but if we got
1976 ;; class names we don't try to compute parsed.
1978 ;; Note that the use of not symbolp in this call to every should be
1979 ;; read as 'classp' we can't use classp itself because it doesn't
1981 (if (every (lambda (s) (not (symbolp s))) specializers)
1982 (setq parsed specializers
1983 unparsed (mapcar (lambda (s)
1984 (if (eq s t) t (class-name s)))
1986 (setq unparsed specializers
1988 (list :early-method ;This is an early method dammit!
1990 (getf initargs :function)
1991 (getf initargs :fast-function)
1993 parsed ;The parsed specializers. This is used
1994 ;by early-method-specializers to cache
1995 ;the parse. Note that this only comes
1996 ;into play when there is more than one
1997 ;early method on an early gf.
1999 (list class ;A list to which real-make-a-method
2000 qualifiers ;can be applied to make a real method
2001 arglist ;corresponding to this early one.
2007 (defun real-make-a-method
2008 (class qualifiers lambda-list specializers initargs doc
2009 &optional slot-name)
2010 (setq specializers (parse-specializers specializers))
2011 (apply #'make-instance class
2012 :qualifiers qualifiers
2013 :lambda-list lambda-list
2014 :specializers specializers
2016 :slot-name slot-name
2020 (defun early-method-function (early-method)
2021 (values (cadr early-method) (caddr early-method)))
2023 (defun early-method-class (early-method)
2024 (find-class (car (fifth early-method))))
2026 (defun early-method-standard-accessor-p (early-method)
2027 (let ((class (first (fifth early-method))))
2028 (or (eq class 'standard-reader-method)
2029 (eq class 'standard-writer-method)
2030 (eq class 'standard-boundp-method))))
2032 (defun early-method-standard-accessor-slot-name (early-method)
2033 (seventh (fifth early-method)))
2035 ;;; Fetch the specializers of an early method. This is basically just
2036 ;;; a simple accessor except that when the second argument is t, this
2037 ;;; converts the specializers from symbols into class objects. The
2038 ;;; class objects are cached in the early method, this makes
2039 ;;; bootstrapping faster because the class objects only have to be
2043 ;;; The second argument should only be passed as T by
2044 ;;; early-lookup-method. This is to implement the rule that only when
2045 ;;; there is more than one early method on a generic function is the
2046 ;;; conversion from class names to class objects done. This
2047 ;;; corresponds to the fact that we are only allowed to have one
2048 ;;; method on any generic function up until the time classes exist.
2049 (defun early-method-specializers (early-method &optional objectsp)
2050 (if (and (listp early-method)
2051 (eq (car early-method) :early-method))
2052 (cond ((eq objectsp t)
2053 (or (fourth early-method)
2054 (setf (fourth early-method)
2055 (mapcar #'find-class (cadddr (fifth early-method))))))
2057 (cadddr (fifth early-method))))
2058 (error "~S is not an early-method." early-method)))
2060 (defun early-method-qualifiers (early-method)
2061 (cadr (fifth early-method)))
2063 (defun early-method-lambda-list (early-method)
2064 (caddr (fifth early-method)))
2066 (defun early-add-named-method (generic-function-name
2071 (let* ((gf (ensure-generic-function generic-function-name))
2073 (dolist (m (early-gf-methods gf))
2074 (when (and (equal (early-method-specializers m) specializers)
2075 (equal (early-method-qualifiers m) qualifiers))
2077 (new (make-a-method 'standard-method
2083 (when existing (remove-method gf existing))
2084 (add-method gf new)))
2086 ;;; This is the early version of ADD-METHOD. Later this will become a
2087 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2088 ;;; special knowledge about ADD-METHOD.
2089 (defun add-method (generic-function method)
2090 (when (not (fsc-instance-p generic-function))
2091 (error "Early ADD-METHOD didn't get a funcallable instance."))
2092 (when (not (and (listp method) (eq (car method) :early-method)))
2093 (error "Early ADD-METHOD didn't get an early method."))
2094 (push method (early-gf-methods generic-function))
2095 (set-arg-info generic-function :new-method method)
2096 (unless (assoc (!early-gf-name generic-function)
2097 *!generic-function-fixups*
2099 (update-dfun generic-function)))
2101 ;;; This is the early version of REMOVE-METHOD. See comments on
2102 ;;; the early version of ADD-METHOD.
2103 (defun remove-method (generic-function method)
2104 (when (not (fsc-instance-p generic-function))
2105 (error "An early remove-method didn't get a funcallable instance."))
2106 (when (not (and (listp method) (eq (car method) :early-method)))
2107 (error "An early remove-method didn't get an early method."))
2108 (setf (early-gf-methods generic-function)
2109 (remove method (early-gf-methods generic-function)))
2110 (set-arg-info generic-function)
2111 (unless (assoc (!early-gf-name generic-function)
2112 *!generic-function-fixups*
2114 (update-dfun generic-function)))
2116 ;;; This is the early version of GET-METHOD. See comments on the early
2117 ;;; version of ADD-METHOD.
2118 (defun get-method (generic-function qualifiers specializers
2119 &optional (errorp t))
2120 (if (early-gf-p generic-function)
2121 (or (dolist (m (early-gf-methods generic-function))
2122 (when (and (or (equal (early-method-specializers m nil)
2124 (equal (early-method-specializers m t)
2126 (equal (early-method-qualifiers m) qualifiers))
2129 (error "can't get early method")
2131 (real-get-method generic-function qualifiers specializers errorp)))
2133 (defun !fix-early-generic-functions ()
2134 (let ((accessors nil))
2135 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2136 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2137 (dolist (early-gf-spec *!early-generic-functions*)
2138 (when (every #'early-method-standard-accessor-p
2139 (early-gf-methods (gdefinition early-gf-spec)))
2140 (push early-gf-spec accessors)))
2141 (dolist (spec (nconc accessors
2142 '(accessor-method-slot-name
2143 generic-function-methods
2148 slot-definition-location
2149 slot-definition-name
2152 class-precedence-list
2153 slot-boundp-using-class
2154 (setf slot-value-using-class)
2155 slot-value-using-class
2158 funcallable-standard-class-p
2161 (setq *!early-generic-functions*
2163 (delete spec *!early-generic-functions* :test #'equal))))
2165 (dolist (early-gf-spec *!early-generic-functions*)
2166 (/show early-gf-spec)
2167 (let* ((gf (gdefinition early-gf-spec))
2168 (methods (mapcar (lambda (early-method)
2169 (let ((args (copy-list (fifth
2172 (early-method-specializers
2174 (apply #'real-make-a-method args)))
2175 (early-gf-methods gf))))
2176 (setf (generic-function-method-class gf) *the-class-standard-method*)
2177 (setf (generic-function-method-combination gf)
2178 *standard-method-combination*)
2179 (set-methods gf methods)))
2181 (dolist (fn *!early-functions*)
2183 (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2185 (dolist (fixup *!generic-function-fixups*)
2187 (let* ((fspec (car fixup))
2188 (gf (gdefinition fspec))
2189 (methods (mapcar (lambda (method)
2190 (let* ((lambda-list (first method))
2191 (specializers (second method))
2192 (method-fn-name (third method))
2193 (fn-name (or method-fn-name fspec))
2194 (fn (fdefinition fn-name))
2198 (lambda (args next-methods)
2202 `(call ,fn-name)))))
2203 (declare (type function fn))
2204 (make-a-method 'standard-method
2211 (setf (generic-function-method-class gf) *the-class-standard-method*)
2212 (setf (generic-function-method-combination gf)
2213 *standard-method-combination*)
2214 (set-methods gf methods))))
2215 (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2217 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2218 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2219 ;;; is really implemented.
2220 (defun parse-defmethod (cdr-of-form)
2221 (declare (list cdr-of-form))
2222 (let ((name (pop cdr-of-form))
2225 (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2226 (push (pop cdr-of-form) qualifiers)
2227 (return (setq qualifiers (nreverse qualifiers)))))
2228 (setq spec-ll (pop cdr-of-form))
2229 (values name qualifiers spec-ll cdr-of-form)))
2231 (defun parse-specializers (specializers)
2232 (declare (list specializers))
2233 (flet ((parse (spec)
2234 (let ((result (specializer-from-type spec)))
2235 (if (specializerp result)
2238 (error "~S was used as a specializer,~%~
2239 but is not the name of a class."
2241 (error "~S is not a legal specializer." spec))))))
2242 (mapcar #'parse specializers)))
2244 (defun unparse-specializers (specializers-or-method)
2245 (if (listp specializers-or-method)
2246 (flet ((unparse (spec)
2247 (if (specializerp spec)
2248 (let ((type (specializer-type spec)))
2249 (if (and (consp type)
2250 (eq (car type) 'class))
2251 (let* ((class (cadr type))
2252 (class-name (class-name class)))
2253 (if (eq class (find-class class-name nil))
2257 (error "~S is not a legal specializer." spec))))
2258 (mapcar #'unparse specializers-or-method))
2259 (unparse-specializers (method-specializers specializers-or-method))))
2261 (defun parse-method-or-spec (spec &optional (errorp t))
2262 (let (gf method name temp)
2265 gf (method-generic-function method)
2266 temp (and gf (generic-function-name gf))
2269 (make-method-spec temp
2270 (method-qualifiers method)
2271 (unparse-specializers
2272 (method-specializers method))))
2273 (make-symbol (format nil "~S" method))))
2274 (multiple-value-bind (gf-spec quals specls)
2275 (parse-defmethod spec)
2276 (and (setq gf (and (or errorp (gboundp gf-spec))
2277 (gdefinition gf-spec)))
2278 (let ((nreq (compute-discriminating-function-arglist-info gf)))
2279 (setq specls (append (parse-specializers specls)
2280 (make-list (- nreq (length specls))
2284 (setq method (get-method gf quals specls errorp))
2286 (intern-fun-name (make-method-spec gf-spec
2289 (values gf method name)))
2291 (defun extract-parameters (specialized-lambda-list)
2292 (multiple-value-bind (parameters ignore1 ignore2)
2293 (parse-specialized-lambda-list specialized-lambda-list)
2294 (declare (ignore ignore1 ignore2))
2297 (defun extract-lambda-list (specialized-lambda-list)
2298 (multiple-value-bind (ignore1 lambda-list ignore2)
2299 (parse-specialized-lambda-list specialized-lambda-list)
2300 (declare (ignore ignore1 ignore2))
2303 (defun extract-specializer-names (specialized-lambda-list)
2304 (multiple-value-bind (ignore1 ignore2 specializers)
2305 (parse-specialized-lambda-list specialized-lambda-list)
2306 (declare (ignore ignore1 ignore2))
2309 (defun extract-required-parameters (specialized-lambda-list)
2310 (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2311 (parse-specialized-lambda-list specialized-lambda-list)
2312 (declare (ignore ignore1 ignore2 ignore3))
2313 required-parameters))
2315 (defun parse-specialized-lambda-list (arglist &optional post-keyword)
2316 ;;(declare (values parameters lambda-list specializers required-parameters))
2317 (let ((arg (car arglist)))
2318 (cond ((null arglist) (values nil nil nil nil))
2320 (values nil arglist nil))
2321 ((memq arg lambda-list-keywords)
2322 (unless (memq arg '(&optional &rest &key &allow-other-keys &aux))
2323 ;; Now, since we try to conform to ANSI, non-standard
2324 ;; lambda-list-keywords should be treated as errors.
2325 (error 'simple-program-error
2326 :format-control "unrecognized lambda-list keyword ~S ~
2328 :format-arguments (list arg)))
2329 ;; When we are at a lambda-list keyword, the parameters
2330 ;; don't include the lambda-list keyword; the lambda-list
2331 ;; does include the lambda-list keyword; and no
2332 ;; specializers are allowed to follow the lambda-list
2333 ;; keywords (at least for now).
2334 (multiple-value-bind (parameters lambda-list)
2335 (parse-specialized-lambda-list (cdr arglist) t)
2336 (when (eq arg '&rest)
2337 ;; check, if &rest is followed by a var ...
2338 (when (or (null lambda-list)
2339 (memq (car lambda-list) lambda-list-keywords))
2340 (error "Error in lambda-list:~%~
2341 After &REST, a DEFMETHOD lambda-list ~
2342 must be followed by at least one variable.")))
2344 (cons arg lambda-list)
2348 ;; After a lambda-list keyword there can be no specializers.
2349 (multiple-value-bind (parameters lambda-list)
2350 (parse-specialized-lambda-list (cdr arglist) t)
2351 (values (cons (if (listp arg) (car arg) arg) parameters)
2352 (cons arg lambda-list)
2356 (multiple-value-bind (parameters lambda-list specializers required)
2357 (parse-specialized-lambda-list (cdr arglist))
2358 (values (cons (if (listp arg) (car arg) arg) parameters)
2359 (cons (if (listp arg) (car arg) arg) lambda-list)
2360 (cons (if (listp arg) (cadr arg) t) specializers)
2361 (cons (if (listp arg) (car arg) arg) required)))))))
2363 (setq *boot-state* 'early)
2365 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2366 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2367 ;;; walker stuff was only used for implementing stuff like that; maybe
2368 ;;; it's not needed any more? Hunt down what it was used for and see.
2370 (defmacro with-slots (slots instance &body body)
2371 (let ((in (gensym)))
2372 `(let ((,in ,instance))
2373 (declare (ignorable ,in))
2374 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2377 (and (symbolp instance)
2378 `((declare (%variable-rebinding ,in ,instance)))))
2380 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2382 (if (symbolp slot-entry)
2386 (if (symbolp slot-entry)
2388 (cadr slot-entry))))
2390 (slot-value ,in ',slot-name))))
2394 (defmacro with-accessors (slots instance &body body)
2395 (let ((in (gensym)))
2396 `(let ((,in ,instance))
2397 (declare (ignorable ,in))
2398 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2401 (and (symbolp instance)
2402 `((declare (%variable-rebinding ,in ,instance)))))
2404 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2405 (let ((var-name (car slot-entry))
2406 (accessor-name (cadr slot-entry)))
2407 `(,var-name (,accessor-name ,in))))