93717ec6d9a4f6fb5c154eeef646f973598598ba
[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 (defun types-from-arguments (generic-function arguments
607                              &optional type-modifier)
608   (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
609       (get-generic-function-info generic-function)
610     (declare (ignore applyp metatypes nkeys))
611     (let ((types-rev nil))
612       (dotimes-fixnum (i nreq)
613         i
614         (unless arguments
615           (error "The function ~S requires at least ~D arguments"
616                  (generic-function-name generic-function)
617                  nreq))
618         (let ((arg (pop arguments)))
619           (push (if type-modifier `(,type-modifier ,arg) arg) types-rev)))
620       (values (nreverse types-rev) arg-info))))
621
622 (defun get-wrappers-from-classes (nkeys wrappers classes metatypes)
623   (let* ((w wrappers) (w-tail w) (mt-tail metatypes))
624     (dolist (class (if (listp classes) classes (list classes)))
625       (unless (eq t (car mt-tail))
626         (let ((c-w (class-wrapper class)))
627           (unless c-w (return-from get-wrappers-from-classes nil))
628           (if (eql nkeys 1)
629               (setq w c-w)
630               (setf (car w-tail) c-w
631                     w-tail (cdr w-tail)))))
632       (setq mt-tail (cdr mt-tail)))
633     w))
634
635 (defun sdfun-for-caching (gf classes)
636   (let ((types (mapcar #'class-eq-type classes)))
637     (multiple-value-bind (methods all-applicable-and-sorted-p)
638         (compute-applicable-methods-using-types gf types)
639       (function-funcall (get-secondary-dispatch-function1
640                          gf methods types nil t all-applicable-and-sorted-p)
641                         nil (mapcar #'class-wrapper classes)))))
642
643 (defun value-for-caching (gf classes)
644   (let ((methods (compute-applicable-methods-using-types
645                    gf (mapcar #'class-eq-type classes))))
646     (method-function-get (or (method-fast-function (car methods))
647                              (method-function (car methods)))
648                          :constant-value)))
649
650 (defun default-secondary-dispatch-function (generic-function)
651   #'(lambda (&rest args)
652       (let ((methods (compute-applicable-methods generic-function args)))
653         (if methods
654             (let ((emf (get-effective-method-function generic-function
655                                                       methods)))
656               (invoke-emf emf args))
657             (apply #'no-applicable-method generic-function args)))))
658
659 (defun list-eq (x y)
660   (loop (when (atom x) (return (eq x y)))
661         (when (atom y) (return nil))
662         (unless (eq (car x) (car y)) (return nil))
663         (setq x (cdr x)  y (cdr y))))
664
665 (defvar *std-cam-methods* nil)
666
667 (defun compute-applicable-methods-emf (generic-function)
668   (if (eq *boot-state* 'complete)
669       (let* ((cam (gdefinition 'compute-applicable-methods))
670              (cam-methods (compute-applicable-methods-using-types
671                            cam (list `(eql ,generic-function) t))))
672         (values (get-effective-method-function cam cam-methods)
673                 (list-eq cam-methods
674                          (or *std-cam-methods*
675                              (setq *std-cam-methods*
676                                    (compute-applicable-methods-using-types
677                                     cam (list `(eql ,cam) t)))))))
678       (values #'compute-applicable-methods-function t)))
679
680 (defun compute-applicable-methods-emf-std-p (gf)
681   (gf-info-c-a-m-emf-std-p (gf-arg-info gf)))
682
683 (defvar *old-c-a-m-gf-methods* nil)
684
685 (defun update-all-c-a-m-gf-info (c-a-m-gf)
686   (let ((methods (generic-function-methods c-a-m-gf)))
687     (if (and *old-c-a-m-gf-methods*
688              (every #'(lambda (old-method)
689                         (member old-method methods))
690                     *old-c-a-m-gf-methods*))
691         (let ((gfs-to-do nil)
692               (gf-classes-to-do nil))
693           (dolist (method methods)
694             (unless (member method *old-c-a-m-gf-methods*)
695               (let ((specl (car (method-specializers method))))
696                 (if (eql-specializer-p specl)
697                     (pushnew (specializer-object specl) gfs-to-do)
698                     (pushnew (specializer-class specl) gf-classes-to-do)))))
699           (map-all-generic-functions
700            #'(lambda (gf)
701                (when (or (member gf gfs-to-do)
702                          (dolist (class gf-classes-to-do nil)
703                            (member class
704                                    (class-precedence-list (class-of gf)))))
705                  (update-c-a-m-gf-info gf)))))
706         (map-all-generic-functions #'update-c-a-m-gf-info))
707     (setq *old-c-a-m-gf-methods* methods)))
708
709 (defun update-gf-info (gf)
710   (update-c-a-m-gf-info gf)
711   (update-gf-simple-accessor-type gf))
712
713 (defun update-c-a-m-gf-info (gf)
714   (unless (early-gf-p gf)
715     (multiple-value-bind (c-a-m-emf std-p)
716         (compute-applicable-methods-emf gf)
717       (let ((arg-info (gf-arg-info gf)))
718         (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
719         (setf (gf-info-c-a-m-emf-std-p arg-info) std-p)))))
720
721 (defun update-gf-simple-accessor-type (gf)
722   (let ((arg-info (gf-arg-info gf)))
723     (setf (gf-info-simple-accessor-type arg-info)
724           (let* ((methods (generic-function-methods gf))
725                  (class (and methods (class-of (car methods))))
726                  (type (and class
727                             (cond ((eq class
728                                        *the-class-standard-reader-method*)
729                                    'reader)
730                                   ((eq class
731                                        *the-class-standard-writer-method*)
732                                    'writer)
733                                   ((eq class
734                                        *the-class-standard-boundp-method*)
735                                    'boundp)))))
736             (when (and (gf-info-c-a-m-emf-std-p arg-info)
737                        type
738                        (dolist (method (cdr methods) t)
739                          (unless (eq class (class-of method)) (return nil)))
740                        (eq (generic-function-method-combination gf)
741                            *standard-method-combination*))
742               type)))))
743
744 (defun get-accessor-method-function (gf type class slotd)
745   (let* ((std-method (standard-svuc-method type))
746          (str-method (structure-svuc-method type))
747          (types1 `((eql ,class) (class-eq ,class) (eql ,slotd)))
748          (types (if (eq type 'writer) `(t ,@types1) types1))
749          (methods (compute-applicable-methods-using-types gf types))
750          (std-p (null (cdr methods))))
751     (values
752      (if std-p
753          (get-optimized-std-accessor-method-function class slotd type)
754          (get-accessor-from-svuc-method-function
755           class slotd
756           (get-secondary-dispatch-function
757            gf methods types
758            `((,(car (or (member std-method methods)
759                         (member str-method methods)
760                         (error "error in get-accessor-method-function")))
761               ,(get-optimized-std-slot-value-using-class-method-function
762                 class slotd type)))
763            (unless (and (eq type 'writer)
764                         (dolist (method methods t)
765                           (unless (eq (car (method-specializers method))
766                                       *the-class-t*)
767                             (return nil))))
768              (let ((wrappers (list (wrapper-of class)
769                                    (class-wrapper class)
770                                    (wrapper-of slotd))))
771                (if (eq type 'writer)
772                    (cons (class-wrapper *the-class-t*) wrappers)
773                    wrappers))))
774           type))
775      std-p)))
776
777 ;;; used by OPTIMIZE-SLOT-VALUE-BY-CLASS-P (vector.lisp)
778 (defun update-slot-value-gf-info (gf type)
779   (unless *new-class*
780     (update-std-or-str-methods gf type))
781   (when (and (standard-svuc-method type) (structure-svuc-method type))
782     (flet ((update-class (class)
783              (when (class-finalized-p class)
784                (dolist (slotd (class-slots class))
785                  (compute-slot-accessor-info slotd type gf)))))
786       (if *new-class*
787           (update-class *new-class*)
788           (map-all-classes #'update-class 'slot-object)))))
789
790 (defvar *standard-slot-value-using-class-method* nil)
791 (defvar *standard-setf-slot-value-using-class-method* nil)
792 (defvar *standard-slot-boundp-using-class-method* nil)
793 (defvar *structure-slot-value-using-class-method* nil)
794 (defvar *structure-setf-slot-value-using-class-method* nil)
795 (defvar *structure-slot-boundp-using-class-method* nil)
796
797 (defun standard-svuc-method (type)
798   (case type
799     (reader *standard-slot-value-using-class-method*)
800     (writer *standard-setf-slot-value-using-class-method*)
801     (boundp *standard-slot-boundp-using-class-method*)))
802
803 (defun set-standard-svuc-method (type method)
804   (case type
805     (reader (setq *standard-slot-value-using-class-method* method))
806     (writer (setq *standard-setf-slot-value-using-class-method* method))
807     (boundp (setq *standard-slot-boundp-using-class-method* method))))
808
809 (defun structure-svuc-method (type)
810   (case type
811     (reader *structure-slot-value-using-class-method*)
812     (writer *structure-setf-slot-value-using-class-method*)
813     (boundp *structure-slot-boundp-using-class-method*)))
814
815 (defun set-structure-svuc-method (type method)
816   (case type
817     (reader (setq *structure-slot-value-using-class-method* method))
818     (writer (setq *structure-setf-slot-value-using-class-method* method))
819     (boundp (setq *structure-slot-boundp-using-class-method* method))))
820
821 (defun update-std-or-str-methods (gf type)
822   (dolist (method (generic-function-methods gf))
823     (let ((specls (method-specializers method)))
824       (when (and (or (not (eq type 'writer))
825                      (eq (pop specls) *the-class-t*))
826                  (every #'classp specls))
827         (cond ((and (eq (class-name (car specls))
828                         'std-class)
829                     (eq (class-name (cadr specls))
830                         'std-object)
831                     (eq (class-name (caddr specls))
832                         'standard-effective-slot-definition))
833                (set-standard-svuc-method type method))
834               ((and (eq (class-name (car specls))
835                         'structure-class)
836                     (eq (class-name (cadr specls))
837                         'structure-object)
838                     (eq (class-name (caddr specls))
839                         'structure-effective-slot-definition))
840                (set-structure-svuc-method type method)))))))
841
842 (defun mec-all-classes-internal (spec precompute-p)
843   (cons (specializer-class spec)
844         (and (classp spec)
845              precompute-p
846              (not (or (eq spec *the-class-t*)
847                       (eq spec *the-class-slot-object*)
848                       (eq spec *the-class-std-object*)
849                       (eq spec *the-class-standard-object*)
850                       (eq spec *the-class-structure-object*)))
851              (let ((sc (class-direct-subclasses spec)))
852                (when sc
853                  (mapcan #'(lambda (class)
854                              (mec-all-classes-internal class precompute-p))
855                          sc))))))
856
857 (defun mec-all-classes (spec precompute-p)
858   (let ((classes (mec-all-classes-internal spec precompute-p)))
859     (if (null (cdr classes))
860         classes
861         (let* ((a-classes (cons nil classes))
862                (tail classes))
863           (loop (when (null (cdr tail))
864                   (return (cdr a-classes)))
865                 (let ((class (cadr tail))
866                       (ttail (cddr tail)))
867                   (if (dolist (c ttail nil)
868                         (when (eq class c) (return t)))
869                       (setf (cdr tail) (cddr tail))
870                       (setf tail (cdr tail)))))))))
871
872 (defun mec-all-class-lists (spec-list precompute-p)
873   (if (null spec-list)
874       (list nil)
875       (let* ((car-all-classes (mec-all-classes (car spec-list)
876                                                precompute-p))
877              (all-class-lists (mec-all-class-lists (cdr spec-list)
878                                                    precompute-p)))
879         (mapcan #'(lambda (list)
880                     (mapcar #'(lambda (c) (cons c list)) car-all-classes))
881                 all-class-lists))))
882
883 (defun make-emf-cache (generic-function valuep cache classes-list new-class)
884   (let* ((arg-info (gf-arg-info generic-function))
885          (nkeys (arg-info-nkeys arg-info))
886          (metatypes (arg-info-metatypes arg-info))
887          (wrappers (unless (eq nkeys 1) (make-list nkeys)))
888          (precompute-p (gf-precompute-dfun-and-emf-p arg-info))
889          (default '(default)))
890     (flet ((add-class-list (classes)
891              (when (or (null new-class) (memq new-class classes))
892                (let ((wrappers (get-wrappers-from-classes
893                                 nkeys wrappers classes metatypes)))
894                  (when (and wrappers
895                             (eq default (probe-cache cache wrappers default)))
896                    (let ((value (cond ((eq valuep t)
897                                        (sdfun-for-caching generic-function
898                                                           classes))
899                                       ((eq valuep :constant-value)
900                                        (value-for-caching generic-function
901                                                           classes)))))
902                      (setq cache (fill-cache cache wrappers value t))))))))
903       (if classes-list
904           (mapc #'add-class-list classes-list)
905           (dolist (method (generic-function-methods generic-function))
906             (mapc #'add-class-list
907                   (mec-all-class-lists (method-specializers method)
908                                        precompute-p))))
909       cache)))
910
911 (defmacro class-test (arg class)
912   (cond ((eq class *the-class-t*)
913          t)
914         ((eq class *the-class-slot-object*)
915          `(not (cl:typep (cl:class-of ,arg) 'cl:built-in-class)))
916         ((eq class *the-class-std-object*)
917          `(or (std-instance-p ,arg) (fsc-instance-p ,arg)))
918         ((eq class *the-class-standard-object*)
919          `(std-instance-p ,arg))
920         ((eq class *the-class-funcallable-standard-object*)
921          `(fsc-instance-p ,arg))
922         (t
923          `(typep ,arg ',(class-name class)))))
924
925 (defmacro class-eq-test (arg class)
926   `(eq (class-of ,arg) ',class))
927
928 (defmacro eql-test (arg object)
929   `(eql ,arg ',object))
930
931 (defun dnet-methods-p (form)
932   (and (consp form)
933        (or (eq (car form) 'methods)
934            (eq (car form) 'unordered-methods))))
935
936 ;;; This is CASE, but without gensyms.
937 (defmacro scase (arg &rest clauses)
938   `(let ((.case-arg. ,arg))
939      (cond ,@(mapcar #'(lambda (clause)
940                          (list* (cond ((null (car clause))
941                                        nil)
942                                       ((consp (car clause))
943                                        (if (null (cdar clause))
944                                            `(eql .case-arg.
945                                                  ',(caar clause))
946                                            `(member .case-arg.
947                                                     ',(car clause))))
948                                       ((member (car clause) '(t otherwise))
949                                        `t)
950                                       (t
951                                        `(eql .case-arg. ',(car clause))))
952                                 nil
953                                 (cdr clause)))
954                      clauses))))
955
956 (defmacro mcase (arg &rest clauses) `(scase ,arg ,@clauses))
957
958 (defun generate-discrimination-net (generic-function methods types sorted-p)
959   (let* ((arg-info (gf-arg-info generic-function))
960          (precedence (arg-info-precedence arg-info)))
961     (generate-discrimination-net-internal
962      generic-function methods types
963      #'(lambda (methods known-types)
964          (if (or sorted-p
965                  (block one-order-p
966                    (let ((sorted-methods nil))
967                      (map-all-orders
968                       (copy-list methods) precedence
969                       #'(lambda (methods)
970                           (when sorted-methods (return-from one-order-p nil))
971                           (setq sorted-methods methods)))
972                      (setq methods sorted-methods))
973                    t))
974              `(methods ,methods ,known-types)
975              `(unordered-methods ,methods ,known-types)))
976      #'(lambda (position type true-value false-value)
977          (let ((arg (dfun-arg-symbol position)))
978            (if (eq (car type) 'eql)
979                (let* ((false-case-p (and (consp false-value)
980                                          (or (eq (car false-value) 'scase)
981                                              (eq (car false-value) 'mcase))
982                                          (eq arg (cadr false-value))))
983                       (false-clauses (if false-case-p
984                                          (cddr false-value)
985                                          `((t ,false-value))))
986                       (case-sym (if (and (dnet-methods-p true-value)
987                                          (if false-case-p
988                                              (eq (car false-value) 'mcase)
989                                              (dnet-methods-p false-value)))
990                                     'mcase
991                                     'scase))
992                       (type-sym `(,(cadr type))))
993                  `(,case-sym ,arg
994                     (,type-sym ,true-value)
995                     ,@false-clauses))
996                `(if ,(let ((arg (dfun-arg-symbol position)))
997                        (case (car type)
998                          (class    `(class-test    ,arg ,(cadr type)))
999                          (class-eq `(class-eq-test ,arg ,(cadr type)))))
1000                     ,true-value
1001                     ,false-value))))
1002      #'identity)))
1003
1004 (defun class-from-type (type)
1005   (if (or (atom type) (eq (car type) t))
1006       *the-class-t*
1007       (case (car type)
1008         (and (dolist (type (cdr type) *the-class-t*)
1009                (when (and (consp type) (not (eq (car type) 'not)))
1010                  (return (class-from-type type)))))
1011         (not *the-class-t*)
1012         (eql (class-of (cadr type)))
1013         (class-eq (cadr type))
1014         (class (cadr type)))))
1015
1016 (defun precompute-effective-methods (gf caching-p &optional classes-list-p)
1017   (let* ((arg-info (gf-arg-info gf))
1018          (methods (generic-function-methods gf))
1019          (precedence (arg-info-precedence arg-info))
1020          (*in-precompute-effective-methods-p* t)
1021          (classes-list nil))
1022     (generate-discrimination-net-internal
1023      gf methods nil
1024      #'(lambda (methods known-types)
1025          (when methods
1026            (when classes-list-p
1027              (push (mapcar #'class-from-type known-types) classes-list))
1028            (let ((no-eql-specls-p (not (methods-contain-eql-specializer-p
1029                                         methods))))
1030              (map-all-orders
1031               methods precedence
1032               #'(lambda (methods)
1033                   (get-secondary-dispatch-function1
1034                    gf methods known-types
1035                    nil caching-p no-eql-specls-p))))))
1036      #'(lambda (position type true-value false-value)
1037          (declare (ignore position type true-value false-value))
1038          nil)
1039      #'(lambda (type)
1040          (if (and (consp type) (eq (car type) 'eql))
1041              `(class-eq ,(class-of (cadr type)))
1042              type)))
1043     classes-list))
1044
1045 ;;; We know that known-type implies neither new-type nor `(not ,new-type).
1046 (defun augment-type (new-type known-type)
1047   (if (or (eq known-type t)
1048           (eq (car new-type) 'eql))
1049       new-type
1050       (let ((so-far (if (and (consp known-type) (eq (car known-type) 'and))
1051                         (cdr known-type)
1052                         (list known-type))))
1053         (unless (eq (car new-type) 'not)
1054           (setq so-far
1055                 (mapcan #'(lambda (type)
1056                             (unless (*subtypep new-type type)
1057                               (list type)))
1058                         so-far)))
1059         (if (null so-far)
1060             new-type
1061             `(and ,new-type ,@so-far)))))
1062
1063 (defun generate-discrimination-net-internal
1064     (gf methods types methods-function test-function type-function)
1065   (let* ((arg-info (gf-arg-info gf))
1066          (precedence (arg-info-precedence arg-info))
1067          (nreq (arg-info-number-required arg-info))
1068          (metatypes (arg-info-metatypes arg-info)))
1069     (labels ((do-column (p-tail contenders known-types)
1070                (if p-tail
1071                    (let* ((position (car p-tail))
1072                           (known-type (or (nth position types) t)))
1073                      (if (eq (nth position metatypes) t)
1074                          (do-column (cdr p-tail) contenders
1075                                     (cons (cons position known-type)
1076                                           known-types))
1077                          (do-methods p-tail contenders
1078                                      known-type () known-types)))
1079                    (funcall methods-function contenders
1080                             (let ((k-t (make-list nreq)))
1081                               (dolist (index+type known-types)
1082                                 (setf (nth (car index+type) k-t)
1083                                       (cdr index+type)))
1084                               k-t))))
1085              (do-methods (p-tail contenders known-type winners known-types)
1086                ;; CONTENDERS
1087                ;;   is a (sorted) list of methods that must be discriminated.
1088                ;; KNOWN-TYPE
1089                ;;   is the type of this argument, constructed from tests
1090                ;;   already made.
1091                ;; WINNERS
1092                ;;   is a (sorted) list of methods that are potentially
1093                ;;   applicable after the discrimination has been made.
1094                (if (null contenders)
1095                    (do-column (cdr p-tail)
1096                               winners
1097                               (cons (cons (car p-tail) known-type)
1098                                     known-types))
1099                    (let* ((position (car p-tail))
1100                           (method (car contenders))
1101                           (specl (nth position (method-specializers method)))
1102                           (type (funcall type-function
1103                                          (type-from-specializer specl))))
1104                      (multiple-value-bind (app-p maybe-app-p)
1105                          (specializer-applicable-using-type-p type known-type)
1106                        (flet ((determined-to-be (truth-value)
1107                                 (if truth-value app-p (not maybe-app-p)))
1108                               (do-if (truth &optional implied)
1109                                 (let ((ntype (if truth type `(not ,type))))
1110                                   (do-methods p-tail
1111                                     (cdr contenders)
1112                                     (if implied
1113                                         known-type
1114                                         (augment-type ntype known-type))
1115                                     (if truth
1116                                         (append winners `(,method))
1117                                         winners)
1118                                     known-types))))
1119                          (cond ((determined-to-be nil) (do-if nil t))
1120                                ((determined-to-be t)   (do-if t   t))
1121                                (t (funcall test-function position type
1122                                            (do-if t) (do-if nil))))))))))
1123       (do-column precedence methods ()))))
1124
1125 (defun compute-secondary-dispatch-function (generic-function net &optional
1126                                             method-alist wrappers)
1127   (function-funcall (compute-secondary-dispatch-function1 generic-function net)
1128                     method-alist wrappers))
1129
1130 (defvar *eq-case-table-limit* 15)
1131 (defvar *case-table-limit* 10)
1132
1133 (defun compute-mcase-parameters (case-list)
1134   (unless (eq t (caar (last case-list)))
1135     (error "The key for the last case arg to mcase was not T"))
1136   (let* ((eq-p (dolist (case case-list t)
1137                  (unless (or (eq (car case) t)
1138                              (symbolp (caar case)))
1139                    (return nil))))
1140          (len (1- (length case-list)))
1141          (type (cond ((= len 1)
1142                       :simple)
1143                      ((<= len
1144                           (if eq-p
1145                               *eq-case-table-limit*
1146                               *case-table-limit*))
1147                       :assoc)
1148                      (t
1149                       :hash-table))))
1150     (list eq-p type)))
1151
1152 (defmacro mlookup (key info default &optional eq-p type)
1153   (unless (or (eq eq-p t) (null eq-p))
1154     (error "Invalid eq-p argument"))
1155   (ecase type
1156     (:simple
1157      `(if (,(if eq-p 'eq 'eql) ,key (car ,info))
1158           (cdr ,info)
1159           ,default))
1160     (:assoc
1161      `(dolist (e ,info ,default)
1162         (when (,(if eq-p 'eq 'eql) (car e) ,key)
1163           (return (cdr e)))))
1164     (:hash-table
1165      `(gethash ,key ,info ,default))))
1166
1167 (defun net-test-converter (form)
1168   (if (atom form)
1169       (default-test-converter form)
1170       (case (car form)
1171         ((invoke-effective-method-function invoke-fast-method-call)
1172          '.call.)
1173         (methods
1174          '.methods.)
1175         (unordered-methods
1176          '.umethods.)
1177         (mcase
1178          `(mlookup ,(cadr form)
1179                    nil
1180                    nil
1181                    ,@(compute-mcase-parameters (cddr form))))
1182         (t (default-test-converter form)))))
1183
1184 (defun net-code-converter (form)
1185   (if (atom form)
1186       (default-code-converter form)
1187       (case (car form)
1188         ((methods unordered-methods)
1189          (let ((gensym (gensym)))
1190            (values gensym
1191                    (list gensym))))
1192         (mcase
1193          (let ((mp (compute-mcase-parameters (cddr form)))
1194                (gensym (gensym)) (default (gensym)))
1195            (values `(mlookup ,(cadr form) ,gensym ,default ,@mp)
1196                    (list gensym default))))
1197         (t
1198          (default-code-converter form)))))
1199
1200 (defun net-constant-converter (form generic-function)
1201   (or (let ((c (methods-converter form generic-function)))
1202         (when c (list c)))
1203       (if (atom form)
1204           (default-constant-converter form)
1205           (case (car form)
1206             (mcase
1207              (let* ((mp (compute-mcase-parameters (cddr form)))
1208                     (list (mapcar #'(lambda (clause)
1209                                       (let ((key (car clause))
1210                                             (meth (cadr clause)))
1211                                         (cons (if (consp key) (car key) key)
1212                                               (methods-converter
1213                                                meth generic-function))))
1214                                   (cddr form)))
1215                     (default (car (last list))))
1216                (list (list* ':mcase mp (nbutlast list))
1217                      (cdr default))))
1218             (t
1219              (default-constant-converter form))))))
1220
1221 (defun methods-converter (form generic-function)
1222   (cond ((and (consp form) (eq (car form) 'methods))
1223          (cons '.methods.
1224                (get-effective-method-function1 generic-function (cadr form))))
1225         ((and (consp form) (eq (car form) 'unordered-methods))
1226          (default-secondary-dispatch-function generic-function))))
1227
1228 (defun convert-methods (constant method-alist wrappers)
1229   (if (and (consp constant)
1230            (eq (car constant) '.methods.))
1231       (funcall (cdr constant) method-alist wrappers)
1232       constant))
1233
1234 (defun convert-table (constant method-alist wrappers)
1235   (cond ((and (consp constant)
1236               (eq (car constant) ':mcase))
1237          (let ((alist (mapcar #'(lambda (k+m)
1238                                   (cons (car k+m)
1239                                         (convert-methods (cdr k+m)
1240                                                          method-alist
1241                                                          wrappers)))
1242                               (cddr constant)))
1243                (mp (cadr constant)))
1244            (ecase (cadr mp)
1245              (:simple
1246               (car alist))
1247              (:assoc
1248               alist)
1249              (:hash-table
1250               (let ((table (make-hash-table :test (if (car mp) 'eq 'eql))))
1251                 (dolist (k+m alist)
1252                   (setf (gethash (car k+m) table) (cdr k+m)))
1253                 table)))))))
1254
1255 (defun compute-secondary-dispatch-function1 (generic-function net
1256                                              &optional function-p)
1257   (cond
1258    ((and (eq (car net) 'methods) (not function-p))
1259     (get-effective-method-function1 generic-function (cadr net)))
1260    (t
1261     (let* ((name (generic-function-name generic-function))
1262            (arg-info (gf-arg-info generic-function))
1263            (metatypes (arg-info-metatypes arg-info))
1264            (applyp (arg-info-applyp arg-info))
1265            (fmc-arg-info (cons (length metatypes) applyp))
1266            (arglist (if function-p
1267                         (make-dfun-lambda-list metatypes applyp)
1268                         (make-fast-method-call-lambda-list metatypes applyp))))
1269       (multiple-value-bind (cfunction constants)
1270           (get-function1 `(,(if function-p
1271                                       'sb-kernel:instance-lambda
1272                                       'lambda)
1273                            ,arglist
1274                                  ,@(unless function-p
1275                                      `((declare (ignore .pv-cell.
1276                                                         .next-method-call.))))
1277                                  (locally (declare #.*optimize-speed*)
1278                                    (let ((emf ,net))
1279                                      ,(make-emf-call metatypes applyp 'emf))))
1280                          #'net-test-converter
1281                          #'net-code-converter
1282                          #'(lambda (form)
1283                              (net-constant-converter form generic-function)))
1284         #'(lambda (method-alist wrappers)
1285             (let* ((alist (list nil))
1286                    (alist-tail alist))
1287               (dolist (constant constants)
1288                 (let* ((a (or (dolist (a alist nil)
1289                                 (when (eq (car a) constant)
1290                                   (return a)))
1291                               (cons constant
1292                                     (or (convert-table
1293                                          constant method-alist wrappers)
1294                                         (convert-methods
1295                                          constant method-alist wrappers)))))
1296                        (new (list a)))
1297                   (setf (cdr alist-tail) new)
1298                   (setf alist-tail new)))
1299               (let ((function (apply cfunction (mapcar #'cdr (cdr alist)))))
1300                 (if function-p
1301                     function
1302                     (make-fast-method-call
1303                      :function (set-function-name function
1304                                                   `(sdfun-method ,name))
1305                      :arg-info fmc-arg-info))))))))))
1306
1307 (defvar *show-make-unordered-methods-emf-calls* nil)
1308
1309 (defun make-unordered-methods-emf (generic-function methods)
1310   (when *show-make-unordered-methods-emf-calls*
1311     (format t "~&make-unordered-methods-emf ~S~%"
1312             (generic-function-name generic-function)))
1313   #'(lambda (&rest args)
1314       (let* ((types (types-from-arguments generic-function args 'eql))
1315              (smethods (sort-applicable-methods generic-function
1316                                                 methods
1317                                                 types))
1318              (emf (get-effective-method-function generic-function smethods)))
1319         (invoke-emf emf args))))
1320 \f
1321 ;;; The value returned by compute-discriminating-function is a function
1322 ;;; object. It is called a discriminating function because it is called
1323 ;;; when the generic function is called and its role is to discriminate
1324 ;;; on the arguments to the generic function and then call appropriate
1325 ;;; method functions.
1326 ;;;
1327 ;;; A discriminating function can only be called when it is installed as
1328 ;;; the funcallable instance function of the generic function for which
1329 ;;; it was computed.
1330 ;;;
1331 ;;; More precisely, if compute-discriminating-function is called with an
1332 ;;; argument <gf1>, and returns a result <df1>, that result must not be
1333 ;;; passed to apply or funcall directly. Rather, <df1> must be stored as
1334 ;;; the funcallable instance function of the same generic function <gf1>
1335 ;;; (using set-funcallable-instance-function). Then the generic function
1336 ;;; can be passed to funcall or apply.
1337 ;;;
1338 ;;; An important exception is that methods on this generic function are
1339 ;;; permitted to return a function which itself ends up calling the value
1340 ;;; returned by a more specific method. This kind of `encapsulation' of
1341 ;;; discriminating function is critical to many uses of the MOP.
1342 ;;;
1343 ;;; As an example, the following canonical case is legal:
1344 ;;;
1345 ;;;   (defmethod compute-discriminating-function ((gf my-generic-function))
1346 ;;;     (let ((std (call-next-method)))
1347 ;;;       #'(lambda (arg)
1348 ;;;         (print (list 'call-to-gf gf arg))
1349 ;;;         (funcall std arg))))
1350 ;;;
1351 ;;; Because many discriminating functions would like to use a dynamic
1352 ;;; strategy in which the precise discriminating function changes with
1353 ;;; time it is important to specify how a discriminating function is
1354 ;;; permitted itself to change the funcallable instance function of the
1355 ;;; generic function.
1356 ;;;
1357 ;;; Discriminating functions may set the funcallable instance function
1358 ;;; of the generic function, but the new value must be generated by making
1359 ;;; a call to COMPUTE-DISCRIMINATING-FUNCTION. This is to ensure that any
1360 ;;; more specific methods which may have encapsulated the discriminating
1361 ;;; function will get a chance to encapsulate the new, inner discriminating
1362 ;;; function.
1363 ;;;
1364 ;;; This implies that if a discriminating function wants to modify itself
1365 ;;; it should first store some information in the generic function proper,
1366 ;;; and then call compute-discriminating-function. The appropriate method
1367 ;;; on compute-discriminating-function will see the information stored in
1368 ;;; the generic function and generate a discriminating function accordingly.
1369 ;;;
1370 ;;; The following is an example of a discriminating function which modifies
1371 ;;; itself in accordance with this protocol:
1372 ;;;
1373 ;;;   (defmethod compute-discriminating-function ((gf my-generic-function))
1374 ;;;     #'(lambda (arg)
1375 ;;;      (cond (<some condition>
1376 ;;;             <store some info in the generic function>
1377 ;;;             (set-funcallable-instance-function
1378 ;;;               gf
1379 ;;;               (compute-discriminating-function gf))
1380 ;;;             (funcall gf arg))
1381 ;;;            (t
1382 ;;;             <call-a-method-of-gf>))))
1383 ;;;
1384 ;;; Whereas this code would not be legal:
1385 ;;;
1386 ;;;   (defmethod compute-discriminating-function ((gf my-generic-function))
1387 ;;;     #'(lambda (arg)
1388 ;;;      (cond (<some condition>
1389 ;;;             (set-funcallable-instance-function
1390 ;;;               gf
1391 ;;;               #'(lambda (a) ..))
1392 ;;;             (funcall gf arg))
1393 ;;;            (t
1394 ;;;             <call-a-method-of-gf>))))
1395 ;;;
1396 ;;; NOTE:  All the examples above assume that all instances of the class
1397 ;;;     my-generic-function accept only one argument.
1398
1399 (defun slot-value-using-class-dfun (class object slotd)
1400   (declare (ignore class))
1401   (function-funcall (slot-definition-reader-function slotd) object))
1402
1403 (defun setf-slot-value-using-class-dfun (new-value class object slotd)
1404   (declare (ignore class))
1405   (function-funcall (slot-definition-writer-function slotd) new-value object))
1406
1407 (defun slot-boundp-using-class-dfun (class object slotd)
1408   (declare (ignore class))
1409   (function-funcall (slot-definition-boundp-function slotd) object))
1410
1411 (defmethod compute-discriminating-function ((gf standard-generic-function))
1412   (with-slots (dfun-state arg-info) gf
1413     (typecase dfun-state
1414       (null (let ((name (generic-function-name gf)))
1415               (when (eq name 'compute-applicable-methods)
1416                 (update-all-c-a-m-gf-info gf))
1417               (cond ((eq name 'slot-value-using-class)
1418                      (update-slot-value-gf-info gf 'reader)
1419                      #'slot-value-using-class-dfun)
1420                     ((equal name '(setf slot-value-using-class))
1421                      (update-slot-value-gf-info gf 'writer)
1422                      #'setf-slot-value-using-class-dfun)
1423                     ((eq name 'slot-boundp-using-class)
1424                      (update-slot-value-gf-info gf 'boundp)
1425                      #'slot-boundp-using-class-dfun)
1426                     ((gf-precompute-dfun-and-emf-p arg-info)
1427                      (make-final-dfun gf))
1428                     (t
1429                      (make-initial-dfun gf)))))
1430       (function dfun-state)
1431       (cons (car dfun-state)))))
1432
1433 (defmethod update-gf-dfun ((class std-class) gf)
1434   (let ((*new-class* class)
1435         #|| (name (generic-function-name gf)) ||#
1436         (arg-info (gf-arg-info gf)))
1437     (cond #||
1438           ((eq name 'slot-value-using-class)
1439            (update-slot-value-gf-info gf 'reader))
1440           ((equal name '(setf slot-value-using-class))
1441            (update-slot-value-gf-info gf 'writer))
1442           ((eq name 'slot-boundp-using-class)
1443            (update-slot-value-gf-info gf 'boundp))
1444           ||#
1445           ((gf-precompute-dfun-and-emf-p arg-info)
1446            (multiple-value-bind (dfun cache info)
1447                (make-final-dfun-internal gf)
1448              (set-dfun gf dfun cache info) ; lest the cache be freed twice
1449              (update-dfun gf dfun cache info))))))
1450 \f
1451 (defmethod function-keywords ((method standard-method))
1452   (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1453       (analyze-lambda-list (if (consp method)
1454                                (early-method-lambda-list method)
1455                                (method-lambda-list method)))
1456     (declare (ignore nreq nopt keysp restp))
1457     (values keywords allow-other-keys-p)))
1458
1459 (defun method-ll->generic-function-ll (ll)
1460   (multiple-value-bind
1461       (nreq nopt keysp restp allow-other-keys-p keywords keyword-parameters)
1462       (analyze-lambda-list ll)
1463     (declare (ignore nreq nopt keysp restp allow-other-keys-p keywords))
1464     (remove-if #'(lambda (s)
1465                    (or (memq s keyword-parameters)
1466                        (eq s '&allow-other-keys)))
1467                ll)))
1468 \f
1469 ;;; This is based on the rules of method lambda list congruency defined in
1470 ;;; the spec. The lambda list it constructs is the pretty union of the
1471 ;;; lambda lists of all the methods. It doesn't take method applicability
1472 ;;; into account at all yet.
1473 (defmethod generic-function-pretty-arglist
1474            ((generic-function standard-generic-function))
1475   (let ((methods (generic-function-methods generic-function))
1476         (arglist ()))
1477     (when methods
1478       (multiple-value-bind (required optional rest key allow-other-keys)
1479           (method-pretty-arglist (car methods))
1480         (dolist (m (cdr methods))
1481           (multiple-value-bind (method-key-keywords
1482                                 method-allow-other-keys
1483                                 method-key)
1484               (function-keywords m)
1485             ;; we've modified function-keywords to return what we want as
1486             ;;  the third value, no other change here.
1487             (declare (ignore method-key-keywords))
1488             (setq key (union key method-key))
1489             (setq allow-other-keys (or allow-other-keys
1490                                        method-allow-other-keys))))
1491         (when allow-other-keys
1492           (setq arglist '(&allow-other-keys)))
1493         (when key
1494           (setq arglist (nconc (list '&key) key arglist)))
1495         (when rest
1496           (setq arglist (nconc (list '&rest rest) arglist)))
1497         (when optional
1498           (setq arglist (nconc (list '&optional) optional arglist)))
1499         (nconc required arglist)))))
1500
1501 (defmethod method-pretty-arglist ((method standard-method))
1502   (let ((required ())
1503         (optional ())
1504         (rest nil)
1505         (key ())
1506         (allow-other-keys nil)
1507         (state 'required)
1508         (arglist (method-lambda-list method)))
1509     (dolist (arg arglist)
1510       (cond ((eq arg '&optional)         (setq state 'optional))
1511             ((eq arg '&rest)             (setq state 'rest))
1512             ((eq arg '&key)              (setq state 'key))
1513             ((eq arg '&allow-other-keys) (setq allow-other-keys t))
1514             ((memq arg lambda-list-keywords))
1515             (t
1516              (ecase state
1517                (required (push arg required))
1518                (optional (push arg optional))
1519                (key      (push arg key))
1520                (rest     (setq rest arg))))))
1521     (values (nreverse required)
1522             (nreverse optional)
1523             rest
1524             (nreverse key)
1525             allow-other-keys)))
1526