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 ;;; Methods themselves are simple inanimate objects. Most properties of
29 ;;; methods are immutable, methods cannot be reinitialized. The following
30 ;;; properties of methods can be changed:
31 ;;; METHOD-GENERIC-FUNCTION
35 ;;; Error checking is done in before methods. Because of the simplicity of
36 ;;; standard method objects the standard primary method can fill the slots.
38 ;;; Methods are not reinitializable.
40 (define-condition metaobject-initialization-violation
41 (reference-condition simple-error)
44 (macrolet ((def (name args control)
45 `(defmethod ,name ,args
46 (declare (ignore initargs))
47 (error 'metaobject-initialization-violation
48 :format-control ,(format nil "~@<~A~@:>" control)
49 :format-arguments (list ',name)
50 :references (list '(:amop :initialization method))))))
51 (def reinitialize-instance ((method method) &rest initargs)
52 "Method objects cannot be redefined by ~S.")
53 (def change-class ((method method) new &rest initargs)
54 "Method objects cannot be redefined by ~S.")
55 ;; NEW being a subclass of method is dealt with in the general
56 ;; method of CHANGE-CLASS
57 (def update-instance-for-redefined-class ((method method) added discarded
59 "No behaviour specified for ~S on method objects.")
60 (def update-instance-for-different-class (old (new method) &rest initargs)
61 "No behaviour specified for ~S on method objects.")
62 (def update-instance-for-different-class ((old method) new &rest initargs)
63 "No behaviour specified for ~S on method objects."))
65 (define-condition invalid-method-initarg (simple-program-error)
66 ((method :initarg :method :reader invalid-method-initarg-method))
69 (format s "~@<In initialization of ~S:~2I~_~?~@:>"
70 (invalid-method-initarg-method c)
71 (simple-condition-format-control c)
72 (simple-condition-format-arguments c)))))
74 (defun invalid-method-initarg (method format-control &rest args)
75 (error 'invalid-method-initarg :method method
76 :format-control format-control :format-arguments args))
78 (defun check-documentation (method doc)
79 (unless (or (null doc) (stringp doc))
80 (invalid-method-initarg method "~@<~S of ~S is neither ~S nor a ~S.~@:>"
81 :documentation doc 'null 'string)))
82 (defun check-lambda-list (method ll)
85 (defun check-method-function (method fun)
86 (unless (functionp fun)
87 (invalid-method-initarg method "~@<~S of ~S is not a ~S.~@:>"
88 :function fun 'function)))
90 (defun check-qualifiers (method qualifiers)
91 (flet ((improper-list ()
92 (invalid-method-initarg method
93 "~@<~S of ~S is an improper list.~@:>"
94 :qualifiers qualifiers)))
95 (dolist-carefully (q qualifiers improper-list)
96 (unless (and q (atom q))
97 (invalid-method-initarg method
98 "~@<~S, in ~S ~S, is not a non-~S atom.~@:>"
99 q :qualifiers qualifiers 'null)))))
101 (defun check-slot-name (method name)
102 (unless (symbolp name)
103 (invalid-method-initarg "~@<~S of ~S is not a ~S.~@:>"
104 :slot-name name 'symbol)))
106 (defun check-specializers (method specializers)
107 (flet ((improper-list ()
108 (invalid-method-initarg method
109 "~@<~S of ~S is an improper list.~@:>"
110 :specializers specializers)))
111 (dolist-carefully (s specializers improper-list)
112 (unless (specializerp s)
113 (invalid-method-initarg method
114 "~@<~S, in ~S ~S, is not a ~S.~@:>"
115 s :specializers specializers 'specializer)))
116 ;; KLUDGE: ANSI says that it's not valid to have methods
117 ;; specializing on classes which are "not defined", leaving
118 ;; unclear what the definedness of a class is; AMOP suggests that
119 ;; forward-referenced-classes, since they have proper names and
120 ;; all, are at least worthy of some level of definition. We allow
121 ;; methods specialized on forward-referenced-classes, but it's
122 ;; non-portable and potentially dubious, so
123 (let ((frcs (remove-if-not #'forward-referenced-class-p specializers)))
125 (style-warn "~@<Defining a method using ~
126 ~V[~;~1{~S~}~;~1{~S and ~S~}~:;~{~#[~;and ~]~S~^, ~}~] ~
127 as ~2:*~V[~;a specializer~:;specializers~].~@:>"
128 (length frcs) frcs)))))
130 (defmethod shared-initialize :before
131 ((method standard-method) slot-names &key
132 qualifiers lambda-list specializers function documentation)
133 (declare (ignore slot-names))
134 ;; FIXME: it's not clear to me (CSR, 2006-08-09) why methods get
135 ;; this extra paranoia and nothing else does; either everything
136 ;; should be aggressively checking initargs, or nothing much should.
137 ;; In either case, it would probably be better to have :type
138 ;; declarations in slots, which would then give a suitable type
139 ;; error (if we implement type-checking for slots...) rather than
140 ;; this hand-crafted thing.
141 (check-qualifiers method qualifiers)
142 (check-lambda-list method lambda-list)
143 (check-specializers method specializers)
144 (check-method-function method function)
145 (check-documentation method documentation))
147 (defmethod shared-initialize :before
148 ((method standard-accessor-method) slot-names &key
149 slot-name slot-definition)
150 (declare (ignore slot-names))
151 (unless slot-definition
152 (check-slot-name method slot-name)))
154 (defmethod shared-initialize :after ((method standard-method) slot-names
155 &rest initargs &key ((method-cell method-cell)))
156 (declare (ignore slot-names method-cell))
157 (initialize-method-function initargs method))
159 (defvar *the-class-generic-function*
160 (find-class 'generic-function))
161 (defvar *the-class-standard-generic-function*
162 (find-class 'standard-generic-function))
164 (defmethod shared-initialize :before
165 ((generic-function standard-generic-function)
167 &key (name nil namep)
168 (lambda-list () lambda-list-p)
169 argument-precedence-order
172 (method-class nil method-class-supplied-p)
173 (method-combination nil method-combination-supplied-p))
174 (declare (ignore slot-names
175 declarations argument-precedence-order documentation
176 lambda-list lambda-list-p))
179 (set-fun-name generic-function name))
181 (flet ((initarg-error (initarg value string)
182 (error "when initializing the generic function ~S:~%~
183 The ~S initialization argument was: ~A.~%~
185 generic-function initarg value string)))
186 (cond (method-class-supplied-p
187 (when (symbolp method-class)
188 (setq method-class (find-class method-class)))
189 (unless (and (classp method-class)
190 (*subtypep (class-eq-specializer method-class)
192 (initarg-error :method-class
194 "a subclass of the class METHOD"))
195 (setf (slot-value generic-function 'method-class) method-class))
196 ((slot-boundp generic-function 'method-class))
198 (initarg-error :method-class
200 "a subclass of the class METHOD")))
201 (cond (method-combination-supplied-p
202 (unless (method-combination-p method-combination)
203 (initarg-error :method-combination
205 "a method combination object")))
206 ((slot-boundp generic-function '%method-combination))
208 (initarg-error :method-combination
210 "a method combination object")))))
212 (defun find-generic-function (name &optional (errorp t))
213 (let ((fun (and (fboundp name) (fdefinition name))))
215 ((and fun (typep fun 'generic-function)) fun)
216 (errorp (error "No generic function named ~S." name))
219 (defun real-add-named-method (generic-function-name qualifiers
220 specializers lambda-list &rest other-initargs)
221 (unless (and (fboundp generic-function-name)
222 (typep (fdefinition generic-function-name) 'generic-function))
223 (style-warn "implicitly creating new generic function ~S"
224 generic-function-name))
225 (let* ((existing-gf (find-generic-function generic-function-name nil))
228 (ensure-generic-function
229 generic-function-name
230 :generic-function-class (class-of existing-gf))
231 (ensure-generic-function generic-function-name)))
232 (proto (method-prototype-for-gf generic-function-name)))
233 ;; FIXME: Destructive modification of &REST list.
234 (setf (getf (getf other-initargs 'plist) :name)
235 (make-method-spec generic-function qualifiers specializers))
236 (let ((new (apply #'make-instance (class-of proto)
237 :qualifiers qualifiers :specializers specializers
238 :lambda-list lambda-list other-initargs)))
239 (add-method generic-function new)
242 (define-condition find-method-length-mismatch
243 (reference-condition simple-error)
245 (:default-initargs :references (list '(:ansi-cl :function find-method))))
247 (defun real-get-method (generic-function qualifiers specializers
249 always-check-specializers)
250 (let ((lspec (length specializers))
251 (methods (generic-function-methods generic-function)))
252 (when (or methods always-check-specializers)
253 (let ((nreq (length (arg-info-metatypes (gf-arg-info
254 generic-function)))))
255 ;; Since we internally bypass FIND-METHOD by using GET-METHOD
256 ;; instead we need to to this here or users may get hit by a
257 ;; failed AVER instead of a sensible error message.
258 (when (/= lspec nreq)
260 'find-method-length-mismatch
262 "~@<The generic function ~S takes ~D required argument~:P; ~
263 was asked to find a method with specializers ~S~@:>"
264 :format-arguments (list generic-function nreq specializers)))))
266 (dolist (method methods)
267 (let ((mspecializers (method-specializers method)))
268 (aver (= lspec (length mspecializers)))
269 (when (and (equal qualifiers (method-qualifiers method))
270 (every #'same-specializer-p specializers
271 (method-specializers method)))
276 (error "~@<There is no method on ~S with ~
277 ~:[no qualifiers~;~:*qualifiers ~S~] ~
278 and specializers ~S.~@:>"
279 generic-function qualifiers specializers))))))
281 (defmethod find-method ((generic-function standard-generic-function)
282 qualifiers specializers &optional (errorp t))
283 ;; ANSI about FIND-METHOD: "The specializers argument contains the
284 ;; parameter specializers for the method. It must correspond in
285 ;; length to the number of required arguments of the generic
286 ;; function, or an error is signaled."
288 ;; This error checking is done by REAL-GET-METHOD.
290 generic-function qualifiers
291 ;; ANSI for FIND-METHOD seems to imply that in fact specializers
292 ;; should always be passed in parsed form instead of being parsed
293 ;; at this point. Since there's no ANSI-blessed way of getting an
294 ;; EQL specializer, that seems unnecessarily painful, so we are
295 ;; nice to our users. -- CSR, 2007-06-01
296 (parse-specializers generic-function specializers) errorp t))
298 ;;; Compute various information about a generic-function's arglist by looking
299 ;;; at the argument lists of the methods. The hair for trying not to use
300 ;;; &REST arguments lives here.
301 ;;; The values returned are:
302 ;;; number-of-required-arguments
303 ;;; the number of required arguments to this generic-function's
304 ;;; discriminating function
306 ;;; whether or not this generic-function's discriminating
307 ;;; function takes an &rest argument.
308 ;;; specialized-argument-positions
309 ;;; a list of the positions of the arguments this generic-function
310 ;;; specializes (e.g. for a classical generic-function this is the
312 (defmethod compute-discriminating-function-arglist-info
313 ((generic-function standard-generic-function))
314 ;;(declare (values number-of-required-arguments &rest-argument-p
315 ;; specialized-argument-postions))
316 (let ((number-required nil)
318 (specialized-positions ())
319 (methods (generic-function-methods generic-function)))
320 (dolist (method methods)
321 (multiple-value-setq (number-required restp specialized-positions)
322 (compute-discriminating-function-arglist-info-internal
323 generic-function method number-required restp specialized-positions)))
324 (values number-required restp (sort specialized-positions #'<))))
326 (defun compute-discriminating-function-arglist-info-internal
327 (generic-function method number-of-requireds restp
328 specialized-argument-positions)
329 (declare (ignore generic-function)
330 (type (or null fixnum) number-of-requireds))
332 (declare (fixnum requireds))
333 ;; Go through this methods arguments seeing how many are required,
334 ;; and whether there is an &rest argument.
335 (dolist (arg (method-lambda-list method))
336 (cond ((eq arg '&aux) (return))
337 ((memq arg '(&optional &rest &key))
338 (return (setq restp t)))
339 ((memq arg lambda-list-keywords))
340 (t (incf requireds))))
341 ;; Now go through this method's type specifiers to see which
342 ;; argument positions are type specified. Treat T specially
343 ;; in the usual sort of way. For efficiency don't bother to
344 ;; keep specialized-argument-positions sorted, rather depend
345 ;; on our caller to do that.
347 (dolist (type-spec (method-specializers method))
348 (unless (eq type-spec *the-class-t*)
349 (pushnew pos specialized-argument-positions))
351 ;; Finally merge the values for this method into the values
352 ;; for the exisiting methods and return them. Note that if
353 ;; num-of-requireds is NIL it means this is the first method
354 ;; and we depend on that.
355 (values (min (or number-of-requireds requireds) requireds)
357 (and number-of-requireds (/= number-of-requireds requireds)))
358 specialized-argument-positions)))
360 (defun make-discriminating-function-arglist (number-required-arguments restp)
361 (nconc (let ((args nil))
362 (dotimes (i number-required-arguments)
363 (push (format-symbol *package* ;; ! is this right?
364 "Discriminating Function Arg ~D"
369 `(&rest ,(format-symbol *package*
370 "Discriminating Function &rest Arg")))))
372 (defmethod generic-function-argument-precedence-order
373 ((gf standard-generic-function))
374 (aver (eq *boot-state* 'complete))
375 (loop with arg-info = (gf-arg-info gf)
376 with lambda-list = (arg-info-lambda-list arg-info)
377 for argument-position in (arg-info-precedence arg-info)
378 collect (nth argument-position lambda-list)))
380 (defmethod generic-function-lambda-list ((gf generic-function))
383 (defmethod gf-fast-method-function-p ((gf standard-generic-function))
384 (gf-info-fast-mf-p (slot-value gf 'arg-info)))
386 (defmethod initialize-instance :after ((gf standard-generic-function)
387 &key (lambda-list nil lambda-list-p)
388 argument-precedence-order)
389 (with-slots (arg-info) gf
392 :lambda-list lambda-list
393 :argument-precedence-order argument-precedence-order)
395 (when (arg-info-valid-p arg-info)
398 (defmethod reinitialize-instance :around
399 ((gf standard-generic-function) &rest args &key
400 (lambda-list nil lambda-list-p) (argument-precedence-order nil apo-p))
401 (let ((old-mc (generic-function-method-combination gf)))
402 (prog1 (call-next-method)
403 ;; KLUDGE: EQ is too strong a test.
404 (unless (eq old-mc (generic-function-method-combination gf))
405 (flush-effective-method-cache gf))
407 ((and lambda-list-p apo-p)
409 :lambda-list lambda-list
410 :argument-precedence-order argument-precedence-order))
411 (lambda-list-p (set-arg-info gf :lambda-list lambda-list))
412 (t (set-arg-info gf)))
413 (when (arg-info-valid-p (gf-arg-info gf))
415 (map-dependents gf (lambda (dependent)
416 (apply #'update-dependent gf dependent args))))))
418 (declaim (special *lazy-dfun-compute-p*))
420 (defun set-methods (gf methods)
421 (setf (generic-function-methods gf) nil)
422 (loop (when (null methods) (return gf))
423 (real-add-method gf (pop methods) methods)))
425 (define-condition new-value-specialization (reference-condition error)
426 ((%method :initarg :method :reader new-value-specialization-method))
429 (format s "~@<Cannot add method ~S to ~S, as it specializes the ~
430 new-value argument.~@:>"
431 (new-value-specialization-method c)
432 #'(setf slot-value-using-class))))
433 (:default-initargs :references
434 (list '(:sbcl :node "Metaobject Protocol")
435 '(:amop :generic-function (setf slot-value-using-class)))))
437 (defun real-add-method (generic-function method &optional skip-dfun-update-p)
438 (when (method-generic-function method)
439 (error "~@<The method ~S is already part of the generic ~
440 function ~S; it can't be added to another generic ~
441 function until it is removed from the first one.~@:>"
442 method (method-generic-function method)))
443 (flet ((similar-lambda-lists-p (method-a method-b)
444 (multiple-value-bind (a-nreq a-nopt a-keyp a-restp)
445 (analyze-lambda-list (method-lambda-list method-a))
446 (multiple-value-bind (b-nreq b-nopt b-keyp b-restp)
447 (analyze-lambda-list (method-lambda-list method-b))
448 (and (= a-nreq b-nreq)
450 (eq (or a-keyp a-restp)
451 (or b-keyp b-restp)))))))
452 (let ((lock (gf-lock generic-function)))
453 ;; HANDLER-CASE takes care of releasing the lock and enabling
454 ;; interrupts before going forth with the error.
456 ;; System lock because interrupts need to be disabled as
457 ;; well: it would be bad to unwind and leave the gf in an
458 ;; inconsistent state.
459 (sb-thread::with-recursive-system-spinlock (lock)
460 (let* ((qualifiers (method-qualifiers method))
461 (specializers (method-specializers method))
462 (existing (get-method generic-function
467 ;; If there is already a method like this one then we must get
468 ;; rid of it before proceeding. Note that we call the generic
469 ;; function REMOVE-METHOD to remove it rather than doing it in
470 ;; some internal way.
471 (when (and existing (similar-lambda-lists-p existing method))
472 (remove-method generic-function existing))
474 ;; KLUDGE: We have a special case here, as we disallow
475 ;; specializations of the NEW-VALUE argument to (SETF
476 ;; SLOT-VALUE-USING-CLASS). GET-ACCESSOR-METHOD-FUNCTION is
477 ;; the optimizing function here: it precomputes the effective
478 ;; method, assuming that there is no dispatch to be done on
479 ;; the new-value argument.
480 (when (and (eq generic-function #'(setf slot-value-using-class))
481 (not (eq *the-class-t* (first specializers))))
482 (error 'new-value-specialization :method method))
484 (setf (method-generic-function method) generic-function)
485 (pushnew method (generic-function-methods generic-function))
486 (dolist (specializer specializers)
487 (add-direct-method specializer method))
489 ;; KLUDGE: SET-ARG-INFO contains the error-detecting logic for
490 ;; detecting attempts to add methods with incongruent lambda
491 ;; lists. However, according to Gerd Moellmann on cmucl-imp,
492 ;; it also depends on the new method already having been added
493 ;; to the generic function. Therefore, we need to remove it
495 (let ((remove-again-p t))
498 (set-arg-info generic-function :new-method method)
499 (setq remove-again-p nil))
501 (remove-method generic-function method))))
503 ;; KLUDGE II: ANSI saith that it is not an error to add a
504 ;; method with invalid qualifiers to a generic function of the
505 ;; wrong kind; it's only an error at generic function
506 ;; invocation time; I dunno what the rationale was, and it
507 ;; sucks. Nevertheless, it's probably a programmer error, so
508 ;; let's warn anyway. -- CSR, 2003-08-20
509 (let ((mc (generic-function-method-combination generic-functioN)))
511 ((eq mc *standard-method-combination*)
512 (when (and qualifiers
514 (not (memq (car qualifiers)
515 '(:around :before :after)))))
516 (warn "~@<Invalid qualifiers for standard method ~
517 combination in method ~S:~2I~_~S.~@:>"
519 ((short-method-combination-p mc)
520 (let ((mc-name (method-combination-type-name mc)))
521 (when (or (null qualifiers)
523 (and (neq (car qualifiers) :around)
524 (neq (car qualifiers) mc-name)))
525 (warn "~@<Invalid qualifiers for ~S method combination ~
526 in method ~S:~2I~_~S.~@:>"
527 mc-name method qualifiers))))))
529 (unless skip-dfun-update-p
530 (update-ctors 'add-method
531 :generic-function generic-function
533 (update-dfun generic-function))
534 (map-dependents generic-function
536 (update-dependent generic-function
537 dep 'add-method method)))))
538 (serious-condition (c)
542 (defun real-remove-method (generic-function method)
543 (when (eq generic-function (method-generic-function method))
544 (let ((lock (gf-lock generic-function)))
545 ;; System lock because interrupts need to be disabled as well:
546 ;; it would be bad to unwind and leave the gf in an inconsistent
548 (sb-thread::with-recursive-system-spinlock (lock)
549 (let* ((specializers (method-specializers method))
550 (methods (generic-function-methods generic-function))
551 (new-methods (remove method methods)))
552 (setf (method-generic-function method) nil
553 (generic-function-methods generic-function) new-methods)
554 (dolist (specializer (method-specializers method))
555 (remove-direct-method specializer method))
556 (set-arg-info generic-function)
557 (update-ctors 'remove-method
558 :generic-function generic-function
560 (update-dfun generic-function)
561 (map-dependents generic-function
563 (update-dependent generic-function
564 dep 'remove-method method)))))))
567 (defun compute-applicable-methods-function (generic-function arguments)
568 (values (compute-applicable-methods-using-types
570 (types-from-args generic-function arguments 'eql))))
572 (defmethod compute-applicable-methods
573 ((generic-function generic-function) arguments)
574 (values (compute-applicable-methods-using-types
576 (types-from-args generic-function arguments 'eql))))
578 (defmethod compute-applicable-methods-using-classes
579 ((generic-function generic-function) classes)
580 (compute-applicable-methods-using-types
582 (types-from-args generic-function classes 'class-eq)))
584 (defun proclaim-incompatible-superclasses (classes)
585 (setq classes (mapcar (lambda (class)
590 (dolist (class classes)
591 (dolist (other-class classes)
592 (unless (eq class other-class)
593 (pushnew other-class (class-incompatible-superclass-list class))))))
595 (defun superclasses-compatible-p (class1 class2)
596 (let ((cpl1 (cpl-or-nil class1))
597 (cpl2 (cpl-or-nil class2)))
599 (dolist (ic (class-incompatible-superclass-list sc1))
601 (return-from superclasses-compatible-p nil))))))
604 #'proclaim-incompatible-superclasses
605 '(;; superclass class
606 (built-in-class std-class structure-class) ; direct subclasses of pcl-class
607 (standard-class funcallable-standard-class)
608 ;; superclass metaobject
609 (class eql-specializer class-eq-specializer method method-combination
610 generic-function slot-definition)
611 ;; metaclass built-in-class
612 (number sequence character ; direct subclasses of t, but not array
613 standard-object structure-object) ; or symbol
614 (number array character symbol ; direct subclasses of t, but not
615 standard-object structure-object) ; sequence
616 (complex float rational) ; direct subclasses of number
617 (integer ratio) ; direct subclasses of rational
618 (list vector) ; direct subclasses of sequence
619 (cons null) ; direct subclasses of list
620 (string bit-vector) ; direct subclasses of vector
623 (defmethod same-specializer-p ((specl1 specializer) (specl2 specializer))
626 (defmethod same-specializer-p ((specl1 class) (specl2 class))
629 (defmethod specializer-class ((specializer class))
632 (defmethod same-specializer-p ((specl1 class-eq-specializer)
633 (specl2 class-eq-specializer))
634 (eq (specializer-class specl1) (specializer-class specl2)))
636 (defmethod same-specializer-p ((specl1 eql-specializer)
637 (specl2 eql-specializer))
638 (eq (specializer-object specl1) (specializer-object specl2)))
640 (defmethod specializer-class ((specializer eql-specializer))
641 (class-of (slot-value specializer 'object)))
643 (defun specializer-class-or-nil (specializer)
644 (and (standard-specializer-p specializer)
645 (specializer-class specializer)))
647 (defun error-need-at-least-n-args (function n)
648 (error 'simple-program-error
649 :format-control "~@<The function ~2I~_~S ~I~_requires ~
650 at least ~W argument~:P.~:>"
651 :format-arguments (list function n)))
653 (defun types-from-args (generic-function arguments &optional type-modifier)
654 (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
655 (get-generic-fun-info generic-function)
656 (declare (ignore applyp metatypes nkeys))
657 (let ((types-rev nil))
658 (dotimes-fixnum (i nreq)
661 (error-need-at-least-n-args (generic-function-name generic-function)
663 (let ((arg (pop arguments)))
664 (push (if type-modifier `(,type-modifier ,arg) arg) types-rev)))
665 (values (nreverse types-rev) arg-info))))
667 (defun get-wrappers-from-classes (nkeys wrappers classes metatypes)
668 (let* ((w wrappers) (w-tail w) (mt-tail metatypes))
669 (dolist (class (if (listp classes) classes (list classes)))
670 (unless (eq t (car mt-tail))
671 (let ((c-w (class-wrapper class)))
672 (unless c-w (return-from get-wrappers-from-classes nil))
675 (setf (car w-tail) c-w
676 w-tail (cdr w-tail)))))
677 (setq mt-tail (cdr mt-tail)))
680 (defun sdfun-for-caching (gf classes)
681 (let ((types (mapcar #'class-eq-type classes)))
682 (multiple-value-bind (methods all-applicable-and-sorted-p)
683 (compute-applicable-methods-using-types gf types)
684 (let ((generator (get-secondary-dispatch-function1
685 gf methods types nil t all-applicable-and-sorted-p)))
686 (make-callable gf methods generator
687 nil (mapcar #'class-wrapper classes))))))
689 (defun value-for-caching (gf classes)
690 (let ((methods (compute-applicable-methods-using-types
691 gf (mapcar #'class-eq-type classes))))
692 (method-plist-value (car methods) :constant-value)))
694 (defun default-secondary-dispatch-function (generic-function)
696 (let ((methods (compute-applicable-methods generic-function args)))
698 (let ((emf (get-effective-method-function generic-function
700 (invoke-emf emf args))
701 (apply #'no-applicable-method generic-function args)))))
704 (loop (when (atom x) (return (eq x y)))
705 (when (atom y) (return nil))
706 (unless (eq (car x) (car y)) (return nil))
710 (defvar *std-cam-methods* nil)
712 (defun compute-applicable-methods-emf (generic-function)
713 (if (eq *boot-state* 'complete)
714 (let* ((cam (gdefinition 'compute-applicable-methods))
715 (cam-methods (compute-applicable-methods-using-types
716 cam (list `(eql ,generic-function) t))))
717 (values (get-effective-method-function cam cam-methods)
719 (or *std-cam-methods*
720 (setq *std-cam-methods*
721 (compute-applicable-methods-using-types
722 cam (list `(eql ,cam) t)))))))
723 (values #'compute-applicable-methods-function t)))
725 (defun compute-applicable-methods-emf-std-p (gf)
726 (gf-info-c-a-m-emf-std-p (gf-arg-info gf)))
728 (defvar *old-c-a-m-gf-methods* nil)
730 (defun update-all-c-a-m-gf-info (c-a-m-gf)
731 (let ((methods (generic-function-methods c-a-m-gf)))
732 (if (and *old-c-a-m-gf-methods*
733 (every (lambda (old-method)
734 (member old-method methods))
735 *old-c-a-m-gf-methods*))
736 (let ((gfs-to-do nil)
737 (gf-classes-to-do nil))
738 (dolist (method methods)
739 (unless (member method *old-c-a-m-gf-methods*)
740 (let ((specl (car (method-specializers method))))
741 (if (eql-specializer-p specl)
742 (pushnew (specializer-object specl) gfs-to-do)
743 (pushnew (specializer-class specl) gf-classes-to-do)))))
744 (map-all-generic-functions
746 (when (or (member gf gfs-to-do)
747 (dolist (class gf-classes-to-do nil)
749 (class-precedence-list (class-of gf)))))
750 (update-c-a-m-gf-info gf)))))
751 (map-all-generic-functions #'update-c-a-m-gf-info))
752 (setq *old-c-a-m-gf-methods* methods)))
754 (defun update-gf-info (gf)
755 (update-c-a-m-gf-info gf)
756 (update-gf-simple-accessor-type gf))
758 (defun update-c-a-m-gf-info (gf)
759 (unless (early-gf-p gf)
760 (multiple-value-bind (c-a-m-emf std-p)
761 (compute-applicable-methods-emf gf)
762 (let ((arg-info (gf-arg-info gf)))
763 (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
764 (setf (gf-info-c-a-m-emf-std-p arg-info) std-p)))))
766 (defun update-gf-simple-accessor-type (gf)
767 (let ((arg-info (gf-arg-info gf)))
768 (setf (gf-info-simple-accessor-type arg-info)
769 (let* ((methods (generic-function-methods gf))
770 (class (and methods (class-of (car methods))))
773 (cond ((or (eq class *the-class-standard-reader-method*)
774 (eq class *the-class-global-reader-method*))
776 ((or (eq class *the-class-standard-writer-method*)
777 (eq class *the-class-global-writer-method*))
779 ((or (eq class *the-class-standard-boundp-method*)
780 (eq class *the-class-global-boundp-method*))
782 (when (and (gf-info-c-a-m-emf-std-p arg-info)
784 (dolist (method (cdr methods) t)
785 (unless (eq class (class-of method)) (return nil)))
786 (eq (generic-function-method-combination gf)
787 *standard-method-combination*))
791 ;;; CMUCL (Gerd's PCL, 2002-04-25) comment:
793 ;;; Return two values. First value is a function to be stored in
794 ;;; effective slot definition SLOTD for reading it with
795 ;;; SLOT-VALUE-USING-CLASS, setting it with (SETF
796 ;;; SLOT-VALUE-USING-CLASS) or testing it with
797 ;;; SLOT-BOUNDP-USING-CLASS. GF is one of these generic functions,
798 ;;; TYPE is one of the symbols READER, WRITER, BOUNDP. CLASS is
801 ;;; Second value is true if the function returned is one of the
802 ;;; optimized standard functions for the purpose, which are used
803 ;;; when only standard methods are applicable.
805 ;;; FIXME: Change all these wacky function names to something sane.
806 (defun get-accessor-method-function (gf type class slotd)
807 (let* ((std-method (standard-svuc-method type))
808 (str-method (structure-svuc-method type))
809 (types1 `((eql ,class) (class-eq ,class) (eql ,slotd)))
810 (types (if (eq type 'writer) `(t ,@types1) types1))
811 (methods (compute-applicable-methods-using-types gf types))
812 (std-p (null (cdr methods))))
815 (get-optimized-std-accessor-method-function class slotd type)
816 (let* ((optimized-std-fun
817 (get-optimized-std-slot-value-using-class-method-function
820 `((,(car (or (member std-method methods)
821 (member str-method methods)
823 'get-accessor-method-function)))
824 ,optimized-std-fun)))
826 (let ((wrappers (list (wrapper-of class)
827 (class-wrapper class)
828 (wrapper-of slotd))))
829 (if (eq type 'writer)
830 (cons (class-wrapper *the-class-t*) wrappers)
832 (sdfun (get-secondary-dispatch-function
833 gf methods types method-alist wrappers)))
834 (get-accessor-from-svuc-method-function class slotd sdfun type)))
837 ;;; used by OPTIMIZE-SLOT-VALUE-BY-CLASS-P (vector.lisp)
838 (defun update-slot-value-gf-info (gf type)
840 (update-std-or-str-methods gf type))
841 (when (and (standard-svuc-method type) (structure-svuc-method type))
842 (flet ((update-accessor-info (class)
843 (when (class-finalized-p class)
844 (dolist (slotd (class-slots class))
845 (compute-slot-accessor-info slotd type gf)))))
847 (update-accessor-info *new-class*)
848 (map-all-classes #'update-accessor-info 'slot-object)))))
850 (defvar *standard-slot-value-using-class-method* nil)
851 (defvar *standard-setf-slot-value-using-class-method* nil)
852 (defvar *standard-slot-boundp-using-class-method* nil)
853 (defvar *condition-slot-value-using-class-method* nil)
854 (defvar *condition-setf-slot-value-using-class-method* nil)
855 (defvar *condition-slot-boundp-using-class-method* nil)
856 (defvar *structure-slot-value-using-class-method* nil)
857 (defvar *structure-setf-slot-value-using-class-method* nil)
858 (defvar *structure-slot-boundp-using-class-method* nil)
860 (defun standard-svuc-method (type)
862 (reader *standard-slot-value-using-class-method*)
863 (writer *standard-setf-slot-value-using-class-method*)
864 (boundp *standard-slot-boundp-using-class-method*)))
866 (defun set-standard-svuc-method (type method)
868 (reader (setq *standard-slot-value-using-class-method* method))
869 (writer (setq *standard-setf-slot-value-using-class-method* method))
870 (boundp (setq *standard-slot-boundp-using-class-method* method))))
872 (defun condition-svuc-method (type)
874 (reader *condition-slot-value-using-class-method*)
875 (writer *condition-setf-slot-value-using-class-method*)
876 (boundp *condition-slot-boundp-using-class-method*)))
878 (defun set-condition-svuc-method (type method)
880 (reader (setq *condition-slot-value-using-class-method* method))
881 (writer (setq *condition-setf-slot-value-using-class-method* method))
882 (boundp (setq *condition-slot-boundp-using-class-method* method))))
884 (defun structure-svuc-method (type)
886 (reader *structure-slot-value-using-class-method*)
887 (writer *structure-setf-slot-value-using-class-method*)
888 (boundp *structure-slot-boundp-using-class-method*)))
890 (defun set-structure-svuc-method (type method)
892 (reader (setq *structure-slot-value-using-class-method* method))
893 (writer (setq *structure-setf-slot-value-using-class-method* method))
894 (boundp (setq *structure-slot-boundp-using-class-method* method))))
896 (defun update-std-or-str-methods (gf type)
897 (dolist (method (generic-function-methods gf))
898 (let ((specls (method-specializers method)))
899 (when (and (or (not (eq type 'writer))
900 (eq (pop specls) *the-class-t*))
901 (every #'classp specls))
902 (cond ((and (eq (class-name (car specls)) 'std-class)
903 (eq (class-name (cadr specls)) 'standard-object)
904 (eq (class-name (caddr specls))
905 'standard-effective-slot-definition))
906 (set-standard-svuc-method type method))
907 ((and (eq (class-name (car specls)) 'condition-class)
908 (eq (class-name (cadr specls)) 'condition)
909 (eq (class-name (caddr specls))
910 'condition-effective-slot-definition))
911 (set-condition-svuc-method type method))
912 ((and (eq (class-name (car specls)) 'structure-class)
913 (eq (class-name (cadr specls)) 'structure-object)
914 (eq (class-name (caddr specls))
915 'structure-effective-slot-definition))
916 (set-structure-svuc-method type method)))))))
918 (defun mec-all-classes-internal (spec precompute-p)
919 (let ((wrapper (class-wrapper (specializer-class spec))))
920 (unless (or (not wrapper) (invalid-wrapper-p wrapper))
921 (cons (specializer-class spec)
924 (not (or (eq spec *the-class-t*)
925 (eq spec *the-class-slot-object*)
926 (eq spec *the-class-standard-object*)
927 (eq spec *the-class-structure-object*)))
928 (let ((sc (class-direct-subclasses spec)))
930 (mapcan (lambda (class)
931 (mec-all-classes-internal class precompute-p))
934 (defun mec-all-classes (spec precompute-p)
935 (let ((classes (mec-all-classes-internal spec precompute-p)))
936 (if (null (cdr classes))
938 (let* ((a-classes (cons nil classes))
940 (loop (when (null (cdr tail))
941 (return (cdr a-classes)))
942 (let ((class (cadr tail))
944 (if (dolist (c ttail nil)
945 (when (eq class c) (return t)))
946 (setf (cdr tail) (cddr tail))
947 (setf tail (cdr tail)))))))))
949 (defun mec-all-class-lists (spec-list precompute-p)
952 (let* ((car-all-classes (mec-all-classes (car spec-list)
954 (all-class-lists (mec-all-class-lists (cdr spec-list)
956 (mapcan (lambda (list)
957 (mapcar (lambda (c) (cons c list)) car-all-classes))
960 (defun make-emf-cache (generic-function valuep cache classes-list new-class)
961 (let* ((arg-info (gf-arg-info generic-function))
962 (nkeys (arg-info-nkeys arg-info))
963 (metatypes (arg-info-metatypes arg-info))
964 (wrappers (unless (eq nkeys 1) (make-list nkeys)))
965 (precompute-p (gf-precompute-dfun-and-emf-p arg-info)))
966 (flet ((add-class-list (classes)
967 (when (or (null new-class) (memq new-class classes))
968 (let ((%wrappers (get-wrappers-from-classes
969 nkeys wrappers classes metatypes)))
970 (when (and %wrappers (not (probe-cache cache %wrappers)))
971 (let ((value (cond ((eq valuep t)
972 (sdfun-for-caching generic-function
974 ((eq valuep :constant-value)
975 (value-for-caching generic-function
977 ;; need to get them again, as finalization might
978 ;; have happened in between, which would
979 ;; invalidate wrappers.
980 (let ((wrappers (get-wrappers-from-classes
981 nkeys wrappers classes metatypes)))
982 (when (if (atom wrappers)
983 (not (invalid-wrapper-p wrappers))
984 (every (complement #'invalid-wrapper-p)
986 (setq cache (fill-cache cache wrappers value))))))))))
988 (mapc #'add-class-list classes-list)
989 (dolist (method (generic-function-methods generic-function))
990 (mapc #'add-class-list
991 (mec-all-class-lists (method-specializers method)
995 (defmacro class-test (arg class)
997 ((eq class *the-class-t*) t)
998 ((eq class *the-class-slot-object*)
999 `(not (typep (classoid-of ,arg) 'built-in-classoid)))
1000 ((eq class *the-class-standard-object*)
1001 `(or (std-instance-p ,arg) (fsc-instance-p ,arg)))
1002 ((eq class *the-class-funcallable-standard-object*)
1003 `(fsc-instance-p ,arg))
1005 `(typep ,arg ',(class-name class)))))
1007 (defmacro class-eq-test (arg class)
1008 `(eq (class-of ,arg) ',class))
1010 (defmacro eql-test (arg object)
1011 `(eql ,arg ',object))
1013 (defun dnet-methods-p (form)
1015 (or (eq (car form) 'methods)
1016 (eq (car form) 'unordered-methods))))
1018 ;;; This is CASE, but without gensyms.
1019 (defmacro scase (arg &rest clauses)
1020 `(let ((.case-arg. ,arg))
1021 (cond ,@(mapcar (lambda (clause)
1022 (list* (cond ((null (car clause))
1024 ((consp (car clause))
1025 (if (null (cdar clause))
1030 ((member (car clause) '(t otherwise))
1033 `(eql .case-arg. ',(car clause))))
1038 (defmacro mcase (arg &rest clauses) `(scase ,arg ,@clauses))
1040 (defun generate-discrimination-net (generic-function methods types sorted-p)
1041 (let* ((arg-info (gf-arg-info generic-function))
1042 (c-a-m-emf-std-p (gf-info-c-a-m-emf-std-p arg-info))
1043 (precedence (arg-info-precedence arg-info)))
1044 (generate-discrimination-net-internal
1045 generic-function methods types
1046 (lambda (methods known-types)
1048 (and c-a-m-emf-std-p
1050 (let ((sorted-methods nil))
1052 (copy-list methods) precedence
1054 (when sorted-methods (return-from one-order-p nil))
1055 (setq sorted-methods methods)))
1056 (setq methods sorted-methods))
1058 `(methods ,methods ,known-types)
1059 `(unordered-methods ,methods ,known-types)))
1060 (lambda (position type true-value false-value)
1061 (let ((arg (dfun-arg-symbol position)))
1062 (if (eq (car type) 'eql)
1063 (let* ((false-case-p (and (consp false-value)
1064 (or (eq (car false-value) 'scase)
1065 (eq (car false-value) 'mcase))
1066 (eq arg (cadr false-value))))
1067 (false-clauses (if false-case-p
1069 `((t ,false-value))))
1070 (case-sym (if (and (dnet-methods-p true-value)
1072 (eq (car false-value) 'mcase)
1073 (dnet-methods-p false-value)))
1076 (type-sym `(,(cadr type))))
1078 (,type-sym ,true-value)
1080 `(if ,(let ((arg (dfun-arg-symbol position)))
1082 (class `(class-test ,arg ,(cadr type)))
1083 (class-eq `(class-eq-test ,arg ,(cadr type)))))
1088 (defun class-from-type (type)
1089 (if (or (atom type) (eq (car type) t))
1092 (and (dolist (type (cdr type) *the-class-t*)
1093 (when (and (consp type) (not (eq (car type) 'not)))
1094 (return (class-from-type type)))))
1096 (eql (class-of (cadr type)))
1097 (class-eq (cadr type))
1098 (class (cadr type)))))
1100 (defun precompute-effective-methods (gf caching-p &optional classes-list-p)
1101 (let* ((arg-info (gf-arg-info gf))
1102 (methods (generic-function-methods gf))
1103 (precedence (arg-info-precedence arg-info))
1104 (*in-precompute-effective-methods-p* t)
1106 (generate-discrimination-net-internal
1108 (lambda (methods known-types)
1110 (when classes-list-p
1111 (push (mapcar #'class-from-type known-types) classes-list))
1112 (let ((no-eql-specls-p (not (methods-contain-eql-specializer-p
1117 (get-secondary-dispatch-function1
1118 gf methods known-types
1119 nil caching-p no-eql-specls-p))))))
1120 (lambda (position type true-value false-value)
1121 (declare (ignore position type true-value false-value))
1124 (if (and (consp type) (eq (car type) 'eql))
1125 `(class-eq ,(class-of (cadr type)))
1129 ;;; We know that known-type implies neither new-type nor `(not ,new-type).
1130 (defun augment-type (new-type known-type)
1131 (if (or (eq known-type t)
1132 (eq (car new-type) 'eql))
1134 (let ((so-far (if (and (consp known-type) (eq (car known-type) 'and))
1136 (list known-type))))
1137 (unless (eq (car new-type) 'not)
1139 (mapcan (lambda (type)
1140 (unless (*subtypep new-type type)
1145 `(and ,new-type ,@so-far)))))
1147 (defun generate-discrimination-net-internal
1148 (gf methods types methods-function test-fun type-function)
1149 (let* ((arg-info (gf-arg-info gf))
1150 (precedence (arg-info-precedence arg-info))
1151 (nreq (arg-info-number-required arg-info))
1152 (metatypes (arg-info-metatypes arg-info)))
1153 (labels ((do-column (p-tail contenders known-types)
1155 (let* ((position (car p-tail))
1156 (known-type (or (nth position types) t)))
1157 (if (eq (nth position metatypes) t)
1158 (do-column (cdr p-tail) contenders
1159 (cons (cons position known-type)
1161 (do-methods p-tail contenders
1162 known-type () known-types)))
1163 (funcall methods-function contenders
1164 (let ((k-t (make-list nreq)))
1165 (dolist (index+type known-types)
1166 (setf (nth (car index+type) k-t)
1169 (do-methods (p-tail contenders known-type winners known-types)
1171 ;; is a (sorted) list of methods that must be discriminated.
1173 ;; is the type of this argument, constructed from tests
1176 ;; is a (sorted) list of methods that are potentially
1177 ;; applicable after the discrimination has been made.
1178 (if (null contenders)
1179 (do-column (cdr p-tail)
1181 (cons (cons (car p-tail) known-type)
1183 (let* ((position (car p-tail))
1184 (method (car contenders))
1185 (specl (nth position (method-specializers method)))
1186 (type (funcall type-function
1187 (type-from-specializer specl))))
1188 (multiple-value-bind (app-p maybe-app-p)
1189 (specializer-applicable-using-type-p type known-type)
1190 (flet ((determined-to-be (truth-value)
1191 (if truth-value app-p (not maybe-app-p)))
1192 (do-if (truth &optional implied)
1193 (let ((ntype (if truth type `(not ,type))))
1198 (augment-type ntype known-type))
1200 (append winners `(,method))
1203 (cond ((determined-to-be nil) (do-if nil t))
1204 ((determined-to-be t) (do-if t t))
1205 (t (funcall test-fun position type
1206 (do-if t) (do-if nil))))))))))
1207 (do-column precedence methods ()))))
1209 (defun compute-secondary-dispatch-function (generic-function net &optional
1210 method-alist wrappers)
1211 (function-funcall (compute-secondary-dispatch-function1 generic-function net)
1212 method-alist wrappers))
1214 (defvar *eq-case-table-limit* 15)
1215 (defvar *case-table-limit* 10)
1217 (defun compute-mcase-parameters (case-list)
1218 (unless (eq t (caar (last case-list)))
1219 (error "The key for the last case arg to mcase was not T"))
1220 (let* ((eq-p (dolist (case case-list t)
1221 (unless (or (eq (car case) t)
1222 (symbolp (caar case)))
1224 (len (1- (length case-list)))
1225 (type (cond ((= len 1)
1229 *eq-case-table-limit*
1230 *case-table-limit*))
1236 (defmacro mlookup (key info default &optional eq-p type)
1237 (unless (or (eq eq-p t) (null eq-p))
1238 (bug "Invalid eq-p argument: ~S" eq-p))
1242 (declare (optimize (inhibit-warnings 3)))
1243 (,(if eq-p 'eq 'eql) ,key (car ,info)))
1247 `(dolist (e ,info ,default)
1249 (declare (optimize (inhibit-warnings 3)))
1250 (,(if eq-p 'eq 'eql) (car e) ,key))
1253 `(gethash ,key ,info ,default))))
1255 (defun net-test-converter (form)
1257 (default-test-converter form)
1259 ((invoke-effective-method-function invoke-fast-method-call
1260 invoke-effective-narrow-method-function)
1267 `(mlookup ,(cadr form)
1270 ,@(compute-mcase-parameters (cddr form))))
1271 (t (default-test-converter form)))))
1273 (defun net-code-converter (form)
1275 (default-code-converter form)
1277 ((methods unordered-methods)
1278 (let ((gensym (gensym)))
1282 (let ((mp (compute-mcase-parameters (cddr form)))
1283 (gensym (gensym)) (default (gensym)))
1284 (values `(mlookup ,(cadr form) ,gensym ,default ,@mp)
1285 (list gensym default))))
1287 (default-code-converter form)))))
1289 (defun net-constant-converter (form generic-function)
1290 (or (let ((c (methods-converter form generic-function)))
1293 (default-constant-converter form)
1296 (let* ((mp (compute-mcase-parameters (cddr form)))
1297 (list (mapcar (lambda (clause)
1298 (let ((key (car clause))
1299 (meth (cadr clause)))
1300 (cons (if (consp key) (car key) key)
1302 meth generic-function))))
1304 (default (car (last list))))
1305 (list (list* :mcase mp (nbutlast list))
1308 (default-constant-converter form))))))
1310 (defun methods-converter (form generic-function)
1311 (cond ((and (consp form) (eq (car form) 'methods))
1313 (get-effective-method-function1 generic-function (cadr form))))
1314 ((and (consp form) (eq (car form) 'unordered-methods))
1315 (default-secondary-dispatch-function generic-function))))
1317 (defun convert-methods (constant method-alist wrappers)
1318 (if (and (consp constant)
1319 (eq (car constant) '.methods.))
1320 (funcall (cdr constant) method-alist wrappers)
1323 (defun convert-table (constant method-alist wrappers)
1324 (cond ((and (consp constant)
1325 (eq (car constant) :mcase))
1326 (let ((alist (mapcar (lambda (k+m)
1328 (convert-methods (cdr k+m)
1332 (mp (cadr constant)))
1339 (let ((table (make-hash-table :test (if (car mp) 'eq 'eql))))
1341 (setf (gethash (car k+m) table) (cdr k+m)))
1344 (defun compute-secondary-dispatch-function1 (generic-function net
1345 &optional function-p)
1347 ((and (eq (car net) 'methods) (not function-p))
1348 (get-effective-method-function1 generic-function (cadr net)))
1350 (let* ((name (generic-function-name generic-function))
1351 (arg-info (gf-arg-info generic-function))
1352 (metatypes (arg-info-metatypes arg-info))
1353 (nargs (length metatypes))
1354 (applyp (arg-info-applyp arg-info))
1355 (fmc-arg-info (cons nargs applyp))
1356 (arglist (if function-p
1357 (make-dfun-lambda-list nargs applyp)
1358 (make-fast-method-call-lambda-list nargs applyp))))
1359 (multiple-value-bind (cfunction constants)
1362 ,@(unless function-p
1363 `((declare (ignore .pv-cell. .next-method-call.))))
1364 (locally (declare #.*optimize-speed*)
1366 ,(make-emf-call nargs applyp 'emf))))
1367 #'net-test-converter
1368 #'net-code-converter
1370 (net-constant-converter form generic-function)))
1371 (lambda (method-alist wrappers)
1372 (let* ((alist (list nil))
1374 (dolist (constant constants)
1375 (let* ((a (or (dolist (a alist nil)
1376 (when (eq (car a) constant)
1380 constant method-alist wrappers)
1382 constant method-alist wrappers)))))
1384 (setf (cdr alist-tail) new)
1385 (setf alist-tail new)))
1386 (let ((function (apply cfunction (mapcar #'cdr (cdr alist)))))
1389 (make-fast-method-call
1390 :function (set-fun-name function `(sdfun-method ,name))
1391 :arg-info fmc-arg-info))))))))))
1393 (defvar *show-make-unordered-methods-emf-calls* nil)
1395 (defun make-unordered-methods-emf (generic-function methods)
1396 (when *show-make-unordered-methods-emf-calls*
1397 (format t "~&make-unordered-methods-emf ~S~%"
1398 (generic-function-name generic-function)))
1399 (lambda (&rest args)
1400 (let* ((types (types-from-args generic-function args 'eql))
1401 (smethods (sort-applicable-methods generic-function
1404 (emf (get-effective-method-function generic-function smethods)))
1405 (invoke-emf emf args))))
1407 ;;; The value returned by compute-discriminating-function is a function
1408 ;;; object. It is called a discriminating function because it is called
1409 ;;; when the generic function is called and its role is to discriminate
1410 ;;; on the arguments to the generic function and then call appropriate
1411 ;;; method functions.
1413 ;;; A discriminating function can only be called when it is installed as
1414 ;;; the funcallable instance function of the generic function for which
1415 ;;; it was computed.
1417 ;;; More precisely, if compute-discriminating-function is called with
1418 ;;; an argument <gf1>, and returns a result <df1>, that result must
1419 ;;; not be passed to apply or funcall directly. Rather, <df1> must be
1420 ;;; stored as the funcallable instance function of the same generic
1421 ;;; function <gf1> (using SET-FUNCALLABLE-INSTANCE-FUNCTION). Then the
1422 ;;; generic function can be passed to funcall or apply.
1424 ;;; An important exception is that methods on this generic function are
1425 ;;; permitted to return a function which itself ends up calling the value
1426 ;;; returned by a more specific method. This kind of `encapsulation' of
1427 ;;; discriminating function is critical to many uses of the MOP.
1429 ;;; As an example, the following canonical case is legal:
1431 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1432 ;;; (let ((std (call-next-method)))
1434 ;;; (print (list 'call-to-gf gf arg))
1435 ;;; (funcall std arg))))
1437 ;;; Because many discriminating functions would like to use a dynamic
1438 ;;; strategy in which the precise discriminating function changes with
1439 ;;; time it is important to specify how a discriminating function is
1440 ;;; permitted itself to change the funcallable instance function of the
1441 ;;; generic function.
1443 ;;; Discriminating functions may set the funcallable instance function
1444 ;;; of the generic function, but the new value must be generated by making
1445 ;;; a call to COMPUTE-DISCRIMINATING-FUNCTION. This is to ensure that any
1446 ;;; more specific methods which may have encapsulated the discriminating
1447 ;;; function will get a chance to encapsulate the new, inner discriminating
1450 ;;; This implies that if a discriminating function wants to modify itself
1451 ;;; it should first store some information in the generic function proper,
1452 ;;; and then call compute-discriminating-function. The appropriate method
1453 ;;; on compute-discriminating-function will see the information stored in
1454 ;;; the generic function and generate a discriminating function accordingly.
1456 ;;; The following is an example of a discriminating function which modifies
1457 ;;; itself in accordance with this protocol:
1459 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1461 ;;; (cond (<some condition>
1462 ;;; <store some info in the generic function>
1463 ;;; (set-funcallable-instance-function
1465 ;;; (compute-discriminating-function gf))
1466 ;;; (funcall gf arg))
1468 ;;; <call-a-method-of-gf>))))
1470 ;;; Whereas this code would not be legal:
1472 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1474 ;;; (cond (<some condition>
1475 ;;; (set-funcallable-instance-function
1477 ;;; (lambda (a) ..))
1478 ;;; (funcall gf arg))
1480 ;;; <call-a-method-of-gf>))))
1482 ;;; NOTE: All the examples above assume that all instances of the class
1483 ;;; my-generic-function accept only one argument.
1485 (defun slot-value-using-class-dfun (class object slotd)
1486 (declare (ignore class))
1487 (function-funcall (slot-definition-reader-function slotd) object))
1489 (defun setf-slot-value-using-class-dfun (new-value class object slotd)
1490 (declare (ignore class))
1491 (function-funcall (slot-definition-writer-function slotd) new-value object))
1493 (defun slot-boundp-using-class-dfun (class object slotd)
1494 (declare (ignore class))
1495 (function-funcall (slot-definition-boundp-function slotd) object))
1497 (defun special-case-for-compute-discriminating-function-p (gf)
1498 (or (eq gf #'slot-value-using-class)
1499 (eq gf #'(setf slot-value-using-class))
1500 (eq gf #'slot-boundp-using-class)))
1502 (defmethod compute-discriminating-function ((gf standard-generic-function))
1503 (let ((dfun-state (slot-value gf 'dfun-state)))
1504 (when (special-case-for-compute-discriminating-function-p gf)
1505 ;; if we have a special case for
1506 ;; COMPUTE-DISCRIMINATING-FUNCTION, then (at least for the
1507 ;; special cases implemented as of 2006-05-09) any information
1508 ;; in the cache is misplaced.
1509 (aver (null dfun-state)))
1510 (typecase dfun-state
1512 (when (eq gf #'compute-applicable-methods)
1513 (update-all-c-a-m-gf-info gf))
1515 ((eq gf #'slot-value-using-class)
1516 (update-slot-value-gf-info gf 'reader)
1517 #'slot-value-using-class-dfun)
1518 ((eq gf #'(setf slot-value-using-class))
1519 (update-slot-value-gf-info gf 'writer)
1520 #'setf-slot-value-using-class-dfun)
1521 ((eq gf #'slot-boundp-using-class)
1522 (update-slot-value-gf-info gf 'boundp)
1523 #'slot-boundp-using-class-dfun)
1524 ((gf-precompute-dfun-and-emf-p (slot-value gf 'arg-info))
1525 (make-final-dfun gf))
1527 (make-initial-dfun gf))))
1528 (function dfun-state)
1529 (cons (car dfun-state)))))
1531 (defmethod update-gf-dfun ((class std-class) gf)
1532 (let ((*new-class* class)
1533 (arg-info (gf-arg-info gf)))
1535 ((special-case-for-compute-discriminating-function-p gf))
1536 ((gf-precompute-dfun-and-emf-p arg-info)
1537 (multiple-value-bind (dfun cache info)
1538 (make-final-dfun-internal gf)
1539 (update-dfun gf dfun cache info))))))
1541 (defmethod (setf class-name) (new-value class)
1542 (let ((classoid (wrapper-classoid (class-wrapper class))))
1543 (if (and new-value (symbolp new-value))
1544 (setf (classoid-name classoid) new-value)
1545 (setf (classoid-name classoid) nil)))
1546 (reinitialize-instance class :name new-value)
1549 (defmethod (setf generic-function-name) (new-value generic-function)
1550 (reinitialize-instance generic-function :name new-value)
1553 (defmethod function-keyword-parameters ((method standard-method))
1554 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p
1555 keywords keyword-parameters)
1556 (analyze-lambda-list (if (consp method)
1557 (early-method-lambda-list method)
1558 (method-lambda-list method)))
1559 (declare (ignore nreq nopt keysp restp keywords))
1560 (values keyword-parameters allow-other-keys-p)))
1562 (defun method-ll->generic-function-ll (ll)
1563 (multiple-value-bind
1564 (nreq nopt keysp restp allow-other-keys-p keywords keyword-parameters)
1565 (analyze-lambda-list ll)
1566 (declare (ignore nreq nopt keysp restp allow-other-keys-p keywords))
1567 (remove-if (lambda (s)
1568 (or (memq s keyword-parameters)
1569 (eq s '&allow-other-keys)))
1572 ;;; This is based on the rules of method lambda list congruency
1573 ;;; defined in the spec. The lambda list it constructs is the pretty
1574 ;;; union of the lambda lists of the generic function and of all its
1575 ;;; methods. It doesn't take method applicability into account at all
1578 ;;; (Notice that we ignore &AUX variables as they're not part of the
1579 ;;; "public interface" of a function.)
1581 (defmethod generic-function-pretty-arglist
1582 ((generic-function standard-generic-function))
1583 (let ((gf-lambda-list (generic-function-lambda-list generic-function))
1584 (methods (generic-function-methods generic-function)))
1587 (multiple-value-bind (gf.required gf.optional gf.rest gf.keys gf.allowp)
1588 (%split-arglist gf-lambda-list)
1589 ;; Possibly extend the keyword parameters of the gf by
1590 ;; additional key parameters of its methods:
1591 (let ((methods.keys nil) (methods.allowp nil))
1593 (multiple-value-bind (m.keyparams m.allow-other-keys)
1594 (function-keyword-parameters m)
1595 (setq methods.keys (union methods.keys m.keyparams :key #'maybe-car))
1596 (setq methods.allowp (or methods.allowp m.allow-other-keys))))
1597 (let ((arglist '()))
1598 (when (or gf.allowp methods.allowp)
1599 (push '&allow-other-keys arglist))
1600 (when (or gf.keys methods.keys)
1601 ;; We make sure that the keys of the gf appear before
1602 ;; those of its methods, since they're probably more
1603 ;; generally appliable.
1604 (setq arglist (nconc (list '&key) gf.keys
1605 (nset-difference methods.keys gf.keys)
1608 (setq arglist (nconc (list '&rest gf.rest) arglist)))
1610 (setq arglist (nconc (list '&optional) gf.optional arglist)))
1611 (nconc gf.required arglist)))))))
1613 (defun maybe-car (thing)
1619 (defun %split-arglist (lambda-list)
1620 ;; This function serves to shrink the number of returned values of
1621 ;; PARSE-LAMBDA-LIST to something handier.
1622 (multiple-value-bind (required optional restp rest keyp keys allowp
1623 auxp aux morep more-context more-count)
1624 (parse-lambda-list lambda-list)
1625 (declare (ignore restp keyp auxp aux morep))
1626 (declare (ignore more-context more-count))
1627 (values required optional rest keys allowp)))