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