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