0.6.11.43:
[sbcl.git] / src / pcl / methods.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3
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
8 ;;;; information.
9
10 ;;;; copyright information from original PCL sources:
11 ;;;;
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
14 ;;;;
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
18 ;;;; control laws.
19 ;;;;
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
22 ;;;; specification.
23
24 (in-package "SB-PCL")
25 \f
26 (defmethod shared-initialize :after ((slotd standard-slot-definition)
27                                      slot-names &key)
28   (declare (ignore slot-names))
29   (with-slots (allocation class)
30     slotd
31     (setq allocation (if (eq allocation :class) class allocation))))
32
33 (defmethod shared-initialize :after ((slotd structure-slot-definition)
34                                      slot-names
35                                      &key (allocation :instance))
36   (declare (ignore slot-names))
37   (unless (eq allocation :instance)
38     (error "Structure slots must have :INSTANCE allocation.")))
39
40 (defmethod inform-type-system-about-class ((class structure-class) (name t))
41   nil)
42
43 ;;; methods
44 ;;;
45 ;;; Methods themselves are simple inanimate objects. Most properties of
46 ;;; methods are immutable, methods cannot be reinitialized. The following
47 ;;; properties of methods can be changed:
48 ;;;   METHOD-GENERIC-FUNCTION
49 ;;;   METHOD-FUNCTION       ??
50
51 (defmethod method-function ((method standard-method))
52   (or (slot-value method 'function)
53       (let ((fmf (slot-value method 'fast-function)))
54         (unless fmf ; The :BEFORE SHARED-INITIALIZE method prevents this.
55           (error "~S doesn't seem to have a METHOD-FUNCTION." method))
56         (setf (slot-value method 'function)
57               (method-function-from-fast-function fmf)))))
58
59 (defmethod accessor-method-class ((method standard-accessor-method))
60   (car (slot-value method 'specializers)))
61
62 (defmethod accessor-method-class ((method standard-writer-method))
63   (cadr (slot-value method 'specializers)))
64
65 ;;; initialization
66 ;;;
67 ;;; Error checking is done in before methods. Because of the simplicity of
68 ;;; standard method objects the standard primary method can fill the slots.
69 ;;;
70 ;;; Methods are not reinitializable.
71
72 (defmethod reinitialize-instance ((method standard-method) &rest initargs)
73   (declare (ignore initargs))
74   (error "An attempt was made to reinitialize the method ~S.~%~
75           Method objects cannot be reinitialized."
76          method))
77
78 (defmethod legal-documentation-p ((object standard-method) x)
79   (if (or (null x) (stringp x))
80       t
81       "a string or NULL"))
82
83 (defmethod legal-lambda-list-p ((object standard-method) x)
84   (declare (ignore x))
85   t)
86
87 (defmethod legal-method-function-p ((object standard-method) x)
88   (if (functionp x)
89       t
90       "a function"))
91
92 (defmethod legal-qualifiers-p ((object standard-method) x)
93   (flet ((improper-list ()
94            (return-from legal-qualifiers-p "Is not a proper list.")))
95     (dolist-carefully (q x improper-list)
96       (let ((ok (legal-qualifier-p object q)))
97         (unless (eq ok t)
98           (return-from legal-qualifiers-p
99             (format nil "Contains ~S which ~A" q ok)))))
100     t))
101
102 (defmethod legal-qualifier-p ((object standard-method) x)
103   (if (and x (atom x))
104       t
105       "is not a non-null atom"))
106
107 (defmethod legal-slot-name-p ((object standard-method) x)
108   (cond ((not (symbolp x)) "is not a symbol and so cannot be bound")
109         ((keywordp x)      "is a keyword and so cannot be bound")
110         ((memq x '(t nil)) "cannot be bound")
111         ((constantp x)     "is a constant and so cannot be bound")
112         (t t)))
113
114 (defmethod legal-specializers-p ((object standard-method) x)
115   (flet ((improper-list ()
116            (return-from legal-specializers-p "Is not a proper list.")))
117     (dolist-carefully (s x improper-list)
118       (let ((ok (legal-specializer-p object s)))
119         (unless (eq ok t)
120           (return-from legal-specializers-p
121             (format nil "Contains ~S which ~A" s ok)))))
122     t))
123
124 (defvar *allow-experimental-specializers-p* nil)
125
126 (defmethod legal-specializer-p ((object standard-method) x)
127   (if (if *allow-experimental-specializers-p*
128           (specializerp x)
129           (or (classp x)
130               (eql-specializer-p x)))
131       t
132       "is neither a class object nor an EQL specializer"))
133
134 (defmethod shared-initialize :before ((method standard-method)
135                                       slot-names
136                                       &key qualifiers
137                                            lambda-list
138                                            specializers
139                                            function
140                                            fast-function
141                                            documentation)
142   (declare (ignore slot-names))
143   (flet ((lose (initarg value string)
144            (error "when initializing the method ~S:~%~
145                    The ~S initialization argument was: ~S.~%~
146                    which ~A."
147                   method initarg value string)))
148     (let ((check-qualifiers    (legal-qualifiers-p method qualifiers))
149           (check-lambda-list   (legal-lambda-list-p method lambda-list))
150           (check-specializers  (legal-specializers-p method specializers))
151           (check-function      (legal-method-function-p method
152                                                         (or function
153                                                             fast-function)))
154           (check-documentation (legal-documentation-p method documentation)))
155       (unless (eq check-qualifiers t)
156         (lose :qualifiers qualifiers check-qualifiers))
157       (unless (eq check-lambda-list t)
158         (lose :lambda-list lambda-list check-lambda-list))
159       (unless (eq check-specializers t)
160         (lose :specializers specializers check-specializers))
161       (unless (eq check-function t)
162         (lose :function function check-function))
163       (unless (eq check-documentation t)
164         (lose :documentation documentation check-documentation)))))
165
166 (defmethod shared-initialize :before ((method standard-accessor-method)
167                                       slot-names
168                                       &key slot-name slot-definition)
169   (declare (ignore slot-names))
170   (unless slot-definition
171     (let ((legalp (legal-slot-name-p method slot-name)))
172       ;; FIXME: nasty convention; should be renamed to ILLEGAL-SLOT-NAME-P and
173       ;; ILLEGALP, and the convention redone to be less twisty
174       (unless (eq legalp t)
175         (error "The value of the :SLOT-NAME initarg ~A." legalp)))))
176
177 (defmethod shared-initialize :after ((method standard-method) slot-names
178                                      &rest initargs
179                                      &key qualifiers method-spec plist)
180   (declare (ignore slot-names method-spec plist))
181   (initialize-method-function initargs nil method)
182   (setf (plist-value method 'qualifiers) qualifiers)
183   #+ignore
184   (setf (slot-value method 'closure-generator)
185         (method-function-closure-generator (slot-value method 'function))))
186
187 (defmethod shared-initialize :after ((method standard-accessor-method)
188                                      slot-names
189                                      &key)
190   (declare (ignore slot-names))
191   (with-slots (slot-name slot-definition)
192     method
193     (unless slot-definition
194       (let ((class (accessor-method-class method)))
195         (when (slot-class-p class)
196           (setq slot-definition (find slot-name (class-direct-slots class)
197                                       :key #'slot-definition-name)))))
198     (when (and slot-definition (null slot-name))
199       (setq slot-name (slot-definition-name slot-definition)))))
200
201 (defmethod method-qualifiers ((method standard-method))
202   (plist-value method 'qualifiers))
203 \f
204 (defvar *the-class-generic-function*
205   (find-class 'generic-function))
206 (defvar *the-class-standard-generic-function*
207   (find-class 'standard-generic-function))
208 \f
209 (defmethod shared-initialize :before
210            ((generic-function standard-generic-function)
211             slot-names
212             &key (name nil namep)
213                  (lambda-list () lambda-list-p)
214                  argument-precedence-order
215                  declarations
216                  documentation
217                  (method-class nil method-class-supplied-p)
218                  (method-combination nil method-combination-supplied-p))
219   (declare (ignore slot-names
220                    declarations argument-precedence-order documentation
221                    lambda-list lambda-list-p))
222
223   (when namep
224     (set-function-name generic-function name))
225
226   (flet ((initarg-error (initarg value string)
227            (error "when initializing the generic function ~S:~%~
228                    The ~S initialization argument was: ~A.~%~
229                    It must be ~A."
230                   generic-function initarg value string)))
231     (cond (method-class-supplied-p
232            (when (symbolp method-class)
233              (setq method-class (find-class method-class)))
234            (unless (and (classp method-class)
235                         (*subtypep (class-eq-specializer method-class)
236                                    *the-class-method*))
237              (initarg-error :method-class
238                             method-class
239                             "a subclass of the class METHOD"))
240            (setf (slot-value generic-function 'method-class) method-class))
241           ((slot-boundp generic-function 'method-class))
242           (t
243            (initarg-error :method-class
244                           "not supplied"
245                           "a subclass of the class METHOD")))
246     (cond (method-combination-supplied-p
247            (unless (method-combination-p method-combination)
248              (initarg-error :method-combination
249                             method-combination
250                             "a method combination object")))
251           ((slot-boundp generic-function 'method-combination))
252           (t
253            (initarg-error :method-combination
254                           "not supplied"
255                           "a method combination object")))))
256
257 #||
258 (defmethod reinitialize-instance ((generic-function standard-generic-function)
259                                   &rest initargs
260                                   &key name
261                                        lambda-list
262                                        argument-precedence-order
263                                        declarations
264                                        documentation
265                                        method-class
266                                        method-combination)
267   (declare (ignore documentation declarations argument-precedence-order
268                    lambda-list name method-class method-combination))
269   (macrolet ((add-initarg (check name slot-name)
270                `(unless ,check
271                   (push (slot-value generic-function ,slot-name) initargs)
272                   (push ,name initargs))))
273 ;   (add-initarg name :name 'name)
274 ;   (add-initarg lambda-list :lambda-list 'lambda-list)
275 ;   (add-initarg argument-precedence-order
276 ;                :argument-precedence-order
277 ;                'argument-precedence-order)
278 ;   (add-initarg declarations :declarations 'declarations)
279 ;   (add-initarg documentation :documentation 'documentation)
280 ;   (add-initarg method-class :method-class 'method-class)
281 ;   (add-initarg method-combination :method-combination 'method-combination)
282     (apply #'call-next-method generic-function initargs)))
283 ||#
284 \f
285 ;;; These three are scheduled for demolition.
286
287 (defmethod remove-named-method (generic-function-name argument-specifiers
288                                                       &optional extra)
289   (let ((generic-function ())
290         (method ()))
291     (cond ((or (null (fboundp generic-function-name))
292                (not (generic-function-p
293                       (setq generic-function
294                             (fdefinition generic-function-name)))))
295            (error "~S does not name a generic function."
296                   generic-function-name))
297           ((null (setq method (get-method generic-function
298                                           extra
299                                           (parse-specializers
300                                             argument-specifiers)
301                                           nil)))
302            (error "There is no method for the generic function ~S~%~
303                    which matches the ARGUMENT-SPECIFIERS ~S."
304                   generic-function
305                   argument-specifiers))
306           (t
307            (remove-method generic-function method)))))
308
309 (defun real-add-named-method (generic-function-name
310                               qualifiers
311                               specializers
312                               lambda-list
313                               &rest other-initargs)
314   (unless (and (fboundp generic-function-name)
315                (typep (fdefinition generic-function-name) 'generic-function))
316     (sb-kernel::style-warn "implicitly creating new generic function ~S"
317                            generic-function-name))
318   ;; XXX What about changing the class of the generic function if
319   ;; there is one? Whose job is that, anyway? Do we need something
320   ;; kind of like CLASS-FOR-REDEFINITION?
321   (let* ((generic-function
322            (ensure-generic-function generic-function-name))
323          (specs (parse-specializers specializers))
324          (proto (method-prototype-for-gf generic-function-name))
325          (new (apply #'make-instance (class-of proto)
326                                      :qualifiers qualifiers
327                                      :specializers specs
328                                      :lambda-list lambda-list
329                                      other-initargs)))
330     (add-method generic-function new)))
331
332 (defun real-get-method (generic-function qualifiers specializers
333                                          &optional (errorp t))
334   (let ((hit 
335           (dolist (method (generic-function-methods generic-function))
336             (when (and (equal qualifiers (method-qualifiers method))
337                        (every #'same-specializer-p specializers
338                               (method-specializers method)))
339               (return method)))))
340     (cond (hit hit)
341           ((null errorp) nil)
342           (t
343            (error "no method on ~S with qualifiers ~:S and specializers ~:S"
344                   generic-function qualifiers specializers)))))
345 \f
346 (defmethod find-method ((generic-function standard-generic-function)
347                         qualifiers specializers &optional (errorp t))
348   (real-get-method generic-function qualifiers
349                    (parse-specializers specializers) errorp))
350 \f
351 ;;; Compute various information about a generic-function's arglist by looking
352 ;;; at the argument lists of the methods. The hair for trying not to use
353 ;;; &REST arguments lives here.
354 ;;;  The values returned are:
355 ;;;    number-of-required-arguments
356 ;;;       the number of required arguments to this generic-function's
357 ;;;       discriminating function
358 ;;;    &rest-argument-p
359 ;;;       whether or not this generic-function's discriminating
360 ;;;       function takes an &rest argument.
361 ;;;    specialized-argument-positions
362 ;;;       a list of the positions of the arguments this generic-function
363 ;;;       specializes (e.g. for a classical generic-function this is the
364 ;;;       list: (1)).
365 (defmethod compute-discriminating-function-arglist-info
366            ((generic-function standard-generic-function))
367   ;;(declare (values number-of-required-arguments &rest-argument-p
368   ;;             specialized-argument-postions))
369   (let ((number-required nil)
370         (restp nil)
371         (specialized-positions ())
372         (methods (generic-function-methods generic-function)))
373     (dolist (method methods)
374       (multiple-value-setq (number-required restp specialized-positions)
375         (compute-discriminating-function-arglist-info-internal
376          generic-function method number-required restp specialized-positions)))
377     (values number-required restp (sort specialized-positions #'<))))
378
379 (defun compute-discriminating-function-arglist-info-internal
380        (generic-function method number-of-requireds restp
381         specialized-argument-positions)
382   (declare (ignore generic-function)
383            (type (or null fixnum) number-of-requireds))
384   (let ((requireds 0))
385     (declare (fixnum requireds))
386     ;; Go through this methods arguments seeing how many are required,
387     ;; and whether there is an &rest argument.
388     (dolist (arg (method-lambda-list method))
389       (cond ((eq arg '&aux) (return))
390             ((memq arg '(&optional &rest &key))
391              (return (setq restp t)))
392             ((memq arg lambda-list-keywords))
393             (t (incf requireds))))
394     ;; Now go through this method's type specifiers to see which
395     ;; argument positions are type specified. Treat T specially
396     ;; in the usual sort of way. For efficiency don't bother to
397     ;; keep specialized-argument-positions sorted, rather depend
398     ;; on our caller to do that.
399     (iterate ((type-spec (list-elements (method-specializers method)))
400               (pos (interval :from 0)))
401       (unless (eq type-spec *the-class-t*)
402         (pushnew pos specialized-argument-positions)))
403     ;; Finally merge the values for this method into the values
404     ;; for the exisiting methods and return them. Note that if
405     ;; num-of-requireds is NIL it means this is the first method
406     ;; and we depend on that.
407     (values (min (or number-of-requireds requireds) requireds)
408             (or restp
409                 (and number-of-requireds (/= number-of-requireds requireds)))
410             specialized-argument-positions)))
411
412 (defun make-discriminating-function-arglist (number-required-arguments restp)
413   (nconc (gathering ((args (collecting)))
414            (iterate ((i (interval :from 0 :below number-required-arguments)))
415              (gather (intern (format nil "Discriminating Function Arg ~D" i))
416                      args)))
417          (when restp
418                `(&rest ,(intern "Discriminating Function &rest Arg")))))
419 \f
420 (defmethod generic-function-lambda-list ((gf generic-function))
421   (gf-lambda-list gf))
422
423 (defmethod gf-fast-method-function-p ((gf standard-generic-function))
424   (gf-info-fast-mf-p (slot-value gf 'arg-info)))
425
426 (defmethod initialize-instance :after ((gf standard-generic-function)
427                                        &key (lambda-list nil lambda-list-p)
428                                        argument-precedence-order)
429   (with-slots (arg-info)
430     gf
431     (if lambda-list-p
432         (set-arg-info gf
433                       :lambda-list lambda-list
434                       :argument-precedence-order argument-precedence-order)
435         (set-arg-info gf))
436     (when (arg-info-valid-p arg-info)
437       (update-dfun gf))))
438
439 (defmethod reinitialize-instance :after ((gf standard-generic-function)
440                                          &rest args
441                                          &key (lambda-list nil lambda-list-p)
442                                          (argument-precedence-order
443                                           nil argument-precedence-order-p))
444   (with-slots (arg-info)
445     gf
446     (if lambda-list-p
447         (if argument-precedence-order-p
448             (set-arg-info gf
449                           :lambda-list lambda-list
450                           :argument-precedence-order argument-precedence-order)
451             (set-arg-info gf
452                           :lambda-list lambda-list))
453         (set-arg-info gf))
454     (when (and (arg-info-valid-p arg-info)
455                args
456                (or lambda-list-p (cddr args)))
457       (update-dfun gf))))
458
459 (declaim (special *lazy-dfun-compute-p*))
460
461 (defun set-methods (gf methods)
462   (setf (generic-function-methods gf) nil)
463   (loop (when (null methods) (return gf))
464         (real-add-method gf (pop methods) methods)))
465
466 (defun real-add-method (generic-function method &optional skip-dfun-update-p)
467   (if (method-generic-function method)
468       (error "The method ~S is already part of the generic~@
469               function ~S. It can't be added to another generic~@
470               function until it is removed from the first one."
471              method (method-generic-function method))
472
473       (let* ((name (generic-function-name generic-function))
474              (qualifiers (method-qualifiers method))
475              (specializers (method-specializers method))
476              (existing (get-method generic-function
477                                    qualifiers
478                                    specializers
479                                    nil)))
480
481         ;; If there is already a method like this one then we must
482         ;; get rid of it before proceeding. Note that we call the
483         ;; generic function remove-method to remove it rather than
484         ;; doing it in some internal way.
485         (when existing (remove-method generic-function existing))
486
487         (setf (method-generic-function method) generic-function)
488         (pushnew method (generic-function-methods generic-function))
489         (dolist (specializer specializers)
490           (add-direct-method specializer method))
491         (set-arg-info generic-function :new-method method)
492         (unless skip-dfun-update-p
493           (when (member name
494                         '(make-instance default-initargs
495                           allocate-instance shared-initialize
496                           initialize-instance))
497             (update-make-instance-function-table (type-class
498                                                   (car specializers))))
499           (update-dfun generic-function))
500         method)))
501
502 (defun real-remove-method (generic-function method)
503   ;; Note: Error check prohibited by ANSI spec removed.
504   (when  (eq generic-function (method-generic-function method))
505     (let* ((name         (generic-function-name generic-function))
506            (specializers (method-specializers method))
507            (methods      (generic-function-methods generic-function))
508            (new-methods  (remove method methods)))
509       (setf (method-generic-function method) nil)
510       (setf (generic-function-methods generic-function) new-methods)
511       (dolist (specializer (method-specializers method))
512         (remove-direct-method specializer method))
513       (set-arg-info generic-function)
514       (when (member name
515                     '(make-instance
516                       default-initargs
517                       allocate-instance shared-initialize initialize-instance))
518         (update-make-instance-function-table (type-class (car specializers))))
519       (update-dfun generic-function)
520       generic-function)))
521 \f
522 (defun compute-applicable-methods-function (generic-function arguments)
523   (values (compute-applicable-methods-using-types
524            generic-function
525            (types-from-arguments generic-function arguments 'eql))))
526
527 (defmethod compute-applicable-methods
528     ((generic-function generic-function) arguments)
529   (values (compute-applicable-methods-using-types
530            generic-function
531            (types-from-arguments generic-function arguments 'eql))))
532
533 (defmethod compute-applicable-methods-using-classes
534     ((generic-function generic-function) classes)
535   (compute-applicable-methods-using-types
536    generic-function
537    (types-from-arguments generic-function classes 'class-eq)))
538
539 (defun proclaim-incompatible-superclasses (classes)
540   (setq classes (mapcar #'(lambda (class)
541                             (if (symbolp class)
542                                 (find-class class)
543                                 class))
544                         classes))
545   (dolist (class classes)
546     (dolist (other-class classes)
547       (unless (eq class other-class)
548         (pushnew other-class (class-incompatible-superclass-list class))))))
549
550 (defun superclasses-compatible-p (class1 class2)
551   (let ((cpl1 (class-precedence-list class1))
552         (cpl2 (class-precedence-list class2)))
553     (dolist (sc1 cpl1 t)
554       (dolist (ic (class-incompatible-superclass-list sc1))
555         (when (memq ic cpl2)
556           (return-from superclasses-compatible-p nil))))))
557
558 (mapc
559  #'proclaim-incompatible-superclasses
560  '(;; superclass class
561    (built-in-class std-class structure-class) ; direct subclasses of pcl-class
562    (standard-class funcallable-standard-class)
563    ;; superclass metaobject
564    (class eql-specializer class-eq-specializer method method-combination
565     generic-function slot-definition)
566    ;; metaclass built-in-class
567    (number sequence character           ; direct subclasses of t, but not array
568     standard-object structure-object)   ;                        or symbol
569    (number array character symbol       ; direct subclasses of t, but not
570     standard-object structure-object)   ;                        sequence
571    (complex float rational)             ; direct subclasses of number
572    (integer ratio)                      ; direct subclasses of rational
573    (list vector)                        ; direct subclasses of sequence
574    (cons null)                          ; direct subclasses of list
575    (string bit-vector)                  ; direct subclasses of vector
576    ))
577 \f
578 (defmethod same-specializer-p ((specl1 specializer) (specl2 specializer))
579   nil)
580
581 (defmethod same-specializer-p ((specl1 class) (specl2 class))
582   (eq specl1 specl2))
583
584 (defmethod specializer-class ((specializer class))
585   specializer)
586
587 (defmethod same-specializer-p ((specl1 class-eq-specializer)
588                                (specl2 class-eq-specializer))
589   (eq (specializer-class specl1) (specializer-class specl2)))
590
591 (defmethod same-specializer-p ((specl1 eql-specializer)
592                                (specl2 eql-specializer))
593   (eq (specializer-object specl1) (specializer-object specl2)))
594
595 (defmethod specializer-class ((specializer eql-specializer))
596   (class-of (slot-value specializer 'object)))
597
598 (defvar *in-gf-arg-info-p* nil)
599 (setf (gdefinition 'arg-info-reader)
600       (let ((mf (initialize-method-function
601                  (make-internal-reader-method-function
602                   'standard-generic-function 'arg-info)
603                  t)))
604         #'(lambda (&rest args) (funcall mf args nil))))
605
606
607 (defun error-need-at-least-n-args (function n)
608   (error "~@<The function ~2I~_~S ~I~_requires at least ~D argument~:P.~:>"
609          function
610          n))
611
612 (defun types-from-arguments (generic-function arguments
613                              &optional type-modifier)
614   (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
615       (get-generic-function-info generic-function)
616     (declare (ignore applyp metatypes nkeys))
617     (let ((types-rev nil))
618       (dotimes-fixnum (i nreq)
619         i
620         (unless arguments
621           (error-need-at-least-n-args (generic-function-name generic-function)
622                                       nreq))
623         (let ((arg (pop arguments)))
624           (push (if type-modifier `(,type-modifier ,arg) arg) types-rev)))
625       (values (nreverse types-rev) arg-info))))
626
627 (defun get-wrappers-from-classes (nkeys wrappers classes metatypes)
628   (let* ((w wrappers) (w-tail w) (mt-tail metatypes))
629     (dolist (class (if (listp classes) classes (list classes)))
630       (unless (eq t (car mt-tail))
631         (let ((c-w (class-wrapper class)))
632           (unless c-w (return-from get-wrappers-from-classes nil))
633           (if (eql nkeys 1)
634               (setq w c-w)
635               (setf (car w-tail) c-w
636                     w-tail (cdr w-tail)))))
637       (setq mt-tail (cdr mt-tail)))
638     w))
639
640 (defun sdfun-for-caching (gf classes)
641   (let ((types (mapcar #'class-eq-type classes)))
642     (multiple-value-bind (methods all-applicable-and-sorted-p)
643         (compute-applicable-methods-using-types gf types)
644       (function-funcall (get-secondary-dispatch-function1
645                          gf methods types nil t all-applicable-and-sorted-p)
646                         nil (mapcar #'class-wrapper classes)))))
647
648 (defun value-for-caching (gf classes)
649   (let ((methods (compute-applicable-methods-using-types
650                    gf (mapcar #'class-eq-type classes))))
651     (method-function-get (or (method-fast-function (car methods))
652                              (method-function (car methods)))
653                          :constant-value)))
654
655 (defun default-secondary-dispatch-function (generic-function)
656   #'(lambda (&rest args)
657       (let ((methods (compute-applicable-methods generic-function args)))
658         (if methods
659             (let ((emf (get-effective-method-function generic-function
660                                                       methods)))
661               (invoke-emf emf args))
662             (apply #'no-applicable-method generic-function args)))))
663
664 (defun list-eq (x y)
665   (loop (when (atom x) (return (eq x y)))
666         (when (atom y) (return nil))
667         (unless (eq (car x) (car y)) (return nil))
668         (setq x (cdr x)  y (cdr y))))
669
670 (defvar *std-cam-methods* nil)
671
672 (defun compute-applicable-methods-emf (generic-function)
673   (if (eq *boot-state* 'complete)
674       (let* ((cam (gdefinition 'compute-applicable-methods))
675              (cam-methods (compute-applicable-methods-using-types
676                            cam (list `(eql ,generic-function) t))))
677         (values (get-effective-method-function cam cam-methods)
678                 (list-eq cam-methods
679                          (or *std-cam-methods*
680                              (setq *std-cam-methods*
681                                    (compute-applicable-methods-using-types
682                                     cam (list `(eql ,cam) t)))))))
683       (values #'compute-applicable-methods-function t)))
684
685 (defun compute-applicable-methods-emf-std-p (gf)
686   (gf-info-c-a-m-emf-std-p (gf-arg-info gf)))
687
688 (defvar *old-c-a-m-gf-methods* nil)
689
690 (defun update-all-c-a-m-gf-info (c-a-m-gf)
691   (let ((methods (generic-function-methods c-a-m-gf)))
692     (if (and *old-c-a-m-gf-methods*
693              (every #'(lambda (old-method)
694                         (member old-method methods))
695                     *old-c-a-m-gf-methods*))
696         (let ((gfs-to-do nil)
697               (gf-classes-to-do nil))
698           (dolist (method methods)
699             (unless (member method *old-c-a-m-gf-methods*)
700               (let ((specl (car (method-specializers method))))
701                 (if (eql-specializer-p specl)
702                     (pushnew (specializer-object specl) gfs-to-do)
703                     (pushnew (specializer-class specl) gf-classes-to-do)))))
704           (map-all-generic-functions
705            #'(lambda (gf)
706                (when (or (member gf gfs-to-do)
707                          (dolist (class gf-classes-to-do nil)
708                            (member class
709                                    (class-precedence-list (class-of gf)))))
710                  (update-c-a-m-gf-info gf)))))
711         (map-all-generic-functions #'update-c-a-m-gf-info))
712     (setq *old-c-a-m-gf-methods* methods)))
713
714 (defun update-gf-info (gf)
715   (update-c-a-m-gf-info gf)
716   (update-gf-simple-accessor-type gf))
717
718 (defun update-c-a-m-gf-info (gf)
719   (unless (early-gf-p gf)
720     (multiple-value-bind (c-a-m-emf std-p)
721         (compute-applicable-methods-emf gf)
722       (let ((arg-info (gf-arg-info gf)))
723         (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
724         (setf (gf-info-c-a-m-emf-std-p arg-info) std-p)))))
725
726 (defun update-gf-simple-accessor-type (gf)
727   (let ((arg-info (gf-arg-info gf)))
728     (setf (gf-info-simple-accessor-type arg-info)
729           (let* ((methods (generic-function-methods gf))
730                  (class (and methods (class-of (car methods))))
731                  (type (and class
732                             (cond ((eq class
733                                        *the-class-standard-reader-method*)
734                                    'reader)
735                                   ((eq class
736                                        *the-class-standard-writer-method*)
737                                    'writer)
738                                   ((eq class
739                                        *the-class-standard-boundp-method*)
740                                    'boundp)))))
741             (when (and (gf-info-c-a-m-emf-std-p arg-info)
742                        type
743                        (dolist (method (cdr methods) t)
744                          (unless (eq class (class-of method)) (return nil)))
745                        (eq (generic-function-method-combination gf)
746                            *standard-method-combination*))
747               type)))))
748
749 (defun get-accessor-method-function (gf type class slotd)
750   (let* ((std-method (standard-svuc-method type))
751          (str-method (structure-svuc-method type))
752          (types1 `((eql ,class) (class-eq ,class) (eql ,slotd)))
753          (types (if (eq type 'writer) `(t ,@types1) types1))
754          (methods (compute-applicable-methods-using-types gf types))
755          (std-p (null (cdr methods))))
756     (values
757      (if std-p
758          (get-optimized-std-accessor-method-function class slotd type)
759          (get-accessor-from-svuc-method-function
760           class slotd
761           (get-secondary-dispatch-function
762            gf methods types
763            `((,(car (or (member std-method methods)
764                         (member str-method methods)
765                         (error "error in get-accessor-method-function")))
766               ,(get-optimized-std-slot-value-using-class-method-function
767                 class slotd type)))
768            (unless (and (eq type 'writer)
769                         (dolist (method methods t)
770                           (unless (eq (car (method-specializers method))
771                                       *the-class-t*)
772                             (return nil))))
773              (let ((wrappers (list (wrapper-of class)
774                                    (class-wrapper class)
775                                    (wrapper-of slotd))))
776                (if (eq type 'writer)
777                    (cons (class-wrapper *the-class-t*) wrappers)
778                    wrappers))))
779           type))
780      std-p)))
781
782 ;;; used by OPTIMIZE-SLOT-VALUE-BY-CLASS-P (vector.lisp)
783 (defun update-slot-value-gf-info (gf type)
784   (unless *new-class*
785     (update-std-or-str-methods gf type))
786   (when (and (standard-svuc-method type) (structure-svuc-method type))
787     (flet ((update-class (class)
788              (when (class-finalized-p class)
789                (dolist (slotd (class-slots class))
790                  (compute-slot-accessor-info slotd type gf)))))
791       (if *new-class*
792           (update-class *new-class*)
793           (map-all-classes #'update-class 'slot-object)))))
794
795 (defvar *standard-slot-value-using-class-method* nil)
796 (defvar *standard-setf-slot-value-using-class-method* nil)
797 (defvar *standard-slot-boundp-using-class-method* nil)
798 (defvar *structure-slot-value-using-class-method* nil)
799 (defvar *structure-setf-slot-value-using-class-method* nil)
800 (defvar *structure-slot-boundp-using-class-method* nil)
801
802 (defun standard-svuc-method (type)
803   (case type
804     (reader *standard-slot-value-using-class-method*)
805     (writer *standard-setf-slot-value-using-class-method*)
806     (boundp *standard-slot-boundp-using-class-method*)))
807
808 (defun set-standard-svuc-method (type method)
809   (case type
810     (reader (setq *standard-slot-value-using-class-method* method))
811     (writer (setq *standard-setf-slot-value-using-class-method* method))
812     (boundp (setq *standard-slot-boundp-using-class-method* method))))
813
814 (defun structure-svuc-method (type)
815   (case type
816     (reader *structure-slot-value-using-class-method*)
817     (writer *structure-setf-slot-value-using-class-method*)
818     (boundp *structure-slot-boundp-using-class-method*)))
819
820 (defun set-structure-svuc-method (type method)
821   (case type
822     (reader (setq *structure-slot-value-using-class-method* method))
823     (writer (setq *structure-setf-slot-value-using-class-method* method))
824     (boundp (setq *structure-slot-boundp-using-class-method* method))))
825
826 (defun update-std-or-str-methods (gf type)
827   (dolist (method (generic-function-methods gf))
828     (let ((specls (method-specializers method)))
829       (when (and (or (not (eq type 'writer))
830                      (eq (pop specls) *the-class-t*))
831                  (every #'classp specls))
832         (cond ((and (eq (class-name (car specls))
833                         'std-class)
834                     (eq (class-name (cadr specls))
835                         'std-object)
836                     (eq (class-name (caddr specls))
837                         'standard-effective-slot-definition))
838                (set-standard-svuc-method type method))
839               ((and (eq (class-name (car specls))
840                         'structure-class)
841                     (eq (class-name (cadr specls))
842                         'structure-object)
843                     (eq (class-name (caddr specls))
844                         'structure-effective-slot-definition))
845                (set-structure-svuc-method type method)))))))
846
847 (defun mec-all-classes-internal (spec precompute-p)
848   (cons (specializer-class spec)
849         (and (classp spec)
850              precompute-p
851              (not (or (eq spec *the-class-t*)
852                       (eq spec *the-class-slot-object*)
853                       (eq spec *the-class-std-object*)
854                       (eq spec *the-class-standard-object*)
855                       (eq spec *the-class-structure-object*)))
856              (let ((sc (class-direct-subclasses spec)))
857                (when sc
858                  (mapcan #'(lambda (class)
859                              (mec-all-classes-internal class precompute-p))
860                          sc))))))
861
862 (defun mec-all-classes (spec precompute-p)
863   (let ((classes (mec-all-classes-internal spec precompute-p)))
864     (if (null (cdr classes))
865         classes
866         (let* ((a-classes (cons nil classes))
867                (tail classes))
868           (loop (when (null (cdr tail))
869                   (return (cdr a-classes)))
870                 (let ((class (cadr tail))
871                       (ttail (cddr tail)))
872                   (if (dolist (c ttail nil)
873                         (when (eq class c) (return t)))
874                       (setf (cdr tail) (cddr tail))
875                       (setf tail (cdr tail)))))))))
876
877 (defun mec-all-class-lists (spec-list precompute-p)
878   (if (null spec-list)
879       (list nil)
880       (let* ((car-all-classes (mec-all-classes (car spec-list)
881                                                precompute-p))
882              (all-class-lists (mec-all-class-lists (cdr spec-list)
883                                                    precompute-p)))
884         (mapcan #'(lambda (list)
885                     (mapcar #'(lambda (c) (cons c list)) car-all-classes))
886                 all-class-lists))))
887
888 (defun make-emf-cache (generic-function valuep cache classes-list new-class)
889   (let* ((arg-info (gf-arg-info generic-function))
890          (nkeys (arg-info-nkeys arg-info))
891          (metatypes (arg-info-metatypes arg-info))
892          (wrappers (unless (eq nkeys 1) (make-list nkeys)))
893          (precompute-p (gf-precompute-dfun-and-emf-p arg-info))
894          (default '(default)))
895     (flet ((add-class-list (classes)
896              (when (or (null new-class) (memq new-class classes))
897                (let ((wrappers (get-wrappers-from-classes
898                                 nkeys wrappers classes metatypes)))
899                  (when (and wrappers
900                             (eq default (probe-cache cache wrappers default)))
901                    (let ((value (cond ((eq valuep t)
902                                        (sdfun-for-caching generic-function
903                                                           classes))
904                                       ((eq valuep :constant-value)
905                                        (value-for-caching generic-function
906                                                           classes)))))
907                      (setq cache (fill-cache cache wrappers value t))))))))
908       (if classes-list
909           (mapc #'add-class-list classes-list)
910           (dolist (method (generic-function-methods generic-function))
911             (mapc #'add-class-list
912                   (mec-all-class-lists (method-specializers method)
913                                        precompute-p))))
914       cache)))
915
916 (defmacro class-test (arg class)
917   (cond ((eq class *the-class-t*)
918          t)
919         ((eq class *the-class-slot-object*)
920          `(not (cl:typep (cl:class-of ,arg) 'cl:built-in-class)))
921         ((eq class *the-class-std-object*)
922          `(or (std-instance-p ,arg) (fsc-instance-p ,arg)))
923         ((eq class *the-class-standard-object*)
924          `(std-instance-p ,arg))
925         ((eq class *the-class-funcallable-standard-object*)
926          `(fsc-instance-p ,arg))
927         (t
928          `(typep ,arg ',(class-name class)))))
929
930 (defmacro class-eq-test (arg class)
931   `(eq (class-of ,arg) ',class))
932
933 (defmacro eql-test (arg object)
934   `(eql ,arg ',object))
935
936 (defun dnet-methods-p (form)
937   (and (consp form)
938        (or (eq (car form) 'methods)
939            (eq (car form) 'unordered-methods))))
940
941 ;;; This is CASE, but without gensyms.
942 (defmacro scase (arg &rest clauses)
943   `(let ((.case-arg. ,arg))
944      (cond ,@(mapcar #'(lambda (clause)
945                          (list* (cond ((null (car clause))
946                                        nil)
947                                       ((consp (car clause))
948                                        (if (null (cdar clause))
949                                            `(eql .case-arg.
950                                                  ',(caar clause))
951                                            `(member .case-arg.
952                                                     ',(car clause))))
953                                       ((member (car clause) '(t otherwise))
954                                        `t)
955                                       (t
956                                        `(eql .case-arg. ',(car clause))))
957                                 nil
958                                 (cdr clause)))
959                      clauses))))
960
961 (defmacro mcase (arg &rest clauses) `(scase ,arg ,@clauses))
962
963 (defun generate-discrimination-net (generic-function methods types sorted-p)
964   (let* ((arg-info (gf-arg-info generic-function))
965          (precedence (arg-info-precedence arg-info)))
966     (generate-discrimination-net-internal
967      generic-function methods types
968      #'(lambda (methods known-types)
969          (if (or sorted-p
970                  (block one-order-p
971                    (let ((sorted-methods nil))
972                      (map-all-orders
973                       (copy-list methods) precedence
974                       #'(lambda (methods)
975                           (when sorted-methods (return-from one-order-p nil))
976                           (setq sorted-methods methods)))
977                      (setq methods sorted-methods))
978                    t))
979              `(methods ,methods ,known-types)
980              `(unordered-methods ,methods ,known-types)))
981      #'(lambda (position type true-value false-value)
982          (let ((arg (dfun-arg-symbol position)))
983            (if (eq (car type) 'eql)
984                (let* ((false-case-p (and (consp false-value)
985                                          (or (eq (car false-value) 'scase)
986                                              (eq (car false-value) 'mcase))
987                                          (eq arg (cadr false-value))))
988                       (false-clauses (if false-case-p
989                                          (cddr false-value)
990                                          `((t ,false-value))))
991                       (case-sym (if (and (dnet-methods-p true-value)
992                                          (if false-case-p
993                                              (eq (car false-value) 'mcase)
994                                              (dnet-methods-p false-value)))
995                                     'mcase
996                                     'scase))
997                       (type-sym `(,(cadr type))))
998                  `(,case-sym ,arg
999                     (,type-sym ,true-value)
1000                     ,@false-clauses))
1001                `(if ,(let ((arg (dfun-arg-symbol position)))
1002                        (case (car type)
1003                          (class    `(class-test    ,arg ,(cadr type)))
1004                          (class-eq `(class-eq-test ,arg ,(cadr type)))))
1005                     ,true-value
1006                     ,false-value))))
1007      #'identity)))
1008
1009 (defun class-from-type (type)
1010   (if (or (atom type) (eq (car type) t))
1011       *the-class-t*
1012       (case (car type)
1013         (and (dolist (type (cdr type) *the-class-t*)
1014                (when (and (consp type) (not (eq (car type) 'not)))
1015                  (return (class-from-type type)))))
1016         (not *the-class-t*)
1017         (eql (class-of (cadr type)))
1018         (class-eq (cadr type))
1019         (class (cadr type)))))
1020
1021 (defun precompute-effective-methods (gf caching-p &optional classes-list-p)
1022   (let* ((arg-info (gf-arg-info gf))
1023          (methods (generic-function-methods gf))
1024          (precedence (arg-info-precedence arg-info))
1025          (*in-precompute-effective-methods-p* t)
1026          (classes-list nil))
1027     (generate-discrimination-net-internal
1028      gf methods nil
1029      #'(lambda (methods known-types)
1030          (when methods
1031            (when classes-list-p
1032              (push (mapcar #'class-from-type known-types) classes-list))
1033            (let ((no-eql-specls-p (not (methods-contain-eql-specializer-p
1034                                         methods))))
1035              (map-all-orders
1036               methods precedence
1037               #'(lambda (methods)
1038                   (get-secondary-dispatch-function1
1039                    gf methods known-types
1040                    nil caching-p no-eql-specls-p))))))
1041      #'(lambda (position type true-value false-value)
1042          (declare (ignore position type true-value false-value))
1043          nil)
1044      #'(lambda (type)
1045          (if (and (consp type) (eq (car type) 'eql))
1046              `(class-eq ,(class-of (cadr type)))
1047              type)))
1048     classes-list))
1049
1050 ;;; We know that known-type implies neither new-type nor `(not ,new-type).
1051 (defun augment-type (new-type known-type)
1052   (if (or (eq known-type t)
1053           (eq (car new-type) 'eql))
1054       new-type
1055       (let ((so-far (if (and (consp known-type) (eq (car known-type) 'and))
1056                         (cdr known-type)
1057                         (list known-type))))
1058         (unless (eq (car new-type) 'not)
1059           (setq so-far
1060                 (mapcan #'(lambda (type)
1061                             (unless (*subtypep new-type type)
1062                               (list type)))
1063                         so-far)))
1064         (if (null so-far)
1065             new-type
1066             `(and ,new-type ,@so-far)))))
1067
1068 (defun generate-discrimination-net-internal
1069     (gf methods types methods-function test-function type-function)
1070   (let* ((arg-info (gf-arg-info gf))
1071          (precedence (arg-info-precedence arg-info))
1072          (nreq (arg-info-number-required arg-info))
1073          (metatypes (arg-info-metatypes arg-info)))
1074     (labels ((do-column (p-tail contenders known-types)
1075                (if p-tail
1076                    (let* ((position (car p-tail))
1077                           (known-type (or (nth position types) t)))
1078                      (if (eq (nth position metatypes) t)
1079                          (do-column (cdr p-tail) contenders
1080                                     (cons (cons position known-type)
1081                                           known-types))
1082                          (do-methods p-tail contenders
1083                                      known-type () known-types)))
1084                    (funcall methods-function contenders
1085                             (let ((k-t (make-list nreq)))
1086                               (dolist (index+type known-types)
1087                                 (setf (nth (car index+type) k-t)
1088                                       (cdr index+type)))
1089                               k-t))))
1090              (do-methods (p-tail contenders known-type winners known-types)
1091                ;; CONTENDERS
1092                ;;   is a (sorted) list of methods that must be discriminated.
1093                ;; KNOWN-TYPE
1094                ;;   is the type of this argument, constructed from tests
1095                ;;   already made.
1096                ;; WINNERS
1097                ;;   is a (sorted) list of methods that are potentially
1098                ;;   applicable after the discrimination has been made.
1099                (if (null contenders)
1100                    (do-column (cdr p-tail)
1101                               winners
1102                               (cons (cons (car p-tail) known-type)
1103                                     known-types))
1104                    (let* ((position (car p-tail))
1105                           (method (car contenders))
1106                           (specl (nth position (method-specializers method)))
1107                           (type (funcall type-function
1108                                          (type-from-specializer specl))))
1109                      (multiple-value-bind (app-p maybe-app-p)
1110                          (specializer-applicable-using-type-p type known-type)
1111                        (flet ((determined-to-be (truth-value)
1112                                 (if truth-value app-p (not maybe-app-p)))
1113                               (do-if (truth &optional implied)
1114                                 (let ((ntype (if truth type `(not ,type))))
1115                                   (do-methods p-tail
1116                                     (cdr contenders)
1117                                     (if implied
1118                                         known-type
1119                                         (augment-type ntype known-type))
1120                                     (if truth
1121                                         (append winners `(,method))
1122                                         winners)
1123                                     known-types))))
1124                          (cond ((determined-to-be nil) (do-if nil t))
1125                                ((determined-to-be t)   (do-if t   t))
1126                                (t (funcall test-function position type
1127                                            (do-if t) (do-if nil))))))))))
1128       (do-column precedence methods ()))))
1129
1130 (defun compute-secondary-dispatch-function (generic-function net &optional
1131                                             method-alist wrappers)
1132   (function-funcall (compute-secondary-dispatch-function1 generic-function net)
1133                     method-alist wrappers))
1134
1135 (defvar *eq-case-table-limit* 15)
1136 (defvar *case-table-limit* 10)
1137
1138 (defun compute-mcase-parameters (case-list)
1139   (unless (eq t (caar (last case-list)))
1140     (error "The key for the last case arg to mcase was not T"))
1141   (let* ((eq-p (dolist (case case-list t)
1142                  (unless (or (eq (car case) t)
1143                              (symbolp (caar case)))
1144                    (return nil))))
1145          (len (1- (length case-list)))
1146          (type (cond ((= len 1)
1147                       :simple)
1148                      ((<= len
1149                           (if eq-p
1150                               *eq-case-table-limit*
1151                               *case-table-limit*))
1152                       :assoc)
1153                      (t
1154                       :hash-table))))
1155     (list eq-p type)))
1156
1157 (defmacro mlookup (key info default &optional eq-p type)
1158   (unless (or (eq eq-p t) (null eq-p))
1159     (error "Invalid eq-p argument"))
1160   (ecase type
1161     (:simple
1162      `(if (,(if eq-p 'eq 'eql) ,key (car ,info))
1163           (cdr ,info)
1164           ,default))
1165     (:assoc
1166      `(dolist (e ,info ,default)
1167         (when (,(if eq-p 'eq 'eql) (car e) ,key)
1168           (return (cdr e)))))
1169     (:hash-table
1170      `(gethash ,key ,info ,default))))
1171
1172 (defun net-test-converter (form)
1173   (if (atom form)
1174       (default-test-converter form)
1175       (case (car form)
1176         ((invoke-effective-method-function invoke-fast-method-call)
1177          '.call.)
1178         (methods
1179          '.methods.)
1180         (unordered-methods
1181          '.umethods.)
1182         (mcase
1183          `(mlookup ,(cadr form)
1184                    nil
1185                    nil
1186                    ,@(compute-mcase-parameters (cddr form))))
1187         (t (default-test-converter form)))))
1188
1189 (defun net-code-converter (form)
1190   (if (atom form)
1191       (default-code-converter form)
1192       (case (car form)
1193         ((methods unordered-methods)
1194          (let ((gensym (gensym)))
1195            (values gensym
1196                    (list gensym))))
1197         (mcase
1198          (let ((mp (compute-mcase-parameters (cddr form)))
1199                (gensym (gensym)) (default (gensym)))
1200            (values `(mlookup ,(cadr form) ,gensym ,default ,@mp)
1201                    (list gensym default))))
1202         (t
1203          (default-code-converter form)))))
1204
1205 (defun net-constant-converter (form generic-function)
1206   (or (let ((c (methods-converter form generic-function)))
1207         (when c (list c)))
1208       (if (atom form)
1209           (default-constant-converter form)
1210           (case (car form)
1211             (mcase
1212              (let* ((mp (compute-mcase-parameters (cddr form)))
1213                     (list (mapcar #'(lambda (clause)
1214                                       (let ((key (car clause))
1215                                             (meth (cadr clause)))
1216                                         (cons (if (consp key) (car key) key)
1217                                               (methods-converter
1218                                                meth generic-function))))
1219                                   (cddr form)))
1220                     (default (car (last list))))
1221                (list (list* ':mcase mp (nbutlast list))
1222                      (cdr default))))
1223             (t
1224              (default-constant-converter form))))))
1225
1226 (defun methods-converter (form generic-function)
1227   (cond ((and (consp form) (eq (car form) 'methods))
1228          (cons '.methods.
1229                (get-effective-method-function1 generic-function (cadr form))))
1230         ((and (consp form) (eq (car form) 'unordered-methods))
1231          (default-secondary-dispatch-function generic-function))))
1232
1233 (defun convert-methods (constant method-alist wrappers)
1234   (if (and (consp constant)
1235            (eq (car constant) '.methods.))
1236       (funcall (cdr constant) method-alist wrappers)
1237       constant))
1238
1239 (defun convert-table (constant method-alist wrappers)
1240   (cond ((and (consp constant)
1241               (eq (car constant) ':mcase))
1242          (let ((alist (mapcar #'(lambda (k+m)
1243                                   (cons (car k+m)
1244                                         (convert-methods (cdr k+m)
1245                                                          method-alist
1246                                                          wrappers)))
1247                               (cddr constant)))
1248                (mp (cadr constant)))
1249            (ecase (cadr mp)
1250              (:simple
1251               (car alist))
1252              (:assoc
1253               alist)
1254              (:hash-table
1255               (let ((table (make-hash-table :test (if (car mp) 'eq 'eql))))
1256                 (dolist (k+m alist)
1257                   (setf (gethash (car k+m) table) (cdr k+m)))
1258                 table)))))))
1259
1260 (defun compute-secondary-dispatch-function1 (generic-function net
1261                                              &optional function-p)
1262   (cond
1263    ((and (eq (car net) 'methods) (not function-p))
1264     (get-effective-method-function1 generic-function (cadr net)))
1265    (t
1266     (let* ((name (generic-function-name generic-function))
1267            (arg-info (gf-arg-info generic-function))
1268            (metatypes (arg-info-metatypes arg-info))
1269            (applyp (arg-info-applyp arg-info))
1270            (fmc-arg-info (cons (length metatypes) applyp))
1271            (arglist (if function-p
1272                         (make-dfun-lambda-list metatypes applyp)
1273                         (make-fast-method-call-lambda-list metatypes applyp))))
1274       (multiple-value-bind (cfunction constants)
1275           (get-function1 `(,(if function-p
1276                                       'sb-kernel:instance-lambda
1277                                       'lambda)
1278                            ,arglist
1279                                  ,@(unless function-p
1280                                      `((declare (ignore .pv-cell.
1281                                                         .next-method-call.))))
1282                                  (locally (declare #.*optimize-speed*)
1283                                    (let ((emf ,net))
1284                                      ,(make-emf-call metatypes applyp 'emf))))
1285                          #'net-test-converter
1286                          #'net-code-converter
1287                          #'(lambda (form)
1288                              (net-constant-converter form generic-function)))
1289         #'(lambda (method-alist wrappers)
1290             (let* ((alist (list nil))
1291                    (alist-tail alist))
1292               (dolist (constant constants)
1293                 (let* ((a (or (dolist (a alist nil)
1294                                 (when (eq (car a) constant)
1295                                   (return a)))
1296                               (cons constant
1297                                     (or (convert-table
1298                                          constant method-alist wrappers)
1299                                         (convert-methods
1300                                          constant method-alist wrappers)))))
1301                        (new (list a)))
1302                   (setf (cdr alist-tail) new)
1303                   (setf alist-tail new)))
1304               (let ((function (apply cfunction (mapcar #'cdr (cdr alist)))))
1305                 (if function-p
1306                     function
1307                     (make-fast-method-call
1308                      :function (set-function-name function
1309                                                   `(sdfun-method ,name))
1310                      :arg-info fmc-arg-info))))))))))
1311
1312 (defvar *show-make-unordered-methods-emf-calls* nil)
1313
1314 (defun make-unordered-methods-emf (generic-function methods)
1315   (when *show-make-unordered-methods-emf-calls*
1316     (format t "~&make-unordered-methods-emf ~S~%"
1317             (generic-function-name generic-function)))
1318   #'(lambda (&rest args)
1319       (let* ((types (types-from-arguments generic-function args 'eql))
1320              (smethods (sort-applicable-methods generic-function
1321                                                 methods
1322                                                 types))
1323              (emf (get-effective-method-function generic-function smethods)))
1324         (invoke-emf emf args))))
1325 \f
1326 ;;; The value returned by compute-discriminating-function is a function
1327 ;;; object. It is called a discriminating function because it is called
1328 ;;; when the generic function is called and its role is to discriminate
1329 ;;; on the arguments to the generic function and then call appropriate
1330 ;;; method functions.
1331 ;;;
1332 ;;; A discriminating function can only be called when it is installed as
1333 ;;; the funcallable instance function of the generic function for which
1334 ;;; it was computed.
1335 ;;;
1336 ;;; More precisely, if compute-discriminating-function is called with an
1337 ;;; argument <gf1>, and returns a result <df1>, that result must not be
1338 ;;; passed to apply or funcall directly. Rather, <df1> must be stored as
1339 ;;; the funcallable instance function of the same generic function <gf1>
1340 ;;; (using set-funcallable-instance-function). Then the generic function
1341 ;;; can be passed to funcall or apply.
1342 ;;;
1343 ;;; An important exception is that methods on this generic function are
1344 ;;; permitted to return a function which itself ends up calling the value
1345 ;;; returned by a more specific method. This kind of `encapsulation' of
1346 ;;; discriminating function is critical to many uses of the MOP.
1347 ;;;
1348 ;;; As an example, the following canonical case is legal:
1349 ;;;
1350 ;;;   (defmethod compute-discriminating-function ((gf my-generic-function))
1351 ;;;     (let ((std (call-next-method)))
1352 ;;;       #'(lambda (arg)
1353 ;;;         (print (list 'call-to-gf gf arg))
1354 ;;;         (funcall std arg))))
1355 ;;;
1356 ;;; Because many discriminating functions would like to use a dynamic
1357 ;;; strategy in which the precise discriminating function changes with
1358 ;;; time it is important to specify how a discriminating function is
1359 ;;; permitted itself to change the funcallable instance function of the
1360 ;;; generic function.
1361 ;;;
1362 ;;; Discriminating functions may set the funcallable instance function
1363 ;;; of the generic function, but the new value must be generated by making
1364 ;;; a call to COMPUTE-DISCRIMINATING-FUNCTION. This is to ensure that any
1365 ;;; more specific methods which may have encapsulated the discriminating
1366 ;;; function will get a chance to encapsulate the new, inner discriminating
1367 ;;; function.
1368 ;;;
1369 ;;; This implies that if a discriminating function wants to modify itself
1370 ;;; it should first store some information in the generic function proper,
1371 ;;; and then call compute-discriminating-function. The appropriate method
1372 ;;; on compute-discriminating-function will see the information stored in
1373 ;;; the generic function and generate a discriminating function accordingly.
1374 ;;;
1375 ;;; The following is an example of a discriminating function which modifies
1376 ;;; itself in accordance with this protocol:
1377 ;;;
1378 ;;;   (defmethod compute-discriminating-function ((gf my-generic-function))
1379 ;;;     #'(lambda (arg)
1380 ;;;      (cond (<some condition>
1381 ;;;             <store some info in the generic function>
1382 ;;;             (set-funcallable-instance-function
1383 ;;;               gf
1384 ;;;               (compute-discriminating-function gf))
1385 ;;;             (funcall gf arg))
1386 ;;;            (t
1387 ;;;             <call-a-method-of-gf>))))
1388 ;;;
1389 ;;; Whereas this code would not be legal:
1390 ;;;
1391 ;;;   (defmethod compute-discriminating-function ((gf my-generic-function))
1392 ;;;     #'(lambda (arg)
1393 ;;;      (cond (<some condition>
1394 ;;;             (set-funcallable-instance-function
1395 ;;;               gf
1396 ;;;               #'(lambda (a) ..))
1397 ;;;             (funcall gf arg))
1398 ;;;            (t
1399 ;;;             <call-a-method-of-gf>))))
1400 ;;;
1401 ;;; NOTE:  All the examples above assume that all instances of the class
1402 ;;;     my-generic-function accept only one argument.
1403
1404 (defun slot-value-using-class-dfun (class object slotd)
1405   (declare (ignore class))
1406   (function-funcall (slot-definition-reader-function slotd) object))
1407
1408 (defun setf-slot-value-using-class-dfun (new-value class object slotd)
1409   (declare (ignore class))
1410   (function-funcall (slot-definition-writer-function slotd) new-value object))
1411
1412 (defun slot-boundp-using-class-dfun (class object slotd)
1413   (declare (ignore class))
1414   (function-funcall (slot-definition-boundp-function slotd) object))
1415
1416 (defmethod compute-discriminating-function ((gf standard-generic-function))
1417   (with-slots (dfun-state arg-info) gf
1418     (typecase dfun-state
1419       (null (let ((name (generic-function-name gf)))
1420               (when (eq name 'compute-applicable-methods)
1421                 (update-all-c-a-m-gf-info gf))
1422               (cond ((eq name 'slot-value-using-class)
1423                      (update-slot-value-gf-info gf 'reader)
1424                      #'slot-value-using-class-dfun)
1425                     ((equal name '(setf slot-value-using-class))
1426                      (update-slot-value-gf-info gf 'writer)
1427                      #'setf-slot-value-using-class-dfun)
1428                     ((eq name 'slot-boundp-using-class)
1429                      (update-slot-value-gf-info gf 'boundp)
1430                      #'slot-boundp-using-class-dfun)
1431                     ((gf-precompute-dfun-and-emf-p arg-info)
1432                      (make-final-dfun gf))
1433                     (t
1434                      (make-initial-dfun gf)))))
1435       (function dfun-state)
1436       (cons (car dfun-state)))))
1437
1438 (defmethod update-gf-dfun ((class std-class) gf)
1439   (let ((*new-class* class)
1440         #|| (name (generic-function-name gf)) ||#
1441         (arg-info (gf-arg-info gf)))
1442     (cond #||
1443           ((eq name 'slot-value-using-class)
1444            (update-slot-value-gf-info gf 'reader))
1445           ((equal name '(setf slot-value-using-class))
1446            (update-slot-value-gf-info gf 'writer))
1447           ((eq name 'slot-boundp-using-class)
1448            (update-slot-value-gf-info gf 'boundp))
1449           ||#
1450           ((gf-precompute-dfun-and-emf-p arg-info)
1451            (multiple-value-bind (dfun cache info)
1452                (make-final-dfun-internal gf)
1453              (set-dfun gf dfun cache info) ; lest the cache be freed twice
1454              (update-dfun gf dfun cache info))))))
1455 \f
1456 (defmethod function-keywords ((method standard-method))
1457   (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1458       (analyze-lambda-list (if (consp method)
1459                                (early-method-lambda-list method)
1460                                (method-lambda-list method)))
1461     (declare (ignore nreq nopt keysp restp))
1462     (values keywords allow-other-keys-p)))
1463
1464 (defun method-ll->generic-function-ll (ll)
1465   (multiple-value-bind
1466       (nreq nopt keysp restp allow-other-keys-p keywords keyword-parameters)
1467       (analyze-lambda-list ll)
1468     (declare (ignore nreq nopt keysp restp allow-other-keys-p keywords))
1469     (remove-if #'(lambda (s)
1470                    (or (memq s keyword-parameters)
1471                        (eq s '&allow-other-keys)))
1472                ll)))
1473 \f
1474 ;;; This is based on the rules of method lambda list congruency defined in
1475 ;;; the spec. The lambda list it constructs is the pretty union of the
1476 ;;; lambda lists of all the methods. It doesn't take method applicability
1477 ;;; into account at all yet.
1478 (defmethod generic-function-pretty-arglist
1479            ((generic-function standard-generic-function))
1480   (let ((methods (generic-function-methods generic-function))
1481         (arglist ()))
1482     (when methods
1483       (multiple-value-bind (required optional rest key allow-other-keys)
1484           (method-pretty-arglist (car methods))
1485         (dolist (m (cdr methods))
1486           (multiple-value-bind (method-key-keywords
1487                                 method-allow-other-keys
1488                                 method-key)
1489               (function-keywords m)
1490             ;; we've modified function-keywords to return what we want as
1491             ;;  the third value, no other change here.
1492             (declare (ignore method-key-keywords))
1493             (setq key (union key method-key))
1494             (setq allow-other-keys (or allow-other-keys
1495                                        method-allow-other-keys))))
1496         (when allow-other-keys
1497           (setq arglist '(&allow-other-keys)))
1498         (when key
1499           (setq arglist (nconc (list '&key) key arglist)))
1500         (when rest
1501           (setq arglist (nconc (list '&rest rest) arglist)))
1502         (when optional
1503           (setq arglist (nconc (list '&optional) optional arglist)))
1504         (nconc required arglist)))))
1505
1506 (defmethod method-pretty-arglist ((method standard-method))
1507   (let ((required ())
1508         (optional ())
1509         (rest nil)
1510         (key ())
1511         (allow-other-keys nil)
1512         (state 'required)
1513         (arglist (method-lambda-list method)))
1514     (dolist (arg arglist)
1515       (cond ((eq arg '&optional)         (setq state 'optional))
1516             ((eq arg '&rest)             (setq state 'rest))
1517             ((eq arg '&key)              (setq state 'key))
1518             ((eq arg '&allow-other-keys) (setq allow-other-keys t))
1519             ((memq arg lambda-list-keywords))
1520             (t
1521              (ecase state
1522                (required (push arg required))
1523                (optional (push arg optional))
1524                (key      (push arg key))
1525                (rest     (setq rest arg))))))
1526     (values (nreverse required)
1527             (nreverse optional)
1528             rest
1529             (nreverse key)
1530             allow-other-keys)))
1531