0.9.0.27: fix bug 281, plus a tiny PCL cleanup
[sbcl.git] / src / pcl / boot.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
28 The CommonLoops evaluator is meta-circular.
29
30 Most of the code in PCL is methods on generic functions, including
31 most of the code that actually implements generic functions and method
32 lookup.
33
34 So, we have a classic bootstrapping problem. The solution to this is
35 to first get a cheap implementation of generic functions running,
36 these are called early generic functions. These early generic
37 functions and the corresponding early methods and early method lookup
38 are used to get enough of the system running that it is possible to
39 create real generic functions and methods and implement real method
40 lookup. At that point (done in the file FIXUP) the function
41 !FIX-EARLY-GENERIC-FUNCTIONS is called to convert all the early generic
42 functions to real generic functions.
43
44 The cheap generic functions are built using the same
45 FUNCALLABLE-INSTANCE objects that real generic functions are made out of.
46 This means that as PCL is being bootstrapped, the cheap generic
47 function objects which are being created are the same objects which
48 will later be real generic functions. This is good because:
49   - we don't cons garbage structure, and
50   - we can keep pointers to the cheap generic function objects
51     during booting because those pointers will still point to
52     the right object after the generic functions are all fixed up.
53
54 This file defines the DEFMETHOD macro and the mechanism used to expand
55 it. This includes the mechanism for processing the body of a method.
56 DEFMETHOD basically expands into a call to LOAD-DEFMETHOD, which
57 basically calls ADD-METHOD to add the method to the generic function.
58 These expansions can be loaded either during bootstrapping or when PCL
59 is fully up and running.
60
61 An important effect of this arrangement is it means we can compile
62 files with DEFMETHOD forms in them in a completely running PCL, but
63 then load those files back in during bootstrapping. This makes
64 development easier. It also means there is only one set of code for
65 processing DEFMETHOD. Bootstrapping works by being sure to have
66 LOAD-METHOD be careful to call only primitives which work during
67 bootstrapping.
68
69 |#
70
71 ;;; FIXME: As of sbcl-0.6.9.10, PCL still uses this nonstandard type
72 ;;; of declaration internally. It would be good to figure out how to
73 ;;; get rid of it, or failing that, (1) document why it's needed and
74 ;;; (2) use a private symbol with a forbidding name which suggests
75 ;;; it's not to be messed with by the user (e.g. SB-PCL:%CLASS)
76 ;;; instead of the too-inviting CLASS. (I tried just deleting the
77 ;;; declarations in MAKE-METHOD-LAMBDA-INTERNAL ca. sbcl-0.6.9.10, but
78 ;;; then things break.)
79 (declaim (declaration class))
80
81 (declaim (notinline make-a-method
82                     add-named-method
83                     ensure-generic-function-using-class
84                     add-method
85                     remove-method))
86
87 (defvar *!early-functions*
88         '((make-a-method early-make-a-method
89                          real-make-a-method)
90           (add-named-method early-add-named-method
91                             real-add-named-method)
92           ))
93
94 ;;; For each of the early functions, arrange to have it point to its
95 ;;; early definition. Do this in a way that makes sure that if we
96 ;;; redefine one of the early definitions the redefinition will take
97 ;;; effect. This makes development easier.
98 (dolist (fns *!early-functions*)
99   (let ((name (car fns))
100         (early-name (cadr fns)))
101     (setf (gdefinition name)
102             (set-fun-name
103              (lambda (&rest args)
104                (apply (fdefinition early-name) args))
105              name))))
106
107 ;;; *!GENERIC-FUNCTION-FIXUPS* is used by !FIX-EARLY-GENERIC-FUNCTIONS
108 ;;; to convert the few functions in the bootstrap which are supposed
109 ;;; to be generic functions but can't be early on.
110 (defvar *!generic-function-fixups*
111   '((add-method
112      ((generic-function method)  ;lambda-list
113       (standard-generic-function method) ;specializers
114       real-add-method))          ;method-function
115     (remove-method
116      ((generic-function method)
117       (standard-generic-function method)
118       real-remove-method))
119     (get-method
120      ((generic-function qualifiers specializers &optional (errorp t))
121       (standard-generic-function t t)
122       real-get-method))
123     (ensure-generic-function-using-class
124      ((generic-function fun-name
125                         &key generic-function-class environment
126                         &allow-other-keys)
127       (generic-function t)
128       real-ensure-gf-using-class--generic-function)
129      ((generic-function fun-name
130                         &key generic-function-class environment
131                         &allow-other-keys)
132       (null t)
133       real-ensure-gf-using-class--null))
134     (make-method-lambda
135      ((proto-generic-function proto-method lambda-expression environment)
136       (standard-generic-function standard-method t t)
137       real-make-method-lambda))
138     (make-method-initargs-form
139      ((proto-generic-function proto-method
140                               lambda-expression
141                               lambda-list environment)
142       (standard-generic-function standard-method t t t)
143       real-make-method-initargs-form))
144     (compute-effective-method
145      ((generic-function combin applicable-methods)
146       (generic-function standard-method-combination t)
147       standard-compute-effective-method))))
148 \f
149 (defmacro defgeneric (fun-name lambda-list &body options)
150   (declare (type list lambda-list))
151   (unless (legal-fun-name-p fun-name)
152     (error 'simple-program-error
153            :format-control "illegal generic function name ~S"
154            :format-arguments (list fun-name)))
155   (check-gf-lambda-list lambda-list)
156   (let ((initargs ())
157         (methods ()))
158     (flet ((duplicate-option (name)
159              (error 'simple-program-error
160                     :format-control "The option ~S appears more than once."
161                     :format-arguments (list name)))
162            (expand-method-definition (qab) ; QAB = qualifiers, arglist, body
163              (let* ((arglist-pos (position-if #'listp qab))
164                     (arglist (elt qab arglist-pos))
165                     (qualifiers (subseq qab 0 arglist-pos))
166                     (body (nthcdr (1+ arglist-pos) qab)))
167                `(push (defmethod ,fun-name ,@qualifiers ,arglist ,@body)
168                       (generic-function-initial-methods (fdefinition ',fun-name))))))
169       (macrolet ((initarg (key) `(getf initargs ,key)))
170         (dolist (option options)
171           (let ((car-option (car option)))
172             (case car-option
173               (declare
174                (when (and
175                       (consp (cadr option))
176                       (member (first (cadr option))
177                               ;; FIXME: this list is slightly weird.
178                               ;; ANSI (on the DEFGENERIC page) in one
179                               ;; place allows only OPTIMIZE; in
180                               ;; another place gives this list of
181                               ;; disallowed declaration specifiers.
182                               ;; This seems to be the only place where
183                               ;; the FUNCTION declaration is
184                               ;; mentioned; TYPE seems to be missing.
185                               ;; Very strange.  -- CSR, 2002-10-21
186                               '(declaration ftype function
187                                 inline notinline special)))
188                  (error 'simple-program-error
189                         :format-control "The declaration specifier ~S ~
190                                          is not allowed inside DEFGENERIC."
191                         :format-arguments (list (cadr option))))
192                (push (cadr option) (initarg :declarations)))
193               (:method-combination
194                (when (initarg car-option)
195                  (duplicate-option car-option))
196                (unless (symbolp (cadr option))
197                  (error 'simple-program-error
198                         :format-control "METHOD-COMBINATION name not a ~
199                                          symbol: ~S"
200                         :format-arguments (list (cadr option))))
201                (setf (initarg car-option)
202                      `',(cdr option)))
203               (:argument-precedence-order
204                (let* ((required (parse-lambda-list lambda-list))
205                       (supplied (cdr option)))
206                  (unless (= (length required) (length supplied))
207                    (error 'simple-program-error
208                           :format-control "argument count discrepancy in ~
209                                            :ARGUMENT-PRECEDENCE-ORDER clause."
210                           :format-arguments nil))
211                  (when (set-difference required supplied)
212                    (error 'simple-program-error
213                           :format-control "unequal sets for ~
214                                            :ARGUMENT-PRECEDENCE-ORDER clause: ~
215                                            ~S and ~S"
216                           :format-arguments (list required supplied)))
217                  (setf (initarg car-option)
218                        `',(cdr option))))
219               ((:documentation :generic-function-class :method-class)
220                (unless (proper-list-of-length-p option 2)
221                  (error "bad list length for ~S" option))
222                (if (initarg car-option)
223                    (duplicate-option car-option)
224                    (setf (initarg car-option) `',(cadr option))))
225               (:method
226                (push (cdr option) methods))
227               (t
228                ;; ANSI requires that unsupported things must get a
229                ;; PROGRAM-ERROR.
230                (error 'simple-program-error
231                       :format-control "unsupported option ~S"
232                       :format-arguments (list option))))))
233
234         (when (initarg :declarations)
235           (setf (initarg :declarations)
236                 `',(initarg :declarations))))
237       `(progn
238          (eval-when (:compile-toplevel :load-toplevel :execute)
239            (compile-or-load-defgeneric ',fun-name))
240          (load-defgeneric ',fun-name ',lambda-list ,@initargs)
241         ,@(mapcar #'expand-method-definition methods)
242         (fdefinition ',fun-name)))))
243
244 (defun compile-or-load-defgeneric (fun-name)
245   (proclaim-as-fun-name fun-name)
246   (note-name-defined fun-name :function)
247   (unless (eq (info :function :where-from fun-name) :declared)
248     (setf (info :function :where-from fun-name) :defined)
249     (setf (info :function :type fun-name)
250           (specifier-type 'function))))
251
252 (defun load-defgeneric (fun-name lambda-list &rest initargs)
253   (when (fboundp fun-name)
254     (style-warn "redefining ~S in DEFGENERIC" fun-name)
255     (let ((fun (fdefinition fun-name)))
256       (when (generic-function-p fun)
257         (loop for method in (generic-function-initial-methods fun)
258               do (remove-method fun method))
259         (setf (generic-function-initial-methods fun) '()))))
260   (apply #'ensure-generic-function
261          fun-name
262          :lambda-list lambda-list
263          :definition-source `((defgeneric ,fun-name) ,*load-pathname*)
264          initargs))
265
266 (define-condition generic-function-lambda-list-error
267     (reference-condition simple-program-error)
268   ()
269   (:default-initargs :references (list '(:ansi-cl :section (3 4 2)))))
270
271 (defun check-gf-lambda-list (lambda-list)
272   (flet ((ensure (arg ok)
273            (unless ok
274              (error 'generic-function-lambda-list-error
275                     :format-control
276                     "~@<invalid ~S ~_in the generic function lambda list ~S~:>"
277                     :format-arguments (list arg lambda-list)))))
278     (multiple-value-bind (required optional restp rest keyp keys allowp
279                           auxp aux morep more-context more-count)
280         (parse-lambda-list lambda-list)
281       (declare (ignore required)) ; since they're no different in a gf ll
282       (declare (ignore restp rest)) ; since they're no different in a gf ll
283       (declare (ignore allowp)) ; since &ALLOW-OTHER-KEYS is fine either way
284       (declare (ignore aux)) ; since we require AUXP=NIL
285       (declare (ignore more-context more-count)) ; safely ignored unless MOREP
286       ;; no defaults allowed for &OPTIONAL arguments
287       (dolist (i optional)
288         (ensure i (or (symbolp i)
289                       (and (consp i) (symbolp (car i)) (null (cdr i))))))
290       ;; no defaults allowed for &KEY arguments
291       (when keyp
292         (dolist (i keys)
293           (ensure i (or (symbolp i)
294                         (and (consp i)
295                              (or (symbolp (car i))
296                                  (and (consp (car i))
297                                       (symbolp (caar i))
298                                       (symbolp (cadar i))
299                                       (null (cddar i))))
300                              (null (cdr i)))))))
301       ;; no &AUX allowed
302       (when auxp
303         (error "&AUX is not allowed in a generic function lambda list: ~S"
304                lambda-list))
305       ;; Oh, *puhlease*... not specifically as per section 3.4.2 of
306       ;; the ANSI spec, but the CMU CL &MORE extension does not
307       ;; belong here!
308       (aver (not morep)))))
309 \f
310 (defmacro defmethod (&rest args &environment env)
311   (multiple-value-bind (name qualifiers lambda-list body)
312       (parse-defmethod args)
313     (multiple-value-bind (proto-gf proto-method)
314         (prototypes-for-make-method-lambda name)
315       (expand-defmethod name
316                         proto-gf
317                         proto-method
318                         qualifiers
319                         lambda-list
320                         body
321                         env))))
322
323 (defun prototypes-for-make-method-lambda (name)
324   (if (not (eq *boot-state* 'complete))
325       (values nil nil)
326       (let ((gf? (and (gboundp name)
327                       (gdefinition name))))
328         (if (or (null gf?)
329                 (not (generic-function-p gf?)))
330             (values (class-prototype (find-class 'standard-generic-function))
331                     (class-prototype (find-class 'standard-method)))
332             (values gf?
333                     (class-prototype (or (generic-function-method-class gf?)
334                                          (find-class 'standard-method))))))))
335
336 ;;; Take a name which is either a generic function name or a list specifying
337 ;;; a SETF generic function (like: (SETF <generic-function-name>)). Return
338 ;;; the prototype instance of the method-class for that generic function.
339 ;;;
340 ;;; If there is no generic function by that name, this returns the
341 ;;; default value, the prototype instance of the class
342 ;;; STANDARD-METHOD. This default value is also returned if the spec
343 ;;; names an ordinary function or even a macro. In effect, this leaves
344 ;;; the signalling of the appropriate error until load time.
345 ;;;
346 ;;; Note: During bootstrapping, this function is allowed to return NIL.
347 (defun method-prototype-for-gf (name)
348   (let ((gf? (and (gboundp name)
349                   (gdefinition name))))
350     (cond ((neq *boot-state* 'complete) nil)
351           ((or (null gf?)
352                (not (generic-function-p gf?)))          ; Someone else MIGHT
353                                                         ; error at load time.
354            (class-prototype (find-class 'standard-method)))
355           (t
356             (class-prototype (or (generic-function-method-class gf?)
357                                  (find-class 'standard-method)))))))
358 \f
359 (defun expand-defmethod (name
360                          proto-gf
361                          proto-method
362                          qualifiers
363                          lambda-list
364                          body
365                          env)
366   (multiple-value-bind (method-lambda unspecialized-lambda-list specializers)
367       (add-method-declarations name qualifiers lambda-list body env)
368     (multiple-value-bind (method-function-lambda initargs)
369         (make-method-lambda proto-gf proto-method method-lambda env)
370       (let ((initargs-form (make-method-initargs-form proto-gf
371                                                       proto-method
372                                                       method-function-lambda
373                                                       initargs
374                                                       env)))
375         `(progn
376           ;; Note: We could DECLAIM the ftype of the generic function
377           ;; here, since ANSI specifies that we create it if it does
378           ;; not exist. However, I chose not to, because I think it's
379           ;; more useful to support a style of programming where every
380           ;; generic function has an explicit DEFGENERIC and any typos
381           ;; in DEFMETHODs are warned about. Otherwise
382           ;;
383           ;;   (DEFGENERIC FOO-BAR-BLETCH ((X T)))
384           ;;   (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
385           ;;   (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
386           ;;   (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
387           ;;   (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
388           ;;   (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
389           ;;
390           ;; compiles without raising an error and runs without
391           ;; raising an error (since SIMPLE-VECTOR cases fall through
392           ;; to VECTOR) but still doesn't do what was intended. I hate
393           ;; that kind of bug (code which silently gives the wrong
394           ;; answer), so we don't do a DECLAIM here. -- WHN 20000229
395           ,(make-defmethod-form name qualifiers specializers
396                                 unspecialized-lambda-list
397                                 (if proto-method
398                                     (class-name (class-of proto-method))
399                                     'standard-method)
400                                 initargs-form
401                                 (getf (getf initargs :plist)
402                                       :pv-table-symbol)))))))
403
404 (defun interned-symbol-p (x)
405   (and (symbolp x) (symbol-package x)))
406
407 (defun make-defmethod-form (name qualifiers specializers
408                                  unspecialized-lambda-list method-class-name
409                                  initargs-form &optional pv-table-symbol)
410   (let (fn
411         fn-lambda)
412     (if (and (interned-symbol-p (fun-name-block-name name))
413              (every #'interned-symbol-p qualifiers)
414              (every (lambda (s)
415                       (if (consp s)
416                           (and (eq (car s) 'eql)
417                                (constantp (cadr s))
418                                (let ((sv (eval (cadr s))))
419                                  (or (interned-symbol-p sv)
420                                      (integerp sv)
421                                      (and (characterp sv)
422                                           (standard-char-p sv)))))
423                           (interned-symbol-p s)))
424                     specializers)
425              (consp initargs-form)
426              (eq (car initargs-form) 'list*)
427              (memq (cadr initargs-form) '(:function :fast-function))
428              (consp (setq fn (caddr initargs-form)))
429              (eq (car fn) 'function)
430              (consp (setq fn-lambda (cadr fn)))
431              (eq (car fn-lambda) 'lambda))
432         (let* ((specls (mapcar (lambda (specl)
433                                  (if (consp specl)
434                                      `(,(car specl) ,(eval (cadr specl)))
435                                    specl))
436                                specializers))
437                (mname `(,(if (eq (cadr initargs-form) :function)
438                              'slow-method 'fast-method)
439                         ,name ,@qualifiers ,specls)))
440           `(progn
441              (defun ,mname ,(cadr fn-lambda)
442                ,@(cddr fn-lambda))
443              ,(make-defmethod-form-internal
444                name qualifiers `',specls
445                unspecialized-lambda-list method-class-name
446                `(list* ,(cadr initargs-form)
447                        #',mname
448                        ,@(cdddr initargs-form))
449                pv-table-symbol)))
450         (make-defmethod-form-internal
451          name qualifiers
452          `(list ,@(mapcar (lambda (specializer)
453                             (if (consp specializer)
454                                 ``(,',(car specializer)
455                                       ,,(cadr specializer))
456                                 `',specializer))
457                           specializers))
458          unspecialized-lambda-list
459          method-class-name
460          initargs-form
461          pv-table-symbol))))
462
463 (defun make-defmethod-form-internal
464     (name qualifiers specializers-form unspecialized-lambda-list
465      method-class-name initargs-form &optional pv-table-symbol)
466   `(load-defmethod
467     ',method-class-name
468     ',name
469     ',qualifiers
470     ,specializers-form
471     ',unspecialized-lambda-list
472     ,initargs-form
473     ;; Paper over a bug in KCL by passing the cache-symbol here in
474     ;; addition to in the list. FIXME: We should no longer need to do
475     ;; this, since the CLOS code is now SBCL-specific, and doesn't
476     ;; need to be ported to every buggy compiler in existence.
477     ',pv-table-symbol))
478
479 (defmacro make-method-function (method-lambda &environment env)
480   (make-method-function-internal method-lambda env))
481
482 (defun make-method-function-internal (method-lambda &optional env)
483   (multiple-value-bind (proto-gf proto-method)
484       (prototypes-for-make-method-lambda nil)
485     (multiple-value-bind (method-function-lambda initargs)
486         (make-method-lambda proto-gf proto-method method-lambda env)
487       (make-method-initargs-form proto-gf
488                                  proto-method
489                                  method-function-lambda
490                                  initargs
491                                  env))))
492
493 (defun add-method-declarations (name qualifiers lambda-list body env)
494   (declare (ignore env))
495   (multiple-value-bind (parameters unspecialized-lambda-list specializers)
496       (parse-specialized-lambda-list lambda-list)
497     (multiple-value-bind (real-body declarations documentation)
498         (parse-body body)
499       (values `(lambda ,unspecialized-lambda-list
500                  ,@(when documentation `(,documentation))
501                  ;; (Old PCL code used a somewhat different style of
502                  ;; list for %METHOD-NAME values. Our names use
503                  ;; ,@QUALIFIERS instead of ,QUALIFIERS so that the
504                  ;; method names look more like what you see in a
505                  ;; DEFMETHOD form.)
506                  ;;
507                  ;; FIXME: As of sbcl-0.7.0.6, code elsewhere, at
508                  ;; least the code to set up named BLOCKs around the
509                  ;; bodies of methods, depends on the function's base
510                  ;; name being the first element of the %METHOD-NAME
511                  ;; list. It would be good to remove this dependency,
512                  ;; perhaps by building the BLOCK here, or by using
513                  ;; another declaration (e.g. %BLOCK-NAME), so that
514                  ;; our method debug names are free to have any format,
515                  ;; e.g. (:METHOD PRINT-OBJECT :AROUND (CLOWN T)).
516                  ;;
517                  ;; Further, as of sbcl-0.7.9.10, the code to
518                  ;; implement NO-NEXT-METHOD is coupled to the form of
519                  ;; this declaration; see the definition of
520                  ;; CALL-NO-NEXT-METHOD (and the passing of
521                  ;; METHOD-NAME-DECLARATION arguments around the
522                  ;; various CALL-NEXT-METHOD logic).
523                  (declare (%method-name (,name
524                                          ,@qualifiers
525                                          ,specializers)))
526                  (declare (%method-lambda-list ,@lambda-list))
527                  ,@declarations
528                  ,@real-body)
529               unspecialized-lambda-list specializers))))
530
531 (defun real-make-method-initargs-form (proto-gf proto-method
532                                        method-lambda initargs env)
533   (declare (ignore proto-gf proto-method))
534   (unless (and (consp method-lambda)
535                (eq (car method-lambda) 'lambda))
536     (error "The METHOD-LAMBDA argument to MAKE-METHOD-FUNCTION, ~S, ~
537             is not a lambda form."
538            method-lambda))
539   (make-method-initargs-form-internal method-lambda initargs env))
540
541 (unless (fboundp 'make-method-initargs-form)
542   (setf (gdefinition 'make-method-initargs-form)
543         (symbol-function 'real-make-method-initargs-form)))
544
545 (defun real-make-method-lambda (proto-gf proto-method method-lambda env)
546   (declare (ignore proto-gf proto-method))
547   (make-method-lambda-internal method-lambda env))
548
549 ;;; a helper function for creating Python-friendly type declarations
550 ;;; in DEFMETHOD forms
551 (defun parameter-specializer-declaration-in-defmethod (parameter specializer)
552   (cond ((and (consp specializer)
553               (eq (car specializer) 'eql))
554          ;; KLUDGE: ANSI, in its wisdom, says that
555          ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at
556          ;; DEFMETHOD expansion time. Thus, although one might think
557          ;; that in
558          ;;   (DEFMETHOD FOO ((X PACKAGE)
559          ;;                   (Y (EQL 12))
560          ;;      ..))
561          ;; the PACKAGE and (EQL 12) forms are both parallel type
562          ;; names, they're not, as is made clear when you do
563          ;;   (DEFMETHOD FOO ((X PACKAGE)
564          ;;                   (Y (EQL 'BAR)))
565          ;;     ..)
566          ;; where Y needs to be a symbol named "BAR", not some cons
567          ;; made by (CONS 'QUOTE 'BAR). I.e. when the
568          ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument
569          ;; to be of type (EQL X). It'd be easy to transform one to
570          ;; the other, but it'd be somewhat messier to do so while
571          ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd
572          ;; once. (The new code wouldn't be messy, but it'd require a
573          ;; big transformation of the old code.) So instead we punt.
574          ;; -- WHN 20000610
575          '(ignorable))
576         ((member specializer
577                  ;; KLUDGE: For some low-level implementation
578                  ;; classes, perhaps because of some problems related
579                  ;; to the incomplete integration of PCL into SBCL's
580                  ;; type system, some specializer classes can't be
581                  ;; declared as argument types. E.g.
582                  ;;   (DEFMETHOD FOO ((X SLOT-OBJECT))
583                  ;;     (DECLARE (TYPE SLOT-OBJECT X))
584                  ;;     ..)
585                  ;; loses when
586                  ;;   (DEFSTRUCT BAR A B)
587                  ;;   (FOO (MAKE-BAR))
588                  ;; perhaps because of the way that STRUCTURE-OBJECT
589                  ;; inherits both from SLOT-OBJECT and from
590                  ;; SB-KERNEL:INSTANCE. In an effort to sweep such
591                  ;; problems under the rug, we exclude these problem
592                  ;; cases by blacklisting them here. -- WHN 2001-01-19
593                  '(slot-object))
594          '(ignorable))
595         ((not (eq *boot-state* 'complete))
596          ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
597          ;; types which don't match their specializers. (Specifically,
598          ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
599          ;; second argument.) Hopefully it only does this kind of
600          ;; weirdness when bootstrapping.. -- WHN 20000610
601          '(ignorable))
602         ((var-globally-special-p parameter)
603          ;; KLUDGE: Don't declare types for global special variables
604          ;; -- our rebinding magic for SETQ cases don't work right
605          ;; there.
606          ;;
607          ;; FIXME: It would be better to detect the SETQ earlier and
608          ;; skip declarations for specials only when needed, not
609          ;; always.
610          ;;
611          ;; --NS 2004-10-14
612          '(ignorable))
613         (t
614          ;; Otherwise, we can usually make Python very happy.
615          (let ((kind (info :type :kind specializer)))
616            (ecase kind
617              ((:primitive) `(type ,specializer ,parameter))
618              ((:defined) 
619               (let ((class (find-class specializer nil)))
620                 ;; CLASS can be null here if the user has erroneously
621                 ;; tried to use a defined type as a specializer; it
622                 ;; can be a non-BUILT-IN-CLASS if the user defines a
623                 ;; type and calls (SETF FIND-CLASS) in a consistent
624                 ;; way.
625                 (when (and class (typep class 'built-in-class))
626                   `(type ,specializer ,parameter))))
627              ((:instance nil)
628               (let ((class (find-class specializer nil)))
629                 (cond
630                   (class
631                    (if (typep class '(or built-in-class structure-class))
632                        `(type ,specializer ,parameter)
633                        ;; don't declare CLOS classes as parameters;
634                        ;; it's too expensive.
635                        '(ignorable)))
636                   (t
637                    ;; we can get here, and still not have a failure
638                    ;; case, by doing MOP programming like (PROGN
639                    ;; (ENSURE-CLASS 'FOO) (DEFMETHOD BAR ((X FOO))
640                    ;; ...)).  Best to let the user know we haven't
641                    ;; been able to extract enough information:
642                    (style-warn
643                     "~@<can't find type for presumed class ~S in ~S.~@:>"
644                     specializer
645                     'parameter-specializer-declaration-in-defmethod)
646                    '(ignorable)))))
647              ((:forthcoming-defclass-type) '(ignorable)))))))
648
649 (defun make-method-lambda-internal (method-lambda &optional env)
650   (unless (and (consp method-lambda) (eq (car method-lambda) 'lambda))
651     (error "The METHOD-LAMBDA argument to MAKE-METHOD-LAMBDA, ~S, ~
652             is not a lambda form."
653            method-lambda))
654   (multiple-value-bind (real-body declarations documentation)
655       (parse-body (cddr method-lambda))
656     (let* ((name-decl (get-declaration '%method-name declarations))
657            (sll-decl (get-declaration '%method-lambda-list declarations))
658            (method-name (when (consp name-decl) (car name-decl)))
659            (generic-function-name (when method-name (car method-name)))
660            (specialized-lambda-list (or sll-decl (cadr method-lambda))))
661       (multiple-value-bind (parameters lambda-list specializers)
662           (parse-specialized-lambda-list specialized-lambda-list)
663         (let* ((required-parameters
664                 (mapcar (lambda (r s) (declare (ignore s)) r)
665                         parameters
666                         specializers))
667                (slots (mapcar #'list required-parameters))
668                (calls (list nil))
669                (class-declarations
670                 `(declare
671                   ;; These declarations seem to be used by PCL to pass
672                   ;; information to itself; when I tried to delete 'em
673                   ;; ca. 0.6.10 it didn't work. I'm not sure how
674                   ;; they work, but note the (VAR-DECLARATION '%CLASS ..)
675                   ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30
676                   ,@(remove nil
677                             (mapcar (lambda (a s) (and (symbolp s)
678                                                        (neq s t)
679                                                        `(%class ,a ,s)))
680                                     parameters
681                                     specializers))
682                   ;; These TYPE declarations weren't in the original
683                   ;; PCL code, but the Python compiler likes them a
684                   ;; lot. (We're telling the compiler about our
685                   ;; knowledge of specialized argument types so that
686                   ;; it can avoid run-time type dispatch overhead,
687                   ;; which can be a huge win for Python.)
688                   ;;
689                   ;; KLUDGE: when I tried moving these to
690                   ;; ADD-METHOD-DECLARATIONS, things broke.  No idea
691                   ;; why.  -- CSR, 2004-06-16
692                   ,@(mapcar #'parameter-specializer-declaration-in-defmethod
693                             parameters
694                             specializers)))
695                (method-lambda
696                 ;; Remove the documentation string and insert the
697                 ;; appropriate class declarations. The documentation
698                 ;; string is removed to make it easy for us to insert
699                 ;; new declarations later, they will just go after the
700                 ;; CADR of the method lambda. The class declarations
701                 ;; are inserted to communicate the class of the method's
702                 ;; arguments to the code walk.
703                 `(lambda ,lambda-list
704                    ;; The default ignorability of method parameters
705                    ;; doesn't seem to be specified by ANSI. PCL had
706                    ;; them basically ignorable but was a little
707                    ;; inconsistent. E.g. even though the two
708                    ;; method definitions 
709                    ;;   (DEFMETHOD FOO ((X T) (Y T)) "Z")
710                    ;;   (DEFMETHOD FOO ((X T) Y) "Z")
711                    ;; are otherwise equivalent, PCL treated Y as
712                    ;; ignorable in the first definition but not in the
713                    ;; second definition. We make all required
714                    ;; parameters ignorable as a way of systematizing
715                    ;; the old PCL behavior. -- WHN 2000-11-24
716                    (declare (ignorable ,@required-parameters))
717                    ,class-declarations
718                    ,@declarations
719                    (block ,(fun-name-block-name generic-function-name)
720                      ,@real-body)))
721                (constant-value-p (and (null (cdr real-body))
722                                       (constantp (car real-body))))
723                (constant-value (and constant-value-p
724                                     (eval (car real-body))))
725                (plist (and constant-value-p
726                            (or (typep constant-value
727                                       '(or number character))
728                                (and (symbolp constant-value)
729                                     (symbol-package constant-value)))
730                            (list :constant-value constant-value)))
731                (applyp (dolist (p lambda-list nil)
732                          (cond ((memq p '(&optional &rest &key))
733                                 (return t))
734                                ((eq p '&aux)
735                                 (return nil))))))
736           (multiple-value-bind
737                 (walked-lambda call-next-method-p closurep
738                                next-method-p-p setq-p)
739               (walk-method-lambda method-lambda
740                                   required-parameters
741                                   env
742                                   slots
743                                   calls)
744             (multiple-value-bind (walked-lambda-body
745                                   walked-declarations
746                                   walked-documentation)
747                 (parse-body (cddr walked-lambda))
748               (declare (ignore walked-documentation))
749               (when (or next-method-p-p call-next-method-p)
750                 (setq plist (list* :needs-next-methods-p t plist)))
751               (when (some #'cdr slots)
752                 (multiple-value-bind (slot-name-lists call-list)
753                     (slot-name-lists-from-slots slots calls)
754                   (let ((pv-table-symbol (make-symbol "pv-table")))
755                     (setq plist
756                           `(,@(when slot-name-lists
757                                 `(:slot-name-lists ,slot-name-lists))
758                               ,@(when call-list
759                                   `(:call-list ,call-list))
760                               :pv-table-symbol ,pv-table-symbol
761                               ,@plist))
762                     (setq walked-lambda-body
763                           `((pv-binding (,required-parameters
764                                          ,slot-name-lists
765                                          ,pv-table-symbol)
766                                         ,@walked-lambda-body))))))
767               (when (and (memq '&key lambda-list)
768                          (not (memq '&allow-other-keys lambda-list)))
769                 (let ((aux (memq '&aux lambda-list)))
770                 (setq lambda-list (nconc (ldiff lambda-list aux)
771                                          (list '&allow-other-keys)
772                                          aux))))
773               (values `(lambda (.method-args. .next-methods.)
774                          (simple-lexical-method-functions
775                           (,lambda-list .method-args. .next-methods.
776                                         :call-next-method-p
777                                         ,call-next-method-p
778                                         :next-method-p-p ,next-method-p-p
779                                         :setq-p ,setq-p
780                                         ;; we need to pass this along
781                                         ;; so that NO-NEXT-METHOD can
782                                         ;; be given a suitable METHOD
783                                         ;; argument; we need the
784                                         ;; QUALIFIERS and SPECIALIZERS
785                                         ;; inside the declaration to
786                                         ;; give to FIND-METHOD.
787                                         :method-name-declaration ,name-decl
788                                         :closurep ,closurep
789                                         :applyp ,applyp)
790                           ,@walked-declarations
791                           ,@walked-lambda-body))
792                       `(,@(when plist
793                       `(:plist ,plist))
794                           ,@(when documentation
795                           `(:documentation ,documentation)))))))))))
796
797 (unless (fboundp 'make-method-lambda)
798   (setf (gdefinition 'make-method-lambda)
799         (symbol-function 'real-make-method-lambda)))
800
801 (defmacro simple-lexical-method-functions ((lambda-list
802                                             method-args
803                                             next-methods
804                                             &rest lmf-options)
805                                            &body body)
806   `(progn
807      ,method-args ,next-methods
808      (bind-simple-lexical-method-macros (,method-args ,next-methods)
809        (bind-lexical-method-functions (,@lmf-options)
810          (bind-args (,lambda-list ,method-args)
811            ,@body)))))
812
813 (defmacro fast-lexical-method-functions ((lambda-list
814                                           next-method-call
815                                           args
816                                           rest-arg
817                                           &rest lmf-options)
818                                          &body body)
819   `(bind-fast-lexical-method-macros (,args ,rest-arg ,next-method-call)
820      (bind-lexical-method-functions (,@lmf-options)
821        (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg)
822          ,@body))))
823
824 (defmacro bind-simple-lexical-method-macros ((method-args next-methods)
825                                              &body body)
826   `(macrolet ((call-next-method-bind (&body body)
827                `(let ((.next-method. (car ,',next-methods))
828                       (,',next-methods (cdr ,',next-methods)))
829                  .next-method. ,',next-methods
830                  ,@body))
831               (call-next-method-body (method-name-declaration cnm-args)
832                `(if .next-method.
833                     (funcall (if (std-instance-p .next-method.)
834                                  (method-function .next-method.)
835                              .next-method.) ; for early methods
836                              (or ,cnm-args ,',method-args)
837                              ,',next-methods)
838                     (apply #'call-no-next-method ',method-name-declaration
839                             (or ,cnm-args ,',method-args))))
840               (next-method-p-body ()
841                `(not (null .next-method.)))
842               (with-rebound-original-args ((call-next-method-p setq-p)
843                                            &body body)
844                 (declare (ignore call-next-method-p setq-p))
845                 `(let () ,@body)))
846     ,@body))
847
848 (defun call-no-next-method (method-name-declaration &rest args)
849   (destructuring-bind (name) method-name-declaration
850     (destructuring-bind (name &rest qualifiers-and-specializers) name
851       ;; KLUDGE: inefficient traversal, but hey.  This should only
852       ;; happen on the slow error path anyway.
853       (let* ((qualifiers (butlast qualifiers-and-specializers))
854              (specializers (car (last qualifiers-and-specializers)))
855              (method (find-method (gdefinition name) qualifiers specializers)))
856         (apply #'no-next-method
857                (method-generic-function method)
858                method
859                args)))))
860
861 (defstruct (method-call (:copier nil))
862   (function #'identity :type function)
863   call-method-args)
864
865 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
866
867 (defmacro invoke-method-call1 (function args cm-args)
868   `(let ((.function. ,function)
869          (.args. ,args)
870          (.cm-args. ,cm-args))
871      (if (and .cm-args. (null (cdr .cm-args.)))
872          (funcall .function. .args. (car .cm-args.))
873          (apply .function. .args. .cm-args.))))
874
875 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
876   `(invoke-method-call1 (method-call-function ,method-call)
877                         ,(if restp
878                              `(list* ,@required-args+rest-arg)
879                              `(list ,@required-args+rest-arg))
880                         (method-call-call-method-args ,method-call)))
881
882 (defstruct (fast-method-call (:copier nil))
883   (function #'identity :type function)
884   pv-cell
885   next-method-call
886   arg-info)
887
888 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
889
890 (defmacro fmc-funcall (fn pv-cell next-method-call &rest args)
891   `(funcall ,fn ,pv-cell ,next-method-call ,@args))
892
893 (defmacro invoke-fast-method-call (method-call &rest required-args+rest-arg)
894   `(fmc-funcall (fast-method-call-function ,method-call)
895                 (fast-method-call-pv-cell ,method-call)
896                 (fast-method-call-next-method-call ,method-call)
897                 ,@required-args+rest-arg))
898
899 (defstruct (fast-instance-boundp (:copier nil))
900   (index 0 :type fixnum))
901
902 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
903
904 (eval-when (:compile-toplevel :load-toplevel :execute)
905   (defvar *allow-emf-call-tracing-p* nil)
906   (defvar *enable-emf-call-tracing-p* #-sb-show nil #+sb-show t))
907 \f
908 ;;;; effective method functions
909
910 (defvar *emf-call-trace-size* 200)
911 (defvar *emf-call-trace* nil)
912 (defvar *emf-call-trace-index* 0)
913
914 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
915 ;;; without explanation. It appears to be intended for debugging, so
916 ;;; it might be useful someday, so I haven't deleted it.
917 ;;; But it isn't documented and isn't used for anything now, so
918 ;;; I've conditionalized it out of the base system. -- WHN 19991213
919 #+sb-show
920 (defun show-emf-call-trace ()
921   (when *emf-call-trace*
922     (let ((j *emf-call-trace-index*)
923           (*enable-emf-call-tracing-p* nil))
924       (format t "~&(The oldest entries are printed first)~%")
925       (dotimes-fixnum (i *emf-call-trace-size*)
926         (let ((ct (aref *emf-call-trace* j)))
927           (when ct (print ct)))
928         (incf j)
929         (when (= j *emf-call-trace-size*)
930           (setq j 0))))))
931
932 (defun trace-emf-call-internal (emf format args)
933   (unless *emf-call-trace*
934     (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
935   (setf (aref *emf-call-trace* *emf-call-trace-index*)
936         (list* emf format args))
937   (incf *emf-call-trace-index*)
938   (when (= *emf-call-trace-index* *emf-call-trace-size*)
939     (setq *emf-call-trace-index* 0)))
940
941 (defmacro trace-emf-call (emf format args)
942   (when *allow-emf-call-tracing-p*
943     `(when *enable-emf-call-tracing-p*
944        (trace-emf-call-internal ,emf ,format ,args))))
945
946 (defmacro invoke-effective-method-function-fast
947     (emf restp &rest required-args+rest-arg)
948   `(progn
949      (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
950      (invoke-fast-method-call ,emf ,@required-args+rest-arg)))
951
952 (defmacro invoke-effective-method-function (emf restp
953                                                 &rest required-args+rest-arg)
954   (unless (constantp restp)
955     (error "The RESTP argument is not constant."))
956   ;; FIXME: The RESTP handling here is confusing and maybe slightly
957   ;; broken if RESTP evaluates to a non-self-evaluating form. E.g. if
958   ;;   (INVOKE-EFFECTIVE-METHOD-FUNCTION EMF '(ERROR "gotcha") ...)
959   ;; then TRACE-EMF-CALL-CALL-INTERNAL might die on a gotcha error.
960   (setq restp (eval restp))
961   `(progn
962      (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
963      (cond ((typep ,emf 'fast-method-call)
964             (invoke-fast-method-call ,emf ,@required-args+rest-arg))
965            ;; "What," you may wonder, "do these next two clauses do?"
966            ;; In that case, you are not a PCL implementor, for they
967            ;; considered this to be self-documenting.:-| Or CSR, for
968            ;; that matter, since he can also figure it out by looking
969            ;; at it without breaking stride. For the rest of us,
970            ;; though: From what the code is doing with .SLOTS. and
971            ;; whatnot, evidently it's implementing SLOT-VALUEish and
972            ;; GET-SLOT-VALUEish things. Then we can reason backwards
973            ;; and conclude that setting EMF to a FIXNUM is an
974            ;; optimized way to represent these slot access operations.
975            ,@(when (and (null restp) (= 1 (length required-args+rest-arg)))
976                `(((typep ,emf 'fixnum)
977                   (let* ((.slots. (get-slots-or-nil
978                                    ,(car required-args+rest-arg)))
979                          (value (when .slots. (clos-slots-ref .slots. ,emf))))
980                     (if (eq value +slot-unbound+)
981                         (slot-unbound-internal ,(car required-args+rest-arg)
982                                                ,emf)
983                         value)))))
984            ,@(when (and (null restp) (= 2 (length required-args+rest-arg)))
985                `(((typep ,emf 'fixnum)
986                   (let ((.new-value. ,(car required-args+rest-arg))
987                         (.slots. (get-slots-or-nil
988                                   ,(cadr required-args+rest-arg))))
989                     (when .slots.
990                       (setf (clos-slots-ref .slots. ,emf) .new-value.))))))
991            ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
992            ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
993            ;; there was no explanation and presumably the code is 10+
994            ;; years stale, I simply deleted it. -- WHN)
995            (t
996             (etypecase ,emf
997               (method-call
998                (invoke-method-call ,emf ,restp ,@required-args+rest-arg))
999               (function
1000                ,(if restp
1001                     `(apply (the function ,emf) ,@required-args+rest-arg)
1002                     `(funcall (the function ,emf)
1003                               ,@required-args+rest-arg))))))))
1004
1005 (defun invoke-emf (emf args)
1006   (trace-emf-call emf t args)
1007   (etypecase emf
1008     (fast-method-call
1009      (let* ((arg-info (fast-method-call-arg-info emf))
1010             (restp (cdr arg-info))
1011             (nreq (car arg-info)))
1012        (if restp
1013            (let* ((rest-args (nthcdr nreq args))
1014                   (req-args (ldiff args rest-args)))
1015              (apply (fast-method-call-function emf)
1016                     (fast-method-call-pv-cell emf)
1017                     (fast-method-call-next-method-call emf)
1018                     (nconc req-args (list rest-args))))
1019            (cond ((null args)
1020                   (if (eql nreq 0)
1021                       (invoke-fast-method-call emf)
1022                       (error "wrong number of args")))
1023                  ((null (cdr args))
1024                   (if (eql nreq 1)
1025                       (invoke-fast-method-call emf (car args))
1026                       (error "wrong number of args")))
1027                  ((null (cddr args))
1028                   (if (eql nreq 2)
1029                       (invoke-fast-method-call emf (car args) (cadr args))
1030                       (error "wrong number of args")))
1031                  (t
1032                   (apply (fast-method-call-function emf)
1033                          (fast-method-call-pv-cell emf)
1034                          (fast-method-call-next-method-call emf)
1035                          args))))))
1036     (method-call
1037      (apply (method-call-function emf)
1038             args
1039             (method-call-call-method-args emf)))
1040     (fixnum
1041      (cond ((null args) (error "1 or 2 args were expected."))
1042            ((null (cdr args))
1043             (let* ((slots (get-slots (car args)))
1044                    (value (clos-slots-ref slots emf)))
1045               (if (eq value +slot-unbound+)
1046                   (slot-unbound-internal (car args) emf)
1047                   value)))
1048            ((null (cddr args))
1049              (setf (clos-slots-ref (get-slots (cadr args)) emf)
1050                    (car args)))
1051            (t (error "1 or 2 args were expected."))))
1052     (fast-instance-boundp
1053      (if (or (null args) (cdr args))
1054          (error "1 arg was expected.")
1055        (let ((slots (get-slots (car args))))
1056          (not (eq (clos-slots-ref slots
1057                                   (fast-instance-boundp-index emf))
1058                   +slot-unbound+)))))
1059     (function
1060      (apply emf args))))
1061 \f
1062 (defmacro bind-fast-lexical-method-macros ((args rest-arg next-method-call)
1063                                            &body body)
1064   (let* ((all-params (append args (when rest-arg (list rest-arg))))
1065          (rebindings (mapcar (lambda (x) (list x x)) all-params)))
1066     `(macrolet ((narrowed-emf (emf)
1067                  ;; INVOKE-EFFECTIVE-METHOD-FUNCTION has code in it to
1068                  ;; dispatch on the possibility that EMF might be of
1069                  ;; type FIXNUM (as an optimized representation of a
1070                  ;; slot accessor). But as far as I (WHN 2002-06-11)
1071                  ;; can tell, it's impossible for such a representation
1072                  ;; to end up as .NEXT-METHOD-CALL. By reassuring
1073                  ;; INVOKE-E-M-F that when called from this context
1074                  ;; it needn't worry about the FIXNUM case, we can
1075                  ;; keep those cases from being compiled, which is
1076                  ;; good both because it saves bytes and because it
1077                  ;; avoids annoying type mismatch compiler warnings.
1078                  ;;
1079                  ;; KLUDGE: In sbcl-0.7.4.29, the compiler's type
1080                  ;; system isn't smart enough about NOT and
1081                  ;; intersection types to benefit from a (NOT FIXNUM)
1082                  ;; declaration here. -- WHN 2002-06-12 (FIXME: maybe
1083                  ;; it is now... -- CSR, 2003-06-07)
1084                  ;;
1085                  ;; FIXME: Might the FUNCTION type be omittable here,
1086                  ;; leaving only METHOD-CALLs? Failing that, could this
1087                  ;; be documented somehow? (It'd be nice if the types
1088                  ;; involved could be understood without solving the
1089                  ;; halting problem.)
1090                  `(the (or function method-call fast-method-call)
1091                    ,emf))
1092                 (call-next-method-bind (&body body)
1093                  `(let () ,@body))
1094                 (call-next-method-body (method-name-declaration cnm-args)
1095                  `(if ,',next-method-call
1096                       ,(locally
1097                         ;; This declaration suppresses a "deleting
1098                         ;; unreachable code" note for the following IF
1099                         ;; when REST-ARG is NIL. It is not nice for
1100                         ;; debugging SBCL itself, but at least it
1101                         ;; keeps us from annoying users.
1102                         (declare (optimize (inhibit-warnings 3)))
1103                         (if (and (null ',rest-arg)
1104                                  (consp cnm-args)
1105                                  (eq (car cnm-args) 'list))
1106                             `(invoke-effective-method-function
1107                               (narrowed-emf ,',next-method-call)
1108                               nil
1109                               ,@(cdr cnm-args))
1110                             (let ((call `(invoke-effective-method-function
1111                                           (narrowed-emf ,',next-method-call)
1112                                           ,',(not (null rest-arg))
1113                                           ,@',args
1114                                           ,@',(when rest-arg `(,rest-arg)))))
1115                               `(if ,cnm-args
1116                                 (bind-args ((,@',args
1117                                              ,@',(when rest-arg
1118                                                        `(&rest ,rest-arg)))
1119                                             ,cnm-args)
1120                                  ,call)
1121                                 ,call))))
1122                       ,(locally
1123                         ;; As above, this declaration suppresses code
1124                         ;; deletion notes.
1125                         (declare (optimize (inhibit-warnings 3)))
1126                         (if (and (null ',rest-arg)
1127                                  (consp cnm-args)
1128                                  (eq (car cnm-args) 'list))
1129                             `(call-no-next-method ',method-name-declaration
1130                               ,@(cdr cnm-args))
1131                             `(call-no-next-method ',method-name-declaration
1132                               ,@',args
1133                               ,@',(when rest-arg
1134                                         `(,rest-arg)))))))
1135                 (next-method-p-body ()
1136                  `(not (null ,',next-method-call)))
1137                 (with-rebound-original-args ((cnm-p setq-p) &body body)
1138                   (if (or cnm-p setq-p)
1139                       `(let ,',rebindings
1140                         (declare (ignorable ,@',all-params))
1141                         ,@body)
1142                       `(let () ,@body))))
1143       ,@body)))
1144
1145 (defmacro bind-lexical-method-functions
1146     ((&key call-next-method-p next-method-p-p setq-p
1147            closurep applyp method-name-declaration)
1148      &body body)
1149   (cond ((and (null call-next-method-p) (null next-method-p-p)
1150               (null closurep) (null applyp) (null setq-p))
1151          `(let () ,@body))
1152         (t
1153          `(call-next-method-bind
1154             (flet (,@(and call-next-method-p
1155                           `((call-next-method (&rest cnm-args)
1156                              (call-next-method-body
1157                               ,method-name-declaration
1158                               cnm-args))))
1159                    ,@(and next-method-p-p
1160                           '((next-method-p ()
1161                              (next-method-p-body)))))
1162               (with-rebound-original-args (,call-next-method-p ,setq-p)
1163                 ,@body))))))
1164
1165 (defmacro bind-args ((lambda-list args) &body body)
1166   (let ((args-tail '.args-tail.)
1167         (key '.key.)
1168         (state 'required))
1169     (flet ((process-var (var)
1170              (if (memq var lambda-list-keywords)
1171                  (progn
1172                    (case var
1173                      (&optional       (setq state 'optional))
1174                      (&key            (setq state 'key))
1175                      (&allow-other-keys)
1176                      (&rest           (setq state 'rest))
1177                      (&aux            (setq state 'aux))
1178                      (otherwise
1179                       (error
1180                        "encountered the non-standard lambda list keyword ~S"
1181                        var)))
1182                    nil)
1183                  (case state
1184                    (required `((,var (pop ,args-tail))))
1185                    (optional (cond ((not (consp var))
1186                                     `((,var (when ,args-tail
1187                                               (pop ,args-tail)))))
1188                                    ((null (cddr var))
1189                                     `((,(car var) (if ,args-tail
1190                                                       (pop ,args-tail)
1191                                                       ,(cadr var)))))
1192                                    (t
1193                                     `((,(caddr var) ,args-tail)
1194                                       (,(car var) (if ,args-tail
1195                                                       (pop ,args-tail)
1196                                                       ,(cadr var)))))))
1197                    (rest `((,var ,args-tail)))
1198                    (key (cond ((not (consp var))
1199                                `((,var (car
1200                                         (get-key-arg-tail ,(keywordicate var)
1201                                                           ,args-tail)))))
1202                               ((null (cddr var))
1203                                (multiple-value-bind (keyword variable)
1204                                    (if (consp (car var))
1205                                        (values (caar var)
1206                                                (cadar var))
1207                                        (values (keywordicate (car var))
1208                                                (car var)))
1209                                  `((,key (get-key-arg-tail ',keyword
1210                                                            ,args-tail))
1211                                    (,variable (if ,key
1212                                                   (car ,key)
1213                                                   ,(cadr var))))))
1214                               (t
1215                                (multiple-value-bind (keyword variable)
1216                                    (if (consp (car var))
1217                                        (values (caar var)
1218                                                (cadar var))
1219                                        (values (keywordicate (car var))
1220                                                (car var)))
1221                                  `((,key (get-key-arg-tail ',keyword
1222                                                            ,args-tail))
1223                                    (,(caddr var) ,key)
1224                                    (,variable (if ,key
1225                                                   (car ,key)
1226                                                   ,(cadr var))))))))
1227                    (aux `(,var))))))
1228       (let ((bindings (mapcan #'process-var lambda-list)))
1229         `(let* ((,args-tail ,args)
1230                 ,@bindings
1231                 (.dummy0.
1232                  ,@(when (eq state 'optional)
1233                      `((unless (null ,args-tail)
1234                          (error 'simple-program-error
1235                                 :format-control "surplus arguments: ~S"
1236                                 :format-arguments (list ,args-tail)))))))
1237            (declare (ignorable ,args-tail .dummy0.))
1238            ,@body)))))
1239
1240 (defun get-key-arg-tail (keyword list)
1241   (loop for (key . tail) on list by #'cddr
1242         when (null tail) do
1243           ;; FIXME: Do we want to export this symbol? Or maybe use an
1244           ;; (ERROR 'SIMPLE-PROGRAM-ERROR) form?
1245           (sb-c::%odd-key-args-error)
1246         when (eq key keyword)
1247           return tail))
1248
1249 (defun walk-method-lambda (method-lambda required-parameters env slots calls)
1250   (let ((call-next-method-p nil)   ; flag indicating that CALL-NEXT-METHOD
1251                                    ; should be in the method definition
1252         (closurep nil)             ; flag indicating that #'CALL-NEXT-METHOD
1253                                    ; was seen in the body of a method
1254         (next-method-p-p nil)      ; flag indicating that NEXT-METHOD-P
1255                                    ; should be in the method definition
1256         (setq-p nil))
1257     (flet ((walk-function (form context env)
1258              (cond ((not (eq context :eval)) form)
1259                    ;; FIXME: Jumping to a conclusion from the way it's used
1260                    ;; above, perhaps CONTEXT should be called SITUATION
1261                    ;; (after the term used in the ANSI specification of
1262                    ;; EVAL-WHEN) and given modern ANSI keyword values
1263                    ;; like :LOAD-TOPLEVEL.
1264                    ((not (listp form)) form)
1265                    ((eq (car form) 'call-next-method)
1266                     (setq call-next-method-p t)
1267                     form)
1268                    ((eq (car form) 'next-method-p)
1269                     (setq next-method-p-p t)
1270                     form)
1271                    ((memq (car form) '(setq multiple-value-setq))
1272                     ;; FIXME: this is possibly a little strong as
1273                     ;; conditions go.  Ideally we would want to detect
1274                     ;; which, if any, of the method parameters are
1275                     ;; being set, and communicate that information to
1276                     ;; e.g. SPLIT-DECLARATIONS.  However, the brute
1277                     ;; force method doesn't really cost much; a little
1278                     ;; loss of discrimination over IGNORED variables
1279                     ;; should be all.  -- CSR, 2004-07-01
1280                     (setq setq-p t)
1281                     form)
1282                    ((and (eq (car form) 'function)
1283                          (cond ((eq (cadr form) 'call-next-method)
1284                                 (setq call-next-method-p t)
1285                                 (setq closurep t)
1286                                 form)
1287                                ((eq (cadr form) 'next-method-p)
1288                                 (setq next-method-p-p t)
1289                                 (setq closurep t)
1290                                 form)
1291                                (t nil))))
1292                    ((and (memq (car form)
1293                                '(slot-value set-slot-value slot-boundp))
1294                          (constantp (caddr form)))
1295                      (let ((parameter (can-optimize-access form
1296                                                            required-parameters
1297                                                            env)))
1298                       (let ((fun (ecase (car form)
1299                                    (slot-value #'optimize-slot-value)
1300                                    (set-slot-value #'optimize-set-slot-value)
1301                                    (slot-boundp #'optimize-slot-boundp))))
1302                         (funcall fun slots parameter form))))
1303                    ((and (eq (car form) 'apply)
1304                          (consp (cadr form))
1305                          (eq (car (cadr form)) 'function)
1306                          (generic-function-name-p (cadr (cadr form))))
1307                     (optimize-generic-function-call
1308                      form required-parameters env slots calls))
1309                    ((generic-function-name-p (car form))
1310                     (optimize-generic-function-call
1311                      form required-parameters env slots calls))
1312                    (t form))))
1313
1314       (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1315         (values walked-lambda
1316                 call-next-method-p
1317                 closurep
1318                 next-method-p-p
1319                 setq-p)))))
1320
1321 (defun generic-function-name-p (name)
1322   (and (legal-fun-name-p name)
1323        (gboundp name)
1324        (if (eq *boot-state* 'complete)
1325            (standard-generic-function-p (gdefinition name))
1326            (funcallable-instance-p (gdefinition name)))))
1327 \f
1328 (defvar *method-function-plist* (make-hash-table :test 'eq))
1329 (defvar *mf1* nil)
1330 (defvar *mf1p* nil)
1331 (defvar *mf1cp* nil)
1332 (defvar *mf2* nil)
1333 (defvar *mf2p* nil)
1334 (defvar *mf2cp* nil)
1335
1336 (defun method-function-plist (method-function)
1337   (unless (eq method-function *mf1*)
1338     (rotatef *mf1* *mf2*)
1339     (rotatef *mf1p* *mf2p*)
1340     (rotatef *mf1cp* *mf2cp*))
1341   (unless (or (eq method-function *mf1*) (null *mf1cp*))
1342     (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1343   (unless (eq method-function *mf1*)
1344     (setf *mf1* method-function
1345           *mf1cp* nil
1346           *mf1p* (gethash method-function *method-function-plist*)))
1347   *mf1p*)
1348
1349 (defun (setf method-function-plist)
1350     (val method-function)
1351   (unless (eq method-function *mf1*)
1352     (rotatef *mf1* *mf2*)
1353     (rotatef *mf1cp* *mf2cp*)
1354     (rotatef *mf1p* *mf2p*))
1355   (unless (or (eq method-function *mf1*) (null *mf1cp*))
1356     (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1357   (setf *mf1* method-function
1358         *mf1cp* t
1359         *mf1p* val))
1360
1361 (defun method-function-get (method-function key &optional default)
1362   (getf (method-function-plist method-function) key default))
1363
1364 (defun (setf method-function-get)
1365     (val method-function key)
1366   (setf (getf (method-function-plist method-function) key) val))
1367
1368 (defun method-function-pv-table (method-function)
1369   (method-function-get method-function :pv-table))
1370
1371 (defun method-function-method (method-function)
1372   (method-function-get method-function :method))
1373
1374 (defun method-function-needs-next-methods-p (method-function)
1375   (method-function-get method-function :needs-next-methods-p t))
1376 \f
1377 (defmacro method-function-closure-generator (method-function)
1378   `(method-function-get ,method-function 'closure-generator))
1379
1380 (defun load-defmethod
1381     (class name quals specls ll initargs &optional pv-table-symbol)
1382   (setq initargs (copy-tree initargs))
1383   (let ((method-spec (or (getf initargs :method-spec)
1384                          (make-method-spec name quals specls))))
1385     (setf (getf initargs :method-spec) method-spec)
1386     (load-defmethod-internal class name quals specls
1387                              ll initargs pv-table-symbol)))
1388
1389 (defun load-defmethod-internal
1390     (method-class gf-spec qualifiers specializers lambda-list
1391                   initargs pv-table-symbol)
1392   (when pv-table-symbol
1393     (setf (getf (getf initargs :plist) :pv-table-symbol)
1394           pv-table-symbol))
1395   (when (and (eq *boot-state* 'complete)
1396              (fboundp gf-spec))
1397     (let* ((gf (fdefinition gf-spec))
1398            (method (and (generic-function-p gf)
1399                         (generic-function-methods gf)
1400                         (find-method gf
1401                                      qualifiers
1402                                      (parse-specializers specializers)
1403                                      nil))))
1404       (when method
1405         (style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1406                     gf-spec qualifiers specializers))))
1407   (let ((method (apply #'add-named-method
1408                        gf-spec qualifiers specializers lambda-list
1409                        :definition-source `((defmethod ,gf-spec
1410                                                 ,@qualifiers
1411                                               ,specializers)
1412                                             ,*load-pathname*)
1413                        initargs)))
1414     (unless (or (eq method-class 'standard-method)
1415                 (eq (find-class method-class nil) (class-of method)))
1416       ;; FIXME: should be STYLE-WARNING?
1417       (format *error-output*
1418               "~&At the time the method with qualifiers ~:S and~%~
1419                specializers ~:S on the generic function ~S~%~
1420                was compiled, the method-class for that generic function was~%~
1421                ~S. But, the method class is now ~S, this~%~
1422                may mean that this method was compiled improperly.~%"
1423               qualifiers specializers gf-spec
1424               method-class (class-name (class-of method))))
1425     method))
1426
1427 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1428   `(slow-method ,gf-spec ,@qualifiers ,unparsed-specializers))
1429
1430 (defun initialize-method-function (initargs &optional return-function-p method)
1431   (let* ((mf (getf initargs :function))
1432          (method-spec (getf initargs :method-spec))
1433          (plist (getf initargs :plist))
1434          (pv-table-symbol (getf plist :pv-table-symbol))
1435          (pv-table nil)
1436          (mff (getf initargs :fast-function)))
1437     (flet ((set-mf-property (p v)
1438              (when mf
1439                (setf (method-function-get mf p) v))
1440              (when mff
1441                (setf (method-function-get mff p) v))))
1442       (when method-spec
1443         (when mf
1444           (setq mf (set-fun-name mf method-spec)))
1445         (when mff
1446           (let ((name `(fast-method ,@(cdr method-spec))))
1447             (set-fun-name mff name)
1448             (unless mf
1449               (set-mf-property :name name)))))
1450       (when plist
1451         (let ((snl (getf plist :slot-name-lists))
1452               (cl (getf plist :call-list)))
1453           (when (or snl cl)
1454             (setq pv-table (intern-pv-table :slot-name-lists snl
1455                                             :call-list cl))
1456             (when pv-table (set pv-table-symbol pv-table))
1457             (set-mf-property :pv-table pv-table)))
1458         (loop (when (null plist) (return nil))
1459               (set-mf-property (pop plist) (pop plist)))
1460         (when method
1461           (set-mf-property :method method))
1462         (when return-function-p
1463           (or mf (method-function-from-fast-function mff)))))))
1464 \f
1465 (defun analyze-lambda-list (lambda-list)
1466   (flet (;; FIXME: Is this redundant with SB-C::MAKE-KEYWORD-FOR-ARG?
1467          (parse-key-arg (arg)
1468            (if (listp arg)
1469                (if (listp (car arg))
1470                    (caar arg)
1471                    (keywordicate (car arg)))
1472                (keywordicate arg))))
1473     (let ((nrequired 0)
1474           (noptional 0)
1475           (keysp nil)
1476           (restp nil)
1477           (nrest 0)
1478           (allow-other-keys-p nil)
1479           (keywords ())
1480           (keyword-parameters ())
1481           (state 'required))
1482       (dolist (x lambda-list)
1483         (if (memq x lambda-list-keywords)
1484             (case x
1485               (&optional         (setq state 'optional))
1486               (&key              (setq keysp t
1487                                        state 'key))
1488               (&allow-other-keys (setq allow-other-keys-p t))
1489               (&rest             (setq restp t
1490                                        state 'rest))
1491               (&aux           (return t))
1492               (otherwise
1493                 (error "encountered the non-standard lambda list keyword ~S"
1494                        x)))
1495             (ecase state
1496               (required  (incf nrequired))
1497               (optional  (incf noptional))
1498               (key       (push (parse-key-arg x) keywords)
1499                          (push x keyword-parameters))
1500               (rest      (incf nrest)))))
1501       (when (and restp (zerop nrest))
1502         (error "Error in lambda-list:~%~
1503                 After &REST, a DEFGENERIC lambda-list ~
1504                 must be followed by at least one variable."))
1505       (values nrequired noptional keysp restp allow-other-keys-p
1506               (reverse keywords)
1507               (reverse keyword-parameters)))))
1508
1509 (defun keyword-spec-name (x)
1510   (let ((key (if (atom x) x (car x))))
1511     (if (atom key)
1512         (keywordicate key)
1513         (car key))))
1514
1515 (defun ftype-declaration-from-lambda-list (lambda-list name)
1516   (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1517                                   keywords keyword-parameters)
1518       (analyze-lambda-list lambda-list)
1519     (declare (ignore keyword-parameters))
1520     (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1521            (old-ftype (if (fun-type-p old) old nil))
1522            (old-restp (and old-ftype (fun-type-rest old-ftype)))
1523            (old-keys (and old-ftype
1524                           (mapcar #'key-info-name
1525                                   (fun-type-keywords
1526                                    old-ftype))))
1527            (old-keysp (and old-ftype (fun-type-keyp old-ftype)))
1528            (old-allowp (and old-ftype
1529                             (fun-type-allowp old-ftype)))
1530            (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1531       `(function ,(append (make-list nrequired :initial-element t)
1532                           (when (plusp noptional)
1533                             (append '(&optional)
1534                                     (make-list noptional :initial-element t)))
1535                           (when (or restp old-restp)
1536                             '(&rest t))
1537                           (when (or keysp old-keysp)
1538                             (append '(&key)
1539                                     (mapcar (lambda (key)
1540                                               `(,key t))
1541                                             keywords)
1542                                     (when (or allow-other-keys-p old-allowp)
1543                                       '(&allow-other-keys)))))
1544                  *))))
1545
1546 (defun defgeneric-declaration (spec lambda-list)
1547   `(ftype ,(ftype-declaration-from-lambda-list lambda-list spec) ,spec))
1548 \f
1549 ;;;; early generic function support
1550
1551 (defvar *!early-generic-functions* ())
1552
1553 (defun ensure-generic-function (fun-name
1554                                 &rest all-keys
1555                                 &key environment
1556                                 &allow-other-keys)
1557   (declare (ignore environment))
1558   (let ((existing (and (gboundp fun-name)
1559                        (gdefinition fun-name))))
1560     (if (and existing
1561              (eq *boot-state* 'complete)
1562              (null (generic-function-p existing)))
1563         (generic-clobbers-function fun-name)
1564         (apply #'ensure-generic-function-using-class
1565                existing fun-name all-keys))))
1566
1567 (defun generic-clobbers-function (fun-name)
1568   (error 'simple-program-error
1569          :format-control "~S already names an ordinary function or a macro."
1570          :format-arguments (list fun-name)))
1571
1572 (defvar *sgf-wrapper*
1573   (boot-make-wrapper (early-class-size 'standard-generic-function)
1574                      'standard-generic-function))
1575
1576 (defvar *sgf-slots-init*
1577   (mapcar (lambda (canonical-slot)
1578             (if (memq (getf canonical-slot :name) '(arg-info source))
1579                 +slot-unbound+
1580                 (let ((initfunction (getf canonical-slot :initfunction)))
1581                   (if initfunction
1582                       (funcall initfunction)
1583                       +slot-unbound+))))
1584           (early-collect-inheritance 'standard-generic-function)))
1585
1586 (defvar *sgf-method-class-index*
1587   (!bootstrap-slot-index 'standard-generic-function 'method-class))
1588
1589 (defun early-gf-p (x)
1590   (and (fsc-instance-p x)
1591        (eq (clos-slots-ref (get-slots x) *sgf-method-class-index*)
1592            +slot-unbound+)))
1593
1594 (defvar *sgf-methods-index*
1595   (!bootstrap-slot-index 'standard-generic-function 'methods))
1596
1597 (defmacro early-gf-methods (gf)
1598   `(clos-slots-ref (get-slots ,gf) *sgf-methods-index*))
1599
1600 (defvar *sgf-arg-info-index*
1601   (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1602
1603 (defmacro early-gf-arg-info (gf)
1604   `(clos-slots-ref (get-slots ,gf) *sgf-arg-info-index*))
1605
1606 (defvar *sgf-dfun-state-index*
1607   (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1608
1609 (defstruct (arg-info
1610             (:conc-name nil)
1611             (:constructor make-arg-info ())
1612             (:copier nil))
1613   (arg-info-lambda-list :no-lambda-list)
1614   arg-info-precedence
1615   arg-info-metatypes
1616   arg-info-number-optional
1617   arg-info-key/rest-p
1618   arg-info-keys   ;nil        no &KEY or &REST allowed
1619                   ;(k1 k2 ..) Each method must accept these &KEY arguments.
1620                   ;T          must have &KEY or &REST
1621
1622   gf-info-simple-accessor-type ; nil, reader, writer, boundp
1623   (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1624
1625   gf-info-static-c-a-m-emf
1626   (gf-info-c-a-m-emf-std-p t)
1627   gf-info-fast-mf-p)
1628
1629 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1630
1631 (defun arg-info-valid-p (arg-info)
1632   (not (null (arg-info-number-optional arg-info))))
1633
1634 (defun arg-info-applyp (arg-info)
1635   (or (plusp (arg-info-number-optional arg-info))
1636       (arg-info-key/rest-p arg-info)))
1637
1638 (defun arg-info-number-required (arg-info)
1639   (length (arg-info-metatypes arg-info)))
1640
1641 (defun arg-info-nkeys (arg-info)
1642   (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1643
1644 (defun create-gf-lambda-list (lambda-list)
1645   ;;; Create a gf lambda list from a method lambda list
1646   (loop for x in lambda-list
1647         collect (if (consp x) (list (car x)) x)
1648         if (eq x '&key) do (loop-finish)))
1649
1650 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1651                         argument-precedence-order)
1652   (let* ((arg-info (if (eq *boot-state* 'complete)
1653                        (gf-arg-info gf)
1654                        (early-gf-arg-info gf)))
1655          (methods (if (eq *boot-state* 'complete)
1656                       (generic-function-methods gf)
1657                       (early-gf-methods gf)))
1658          (was-valid-p (integerp (arg-info-number-optional arg-info)))
1659          (first-p (and new-method (null (cdr methods)))))
1660     (when (and (not lambda-list-p) methods)
1661       (setq lambda-list (gf-lambda-list gf)))
1662     (when (or lambda-list-p
1663               (and first-p
1664                    (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1665       (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1666           (analyze-lambda-list lambda-list)
1667         (when (and methods (not first-p))
1668           (let ((gf-nreq (arg-info-number-required arg-info))
1669                 (gf-nopt (arg-info-number-optional arg-info))
1670                 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1671             (unless (and (= nreq gf-nreq)
1672                          (= nopt gf-nopt)
1673                          (eq (or keysp restp) gf-key/rest-p))
1674               (error "The lambda-list ~S is incompatible with ~
1675                      existing methods of ~S."
1676                      lambda-list gf))))
1677         (setf (arg-info-lambda-list arg-info)
1678               (if lambda-list-p
1679                   lambda-list
1680                    (create-gf-lambda-list lambda-list)))
1681         (when (or lambda-list-p argument-precedence-order
1682                   (null (arg-info-precedence arg-info)))
1683           (setf (arg-info-precedence arg-info)
1684                 (compute-precedence lambda-list nreq argument-precedence-order)))
1685         (setf (arg-info-metatypes arg-info) (make-list nreq))
1686         (setf (arg-info-number-optional arg-info) nopt)
1687         (setf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1688         (setf (arg-info-keys arg-info)
1689               (if lambda-list-p
1690                   (if allow-other-keys-p t keywords)
1691                   (arg-info-key/rest-p arg-info)))))
1692     (when new-method
1693       (check-method-arg-info gf arg-info new-method))
1694     (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1695     arg-info))
1696
1697 (defun check-method-arg-info (gf arg-info method)
1698   (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1699       (analyze-lambda-list (if (consp method)
1700                                (early-method-lambda-list method)
1701                                (method-lambda-list method)))
1702     (flet ((lose (string &rest args)
1703              (error 'simple-program-error
1704                     :format-control "~@<attempt to add the method~2I~_~S~I~_~
1705                                      to the generic function~2I~_~S;~I~_~
1706                                      but ~?~:>"
1707                     :format-arguments (list method gf string args)))
1708            (comparison-description (x y)
1709              (if (> x y) "more" "fewer")))
1710       (let ((gf-nreq (arg-info-number-required arg-info))
1711             (gf-nopt (arg-info-number-optional arg-info))
1712             (gf-key/rest-p (arg-info-key/rest-p arg-info))
1713             (gf-keywords (arg-info-keys arg-info)))
1714         (unless (= nreq gf-nreq)
1715           (lose
1716            "the method has ~A required arguments than the generic function."
1717            (comparison-description nreq gf-nreq)))
1718         (unless (= nopt gf-nopt)
1719           (lose
1720            "the method has ~A optional arguments than the generic function."
1721            (comparison-description nopt gf-nopt)))
1722         (unless (eq (or keysp restp) gf-key/rest-p)
1723           (lose
1724            "the method and generic function differ in whether they accept~_~
1725             &REST or &KEY arguments."))
1726         (when (consp gf-keywords)
1727           (unless (or (and restp (not keysp))
1728                       allow-other-keys-p
1729                       (every (lambda (k) (memq k keywords)) gf-keywords))
1730             (lose "the method does not accept each of the &KEY arguments~2I~_~
1731                    ~S."
1732                   gf-keywords)))))))
1733
1734 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1735   (let* ((existing-p (and methods (cdr methods) new-method))
1736          (nreq (length (arg-info-metatypes arg-info)))
1737          (metatypes (if existing-p
1738                         (arg-info-metatypes arg-info)
1739                         (make-list nreq)))
1740          (type (if existing-p
1741                    (gf-info-simple-accessor-type arg-info)
1742                    nil)))
1743     (when (arg-info-valid-p arg-info)
1744       (dolist (method (if new-method (list new-method) methods))
1745         (let* ((specializers (if (or (eq *boot-state* 'complete)
1746                                      (not (consp method)))
1747                                  (method-specializers method)
1748                                  (early-method-specializers method t)))
1749                (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1750                           (class-of method)
1751                           (early-method-class method)))
1752                (new-type (when (and class
1753                                     (or (not (eq *boot-state* 'complete))
1754                                         (eq (generic-function-method-combination gf)
1755                                             *standard-method-combination*)))
1756                            (cond ((eq class *the-class-standard-reader-method*)
1757                                   'reader)
1758                                  ((eq class *the-class-standard-writer-method*)
1759                                   'writer)
1760                                  ((eq class *the-class-standard-boundp-method*)
1761                                   'boundp)))))
1762           (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1763           (setq type (cond ((null type) new-type)
1764                            ((eq type new-type) type)
1765                            (t nil)))))
1766       (setf (arg-info-metatypes arg-info) metatypes)
1767       (setf (gf-info-simple-accessor-type arg-info) type)))
1768   (when (or (not was-valid-p) first-p)
1769     (multiple-value-bind (c-a-m-emf std-p)
1770         (if (early-gf-p gf)
1771             (values t t)
1772             (compute-applicable-methods-emf gf))
1773       (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1774       (setf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1775       (unless (gf-info-c-a-m-emf-std-p arg-info)
1776         (setf (gf-info-simple-accessor-type arg-info) t))))
1777   (unless was-valid-p
1778     (let ((name (if (eq *boot-state* 'complete)
1779                     (generic-function-name gf)
1780                     (!early-gf-name gf))))
1781       (setf (gf-precompute-dfun-and-emf-p arg-info)
1782             (cond
1783               ((and (consp name)
1784                     (member (car name)
1785                             *internal-pcl-generalized-fun-name-symbols*))
1786                 nil)
1787               (t (let* ((symbol (fun-name-block-name name))
1788                         (package (symbol-package symbol)))
1789                    (and (or (eq package *pcl-package*)
1790                             (memq package (package-use-list *pcl-package*)))
1791                         ;; FIXME: this test will eventually be
1792                         ;; superseded by the *internal-pcl...* test,
1793                         ;; above.  While we are in a process of
1794                         ;; transition, however, it should probably
1795                         ;; remain.
1796                         (not (find #\Space (symbol-name symbol))))))))))
1797   (setf (gf-info-fast-mf-p arg-info)
1798         (or (not (eq *boot-state* 'complete))
1799             (let* ((method-class (generic-function-method-class gf))
1800                    (methods (compute-applicable-methods
1801                              #'make-method-lambda
1802                              (list gf (class-prototype method-class)
1803                                    '(lambda) nil))))
1804               (and methods (null (cdr methods))
1805                    (let ((specls (method-specializers (car methods))))
1806                      (and (classp (car specls))
1807                           (eq 'standard-generic-function
1808                               (class-name (car specls)))
1809                           (classp (cadr specls))
1810                           (eq 'standard-method
1811                               (class-name (cadr specls)))))))))
1812   arg-info)
1813
1814 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
1815 ;;;
1816 ;;; The STATIC-SLOTS field of the funcallable instances used as early
1817 ;;; generic functions is used to store the early methods and early
1818 ;;; discriminator code for the early generic function. The static
1819 ;;; slots field of the fins contains a list whose:
1820 ;;;    CAR    -   a list of the early methods on this early gf
1821 ;;;    CADR   -   the early discriminator code for this method
1822 (defun ensure-generic-function-using-class (existing spec &rest keys
1823                                             &key (lambda-list nil
1824                                                               lambda-list-p)
1825                                             argument-precedence-order
1826                                             &allow-other-keys)
1827   (declare (ignore keys))
1828   (cond ((and existing (early-gf-p existing))
1829          (when lambda-list-p
1830            (set-arg-info existing :lambda-list lambda-list))
1831          existing)
1832         ((assoc spec *!generic-function-fixups* :test #'equal)
1833          (if existing
1834              (make-early-gf spec lambda-list lambda-list-p existing
1835                             argument-precedence-order)
1836              (error "The function ~S is not already defined." spec)))
1837         (existing
1838          (error "~S should be on the list ~S."
1839                 spec
1840                 '*!generic-function-fixups*))
1841         (t
1842          (pushnew spec *!early-generic-functions* :test #'equal)
1843          (make-early-gf spec lambda-list lambda-list-p nil
1844                         argument-precedence-order))))
1845
1846 (defun make-early-gf (spec &optional lambda-list lambda-list-p
1847                       function argument-precedence-order)
1848   (let ((fin (allocate-funcallable-instance *sgf-wrapper* *sgf-slots-init*)))
1849     (set-funcallable-instance-function
1850      fin
1851      (or function
1852          (if (eq spec 'print-object)
1853              #'(instance-lambda (instance stream)
1854                  (print-unreadable-object (instance stream :identity t)
1855                    (format stream "std-instance")))
1856              #'(instance-lambda (&rest args)
1857                  (declare (ignore args))
1858                  (error "The function of the funcallable-instance ~S~
1859                          has not been set." fin)))))
1860     (setf (gdefinition spec) fin)
1861     (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
1862     (!bootstrap-set-slot 'standard-generic-function
1863                          fin
1864                          'source
1865                          *load-pathname*)
1866     (set-fun-name fin spec)
1867     (let ((arg-info (make-arg-info)))
1868       (setf (early-gf-arg-info fin) arg-info)
1869       (when lambda-list-p
1870         (proclaim (defgeneric-declaration spec lambda-list))
1871         (if argument-precedence-order
1872             (set-arg-info fin
1873                           :lambda-list lambda-list
1874                           :argument-precedence-order argument-precedence-order)
1875             (set-arg-info fin :lambda-list lambda-list))))
1876     fin))
1877
1878 (defun set-dfun (gf &optional dfun cache info)
1879   (when cache
1880     (setf (cache-owner cache) gf))
1881   (let ((new-state (if (and dfun (or cache info))
1882                        (list* dfun cache info)
1883                        dfun)))
1884     (if (eq *boot-state* 'complete)
1885         (setf (gf-dfun-state gf) new-state)
1886         (setf (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*)
1887               new-state)))
1888   dfun)
1889
1890 (defun gf-dfun-cache (gf)
1891   (let ((state (if (eq *boot-state* 'complete)
1892                    (gf-dfun-state gf)
1893                    (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1894     (typecase state
1895       (function nil)
1896       (cons (cadr state)))))
1897
1898 (defun gf-dfun-info (gf)
1899   (let ((state (if (eq *boot-state* 'complete)
1900                    (gf-dfun-state gf)
1901                    (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1902     (typecase state
1903       (function nil)
1904       (cons (cddr state)))))
1905
1906 (defvar *sgf-name-index*
1907   (!bootstrap-slot-index 'standard-generic-function 'name))
1908
1909 (defun !early-gf-name (gf)
1910   (clos-slots-ref (get-slots gf) *sgf-name-index*))
1911
1912 (defun gf-lambda-list (gf)
1913   (let ((arg-info (if (eq *boot-state* 'complete)
1914                       (gf-arg-info gf)
1915                       (early-gf-arg-info gf))))
1916     (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
1917         (let ((methods (if (eq *boot-state* 'complete)
1918                            (generic-function-methods gf)
1919                            (early-gf-methods gf))))
1920           (if (null methods)
1921               (progn
1922                 (warn "no way to determine the lambda list for ~S" gf)
1923                 nil)
1924               (let* ((method (car (last methods)))
1925                      (ll (if (consp method)
1926                              (early-method-lambda-list method)
1927                              (method-lambda-list method))))
1928                 (create-gf-lambda-list ll))))
1929         (arg-info-lambda-list arg-info))))
1930
1931 (defmacro real-ensure-gf-internal (gf-class all-keys env)
1932   `(progn
1933      (cond ((symbolp ,gf-class)
1934             (setq ,gf-class (find-class ,gf-class t ,env)))
1935            ((classp ,gf-class))
1936            (t
1937             (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
1938                     class nor a symbol that names a class."
1939                    ,gf-class)))
1940      (remf ,all-keys :generic-function-class)
1941      (remf ,all-keys :environment)
1942      (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
1943        (unless (eq combin '.shes-not-there.)
1944          (setf (getf ,all-keys :method-combination)
1945                (find-method-combination (class-prototype ,gf-class)
1946                                         (car combin)
1947                                         (cdr combin)))))
1948     (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
1949       (unless (eq method-class '.shes-not-there.)
1950         (setf (getf ,all-keys :method-class)
1951               (find-class method-class t ,env))))))
1952
1953 (defun real-ensure-gf-using-class--generic-function
1954        (existing
1955         fun-name
1956         &rest all-keys
1957         &key environment (lambda-list nil lambda-list-p)
1958              (generic-function-class 'standard-generic-function gf-class-p)
1959         &allow-other-keys)
1960   (real-ensure-gf-internal generic-function-class all-keys environment)
1961   (unless (or (null gf-class-p)
1962               (eq (class-of existing) generic-function-class))
1963     (change-class existing generic-function-class))
1964   (prog1
1965       (apply #'reinitialize-instance existing all-keys)
1966     (when lambda-list-p
1967       (proclaim (defgeneric-declaration fun-name lambda-list)))))
1968
1969 (defun real-ensure-gf-using-class--null
1970        (existing
1971         fun-name
1972         &rest all-keys
1973         &key environment (lambda-list nil lambda-list-p)
1974              (generic-function-class 'standard-generic-function)
1975         &allow-other-keys)
1976   (declare (ignore existing))
1977   (real-ensure-gf-internal generic-function-class all-keys environment)
1978   (prog1
1979       (setf (gdefinition fun-name)
1980             (apply #'make-instance generic-function-class
1981                    :name fun-name all-keys))
1982     (when lambda-list-p
1983       (proclaim (defgeneric-declaration fun-name lambda-list)))))
1984 \f
1985 (defun get-generic-fun-info (gf)
1986   ;; values   nreq applyp metatypes nkeys arg-info
1987   (multiple-value-bind (applyp metatypes arg-info)
1988       (let* ((arg-info (if (early-gf-p gf)
1989                            (early-gf-arg-info gf)
1990                            (gf-arg-info gf)))
1991              (metatypes (arg-info-metatypes arg-info)))
1992         (values (arg-info-applyp arg-info)
1993                 metatypes
1994                 arg-info))
1995     (values (length metatypes) applyp metatypes
1996             (count-if (lambda (x) (neq x t)) metatypes)
1997             arg-info)))
1998
1999 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
2000                             &optional slot-name)
2001   (initialize-method-function initargs)
2002   (let ((parsed ())
2003         (unparsed ()))
2004     ;; Figure out whether we got class objects or class names as the
2005     ;; specializers and set parsed and unparsed appropriately. If we
2006     ;; got class objects, then we can compute unparsed, but if we got
2007     ;; class names we don't try to compute parsed.
2008     ;;
2009     ;; Note that the use of not symbolp in this call to every should be
2010     ;; read as 'classp' we can't use classp itself because it doesn't
2011     ;; exist yet.
2012     (if (every (lambda (s) (not (symbolp s))) specializers)
2013         (setq parsed specializers
2014               unparsed (mapcar (lambda (s)
2015                                  (if (eq s t) t (class-name s)))
2016                                specializers))
2017         (setq unparsed specializers
2018               parsed ()))
2019     (list :early-method           ;This is an early method dammit!
2020
2021           (getf initargs :function)
2022           (getf initargs :fast-function)
2023
2024           parsed                  ;The parsed specializers. This is used
2025                                   ;by early-method-specializers to cache
2026                                   ;the parse. Note that this only comes
2027                                   ;into play when there is more than one
2028                                   ;early method on an early gf.
2029
2030           (list class        ;A list to which real-make-a-method
2031                 qualifiers      ;can be applied to make a real method
2032                 arglist    ;corresponding to this early one.
2033                 unparsed
2034                 initargs
2035                 doc
2036                 slot-name))))
2037
2038 (defun real-make-a-method
2039        (class qualifiers lambda-list specializers initargs doc
2040         &optional slot-name)
2041   (setq specializers (parse-specializers specializers))
2042   (apply #'make-instance class
2043          :qualifiers qualifiers
2044          :lambda-list lambda-list
2045          :specializers specializers
2046          :documentation doc
2047          :slot-name slot-name
2048          :allow-other-keys t
2049          initargs))
2050
2051 (defun early-method-function (early-method)
2052   (values (cadr early-method) (caddr early-method)))
2053
2054 (defun early-method-class (early-method)
2055   (find-class (car (fifth early-method))))
2056
2057 (defun early-method-standard-accessor-p (early-method)
2058   (let ((class (first (fifth early-method))))
2059     (or (eq class 'standard-reader-method)
2060         (eq class 'standard-writer-method)
2061         (eq class 'standard-boundp-method))))
2062
2063 (defun early-method-standard-accessor-slot-name (early-method)
2064   (seventh (fifth early-method)))
2065
2066 ;;; Fetch the specializers of an early method. This is basically just
2067 ;;; a simple accessor except that when the second argument is t, this
2068 ;;; converts the specializers from symbols into class objects. The
2069 ;;; class objects are cached in the early method, this makes
2070 ;;; bootstrapping faster because the class objects only have to be
2071 ;;; computed once.
2072 ;;;
2073 ;;; NOTE:
2074 ;;;  The second argument should only be passed as T by
2075 ;;;  early-lookup-method. This is to implement the rule that only when
2076 ;;;  there is more than one early method on a generic function is the
2077 ;;;  conversion from class names to class objects done. This
2078 ;;;  corresponds to the fact that we are only allowed to have one
2079 ;;;  method on any generic function up until the time classes exist.
2080 (defun early-method-specializers (early-method &optional objectsp)
2081   (if (and (listp early-method)
2082            (eq (car early-method) :early-method))
2083       (cond ((eq objectsp t)
2084              (or (fourth early-method)
2085                  (setf (fourth early-method)
2086                        (mapcar #'find-class (cadddr (fifth early-method))))))
2087             (t
2088              (cadddr (fifth early-method))))
2089       (error "~S is not an early-method." early-method)))
2090
2091 (defun early-method-qualifiers (early-method)
2092   (cadr (fifth early-method)))
2093
2094 (defun early-method-lambda-list (early-method)
2095   (caddr (fifth early-method)))
2096
2097 (defun early-add-named-method (generic-function-name
2098                                qualifiers
2099                                specializers
2100                                arglist
2101                                &rest initargs)
2102   (let* ((gf (ensure-generic-function generic-function-name))
2103          (existing
2104            (dolist (m (early-gf-methods gf))
2105              (when (and (equal (early-method-specializers m) specializers)
2106                         (equal (early-method-qualifiers m) qualifiers))
2107                (return m))))
2108          (new (make-a-method 'standard-method
2109                              qualifiers
2110                              arglist
2111                              specializers
2112                              initargs
2113                              ())))
2114     (when existing (remove-method gf existing))
2115     (add-method gf new)))
2116
2117 ;;; This is the early version of ADD-METHOD. Later this will become a
2118 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2119 ;;; special knowledge about ADD-METHOD.
2120 (defun add-method (generic-function method)
2121   (when (not (fsc-instance-p generic-function))
2122     (error "Early ADD-METHOD didn't get a funcallable instance."))
2123   (when (not (and (listp method) (eq (car method) :early-method)))
2124     (error "Early ADD-METHOD didn't get an early method."))
2125   (push method (early-gf-methods generic-function))
2126   (set-arg-info generic-function :new-method method)
2127   (unless (assoc (!early-gf-name generic-function)
2128                  *!generic-function-fixups*
2129                  :test #'equal)
2130     (update-dfun generic-function)))
2131
2132 ;;; This is the early version of REMOVE-METHOD. See comments on
2133 ;;; the early version of ADD-METHOD.
2134 (defun remove-method (generic-function method)
2135   (when (not (fsc-instance-p generic-function))
2136     (error "An early remove-method didn't get a funcallable instance."))
2137   (when (not (and (listp method) (eq (car method) :early-method)))
2138     (error "An early remove-method didn't get an early method."))
2139   (setf (early-gf-methods generic-function)
2140         (remove method (early-gf-methods generic-function)))
2141   (set-arg-info generic-function)
2142   (unless (assoc (!early-gf-name generic-function)
2143                  *!generic-function-fixups*
2144                  :test #'equal)
2145     (update-dfun generic-function)))
2146
2147 ;;; This is the early version of GET-METHOD. See comments on the early
2148 ;;; version of ADD-METHOD.
2149 (defun get-method (generic-function qualifiers specializers
2150                                     &optional (errorp t))
2151   (if (early-gf-p generic-function)
2152       (or (dolist (m (early-gf-methods generic-function))
2153             (when (and (or (equal (early-method-specializers m nil)
2154                                   specializers)
2155                            (equal (early-method-specializers m t)
2156                                   specializers))
2157                        (equal (early-method-qualifiers m) qualifiers))
2158               (return m)))
2159           (if errorp
2160               (error "can't get early method")
2161               nil))
2162       (real-get-method generic-function qualifiers specializers errorp)))
2163
2164 (defun !fix-early-generic-functions ()
2165   (let ((accessors nil))
2166     ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2167     ;; FIX-EARLY-GENERIC-FUNCTIONS.
2168     (dolist (early-gf-spec *!early-generic-functions*)
2169       (when (every #'early-method-standard-accessor-p
2170                    (early-gf-methods (gdefinition early-gf-spec)))
2171         (push early-gf-spec accessors)))
2172     (dolist (spec (nconc accessors
2173                          '(accessor-method-slot-name
2174                            generic-function-methods
2175                            method-specializers
2176                            specializerp
2177                            specializer-type
2178                            specializer-class
2179                            slot-definition-location
2180                            slot-definition-name
2181                            class-slots
2182                            gf-arg-info
2183                            class-precedence-list
2184                            slot-boundp-using-class
2185                            (setf slot-value-using-class)
2186                            slot-value-using-class
2187                            structure-class-p
2188                            standard-class-p
2189                            funcallable-standard-class-p
2190                            specializerp)))
2191       (/show spec)
2192       (setq *!early-generic-functions*
2193             (cons spec
2194                   (delete spec *!early-generic-functions* :test #'equal))))
2195
2196     (dolist (early-gf-spec *!early-generic-functions*)
2197       (/show early-gf-spec)
2198       (let* ((gf (gdefinition early-gf-spec))
2199              (methods (mapcar (lambda (early-method)
2200                                 (let ((args (copy-list (fifth
2201                                                         early-method))))
2202                                   (setf (fourth args)
2203                                         (early-method-specializers
2204                                          early-method t))
2205                                   (apply #'real-make-a-method args)))
2206                               (early-gf-methods gf))))
2207         (setf (generic-function-method-class gf) *the-class-standard-method*)
2208         (setf (generic-function-method-combination gf)
2209               *standard-method-combination*)
2210         (set-methods gf methods)))
2211
2212     (dolist (fn *!early-functions*)
2213       (/show fn)
2214       (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2215
2216     (dolist (fixup *!generic-function-fixups*)
2217       (/show fixup)
2218       (let* ((fspec (car fixup))
2219              (gf (gdefinition fspec))
2220              (methods (mapcar (lambda (method)
2221                                 (let* ((lambda-list (first method))
2222                                        (specializers (second method))
2223                                        (method-fn-name (third method))
2224                                        (fn-name (or method-fn-name fspec))
2225                                        (fn (fdefinition fn-name))
2226                                        (initargs
2227                                         (list :function
2228                                               (set-fun-name
2229                                                (lambda (args next-methods)
2230                                                  (declare (ignore
2231                                                            next-methods))
2232                                                  (apply fn args))
2233                                                `(call ,fn-name)))))
2234                                   (declare (type function fn))
2235                                   (make-a-method 'standard-method
2236                                                  ()
2237                                                  lambda-list
2238                                                  specializers
2239                                                  initargs
2240                                                  nil)))
2241                               (cdr fixup))))
2242         (setf (generic-function-method-class gf) *the-class-standard-method*)
2243         (setf (generic-function-method-combination gf)
2244               *standard-method-combination*)
2245         (set-methods gf methods))))
2246   (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2247 \f
2248 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2249 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2250 ;;; is really implemented.
2251 (defun parse-defmethod (cdr-of-form)
2252   (declare (list cdr-of-form))
2253   (let ((name (pop cdr-of-form))
2254         (qualifiers ())
2255         (spec-ll ()))
2256     (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2257               (push (pop cdr-of-form) qualifiers)
2258               (return (setq qualifiers (nreverse qualifiers)))))
2259     (setq spec-ll (pop cdr-of-form))
2260     (values name qualifiers spec-ll cdr-of-form)))
2261
2262 (defun parse-specializers (specializers)
2263   (declare (list specializers))
2264   (flet ((parse (spec)
2265            (let ((result (specializer-from-type spec)))
2266              (if (specializerp result)
2267                  result
2268                  (if (symbolp spec)
2269                      (error "~S was used as a specializer,~%~
2270                              but is not the name of a class."
2271                             spec)
2272                      (error "~S is not a legal specializer." spec))))))
2273     (mapcar #'parse specializers)))
2274
2275 (defun unparse-specializers (specializers-or-method)
2276   (if (listp specializers-or-method)
2277       (flet ((unparse (spec)
2278                (if (specializerp spec)
2279                    (let ((type (specializer-type spec)))
2280                      (if (and (consp type)
2281                               (eq (car type) 'class))
2282                          (let* ((class (cadr type))
2283                                 (class-name (class-name class)))
2284                            (if (eq class (find-class class-name nil))
2285                                class-name
2286                                type))
2287                          type))
2288                    (error "~S is not a legal specializer." spec))))
2289         (mapcar #'unparse specializers-or-method))
2290       (unparse-specializers (method-specializers specializers-or-method))))
2291
2292 (defun parse-method-or-spec (spec &optional (errorp t))
2293   (let (gf method name temp)
2294     (if (method-p spec) 
2295         (setq method spec
2296               gf (method-generic-function method)
2297               temp (and gf (generic-function-name gf))
2298               name (if temp
2299                        (make-method-spec temp
2300                                          (method-qualifiers method)
2301                                          (unparse-specializers
2302                                           (method-specializers method)))
2303                        (make-symbol (format nil "~S" method))))
2304         (multiple-value-bind (gf-spec quals specls)
2305             (parse-defmethod spec)
2306           (and (setq gf (and (or errorp (gboundp gf-spec))
2307                              (gdefinition gf-spec)))
2308                (let ((nreq (compute-discriminating-function-arglist-info gf)))
2309                  (setq specls (append (parse-specializers specls)
2310                                       (make-list (- nreq (length specls))
2311                                                  :initial-element
2312                                                  *the-class-t*)))
2313                  (and
2314                    (setq method (get-method gf quals specls errorp))
2315                    (setq name
2316                          (make-method-spec
2317                           gf-spec quals (unparse-specializers specls))))))))
2318     (values gf method name)))
2319 \f
2320 (defun extract-parameters (specialized-lambda-list)
2321   (multiple-value-bind (parameters ignore1 ignore2)
2322       (parse-specialized-lambda-list specialized-lambda-list)
2323     (declare (ignore ignore1 ignore2))
2324     parameters))
2325
2326 (defun extract-lambda-list (specialized-lambda-list)
2327   (multiple-value-bind (ignore1 lambda-list ignore2)
2328       (parse-specialized-lambda-list specialized-lambda-list)
2329     (declare (ignore ignore1 ignore2))
2330     lambda-list))
2331
2332 (defun extract-specializer-names (specialized-lambda-list)
2333   (multiple-value-bind (ignore1 ignore2 specializers)
2334       (parse-specialized-lambda-list specialized-lambda-list)
2335     (declare (ignore ignore1 ignore2))
2336     specializers))
2337
2338 (defun extract-required-parameters (specialized-lambda-list)
2339   (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2340       (parse-specialized-lambda-list specialized-lambda-list)
2341     (declare (ignore ignore1 ignore2 ignore3))
2342     required-parameters))
2343
2344 (define-condition specialized-lambda-list-error
2345     (reference-condition simple-program-error)
2346   ()
2347   (:default-initargs :references (list '(:ansi-cl :section (3 4 3)))))
2348
2349 (defun parse-specialized-lambda-list
2350     (arglist
2351      &optional supplied-keywords (allowed-keywords '(&optional &rest &key &aux))
2352      &aux (specialized-lambda-list-keywords
2353            '(&optional &rest &key &allow-other-keys &aux)))
2354   (let ((arg (car arglist)))
2355     (cond ((null arglist) (values nil nil nil nil))
2356           ((eq arg '&aux)
2357            (values nil arglist nil nil))
2358           ((memq arg lambda-list-keywords)
2359            ;; non-standard lambda-list-keywords are errors.
2360            (unless (memq arg specialized-lambda-list-keywords)
2361              (error 'specialized-lambda-list-error
2362                     :format-control "unknown specialized-lambda-list ~
2363                                      keyword ~S~%"
2364                     :format-arguments (list arg)))
2365            ;; no multiple &rest x &rest bla specifying
2366            (when (memq arg supplied-keywords)
2367              (error 'specialized-lambda-list-error
2368                     :format-control "multiple occurrence of ~
2369                                      specialized-lambda-list keyword ~S~%"
2370                     :format-arguments (list arg)))
2371            ;; And no placing &key in front of &optional, either.
2372            (unless (memq arg allowed-keywords)
2373              (error 'specialized-lambda-list-error
2374                     :format-control "misplaced specialized-lambda-list ~
2375                                      keyword ~S~%"
2376                     :format-arguments (list arg)))
2377            ;; When we are at a lambda-list keyword, the parameters
2378            ;; don't include the lambda-list keyword; the lambda-list
2379            ;; does include the lambda-list keyword; and no
2380            ;; specializers are allowed to follow the lambda-list
2381            ;; keywords (at least for now).
2382            (multiple-value-bind (parameters lambda-list)
2383                (parse-specialized-lambda-list (cdr arglist)
2384                                               (cons arg supplied-keywords)
2385                                               (if (eq arg '&key)
2386                                                   (cons '&allow-other-keys
2387                                                         (cdr (member arg allowed-keywords)))
2388                                                 (cdr (member arg allowed-keywords))))
2389              (when (and (eq arg '&rest)
2390                         (or (null lambda-list)
2391                             (memq (car lambda-list)
2392                                   specialized-lambda-list-keywords)
2393                             (not (or (null (cadr lambda-list))
2394                                      (memq (cadr lambda-list)
2395                                            specialized-lambda-list-keywords)))))
2396                (error 'specialized-lambda-list-error
2397                       :format-control
2398                       "in a specialized-lambda-list, excactly one ~
2399                        variable must follow &REST.~%"
2400                       :format-arguments nil))
2401              (values parameters
2402                      (cons arg lambda-list)
2403                      ()
2404                      ())))
2405           (supplied-keywords
2406            ;; After a lambda-list keyword there can be no specializers.
2407            (multiple-value-bind (parameters lambda-list)
2408                (parse-specialized-lambda-list (cdr arglist)
2409                                               supplied-keywords
2410                                               allowed-keywords)
2411              (values (cons (if (listp arg) (car arg) arg) parameters)
2412                      (cons arg lambda-list)
2413                      ()
2414                      ())))
2415           (t
2416            (multiple-value-bind (parameters lambda-list specializers required)
2417                (parse-specialized-lambda-list (cdr arglist))
2418              (values (cons (if (listp arg) (car arg) arg) parameters)
2419                      (cons (if (listp arg) (car arg) arg) lambda-list)
2420                      (cons (if (listp arg) (cadr arg) t) specializers)
2421                      (cons (if (listp arg) (car arg) arg) required)))))))
2422 \f
2423 (setq *boot-state* 'early)
2424 \f
2425 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2426 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2427 ;;; walker stuff was only used for implementing stuff like that; maybe
2428 ;;; it's not needed any more? Hunt down what it was used for and see.
2429
2430 (defmacro with-slots (slots instance &body body)
2431   (let ((in (gensym)))
2432     `(let ((,in ,instance))
2433        (declare (ignorable ,in))
2434        ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2435                              (third instance)
2436                              instance)))
2437            (and (symbolp instance)
2438                 `((declare (%variable-rebinding ,in ,instance)))))
2439        ,in
2440        (symbol-macrolet ,(mapcar (lambda (slot-entry)
2441                                    (let ((var-name
2442                                           (if (symbolp slot-entry)
2443                                               slot-entry
2444                                               (car slot-entry)))
2445                                          (slot-name
2446                                           (if (symbolp slot-entry)
2447                                               slot-entry
2448                                               (cadr slot-entry))))
2449                                      `(,var-name
2450                                        (slot-value ,in ',slot-name))))
2451                                  slots)
2452                         ,@body))))
2453
2454 (defmacro with-accessors (slots instance &body body)
2455   (let ((in (gensym)))
2456     `(let ((,in ,instance))
2457        (declare (ignorable ,in))
2458        ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2459                              (third instance)
2460                              instance)))
2461            (and (symbolp instance)
2462                 `((declare (%variable-rebinding ,in ,instance)))))
2463        ,in
2464        (symbol-macrolet ,(mapcar (lambda (slot-entry)
2465                                    (let ((var-name (car slot-entry))
2466                                          (accessor-name (cadr slot-entry)))
2467                                      `(,var-name (,accessor-name ,in))))
2468                                  slots)
2469           ,@body))))