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