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