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