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))
392 (getf (getf initargs :plist)
393 :pv-table-symbol)))))))
395 (defun interned-symbol-p (x)
396 (and (symbolp x) (symbol-package x)))
398 (defun make-defmethod-form (name qualifiers specializers
399 unspecialized-lambda-list method-class-name
400 initargs-form &optional pv-table-symbol)
403 (if (and (interned-symbol-p (fun-name-block-name name))
404 (every #'interned-symbol-p qualifiers)
407 (and (eq (car s) 'eql)
409 (let ((sv (constant-form-value (cadr s))))
410 (or (interned-symbol-p sv)
413 (standard-char-p sv)))))
414 (interned-symbol-p s)))
416 (consp initargs-form)
417 (eq (car initargs-form) 'list*)
418 (memq (cadr initargs-form) '(:function :fast-function))
419 (consp (setq fn (caddr initargs-form)))
420 (eq (car fn) 'function)
421 (consp (setq fn-lambda (cadr fn)))
422 (eq (car fn-lambda) 'lambda))
423 (let* ((specls (mapcar (lambda (specl)
425 `(,(car specl) ,(eval (cadr specl)))
428 (mname `(,(if (eq (cadr initargs-form) :function)
429 'slow-method 'fast-method)
430 ,name ,@qualifiers ,specls)))
432 (defun ,mname ,(cadr fn-lambda)
434 ,(make-defmethod-form-internal
435 name qualifiers `',specls
436 unspecialized-lambda-list method-class-name
437 `(list* ,(cadr initargs-form)
439 ,@(cdddr initargs-form))
441 (make-defmethod-form-internal
443 `(list ,@(mapcar (lambda (specializer)
444 (if (consp specializer)
445 ``(,',(car specializer)
446 ,,(cadr specializer))
449 unspecialized-lambda-list
454 (defun make-defmethod-form-internal
455 (name qualifiers specializers-form unspecialized-lambda-list
456 method-class-name initargs-form &optional pv-table-symbol)
462 ',unspecialized-lambda-list
464 ;; Paper over a bug in KCL by passing the cache-symbol here in
465 ;; addition to in the list. FIXME: We should no longer need to do
466 ;; this, since the CLOS code is now SBCL-specific, and doesn't
467 ;; need to be ported to every buggy compiler in existence.
469 (sb-c:source-location)))
471 (defmacro make-method-function (method-lambda &environment env)
472 (make-method-function-internal method-lambda env))
474 (defun make-method-function-internal (method-lambda &optional env)
475 (multiple-value-bind (proto-gf proto-method)
476 (prototypes-for-make-method-lambda nil)
477 (multiple-value-bind (method-function-lambda initargs)
478 (make-method-lambda proto-gf proto-method method-lambda env)
479 (make-method-initargs-form proto-gf
481 method-function-lambda
485 (defun add-method-declarations (name qualifiers lambda-list body env)
486 (declare (ignore env))
487 (multiple-value-bind (parameters unspecialized-lambda-list specializers)
488 (parse-specialized-lambda-list lambda-list)
489 (multiple-value-bind (real-body declarations documentation)
491 (values `(lambda ,unspecialized-lambda-list
492 ,@(when documentation `(,documentation))
493 ;; (Old PCL code used a somewhat different style of
494 ;; list for %METHOD-NAME values. Our names use
495 ;; ,@QUALIFIERS instead of ,QUALIFIERS so that the
496 ;; method names look more like what you see in a
499 ;; FIXME: As of sbcl-0.7.0.6, code elsewhere, at
500 ;; least the code to set up named BLOCKs around the
501 ;; bodies of methods, depends on the function's base
502 ;; name being the first element of the %METHOD-NAME
503 ;; list. It would be good to remove this dependency,
504 ;; perhaps by building the BLOCK here, or by using
505 ;; another declaration (e.g. %BLOCK-NAME), so that
506 ;; our method debug names are free to have any format,
507 ;; e.g. (:METHOD PRINT-OBJECT :AROUND (CLOWN T)).
509 ;; Further, as of sbcl-0.7.9.10, the code to
510 ;; implement NO-NEXT-METHOD is coupled to the form of
511 ;; this declaration; see the definition of
512 ;; CALL-NO-NEXT-METHOD (and the passing of
513 ;; METHOD-NAME-DECLARATION arguments around the
514 ;; various CALL-NEXT-METHOD logic).
515 (declare (%method-name (,name
518 (declare (%method-lambda-list ,@lambda-list))
521 unspecialized-lambda-list specializers))))
523 (defun real-make-method-initargs-form (proto-gf proto-method
524 method-lambda initargs env)
525 (declare (ignore proto-gf proto-method))
526 (unless (and (consp method-lambda)
527 (eq (car method-lambda) 'lambda))
528 (error "The METHOD-LAMBDA argument to MAKE-METHOD-FUNCTION, ~S, ~
529 is not a lambda form."
531 (make-method-initargs-form-internal method-lambda initargs env))
533 (unless (fboundp 'make-method-initargs-form)
534 (setf (gdefinition 'make-method-initargs-form)
535 (symbol-function 'real-make-method-initargs-form)))
537 (defun real-make-method-lambda (proto-gf proto-method method-lambda env)
538 (declare (ignore proto-gf proto-method))
539 (make-method-lambda-internal method-lambda env))
541 ;;; a helper function for creating Python-friendly type declarations
542 ;;; in DEFMETHOD forms
543 (defun parameter-specializer-declaration-in-defmethod (parameter specializer)
544 (cond ((and (consp specializer)
545 (eq (car specializer) 'eql))
546 ;; KLUDGE: ANSI, in its wisdom, says that
547 ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at
548 ;; DEFMETHOD expansion time. Thus, although one might think
550 ;; (DEFMETHOD FOO ((X PACKAGE)
553 ;; the PACKAGE and (EQL 12) forms are both parallel type
554 ;; names, they're not, as is made clear when you do
555 ;; (DEFMETHOD FOO ((X PACKAGE)
558 ;; where Y needs to be a symbol named "BAR", not some cons
559 ;; made by (CONS 'QUOTE 'BAR). I.e. when the
560 ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument
561 ;; to be of type (EQL X). It'd be easy to transform one to
562 ;; the other, but it'd be somewhat messier to do so while
563 ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd
564 ;; once. (The new code wouldn't be messy, but it'd require a
565 ;; big transformation of the old code.) So instead we punt.
569 ;; KLUDGE: For some low-level implementation
570 ;; classes, perhaps because of some problems related
571 ;; to the incomplete integration of PCL into SBCL's
572 ;; type system, some specializer classes can't be
573 ;; declared as argument types. E.g.
574 ;; (DEFMETHOD FOO ((X SLOT-OBJECT))
575 ;; (DECLARE (TYPE SLOT-OBJECT X))
578 ;; (DEFSTRUCT BAR A B)
580 ;; perhaps because of the way that STRUCTURE-OBJECT
581 ;; inherits both from SLOT-OBJECT and from
582 ;; SB-KERNEL:INSTANCE. In an effort to sweep such
583 ;; problems under the rug, we exclude these problem
584 ;; cases by blacklisting them here. -- WHN 2001-01-19
587 ((not (eq *boot-state* 'complete))
588 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
589 ;; types which don't match their specializers. (Specifically,
590 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
591 ;; second argument.) Hopefully it only does this kind of
592 ;; weirdness when bootstrapping.. -- WHN 20000610
594 ((var-globally-special-p parameter)
595 ;; KLUDGE: Don't declare types for global special variables
596 ;; -- our rebinding magic for SETQ cases don't work right
599 ;; FIXME: It would be better to detect the SETQ earlier and
600 ;; skip declarations for specials only when needed, not
606 ;; Otherwise, we can usually make Python very happy.
608 ;; KLUDGE: Since INFO doesn't work right for class objects here,
609 ;; and they are valid specializers, see if the specializer is
610 ;; a named class, and use the name in that case -- otherwise
611 ;; the class instance is ok, since info will just return NIL, NIL.
613 ;; We still need to deal with the class case too, but at
614 ;; least #.(find-class 'integer) and integer as equivalent
615 ;; specializers with this.
616 (let* ((specializer (if (and (typep specializer 'class)
617 (eq specializer (find-class (class-name specializer))))
618 (class-name specializer)
620 (kind (info :type :kind specializer)))
622 (flet ((specializer-class ()
623 (if (typep specializer 'class)
625 (find-class specializer nil))))
627 ((:primitive) `(type ,specializer ,parameter))
629 (let ((class (specializer-class)))
630 ;; CLASS can be null here if the user has erroneously
631 ;; tried to use a defined type as a specializer; it
632 ;; can be a non-BUILT-IN-CLASS if the user defines a
633 ;; type and calls (SETF FIND-CLASS) in a consistent
635 (when (and class (typep class 'built-in-class))
636 `(type ,specializer ,parameter))))
638 (let ((class (specializer-class)))
641 (if (typep class '(or built-in-class structure-class))
642 `(type ,specializer ,parameter)
643 ;; don't declare CLOS classes as parameters;
644 ;; it's too expensive.
647 ;; we can get here, and still not have a failure
648 ;; case, by doing MOP programming like (PROGN
649 ;; (ENSURE-CLASS 'FOO) (DEFMETHOD BAR ((X FOO))
650 ;; ...)). Best to let the user know we haven't
651 ;; been able to extract enough information:
653 "~@<can't find type for presumed class ~S in ~S.~@:>"
655 'parameter-specializer-declaration-in-defmethod)
657 ((:forthcoming-defclass-type)
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)
750 (walk-method-lambda method-lambda
755 (multiple-value-bind (walked-lambda-body
757 walked-documentation)
758 (parse-body (cddr walked-lambda))
759 (declare (ignore walked-documentation))
760 (when (or next-method-p-p call-next-method-p)
761 (setq plist (list* :needs-next-methods-p t plist)))
762 (when (some #'cdr slots)
763 (multiple-value-bind (slot-name-lists call-list)
764 (slot-name-lists-from-slots slots calls)
765 (let ((pv-table-symbol (make-symbol "pv-table")))
767 `(,@(when slot-name-lists
768 `(:slot-name-lists ,slot-name-lists))
770 `(:call-list ,call-list))
771 :pv-table-symbol ,pv-table-symbol
773 (setq walked-lambda-body
774 `((pv-binding (,required-parameters
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
802 ,@walked-lambda-body))
805 ,@(when documentation
806 `(:documentation ,documentation)))))))))))
808 (unless (fboundp 'make-method-lambda)
809 (setf (gdefinition 'make-method-lambda)
810 (symbol-function 'real-make-method-lambda)))
812 (defmacro simple-lexical-method-functions ((lambda-list
818 ,method-args ,next-methods
819 (bind-simple-lexical-method-functions (,method-args ,next-methods
821 (bind-args (,lambda-list ,method-args)
824 (defmacro fast-lexical-method-functions ((lambda-list
830 `(bind-fast-lexical-method-functions (,args ,rest-arg ,next-method-call ,lmf-options)
831 (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg)
834 (defmacro bind-simple-lexical-method-functions
835 ((method-args next-methods (&key call-next-method-p next-method-p-p setq-p
836 closurep applyp method-name-declaration))
839 (if (not (or call-next-method-p setq-p closurep next-method-p-p applyp))
842 `(let ((.next-method. (car ,next-methods))
843 (,next-methods (cdr ,next-methods)))
844 (declare (ignorable .next-method. ,next-methods))
845 (flet (,@(and call-next-method-p
848 ,@(if (safe-code-p env)
849 `((%check-cnm-args cnm-args
851 ',method-name-declaration))
854 (funcall (if (std-instance-p .next-method.)
855 (method-function .next-method.)
856 .next-method.) ; for early methods
857 (or cnm-args ,method-args)
859 (apply #'call-no-next-method
860 ',method-name-declaration
861 (or cnm-args ,method-args))))))
862 ,@(and next-method-p-p
864 (not (null .next-method.))))))
867 (defun call-no-next-method (method-name-declaration &rest args)
868 (destructuring-bind (name) method-name-declaration
869 (destructuring-bind (name &rest qualifiers-and-specializers) name
870 ;; KLUDGE: inefficient traversal, but hey. This should only
871 ;; happen on the slow error path anyway.
872 (let* ((qualifiers (butlast qualifiers-and-specializers))
873 (specializers (car (last qualifiers-and-specializers)))
874 (method (find-method (gdefinition name) qualifiers specializers)))
875 (apply #'no-next-method
876 (method-generic-function method)
880 (defstruct (method-call (:copier nil))
881 (function #'identity :type function)
884 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
886 (defmacro invoke-method-call1 (function args cm-args)
887 `(let ((.function. ,function)
889 (.cm-args. ,cm-args))
890 (if (and .cm-args. (null (cdr .cm-args.)))
891 (funcall .function. .args. (car .cm-args.))
892 (apply .function. .args. .cm-args.))))
894 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
895 `(invoke-method-call1 (method-call-function ,method-call)
897 `(list* ,@required-args+rest-arg)
898 `(list ,@required-args+rest-arg))
899 (method-call-call-method-args ,method-call)))
901 (defstruct (fast-method-call (:copier nil))
902 (function #'identity :type function)
907 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
909 (defmacro fmc-funcall (fn pv-cell next-method-call &rest args)
910 `(funcall ,fn ,pv-cell ,next-method-call ,@args))
912 (defmacro invoke-fast-method-call (method-call &rest required-args+rest-arg)
913 `(fmc-funcall (fast-method-call-function ,method-call)
914 (fast-method-call-pv-cell ,method-call)
915 (fast-method-call-next-method-call ,method-call)
916 ,@required-args+rest-arg))
918 (defstruct (fast-instance-boundp (:copier nil))
919 (index 0 :type fixnum))
921 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
923 (eval-when (:compile-toplevel :load-toplevel :execute)
924 (defvar *allow-emf-call-tracing-p* nil)
925 (defvar *enable-emf-call-tracing-p* #-sb-show nil #+sb-show t))
927 ;;;; effective method functions
929 (defvar *emf-call-trace-size* 200)
930 (defvar *emf-call-trace* nil)
931 (defvar *emf-call-trace-index* 0)
933 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
934 ;;; without explanation. It appears to be intended for debugging, so
935 ;;; it might be useful someday, so I haven't deleted it.
936 ;;; But it isn't documented and isn't used for anything now, so
937 ;;; I've conditionalized it out of the base system. -- WHN 19991213
939 (defun show-emf-call-trace ()
940 (when *emf-call-trace*
941 (let ((j *emf-call-trace-index*)
942 (*enable-emf-call-tracing-p* nil))
943 (format t "~&(The oldest entries are printed first)~%")
944 (dotimes-fixnum (i *emf-call-trace-size*)
945 (let ((ct (aref *emf-call-trace* j)))
946 (when ct (print ct)))
948 (when (= j *emf-call-trace-size*)
951 (defun trace-emf-call-internal (emf format args)
952 (unless *emf-call-trace*
953 (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
954 (setf (aref *emf-call-trace* *emf-call-trace-index*)
955 (list* emf format args))
956 (incf *emf-call-trace-index*)
957 (when (= *emf-call-trace-index* *emf-call-trace-size*)
958 (setq *emf-call-trace-index* 0)))
960 (defmacro trace-emf-call (emf format args)
961 (when *allow-emf-call-tracing-p*
962 `(when *enable-emf-call-tracing-p*
963 (trace-emf-call-internal ,emf ,format ,args))))
965 (defmacro invoke-effective-method-function-fast
966 (emf restp &rest required-args+rest-arg)
968 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
969 (invoke-fast-method-call ,emf ,@required-args+rest-arg)))
971 (defmacro invoke-effective-method-function (emf restp
972 &rest required-args+rest-arg)
973 (unless (constantp restp)
974 (error "The RESTP argument is not constant."))
975 ;; FIXME: The RESTP handling here is confusing and maybe slightly
976 ;; broken if RESTP evaluates to a non-self-evaluating form. E.g. if
977 ;; (INVOKE-EFFECTIVE-METHOD-FUNCTION EMF '(ERROR "gotcha") ...)
978 ;; then TRACE-EMF-CALL-CALL-INTERNAL might die on a gotcha error.
979 (setq restp (constant-form-value restp))
981 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
982 (cond ((typep ,emf 'fast-method-call)
983 (invoke-fast-method-call ,emf ,@required-args+rest-arg))
984 ;; "What," you may wonder, "do these next two clauses do?"
985 ;; In that case, you are not a PCL implementor, for they
986 ;; considered this to be self-documenting.:-| Or CSR, for
987 ;; that matter, since he can also figure it out by looking
988 ;; at it without breaking stride. For the rest of us,
989 ;; though: From what the code is doing with .SLOTS. and
990 ;; whatnot, evidently it's implementing SLOT-VALUEish and
991 ;; GET-SLOT-VALUEish things. Then we can reason backwards
992 ;; and conclude that setting EMF to a FIXNUM is an
993 ;; optimized way to represent these slot access operations.
994 ,@(when (and (null restp) (= 1 (length required-args+rest-arg)))
995 `(((typep ,emf 'fixnum)
996 (let* ((.slots. (get-slots-or-nil
997 ,(car required-args+rest-arg)))
998 (value (when .slots. (clos-slots-ref .slots. ,emf))))
999 (if (eq value +slot-unbound+)
1000 (slot-unbound-internal ,(car required-args+rest-arg)
1003 ,@(when (and (null restp) (= 2 (length required-args+rest-arg)))
1004 `(((typep ,emf 'fixnum)
1005 (let ((.new-value. ,(car required-args+rest-arg))
1006 (.slots. (get-slots-or-nil
1007 ,(cadr required-args+rest-arg))))
1009 (setf (clos-slots-ref .slots. ,emf) .new-value.))))))
1010 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
1011 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
1012 ;; there was no explanation and presumably the code is 10+
1013 ;; years stale, I simply deleted it. -- WHN)
1017 (invoke-method-call ,emf ,restp ,@required-args+rest-arg))
1020 `(apply (the function ,emf) ,@required-args+rest-arg)
1021 `(funcall (the function ,emf)
1022 ,@required-args+rest-arg))))))))
1024 (defun invoke-emf (emf args)
1025 (trace-emf-call emf t args)
1028 (let* ((arg-info (fast-method-call-arg-info emf))
1029 (restp (cdr arg-info))
1030 (nreq (car arg-info)))
1032 (let* ((rest-args (nthcdr nreq args))
1033 (req-args (ldiff args rest-args)))
1034 (apply (fast-method-call-function emf)
1035 (fast-method-call-pv-cell emf)
1036 (fast-method-call-next-method-call emf)
1037 (nconc req-args (list rest-args))))
1040 (invoke-fast-method-call emf)
1041 (error 'simple-program-error
1042 :format-control "invalid number of arguments: 0"
1043 :format-arguments nil)))
1046 (invoke-fast-method-call emf (car args))
1047 (error 'simple-program-error
1048 :format-control "invalid number of arguments: 1"
1049 :format-arguments nil)))
1052 (invoke-fast-method-call emf (car args) (cadr args))
1053 (error 'simple-program-error
1054 :format-control "invalid number of arguments: 2"
1055 :format-arguments nil)))
1057 (apply (fast-method-call-function emf)
1058 (fast-method-call-pv-cell emf)
1059 (fast-method-call-next-method-call emf)
1062 (apply (method-call-function emf)
1064 (method-call-call-method-args emf)))
1067 (error 'simple-program-error
1068 :format-control "invalid number of arguments: 0"
1069 :format-arguments nil))
1071 (let* ((slots (get-slots (car args)))
1072 (value (clos-slots-ref slots emf)))
1073 (if (eq value +slot-unbound+)
1074 (slot-unbound-internal (car args) emf)
1077 (setf (clos-slots-ref (get-slots (cadr args)) emf)
1079 (t (error 'simple-program-error
1080 :format-control "invalid number of arguments"
1081 :format-arguments nil))))
1082 (fast-instance-boundp
1083 (if (or (null args) (cdr args))
1084 (error 'simple-program-error
1085 :format-control "invalid number of arguments"
1086 :format-arguments nil)
1087 (let ((slots (get-slots (car args))))
1088 (not (eq (clos-slots-ref slots (fast-instance-boundp-index emf))
1094 (defmacro fast-narrowed-emf (emf)
1095 ;; INVOKE-EFFECTIVE-METHOD-FUNCTION has code in it to dispatch on
1096 ;; the possibility that EMF might be of type FIXNUM (as an optimized
1097 ;; representation of a slot accessor). But as far as I (WHN
1098 ;; 2002-06-11) can tell, it's impossible for such a representation
1099 ;; to end up as .NEXT-METHOD-CALL. By reassuring INVOKE-E-M-F that
1100 ;; when called from this context it needn't worry about the FIXNUM
1101 ;; case, we can keep those cases from being compiled, which is good
1102 ;; both because it saves bytes and because it avoids annoying type
1103 ;; mismatch compiler warnings.
1105 ;; KLUDGE: In sbcl-0.7.4.29, the compiler's type system isn't smart
1106 ;; enough about NOT and intersection types to benefit from a (NOT
1107 ;; FIXNUM) declaration here. -- WHN 2002-06-12 (FIXME: maybe it is
1108 ;; now... -- CSR, 2003-06-07)
1110 ;; FIXME: Might the FUNCTION type be omittable here, leaving only
1111 ;; METHOD-CALLs? Failing that, could this be documented somehow?
1112 ;; (It'd be nice if the types involved could be understood without
1113 ;; solving the halting problem.)
1114 `(the (or function method-call fast-method-call)
1117 (defmacro fast-call-next-method-body ((args next-method-call rest-arg)
1118 method-name-declaration
1120 `(if ,next-method-call
1121 ,(let ((call `(invoke-effective-method-function
1122 (fast-narrowed-emf ,next-method-call)
1123 ,(not (null rest-arg))
1125 ,@(when rest-arg `(,rest-arg)))))
1129 `(&rest ,rest-arg)))
1133 (call-no-next-method ',method-name-declaration
1138 (defmacro bind-fast-lexical-method-functions
1139 ((args rest-arg next-method-call (&key
1142 method-name-declaration
1148 (let* ((all-params (append args (when rest-arg (list rest-arg))))
1149 (rebindings (when (or setq-p call-next-method-p)
1150 (mapcar (lambda (x) (list x x)) all-params))))
1151 (if (not (or call-next-method-p setq-p closurep next-method-p-p applyp))
1154 `(flet (,@(when call-next-method-p
1155 `((call-next-method (&rest cnm-args)
1156 (declare (muffle-conditions code-deletion-note))
1157 ,@(if (safe-code-p env)
1158 `((%check-cnm-args cnm-args (list ,@args)
1159 ',method-name-declaration))
1161 (fast-call-next-method-body (,args
1164 ,method-name-declaration
1166 ,@(when next-method-p-p
1169 (not (null ,next-method-call))))))
1171 ,@(when rebindings `((declare (ignorable ,@all-params))))
1174 ;;; CMUCL comment (Gerd Moellmann):
1176 ;;; The standard says it's an error if CALL-NEXT-METHOD is called with
1177 ;;; arguments, and the set of methods applicable to those arguments is
1178 ;;; different from the set of methods applicable to the original
1179 ;;; method arguments. (According to Barry Margolin, this rule was
1180 ;;; probably added to ensure that before and around methods are always
1181 ;;; run before primary methods.)
1183 ;;; This could be optimized for the case that the generic function
1184 ;;; doesn't have hairy methods, does have standard method combination,
1185 ;;; is a standard generic function, there are no methods defined on it
1186 ;;; for COMPUTE-APPLICABLE-METHODS and probably a lot more of such
1187 ;;; preconditions. That looks hairy and is probably not worth it,
1188 ;;; because this check will never be fast.
1189 (defun %check-cnm-args (cnm-args orig-args method-name-declaration)
1191 (let* ((gf (fdefinition (caar method-name-declaration)))
1192 (omethods (compute-applicable-methods gf orig-args))
1193 (nmethods (compute-applicable-methods gf cnm-args)))
1194 (unless (equal omethods nmethods)
1195 (error "~@<The set of methods ~S applicable to argument~P ~
1196 ~{~S~^, ~} to call-next-method is different from ~
1197 the set of methods ~S applicable to the original ~
1198 method argument~P ~{~S~^, ~}.~@:>"
1199 nmethods (length cnm-args) cnm-args omethods
1200 (length orig-args) orig-args)))))
1202 (defmacro bind-args ((lambda-list args) &body body)
1203 (let ((args-tail '.args-tail.)
1206 (flet ((process-var (var)
1207 (if (memq var lambda-list-keywords)
1210 (&optional (setq state 'optional))
1211 (&key (setq state 'key))
1213 (&rest (setq state 'rest))
1214 (&aux (setq state 'aux))
1217 "encountered the non-standard lambda list keyword ~S"
1221 (required `((,var (pop ,args-tail))))
1222 (optional (cond ((not (consp var))
1223 `((,var (when ,args-tail
1224 (pop ,args-tail)))))
1226 `((,(car var) (if ,args-tail
1230 `((,(caddr var) ,args-tail)
1231 (,(car var) (if ,args-tail
1234 (rest `((,var ,args-tail)))
1235 (key (cond ((not (consp var))
1237 (get-key-arg-tail ,(keywordicate var)
1240 (multiple-value-bind (keyword variable)
1241 (if (consp (car var))
1244 (values (keywordicate (car var))
1246 `((,key (get-key-arg-tail ',keyword
1252 (multiple-value-bind (keyword variable)
1253 (if (consp (car var))
1256 (values (keywordicate (car var))
1258 `((,key (get-key-arg-tail ',keyword
1265 (let ((bindings (mapcan #'process-var lambda-list)))
1266 `(let* ((,args-tail ,args)
1269 ,@(when (eq state 'optional)
1270 `((unless (null ,args-tail)
1271 (error 'simple-program-error
1272 :format-control "surplus arguments: ~S"
1273 :format-arguments (list ,args-tail)))))))
1274 (declare (ignorable ,args-tail .dummy0.))
1277 (defun get-key-arg-tail (keyword list)
1278 (loop for (key . tail) on list by #'cddr
1280 ;; FIXME: Do we want to export this symbol? Or maybe use an
1281 ;; (ERROR 'SIMPLE-PROGRAM-ERROR) form?
1282 (sb-c::%odd-key-args-error)
1283 when (eq key keyword)
1286 (defun walk-method-lambda (method-lambda required-parameters env slots calls)
1287 (let ((call-next-method-p nil) ; flag indicating that CALL-NEXT-METHOD
1288 ; should be in the method definition
1289 (closurep nil) ; flag indicating that #'CALL-NEXT-METHOD
1290 ; was seen in the body of a method
1291 (next-method-p-p nil) ; flag indicating that NEXT-METHOD-P
1292 ; should be in the method definition
1294 (flet ((walk-function (form context env)
1295 (cond ((not (eq context :eval)) form)
1296 ;; FIXME: Jumping to a conclusion from the way it's used
1297 ;; above, perhaps CONTEXT should be called SITUATION
1298 ;; (after the term used in the ANSI specification of
1299 ;; EVAL-WHEN) and given modern ANSI keyword values
1300 ;; like :LOAD-TOPLEVEL.
1301 ((not (listp form)) form)
1302 ((eq (car form) 'call-next-method)
1303 (setq call-next-method-p t)
1305 ((eq (car form) 'next-method-p)
1306 (setq next-method-p-p t)
1308 ((memq (car form) '(setq multiple-value-setq))
1309 ;; FIXME: this is possibly a little strong as
1310 ;; conditions go. Ideally we would want to detect
1311 ;; which, if any, of the method parameters are
1312 ;; being set, and communicate that information to
1313 ;; e.g. SPLIT-DECLARATIONS. However, the brute
1314 ;; force method doesn't really cost much; a little
1315 ;; loss of discrimination over IGNORED variables
1316 ;; should be all. -- CSR, 2004-07-01
1319 ((and (eq (car form) 'function)
1320 (cond ((eq (cadr form) 'call-next-method)
1321 (setq call-next-method-p t)
1324 ((eq (cadr form) 'next-method-p)
1325 (setq next-method-p-p t)
1329 ((and (memq (car form)
1330 '(slot-value set-slot-value slot-boundp))
1331 (constantp (caddr form)))
1332 (let ((parameter (can-optimize-access form
1335 (let ((fun (ecase (car form)
1336 (slot-value #'optimize-slot-value)
1337 (set-slot-value #'optimize-set-slot-value)
1338 (slot-boundp #'optimize-slot-boundp))))
1339 (funcall fun slots parameter form))))
1340 ((and (eq (car form) 'apply)
1342 (eq (car (cadr form)) 'function)
1343 (generic-function-name-p (cadr (cadr form))))
1344 (optimize-generic-function-call
1345 form required-parameters env slots calls))
1346 ((generic-function-name-p (car form))
1347 (optimize-generic-function-call
1348 form required-parameters env slots calls))
1351 (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1352 (values walked-lambda
1358 (defun generic-function-name-p (name)
1359 (and (legal-fun-name-p name)
1361 (if (eq *boot-state* 'complete)
1362 (standard-generic-function-p (gdefinition name))
1363 (funcallable-instance-p (gdefinition name)))))
1365 (defvar *method-function-plist* (make-hash-table :test 'eq))
1367 (defun method-function-plist (method-function)
1368 (gethash method-function *method-function-plist*))
1370 (defun (setf method-function-plist) (val method-function)
1371 (setf (gethash method-function *method-function-plist*) val))
1373 (defun method-function-get (method-function key &optional default)
1374 (getf (method-function-plist method-function) key default))
1376 (defun (setf method-function-get)
1377 (val method-function key)
1378 (setf (getf (method-function-plist method-function) key) val))
1380 (defun method-function-pv-table (method-function)
1381 (method-function-get method-function :pv-table))
1383 (defun method-function-method (method-function)
1384 (method-function-get method-function :method))
1386 (defun method-function-needs-next-methods-p (method-function)
1387 (method-function-get method-function :needs-next-methods-p t))
1389 (defmacro method-function-closure-generator (method-function)
1390 `(method-function-get ,method-function 'closure-generator))
1392 (defun load-defmethod
1393 (class name quals specls ll initargs pv-table-symbol source-location)
1394 (setq initargs (copy-tree initargs))
1395 (let ((method-spec (or (getf initargs :method-spec)
1396 (make-method-spec name quals specls))))
1397 (setf (getf initargs :method-spec) method-spec)
1398 (load-defmethod-internal class name quals specls
1399 ll initargs pv-table-symbol
1402 (defun load-defmethod-internal
1403 (method-class gf-spec qualifiers specializers lambda-list
1404 initargs pv-table-symbol source-location)
1405 (when pv-table-symbol
1406 (setf (getf (getf initargs :plist) :pv-table-symbol)
1408 (when (and (eq *boot-state* 'complete)
1410 (let* ((gf (fdefinition gf-spec))
1411 (method (and (generic-function-p gf)
1412 (generic-function-methods gf)
1415 (parse-specializers specializers)
1418 (style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1419 gf-spec qualifiers specializers))))
1420 (let ((method (apply #'add-named-method
1421 gf-spec qualifiers specializers lambda-list
1422 :definition-source source-location
1424 (unless (or (eq method-class 'standard-method)
1425 (eq (find-class method-class nil) (class-of method)))
1426 ;; FIXME: should be STYLE-WARNING?
1427 (format *error-output*
1428 "~&At the time the method with qualifiers ~:S and~%~
1429 specializers ~:S on the generic function ~S~%~
1430 was compiled, the method-class for that generic function was~%~
1431 ~S. But, the method class is now ~S, this~%~
1432 may mean that this method was compiled improperly.~%"
1433 qualifiers specializers gf-spec
1434 method-class (class-name (class-of method))))
1437 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1438 `(slow-method ,gf-spec ,@qualifiers ,unparsed-specializers))
1440 (defun initialize-method-function (initargs &optional return-function-p method)
1441 (let* ((mf (getf initargs :function))
1442 (method-spec (getf initargs :method-spec))
1443 (plist (getf initargs :plist))
1444 (pv-table-symbol (getf plist :pv-table-symbol))
1446 (mff (getf initargs :fast-function)))
1447 (flet ((set-mf-property (p v)
1449 (setf (method-function-get mf p) v))
1451 (setf (method-function-get mff p) v))))
1454 (setq mf (set-fun-name mf method-spec)))
1456 (let ((name `(fast-method ,@(cdr method-spec))))
1457 (set-fun-name mff name)
1459 (set-mf-property :name name)))))
1461 (let ((snl (getf plist :slot-name-lists))
1462 (cl (getf plist :call-list)))
1464 (setq pv-table (intern-pv-table :slot-name-lists snl
1466 (when pv-table (set pv-table-symbol pv-table))
1467 (set-mf-property :pv-table pv-table)))
1468 (loop (when (null plist) (return nil))
1469 (set-mf-property (pop plist) (pop plist)))
1471 (set-mf-property :method method))
1472 (when return-function-p
1473 (or mf (method-function-from-fast-function mff)))))))
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-fast-function-index*
1752 (!bootstrap-slot-index 'standard-method 'fast-function))
1753 (defvar *sm-%function-index*
1754 (!bootstrap-slot-index 'standard-method '%function))
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 fast-function %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 ((standard-method-classes
1780 (list *the-class-standard-method*
1781 *the-class-standard-reader-method*
1782 *the-class-standard-writer-method*
1783 *the-class-standard-boundp-method*))
1784 (class (class-of method)))
1785 (if (member class standard-method-classes)
1786 (clos-slots-ref (get-slots method) *sm-fast-function-index*)
1787 (method-fast-function method))))
1788 (defun safe-method-function (method)
1789 (let ((standard-method-classes
1790 (list *the-class-standard-method*
1791 *the-class-standard-reader-method*
1792 *the-class-standard-writer-method*
1793 *the-class-standard-boundp-method*))
1794 (class (class-of method)))
1795 (if (member class standard-method-classes)
1796 (clos-slots-ref (get-slots method) *sm-%function-index*)
1797 (method-function method))))
1798 (defun safe-method-qualifiers (method)
1799 (let ((standard-method-classes
1800 (list *the-class-standard-method*
1801 *the-class-standard-reader-method*
1802 *the-class-standard-writer-method*
1803 *the-class-standard-boundp-method*))
1804 (class (class-of method)))
1805 (if (member class standard-method-classes)
1806 (let ((plist (clos-slots-ref (get-slots method) *sm-plist-index*)))
1807 (getf plist 'qualifiers))
1808 (method-qualifiers method))))
1810 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1811 (let* ((existing-p (and methods (cdr methods) new-method))
1812 (nreq (length (arg-info-metatypes arg-info)))
1813 (metatypes (if existing-p
1814 (arg-info-metatypes arg-info)
1816 (type (if existing-p
1817 (gf-info-simple-accessor-type arg-info)
1819 (when (arg-info-valid-p arg-info)
1820 (dolist (method (if new-method (list new-method) methods))
1821 (let* ((specializers (if (or (eq *boot-state* 'complete)
1822 (not (consp method)))
1823 (safe-method-specializers method)
1824 (early-method-specializers method t)))
1825 (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1827 (early-method-class method)))
1828 (new-type (when (and class
1829 (or (not (eq *boot-state* 'complete))
1830 (eq (generic-function-method-combination gf)
1831 *standard-method-combination*)))
1832 (cond ((eq class *the-class-standard-reader-method*)
1834 ((eq class *the-class-standard-writer-method*)
1836 ((eq class *the-class-standard-boundp-method*)
1838 (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1839 (setq type (cond ((null type) new-type)
1840 ((eq type new-type) type)
1842 (setf (arg-info-metatypes arg-info) metatypes)
1843 (setf (gf-info-simple-accessor-type arg-info) type)))
1844 (when (or (not was-valid-p) first-p)
1845 (multiple-value-bind (c-a-m-emf std-p)
1848 (compute-applicable-methods-emf gf))
1849 (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1850 (setf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1851 (unless (gf-info-c-a-m-emf-std-p arg-info)
1852 (setf (gf-info-simple-accessor-type arg-info) t))))
1854 (let ((name (if (eq *boot-state* 'complete)
1855 (generic-function-name gf)
1856 (!early-gf-name gf))))
1857 (setf (gf-precompute-dfun-and-emf-p arg-info)
1861 *internal-pcl-generalized-fun-name-symbols*))
1863 (t (let* ((symbol (fun-name-block-name name))
1864 (package (symbol-package symbol)))
1865 (and (or (eq package *pcl-package*)
1866 (memq package (package-use-list *pcl-package*)))
1867 ;; FIXME: this test will eventually be
1868 ;; superseded by the *internal-pcl...* test,
1869 ;; above. While we are in a process of
1870 ;; transition, however, it should probably
1872 (not (find #\Space (symbol-name symbol))))))))))
1873 (setf (gf-info-fast-mf-p arg-info)
1874 (or (not (eq *boot-state* 'complete))
1875 (let* ((method-class (generic-function-method-class gf))
1876 (methods (compute-applicable-methods
1877 #'make-method-lambda
1878 (list gf (class-prototype method-class)
1880 (and methods (null (cdr methods))
1881 (let ((specls (method-specializers (car methods))))
1882 (and (classp (car specls))
1883 (eq 'standard-generic-function
1884 (class-name (car specls)))
1885 (classp (cadr specls))
1886 (eq 'standard-method
1887 (class-name (cadr specls)))))))))
1890 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
1892 ;;; The STATIC-SLOTS field of the funcallable instances used as early
1893 ;;; generic functions is used to store the early methods and early
1894 ;;; discriminator code for the early generic function. The static
1895 ;;; slots field of the fins contains a list whose:
1896 ;;; CAR - a list of the early methods on this early gf
1897 ;;; CADR - the early discriminator code for this method
1898 (defun ensure-generic-function-using-class (existing spec &rest keys
1899 &key (lambda-list nil
1901 argument-precedence-order
1904 (declare (ignore keys))
1905 (cond ((and existing (early-gf-p existing))
1907 (set-arg-info existing :lambda-list lambda-list))
1909 ((assoc spec *!generic-function-fixups* :test #'equal)
1911 (make-early-gf spec lambda-list lambda-list-p existing
1912 argument-precedence-order source-location)
1913 (error "The function ~S is not already defined." spec)))
1915 (error "~S should be on the list ~S."
1917 '*!generic-function-fixups*))
1919 (pushnew spec *!early-generic-functions* :test #'equal)
1920 (make-early-gf spec lambda-list lambda-list-p nil
1921 argument-precedence-order source-location))))
1923 (defun make-early-gf (spec &optional lambda-list lambda-list-p
1924 function argument-precedence-order source-location)
1925 (let ((fin (allocate-funcallable-instance *sgf-wrapper* *sgf-slots-init*)))
1926 (set-funcallable-instance-function
1929 (if (eq spec 'print-object)
1930 #'(lambda (instance stream)
1931 (print-unreadable-object (instance stream :identity t)
1932 (format stream "std-instance")))
1933 #'(lambda (&rest args)
1934 (declare (ignore args))
1935 (error "The function of the funcallable-instance ~S~
1936 has not been set." fin)))))
1937 (setf (gdefinition spec) fin)
1938 (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
1939 (!bootstrap-set-slot 'standard-generic-function
1943 (set-fun-name fin spec)
1944 (let ((arg-info (make-arg-info)))
1945 (setf (early-gf-arg-info fin) arg-info)
1947 (proclaim (defgeneric-declaration spec lambda-list))
1948 (if argument-precedence-order
1950 :lambda-list lambda-list
1951 :argument-precedence-order argument-precedence-order)
1952 (set-arg-info fin :lambda-list lambda-list))))
1955 (defun safe-gf-dfun-state (generic-function)
1956 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
1957 (clos-slots-ref (get-slots generic-function) *sgf-dfun-state-index*)
1958 (gf-dfun-state generic-function)))
1959 (defun (setf safe-gf-dfun-state) (new-value generic-function)
1960 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
1961 (setf (clos-slots-ref (get-slots generic-function)
1962 *sgf-dfun-state-index*)
1964 (setf (gf-dfun-state generic-function) new-value)))
1966 (defun set-dfun (gf &optional dfun cache info)
1968 (setf (cache-owner cache) gf))
1969 (let ((new-state (if (and dfun (or cache info))
1970 (list* dfun cache info)
1972 (if (eq *boot-state* 'complete)
1973 (setf (safe-gf-dfun-state gf) new-state)
1974 (setf (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*)
1978 (defun gf-dfun-cache (gf)
1979 (let ((state (if (eq *boot-state* 'complete)
1980 (safe-gf-dfun-state gf)
1981 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1984 (cons (cadr state)))))
1986 (defun gf-dfun-info (gf)
1987 (let ((state (if (eq *boot-state* 'complete)
1988 (safe-gf-dfun-state gf)
1989 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1992 (cons (cddr state)))))
1994 (defvar *sgf-name-index*
1995 (!bootstrap-slot-index 'standard-generic-function 'name))
1997 (defun !early-gf-name (gf)
1998 (clos-slots-ref (get-slots gf) *sgf-name-index*))
2000 (defun gf-lambda-list (gf)
2001 (let ((arg-info (if (eq *boot-state* 'complete)
2003 (early-gf-arg-info gf))))
2004 (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
2005 (let ((methods (if (eq *boot-state* 'complete)
2006 (generic-function-methods gf)
2007 (early-gf-methods gf))))
2010 (warn "no way to determine the lambda list for ~S" gf)
2012 (let* ((method (car (last methods)))
2013 (ll (if (consp method)
2014 (early-method-lambda-list method)
2015 (method-lambda-list method))))
2016 (create-gf-lambda-list ll))))
2017 (arg-info-lambda-list arg-info))))
2019 (defmacro real-ensure-gf-internal (gf-class all-keys env)
2021 (cond ((symbolp ,gf-class)
2022 (setq ,gf-class (find-class ,gf-class t ,env)))
2023 ((classp ,gf-class))
2025 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
2026 class nor a symbol that names a class."
2028 (remf ,all-keys :generic-function-class)
2029 (remf ,all-keys :environment)
2030 (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
2031 (unless (eq combin '.shes-not-there.)
2032 (setf (getf ,all-keys :method-combination)
2033 (find-method-combination (class-prototype ,gf-class)
2036 (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
2037 (unless (eq method-class '.shes-not-there.)
2038 (setf (getf ,all-keys :method-class)
2039 (cond ((classp method-class)
2041 (t (find-class method-class t ,env))))))))
2043 (defun real-ensure-gf-using-class--generic-function
2047 &key environment (lambda-list nil lambda-list-p)
2048 (generic-function-class 'standard-generic-function gf-class-p)
2050 (real-ensure-gf-internal generic-function-class all-keys environment)
2051 (unless (or (null gf-class-p)
2052 (eq (class-of existing) generic-function-class))
2053 (change-class existing generic-function-class))
2055 (apply #'reinitialize-instance existing all-keys)
2057 (proclaim (defgeneric-declaration fun-name lambda-list)))))
2059 (defun real-ensure-gf-using-class--null
2063 &key environment (lambda-list nil lambda-list-p)
2064 (generic-function-class 'standard-generic-function)
2066 (declare (ignore existing))
2067 (real-ensure-gf-internal generic-function-class all-keys environment)
2069 (setf (gdefinition fun-name)
2070 (apply #'make-instance generic-function-class
2071 :name fun-name all-keys))
2073 (proclaim (defgeneric-declaration fun-name lambda-list)))))
2075 (defun safe-gf-arg-info (generic-function)
2076 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
2077 (clos-slots-ref (fsc-instance-slots generic-function)
2078 *sgf-arg-info-index*)
2079 (gf-arg-info generic-function)))
2081 ;;; FIXME: this function took on a slightly greater role than it
2082 ;;; previously had around 2005-11-02, when CSR fixed the bug whereby
2083 ;;; having more than one subclass of standard-generic-function caused
2084 ;;; the whole system to die horribly through a metacircle in
2085 ;;; GF-ARG-INFO. The fix is to be slightly more disciplined about
2086 ;;; calling accessor methods -- we call GET-GENERIC-FUN-INFO when
2087 ;;; computing discriminating functions, so we need to be careful about
2088 ;;; having a base case for the recursion, and we provide that with the
2089 ;;; STANDARD-GENERIC-FUNCTION case below. However, we are not (yet)
2090 ;;; as disciplined as CLISP's CLOS/MOP, and it would be nice to get to
2091 ;;; that stage, where all potentially dangerous cases are enumerated
2092 ;;; and stopped. -- CSR, 2005-11-02.
2093 (defun get-generic-fun-info (gf)
2094 ;; values nreq applyp metatypes nkeys arg-info
2095 (multiple-value-bind (applyp metatypes arg-info)
2096 (let* ((arg-info (if (early-gf-p gf)
2097 (early-gf-arg-info gf)
2098 (safe-gf-arg-info gf)))
2099 (metatypes (arg-info-metatypes arg-info)))
2100 (values (arg-info-applyp arg-info)
2103 (values (length metatypes) applyp metatypes
2104 (count-if (lambda (x) (neq x t)) metatypes)
2107 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
2108 &optional slot-name)
2109 (initialize-method-function initargs)
2112 ;; Figure out whether we got class objects or class names as the
2113 ;; specializers and set parsed and unparsed appropriately. If we
2114 ;; got class objects, then we can compute unparsed, but if we got
2115 ;; class names we don't try to compute parsed.
2117 ;; Note that the use of not symbolp in this call to every should be
2118 ;; read as 'classp' we can't use classp itself because it doesn't
2120 (if (every (lambda (s) (not (symbolp s))) specializers)
2121 (setq parsed specializers
2122 unparsed (mapcar (lambda (s)
2123 (if (eq s t) t (class-name s)))
2125 (setq unparsed specializers
2127 (list :early-method ;This is an early method dammit!
2129 (getf initargs :function)
2130 (getf initargs :fast-function)
2132 parsed ;The parsed specializers. This is used
2133 ;by early-method-specializers to cache
2134 ;the parse. Note that this only comes
2135 ;into play when there is more than one
2136 ;early method on an early gf.
2138 (list class ;A list to which real-make-a-method
2139 qualifiers ;can be applied to make a real method
2140 arglist ;corresponding to this early one.
2146 (defun real-make-a-method
2147 (class qualifiers lambda-list specializers initargs doc
2148 &optional slot-name)
2149 (setq specializers (parse-specializers specializers))
2150 (apply #'make-instance class
2151 :qualifiers qualifiers
2152 :lambda-list lambda-list
2153 :specializers specializers
2155 :slot-name slot-name
2159 (defun early-method-function (early-method)
2160 (values (cadr early-method) (caddr early-method)))
2162 (defun early-method-class (early-method)
2163 (find-class (car (fifth early-method))))
2165 (defun early-method-standard-accessor-p (early-method)
2166 (let ((class (first (fifth early-method))))
2167 (or (eq class 'standard-reader-method)
2168 (eq class 'standard-writer-method)
2169 (eq class 'standard-boundp-method))))
2171 (defun early-method-standard-accessor-slot-name (early-method)
2172 (seventh (fifth early-method)))
2174 ;;; Fetch the specializers of an early method. This is basically just
2175 ;;; a simple accessor except that when the second argument is t, this
2176 ;;; converts the specializers from symbols into class objects. The
2177 ;;; class objects are cached in the early method, this makes
2178 ;;; bootstrapping faster because the class objects only have to be
2182 ;;; The second argument should only be passed as T by
2183 ;;; early-lookup-method. This is to implement the rule that only when
2184 ;;; there is more than one early method on a generic function is the
2185 ;;; conversion from class names to class objects done. This
2186 ;;; corresponds to the fact that we are only allowed to have one
2187 ;;; method on any generic function up until the time classes exist.
2188 (defun early-method-specializers (early-method &optional objectsp)
2189 (if (and (listp early-method)
2190 (eq (car early-method) :early-method))
2191 (cond ((eq objectsp t)
2192 (or (fourth early-method)
2193 (setf (fourth early-method)
2194 (mapcar #'find-class (cadddr (fifth early-method))))))
2196 (cadddr (fifth early-method))))
2197 (error "~S is not an early-method." early-method)))
2199 (defun early-method-qualifiers (early-method)
2200 (cadr (fifth early-method)))
2202 (defun early-method-lambda-list (early-method)
2203 (caddr (fifth early-method)))
2205 (defun early-add-named-method (generic-function-name
2210 (let* ((gf (ensure-generic-function generic-function-name))
2212 (dolist (m (early-gf-methods gf))
2213 (when (and (equal (early-method-specializers m) specializers)
2214 (equal (early-method-qualifiers m) qualifiers))
2216 (new (make-a-method 'standard-method
2222 (when existing (remove-method gf existing))
2223 (add-method gf new)))
2225 ;;; This is the early version of ADD-METHOD. Later this will become a
2226 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2227 ;;; special knowledge about ADD-METHOD.
2228 (defun add-method (generic-function method)
2229 (when (not (fsc-instance-p generic-function))
2230 (error "Early ADD-METHOD didn't get a funcallable instance."))
2231 (when (not (and (listp method) (eq (car method) :early-method)))
2232 (error "Early ADD-METHOD didn't get an early method."))
2233 (push method (early-gf-methods generic-function))
2234 (set-arg-info generic-function :new-method method)
2235 (unless (assoc (!early-gf-name generic-function)
2236 *!generic-function-fixups*
2238 (update-dfun generic-function)))
2240 ;;; This is the early version of REMOVE-METHOD. See comments on
2241 ;;; the early version of ADD-METHOD.
2242 (defun remove-method (generic-function method)
2243 (when (not (fsc-instance-p generic-function))
2244 (error "An early remove-method didn't get a funcallable instance."))
2245 (when (not (and (listp method) (eq (car method) :early-method)))
2246 (error "An early remove-method didn't get an early method."))
2247 (setf (early-gf-methods generic-function)
2248 (remove method (early-gf-methods generic-function)))
2249 (set-arg-info generic-function)
2250 (unless (assoc (!early-gf-name generic-function)
2251 *!generic-function-fixups*
2253 (update-dfun generic-function)))
2255 ;;; This is the early version of GET-METHOD. See comments on the early
2256 ;;; version of ADD-METHOD.
2257 (defun get-method (generic-function qualifiers specializers
2258 &optional (errorp t))
2259 (if (early-gf-p generic-function)
2260 (or (dolist (m (early-gf-methods generic-function))
2261 (when (and (or (equal (early-method-specializers m nil)
2263 (equal (early-method-specializers m t)
2265 (equal (early-method-qualifiers m) qualifiers))
2268 (error "can't get early method")
2270 (real-get-method generic-function qualifiers specializers errorp)))
2272 (defun !fix-early-generic-functions ()
2273 (let ((accessors nil))
2274 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2275 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2276 (dolist (early-gf-spec *!early-generic-functions*)
2277 (when (every #'early-method-standard-accessor-p
2278 (early-gf-methods (gdefinition early-gf-spec)))
2279 (push early-gf-spec accessors)))
2280 (dolist (spec (nconc accessors
2281 '(accessor-method-slot-name
2282 generic-function-methods
2287 slot-definition-location
2288 slot-definition-name
2291 class-precedence-list
2292 slot-boundp-using-class
2293 (setf slot-value-using-class)
2294 slot-value-using-class
2297 funcallable-standard-class-p
2300 (setq *!early-generic-functions*
2302 (delete spec *!early-generic-functions* :test #'equal))))
2304 (dolist (early-gf-spec *!early-generic-functions*)
2305 (/show early-gf-spec)
2306 (let* ((gf (gdefinition early-gf-spec))
2307 (methods (mapcar (lambda (early-method)
2308 (let ((args (copy-list (fifth
2311 (early-method-specializers
2313 (apply #'real-make-a-method args)))
2314 (early-gf-methods gf))))
2315 (setf (generic-function-method-class gf) *the-class-standard-method*)
2316 (setf (generic-function-method-combination gf)
2317 *standard-method-combination*)
2318 (set-methods gf methods)))
2320 (dolist (fn *!early-functions*)
2322 (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2324 (dolist (fixup *!generic-function-fixups*)
2326 (let* ((fspec (car fixup))
2327 (gf (gdefinition fspec))
2328 (methods (mapcar (lambda (method)
2329 (let* ((lambda-list (first method))
2330 (specializers (second method))
2331 (method-fn-name (third method))
2332 (fn-name (or method-fn-name fspec))
2333 (fn (fdefinition fn-name))
2337 (lambda (args next-methods)
2341 `(call ,fn-name)))))
2342 (declare (type function fn))
2343 (make-a-method 'standard-method
2350 (setf (generic-function-method-class gf) *the-class-standard-method*)
2351 (setf (generic-function-method-combination gf)
2352 *standard-method-combination*)
2353 (set-methods gf methods))))
2354 (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2356 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2357 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2358 ;;; is really implemented.
2359 (defun parse-defmethod (cdr-of-form)
2360 (declare (list cdr-of-form))
2361 (let ((name (pop cdr-of-form))
2364 (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2365 (push (pop cdr-of-form) qualifiers)
2366 (return (setq qualifiers (nreverse qualifiers)))))
2367 (setq spec-ll (pop cdr-of-form))
2368 (values name qualifiers spec-ll cdr-of-form)))
2370 (defun parse-specializers (specializers)
2371 (declare (list specializers))
2372 (flet ((parse (spec)
2373 (let ((result (specializer-from-type spec)))
2374 (if (specializerp result)
2377 (error "~S was used as a specializer,~%~
2378 but is not the name of a class."
2380 (error "~S is not a legal specializer." spec))))))
2381 (mapcar #'parse specializers)))
2383 (defun unparse-specializers (specializers-or-method)
2384 (if (listp specializers-or-method)
2385 (flet ((unparse (spec)
2386 (if (specializerp spec)
2387 (let ((type (specializer-type spec)))
2388 (if (and (consp type)
2389 (eq (car type) 'class))
2390 (let* ((class (cadr type))
2391 (class-name (class-name class)))
2392 (if (eq class (find-class class-name nil))
2396 (error "~S is not a legal specializer." spec))))
2397 (mapcar #'unparse specializers-or-method))
2398 (unparse-specializers (method-specializers specializers-or-method))))
2400 (defun parse-method-or-spec (spec &optional (errorp t))
2401 (let (gf method name temp)
2404 gf (method-generic-function method)
2405 temp (and gf (generic-function-name gf))
2407 (make-method-spec temp
2408 (method-qualifiers method)
2409 (unparse-specializers
2410 (method-specializers method)))
2411 (make-symbol (format nil "~S" method))))
2412 (multiple-value-bind (gf-spec quals specls)
2413 (parse-defmethod spec)
2414 (and (setq gf (and (or errorp (fboundp gf-spec))
2415 (gdefinition gf-spec)))
2416 (let ((nreq (compute-discriminating-function-arglist-info gf)))
2417 (setq specls (append (parse-specializers specls)
2418 (make-list (- nreq (length specls))
2422 (setq method (get-method gf quals specls errorp))
2425 gf-spec quals (unparse-specializers specls))))))))
2426 (values gf method name)))
2428 (defun extract-parameters (specialized-lambda-list)
2429 (multiple-value-bind (parameters ignore1 ignore2)
2430 (parse-specialized-lambda-list specialized-lambda-list)
2431 (declare (ignore ignore1 ignore2))
2434 (defun extract-lambda-list (specialized-lambda-list)
2435 (multiple-value-bind (ignore1 lambda-list ignore2)
2436 (parse-specialized-lambda-list specialized-lambda-list)
2437 (declare (ignore ignore1 ignore2))
2440 (defun extract-specializer-names (specialized-lambda-list)
2441 (multiple-value-bind (ignore1 ignore2 specializers)
2442 (parse-specialized-lambda-list specialized-lambda-list)
2443 (declare (ignore ignore1 ignore2))
2446 (defun extract-required-parameters (specialized-lambda-list)
2447 (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2448 (parse-specialized-lambda-list specialized-lambda-list)
2449 (declare (ignore ignore1 ignore2 ignore3))
2450 required-parameters))
2452 (define-condition specialized-lambda-list-error
2453 (reference-condition simple-program-error)
2455 (:default-initargs :references (list '(:ansi-cl :section (3 4 3)))))
2457 (defun parse-specialized-lambda-list
2459 &optional supplied-keywords (allowed-keywords '(&optional &rest &key &aux))
2460 &aux (specialized-lambda-list-keywords
2461 '(&optional &rest &key &allow-other-keys &aux)))
2462 (let ((arg (car arglist)))
2463 (cond ((null arglist) (values nil nil nil nil))
2465 (values nil arglist nil nil))
2466 ((memq arg lambda-list-keywords)
2467 ;; non-standard lambda-list-keywords are errors.
2468 (unless (memq arg specialized-lambda-list-keywords)
2469 (error 'specialized-lambda-list-error
2470 :format-control "unknown specialized-lambda-list ~
2472 :format-arguments (list arg)))
2473 ;; no multiple &rest x &rest bla specifying
2474 (when (memq arg supplied-keywords)
2475 (error 'specialized-lambda-list-error
2476 :format-control "multiple occurrence of ~
2477 specialized-lambda-list keyword ~S~%"
2478 :format-arguments (list arg)))
2479 ;; And no placing &key in front of &optional, either.
2480 (unless (memq arg allowed-keywords)
2481 (error 'specialized-lambda-list-error
2482 :format-control "misplaced specialized-lambda-list ~
2484 :format-arguments (list arg)))
2485 ;; When we are at a lambda-list keyword, the parameters
2486 ;; don't include the lambda-list keyword; the lambda-list
2487 ;; does include the lambda-list keyword; and no
2488 ;; specializers are allowed to follow the lambda-list
2489 ;; keywords (at least for now).
2490 (multiple-value-bind (parameters lambda-list)
2491 (parse-specialized-lambda-list (cdr arglist)
2492 (cons arg supplied-keywords)
2494 (cons '&allow-other-keys
2495 (cdr (member arg allowed-keywords)))
2496 (cdr (member arg allowed-keywords))))
2497 (when (and (eq arg '&rest)
2498 (or (null lambda-list)
2499 (memq (car lambda-list)
2500 specialized-lambda-list-keywords)
2501 (not (or (null (cadr lambda-list))
2502 (memq (cadr lambda-list)
2503 specialized-lambda-list-keywords)))))
2504 (error 'specialized-lambda-list-error
2506 "in a specialized-lambda-list, excactly one ~
2507 variable must follow &REST.~%"
2508 :format-arguments nil))
2510 (cons arg lambda-list)
2514 ;; After a lambda-list keyword there can be no specializers.
2515 (multiple-value-bind (parameters lambda-list)
2516 (parse-specialized-lambda-list (cdr arglist)
2519 (values (cons (if (listp arg) (car arg) arg) parameters)
2520 (cons arg lambda-list)
2524 (multiple-value-bind (parameters lambda-list specializers required)
2525 (parse-specialized-lambda-list (cdr arglist))
2526 (values (cons (if (listp arg) (car arg) arg) parameters)
2527 (cons (if (listp arg) (car arg) arg) lambda-list)
2528 (cons (if (listp arg) (cadr arg) t) specializers)
2529 (cons (if (listp arg) (car arg) arg) required)))))))
2531 (setq *boot-state* 'early)
2533 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2534 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2535 ;;; walker stuff was only used for implementing stuff like that; maybe
2536 ;;; it's not needed any more? Hunt down what it was used for and see.
2538 (defmacro with-slots (slots instance &body body)
2539 (let ((in (gensym)))
2540 `(let ((,in ,instance))
2541 (declare (ignorable ,in))
2542 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2545 (and (symbolp instance)
2546 `((declare (%variable-rebinding ,in ,instance)))))
2548 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2550 (if (symbolp slot-entry)
2554 (if (symbolp slot-entry)
2556 (cadr slot-entry))))
2558 (slot-value ,in ',slot-name))))
2562 (defmacro with-accessors (slots instance &body body)
2563 (let ((in (gensym)))
2564 `(let ((,in ,instance))
2565 (declare (ignorable ,in))
2566 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2569 (and (symbolp instance)
2570 `((declare (%variable-rebinding ,in ,instance)))))
2572 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2573 (let ((var-name (car slot-entry))
2574 (accessor-name (cadr slot-entry)))
2575 `(,var-name (,accessor-name ,in))))