0.pre7.139:
[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-fun (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-fun t)
146         (lose :function function check-fun))
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-fun-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     (let ((pos 0))
384       (dolist (type-spec (method-specializers method))
385         (unless (eq type-spec *the-class-t*)
386           (pushnew pos specialized-argument-positions))
387         (incf pos)))
388     ;; Finally merge the values for this method into the values
389     ;; for the exisiting methods and return them. Note that if
390     ;; num-of-requireds is NIL it means this is the first method
391     ;; and we depend on that.
392     (values (min (or number-of-requireds requireds) requireds)
393             (or restp
394                 (and number-of-requireds (/= number-of-requireds requireds)))
395             specialized-argument-positions)))
396
397 (defun make-discriminating-function-arglist (number-required-arguments restp)
398   (nconc (let ((args nil))
399            (dotimes (i number-required-arguments)
400              (push (intern (format nil "Discriminating Function Arg ~D" i))
401                    args))
402            (nreverse args))
403          (when restp
404                `(&rest ,(intern "Discriminating Function &rest Arg")))))
405 \f
406 (defmethod generic-function-lambda-list ((gf generic-function))
407   (gf-lambda-list gf))
408
409 (defmethod gf-fast-method-function-p ((gf standard-generic-function))
410   (gf-info-fast-mf-p (slot-value gf 'arg-info)))
411
412 (defmethod initialize-instance :after ((gf standard-generic-function)
413                                        &key (lambda-list nil lambda-list-p)
414                                        argument-precedence-order)
415   (with-slots (arg-info)
416     gf
417     (if lambda-list-p
418         (set-arg-info gf
419                       :lambda-list lambda-list
420                       :argument-precedence-order argument-precedence-order)
421         (set-arg-info gf))
422     (when (arg-info-valid-p arg-info)
423       (update-dfun gf))))
424
425 (defmethod reinitialize-instance :after ((gf standard-generic-function)
426                                          &rest args
427                                          &key (lambda-list nil lambda-list-p)
428                                          (argument-precedence-order
429                                           nil argument-precedence-order-p))
430   (with-slots (arg-info)
431     gf
432     (if lambda-list-p
433         (if argument-precedence-order-p
434             (set-arg-info gf
435                           :lambda-list lambda-list
436                           :argument-precedence-order argument-precedence-order)
437             (set-arg-info gf
438                           :lambda-list lambda-list))
439         (set-arg-info gf))
440     (when (and (arg-info-valid-p arg-info)
441                args
442                (or lambda-list-p (cddr args)))
443       (update-dfun gf))))
444
445 (declaim (special *lazy-dfun-compute-p*))
446
447 (defun set-methods (gf methods)
448   (setf (generic-function-methods gf) nil)
449   (loop (when (null methods) (return gf))
450         (real-add-method gf (pop methods) methods)))
451
452 (defun real-add-method (generic-function method &optional skip-dfun-update-p)
453   (if (method-generic-function method)
454       (error "The method ~S is already part of the generic~@
455               function ~S. It can't be added to another generic~@
456               function until it is removed from the first one."
457              method (method-generic-function method))
458
459       (let* ((name (generic-function-name generic-function))
460              (qualifiers (method-qualifiers method))
461              (specializers (method-specializers method))
462              (existing (get-method generic-function
463                                    qualifiers
464                                    specializers
465                                    nil)))
466
467         ;; If there is already a method like this one then we must
468         ;; get rid of it before proceeding. Note that we call the
469         ;; generic function remove-method to remove it rather than
470         ;; doing it in some internal way.
471         (when existing (remove-method generic-function existing))
472
473         (setf (method-generic-function method) generic-function)
474         (pushnew method (generic-function-methods generic-function))
475         (dolist (specializer specializers)
476           (add-direct-method specializer method))
477         (set-arg-info generic-function :new-method method)
478         (unless skip-dfun-update-p
479           (when (member name
480                         '(make-instance default-initargs
481                           allocate-instance shared-initialize
482                           initialize-instance))
483             (update-make-instance-function-table (type-class
484                                                   (car specializers))))
485           (update-dfun generic-function))
486         method)))
487
488 (defun real-remove-method (generic-function method)
489   ;; Note: Error check prohibited by ANSI spec removed.
490   (when  (eq generic-function (method-generic-function method))
491     (let* ((name         (generic-function-name generic-function))
492            (specializers (method-specializers method))
493            (methods      (generic-function-methods generic-function))
494            (new-methods  (remove method methods)))
495       (setf (method-generic-function method) nil)
496       (setf (generic-function-methods generic-function) new-methods)
497       (dolist (specializer (method-specializers method))
498         (remove-direct-method specializer method))
499       (set-arg-info generic-function)
500       (when (member name
501                     '(make-instance
502                       default-initargs
503                       allocate-instance shared-initialize initialize-instance))
504         (update-make-instance-function-table (type-class (car specializers))))
505       (update-dfun generic-function)
506       generic-function)))
507 \f
508 (defun compute-applicable-methods-function (generic-function arguments)
509   (values (compute-applicable-methods-using-types
510            generic-function
511            (types-from-args generic-function arguments 'eql))))
512
513 (defmethod compute-applicable-methods
514     ((generic-function generic-function) arguments)
515   (values (compute-applicable-methods-using-types
516            generic-function
517            (types-from-args generic-function arguments 'eql))))
518
519 (defmethod compute-applicable-methods-using-classes
520     ((generic-function generic-function) classes)
521   (compute-applicable-methods-using-types
522    generic-function
523    (types-from-args generic-function classes 'class-eq)))
524
525 (defun proclaim-incompatible-superclasses (classes)
526   (setq classes (mapcar (lambda (class)
527                           (if (symbolp class)
528                               (find-class class)
529                               class))
530                         classes))
531   (dolist (class classes)
532     (dolist (other-class classes)
533       (unless (eq class other-class)
534         (pushnew other-class (class-incompatible-superclass-list class))))))
535
536 (defun superclasses-compatible-p (class1 class2)
537   (let ((cpl1 (class-precedence-list class1))
538         (cpl2 (class-precedence-list class2)))
539     (dolist (sc1 cpl1 t)
540       (dolist (ic (class-incompatible-superclass-list sc1))
541         (when (memq ic cpl2)
542           (return-from superclasses-compatible-p nil))))))
543
544 (mapc
545  #'proclaim-incompatible-superclasses
546  '(;; superclass class
547    (built-in-class std-class structure-class) ; direct subclasses of pcl-class
548    (standard-class funcallable-standard-class)
549    ;; superclass metaobject
550    (class eql-specializer class-eq-specializer method method-combination
551     generic-function slot-definition)
552    ;; metaclass built-in-class
553    (number sequence character           ; direct subclasses of t, but not array
554     standard-object structure-object)   ;                        or symbol
555    (number array character symbol       ; direct subclasses of t, but not
556     standard-object structure-object)   ;                        sequence
557    (complex float rational)             ; direct subclasses of number
558    (integer ratio)                      ; direct subclasses of rational
559    (list vector)                        ; direct subclasses of sequence
560    (cons null)                          ; direct subclasses of list
561    (string bit-vector)                  ; direct subclasses of vector
562    ))
563 \f
564 (defmethod same-specializer-p ((specl1 specializer) (specl2 specializer))
565   nil)
566
567 (defmethod same-specializer-p ((specl1 class) (specl2 class))
568   (eq specl1 specl2))
569
570 (defmethod specializer-class ((specializer class))
571   specializer)
572
573 (defmethod same-specializer-p ((specl1 class-eq-specializer)
574                                (specl2 class-eq-specializer))
575   (eq (specializer-class specl1) (specializer-class specl2)))
576
577 (defmethod same-specializer-p ((specl1 eql-specializer)
578                                (specl2 eql-specializer))
579   (eq (specializer-object specl1) (specializer-object specl2)))
580
581 (defmethod specializer-class ((specializer eql-specializer))
582   (class-of (slot-value specializer 'object)))
583
584 (defvar *in-gf-arg-info-p* nil)
585 (setf (gdefinition 'arg-info-reader)
586       (let ((mf (initialize-method-function
587                  (make-internal-reader-method-function
588                   'standard-generic-function 'arg-info)
589                  t)))
590         (lambda (&rest args) (funcall mf args nil))))
591
592
593 (defun error-need-at-least-n-args (function n)
594   (error "~@<The function ~2I~_~S ~I~_requires at least ~W argument~:P.~:>"
595          function
596          n))
597
598 (defun types-from-args (generic-function arguments &optional type-modifier)
599   (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
600       (get-generic-fun-info generic-function)
601     (declare (ignore applyp metatypes nkeys))
602     (let ((types-rev nil))
603       (dotimes-fixnum (i nreq)
604         i
605         (unless arguments
606           (error-need-at-least-n-args (generic-function-name generic-function)
607                                       nreq))
608         (let ((arg (pop arguments)))
609           (push (if type-modifier `(,type-modifier ,arg) arg) types-rev)))
610       (values (nreverse types-rev) arg-info))))
611
612 (defun get-wrappers-from-classes (nkeys wrappers classes metatypes)
613   (let* ((w wrappers) (w-tail w) (mt-tail metatypes))
614     (dolist (class (if (listp classes) classes (list classes)))
615       (unless (eq t (car mt-tail))
616         (let ((c-w (class-wrapper class)))
617           (unless c-w (return-from get-wrappers-from-classes nil))
618           (if (eql nkeys 1)
619               (setq w c-w)
620               (setf (car w-tail) c-w
621                     w-tail (cdr w-tail)))))
622       (setq mt-tail (cdr mt-tail)))
623     w))
624
625 (defun sdfun-for-caching (gf classes)
626   (let ((types (mapcar #'class-eq-type classes)))
627     (multiple-value-bind (methods all-applicable-and-sorted-p)
628         (compute-applicable-methods-using-types gf types)
629       (function-funcall (get-secondary-dispatch-function1
630                          gf methods types nil t all-applicable-and-sorted-p)
631                         nil (mapcar #'class-wrapper classes)))))
632
633 (defun value-for-caching (gf classes)
634   (let ((methods (compute-applicable-methods-using-types
635                    gf (mapcar #'class-eq-type classes))))
636     (method-function-get (or (method-fast-function (car methods))
637                              (method-function (car methods)))
638                          :constant-value)))
639
640 (defun default-secondary-dispatch-function (generic-function)
641   (lambda (&rest args)
642     (let ((methods (compute-applicable-methods generic-function args)))
643       (if methods
644           (let ((emf (get-effective-method-function generic-function
645                                                     methods)))
646             (invoke-emf emf args))
647           (apply #'no-applicable-method generic-function args)))))
648
649 (defun list-eq (x y)
650   (loop (when (atom x) (return (eq x y)))
651         (when (atom y) (return nil))
652         (unless (eq (car x) (car y)) (return nil))
653         (setq x (cdr x)  y (cdr y))))
654
655 (defvar *std-cam-methods* nil)
656
657 (defun compute-applicable-methods-emf (generic-function)
658   (if (eq *boot-state* 'complete)
659       (let* ((cam (gdefinition 'compute-applicable-methods))
660              (cam-methods (compute-applicable-methods-using-types
661                            cam (list `(eql ,generic-function) t))))
662         (values (get-effective-method-function cam cam-methods)
663                 (list-eq cam-methods
664                          (or *std-cam-methods*
665                              (setq *std-cam-methods*
666                                    (compute-applicable-methods-using-types
667                                     cam (list `(eql ,cam) t)))))))
668       (values #'compute-applicable-methods-function t)))
669
670 (defun compute-applicable-methods-emf-std-p (gf)
671   (gf-info-c-a-m-emf-std-p (gf-arg-info gf)))
672
673 (defvar *old-c-a-m-gf-methods* nil)
674
675 (defun update-all-c-a-m-gf-info (c-a-m-gf)
676   (let ((methods (generic-function-methods c-a-m-gf)))
677     (if (and *old-c-a-m-gf-methods*
678              (every (lambda (old-method)
679                       (member old-method methods))
680                     *old-c-a-m-gf-methods*))
681         (let ((gfs-to-do nil)
682               (gf-classes-to-do nil))
683           (dolist (method methods)
684             (unless (member method *old-c-a-m-gf-methods*)
685               (let ((specl (car (method-specializers method))))
686                 (if (eql-specializer-p specl)
687                     (pushnew (specializer-object specl) gfs-to-do)
688                     (pushnew (specializer-class specl) gf-classes-to-do)))))
689           (map-all-generic-functions
690            (lambda (gf)
691              (when (or (member gf gfs-to-do)
692                        (dolist (class gf-classes-to-do nil)
693                          (member class
694                                  (class-precedence-list (class-of gf)))))
695                (update-c-a-m-gf-info gf)))))
696         (map-all-generic-functions #'update-c-a-m-gf-info))
697     (setq *old-c-a-m-gf-methods* methods)))
698
699 (defun update-gf-info (gf)
700   (update-c-a-m-gf-info gf)
701   (update-gf-simple-accessor-type gf))
702
703 (defun update-c-a-m-gf-info (gf)
704   (unless (early-gf-p gf)
705     (multiple-value-bind (c-a-m-emf std-p)
706         (compute-applicable-methods-emf gf)
707       (let ((arg-info (gf-arg-info gf)))
708         (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
709         (setf (gf-info-c-a-m-emf-std-p arg-info) std-p)))))
710
711 (defun update-gf-simple-accessor-type (gf)
712   (let ((arg-info (gf-arg-info gf)))
713     (setf (gf-info-simple-accessor-type arg-info)
714           (let* ((methods (generic-function-methods gf))
715                  (class (and methods (class-of (car methods))))
716                  (type (and class
717                             (cond ((eq class
718                                        *the-class-standard-reader-method*)
719                                    'reader)
720                                   ((eq class
721                                        *the-class-standard-writer-method*)
722                                    'writer)
723                                   ((eq class
724                                        *the-class-standard-boundp-method*)
725                                    'boundp)))))
726             (when (and (gf-info-c-a-m-emf-std-p arg-info)
727                        type
728                        (dolist (method (cdr methods) t)
729                          (unless (eq class (class-of method)) (return nil)))
730                        (eq (generic-function-method-combination gf)
731                            *standard-method-combination*))
732               type)))))
733
734 (defun get-accessor-method-function (gf type class slotd)
735   (let* ((std-method (standard-svuc-method type))
736          (str-method (structure-svuc-method type))
737          (types1 `((eql ,class) (class-eq ,class) (eql ,slotd)))
738          (types (if (eq type 'writer) `(t ,@types1) types1))
739          (methods (compute-applicable-methods-using-types gf types))
740          (std-p (null (cdr methods))))
741     (values
742      (if std-p
743          (get-optimized-std-accessor-method-function class slotd type)
744          (get-accessor-from-svuc-method-function
745           class slotd
746           (get-secondary-dispatch-function
747            gf methods types
748            `((,(car (or (member std-method methods)
749                         (member str-method methods)
750                         (error "error in get-accessor-method-function")))
751               ,(get-optimized-std-slot-value-using-class-method-function
752                 class slotd type)))
753            (unless (and (eq type 'writer)
754                         (dolist (method methods t)
755                           (unless (eq (car (method-specializers method))
756                                       *the-class-t*)
757                             (return nil))))
758              (let ((wrappers (list (wrapper-of class)
759                                    (class-wrapper class)
760                                    (wrapper-of slotd))))
761                (if (eq type 'writer)
762                    (cons (class-wrapper *the-class-t*) wrappers)
763                    wrappers))))
764           type))
765      std-p)))
766
767 ;;; used by OPTIMIZE-SLOT-VALUE-BY-CLASS-P (vector.lisp)
768 (defun update-slot-value-gf-info (gf type)
769   (unless *new-class*
770     (update-std-or-str-methods gf type))
771   (when (and (standard-svuc-method type) (structure-svuc-method type))
772     (flet ((update-class (class)
773              (when (class-finalized-p class)
774                (dolist (slotd (class-slots class))
775                  (compute-slot-accessor-info slotd type gf)))))
776       (if *new-class*
777           (update-class *new-class*)
778           (map-all-classes #'update-class 'slot-object)))))
779
780 (defvar *standard-slot-value-using-class-method* nil)
781 (defvar *standard-setf-slot-value-using-class-method* nil)
782 (defvar *standard-slot-boundp-using-class-method* nil)
783 (defvar *structure-slot-value-using-class-method* nil)
784 (defvar *structure-setf-slot-value-using-class-method* nil)
785 (defvar *structure-slot-boundp-using-class-method* nil)
786
787 (defun standard-svuc-method (type)
788   (case type
789     (reader *standard-slot-value-using-class-method*)
790     (writer *standard-setf-slot-value-using-class-method*)
791     (boundp *standard-slot-boundp-using-class-method*)))
792
793 (defun set-standard-svuc-method (type method)
794   (case type
795     (reader (setq *standard-slot-value-using-class-method* method))
796     (writer (setq *standard-setf-slot-value-using-class-method* method))
797     (boundp (setq *standard-slot-boundp-using-class-method* method))))
798
799 (defun structure-svuc-method (type)
800   (case type
801     (reader *structure-slot-value-using-class-method*)
802     (writer *structure-setf-slot-value-using-class-method*)
803     (boundp *structure-slot-boundp-using-class-method*)))
804
805 (defun set-structure-svuc-method (type method)
806   (case type
807     (reader (setq *structure-slot-value-using-class-method* method))
808     (writer (setq *structure-setf-slot-value-using-class-method* method))
809     (boundp (setq *structure-slot-boundp-using-class-method* method))))
810
811 (defun update-std-or-str-methods (gf type)
812   (dolist (method (generic-function-methods gf))
813     (let ((specls (method-specializers method)))
814       (when (and (or (not (eq type 'writer))
815                      (eq (pop specls) *the-class-t*))
816                  (every #'classp specls))
817         (cond ((and (eq (class-name (car specls))
818                         'std-class)
819                     (eq (class-name (cadr specls))
820                         'std-object)
821                     (eq (class-name (caddr specls))
822                         'standard-effective-slot-definition))
823                (set-standard-svuc-method type method))
824               ((and (eq (class-name (car specls))
825                         'structure-class)
826                     (eq (class-name (cadr specls))
827                         'structure-object)
828                     (eq (class-name (caddr specls))
829                         'structure-effective-slot-definition))
830                (set-structure-svuc-method type method)))))))
831
832 (defun mec-all-classes-internal (spec precompute-p)
833   (cons (specializer-class spec)
834         (and (classp spec)
835              precompute-p
836              (not (or (eq spec *the-class-t*)
837                       (eq spec *the-class-slot-object*)
838                       (eq spec *the-class-std-object*)
839                       (eq spec *the-class-standard-object*)
840                       (eq spec *the-class-structure-object*)))
841              (let ((sc (class-direct-subclasses spec)))
842                (when sc
843                  (mapcan (lambda (class)
844                            (mec-all-classes-internal class precompute-p))
845                          sc))))))
846
847 (defun mec-all-classes (spec precompute-p)
848   (let ((classes (mec-all-classes-internal spec precompute-p)))
849     (if (null (cdr classes))
850         classes
851         (let* ((a-classes (cons nil classes))
852                (tail classes))
853           (loop (when (null (cdr tail))
854                   (return (cdr a-classes)))
855                 (let ((class (cadr tail))
856                       (ttail (cddr tail)))
857                   (if (dolist (c ttail nil)
858                         (when (eq class c) (return t)))
859                       (setf (cdr tail) (cddr tail))
860                       (setf tail (cdr tail)))))))))
861
862 (defun mec-all-class-lists (spec-list precompute-p)
863   (if (null spec-list)
864       (list nil)
865       (let* ((car-all-classes (mec-all-classes (car spec-list)
866                                                precompute-p))
867              (all-class-lists (mec-all-class-lists (cdr spec-list)
868                                                    precompute-p)))
869         (mapcan (lambda (list)
870                   (mapcar (lambda (c) (cons c list)) car-all-classes))
871                 all-class-lists))))
872
873 (defun make-emf-cache (generic-function valuep cache classes-list new-class)
874   (let* ((arg-info (gf-arg-info generic-function))
875          (nkeys (arg-info-nkeys arg-info))
876          (metatypes (arg-info-metatypes arg-info))
877          (wrappers (unless (eq nkeys 1) (make-list nkeys)))
878          (precompute-p (gf-precompute-dfun-and-emf-p arg-info))
879          (default '(default)))
880     (flet ((add-class-list (classes)
881              (when (or (null new-class) (memq new-class classes))
882                (let ((wrappers (get-wrappers-from-classes
883                                 nkeys wrappers classes metatypes)))
884                  (when (and wrappers
885                             (eq default (probe-cache cache wrappers default)))
886                    (let ((value (cond ((eq valuep t)
887                                        (sdfun-for-caching generic-function
888                                                           classes))
889                                       ((eq valuep :constant-value)
890                                        (value-for-caching generic-function
891                                                           classes)))))
892                      (setq cache (fill-cache cache wrappers value t))))))))
893       (if classes-list
894           (mapc #'add-class-list classes-list)
895           (dolist (method (generic-function-methods generic-function))
896             (mapc #'add-class-list
897                   (mec-all-class-lists (method-specializers method)
898                                        precompute-p))))
899       cache)))
900
901 (defmacro class-test (arg class)
902   (cond ((eq class *the-class-t*)
903          t)
904         ((eq class *the-class-slot-object*)
905          `(not (cl:typep (cl:class-of ,arg) 'cl:built-in-class)))
906         ((eq class *the-class-std-object*)
907          `(or (std-instance-p ,arg) (fsc-instance-p ,arg)))
908         ((eq class *the-class-standard-object*)
909          `(std-instance-p ,arg))
910         ((eq class *the-class-funcallable-standard-object*)
911          `(fsc-instance-p ,arg))
912         (t
913          `(typep ,arg ',(class-name class)))))
914
915 (defmacro class-eq-test (arg class)
916   `(eq (class-of ,arg) ',class))
917
918 (defmacro eql-test (arg object)
919   `(eql ,arg ',object))
920
921 (defun dnet-methods-p (form)
922   (and (consp form)
923        (or (eq (car form) 'methods)
924            (eq (car form) 'unordered-methods))))
925
926 ;;; This is CASE, but without gensyms.
927 (defmacro scase (arg &rest clauses)
928   `(let ((.case-arg. ,arg))
929      (cond ,@(mapcar (lambda (clause)
930                        (list* (cond ((null (car clause))
931                                      nil)
932                                     ((consp (car clause))
933                                      (if (null (cdar clause))
934                                          `(eql .case-arg.
935                                                ',(caar clause))
936                                          `(member .case-arg.
937                                                   ',(car clause))))
938                                     ((member (car clause) '(t otherwise))
939                                      `t)
940                                     (t
941                                      `(eql .case-arg. ',(car clause))))
942                               nil
943                               (cdr clause)))
944                      clauses))))
945
946 (defmacro mcase (arg &rest clauses) `(scase ,arg ,@clauses))
947
948 (defun generate-discrimination-net (generic-function methods types sorted-p)
949   (let* ((arg-info (gf-arg-info generic-function))
950          (precedence (arg-info-precedence arg-info)))
951     (generate-discrimination-net-internal
952      generic-function methods types
953      (lambda (methods known-types)
954        (if (or sorted-p
955                (block one-order-p
956                  (let ((sorted-methods nil))
957                    (map-all-orders
958                     (copy-list methods) precedence
959                     (lambda (methods)
960                       (when sorted-methods (return-from one-order-p nil))
961                       (setq sorted-methods methods)))
962                    (setq methods sorted-methods))
963                  t))
964            `(methods ,methods ,known-types)
965            `(unordered-methods ,methods ,known-types)))
966      (lambda (position type true-value false-value)
967        (let ((arg (dfun-arg-symbol position)))
968          (if (eq (car type) 'eql)
969              (let* ((false-case-p (and (consp false-value)
970                                        (or (eq (car false-value) 'scase)
971                                            (eq (car false-value) 'mcase))
972                                        (eq arg (cadr false-value))))
973                     (false-clauses (if false-case-p
974                                        (cddr false-value)
975                                        `((t ,false-value))))
976                     (case-sym (if (and (dnet-methods-p true-value)
977                                        (if false-case-p
978                                            (eq (car false-value) 'mcase)
979                                            (dnet-methods-p false-value)))
980                                   'mcase
981                                   'scase))
982                     (type-sym `(,(cadr type))))
983                `(,case-sym ,arg
984                            (,type-sym ,true-value)
985                            ,@false-clauses))
986              `(if ,(let ((arg (dfun-arg-symbol position)))
987                      (case (car type)
988                        (class    `(class-test    ,arg ,(cadr type)))
989                        (class-eq `(class-eq-test ,arg ,(cadr type)))))
990                   ,true-value
991                   ,false-value))))
992      #'identity)))
993
994 (defun class-from-type (type)
995   (if (or (atom type) (eq (car type) t))
996       *the-class-t*
997       (case (car type)
998         (and (dolist (type (cdr type) *the-class-t*)
999                (when (and (consp type) (not (eq (car type) 'not)))
1000                  (return (class-from-type type)))))
1001         (not *the-class-t*)
1002         (eql (class-of (cadr type)))
1003         (class-eq (cadr type))
1004         (class (cadr type)))))
1005
1006 (defun precompute-effective-methods (gf caching-p &optional classes-list-p)
1007   (let* ((arg-info (gf-arg-info gf))
1008          (methods (generic-function-methods gf))
1009          (precedence (arg-info-precedence arg-info))
1010          (*in-precompute-effective-methods-p* t)
1011          (classes-list nil))
1012     (generate-discrimination-net-internal
1013      gf methods nil
1014      (lambda (methods known-types)
1015        (when methods
1016          (when classes-list-p
1017            (push (mapcar #'class-from-type known-types) classes-list))
1018          (let ((no-eql-specls-p (not (methods-contain-eql-specializer-p
1019                                       methods))))
1020            (map-all-orders
1021             methods precedence
1022             (lambda (methods)
1023               (get-secondary-dispatch-function1
1024                gf methods known-types
1025                nil caching-p no-eql-specls-p))))))
1026      (lambda (position type true-value false-value)
1027        (declare (ignore position type true-value false-value))
1028        nil)
1029      (lambda (type)
1030        (if (and (consp type) (eq (car type) 'eql))
1031            `(class-eq ,(class-of (cadr type)))
1032            type)))
1033     classes-list))
1034
1035 ;;; We know that known-type implies neither new-type nor `(not ,new-type).
1036 (defun augment-type (new-type known-type)
1037   (if (or (eq known-type t)
1038           (eq (car new-type) 'eql))
1039       new-type
1040       (let ((so-far (if (and (consp known-type) (eq (car known-type) 'and))
1041                         (cdr known-type)
1042                         (list known-type))))
1043         (unless (eq (car new-type) 'not)
1044           (setq so-far
1045                 (mapcan (lambda (type)
1046                           (unless (*subtypep new-type type)
1047                             (list type)))
1048                         so-far)))
1049         (if (null so-far)
1050             new-type
1051             `(and ,new-type ,@so-far)))))
1052
1053 (defun generate-discrimination-net-internal
1054     (gf methods types methods-function test-fun type-function)
1055   (let* ((arg-info (gf-arg-info gf))
1056          (precedence (arg-info-precedence arg-info))
1057          (nreq (arg-info-number-required arg-info))
1058          (metatypes (arg-info-metatypes arg-info)))
1059     (labels ((do-column (p-tail contenders known-types)
1060                (if p-tail
1061                    (let* ((position (car p-tail))
1062                           (known-type (or (nth position types) t)))
1063                      (if (eq (nth position metatypes) t)
1064                          (do-column (cdr p-tail) contenders
1065                                     (cons (cons position known-type)
1066                                           known-types))
1067                          (do-methods p-tail contenders
1068                                      known-type () known-types)))
1069                    (funcall methods-function contenders
1070                             (let ((k-t (make-list nreq)))
1071                               (dolist (index+type known-types)
1072                                 (setf (nth (car index+type) k-t)
1073                                       (cdr index+type)))
1074                               k-t))))
1075              (do-methods (p-tail contenders known-type winners known-types)
1076                ;; CONTENDERS
1077                ;;   is a (sorted) list of methods that must be discriminated.
1078                ;; KNOWN-TYPE
1079                ;;   is the type of this argument, constructed from tests
1080                ;;   already made.
1081                ;; WINNERS
1082                ;;   is a (sorted) list of methods that are potentially
1083                ;;   applicable after the discrimination has been made.
1084                (if (null contenders)
1085                    (do-column (cdr p-tail)
1086                               winners
1087                               (cons (cons (car p-tail) known-type)
1088                                     known-types))
1089                    (let* ((position (car p-tail))
1090                           (method (car contenders))
1091                           (specl (nth position (method-specializers method)))
1092                           (type (funcall type-function
1093                                          (type-from-specializer specl))))
1094                      (multiple-value-bind (app-p maybe-app-p)
1095                          (specializer-applicable-using-type-p type known-type)
1096                        (flet ((determined-to-be (truth-value)
1097                                 (if truth-value app-p (not maybe-app-p)))
1098                               (do-if (truth &optional implied)
1099                                 (let ((ntype (if truth type `(not ,type))))
1100                                   (do-methods p-tail
1101                                     (cdr contenders)
1102                                     (if implied
1103                                         known-type
1104                                         (augment-type ntype known-type))
1105                                     (if truth
1106                                         (append winners `(,method))
1107                                         winners)
1108                                     known-types))))
1109                          (cond ((determined-to-be nil) (do-if nil t))
1110                                ((determined-to-be t)   (do-if t   t))
1111                                (t (funcall test-fun position type
1112                                            (do-if t) (do-if nil))))))))))
1113       (do-column precedence methods ()))))
1114
1115 (defun compute-secondary-dispatch-function (generic-function net &optional
1116                                             method-alist wrappers)
1117   (function-funcall (compute-secondary-dispatch-function1 generic-function net)
1118                     method-alist wrappers))
1119
1120 (defvar *eq-case-table-limit* 15)
1121 (defvar *case-table-limit* 10)
1122
1123 (defun compute-mcase-parameters (case-list)
1124   (unless (eq t (caar (last case-list)))
1125     (error "The key for the last case arg to mcase was not T"))
1126   (let* ((eq-p (dolist (case case-list t)
1127                  (unless (or (eq (car case) t)
1128                              (symbolp (caar case)))
1129                    (return nil))))
1130          (len (1- (length case-list)))
1131          (type (cond ((= len 1)
1132                       :simple)
1133                      ((<= len
1134                           (if eq-p
1135                               *eq-case-table-limit*
1136                               *case-table-limit*))
1137                       :assoc)
1138                      (t
1139                       :hash-table))))
1140     (list eq-p type)))
1141
1142 (defmacro mlookup (key info default &optional eq-p type)
1143   (unless (or (eq eq-p t) (null eq-p))
1144     (error "Invalid eq-p argument"))
1145   (ecase type
1146     (:simple
1147      `(if (,(if eq-p 'eq 'eql) ,key (car ,info))
1148           (cdr ,info)
1149           ,default))
1150     (:assoc
1151      `(dolist (e ,info ,default)
1152         (when (,(if eq-p 'eq 'eql) (car e) ,key)
1153           (return (cdr e)))))
1154     (:hash-table
1155      `(gethash ,key ,info ,default))))
1156
1157 (defun net-test-converter (form)
1158   (if (atom form)
1159       (default-test-converter form)
1160       (case (car form)
1161         ((invoke-effective-method-function invoke-fast-method-call)
1162          '.call.)
1163         (methods
1164          '.methods.)
1165         (unordered-methods
1166          '.umethods.)
1167         (mcase
1168          `(mlookup ,(cadr form)
1169                    nil
1170                    nil
1171                    ,@(compute-mcase-parameters (cddr form))))
1172         (t (default-test-converter form)))))
1173
1174 (defun net-code-converter (form)
1175   (if (atom form)
1176       (default-code-converter form)
1177       (case (car form)
1178         ((methods unordered-methods)
1179          (let ((gensym (gensym)))
1180            (values gensym
1181                    (list gensym))))
1182         (mcase
1183          (let ((mp (compute-mcase-parameters (cddr form)))
1184                (gensym (gensym)) (default (gensym)))
1185            (values `(mlookup ,(cadr form) ,gensym ,default ,@mp)
1186                    (list gensym default))))
1187         (t
1188          (default-code-converter form)))))
1189
1190 (defun net-constant-converter (form generic-function)
1191   (or (let ((c (methods-converter form generic-function)))
1192         (when c (list c)))
1193       (if (atom form)
1194           (default-constant-converter form)
1195           (case (car form)
1196             (mcase
1197              (let* ((mp (compute-mcase-parameters (cddr form)))
1198                     (list (mapcar (lambda (clause)
1199                                     (let ((key (car clause))
1200                                           (meth (cadr clause)))
1201                                       (cons (if (consp key) (car key) key)
1202                                             (methods-converter
1203                                              meth generic-function))))
1204                                   (cddr form)))
1205                     (default (car (last list))))
1206                (list (list* :mcase mp (nbutlast list))
1207                      (cdr default))))
1208             (t
1209              (default-constant-converter form))))))
1210
1211 (defun methods-converter (form generic-function)
1212   (cond ((and (consp form) (eq (car form) 'methods))
1213          (cons '.methods.
1214                (get-effective-method-function1 generic-function (cadr form))))
1215         ((and (consp form) (eq (car form) 'unordered-methods))
1216          (default-secondary-dispatch-function generic-function))))
1217
1218 (defun convert-methods (constant method-alist wrappers)
1219   (if (and (consp constant)
1220            (eq (car constant) '.methods.))
1221       (funcall (cdr constant) method-alist wrappers)
1222       constant))
1223
1224 (defun convert-table (constant method-alist wrappers)
1225   (cond ((and (consp constant)
1226               (eq (car constant) :mcase))
1227          (let ((alist (mapcar (lambda (k+m)
1228                                 (cons (car k+m)
1229                                       (convert-methods (cdr k+m)
1230                                                        method-alist
1231                                                        wrappers)))
1232                               (cddr constant)))
1233                (mp (cadr constant)))
1234            (ecase (cadr mp)
1235              (:simple
1236               (car alist))
1237              (:assoc
1238               alist)
1239              (:hash-table
1240               (let ((table (make-hash-table :test (if (car mp) 'eq 'eql))))
1241                 (dolist (k+m alist)
1242                   (setf (gethash (car k+m) table) (cdr k+m)))
1243                 table)))))))
1244
1245 (defun compute-secondary-dispatch-function1 (generic-function net
1246                                              &optional function-p)
1247   (cond
1248    ((and (eq (car net) 'methods) (not function-p))
1249     (get-effective-method-function1 generic-function (cadr net)))
1250    (t
1251     (let* ((name (generic-function-name generic-function))
1252            (arg-info (gf-arg-info generic-function))
1253            (metatypes (arg-info-metatypes arg-info))
1254            (applyp (arg-info-applyp arg-info))
1255            (fmc-arg-info (cons (length metatypes) applyp))
1256            (arglist (if function-p
1257                         (make-dfun-lambda-list metatypes applyp)
1258                         (make-fast-method-call-lambda-list metatypes applyp))))
1259       (multiple-value-bind (cfunction constants)
1260           (get-fun1 `(,(if function-p
1261                            'sb-kernel:instance-lambda
1262                            'lambda)
1263                       ,arglist
1264                       ,@(unless function-p
1265                           `((declare (ignore .pv-cell.
1266                                              .next-method-call.))))
1267                       (locally (declare #.*optimize-speed*)
1268                                (let ((emf ,net))
1269                                  ,(make-emf-call metatypes applyp 'emf))))
1270                     #'net-test-converter
1271                     #'net-code-converter
1272                     (lambda (form)
1273                       (net-constant-converter form generic-function)))
1274         (lambda (method-alist wrappers)
1275           (let* ((alist (list nil))
1276                  (alist-tail alist))
1277             (dolist (constant constants)
1278               (let* ((a (or (dolist (a alist nil)
1279                               (when (eq (car a) constant)
1280                                 (return a)))
1281                             (cons constant
1282                                   (or (convert-table
1283                                        constant method-alist wrappers)
1284                                       (convert-methods
1285                                        constant method-alist wrappers)))))
1286                      (new (list a)))
1287                 (setf (cdr alist-tail) new)
1288                 (setf alist-tail new)))
1289             (let ((function (apply cfunction (mapcar #'cdr (cdr alist)))))
1290               (if function-p
1291                   function
1292                   (make-fast-method-call
1293                    :function (set-fun-name function `(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-args 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-fun). 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-fun
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-fun
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     (if methods
1466       (let ((arglist ()))
1467         ;; arglist is constructed from the GF's methods - maybe with
1468         ;; keys and rest stuff added
1469         (multiple-value-bind (required optional rest key allow-other-keys)
1470             (method-pretty-arglist (car methods))
1471           (dolist (m (cdr methods))
1472             (multiple-value-bind (method-key-keywords
1473                                   method-allow-other-keys
1474                                   method-key)
1475                 (function-keywords m)
1476               ;; we've modified function-keywords to return what we want as
1477               ;;  the third value, no other change here.
1478               (declare (ignore method-key-keywords))
1479               (setq key (union key method-key))
1480               (setq allow-other-keys (or allow-other-keys
1481                                          method-allow-other-keys))))
1482           (when allow-other-keys
1483             (setq arglist '(&allow-other-keys)))
1484           (when key
1485             (setq arglist (nconc (list '&key) key arglist)))
1486           (when rest
1487             (setq arglist (nconc (list '&rest rest) arglist)))
1488           (when optional
1489             (setq arglist (nconc (list '&optional) optional arglist)))
1490           (nconc required arglist)))
1491       ;; otherwise we take the lambda-list from the GF directly, with no
1492       ;; other 'keys' added ...
1493       (let ((lambda-list (generic-function-lambda-list generic-function)))
1494         lambda-list))))
1495
1496 (defmethod method-pretty-arglist ((method standard-method))
1497   (let ((required ())
1498         (optional ())
1499         (rest nil)
1500         (key ())
1501         (allow-other-keys nil)
1502         (state 'required)
1503         (arglist (method-lambda-list method)))
1504     (dolist (arg arglist)
1505       (cond ((eq arg '&optional)         (setq state 'optional))
1506             ((eq arg '&rest)             (setq state 'rest))
1507             ((eq arg '&key)              (setq state 'key))
1508             ((eq arg '&allow-other-keys) (setq allow-other-keys t))
1509             ((memq arg lambda-list-keywords))
1510             (t
1511              (ecase state
1512                (required (push arg required))
1513                (optional (push arg optional))
1514                (key      (push arg key))
1515                (rest     (setq rest arg))))))
1516     (values (nreverse required)
1517             (nreverse optional)
1518             rest
1519             (nreverse key)
1520             allow-other-keys)))
1521