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
231 (sb-c:source-location) ,@initargs)
232 ,@(mapcar #'expand-method-definition methods)
233 (fdefinition ',fun-name)))))
235 (defun compile-or-load-defgeneric (fun-name)
236 (proclaim-as-fun-name fun-name)
237 (note-name-defined fun-name :function)
238 (unless (eq (info :function :where-from fun-name) :declared)
239 (setf (info :function :where-from fun-name) :defined)
240 (setf (info :function :type fun-name)
241 (specifier-type 'function))))
243 (defun load-defgeneric (fun-name lambda-list source-location &rest initargs)
244 (when (fboundp fun-name)
245 (style-warn "redefining ~S in DEFGENERIC" fun-name)
246 (let ((fun (fdefinition fun-name)))
247 (when (generic-function-p fun)
248 (loop for method in (generic-function-initial-methods fun)
249 do (remove-method fun method))
250 (setf (generic-function-initial-methods fun) '()))))
251 (apply #'ensure-generic-function
253 :lambda-list lambda-list
254 :definition-source source-location
257 (define-condition generic-function-lambda-list-error
258 (reference-condition simple-program-error)
260 (:default-initargs :references (list '(:ansi-cl :section (3 4 2)))))
262 (defun check-gf-lambda-list (lambda-list)
263 (flet ((ensure (arg ok)
265 (error 'generic-function-lambda-list-error
267 "~@<invalid ~S ~_in the generic function lambda list ~S~:>"
268 :format-arguments (list arg lambda-list)))))
269 (multiple-value-bind (required optional restp rest keyp keys allowp
270 auxp aux morep more-context more-count)
271 (parse-lambda-list lambda-list)
272 (declare (ignore required)) ; since they're no different in a gf ll
273 (declare (ignore restp rest)) ; since they're no different in a gf ll
274 (declare (ignore allowp)) ; since &ALLOW-OTHER-KEYS is fine either way
275 (declare (ignore aux)) ; since we require AUXP=NIL
276 (declare (ignore more-context more-count)) ; safely ignored unless MOREP
277 ;; no defaults allowed for &OPTIONAL arguments
279 (ensure i (or (symbolp i)
280 (and (consp i) (symbolp (car i)) (null (cdr i))))))
281 ;; no defaults allowed for &KEY arguments
284 (ensure i (or (symbolp i)
286 (or (symbolp (car i))
294 (error "&AUX is not allowed in a generic function lambda list: ~S"
296 ;; Oh, *puhlease*... not specifically as per section 3.4.2 of
297 ;; the ANSI spec, but the CMU CL &MORE extension does not
299 (aver (not morep)))))
301 (defmacro defmethod (&rest args &environment env)
302 (multiple-value-bind (name qualifiers lambda-list body)
303 (parse-defmethod args)
304 (multiple-value-bind (proto-gf proto-method)
305 (prototypes-for-make-method-lambda name)
306 (expand-defmethod name
314 (defun prototypes-for-make-method-lambda (name)
315 (if (not (eq *boot-state* 'complete))
317 (let ((gf? (and (fboundp name)
318 (gdefinition name))))
320 (not (generic-function-p gf?)))
321 (values (class-prototype (find-class 'standard-generic-function))
322 (class-prototype (find-class 'standard-method)))
324 (class-prototype (or (generic-function-method-class gf?)
325 (find-class 'standard-method))))))))
327 ;;; Take a name which is either a generic function name or a list specifying
328 ;;; a SETF generic function (like: (SETF <generic-function-name>)). Return
329 ;;; the prototype instance of the method-class for that generic function.
331 ;;; If there is no generic function by that name, this returns the
332 ;;; default value, the prototype instance of the class
333 ;;; STANDARD-METHOD. This default value is also returned if the spec
334 ;;; names an ordinary function or even a macro. In effect, this leaves
335 ;;; the signalling of the appropriate error until load time.
337 ;;; Note: During bootstrapping, this function is allowed to return NIL.
338 (defun method-prototype-for-gf (name)
339 (let ((gf? (and (fboundp name)
340 (gdefinition name))))
341 (cond ((neq *boot-state* 'complete) nil)
343 (not (generic-function-p gf?))) ; Someone else MIGHT
344 ; error at load time.
345 (class-prototype (find-class 'standard-method)))
347 (class-prototype (or (generic-function-method-class gf?)
348 (find-class 'standard-method)))))))
350 (defun expand-defmethod (name
357 (multiple-value-bind (method-lambda unspecialized-lambda-list specializers)
358 (add-method-declarations name qualifiers lambda-list body env)
359 (multiple-value-bind (method-function-lambda initargs)
360 (make-method-lambda proto-gf proto-method method-lambda env)
361 (let ((initargs-form (make-method-initargs-form proto-gf
363 method-function-lambda
367 ;; Note: We could DECLAIM the ftype of the generic function
368 ;; here, since ANSI specifies that we create it if it does
369 ;; not exist. However, I chose not to, because I think it's
370 ;; more useful to support a style of programming where every
371 ;; generic function has an explicit DEFGENERIC and any typos
372 ;; in DEFMETHODs are warned about. Otherwise
374 ;; (DEFGENERIC FOO-BAR-BLETCH ((X T)))
375 ;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
376 ;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
377 ;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
378 ;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
379 ;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
381 ;; compiles without raising an error and runs without
382 ;; raising an error (since SIMPLE-VECTOR cases fall through
383 ;; to VECTOR) but still doesn't do what was intended. I hate
384 ;; that kind of bug (code which silently gives the wrong
385 ;; answer), so we don't do a DECLAIM here. -- WHN 20000229
386 ,(make-defmethod-form name qualifiers specializers
387 unspecialized-lambda-list
389 (class-name (class-of proto-method))
393 (defun interned-symbol-p (x)
394 (and (symbolp x) (symbol-package x)))
396 (defun make-defmethod-form
397 (name qualifiers specializers unspecialized-lambda-list
398 method-class-name initargs-form)
401 (if (and (interned-symbol-p (fun-name-block-name name))
402 (every #'interned-symbol-p qualifiers)
405 (and (eq (car s) 'eql)
407 (let ((sv (constant-form-value (cadr s))))
408 (or (interned-symbol-p sv)
411 (standard-char-p sv)))))
412 (interned-symbol-p s)))
414 (consp initargs-form)
415 (eq (car initargs-form) 'list*)
416 (memq (cadr initargs-form) '(:function))
417 (consp (setq fn (caddr initargs-form)))
418 (eq (car fn) 'function)
419 (consp (setq fn-lambda (cadr fn)))
420 (eq (car fn-lambda) 'lambda))
421 (let* ((specls (mapcar (lambda (specl)
423 `(,(car specl) ,(eval (cadr specl)))
426 (mname `(,(if (eq (cadr initargs-form) :function)
427 'slow-method 'fast-method)
428 ,name ,@qualifiers ,specls)))
430 (defun ,mname ,(cadr fn-lambda)
432 ,(make-defmethod-form-internal
433 name qualifiers `',specls
434 unspecialized-lambda-list method-class-name
435 `(list* ,(cadr initargs-form)
437 ,@(cdddr initargs-form)))))
438 (make-defmethod-form-internal
440 `(list ,@(mapcar (lambda (specializer)
441 (if (consp specializer)
442 ``(,',(car specializer)
443 ,,(cadr specializer))
446 unspecialized-lambda-list
450 (defun make-defmethod-form-internal
451 (name qualifiers specializers-form unspecialized-lambda-list
452 method-class-name initargs-form)
458 ',unspecialized-lambda-list
460 (sb-c:source-location)))
462 (defmacro make-method-function (method-lambda &environment env)
463 (make-method-function-internal method-lambda env))
465 (defun make-method-function-internal (method-lambda &optional env)
466 (multiple-value-bind (proto-gf proto-method)
467 (prototypes-for-make-method-lambda nil)
468 (multiple-value-bind (method-function-lambda initargs)
469 (make-method-lambda proto-gf proto-method method-lambda env)
470 (make-method-initargs-form proto-gf
472 method-function-lambda
476 (defun add-method-declarations (name qualifiers lambda-list body env)
477 (declare (ignore env))
478 (multiple-value-bind (parameters unspecialized-lambda-list specializers)
479 (parse-specialized-lambda-list lambda-list)
480 (multiple-value-bind (real-body declarations documentation)
482 (values `(lambda ,unspecialized-lambda-list
483 ,@(when documentation `(,documentation))
484 ;; (Old PCL code used a somewhat different style of
485 ;; list for %METHOD-NAME values. Our names use
486 ;; ,@QUALIFIERS instead of ,QUALIFIERS so that the
487 ;; method names look more like what you see in a
490 ;; FIXME: As of sbcl-0.7.0.6, code elsewhere, at
491 ;; least the code to set up named BLOCKs around the
492 ;; bodies of methods, depends on the function's base
493 ;; name being the first element of the %METHOD-NAME
494 ;; list. It would be good to remove this dependency,
495 ;; perhaps by building the BLOCK here, or by using
496 ;; another declaration (e.g. %BLOCK-NAME), so that
497 ;; our method debug names are free to have any format,
498 ;; e.g. (:METHOD PRINT-OBJECT :AROUND (CLOWN T)).
500 ;; Further, as of sbcl-0.7.9.10, the code to
501 ;; implement NO-NEXT-METHOD is coupled to the form of
502 ;; this declaration; see the definition of
503 ;; CALL-NO-NEXT-METHOD (and the passing of
504 ;; METHOD-NAME-DECLARATION arguments around the
505 ;; various CALL-NEXT-METHOD logic).
506 (declare (%method-name (,name
509 (declare (%method-lambda-list ,@lambda-list))
512 unspecialized-lambda-list specializers))))
514 (defun real-make-method-initargs-form (proto-gf proto-method
515 method-lambda initargs env)
516 (declare (ignore proto-gf proto-method))
517 (unless (and (consp method-lambda)
518 (eq (car method-lambda) 'lambda))
519 (error "The METHOD-LAMBDA argument to MAKE-METHOD-FUNCTION, ~S, ~
520 is not a lambda form."
522 (make-method-initargs-form-internal method-lambda initargs env))
524 (unless (fboundp 'make-method-initargs-form)
525 (setf (gdefinition 'make-method-initargs-form)
526 (symbol-function 'real-make-method-initargs-form)))
528 (defun real-make-method-lambda (proto-gf proto-method method-lambda env)
529 (declare (ignore proto-gf proto-method))
530 (make-method-lambda-internal method-lambda env))
532 ;;; a helper function for creating Python-friendly type declarations
533 ;;; in DEFMETHOD forms
534 (defun parameter-specializer-declaration-in-defmethod (parameter specializer)
535 (cond ((and (consp specializer)
536 (eq (car specializer) 'eql))
537 ;; KLUDGE: ANSI, in its wisdom, says that
538 ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at
539 ;; DEFMETHOD expansion time. Thus, although one might think
541 ;; (DEFMETHOD FOO ((X PACKAGE)
544 ;; the PACKAGE and (EQL 12) forms are both parallel type
545 ;; names, they're not, as is made clear when you do
546 ;; (DEFMETHOD FOO ((X PACKAGE)
549 ;; where Y needs to be a symbol named "BAR", not some cons
550 ;; made by (CONS 'QUOTE 'BAR). I.e. when the
551 ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument
552 ;; to be of type (EQL X). It'd be easy to transform one to
553 ;; the other, but it'd be somewhat messier to do so while
554 ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd
555 ;; once. (The new code wouldn't be messy, but it'd require a
556 ;; big transformation of the old code.) So instead we punt.
560 ;; KLUDGE: For some low-level implementation
561 ;; classes, perhaps because of some problems related
562 ;; to the incomplete integration of PCL into SBCL's
563 ;; type system, some specializer classes can't be
564 ;; declared as argument types. E.g.
565 ;; (DEFMETHOD FOO ((X SLOT-OBJECT))
566 ;; (DECLARE (TYPE SLOT-OBJECT X))
569 ;; (DEFSTRUCT BAR A B)
571 ;; perhaps because of the way that STRUCTURE-OBJECT
572 ;; inherits both from SLOT-OBJECT and from
573 ;; SB-KERNEL:INSTANCE. In an effort to sweep such
574 ;; problems under the rug, we exclude these problem
575 ;; cases by blacklisting them here. -- WHN 2001-01-19
576 (list 'slot-object #+nil (find-class 'slot-object)))
578 ((not (eq *boot-state* 'complete))
579 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
580 ;; types which don't match their specializers. (Specifically,
581 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
582 ;; second argument.) Hopefully it only does this kind of
583 ;; weirdness when bootstrapping.. -- WHN 20000610
585 ((typep specializer 'eql-specializer)
586 `(type (eql ,(eql-specializer-object specializer)) ,parameter))
587 ((var-globally-special-p parameter)
588 ;; KLUDGE: Don't declare types for global special variables
589 ;; -- our rebinding magic for SETQ cases don't work right
592 ;; FIXME: It would be better to detect the SETQ earlier and
593 ;; skip declarations for specials only when needed, not
599 ;; Otherwise, we can usually make Python very happy.
601 ;; KLUDGE: Since INFO doesn't work right for class objects here,
602 ;; and they are valid specializers, see if the specializer is
603 ;; a named class, and use the name in that case -- otherwise
604 ;; the class instance is ok, since info will just return NIL, NIL.
606 ;; We still need to deal with the class case too, but at
607 ;; least #.(find-class 'integer) and integer as equivalent
608 ;; specializers with this.
609 (let* ((specializer (if (and (typep specializer 'class)
610 (let ((name (class-name specializer)))
611 (and name (symbolp name)
612 (eq specializer (find-class name nil)))))
613 (class-name specializer)
615 (kind (info :type :kind specializer)))
617 (flet ((specializer-class ()
618 (if (typep specializer 'class)
620 (find-class specializer nil))))
622 ((:primitive) `(type ,specializer ,parameter))
624 (let ((class (specializer-class)))
625 ;; CLASS can be null here if the user has erroneously
626 ;; tried to use a defined type as a specializer; it
627 ;; can be a non-BUILT-IN-CLASS if the user defines a
628 ;; type and calls (SETF FIND-CLASS) in a consistent
630 (when (and class (typep class 'built-in-class))
631 `(type ,specializer ,parameter))))
633 (let ((class (specializer-class)))
636 (if (typep class '(or built-in-class structure-class))
637 `(type ,specializer ,parameter)
638 ;; don't declare CLOS classes as parameters;
639 ;; it's too expensive.
642 ;; we can get here, and still not have a failure
643 ;; case, by doing MOP programming like (PROGN
644 ;; (ENSURE-CLASS 'FOO) (DEFMETHOD BAR ((X FOO))
645 ;; ...)). Best to let the user know we haven't
646 ;; been able to extract enough information:
648 "~@<can't find type for presumed class ~S in ~S.~@:>"
650 'parameter-specializer-declaration-in-defmethod)
652 ((:forthcoming-defclass-type)
655 ;;; For passing a list (groveled by the walker) of the required
656 ;;; parameters whose bindings are modified in the method body to the
657 ;;; optimized-slot-value* macros.
658 (define-symbol-macro %parameter-binding-modified ())
660 (defun make-method-lambda-internal (method-lambda &optional env)
661 (unless (and (consp method-lambda) (eq (car method-lambda) 'lambda))
662 (error "The METHOD-LAMBDA argument to MAKE-METHOD-LAMBDA, ~S, ~
663 is not a lambda form."
665 (multiple-value-bind (real-body declarations documentation)
666 (parse-body (cddr method-lambda))
667 (let* ((name-decl (get-declaration '%method-name declarations))
668 (sll-decl (get-declaration '%method-lambda-list declarations))
669 (method-name (when (consp name-decl) (car name-decl)))
670 (generic-function-name (when method-name (car method-name)))
671 (specialized-lambda-list (or sll-decl (cadr method-lambda))))
672 (multiple-value-bind (parameters lambda-list specializers)
673 (parse-specialized-lambda-list specialized-lambda-list)
674 (let* ((required-parameters
675 (mapcar (lambda (r s) (declare (ignore s)) r)
678 (slots (mapcar #'list required-parameters))
682 ;; These declarations seem to be used by PCL to pass
683 ;; information to itself; when I tried to delete 'em
684 ;; ca. 0.6.10 it didn't work. I'm not sure how
685 ;; they work, but note the (VAR-DECLARATION '%CLASS ..)
686 ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30
688 (mapcar (lambda (a s) (and (symbolp s)
693 ;; These TYPE declarations weren't in the original
694 ;; PCL code, but the Python compiler likes them a
695 ;; lot. (We're telling the compiler about our
696 ;; knowledge of specialized argument types so that
697 ;; it can avoid run-time type dispatch overhead,
698 ;; which can be a huge win for Python.)
700 ;; KLUDGE: when I tried moving these to
701 ;; ADD-METHOD-DECLARATIONS, things broke. No idea
702 ;; why. -- CSR, 2004-06-16
703 ,@(mapcar #'parameter-specializer-declaration-in-defmethod
707 ;; Remove the documentation string and insert the
708 ;; appropriate class declarations. The documentation
709 ;; string is removed to make it easy for us to insert
710 ;; new declarations later, they will just go after the
711 ;; CADR of the method lambda. The class declarations
712 ;; are inserted to communicate the class of the method's
713 ;; arguments to the code walk.
714 `(lambda ,lambda-list
715 ;; The default ignorability of method parameters
716 ;; doesn't seem to be specified by ANSI. PCL had
717 ;; them basically ignorable but was a little
718 ;; inconsistent. E.g. even though the two
719 ;; method definitions
720 ;; (DEFMETHOD FOO ((X T) (Y T)) "Z")
721 ;; (DEFMETHOD FOO ((X T) Y) "Z")
722 ;; are otherwise equivalent, PCL treated Y as
723 ;; ignorable in the first definition but not in the
724 ;; second definition. We make all required
725 ;; parameters ignorable as a way of systematizing
726 ;; the old PCL behavior. -- WHN 2000-11-24
727 (declare (ignorable ,@required-parameters))
730 (block ,(fun-name-block-name generic-function-name)
732 (constant-value-p (and (null (cdr real-body))
733 (constantp (car real-body))))
734 (constant-value (and constant-value-p
735 (constant-form-value (car real-body))))
736 (plist (and constant-value-p
737 (or (typep constant-value
738 '(or number character))
739 (and (symbolp constant-value)
740 (symbol-package constant-value)))
741 (list :constant-value constant-value)))
742 (applyp (dolist (p lambda-list nil)
743 (cond ((memq p '(&optional &rest &key))
748 (walked-lambda call-next-method-p closurep
749 next-method-p-p setq-p
751 (walk-method-lambda method-lambda
756 (multiple-value-bind (walked-lambda-body
758 walked-documentation)
759 (parse-body (cddr walked-lambda))
760 (declare (ignore walked-documentation))
761 (when (some #'cdr slots)
762 (multiple-value-bind (slot-name-lists call-list)
763 (slot-name-lists-from-slots slots calls)
765 `(,@(when slot-name-lists
766 `(:slot-name-lists ,slot-name-lists))
768 `(:call-list ,call-list))
770 (setq walked-lambda-body
771 `((pv-binding (,required-parameters
775 :slot-name-lists ',slot-name-lists
776 :call-list ',call-list)))
777 ,@walked-lambda-body)))))
778 (when (and (memq '&key lambda-list)
779 (not (memq '&allow-other-keys lambda-list)))
780 (let ((aux (memq '&aux lambda-list)))
781 (setq lambda-list (nconc (ldiff lambda-list aux)
782 (list '&allow-other-keys)
784 (values `(lambda (.method-args. .next-methods.)
785 (simple-lexical-method-functions
786 (,lambda-list .method-args. .next-methods.
789 :next-method-p-p ,next-method-p-p
791 ;; we need to pass this along
792 ;; so that NO-NEXT-METHOD can
793 ;; be given a suitable METHOD
794 ;; argument; we need the
795 ;; QUALIFIERS and SPECIALIZERS
796 ;; inside the declaration to
797 ;; give to FIND-METHOD.
798 :method-name-declaration ,name-decl
801 ,@walked-declarations
803 (declare (disable-package-locks
804 %parameter-binding-modified))
805 (symbol-macrolet ((%parameter-binding-modified
806 ',@parameters-setqd))
807 (declare (enable-package-locks
808 %parameter-binding-modified))
809 ,@walked-lambda-body))))
812 ,@(when documentation
813 `(:documentation ,documentation)))))))))))
815 (unless (fboundp 'make-method-lambda)
816 (setf (gdefinition 'make-method-lambda)
817 (symbol-function 'real-make-method-lambda)))
819 (defmacro simple-lexical-method-functions ((lambda-list
825 ,method-args ,next-methods
826 (bind-simple-lexical-method-functions (,method-args ,next-methods
828 (bind-args (,lambda-list ,method-args)
831 (defmacro fast-lexical-method-functions ((lambda-list
837 `(bind-fast-lexical-method-functions (,args ,rest-arg ,next-method-call ,lmf-options)
838 (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg)
841 (defmacro bind-simple-lexical-method-functions
842 ((method-args next-methods (&key call-next-method-p next-method-p-p setq-p
843 closurep applyp method-name-declaration))
846 (if (not (or call-next-method-p setq-p closurep next-method-p-p applyp))
849 `(let ((.next-method. (car ,next-methods))
850 (,next-methods (cdr ,next-methods)))
851 (declare (ignorable .next-method. ,next-methods))
852 (flet (,@(and call-next-method-p
855 ,@(if (safe-code-p env)
856 `((%check-cnm-args cnm-args
858 ',method-name-declaration))
861 (funcall (if (std-instance-p .next-method.)
862 (method-function .next-method.)
863 .next-method.) ; for early methods
864 (or cnm-args ,method-args)
866 (apply #'call-no-next-method
867 ',method-name-declaration
868 (or cnm-args ,method-args))))))
869 ,@(and next-method-p-p
871 (not (null .next-method.))))))
874 (defun call-no-next-method (method-name-declaration &rest args)
875 (destructuring-bind (name) method-name-declaration
876 (destructuring-bind (name &rest qualifiers-and-specializers) name
877 ;; KLUDGE: inefficient traversal, but hey. This should only
878 ;; happen on the slow error path anyway.
879 (let* ((qualifiers (butlast qualifiers-and-specializers))
880 (specializers (car (last qualifiers-and-specializers)))
881 (method (find-method (gdefinition name) qualifiers specializers)))
882 (apply #'no-next-method
883 (method-generic-function method)
887 (defstruct (method-call (:copier nil))
888 (function #'identity :type function)
890 (defstruct (constant-method-call (:copier nil) (:include method-call))
893 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
895 (defmacro invoke-method-call1 (function args cm-args)
896 `(let ((.function. ,function)
898 (.cm-args. ,cm-args))
899 (if (and .cm-args. (null (cdr .cm-args.)))
900 (funcall .function. .args. (car .cm-args.))
901 (apply .function. .args. .cm-args.))))
903 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
904 `(invoke-method-call1 (method-call-function ,method-call)
906 `(list* ,@required-args+rest-arg)
907 `(list ,@required-args+rest-arg))
908 (method-call-call-method-args ,method-call)))
910 (defstruct (fast-method-call (:copier nil))
911 (function #'identity :type function)
915 (defstruct (constant-fast-method-call
916 (:copier nil) (:include fast-method-call))
919 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
921 (defmacro fmc-funcall (fn pv-cell next-method-call &rest args)
922 `(funcall ,fn ,pv-cell ,next-method-call ,@args))
924 (defmacro invoke-fast-method-call (method-call &rest required-args+rest-arg)
925 `(fmc-funcall (fast-method-call-function ,method-call)
926 (fast-method-call-pv-cell ,method-call)
927 (fast-method-call-next-method-call ,method-call)
928 ,@required-args+rest-arg))
930 (defstruct (fast-instance-boundp (:copier nil))
931 (index 0 :type fixnum))
933 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
935 (eval-when (:compile-toplevel :load-toplevel :execute)
936 (defvar *allow-emf-call-tracing-p* nil)
937 (defvar *enable-emf-call-tracing-p* #-sb-show nil #+sb-show t))
939 ;;;; effective method functions
941 (defvar *emf-call-trace-size* 200)
942 (defvar *emf-call-trace* nil)
943 (defvar *emf-call-trace-index* 0)
945 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
946 ;;; without explanation. It appears to be intended for debugging, so
947 ;;; it might be useful someday, so I haven't deleted it.
948 ;;; But it isn't documented and isn't used for anything now, so
949 ;;; I've conditionalized it out of the base system. -- WHN 19991213
951 (defun show-emf-call-trace ()
952 (when *emf-call-trace*
953 (let ((j *emf-call-trace-index*)
954 (*enable-emf-call-tracing-p* nil))
955 (format t "~&(The oldest entries are printed first)~%")
956 (dotimes-fixnum (i *emf-call-trace-size*)
957 (let ((ct (aref *emf-call-trace* j)))
958 (when ct (print ct)))
960 (when (= j *emf-call-trace-size*)
963 (defun trace-emf-call-internal (emf format args)
964 (unless *emf-call-trace*
965 (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
966 (setf (aref *emf-call-trace* *emf-call-trace-index*)
967 (list* emf format args))
968 (incf *emf-call-trace-index*)
969 (when (= *emf-call-trace-index* *emf-call-trace-size*)
970 (setq *emf-call-trace-index* 0)))
972 (defmacro trace-emf-call (emf format args)
973 (when *allow-emf-call-tracing-p*
974 `(when *enable-emf-call-tracing-p*
975 (trace-emf-call-internal ,emf ,format ,args))))
977 (defmacro invoke-effective-method-function-fast
978 (emf restp &rest required-args+rest-arg)
980 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
981 (invoke-fast-method-call ,emf ,@required-args+rest-arg)))
983 (defun effective-method-optimized-slot-access-clause
984 (emf restp required-args+rest-arg)
985 ;; "What," you may wonder, "do these next two clauses do?" In that
986 ;; case, you are not a PCL implementor, for they considered this to
987 ;; be self-documenting.:-| Or CSR, for that matter, since he can
988 ;; also figure it out by looking at it without breaking stride. For
989 ;; the rest of us, though: From what the code is doing with .SLOTS.
990 ;; and whatnot, evidently it's implementing SLOT-VALUEish and
991 ;; GET-SLOT-VALUEish things. Then we can reason backwards and
992 ;; conclude that setting EMF to a FIXNUM is an optimized way to
993 ;; represent these slot access operations.
995 (let ((length (length required-args+rest-arg)))
998 (let* ((.slots. (get-slots-or-nil
999 ,(car required-args+rest-arg)))
1000 (value (when .slots. (clos-slots-ref .slots. ,emf))))
1001 (if (eq value +slot-unbound+)
1002 (slot-unbound-internal ,(car required-args+rest-arg)
1007 (let ((.new-value. ,(car required-args+rest-arg))
1008 (.slots. (get-slots-or-nil
1009 ,(cadr required-args+rest-arg))))
1011 (setf (clos-slots-ref .slots. ,emf) .new-value.)))))))
1012 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
1013 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
1014 ;; there was no explanation and presumably the code is 10+
1015 ;; years stale, I simply deleted it. -- WHN)
1018 ;;; Before SBCL 0.9.16.7 instead of
1019 ;;; INVOKE-NARROW-EFFECTIVE-METHOD-FUNCTION we passed a (THE (OR
1020 ;;; FUNCTION METHOD-CALL FAST-METHOD-CALL) EMF) form as the EMF. Now,
1021 ;;; to make less work for the compiler we take a path that doesn't
1022 ;;; involve the slot-accessor clause (where EMF is a FIXNUM) at all.
1023 (macrolet ((def (name &optional narrow)
1024 `(defmacro ,name (emf restp &rest required-args+rest-arg)
1025 (unless (constantp restp)
1026 (error "The RESTP argument is not constant."))
1027 (setq restp (constant-form-value restp))
1028 (with-unique-names (emf-n)
1030 (declare (optimize (sb-c:insert-step-conditions 0)))
1031 (let ((,emf-n ,emf))
1032 (trace-emf-call ,emf-n ,restp (list ,@required-args+rest-arg))
1035 (invoke-fast-method-call ,emf-n ,@required-args+rest-arg))
1037 `(effective-method-optimized-slot-access-clause
1038 emf-n restp required-args+rest-arg))
1040 (invoke-method-call ,emf-n ,restp ,@required-args+rest-arg))
1043 `(apply ,emf-n ,@required-args+rest-arg)
1044 `(funcall ,emf-n ,@required-args+rest-arg))))))))))
1045 (def invoke-effective-method-function nil)
1046 (def invoke-narrow-effective-method-function t))
1048 (defun invoke-emf (emf args)
1049 (trace-emf-call emf t args)
1052 (let* ((arg-info (fast-method-call-arg-info emf))
1053 (restp (cdr arg-info))
1054 (nreq (car arg-info)))
1056 (let* ((rest-args (nthcdr nreq args))
1057 (req-args (ldiff args rest-args)))
1058 (apply (fast-method-call-function emf)
1059 (fast-method-call-pv-cell emf)
1060 (fast-method-call-next-method-call emf)
1061 (nconc req-args (list rest-args))))
1064 (invoke-fast-method-call emf)
1065 (error 'simple-program-error
1066 :format-control "invalid number of arguments: 0"
1067 :format-arguments nil)))
1070 (invoke-fast-method-call emf (car args))
1071 (error 'simple-program-error
1072 :format-control "invalid number of arguments: 1"
1073 :format-arguments nil)))
1076 (invoke-fast-method-call emf (car args) (cadr args))
1077 (error 'simple-program-error
1078 :format-control "invalid number of arguments: 2"
1079 :format-arguments nil)))
1081 (apply (fast-method-call-function emf)
1082 (fast-method-call-pv-cell emf)
1083 (fast-method-call-next-method-call emf)
1086 (apply (method-call-function emf)
1088 (method-call-call-method-args emf)))
1091 (error 'simple-program-error
1092 :format-control "invalid number of arguments: 0"
1093 :format-arguments nil))
1095 (let* ((slots (get-slots (car args)))
1096 (value (clos-slots-ref slots emf)))
1097 (if (eq value +slot-unbound+)
1098 (slot-unbound-internal (car args) emf)
1101 (setf (clos-slots-ref (get-slots (cadr args)) emf)
1103 (t (error 'simple-program-error
1104 :format-control "invalid number of arguments"
1105 :format-arguments nil))))
1106 (fast-instance-boundp
1107 (if (or (null args) (cdr args))
1108 (error 'simple-program-error
1109 :format-control "invalid number of arguments"
1110 :format-arguments nil)
1111 (let ((slots (get-slots (car args))))
1112 (not (eq (clos-slots-ref slots (fast-instance-boundp-index emf))
1118 (defmacro fast-call-next-method-body ((args next-method-call rest-arg)
1119 method-name-declaration
1121 `(if ,next-method-call
1122 ,(let ((call `(invoke-narrow-effective-method-function
1124 ,(not (null rest-arg))
1126 ,@(when rest-arg `(,rest-arg)))))
1130 `(&rest ,rest-arg)))
1134 (call-no-next-method ',method-name-declaration
1139 (defmacro bind-fast-lexical-method-functions
1140 ((args rest-arg next-method-call (&key
1143 method-name-declaration
1149 (let* ((all-params (append args (when rest-arg (list rest-arg))))
1150 (rebindings (when (or setq-p call-next-method-p)
1151 (mapcar (lambda (x) (list x x)) all-params))))
1152 (if (not (or call-next-method-p setq-p closurep next-method-p-p applyp))
1155 `(flet (,@(when call-next-method-p
1156 `((call-next-method (&rest cnm-args)
1157 (declare (muffle-conditions code-deletion-note)
1158 (optimize (sb-c:insert-step-conditions 0)))
1159 ,@(if (safe-code-p env)
1160 `((%check-cnm-args cnm-args (list ,@args)
1161 ',method-name-declaration))
1163 (fast-call-next-method-body (,args
1166 ,method-name-declaration
1168 ,@(when next-method-p-p
1170 (declare (optimize (sb-c:insert-step-conditions 0)))
1171 (not (null ,next-method-call))))))
1173 ,@(when rebindings `((declare (ignorable ,@all-params))))
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 (;; flag indicating that CALL-NEXT-METHOD should be in the
1290 ;; method definition
1291 (call-next-method-p nil)
1292 ;; flag indicating that #'CALL-NEXT-METHOD was seen in the
1295 ;; flag indicating that NEXT-METHOD-P should be in the method
1297 (next-method-p-p nil)
1298 ;; a list of all required parameters whose bindings might be
1299 ;; modified in the method body.
1300 (parameters-setqd nil))
1301 (flet ((walk-function (form context env)
1302 (cond ((not (eq context :eval)) form)
1303 ;; FIXME: Jumping to a conclusion from the way it's used
1304 ;; above, perhaps CONTEXT should be called SITUATION
1305 ;; (after the term used in the ANSI specification of
1306 ;; EVAL-WHEN) and given modern ANSI keyword values
1307 ;; like :LOAD-TOPLEVEL.
1308 ((not (listp form)) form)
1309 ((eq (car form) 'call-next-method)
1310 (setq call-next-method-p t)
1312 ((eq (car form) 'next-method-p)
1313 (setq next-method-p-p t)
1315 ((memq (car form) '(setq multiple-value-setq))
1316 ;; FIXME: this is possibly a little strong as
1317 ;; conditions go. Ideally we would want to detect
1318 ;; which, if any, of the method parameters are
1319 ;; being set, and communicate that information to
1320 ;; e.g. SPLIT-DECLARATIONS. However, the brute
1321 ;; force method doesn't really cost much; a little
1322 ;; loss of discrimination over IGNORED variables
1323 ;; should be all. -- CSR, 2004-07-01
1325 ;; As of 2006-09-18 modified parameter bindings
1326 ;; are now tracked with more granularity than just
1327 ;; one SETQ-P flag, in order to disable SLOT-VALUE
1328 ;; optimizations for parameters that are SETQd.
1329 ;; The old binary SETQ-P flag is still used for
1330 ;; all other purposes, since as noted above, the
1331 ;; extra cost is minimal. -- JES, 2006-09-18
1333 ;; The walker will split (SETQ A 1 B 2) to
1334 ;; separate (SETQ A 1) and (SETQ B 2) forms, so we
1335 ;; only need to handle the simple case of SETQ
1337 (let ((vars (if (eq (car form) 'setq)
1338 (list (second form))
1341 ;; Note that we don't need to check for
1342 ;; %VARIABLE-REBINDING declarations like is
1343 ;; done in CAN-OPTIMIZE-ACCESS1, since the
1344 ;; bindings that will have that declation will
1346 (when (var-declaration '%class var env)
1347 ;; If a parameter binding is shadowed by
1348 ;; another binding it won't have a %CLASS
1349 ;; declaration anymore, and this won't get
1351 (pushnew var parameters-setqd))))
1353 ((and (eq (car form) 'function)
1354 (cond ((eq (cadr form) 'call-next-method)
1355 (setq call-next-method-p t)
1358 ((eq (cadr form) 'next-method-p)
1359 (setq next-method-p-p t)
1363 ((and (memq (car form)
1364 '(slot-value set-slot-value slot-boundp))
1365 (constantp (caddr form)))
1366 (let ((parameter (can-optimize-access form
1369 (let ((fun (ecase (car form)
1370 (slot-value #'optimize-slot-value)
1371 (set-slot-value #'optimize-set-slot-value)
1372 (slot-boundp #'optimize-slot-boundp))))
1373 (funcall fun slots parameter form))))
1374 ((and (eq (car form) 'apply)
1376 (eq (car (cadr form)) 'function)
1377 (generic-function-name-p (cadr (cadr form))))
1378 (optimize-generic-function-call
1379 form required-parameters env slots calls))
1380 ((generic-function-name-p (car form))
1381 (optimize-generic-function-call
1382 form required-parameters env slots calls))
1385 (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1386 (values walked-lambda
1390 (not (null parameters-setqd))
1391 parameters-setqd)))))
1393 (defun generic-function-name-p (name)
1394 (and (legal-fun-name-p name)
1396 (if (eq *boot-state* 'complete)
1397 (standard-generic-function-p (gdefinition name))
1398 (funcallable-instance-p (gdefinition name)))))
1400 (defun method-plist-value (method key &optional default)
1401 (let ((plist (if (consp method)
1402 (getf (early-method-initargs method) 'plist)
1403 (object-plist method))))
1404 (getf plist key default)))
1406 (defun (setf method-plist-value) (new-value method key &optional default)
1408 (setf (getf (getf (early-method-initargs method) 'plist) key default)
1410 (setf (getf (object-plist method) key default) new-value)))
1412 (defun load-defmethod
1413 (class name quals specls ll initargs source-location)
1414 (setq initargs (copy-tree initargs))
1415 (setf (getf (getf initargs 'plist) :name)
1416 (make-method-spec name quals specls))
1417 (load-defmethod-internal class name quals specls
1418 ll initargs source-location))
1420 (defun load-defmethod-internal
1421 (method-class gf-spec qualifiers specializers lambda-list
1422 initargs source-location)
1423 (when (and (eq *boot-state* 'complete)
1425 (let* ((gf (fdefinition gf-spec))
1426 (method (and (generic-function-p gf)
1427 (generic-function-methods gf)
1430 (parse-specializers specializers)
1433 (style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1434 gf-spec qualifiers specializers))))
1435 (let ((method (apply #'add-named-method
1436 gf-spec qualifiers specializers lambda-list
1437 :definition-source source-location
1439 (unless (or (eq method-class 'standard-method)
1440 (eq (find-class method-class nil) (class-of method)))
1441 ;; FIXME: should be STYLE-WARNING?
1442 (format *error-output*
1443 "~&At the time the method with qualifiers ~:S and~%~
1444 specializers ~:S on the generic function ~S~%~
1445 was compiled, the method-class for that generic function was~%~
1446 ~S. But, the method class is now ~S, this~%~
1447 may mean that this method was compiled improperly.~%"
1448 qualifiers specializers gf-spec
1449 method-class (class-name (class-of method))))
1452 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1453 `(slow-method ,gf-spec ,@qualifiers ,unparsed-specializers))
1455 (defun initialize-method-function (initargs method)
1456 (let* ((mf (getf initargs :function))
1457 (mff (and (typep mf '%method-function)
1458 (%method-function-fast-function mf)))
1459 (plist (getf initargs 'plist))
1460 (name (getf plist :name)))
1463 (setq mf (set-fun-name mf name)))
1464 (when (and mff (consp name) (eq (car name) 'slow-method))
1465 (let ((fast-name `(fast-method ,@(cdr name))))
1466 (set-fun-name mff fast-name))))
1468 (let ((plist plist))
1469 (let ((snl (getf plist :slot-name-lists))
1470 (cl (getf plist :call-list)))
1472 (setf (method-plist-value method :pv-table)
1473 (intern-pv-table :slot-name-lists snl :call-list cl))))))))
1475 (defun analyze-lambda-list (lambda-list)
1476 (flet (;; FIXME: Is this redundant with SB-C::MAKE-KEYWORD-FOR-ARG?
1477 (parse-key-arg (arg)
1479 (if (listp (car arg))
1481 (keywordicate (car arg)))
1482 (keywordicate arg))))
1488 (allow-other-keys-p nil)
1490 (keyword-parameters ())
1492 (dolist (x lambda-list)
1493 (if (memq x lambda-list-keywords)
1495 (&optional (setq state 'optional))
1498 (&allow-other-keys (setq allow-other-keys-p t))
1499 (&rest (setq restp t
1503 (error "encountered the non-standard lambda list keyword ~S"
1506 (required (incf nrequired))
1507 (optional (incf noptional))
1508 (key (push (parse-key-arg x) keywords)
1509 (push x keyword-parameters))
1510 (rest (incf nrest)))))
1511 (when (and restp (zerop nrest))
1512 (error "Error in lambda-list:~%~
1513 After &REST, a DEFGENERIC lambda-list ~
1514 must be followed by at least one variable."))
1515 (values nrequired noptional keysp restp allow-other-keys-p
1517 (reverse keyword-parameters)))))
1519 (defun keyword-spec-name (x)
1520 (let ((key (if (atom x) x (car x))))
1525 (defun ftype-declaration-from-lambda-list (lambda-list name)
1526 (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1527 keywords keyword-parameters)
1528 (analyze-lambda-list lambda-list)
1529 (declare (ignore keyword-parameters))
1530 (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1531 (old-ftype (if (fun-type-p old) old nil))
1532 (old-restp (and old-ftype (fun-type-rest old-ftype)))
1533 (old-keys (and old-ftype
1534 (mapcar #'key-info-name
1537 (old-keysp (and old-ftype (fun-type-keyp old-ftype)))
1538 (old-allowp (and old-ftype
1539 (fun-type-allowp old-ftype)))
1540 (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1541 `(function ,(append (make-list nrequired :initial-element t)
1542 (when (plusp noptional)
1543 (append '(&optional)
1544 (make-list noptional :initial-element t)))
1545 (when (or restp old-restp)
1547 (when (or keysp old-keysp)
1549 (mapcar (lambda (key)
1552 (when (or allow-other-keys-p old-allowp)
1553 '(&allow-other-keys)))))
1556 (defun defgeneric-declaration (spec lambda-list)
1557 `(ftype ,(ftype-declaration-from-lambda-list lambda-list spec) ,spec))
1559 ;;;; early generic function support
1561 (defvar *!early-generic-functions* ())
1563 (defun ensure-generic-function (fun-name
1565 &key environment source-location
1567 (declare (ignore environment))
1568 (let ((existing (and (fboundp fun-name)
1569 (gdefinition fun-name))))
1571 (eq *boot-state* 'complete)
1572 (null (generic-function-p existing)))
1573 (generic-clobbers-function fun-name)
1574 (apply #'ensure-generic-function-using-class
1575 existing fun-name all-keys))))
1577 (defun generic-clobbers-function (fun-name)
1578 (error 'simple-program-error
1579 :format-control "~S already names an ordinary function or a macro."
1580 :format-arguments (list fun-name)))
1582 (defvar *sgf-wrapper*
1583 (boot-make-wrapper (early-class-size 'standard-generic-function)
1584 'standard-generic-function))
1586 (defvar *sgf-slots-init*
1587 (mapcar (lambda (canonical-slot)
1588 (if (memq (getf canonical-slot :name) '(arg-info source))
1590 (let ((initfunction (getf canonical-slot :initfunction)))
1592 (funcall initfunction)
1594 (early-collect-inheritance 'standard-generic-function)))
1596 (defvar *sgf-method-class-index*
1597 (!bootstrap-slot-index 'standard-generic-function 'method-class))
1599 (defun early-gf-p (x)
1600 (and (fsc-instance-p x)
1601 (eq (clos-slots-ref (get-slots x) *sgf-method-class-index*)
1604 (defvar *sgf-methods-index*
1605 (!bootstrap-slot-index 'standard-generic-function 'methods))
1607 (defmacro early-gf-methods (gf)
1608 `(clos-slots-ref (get-slots ,gf) *sgf-methods-index*))
1610 (defun safe-generic-function-methods (generic-function)
1611 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
1612 (clos-slots-ref (get-slots generic-function) *sgf-methods-index*)
1613 (generic-function-methods generic-function)))
1615 (defvar *sgf-arg-info-index*
1616 (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1618 (defmacro early-gf-arg-info (gf)
1619 `(clos-slots-ref (get-slots ,gf) *sgf-arg-info-index*))
1621 (defvar *sgf-dfun-state-index*
1622 (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1624 (defstruct (arg-info
1626 (:constructor make-arg-info ())
1628 (arg-info-lambda-list :no-lambda-list)
1631 arg-info-number-optional
1633 arg-info-keys ;nil no &KEY or &REST allowed
1634 ;(k1 k2 ..) Each method must accept these &KEY arguments.
1635 ;T must have &KEY or &REST
1637 gf-info-simple-accessor-type ; nil, reader, writer, boundp
1638 (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1640 gf-info-static-c-a-m-emf
1641 (gf-info-c-a-m-emf-std-p t)
1644 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1646 (defun arg-info-valid-p (arg-info)
1647 (not (null (arg-info-number-optional arg-info))))
1649 (defun arg-info-applyp (arg-info)
1650 (or (plusp (arg-info-number-optional arg-info))
1651 (arg-info-key/rest-p arg-info)))
1653 (defun arg-info-number-required (arg-info)
1654 (length (arg-info-metatypes arg-info)))
1656 (defun arg-info-nkeys (arg-info)
1657 (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1659 (defun create-gf-lambda-list (lambda-list)
1660 ;;; Create a gf lambda list from a method lambda list
1661 (loop for x in lambda-list
1662 collect (if (consp x) (list (car x)) x)
1663 if (eq x '&key) do (loop-finish)))
1665 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1666 argument-precedence-order)
1667 (let* ((arg-info (if (eq *boot-state* 'complete)
1669 (early-gf-arg-info gf)))
1670 (methods (if (eq *boot-state* 'complete)
1671 (generic-function-methods gf)
1672 (early-gf-methods gf)))
1673 (was-valid-p (integerp (arg-info-number-optional arg-info)))
1674 (first-p (and new-method (null (cdr methods)))))
1675 (when (and (not lambda-list-p) methods)
1676 (setq lambda-list (gf-lambda-list gf)))
1677 (when (or lambda-list-p
1679 (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1680 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1681 (analyze-lambda-list lambda-list)
1682 (when (and methods (not first-p))
1683 (let ((gf-nreq (arg-info-number-required arg-info))
1684 (gf-nopt (arg-info-number-optional arg-info))
1685 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1686 (unless (and (= nreq gf-nreq)
1688 (eq (or keysp restp) gf-key/rest-p))
1689 (error "The lambda-list ~S is incompatible with ~
1690 existing methods of ~S."
1692 (setf (arg-info-lambda-list arg-info)
1695 (create-gf-lambda-list lambda-list)))
1696 (when (or lambda-list-p argument-precedence-order
1697 (null (arg-info-precedence arg-info)))
1698 (setf (arg-info-precedence arg-info)
1699 (compute-precedence lambda-list nreq argument-precedence-order)))
1700 (setf (arg-info-metatypes arg-info) (make-list nreq))
1701 (setf (arg-info-number-optional arg-info) nopt)
1702 (setf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1703 (setf (arg-info-keys arg-info)
1705 (if allow-other-keys-p t keywords)
1706 (arg-info-key/rest-p arg-info)))))
1708 (check-method-arg-info gf arg-info new-method))
1709 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1712 (defun check-method-arg-info (gf arg-info method)
1713 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1714 (analyze-lambda-list (if (consp method)
1715 (early-method-lambda-list method)
1716 (method-lambda-list method)))
1717 (flet ((lose (string &rest args)
1718 (error 'simple-program-error
1719 :format-control "~@<attempt to add the method~2I~_~S~I~_~
1720 to the generic function~2I~_~S;~I~_~
1722 :format-arguments (list method gf string args)))
1723 (comparison-description (x y)
1724 (if (> x y) "more" "fewer")))
1725 (let ((gf-nreq (arg-info-number-required arg-info))
1726 (gf-nopt (arg-info-number-optional arg-info))
1727 (gf-key/rest-p (arg-info-key/rest-p arg-info))
1728 (gf-keywords (arg-info-keys arg-info)))
1729 (unless (= nreq gf-nreq)
1731 "the method has ~A required arguments than the generic function."
1732 (comparison-description nreq gf-nreq)))
1733 (unless (= nopt gf-nopt)
1735 "the method has ~A optional arguments than the generic function."
1736 (comparison-description nopt gf-nopt)))
1737 (unless (eq (or keysp restp) gf-key/rest-p)
1739 "the method and generic function differ in whether they accept~_~
1740 &REST or &KEY arguments."))
1741 (when (consp gf-keywords)
1742 (unless (or (and restp (not keysp))
1744 (every (lambda (k) (memq k keywords)) gf-keywords))
1745 (lose "the method does not accept each of the &KEY arguments~2I~_~
1749 (defvar *sm-specializers-index*
1750 (!bootstrap-slot-index 'standard-method 'specializers))
1751 (defvar *sm-%function-index*
1752 (!bootstrap-slot-index 'standard-method '%function))
1753 (defvar *sm-qualifiers-index*
1754 (!bootstrap-slot-index 'standard-method 'qualifiers))
1755 (defvar *sm-plist-index*
1756 (!bootstrap-slot-index 'standard-method 'plist))
1758 ;;; FIXME: we don't actually need this; we could test for the exact
1759 ;;; class and deal with it as appropriate. In fact we probably don't
1760 ;;; need it anyway because we only use this for METHOD-SPECIALIZERS on
1761 ;;; the standard reader method for METHOD-SPECIALIZERS. Probably.
1762 (dolist (s '(specializers %function plist))
1763 (aver (= (symbol-value (intern (format nil "*SM-~A-INDEX*" s)))
1764 (!bootstrap-slot-index 'standard-reader-method s)
1765 (!bootstrap-slot-index 'standard-writer-method s)
1766 (!bootstrap-slot-index 'standard-boundp-method s))))
1768 (defun safe-method-specializers (method)
1769 (let ((standard-method-classes
1770 (list *the-class-standard-method*
1771 *the-class-standard-reader-method*
1772 *the-class-standard-writer-method*
1773 *the-class-standard-boundp-method*))
1774 (class (class-of method)))
1775 (if (member class standard-method-classes)
1776 (clos-slots-ref (get-slots method) *sm-specializers-index*)
1777 (method-specializers method))))
1778 (defun safe-method-fast-function (method)
1779 (let ((mf (safe-method-function method)))
1780 (and (typep mf '%method-function)
1781 (%method-function-fast-function mf))))
1782 (defun safe-method-function (method)
1783 (let ((standard-method-classes
1784 (list *the-class-standard-method*
1785 *the-class-standard-reader-method*
1786 *the-class-standard-writer-method*
1787 *the-class-standard-boundp-method*))
1788 (class (class-of method)))
1789 (if (member class standard-method-classes)
1790 (clos-slots-ref (get-slots method) *sm-%function-index*)
1791 (method-function method))))
1792 (defun safe-method-qualifiers (method)
1793 (let ((standard-method-classes
1794 (list *the-class-standard-method*
1795 *the-class-standard-reader-method*
1796 *the-class-standard-writer-method*
1797 *the-class-standard-boundp-method*))
1798 (class (class-of method)))
1799 (if (member class standard-method-classes)
1800 (clos-slots-ref (get-slots method) *sm-qualifiers-index*)
1801 (method-qualifiers method))))
1803 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1804 (let* ((existing-p (and methods (cdr methods) new-method))
1805 (nreq (length (arg-info-metatypes arg-info)))
1806 (metatypes (if existing-p
1807 (arg-info-metatypes arg-info)
1809 (type (if existing-p
1810 (gf-info-simple-accessor-type arg-info)
1812 (when (arg-info-valid-p arg-info)
1813 (dolist (method (if new-method (list new-method) methods))
1814 (let* ((specializers (if (or (eq *boot-state* 'complete)
1815 (not (consp method)))
1816 (safe-method-specializers method)
1817 (early-method-specializers method t)))
1818 (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1820 (early-method-class method)))
1823 (or (not (eq *boot-state* 'complete))
1824 (eq (generic-function-method-combination gf)
1825 *standard-method-combination*)))
1826 (cond ((or (eq class *the-class-standard-reader-method*)
1827 (eq class *the-class-global-reader-method*))
1829 ((or (eq class *the-class-standard-writer-method*)
1830 (eq class *the-class-global-writer-method*))
1832 ((or (eq class *the-class-standard-boundp-method*)
1833 (eq class *the-class-global-boundp-method*))
1835 (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1836 (setq type (cond ((null type) new-type)
1837 ((eq type new-type) type)
1839 (setf (arg-info-metatypes arg-info) metatypes)
1840 (setf (gf-info-simple-accessor-type arg-info) type)))
1841 (when (or (not was-valid-p) first-p)
1842 (multiple-value-bind (c-a-m-emf std-p)
1845 (compute-applicable-methods-emf gf))
1846 (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1847 (setf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1848 (unless (gf-info-c-a-m-emf-std-p arg-info)
1849 (setf (gf-info-simple-accessor-type arg-info) t))))
1851 (let ((name (if (eq *boot-state* 'complete)
1852 (generic-function-name gf)
1853 (!early-gf-name gf))))
1854 (setf (gf-precompute-dfun-and-emf-p arg-info)
1858 *internal-pcl-generalized-fun-name-symbols*))
1860 (t (let* ((symbol (fun-name-block-name name))
1861 (package (symbol-package symbol)))
1862 (and (or (eq package *pcl-package*)
1863 (memq package (package-use-list *pcl-package*)))
1864 ;; FIXME: this test will eventually be
1865 ;; superseded by the *internal-pcl...* test,
1866 ;; above. While we are in a process of
1867 ;; transition, however, it should probably
1869 (not (find #\Space (symbol-name symbol))))))))))
1870 (setf (gf-info-fast-mf-p arg-info)
1871 (or (not (eq *boot-state* 'complete))
1872 (let* ((method-class (generic-function-method-class gf))
1873 (methods (compute-applicable-methods
1874 #'make-method-lambda
1875 (list gf (class-prototype method-class)
1877 (and methods (null (cdr methods))
1878 (let ((specls (method-specializers (car methods))))
1879 (and (classp (car specls))
1880 (eq 'standard-generic-function
1881 (class-name (car specls)))
1882 (classp (cadr specls))
1883 (eq 'standard-method
1884 (class-name (cadr specls)))))))))
1887 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
1889 ;;; The STATIC-SLOTS field of the funcallable instances used as early
1890 ;;; generic functions is used to store the early methods and early
1891 ;;; discriminator code for the early generic function. The static
1892 ;;; slots field of the fins contains a list whose:
1893 ;;; CAR - a list of the early methods on this early gf
1894 ;;; CADR - the early discriminator code for this method
1895 (defun ensure-generic-function-using-class (existing spec &rest keys
1896 &key (lambda-list nil
1898 argument-precedence-order
1901 (declare (ignore keys))
1902 (cond ((and existing (early-gf-p existing))
1904 (set-arg-info existing :lambda-list lambda-list))
1906 ((assoc spec *!generic-function-fixups* :test #'equal)
1908 (make-early-gf spec lambda-list lambda-list-p existing
1909 argument-precedence-order source-location)
1910 (error "The function ~S is not already defined." spec)))
1912 (error "~S should be on the list ~S."
1914 '*!generic-function-fixups*))
1916 (pushnew spec *!early-generic-functions* :test #'equal)
1917 (make-early-gf spec lambda-list lambda-list-p nil
1918 argument-precedence-order source-location))))
1920 (defun make-early-gf (spec &optional lambda-list lambda-list-p
1921 function argument-precedence-order source-location)
1922 (let ((fin (allocate-standard-funcallable-instance
1923 *sgf-wrapper* *sgf-slots-init*)))
1924 (set-funcallable-instance-function
1927 (if (eq spec 'print-object)
1928 #'(lambda (instance stream)
1929 (print-unreadable-object (instance stream :identity t)
1930 (format stream "std-instance")))
1931 #'(lambda (&rest args)
1932 (declare (ignore args))
1933 (error "The function of the funcallable-instance ~S~
1934 has not been set." fin)))))
1935 (setf (gdefinition spec) fin)
1936 (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
1937 (!bootstrap-set-slot 'standard-generic-function
1941 (set-fun-name fin spec)
1942 (let ((arg-info (make-arg-info)))
1943 (setf (early-gf-arg-info fin) arg-info)
1945 (proclaim (defgeneric-declaration spec lambda-list))
1946 (if argument-precedence-order
1948 :lambda-list lambda-list
1949 :argument-precedence-order argument-precedence-order)
1950 (set-arg-info fin :lambda-list lambda-list))))
1953 (defun safe-gf-dfun-state (generic-function)
1954 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
1955 (clos-slots-ref (get-slots generic-function) *sgf-dfun-state-index*)
1956 (gf-dfun-state generic-function)))
1957 (defun (setf safe-gf-dfun-state) (new-value generic-function)
1958 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
1959 (setf (clos-slots-ref (get-slots generic-function)
1960 *sgf-dfun-state-index*)
1962 (setf (gf-dfun-state generic-function) new-value)))
1964 (defun set-dfun (gf &optional dfun cache info)
1966 (setf (cache-owner cache) gf))
1967 (let ((new-state (if (and dfun (or cache info))
1968 (list* dfun cache info)
1970 (if (eq *boot-state* 'complete)
1971 (setf (safe-gf-dfun-state gf) new-state)
1972 (setf (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*)
1976 (defun gf-dfun-cache (gf)
1977 (let ((state (if (eq *boot-state* 'complete)
1978 (safe-gf-dfun-state gf)
1979 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1982 (cons (cadr state)))))
1984 (defun gf-dfun-info (gf)
1985 (let ((state (if (eq *boot-state* 'complete)
1986 (safe-gf-dfun-state gf)
1987 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1990 (cons (cddr state)))))
1992 (defvar *sgf-name-index*
1993 (!bootstrap-slot-index 'standard-generic-function 'name))
1995 (defun !early-gf-name (gf)
1996 (clos-slots-ref (get-slots gf) *sgf-name-index*))
1998 (defun gf-lambda-list (gf)
1999 (let ((arg-info (if (eq *boot-state* 'complete)
2001 (early-gf-arg-info gf))))
2002 (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
2003 (let ((methods (if (eq *boot-state* 'complete)
2004 (generic-function-methods gf)
2005 (early-gf-methods gf))))
2008 (warn "no way to determine the lambda list for ~S" gf)
2010 (let* ((method (car (last methods)))
2011 (ll (if (consp method)
2012 (early-method-lambda-list method)
2013 (method-lambda-list method))))
2014 (create-gf-lambda-list ll))))
2015 (arg-info-lambda-list arg-info))))
2017 (defmacro real-ensure-gf-internal (gf-class all-keys env)
2019 (cond ((symbolp ,gf-class)
2020 (setq ,gf-class (find-class ,gf-class t ,env)))
2021 ((classp ,gf-class))
2023 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
2024 class nor a symbol that names a class."
2026 (unless (class-finalized-p ,gf-class)
2027 (if (class-has-a-forward-referenced-superclass-p ,gf-class)
2028 ;; FIXME: reference MOP documentation -- this is an
2029 ;; additional requirement on our users
2030 (error "The generic function class ~S is not finalizeable" ,gf-class)
2031 (finalize-inheritance ,gf-class)))
2032 (remf ,all-keys :generic-function-class)
2033 (remf ,all-keys :environment)
2034 (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
2035 (unless (eq combin '.shes-not-there.)
2036 (setf (getf ,all-keys :method-combination)
2037 (find-method-combination (class-prototype ,gf-class)
2040 (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
2041 (unless (eq method-class '.shes-not-there.)
2042 (setf (getf ,all-keys :method-class)
2043 (cond ((classp method-class)
2045 (t (find-class method-class t ,env))))))))
2047 (defun real-ensure-gf-using-class--generic-function
2051 &key environment (lambda-list nil lambda-list-p)
2052 (generic-function-class 'standard-generic-function)
2054 (real-ensure-gf-internal generic-function-class all-keys environment)
2055 ;; KLUDGE: the above macro does SETQ on GENERIC-FUNCTION-CLASS,
2056 ;; which is what makes the next line work
2057 (unless (eq (class-of existing) generic-function-class)
2058 (change-class existing generic-function-class))
2060 (apply #'reinitialize-instance existing all-keys)
2062 (proclaim (defgeneric-declaration fun-name lambda-list)))))
2064 (defun real-ensure-gf-using-class--null
2068 &key environment (lambda-list nil lambda-list-p)
2069 (generic-function-class 'standard-generic-function)
2071 (declare (ignore existing))
2072 (real-ensure-gf-internal generic-function-class all-keys environment)
2074 (setf (gdefinition fun-name)
2075 (apply #'make-instance generic-function-class
2076 :name fun-name all-keys))
2078 (proclaim (defgeneric-declaration fun-name lambda-list)))))
2080 (defun safe-gf-arg-info (generic-function)
2081 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
2082 (clos-slots-ref (fsc-instance-slots generic-function)
2083 *sgf-arg-info-index*)
2084 (gf-arg-info generic-function)))
2086 ;;; FIXME: this function took on a slightly greater role than it
2087 ;;; previously had around 2005-11-02, when CSR fixed the bug whereby
2088 ;;; having more than one subclass of standard-generic-function caused
2089 ;;; the whole system to die horribly through a metacircle in
2090 ;;; GF-ARG-INFO. The fix is to be slightly more disciplined about
2091 ;;; calling accessor methods -- we call GET-GENERIC-FUN-INFO when
2092 ;;; computing discriminating functions, so we need to be careful about
2093 ;;; having a base case for the recursion, and we provide that with the
2094 ;;; STANDARD-GENERIC-FUNCTION case below. However, we are not (yet)
2095 ;;; as disciplined as CLISP's CLOS/MOP, and it would be nice to get to
2096 ;;; that stage, where all potentially dangerous cases are enumerated
2097 ;;; and stopped. -- CSR, 2005-11-02.
2098 (defun get-generic-fun-info (gf)
2099 ;; values nreq applyp metatypes nkeys arg-info
2100 (multiple-value-bind (applyp metatypes arg-info)
2101 (let* ((arg-info (if (early-gf-p gf)
2102 (early-gf-arg-info gf)
2103 (safe-gf-arg-info gf)))
2104 (metatypes (arg-info-metatypes arg-info)))
2105 (values (arg-info-applyp arg-info)
2108 (values (length metatypes) applyp metatypes
2109 (count-if (lambda (x) (neq x t)) metatypes)
2112 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
2113 &key slot-name object-class method-class-function)
2116 ;; Figure out whether we got class objects or class names as the
2117 ;; specializers and set parsed and unparsed appropriately. If we
2118 ;; got class objects, then we can compute unparsed, but if we got
2119 ;; class names we don't try to compute parsed.
2121 ;; Note that the use of not symbolp in this call to every should be
2122 ;; read as 'classp' we can't use classp itself because it doesn't
2124 (if (every (lambda (s) (not (symbolp s))) specializers)
2125 (setq parsed specializers
2126 unparsed (mapcar (lambda (s)
2127 (if (eq s t) t (class-name s)))
2129 (setq unparsed specializers
2134 (getf initargs :function)
2135 (let ((mf (getf initargs :function)))
2137 (and (typep mf '%method-function)
2138 (%method-function-fast-function mf)))
2140 ;; the parsed specializers. This is used by
2141 ;; EARLY-METHOD-SPECIALIZERS to cache the parse.
2142 ;; Note that this only comes into play when there is
2143 ;; more than one early method on an early gf.
2146 ;; A list to which REAL-MAKE-A-METHOD can be applied
2147 ;; to make a real method corresponding to this early
2150 (list class qualifiers arglist unparsed
2153 (list :slot-name slot-name :object-class object-class
2154 :method-class-function method-class-function))))))
2155 (initialize-method-function initargs result)
2158 (defun real-make-a-method
2159 (class qualifiers lambda-list specializers initargs doc
2160 &rest args &key slot-name object-class method-class-function)
2161 (setq specializers (parse-specializers specializers))
2162 (if method-class-function
2163 (let* ((object-class (if (classp object-class) object-class
2164 (find-class object-class)))
2165 (slots (class-direct-slots object-class))
2166 (slot-definition (find slot-name slots
2167 :key #'slot-definition-name)))
2169 (aver slot-definition)
2170 (let ((initargs (list* :qualifiers qualifiers :lambda-list lambda-list
2171 :specializers specializers :documentation doc
2172 :slot-definition slot-definition
2173 :slot-name slot-name initargs)))
2174 (apply #'make-instance
2175 (apply method-class-function object-class slot-definition
2178 (apply #'make-instance class :qualifiers qualifiers
2179 :lambda-list lambda-list :specializers specializers
2180 :documentation doc (append args initargs))))
2182 (defun early-method-function (early-method)
2183 (values (cadr early-method) (caddr early-method)))
2185 (defun early-method-class (early-method)
2186 (find-class (car (fifth early-method))))
2188 (defun early-method-standard-accessor-p (early-method)
2189 (let ((class (first (fifth early-method))))
2190 (or (eq class 'standard-reader-method)
2191 (eq class 'standard-writer-method)
2192 (eq class 'standard-boundp-method))))
2194 (defun early-method-standard-accessor-slot-name (early-method)
2195 (eighth (fifth early-method)))
2197 ;;; Fetch the specializers of an early method. This is basically just
2198 ;;; a simple accessor except that when the second argument is t, this
2199 ;;; converts the specializers from symbols into class objects. The
2200 ;;; class objects are cached in the early method, this makes
2201 ;;; bootstrapping faster because the class objects only have to be
2205 ;;; The second argument should only be passed as T by
2206 ;;; early-lookup-method. This is to implement the rule that only when
2207 ;;; there is more than one early method on a generic function is the
2208 ;;; conversion from class names to class objects done. This
2209 ;;; corresponds to the fact that we are only allowed to have one
2210 ;;; method on any generic function up until the time classes exist.
2211 (defun early-method-specializers (early-method &optional objectsp)
2212 (if (and (listp early-method)
2213 (eq (car early-method) :early-method))
2214 (cond ((eq objectsp t)
2215 (or (fourth early-method)
2216 (setf (fourth early-method)
2217 (mapcar #'find-class (cadddr (fifth early-method))))))
2219 (fourth (fifth early-method))))
2220 (error "~S is not an early-method." early-method)))
2222 (defun early-method-qualifiers (early-method)
2223 (second (fifth early-method)))
2225 (defun early-method-lambda-list (early-method)
2226 (third (fifth early-method)))
2228 (defun early-method-initargs (early-method)
2229 (fifth (fifth early-method)))
2231 (defun (setf early-method-initargs) (new-value early-method)
2232 (setf (fifth (fifth early-method)) new-value))
2234 (defun early-add-named-method (generic-function-name
2239 (let* (;; we don't need to deal with the :generic-function-class
2240 ;; argument here because the default,
2241 ;; STANDARD-GENERIC-FUNCTION, is right for all early generic
2242 ;; functions. (See REAL-ADD-NAMED-METHOD)
2243 (gf (ensure-generic-function generic-function-name))
2245 (dolist (m (early-gf-methods gf))
2246 (when (and (equal (early-method-specializers m) specializers)
2247 (equal (early-method-qualifiers m) qualifiers))
2249 (new (make-a-method 'standard-method
2255 (when existing (remove-method gf existing))
2256 (add-method gf new)))
2258 ;;; This is the early version of ADD-METHOD. Later this will become a
2259 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2260 ;;; special knowledge about ADD-METHOD.
2261 (defun add-method (generic-function method)
2262 (when (not (fsc-instance-p generic-function))
2263 (error "Early ADD-METHOD didn't get a funcallable instance."))
2264 (when (not (and (listp method) (eq (car method) :early-method)))
2265 (error "Early ADD-METHOD didn't get an early method."))
2266 (push method (early-gf-methods generic-function))
2267 (set-arg-info generic-function :new-method method)
2268 (unless (assoc (!early-gf-name generic-function)
2269 *!generic-function-fixups*
2271 (update-dfun generic-function)))
2273 ;;; This is the early version of REMOVE-METHOD. See comments on
2274 ;;; the early version of ADD-METHOD.
2275 (defun remove-method (generic-function method)
2276 (when (not (fsc-instance-p generic-function))
2277 (error "An early remove-method didn't get a funcallable instance."))
2278 (when (not (and (listp method) (eq (car method) :early-method)))
2279 (error "An early remove-method didn't get an early method."))
2280 (setf (early-gf-methods generic-function)
2281 (remove method (early-gf-methods generic-function)))
2282 (set-arg-info generic-function)
2283 (unless (assoc (!early-gf-name generic-function)
2284 *!generic-function-fixups*
2286 (update-dfun generic-function)))
2288 ;;; This is the early version of GET-METHOD. See comments on the early
2289 ;;; version of ADD-METHOD.
2290 (defun get-method (generic-function qualifiers specializers
2291 &optional (errorp t))
2292 (if (early-gf-p generic-function)
2293 (or (dolist (m (early-gf-methods generic-function))
2294 (when (and (or (equal (early-method-specializers m nil)
2296 (equal (early-method-specializers m t)
2298 (equal (early-method-qualifiers m) qualifiers))
2301 (error "can't get early method")
2303 (real-get-method generic-function qualifiers specializers errorp)))
2305 (defun !fix-early-generic-functions ()
2306 (let ((accessors nil))
2307 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2308 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2309 (dolist (early-gf-spec *!early-generic-functions*)
2310 (when (every #'early-method-standard-accessor-p
2311 (early-gf-methods (gdefinition early-gf-spec)))
2312 (push early-gf-spec accessors)))
2313 (dolist (spec (nconc accessors
2314 '(accessor-method-slot-name
2315 generic-function-methods
2320 slot-definition-location
2321 slot-definition-name
2324 class-precedence-list
2325 slot-boundp-using-class
2326 (setf slot-value-using-class)
2327 slot-value-using-class
2330 funcallable-standard-class-p
2333 (setq *!early-generic-functions*
2335 (delete spec *!early-generic-functions* :test #'equal))))
2337 (dolist (early-gf-spec *!early-generic-functions*)
2338 (/show early-gf-spec)
2339 (let* ((gf (gdefinition early-gf-spec))
2340 (methods (mapcar (lambda (early-method)
2341 (let ((args (copy-list (fifth
2344 (early-method-specializers
2346 (apply #'real-make-a-method args)))
2347 (early-gf-methods gf))))
2348 (setf (generic-function-method-class gf) *the-class-standard-method*)
2349 (setf (generic-function-method-combination gf)
2350 *standard-method-combination*)
2351 (set-methods gf methods)))
2353 (dolist (fn *!early-functions*)
2355 (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2357 (dolist (fixup *!generic-function-fixups*)
2359 (let* ((fspec (car fixup))
2360 (gf (gdefinition fspec))
2361 (methods (mapcar (lambda (method)
2362 (let* ((lambda-list (first method))
2363 (specializers (second method))
2364 (method-fn-name (third method))
2365 (fn-name (or method-fn-name fspec))
2366 (fn (fdefinition fn-name))
2370 (lambda (args next-methods)
2374 `(call ,fn-name)))))
2375 (declare (type function fn))
2376 (make-a-method 'standard-method
2383 (setf (generic-function-method-class gf) *the-class-standard-method*)
2384 (setf (generic-function-method-combination gf)
2385 *standard-method-combination*)
2386 (set-methods gf methods))))
2387 (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2389 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2390 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2391 ;;; is really implemented.
2392 (defun parse-defmethod (cdr-of-form)
2393 (declare (list cdr-of-form))
2394 (let ((name (pop cdr-of-form))
2397 (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2398 (push (pop cdr-of-form) qualifiers)
2399 (return (setq qualifiers (nreverse qualifiers)))))
2400 (setq spec-ll (pop cdr-of-form))
2401 (values name qualifiers spec-ll cdr-of-form)))
2403 (defun parse-specializers (specializers)
2404 (declare (list specializers))
2405 (flet ((parse (spec)
2406 (let ((result (specializer-from-type spec)))
2407 (if (specializerp result)
2410 (error "~S was used as a specializer,~%~
2411 but is not the name of a class."
2413 (error "~S is not a legal specializer." spec))))))
2414 (mapcar #'parse specializers)))
2416 (defun unparse-specializers (specializers-or-method)
2417 (if (listp specializers-or-method)
2418 (flet ((unparse (spec)
2419 (if (specializerp spec)
2420 (let ((type (specializer-type spec)))
2421 (if (and (consp type)
2422 (eq (car type) 'class))
2423 (let* ((class (cadr type))
2424 (class-name (class-name class)))
2425 (if (eq class (find-class class-name nil))
2429 (error "~S is not a legal specializer." spec))))
2430 (mapcar #'unparse specializers-or-method))
2431 (unparse-specializers (method-specializers specializers-or-method))))
2433 (defun parse-method-or-spec (spec &optional (errorp t))
2434 (let (gf method name temp)
2437 gf (method-generic-function method)
2438 temp (and gf (generic-function-name gf))
2440 (make-method-spec temp
2441 (method-qualifiers method)
2442 (unparse-specializers
2443 (method-specializers method)))
2444 (make-symbol (format nil "~S" method))))
2445 (multiple-value-bind (gf-spec quals specls)
2446 (parse-defmethod spec)
2447 (and (setq gf (and (or errorp (fboundp gf-spec))
2448 (gdefinition gf-spec)))
2449 (let ((nreq (compute-discriminating-function-arglist-info gf)))
2450 (setq specls (append (parse-specializers specls)
2451 (make-list (- nreq (length specls))
2455 (setq method (get-method gf quals specls errorp))
2458 gf-spec quals (unparse-specializers specls))))))))
2459 (values gf method name)))
2461 (defun extract-parameters (specialized-lambda-list)
2462 (multiple-value-bind (parameters ignore1 ignore2)
2463 (parse-specialized-lambda-list specialized-lambda-list)
2464 (declare (ignore ignore1 ignore2))
2467 (defun extract-lambda-list (specialized-lambda-list)
2468 (multiple-value-bind (ignore1 lambda-list ignore2)
2469 (parse-specialized-lambda-list specialized-lambda-list)
2470 (declare (ignore ignore1 ignore2))
2473 (defun extract-specializer-names (specialized-lambda-list)
2474 (multiple-value-bind (ignore1 ignore2 specializers)
2475 (parse-specialized-lambda-list specialized-lambda-list)
2476 (declare (ignore ignore1 ignore2))
2479 (defun extract-required-parameters (specialized-lambda-list)
2480 (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2481 (parse-specialized-lambda-list specialized-lambda-list)
2482 (declare (ignore ignore1 ignore2 ignore3))
2483 required-parameters))
2485 (define-condition specialized-lambda-list-error
2486 (reference-condition simple-program-error)
2488 (:default-initargs :references (list '(:ansi-cl :section (3 4 3)))))
2490 (defun parse-specialized-lambda-list
2492 &optional supplied-keywords (allowed-keywords '(&optional &rest &key &aux))
2493 &aux (specialized-lambda-list-keywords
2494 '(&optional &rest &key &allow-other-keys &aux)))
2495 (let ((arg (car arglist)))
2496 (cond ((null arglist) (values nil nil nil nil))
2498 (values nil arglist nil nil))
2499 ((memq arg lambda-list-keywords)
2500 ;; non-standard lambda-list-keywords are errors.
2501 (unless (memq arg specialized-lambda-list-keywords)
2502 (error 'specialized-lambda-list-error
2503 :format-control "unknown specialized-lambda-list ~
2505 :format-arguments (list arg)))
2506 ;; no multiple &rest x &rest bla specifying
2507 (when (memq arg supplied-keywords)
2508 (error 'specialized-lambda-list-error
2509 :format-control "multiple occurrence of ~
2510 specialized-lambda-list keyword ~S~%"
2511 :format-arguments (list arg)))
2512 ;; And no placing &key in front of &optional, either.
2513 (unless (memq arg allowed-keywords)
2514 (error 'specialized-lambda-list-error
2515 :format-control "misplaced specialized-lambda-list ~
2517 :format-arguments (list arg)))
2518 ;; When we are at a lambda-list keyword, the parameters
2519 ;; don't include the lambda-list keyword; the lambda-list
2520 ;; does include the lambda-list keyword; and no
2521 ;; specializers are allowed to follow the lambda-list
2522 ;; keywords (at least for now).
2523 (multiple-value-bind (parameters lambda-list)
2524 (parse-specialized-lambda-list (cdr arglist)
2525 (cons arg supplied-keywords)
2527 (cons '&allow-other-keys
2528 (cdr (member arg allowed-keywords)))
2529 (cdr (member arg allowed-keywords))))
2530 (when (and (eq arg '&rest)
2531 (or (null lambda-list)
2532 (memq (car lambda-list)
2533 specialized-lambda-list-keywords)
2534 (not (or (null (cadr lambda-list))
2535 (memq (cadr lambda-list)
2536 specialized-lambda-list-keywords)))))
2537 (error 'specialized-lambda-list-error
2539 "in a specialized-lambda-list, excactly one ~
2540 variable must follow &REST.~%"
2541 :format-arguments nil))
2543 (cons arg lambda-list)
2547 ;; After a lambda-list keyword there can be no specializers.
2548 (multiple-value-bind (parameters lambda-list)
2549 (parse-specialized-lambda-list (cdr arglist)
2552 (values (cons (if (listp arg) (car arg) arg) parameters)
2553 (cons arg lambda-list)
2557 (multiple-value-bind (parameters lambda-list specializers required)
2558 (parse-specialized-lambda-list (cdr arglist))
2559 (values (cons (if (listp arg) (car arg) arg) parameters)
2560 (cons (if (listp arg) (car arg) arg) lambda-list)
2561 (cons (if (listp arg) (cadr arg) t) specializers)
2562 (cons (if (listp arg) (car arg) arg) required)))))))
2564 (setq *boot-state* 'early)
2566 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2567 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2568 ;;; walker stuff was only used for implementing stuff like that; maybe
2569 ;;; it's not needed any more? Hunt down what it was used for and see.
2571 (defmacro with-slots (slots instance &body body)
2572 (let ((in (gensym)))
2573 `(let ((,in ,instance))
2574 (declare (ignorable ,in))
2575 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2578 (and (symbolp instance)
2579 `((declare (%variable-rebinding ,in ,instance)))))
2581 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2583 (if (symbolp slot-entry)
2587 (if (symbolp slot-entry)
2589 (cadr slot-entry))))
2591 (slot-value ,in ',slot-name))))
2595 (defmacro with-accessors (slots instance &body body)
2596 (let ((in (gensym)))
2597 `(let ((,in ,instance))
2598 (declare (ignorable ,in))
2599 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2602 (and (symbolp instance)
2603 `((declare (%variable-rebinding ,in ,instance)))))
2605 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2606 (let ((var-name (car slot-entry))
2607 (accessor-name (cadr slot-entry)))
2608 `(,var-name (,accessor-name ,in))))