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