0.8.18.8:
[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                              '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 ((type (info :type :kind specializer)))
616            (ecase type
617              ((:primitive :defined :instance :forthcoming-defclass-type)
618               `(type ,specializer ,parameter))
619              ((nil)
620               (let ((class (find-class specializer nil)))
621                 (if class
622                     `(type ,(class-name class) ,parameter)
623                     (progn
624                       ;; we can get here, and still not have a failure
625                       ;; case, by doing MOP programming like (PROGN
626                       ;; (ENSURE-CLASS 'FOO) (DEFMETHOD BAR ((X FOO))
627                       ;; ...)).  Best to let the user know we haven't
628                       ;; been able to extract enough information:
629                       (style-warn
630                        "~@<can't find type for presumed class ~S in ~S.~@:>"
631                        specializer
632                        'parameter-specializer-declaration-in-defmethod)
633                       '(ignorable))))))))))
634
635 (defun make-method-lambda-internal (method-lambda &optional env)
636   (unless (and (consp method-lambda) (eq (car method-lambda) 'lambda))
637     (error "The METHOD-LAMBDA argument to MAKE-METHOD-LAMBDA, ~S, ~
638             is not a lambda form."
639            method-lambda))
640   (multiple-value-bind (real-body declarations documentation)
641       (parse-body (cddr method-lambda))
642     (let* ((name-decl (get-declaration '%method-name declarations))
643            (sll-decl (get-declaration '%method-lambda-list declarations))
644            (method-name (when (consp name-decl) (car name-decl)))
645            (generic-function-name (when method-name (car method-name)))
646            (specialized-lambda-list (or sll-decl (cadr method-lambda))))
647       (multiple-value-bind (parameters lambda-list specializers)
648           (parse-specialized-lambda-list specialized-lambda-list)
649         (let* ((required-parameters
650                 (mapcar (lambda (r s) (declare (ignore s)) r)
651                         parameters
652                         specializers))
653                (slots (mapcar #'list required-parameters))
654                (calls (list nil))
655                (class-declarations
656                 `(declare
657                   ;; These declarations seem to be used by PCL to pass
658                   ;; information to itself; when I tried to delete 'em
659                   ;; ca. 0.6.10 it didn't work. I'm not sure how
660                   ;; they work, but note the (VAR-DECLARATION '%CLASS ..)
661                   ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30
662                   ,@(remove nil
663                             (mapcar (lambda (a s) (and (symbolp s)
664                                                        (neq s t)
665                                                        `(%class ,a ,s)))
666                                     parameters
667                                     specializers))
668                   ;; These TYPE declarations weren't in the original
669                   ;; PCL code, but the Python compiler likes them a
670                   ;; lot. (We're telling the compiler about our
671                   ;; knowledge of specialized argument types so that
672                   ;; it can avoid run-time type dispatch overhead,
673                   ;; which can be a huge win for Python.)
674                   ;;
675                   ;; KLUDGE: when I tried moving these to
676                   ;; ADD-METHOD-DECLARATIONS, things broke.  No idea
677                   ;; why.  -- CSR, 2004-06-16
678                   ,@(mapcar #'parameter-specializer-declaration-in-defmethod
679                             parameters
680                             specializers)))
681                (method-lambda
682                 ;; Remove the documentation string and insert the
683                 ;; appropriate class declarations. The documentation
684                 ;; string is removed to make it easy for us to insert
685                 ;; new declarations later, they will just go after the
686                 ;; CADR of the method lambda. The class declarations
687                 ;; are inserted to communicate the class of the method's
688                 ;; arguments to the code walk.
689                 `(lambda ,lambda-list
690                    ;; The default ignorability of method parameters
691                    ;; doesn't seem to be specified by ANSI. PCL had
692                    ;; them basically ignorable but was a little
693                    ;; inconsistent. E.g. even though the two
694                    ;; method definitions 
695                    ;;   (DEFMETHOD FOO ((X T) (Y T)) "Z")
696                    ;;   (DEFMETHOD FOO ((X T) Y) "Z")
697                    ;; are otherwise equivalent, PCL treated Y as
698                    ;; ignorable in the first definition but not in the
699                    ;; second definition. We make all required
700                    ;; parameters ignorable as a way of systematizing
701                    ;; the old PCL behavior. -- WHN 2000-11-24
702                    (declare (ignorable ,@required-parameters))
703                    ,class-declarations
704                    ,@declarations
705                    (block ,(fun-name-block-name generic-function-name)
706                      ,@real-body)))
707                (constant-value-p (and (null (cdr real-body))
708                                       (constantp (car real-body))))
709                (constant-value (and constant-value-p
710                                     (eval (car real-body))))
711                (plist (and constant-value-p
712                            (or (typep constant-value
713                                       '(or number character))
714                                (and (symbolp constant-value)
715                                     (symbol-package constant-value)))
716                            (list :constant-value constant-value)))
717                (applyp (dolist (p lambda-list nil)
718                          (cond ((memq p '(&optional &rest &key))
719                                 (return t))
720                                ((eq p '&aux)
721                                 (return nil))))))
722           (multiple-value-bind
723                 (walked-lambda call-next-method-p closurep
724                                next-method-p-p setq-p)
725               (walk-method-lambda method-lambda
726                                   required-parameters
727                                   env
728                                   slots
729                                   calls)
730             (multiple-value-bind (walked-lambda-body
731                                   walked-declarations
732                                   walked-documentation)
733                 (parse-body (cddr walked-lambda))
734               (declare (ignore walked-documentation))
735               (when (or next-method-p-p call-next-method-p)
736                 (setq plist (list* :needs-next-methods-p t plist)))
737               (when (some #'cdr slots)
738                 (multiple-value-bind (slot-name-lists call-list)
739                     (slot-name-lists-from-slots slots calls)
740                   (let ((pv-table-symbol (make-symbol "pv-table")))
741                     (setq plist
742                           `(,@(when slot-name-lists
743                                 `(:slot-name-lists ,slot-name-lists))
744                               ,@(when call-list
745                                   `(:call-list ,call-list))
746                               :pv-table-symbol ,pv-table-symbol
747                               ,@plist))
748                     (setq walked-lambda-body
749                           `((pv-binding (,required-parameters
750                                          ,slot-name-lists
751                                          ,pv-table-symbol)
752                                         ,@walked-lambda-body))))))
753               (when (and (memq '&key lambda-list)
754                          (not (memq '&allow-other-keys lambda-list)))
755                 (let ((aux (memq '&aux lambda-list)))
756                 (setq lambda-list (nconc (ldiff lambda-list aux)
757                                          (list '&allow-other-keys)
758                                          aux))))
759               (values `(lambda (.method-args. .next-methods.)
760                          (simple-lexical-method-functions
761                           (,lambda-list .method-args. .next-methods.
762                                         :call-next-method-p
763                                         ,call-next-method-p
764                                         :next-method-p-p ,next-method-p-p
765                                         :setq-p ,setq-p
766                                         ;; we need to pass this along
767                                         ;; so that NO-NEXT-METHOD can
768                                         ;; be given a suitable METHOD
769                                         ;; argument; we need the
770                                         ;; QUALIFIERS and SPECIALIZERS
771                                         ;; inside the declaration to
772                                         ;; give to FIND-METHOD.
773                                         :method-name-declaration ,name-decl
774                                         :closurep ,closurep
775                                         :applyp ,applyp)
776                           ,@walked-declarations
777                           ,@walked-lambda-body))
778                       `(,@(when plist
779                       `(:plist ,plist))
780                           ,@(when documentation
781                           `(:documentation ,documentation)))))))))))
782
783 (unless (fboundp 'make-method-lambda)
784   (setf (gdefinition 'make-method-lambda)
785         (symbol-function 'real-make-method-lambda)))
786
787 (defmacro simple-lexical-method-functions ((lambda-list
788                                             method-args
789                                             next-methods
790                                             &rest lmf-options)
791                                            &body body)
792   `(progn
793      ,method-args ,next-methods
794      (bind-simple-lexical-method-macros (,method-args ,next-methods)
795        (bind-lexical-method-functions (,@lmf-options)
796          (bind-args (,lambda-list ,method-args)
797            ,@body)))))
798
799 (defmacro fast-lexical-method-functions ((lambda-list
800                                           next-method-call
801                                           args
802                                           rest-arg
803                                           &rest lmf-options)
804                                          &body body)
805   `(bind-fast-lexical-method-macros (,args ,rest-arg ,next-method-call)
806      (bind-lexical-method-functions (,@lmf-options)
807        (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg)
808          ,@body))))
809
810 (defmacro bind-simple-lexical-method-macros ((method-args next-methods)
811                                              &body body)
812   `(macrolet ((call-next-method-bind (&body body)
813                `(let ((.next-method. (car ,',next-methods))
814                       (,',next-methods (cdr ,',next-methods)))
815                  .next-method. ,',next-methods
816                  ,@body))
817               (call-next-method-body (method-name-declaration cnm-args)
818                `(if .next-method.
819                     (funcall (if (std-instance-p .next-method.)
820                                  (method-function .next-method.)
821                              .next-method.) ; for early methods
822                              (or ,cnm-args ,',method-args)
823                              ,',next-methods)
824                     (apply #'call-no-next-method ',method-name-declaration
825                             (or ,cnm-args ,',method-args))))
826               (next-method-p-body ()
827                `(not (null .next-method.)))
828               (with-rebound-original-args ((call-next-method-p setq-p)
829                                            &body body)
830                 (declare (ignore call-next-method-p setq-p))
831                 `(let () ,@body)))
832     ,@body))
833
834 (defun call-no-next-method (method-name-declaration &rest args)
835   (destructuring-bind (name) method-name-declaration
836     (destructuring-bind (name &rest qualifiers-and-specializers) name
837       ;; KLUDGE: inefficient traversal, but hey.  This should only
838       ;; happen on the slow error path anyway.
839       (let* ((qualifiers (butlast qualifiers-and-specializers))
840              (specializers (car (last qualifiers-and-specializers)))
841              (method (find-method (gdefinition name) qualifiers specializers)))
842         (apply #'no-next-method
843                (method-generic-function method)
844                method
845                args)))))
846
847 (defstruct (method-call (:copier nil))
848   (function #'identity :type function)
849   call-method-args)
850
851 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
852
853 (defmacro invoke-method-call1 (function args cm-args)
854   `(let ((.function. ,function)
855          (.args. ,args)
856          (.cm-args. ,cm-args))
857      (if (and .cm-args. (null (cdr .cm-args.)))
858          (funcall .function. .args. (car .cm-args.))
859          (apply .function. .args. .cm-args.))))
860
861 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
862   `(invoke-method-call1 (method-call-function ,method-call)
863                         ,(if restp
864                              `(list* ,@required-args+rest-arg)
865                              `(list ,@required-args+rest-arg))
866                         (method-call-call-method-args ,method-call)))
867
868 (defstruct (fast-method-call (:copier nil))
869   (function #'identity :type function)
870   pv-cell
871   next-method-call
872   arg-info)
873
874 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
875
876 (defmacro fmc-funcall (fn pv-cell next-method-call &rest args)
877   `(funcall ,fn ,pv-cell ,next-method-call ,@args))
878
879 (defmacro invoke-fast-method-call (method-call &rest required-args+rest-arg)
880   `(fmc-funcall (fast-method-call-function ,method-call)
881                 (fast-method-call-pv-cell ,method-call)
882                 (fast-method-call-next-method-call ,method-call)
883                 ,@required-args+rest-arg))
884
885 (defstruct (fast-instance-boundp (:copier nil))
886   (index 0 :type fixnum))
887
888 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
889
890 (eval-when (:compile-toplevel :load-toplevel :execute)
891   (defvar *allow-emf-call-tracing-p* nil)
892   (defvar *enable-emf-call-tracing-p* #-sb-show nil #+sb-show t))
893 \f
894 ;;;; effective method functions
895
896 (defvar *emf-call-trace-size* 200)
897 (defvar *emf-call-trace* nil)
898 (defvar *emf-call-trace-index* 0)
899
900 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
901 ;;; without explanation. It appears to be intended for debugging, so
902 ;;; it might be useful someday, so I haven't deleted it.
903 ;;; But it isn't documented and isn't used for anything now, so
904 ;;; I've conditionalized it out of the base system. -- WHN 19991213
905 #+sb-show
906 (defun show-emf-call-trace ()
907   (when *emf-call-trace*
908     (let ((j *emf-call-trace-index*)
909           (*enable-emf-call-tracing-p* nil))
910       (format t "~&(The oldest entries are printed first)~%")
911       (dotimes-fixnum (i *emf-call-trace-size*)
912         (let ((ct (aref *emf-call-trace* j)))
913           (when ct (print ct)))
914         (incf j)
915         (when (= j *emf-call-trace-size*)
916           (setq j 0))))))
917
918 (defun trace-emf-call-internal (emf format args)
919   (unless *emf-call-trace*
920     (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
921   (setf (aref *emf-call-trace* *emf-call-trace-index*)
922         (list* emf format args))
923   (incf *emf-call-trace-index*)
924   (when (= *emf-call-trace-index* *emf-call-trace-size*)
925     (setq *emf-call-trace-index* 0)))
926
927 (defmacro trace-emf-call (emf format args)
928   (when *allow-emf-call-tracing-p*
929     `(when *enable-emf-call-tracing-p*
930        (trace-emf-call-internal ,emf ,format ,args))))
931
932 (defmacro invoke-effective-method-function-fast
933     (emf restp &rest required-args+rest-arg)
934   `(progn
935      (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
936      (invoke-fast-method-call ,emf ,@required-args+rest-arg)))
937
938 (defmacro invoke-effective-method-function (emf restp
939                                                 &rest required-args+rest-arg)
940   (unless (constantp restp)
941     (error "The RESTP argument is not constant."))
942   ;; FIXME: The RESTP handling here is confusing and maybe slightly
943   ;; broken if RESTP evaluates to a non-self-evaluating form. E.g. if
944   ;;   (INVOKE-EFFECTIVE-METHOD-FUNCTION EMF '(ERROR "gotcha") ...)
945   ;; then TRACE-EMF-CALL-CALL-INTERNAL might die on a gotcha error.
946   (setq restp (eval restp))
947   `(progn
948      (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
949      (cond ((typep ,emf 'fast-method-call)
950             (invoke-fast-method-call ,emf ,@required-args+rest-arg))
951            ;; "What," you may wonder, "do these next two clauses do?"
952            ;; In that case, you are not a PCL implementor, for they
953            ;; considered this to be self-documenting.:-| Or CSR, for
954            ;; that matter, since he can also figure it out by looking
955            ;; at it without breaking stride. For the rest of us,
956            ;; though: From what the code is doing with .SLOTS. and
957            ;; whatnot, evidently it's implementing SLOT-VALUEish and
958            ;; GET-SLOT-VALUEish things. Then we can reason backwards
959            ;; and conclude that setting EMF to a FIXNUM is an
960            ;; optimized way to represent these slot access operations.
961            ,@(when (and (null restp) (= 1 (length required-args+rest-arg)))
962                `(((typep ,emf 'fixnum)
963                   (let* ((.slots. (get-slots-or-nil
964                                    ,(car required-args+rest-arg)))
965                          (value (when .slots. (clos-slots-ref .slots. ,emf))))
966                     (if (eq value +slot-unbound+)
967                         (slot-unbound-internal ,(car required-args+rest-arg)
968                                                ,emf)
969                         value)))))
970            ,@(when (and (null restp) (= 2 (length required-args+rest-arg)))
971                `(((typep ,emf 'fixnum)
972                   (let ((.new-value. ,(car required-args+rest-arg))
973                         (.slots. (get-slots-or-nil
974                                   ,(cadr required-args+rest-arg))))
975                     (when .slots.
976                       (setf (clos-slots-ref .slots. ,emf) .new-value.))))))
977            ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
978            ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
979            ;; there was no explanation and presumably the code is 10+
980            ;; years stale, I simply deleted it. -- WHN)
981            (t
982             (etypecase ,emf
983               (method-call
984                (invoke-method-call ,emf ,restp ,@required-args+rest-arg))
985               (function
986                ,(if restp
987                     `(apply (the function ,emf) ,@required-args+rest-arg)
988                     `(funcall (the function ,emf)
989                               ,@required-args+rest-arg))))))))
990
991 (defun invoke-emf (emf args)
992   (trace-emf-call emf t args)
993   (etypecase emf
994     (fast-method-call
995      (let* ((arg-info (fast-method-call-arg-info emf))
996             (restp (cdr arg-info))
997             (nreq (car arg-info)))
998        (if restp
999            (let* ((rest-args (nthcdr nreq args))
1000                   (req-args (ldiff args rest-args)))
1001              (apply (fast-method-call-function emf)
1002                     (fast-method-call-pv-cell emf)
1003                     (fast-method-call-next-method-call emf)
1004                     (nconc req-args (list rest-args))))
1005            (cond ((null args)
1006                   (if (eql nreq 0)
1007                       (invoke-fast-method-call emf)
1008                       (error "wrong number of args")))
1009                  ((null (cdr args))
1010                   (if (eql nreq 1)
1011                       (invoke-fast-method-call emf (car args))
1012                       (error "wrong number of args")))
1013                  ((null (cddr args))
1014                   (if (eql nreq 2)
1015                       (invoke-fast-method-call emf (car args) (cadr args))
1016                       (error "wrong number of args")))
1017                  (t
1018                   (apply (fast-method-call-function emf)
1019                          (fast-method-call-pv-cell emf)
1020                          (fast-method-call-next-method-call emf)
1021                          args))))))
1022     (method-call
1023      (apply (method-call-function emf)
1024             args
1025             (method-call-call-method-args emf)))
1026     (fixnum
1027      (cond ((null args) (error "1 or 2 args were expected."))
1028            ((null (cdr args))
1029             (let* ((slots (get-slots (car args)))
1030                    (value (clos-slots-ref slots emf)))
1031               (if (eq value +slot-unbound+)
1032                   (slot-unbound-internal (car args) emf)
1033                   value)))
1034            ((null (cddr args))
1035              (setf (clos-slots-ref (get-slots (cadr args)) emf)
1036                    (car args)))
1037            (t (error "1 or 2 args were expected."))))
1038     (fast-instance-boundp
1039      (if (or (null args) (cdr args))
1040          (error "1 arg was expected.")
1041        (let ((slots (get-slots (car args))))
1042          (not (eq (clos-slots-ref slots
1043                                   (fast-instance-boundp-index emf))
1044                   +slot-unbound+)))))
1045     (function
1046      (apply emf args))))
1047 \f
1048 (defmacro bind-fast-lexical-method-macros ((args rest-arg next-method-call)
1049                                            &body body)
1050   (let* ((all-params (append args (when rest-arg (list rest-arg))))
1051          (rebindings (mapcar (lambda (x) (list x x)) all-params)))
1052     `(macrolet ((narrowed-emf (emf)
1053                  ;; INVOKE-EFFECTIVE-METHOD-FUNCTION has code in it to
1054                  ;; dispatch on the possibility that EMF might be of
1055                  ;; type FIXNUM (as an optimized representation of a
1056                  ;; slot accessor). But as far as I (WHN 2002-06-11)
1057                  ;; can tell, it's impossible for such a representation
1058                  ;; to end up as .NEXT-METHOD-CALL. By reassuring
1059                  ;; INVOKE-E-M-F that when called from this context
1060                  ;; it needn't worry about the FIXNUM case, we can
1061                  ;; keep those cases from being compiled, which is
1062                  ;; good both because it saves bytes and because it
1063                  ;; avoids annoying type mismatch compiler warnings.
1064                  ;;
1065                  ;; KLUDGE: In sbcl-0.7.4.29, the compiler's type
1066                  ;; system isn't smart enough about NOT and
1067                  ;; intersection types to benefit from a (NOT FIXNUM)
1068                  ;; declaration here. -- WHN 2002-06-12 (FIXME: maybe
1069                  ;; it is now... -- CSR, 2003-06-07)
1070                  ;;
1071                  ;; FIXME: Might the FUNCTION type be omittable here,
1072                  ;; leaving only METHOD-CALLs? Failing that, could this
1073                  ;; be documented somehow? (It'd be nice if the types
1074                  ;; involved could be understood without solving the
1075                  ;; halting problem.)
1076                  `(the (or function method-call fast-method-call)
1077                    ,emf))
1078                 (call-next-method-bind (&body body)
1079                  `(let () ,@body))
1080                 (call-next-method-body (method-name-declaration cnm-args)
1081                  `(if ,',next-method-call
1082                       ,(locally
1083                         ;; This declaration suppresses a "deleting
1084                         ;; unreachable code" note for the following IF
1085                         ;; when REST-ARG is NIL. It is not nice for
1086                         ;; debugging SBCL itself, but at least it
1087                         ;; keeps us from annoying users.
1088                         (declare (optimize (inhibit-warnings 3)))
1089                         (if (and (null ',rest-arg)
1090                                  (consp cnm-args)
1091                                  (eq (car cnm-args) 'list))
1092                             `(invoke-effective-method-function
1093                               (narrowed-emf ,',next-method-call)
1094                               nil
1095                               ,@(cdr cnm-args))
1096                             (let ((call `(invoke-effective-method-function
1097                                           (narrowed-emf ,',next-method-call)
1098                                           ,',(not (null rest-arg))
1099                                           ,@',args
1100                                           ,@',(when rest-arg `(,rest-arg)))))
1101                               `(if ,cnm-args
1102                                 (bind-args ((,@',args
1103                                              ,@',(when rest-arg
1104                                                        `(&rest ,rest-arg)))
1105                                             ,cnm-args)
1106                                  ,call)
1107                                 ,call))))
1108                       ,(locally
1109                         ;; As above, this declaration suppresses code
1110                         ;; deletion notes.
1111                         (declare (optimize (inhibit-warnings 3)))
1112                         (if (and (null ',rest-arg)
1113                                  (consp cnm-args)
1114                                  (eq (car cnm-args) 'list))
1115                             `(call-no-next-method ',method-name-declaration
1116                               ,@(cdr cnm-args))
1117                             `(call-no-next-method ',method-name-declaration
1118                               ,@',args
1119                               ,@',(when rest-arg
1120                                         `(,rest-arg)))))))
1121                 (next-method-p-body ()
1122                  `(not (null ,',next-method-call)))
1123                 (with-rebound-original-args ((cnm-p setq-p) &body body)
1124                   (if (or cnm-p setq-p)
1125                       `(let ,',rebindings
1126                         (declare (ignorable ,@',all-params))
1127                         ,@body)
1128                       `(let () ,@body))))
1129       ,@body)))
1130
1131 (defmacro bind-lexical-method-functions
1132     ((&key call-next-method-p next-method-p-p setq-p
1133            closurep applyp method-name-declaration)
1134      &body body)
1135   (cond ((and (null call-next-method-p) (null next-method-p-p)
1136               (null closurep) (null applyp) (null setq-p))
1137          `(let () ,@body))
1138         (t
1139          `(call-next-method-bind
1140             (flet (,@(and call-next-method-p
1141                           `((call-next-method (&rest cnm-args)
1142                              (call-next-method-body
1143                               ,method-name-declaration
1144                               cnm-args))))
1145                    ,@(and next-method-p-p
1146                           '((next-method-p ()
1147                              (next-method-p-body)))))
1148               (with-rebound-original-args (,call-next-method-p ,setq-p)
1149                 ,@body))))))
1150
1151 (defmacro bind-args ((lambda-list args) &body body)
1152   (let ((args-tail '.args-tail.)
1153         (key '.key.)
1154         (state 'required))
1155     (flet ((process-var (var)
1156              (if (memq var lambda-list-keywords)
1157                  (progn
1158                    (case var
1159                      (&optional       (setq state 'optional))
1160                      (&key            (setq state 'key))
1161                      (&allow-other-keys)
1162                      (&rest           (setq state 'rest))
1163                      (&aux            (setq state 'aux))
1164                      (otherwise
1165                       (error
1166                        "encountered the non-standard lambda list keyword ~S"
1167                        var)))
1168                    nil)
1169                  (case state
1170                    (required `((,var (pop ,args-tail))))
1171                    (optional (cond ((not (consp var))
1172                                     `((,var (when ,args-tail
1173                                               (pop ,args-tail)))))
1174                                    ((null (cddr var))
1175                                     `((,(car var) (if ,args-tail
1176                                                       (pop ,args-tail)
1177                                                       ,(cadr var)))))
1178                                    (t
1179                                     `((,(caddr var) ,args-tail)
1180                                       (,(car var) (if ,args-tail
1181                                                       (pop ,args-tail)
1182                                                       ,(cadr var)))))))
1183                    (rest `((,var ,args-tail)))
1184                    (key (cond ((not (consp var))
1185                                `((,var (car
1186                                         (get-key-arg-tail ,(keywordicate var)
1187                                                           ,args-tail)))))
1188                               ((null (cddr var))
1189                                (multiple-value-bind (keyword variable)
1190                                    (if (consp (car var))
1191                                        (values (caar var)
1192                                                (cadar var))
1193                                        (values (keywordicate (car var))
1194                                                (car var)))
1195                                  `((,key (get-key-arg-tail ',keyword
1196                                                            ,args-tail))
1197                                    (,variable (if ,key
1198                                                   (car ,key)
1199                                                   ,(cadr var))))))
1200                               (t
1201                                (multiple-value-bind (keyword variable)
1202                                    (if (consp (car var))
1203                                        (values (caar var)
1204                                                (cadar var))
1205                                        (values (keywordicate (car var))
1206                                                (car var)))
1207                                  `((,key (get-key-arg-tail ',keyword
1208                                                            ,args-tail))
1209                                    (,(caddr var) ,key)
1210                                    (,variable (if ,key
1211                                                   (car ,key)
1212                                                   ,(cadr var))))))))
1213                    (aux `(,var))))))
1214       (let ((bindings (mapcan #'process-var lambda-list)))
1215         `(let* ((,args-tail ,args)
1216                 ,@bindings
1217                 (.dummy0.
1218                  ,@(when (eq state 'optional)
1219                      `((unless (null ,args-tail)
1220                          (error 'simple-program-error
1221                                 :format-control "surplus arguments: ~S"
1222                                 :format-arguments (list ,args-tail)))))))
1223            (declare (ignorable ,args-tail .dummy0.))
1224            ,@body)))))
1225
1226 (defun get-key-arg-tail (keyword list)
1227   (loop for (key . tail) on list by #'cddr
1228         when (null tail) do
1229           ;; FIXME: Do we want to export this symbol? Or maybe use an
1230           ;; (ERROR 'SIMPLE-PROGRAM-ERROR) form?
1231           (sb-c::%odd-key-args-error)
1232         when (eq key keyword)
1233           return tail))
1234
1235 (defun walk-method-lambda (method-lambda required-parameters env slots calls)
1236   (let ((call-next-method-p nil)   ; flag indicating that CALL-NEXT-METHOD
1237                                    ; should be in the method definition
1238         (closurep nil)             ; flag indicating that #'CALL-NEXT-METHOD
1239                                    ; was seen in the body of a method
1240         (next-method-p-p nil)      ; flag indicating that NEXT-METHOD-P
1241                                    ; should be in the method definition
1242         (setq-p nil))
1243     (flet ((walk-function (form context env)
1244              (cond ((not (eq context :eval)) form)
1245                    ;; FIXME: Jumping to a conclusion from the way it's used
1246                    ;; above, perhaps CONTEXT should be called SITUATION
1247                    ;; (after the term used in the ANSI specification of
1248                    ;; EVAL-WHEN) and given modern ANSI keyword values
1249                    ;; like :LOAD-TOPLEVEL.
1250                    ((not (listp form)) form)
1251                    ((eq (car form) 'call-next-method)
1252                     (setq call-next-method-p t)
1253                     form)
1254                    ((eq (car form) 'next-method-p)
1255                     (setq next-method-p-p t)
1256                     form)
1257                    ((eq (car form) 'setq)
1258                     ;; FIXME: this is possibly a little strong as
1259                     ;; conditions go.  Ideally we would want to detect
1260                     ;; which, if any, of the method parameters are
1261                     ;; being set, and communicate that information to
1262                     ;; e.g. SPLIT-DECLARATIONS.  However, the brute
1263                     ;; force method doesn't really cost much; a little
1264                     ;; loss of discrimination over IGNORED variables
1265                     ;; should be all.  -- CSR, 2004-07-01
1266                     (setq setq-p t)
1267                     form)
1268                    ((and (eq (car form) 'function)
1269                          (cond ((eq (cadr form) 'call-next-method)
1270                                 (setq call-next-method-p t)
1271                                 (setq closurep t)
1272                                 form)
1273                                ((eq (cadr form) 'next-method-p)
1274                                 (setq next-method-p-p t)
1275                                 (setq closurep t)
1276                                 form)
1277                                (t nil))))
1278                    ((and (memq (car form)
1279                                '(slot-value set-slot-value slot-boundp))
1280                          (constantp (caddr form)))
1281                      (let ((parameter (can-optimize-access form
1282                                                            required-parameters
1283                                                            env)))
1284                       (let ((fun (ecase (car form)
1285                                    (slot-value #'optimize-slot-value)
1286                                    (set-slot-value #'optimize-set-slot-value)
1287                                    (slot-boundp #'optimize-slot-boundp))))
1288                         (funcall fun slots parameter form))))
1289                    ((and (eq (car form) 'apply)
1290                          (consp (cadr form))
1291                          (eq (car (cadr form)) 'function)
1292                          (generic-function-name-p (cadr (cadr form))))
1293                     (optimize-generic-function-call
1294                      form required-parameters env slots calls))
1295                    ((generic-function-name-p (car form))
1296                     (optimize-generic-function-call
1297                      form required-parameters env slots calls))
1298                    (t form))))
1299
1300       (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1301         (values walked-lambda
1302                 call-next-method-p
1303                 closurep
1304                 next-method-p-p
1305                 setq-p)))))
1306
1307 (defun generic-function-name-p (name)
1308   (and (legal-fun-name-p name)
1309        (gboundp name)
1310        (if (eq *boot-state* 'complete)
1311            (standard-generic-function-p (gdefinition name))
1312            (funcallable-instance-p (gdefinition name)))))
1313 \f
1314 (defvar *method-function-plist* (make-hash-table :test 'eq))
1315 (defvar *mf1* nil)
1316 (defvar *mf1p* nil)
1317 (defvar *mf1cp* nil)
1318 (defvar *mf2* nil)
1319 (defvar *mf2p* nil)
1320 (defvar *mf2cp* nil)
1321
1322 (defun method-function-plist (method-function)
1323   (unless (eq method-function *mf1*)
1324     (rotatef *mf1* *mf2*)
1325     (rotatef *mf1p* *mf2p*)
1326     (rotatef *mf1cp* *mf2cp*))
1327   (unless (or (eq method-function *mf1*) (null *mf1cp*))
1328     (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1329   (unless (eq method-function *mf1*)
1330     (setf *mf1* method-function
1331           *mf1cp* nil
1332           *mf1p* (gethash method-function *method-function-plist*)))
1333   *mf1p*)
1334
1335 (defun (setf method-function-plist)
1336     (val method-function)
1337   (unless (eq method-function *mf1*)
1338     (rotatef *mf1* *mf2*)
1339     (rotatef *mf1cp* *mf2cp*)
1340     (rotatef *mf1p* *mf2p*))
1341   (unless (or (eq method-function *mf1*) (null *mf1cp*))
1342     (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1343   (setf *mf1* method-function
1344         *mf1cp* t
1345         *mf1p* val))
1346
1347 (defun method-function-get (method-function key &optional default)
1348   (getf (method-function-plist method-function) key default))
1349
1350 (defun (setf method-function-get)
1351     (val method-function key)
1352   (setf (getf (method-function-plist method-function) key) val))
1353
1354 (defun method-function-pv-table (method-function)
1355   (method-function-get method-function :pv-table))
1356
1357 (defun method-function-method (method-function)
1358   (method-function-get method-function :method))
1359
1360 (defun method-function-needs-next-methods-p (method-function)
1361   (method-function-get method-function :needs-next-methods-p t))
1362 \f
1363 (defmacro method-function-closure-generator (method-function)
1364   `(method-function-get ,method-function 'closure-generator))
1365
1366 (defun load-defmethod
1367     (class name quals specls ll initargs &optional pv-table-symbol)
1368   (setq initargs (copy-tree initargs))
1369   (let ((method-spec (or (getf initargs :method-spec)
1370                          (make-method-spec name quals specls))))
1371     (setf (getf initargs :method-spec) method-spec)
1372     (load-defmethod-internal class name quals specls
1373                              ll initargs pv-table-symbol)))
1374
1375 (defun load-defmethod-internal
1376     (method-class gf-spec qualifiers specializers lambda-list
1377                   initargs pv-table-symbol)
1378   (when pv-table-symbol
1379     (setf (getf (getf initargs :plist) :pv-table-symbol)
1380           pv-table-symbol))
1381   (when (and (eq *boot-state* 'complete)
1382              (fboundp gf-spec))
1383     (let* ((gf (fdefinition gf-spec))
1384            (method (and (generic-function-p gf)
1385                         (generic-function-methods gf)
1386                         (find-method gf
1387                                      qualifiers
1388                                      (parse-specializers specializers)
1389                                      nil))))
1390       (when method
1391         (style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1392                     gf-spec qualifiers specializers))))
1393   (let ((method (apply #'add-named-method
1394                        gf-spec qualifiers specializers lambda-list
1395                        :definition-source `((defmethod ,gf-spec
1396                                                 ,@qualifiers
1397                                               ,specializers)
1398                                             ,*load-pathname*)
1399                        initargs)))
1400     (unless (or (eq method-class 'standard-method)
1401                 (eq (find-class method-class nil) (class-of method)))
1402       ;; FIXME: should be STYLE-WARNING?
1403       (format *error-output*
1404               "~&At the time the method with qualifiers ~:S and~%~
1405                specializers ~:S on the generic function ~S~%~
1406                was compiled, the method-class for that generic function was~%~
1407                ~S. But, the method class is now ~S, this~%~
1408                may mean that this method was compiled improperly.~%"
1409               qualifiers specializers gf-spec
1410               method-class (class-name (class-of method))))
1411     method))
1412
1413 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1414   `(method ,gf-spec ,@qualifiers ,unparsed-specializers))
1415
1416 (defun initialize-method-function (initargs &optional return-function-p method)
1417   (let* ((mf (getf initargs :function))
1418          (method-spec (getf initargs :method-spec))
1419          (plist (getf initargs :plist))
1420          (pv-table-symbol (getf plist :pv-table-symbol))
1421          (pv-table nil)
1422          (mff (getf initargs :fast-function)))
1423     (flet ((set-mf-property (p v)
1424              (when mf
1425                (setf (method-function-get mf p) v))
1426              (when mff
1427                (setf (method-function-get mff p) v))))
1428       (when method-spec
1429         (when mf
1430           (setq mf (set-fun-name mf method-spec)))
1431         (when mff
1432           (let ((name `(fast-method ,@(cdr method-spec))))
1433             (set-fun-name mff name)
1434             (unless mf
1435               (set-mf-property :name name)))))
1436       (when plist
1437         (let ((snl (getf plist :slot-name-lists))
1438               (cl (getf plist :call-list)))
1439           (when (or snl cl)
1440             (setq pv-table (intern-pv-table :slot-name-lists snl
1441                                             :call-list cl))
1442             (when pv-table (set pv-table-symbol pv-table))
1443             (set-mf-property :pv-table pv-table)))
1444         (loop (when (null plist) (return nil))
1445               (set-mf-property (pop plist) (pop plist)))
1446         (when method
1447           (set-mf-property :method method))
1448         (when return-function-p
1449           (or mf (method-function-from-fast-function mff)))))))
1450 \f
1451 (defun analyze-lambda-list (lambda-list)
1452   (flet (;; FIXME: Is this redundant with SB-C::MAKE-KEYWORD-FOR-ARG?
1453          (parse-key-arg (arg)
1454            (if (listp arg)
1455                (if (listp (car arg))
1456                    (caar arg)
1457                    (keywordicate (car arg)))
1458                (keywordicate arg))))
1459     (let ((nrequired 0)
1460           (noptional 0)
1461           (keysp nil)
1462           (restp nil)
1463           (nrest 0)
1464           (allow-other-keys-p nil)
1465           (keywords ())
1466           (keyword-parameters ())
1467           (state 'required))
1468       (dolist (x lambda-list)
1469         (if (memq x lambda-list-keywords)
1470             (case x
1471               (&optional         (setq state 'optional))
1472               (&key              (setq keysp t
1473                                        state 'key))
1474               (&allow-other-keys (setq allow-other-keys-p t))
1475               (&rest             (setq restp t
1476                                        state 'rest))
1477               (&aux           (return t))
1478               (otherwise
1479                 (error "encountered the non-standard lambda list keyword ~S"
1480                        x)))
1481             (ecase state
1482               (required  (incf nrequired))
1483               (optional  (incf noptional))
1484               (key       (push (parse-key-arg x) keywords)
1485                          (push x keyword-parameters))
1486               (rest      (incf nrest)))))
1487       (when (and restp (zerop nrest))
1488         (error "Error in lambda-list:~%~
1489                 After &REST, a DEFGENERIC lambda-list ~
1490                 must be followed by at least one variable."))
1491       (values nrequired noptional keysp restp allow-other-keys-p
1492               (reverse keywords)
1493               (reverse keyword-parameters)))))
1494
1495 (defun keyword-spec-name (x)
1496   (let ((key (if (atom x) x (car x))))
1497     (if (atom key)
1498         (keywordicate key)
1499         (car key))))
1500
1501 (defun ftype-declaration-from-lambda-list (lambda-list name)
1502   (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1503                                   keywords keyword-parameters)
1504       (analyze-lambda-list lambda-list)
1505     (declare (ignore keyword-parameters))
1506     (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1507            (old-ftype (if (fun-type-p old) old nil))
1508            (old-restp (and old-ftype (fun-type-rest old-ftype)))
1509            (old-keys (and old-ftype
1510                           (mapcar #'key-info-name
1511                                   (fun-type-keywords
1512                                    old-ftype))))
1513            (old-keysp (and old-ftype (fun-type-keyp old-ftype)))
1514            (old-allowp (and old-ftype
1515                             (fun-type-allowp old-ftype)))
1516            (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1517       `(function ,(append (make-list nrequired :initial-element t)
1518                           (when (plusp noptional)
1519                             (append '(&optional)
1520                                     (make-list noptional :initial-element t)))
1521                           (when (or restp old-restp)
1522                             '(&rest t))
1523                           (when (or keysp old-keysp)
1524                             (append '(&key)
1525                                     (mapcar (lambda (key)
1526                                               `(,key t))
1527                                             keywords)
1528                                     (when (or allow-other-keys-p old-allowp)
1529                                       '(&allow-other-keys)))))
1530                  *))))
1531
1532 (defun defgeneric-declaration (spec lambda-list)
1533   `(ftype ,(ftype-declaration-from-lambda-list lambda-list spec) ,spec))
1534 \f
1535 ;;;; early generic function support
1536
1537 (defvar *!early-generic-functions* ())
1538
1539 (defun ensure-generic-function (fun-name
1540                                 &rest all-keys
1541                                 &key environment
1542                                 &allow-other-keys)
1543   (declare (ignore environment))
1544   (let ((existing (and (gboundp fun-name)
1545                        (gdefinition fun-name))))
1546     (if (and existing
1547              (eq *boot-state* 'complete)
1548              (null (generic-function-p existing)))
1549         (generic-clobbers-function fun-name)
1550         (apply #'ensure-generic-function-using-class
1551                existing fun-name all-keys))))
1552
1553 (defun generic-clobbers-function (fun-name)
1554   (error 'simple-program-error
1555          :format-control "~S already names an ordinary function or a macro."
1556          :format-arguments (list fun-name)))
1557
1558 (defvar *sgf-wrapper*
1559   (boot-make-wrapper (early-class-size 'standard-generic-function)
1560                      'standard-generic-function))
1561
1562 (defvar *sgf-slots-init*
1563   (mapcar (lambda (canonical-slot)
1564             (if (memq (getf canonical-slot :name) '(arg-info source))
1565                 +slot-unbound+
1566                 (let ((initfunction (getf canonical-slot :initfunction)))
1567                   (if initfunction
1568                       (funcall initfunction)
1569                       +slot-unbound+))))
1570           (early-collect-inheritance 'standard-generic-function)))
1571
1572 (defvar *sgf-method-class-index*
1573   (!bootstrap-slot-index 'standard-generic-function 'method-class))
1574
1575 (defun early-gf-p (x)
1576   (and (fsc-instance-p x)
1577        (eq (clos-slots-ref (get-slots x) *sgf-method-class-index*)
1578            +slot-unbound+)))
1579
1580 (defvar *sgf-methods-index*
1581   (!bootstrap-slot-index 'standard-generic-function 'methods))
1582
1583 (defmacro early-gf-methods (gf)
1584   `(clos-slots-ref (get-slots ,gf) *sgf-methods-index*))
1585
1586 (defvar *sgf-arg-info-index*
1587   (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1588
1589 (defmacro early-gf-arg-info (gf)
1590   `(clos-slots-ref (get-slots ,gf) *sgf-arg-info-index*))
1591
1592 (defvar *sgf-dfun-state-index*
1593   (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1594
1595 (defstruct (arg-info
1596             (:conc-name nil)
1597             (:constructor make-arg-info ())
1598             (:copier nil))
1599   (arg-info-lambda-list :no-lambda-list)
1600   arg-info-precedence
1601   arg-info-metatypes
1602   arg-info-number-optional
1603   arg-info-key/rest-p
1604   arg-info-keys   ;nil        no &KEY or &REST allowed
1605                   ;(k1 k2 ..) Each method must accept these &KEY arguments.
1606                   ;T          must have &KEY or &REST
1607
1608   gf-info-simple-accessor-type ; nil, reader, writer, boundp
1609   (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1610
1611   gf-info-static-c-a-m-emf
1612   (gf-info-c-a-m-emf-std-p t)
1613   gf-info-fast-mf-p)
1614
1615 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1616
1617 (defun arg-info-valid-p (arg-info)
1618   (not (null (arg-info-number-optional arg-info))))
1619
1620 (defun arg-info-applyp (arg-info)
1621   (or (plusp (arg-info-number-optional arg-info))
1622       (arg-info-key/rest-p arg-info)))
1623
1624 (defun arg-info-number-required (arg-info)
1625   (length (arg-info-metatypes arg-info)))
1626
1627 (defun arg-info-nkeys (arg-info)
1628   (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1629
1630 ;;; Keep pages clean by not setting if the value is already the same.
1631 (defmacro esetf (pos val)
1632   (with-unique-names (valsym)
1633     `(let ((,valsym ,val))
1634        (unless (equal ,pos ,valsym)
1635          (setf ,pos ,valsym)))))
1636
1637 (defun create-gf-lambda-list (lambda-list)
1638   ;;; Create a gf lambda list from a method lambda list
1639   (loop for x in lambda-list
1640         collect (if (consp x) (list (car x)) x)
1641         if (eq x '&key) do (loop-finish)))
1642
1643 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1644                         argument-precedence-order)
1645   (let* ((arg-info (if (eq *boot-state* 'complete)
1646                        (gf-arg-info gf)
1647                        (early-gf-arg-info gf)))
1648          (methods (if (eq *boot-state* 'complete)
1649                       (generic-function-methods gf)
1650                       (early-gf-methods gf)))
1651          (was-valid-p (integerp (arg-info-number-optional arg-info)))
1652          (first-p (and new-method (null (cdr methods)))))
1653     (when (and (not lambda-list-p) methods)
1654       (setq lambda-list (gf-lambda-list gf)))
1655     (when (or lambda-list-p
1656               (and first-p
1657                    (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1658       (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1659           (analyze-lambda-list lambda-list)
1660         (when (and methods (not first-p))
1661           (let ((gf-nreq (arg-info-number-required arg-info))
1662                 (gf-nopt (arg-info-number-optional arg-info))
1663                 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1664             (unless (and (= nreq gf-nreq)
1665                          (= nopt gf-nopt)
1666                          (eq (or keysp restp) gf-key/rest-p))
1667               (error "The lambda-list ~S is incompatible with ~
1668                      existing methods of ~S."
1669                      lambda-list gf))))
1670         (esetf (arg-info-lambda-list arg-info)
1671                (if lambda-list-p
1672                    lambda-list
1673                    (create-gf-lambda-list lambda-list)))
1674         (when (or lambda-list-p argument-precedence-order
1675                   (null (arg-info-precedence arg-info)))
1676           (esetf (arg-info-precedence arg-info)
1677                  (compute-precedence lambda-list nreq
1678                                      argument-precedence-order)))
1679         (esetf (arg-info-metatypes arg-info) (make-list nreq))
1680         (esetf (arg-info-number-optional arg-info) nopt)
1681         (esetf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1682         (esetf (arg-info-keys arg-info)
1683                (if lambda-list-p
1684                    (if allow-other-keys-p t keywords)
1685                    (arg-info-key/rest-p arg-info)))))
1686     (when new-method
1687       (check-method-arg-info gf arg-info new-method))
1688     (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1689     arg-info))
1690
1691 (defun check-method-arg-info (gf arg-info method)
1692   (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1693       (analyze-lambda-list (if (consp method)
1694                                (early-method-lambda-list method)
1695                                (method-lambda-list method)))
1696     (flet ((lose (string &rest args)
1697              (error 'simple-program-error
1698                     :format-control "~@<attempt to add the method~2I~_~S~I~_~
1699                                      to the generic function~2I~_~S;~I~_~
1700                                      but ~?~:>"
1701                     :format-arguments (list method gf string args)))
1702            (comparison-description (x y)
1703              (if (> x y) "more" "fewer")))
1704       (let ((gf-nreq (arg-info-number-required arg-info))
1705             (gf-nopt (arg-info-number-optional arg-info))
1706             (gf-key/rest-p (arg-info-key/rest-p arg-info))
1707             (gf-keywords (arg-info-keys arg-info)))
1708         (unless (= nreq gf-nreq)
1709           (lose
1710            "the method has ~A required arguments than the generic function."
1711            (comparison-description nreq gf-nreq)))
1712         (unless (= nopt gf-nopt)
1713           (lose
1714            "the method has ~A optional arguments than the generic function."
1715            (comparison-description nopt gf-nopt)))
1716         (unless (eq (or keysp restp) gf-key/rest-p)
1717           (lose
1718            "the method and generic function differ in whether they accept~_~
1719             &REST or &KEY arguments."))
1720         (when (consp gf-keywords)
1721           (unless (or (and restp (not keysp))
1722                       allow-other-keys-p
1723                       (every (lambda (k) (memq k keywords)) gf-keywords))
1724             (lose "the method does not accept each of the &KEY arguments~2I~_~
1725                    ~S."
1726                   gf-keywords)))))))
1727
1728 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1729   (let* ((existing-p (and methods (cdr methods) new-method))
1730          (nreq (length (arg-info-metatypes arg-info)))
1731          (metatypes (if existing-p
1732                         (arg-info-metatypes arg-info)
1733                         (make-list nreq)))
1734          (type (if existing-p
1735                    (gf-info-simple-accessor-type arg-info)
1736                    nil)))
1737     (when (arg-info-valid-p arg-info)
1738       (dolist (method (if new-method (list new-method) methods))
1739         (let* ((specializers (if (or (eq *boot-state* 'complete)
1740                                      (not (consp method)))
1741                                  (method-specializers method)
1742                                  (early-method-specializers method t)))
1743                (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1744                           (class-of method)
1745                           (early-method-class method)))
1746                (new-type (when (and class
1747                                     (or (not (eq *boot-state* 'complete))
1748                                         (eq (generic-function-method-combination gf)
1749                                             *standard-method-combination*)))
1750                            (cond ((eq class *the-class-standard-reader-method*)
1751                                   'reader)
1752                                  ((eq class *the-class-standard-writer-method*)
1753                                   'writer)
1754                                  ((eq class *the-class-standard-boundp-method*)
1755                                   'boundp)))))
1756           (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1757           (setq type (cond ((null type) new-type)
1758                            ((eq type new-type) type)
1759                            (t nil)))))
1760       (esetf (arg-info-metatypes arg-info) metatypes)
1761       (esetf (gf-info-simple-accessor-type arg-info) type)))
1762   (when (or (not was-valid-p) first-p)
1763     (multiple-value-bind (c-a-m-emf std-p)
1764         (if (early-gf-p gf)
1765             (values t t)
1766             (compute-applicable-methods-emf gf))
1767       (esetf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1768       (esetf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1769       (unless (gf-info-c-a-m-emf-std-p arg-info)
1770         (esetf (gf-info-simple-accessor-type arg-info) t))))
1771   (unless was-valid-p
1772     (let ((name (if (eq *boot-state* 'complete)
1773                     (generic-function-name gf)
1774                     (!early-gf-name gf))))
1775       (esetf (gf-precompute-dfun-and-emf-p arg-info)
1776              (cond
1777                ((and (consp name)
1778                      (member (car name)
1779                              *internal-pcl-generalized-fun-name-symbols*))
1780                 nil)
1781                (t (let* ((symbol (fun-name-block-name name))
1782                          (package (symbol-package symbol)))
1783                     (and (or (eq package *pcl-package*)
1784                              (memq package (package-use-list *pcl-package*)))
1785                          ;; FIXME: this test will eventually be
1786                          ;; superseded by the *internal-pcl...* test,
1787                          ;; above.  While we are in a process of
1788                          ;; transition, however, it should probably
1789                          ;; remain.
1790                          (not (find #\Space (symbol-name symbol))))))))))
1791   (esetf (gf-info-fast-mf-p arg-info)
1792          (or (not (eq *boot-state* 'complete))
1793              (let* ((method-class (generic-function-method-class gf))
1794                     (methods (compute-applicable-methods
1795                               #'make-method-lambda
1796                               (list gf (class-prototype method-class)
1797                                     '(lambda) nil))))
1798                (and methods (null (cdr methods))
1799                     (let ((specls (method-specializers (car methods))))
1800                       (and (classp (car specls))
1801                            (eq 'standard-generic-function
1802                                (class-name (car specls)))
1803                            (classp (cadr specls))
1804                            (eq 'standard-method
1805                                (class-name (cadr specls)))))))))
1806   arg-info)
1807
1808 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
1809 ;;;
1810 ;;; The STATIC-SLOTS field of the funcallable instances used as early
1811 ;;; generic functions is used to store the early methods and early
1812 ;;; discriminator code for the early generic function. The static
1813 ;;; slots field of the fins contains a list whose:
1814 ;;;    CAR    -   a list of the early methods on this early gf
1815 ;;;    CADR   -   the early discriminator code for this method
1816 (defun ensure-generic-function-using-class (existing spec &rest keys
1817                                             &key (lambda-list nil
1818                                                               lambda-list-p)
1819                                             argument-precedence-order
1820                                             &allow-other-keys)
1821   (declare (ignore keys))
1822   (cond ((and existing (early-gf-p existing))
1823          (when lambda-list-p
1824            (set-arg-info existing :lambda-list lambda-list))
1825          existing)
1826         ((assoc spec *!generic-function-fixups* :test #'equal)
1827          (if existing
1828              (make-early-gf spec lambda-list lambda-list-p existing
1829                             argument-precedence-order)
1830              (error "The function ~S is not already defined." spec)))
1831         (existing
1832          (error "~S should be on the list ~S."
1833                 spec
1834                 '*!generic-function-fixups*))
1835         (t
1836          (pushnew spec *!early-generic-functions* :test #'equal)
1837          (make-early-gf spec lambda-list lambda-list-p nil
1838                         argument-precedence-order))))
1839
1840 (defun make-early-gf (spec &optional lambda-list lambda-list-p
1841                       function argument-precedence-order)
1842   (let ((fin (allocate-funcallable-instance *sgf-wrapper* *sgf-slots-init*)))
1843     (set-funcallable-instance-function
1844      fin
1845      (or function
1846          (if (eq spec 'print-object)
1847              #'(instance-lambda (instance stream)
1848                  (print-unreadable-object (instance stream :identity t)
1849                    (format stream "std-instance")))
1850              #'(instance-lambda (&rest args)
1851                  (declare (ignore args))
1852                  (error "The function of the funcallable-instance ~S~
1853                          has not been set." fin)))))
1854     (setf (gdefinition spec) fin)
1855     (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
1856     (!bootstrap-set-slot 'standard-generic-function
1857                          fin
1858                          'source
1859                          *load-pathname*)
1860     (set-fun-name fin spec)
1861     (let ((arg-info (make-arg-info)))
1862       (setf (early-gf-arg-info fin) arg-info)
1863       (when lambda-list-p
1864         (proclaim (defgeneric-declaration spec lambda-list))
1865         (if argument-precedence-order
1866             (set-arg-info fin
1867                           :lambda-list lambda-list
1868                           :argument-precedence-order argument-precedence-order)
1869             (set-arg-info fin :lambda-list lambda-list))))
1870     fin))
1871
1872 (defun set-dfun (gf &optional dfun cache info)
1873   (when cache
1874     (setf (cache-owner cache) gf))
1875   (let ((new-state (if (and dfun (or cache info))
1876                        (list* dfun cache info)
1877                        dfun)))
1878     (if (eq *boot-state* 'complete)
1879         (setf (gf-dfun-state gf) new-state)
1880         (setf (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*)
1881               new-state)))
1882   dfun)
1883
1884 (defun gf-dfun-cache (gf)
1885   (let ((state (if (eq *boot-state* 'complete)
1886                    (gf-dfun-state gf)
1887                    (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1888     (typecase state
1889       (function nil)
1890       (cons (cadr state)))))
1891
1892 (defun gf-dfun-info (gf)
1893   (let ((state (if (eq *boot-state* 'complete)
1894                    (gf-dfun-state gf)
1895                    (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1896     (typecase state
1897       (function nil)
1898       (cons (cddr state)))))
1899
1900 (defvar *sgf-name-index*
1901   (!bootstrap-slot-index 'standard-generic-function 'name))
1902
1903 (defun !early-gf-name (gf)
1904   (clos-slots-ref (get-slots gf) *sgf-name-index*))
1905
1906 (defun gf-lambda-list (gf)
1907   (let ((arg-info (if (eq *boot-state* 'complete)
1908                       (gf-arg-info gf)
1909                       (early-gf-arg-info gf))))
1910     (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
1911         (let ((methods (if (eq *boot-state* 'complete)
1912                            (generic-function-methods gf)
1913                            (early-gf-methods gf))))
1914           (if (null methods)
1915               (progn
1916                 (warn "no way to determine the lambda list for ~S" gf)
1917                 nil)
1918               (let* ((method (car (last methods)))
1919                      (ll (if (consp method)
1920                              (early-method-lambda-list method)
1921                              (method-lambda-list method))))
1922                 (create-gf-lambda-list ll))))
1923         (arg-info-lambda-list arg-info))))
1924
1925 (defmacro real-ensure-gf-internal (gf-class all-keys env)
1926   `(progn
1927      (cond ((symbolp ,gf-class)
1928             (setq ,gf-class (find-class ,gf-class t ,env)))
1929            ((classp ,gf-class))
1930            (t
1931             (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
1932                     class nor a symbol that names a class."
1933                    ,gf-class)))
1934      (remf ,all-keys :generic-function-class)
1935      (remf ,all-keys :environment)
1936      (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
1937        (unless (eq combin '.shes-not-there.)
1938          (setf (getf ,all-keys :method-combination)
1939                (find-method-combination (class-prototype ,gf-class)
1940                                         (car combin)
1941                                         (cdr combin)))))
1942     (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
1943       (unless (eq method-class '.shes-not-there.)
1944         (setf (getf ,all-keys :method-class)
1945               (find-class method-class t ,env))))))
1946
1947 (defun real-ensure-gf-using-class--generic-function
1948        (existing
1949         fun-name
1950         &rest all-keys
1951         &key environment (lambda-list nil lambda-list-p)
1952              (generic-function-class 'standard-generic-function gf-class-p)
1953         &allow-other-keys)
1954   (real-ensure-gf-internal generic-function-class all-keys environment)
1955   (unless (or (null gf-class-p)
1956               (eq (class-of existing) generic-function-class))
1957     (change-class existing generic-function-class))
1958   (prog1
1959       (apply #'reinitialize-instance existing all-keys)
1960     (when lambda-list-p
1961       (proclaim (defgeneric-declaration fun-name lambda-list)))))
1962
1963 (defun real-ensure-gf-using-class--null
1964        (existing
1965         fun-name
1966         &rest all-keys
1967         &key environment (lambda-list nil lambda-list-p)
1968              (generic-function-class 'standard-generic-function)
1969         &allow-other-keys)
1970   (declare (ignore existing))
1971   (real-ensure-gf-internal generic-function-class all-keys environment)
1972   (prog1
1973       (setf (gdefinition fun-name)
1974             (apply #'make-instance generic-function-class
1975                    :name fun-name all-keys))
1976     (when lambda-list-p
1977       (proclaim (defgeneric-declaration fun-name lambda-list)))))
1978 \f
1979 (defun get-generic-fun-info (gf)
1980   ;; values   nreq applyp metatypes nkeys arg-info
1981   (multiple-value-bind (applyp metatypes arg-info)
1982       (let* ((arg-info (if (early-gf-p gf)
1983                            (early-gf-arg-info gf)
1984                            (gf-arg-info gf)))
1985              (metatypes (arg-info-metatypes arg-info)))
1986         (values (arg-info-applyp arg-info)
1987                 metatypes
1988                 arg-info))
1989     (values (length metatypes) applyp metatypes
1990             (count-if (lambda (x) (neq x t)) metatypes)
1991             arg-info)))
1992
1993 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
1994                             &optional slot-name)
1995   (initialize-method-function initargs)
1996   (let ((parsed ())
1997         (unparsed ()))
1998     ;; Figure out whether we got class objects or class names as the
1999     ;; specializers and set parsed and unparsed appropriately. If we
2000     ;; got class objects, then we can compute unparsed, but if we got
2001     ;; class names we don't try to compute parsed.
2002     ;;
2003     ;; Note that the use of not symbolp in this call to every should be
2004     ;; read as 'classp' we can't use classp itself because it doesn't
2005     ;; exist yet.
2006     (if (every (lambda (s) (not (symbolp s))) specializers)
2007         (setq parsed specializers
2008               unparsed (mapcar (lambda (s)
2009                                  (if (eq s t) t (class-name s)))
2010                                specializers))
2011         (setq unparsed specializers
2012               parsed ()))
2013     (list :early-method           ;This is an early method dammit!
2014
2015           (getf initargs :function)
2016           (getf initargs :fast-function)
2017
2018           parsed                  ;The parsed specializers. This is used
2019                                   ;by early-method-specializers to cache
2020                                   ;the parse. Note that this only comes
2021                                   ;into play when there is more than one
2022                                   ;early method on an early gf.
2023
2024           (list class        ;A list to which real-make-a-method
2025                 qualifiers      ;can be applied to make a real method
2026                 arglist    ;corresponding to this early one.
2027                 unparsed
2028                 initargs
2029                 doc
2030                 slot-name))))
2031
2032 (defun real-make-a-method
2033        (class qualifiers lambda-list specializers initargs doc
2034         &optional slot-name)
2035   (setq specializers (parse-specializers specializers))
2036   (apply #'make-instance class
2037          :qualifiers qualifiers
2038          :lambda-list lambda-list
2039          :specializers specializers
2040          :documentation doc
2041          :slot-name slot-name
2042          :allow-other-keys t
2043          initargs))
2044
2045 (defun early-method-function (early-method)
2046   (values (cadr early-method) (caddr early-method)))
2047
2048 (defun early-method-class (early-method)
2049   (find-class (car (fifth early-method))))
2050
2051 (defun early-method-standard-accessor-p (early-method)
2052   (let ((class (first (fifth early-method))))
2053     (or (eq class 'standard-reader-method)
2054         (eq class 'standard-writer-method)
2055         (eq class 'standard-boundp-method))))
2056
2057 (defun early-method-standard-accessor-slot-name (early-method)
2058   (seventh (fifth early-method)))
2059
2060 ;;; Fetch the specializers of an early method. This is basically just
2061 ;;; a simple accessor except that when the second argument is t, this
2062 ;;; converts the specializers from symbols into class objects. The
2063 ;;; class objects are cached in the early method, this makes
2064 ;;; bootstrapping faster because the class objects only have to be
2065 ;;; computed once.
2066 ;;;
2067 ;;; NOTE:
2068 ;;;  The second argument should only be passed as T by
2069 ;;;  early-lookup-method. This is to implement the rule that only when
2070 ;;;  there is more than one early method on a generic function is the
2071 ;;;  conversion from class names to class objects done. This
2072 ;;;  corresponds to the fact that we are only allowed to have one
2073 ;;;  method on any generic function up until the time classes exist.
2074 (defun early-method-specializers (early-method &optional objectsp)
2075   (if (and (listp early-method)
2076            (eq (car early-method) :early-method))
2077       (cond ((eq objectsp t)
2078              (or (fourth early-method)
2079                  (setf (fourth early-method)
2080                        (mapcar #'find-class (cadddr (fifth early-method))))))
2081             (t
2082              (cadddr (fifth early-method))))
2083       (error "~S is not an early-method." early-method)))
2084
2085 (defun early-method-qualifiers (early-method)
2086   (cadr (fifth early-method)))
2087
2088 (defun early-method-lambda-list (early-method)
2089   (caddr (fifth early-method)))
2090
2091 (defun early-add-named-method (generic-function-name
2092                                qualifiers
2093                                specializers
2094                                arglist
2095                                &rest initargs)
2096   (let* ((gf (ensure-generic-function generic-function-name))
2097          (existing
2098            (dolist (m (early-gf-methods gf))
2099              (when (and (equal (early-method-specializers m) specializers)
2100                         (equal (early-method-qualifiers m) qualifiers))
2101                (return m))))
2102          (new (make-a-method 'standard-method
2103                              qualifiers
2104                              arglist
2105                              specializers
2106                              initargs
2107                              ())))
2108     (when existing (remove-method gf existing))
2109     (add-method gf new)))
2110
2111 ;;; This is the early version of ADD-METHOD. Later this will become a
2112 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2113 ;;; special knowledge about ADD-METHOD.
2114 (defun add-method (generic-function method)
2115   (when (not (fsc-instance-p generic-function))
2116     (error "Early ADD-METHOD didn't get a funcallable instance."))
2117   (when (not (and (listp method) (eq (car method) :early-method)))
2118     (error "Early ADD-METHOD didn't get an early method."))
2119   (push method (early-gf-methods generic-function))
2120   (set-arg-info generic-function :new-method method)
2121   (unless (assoc (!early-gf-name generic-function)
2122                  *!generic-function-fixups*
2123                  :test #'equal)
2124     (update-dfun generic-function)))
2125
2126 ;;; This is the early version of REMOVE-METHOD. See comments on
2127 ;;; the early version of ADD-METHOD.
2128 (defun remove-method (generic-function method)
2129   (when (not (fsc-instance-p generic-function))
2130     (error "An early remove-method didn't get a funcallable instance."))
2131   (when (not (and (listp method) (eq (car method) :early-method)))
2132     (error "An early remove-method didn't get an early method."))
2133   (setf (early-gf-methods generic-function)
2134         (remove method (early-gf-methods generic-function)))
2135   (set-arg-info generic-function)
2136   (unless (assoc (!early-gf-name generic-function)
2137                  *!generic-function-fixups*
2138                  :test #'equal)
2139     (update-dfun generic-function)))
2140
2141 ;;; This is the early version of GET-METHOD. See comments on the early
2142 ;;; version of ADD-METHOD.
2143 (defun get-method (generic-function qualifiers specializers
2144                                     &optional (errorp t))
2145   (if (early-gf-p generic-function)
2146       (or (dolist (m (early-gf-methods generic-function))
2147             (when (and (or (equal (early-method-specializers m nil)
2148                                   specializers)
2149                            (equal (early-method-specializers m t)
2150                                   specializers))
2151                        (equal (early-method-qualifiers m) qualifiers))
2152               (return m)))
2153           (if errorp
2154               (error "can't get early method")
2155               nil))
2156       (real-get-method generic-function qualifiers specializers errorp)))
2157
2158 (defun !fix-early-generic-functions ()
2159   (let ((accessors nil))
2160     ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2161     ;; FIX-EARLY-GENERIC-FUNCTIONS.
2162     (dolist (early-gf-spec *!early-generic-functions*)
2163       (when (every #'early-method-standard-accessor-p
2164                    (early-gf-methods (gdefinition early-gf-spec)))
2165         (push early-gf-spec accessors)))
2166     (dolist (spec (nconc accessors
2167                          '(accessor-method-slot-name
2168                            generic-function-methods
2169                            method-specializers
2170                            specializerp
2171                            specializer-type
2172                            specializer-class
2173                            slot-definition-location
2174                            slot-definition-name
2175                            class-slots
2176                            gf-arg-info
2177                            class-precedence-list
2178                            slot-boundp-using-class
2179                            (setf slot-value-using-class)
2180                            slot-value-using-class
2181                            structure-class-p
2182                            standard-class-p
2183                            funcallable-standard-class-p
2184                            specializerp)))
2185       (/show spec)
2186       (setq *!early-generic-functions*
2187             (cons spec
2188                   (delete spec *!early-generic-functions* :test #'equal))))
2189
2190     (dolist (early-gf-spec *!early-generic-functions*)
2191       (/show early-gf-spec)
2192       (let* ((gf (gdefinition early-gf-spec))
2193              (methods (mapcar (lambda (early-method)
2194                                 (let ((args (copy-list (fifth
2195                                                         early-method))))
2196                                   (setf (fourth args)
2197                                         (early-method-specializers
2198                                          early-method t))
2199                                   (apply #'real-make-a-method args)))
2200                               (early-gf-methods gf))))
2201         (setf (generic-function-method-class gf) *the-class-standard-method*)
2202         (setf (generic-function-method-combination gf)
2203               *standard-method-combination*)
2204         (set-methods gf methods)))
2205
2206     (dolist (fn *!early-functions*)
2207       (/show fn)
2208       (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2209
2210     (dolist (fixup *!generic-function-fixups*)
2211       (/show fixup)
2212       (let* ((fspec (car fixup))
2213              (gf (gdefinition fspec))
2214              (methods (mapcar (lambda (method)
2215                                 (let* ((lambda-list (first method))
2216                                        (specializers (second method))
2217                                        (method-fn-name (third method))
2218                                        (fn-name (or method-fn-name fspec))
2219                                        (fn (fdefinition fn-name))
2220                                        (initargs
2221                                         (list :function
2222                                               (set-fun-name
2223                                                (lambda (args next-methods)
2224                                                  (declare (ignore
2225                                                            next-methods))
2226                                                  (apply fn args))
2227                                                `(call ,fn-name)))))
2228                                   (declare (type function fn))
2229                                   (make-a-method 'standard-method
2230                                                  ()
2231                                                  lambda-list
2232                                                  specializers
2233                                                  initargs
2234                                                  nil)))
2235                               (cdr fixup))))
2236         (setf (generic-function-method-class gf) *the-class-standard-method*)
2237         (setf (generic-function-method-combination gf)
2238               *standard-method-combination*)
2239         (set-methods gf methods))))
2240   (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2241 \f
2242 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2243 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2244 ;;; is really implemented.
2245 (defun parse-defmethod (cdr-of-form)
2246   (declare (list cdr-of-form))
2247   (let ((name (pop cdr-of-form))
2248         (qualifiers ())
2249         (spec-ll ()))
2250     (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2251               (push (pop cdr-of-form) qualifiers)
2252               (return (setq qualifiers (nreverse qualifiers)))))
2253     (setq spec-ll (pop cdr-of-form))
2254     (values name qualifiers spec-ll cdr-of-form)))
2255
2256 (defun parse-specializers (specializers)
2257   (declare (list specializers))
2258   (flet ((parse (spec)
2259            (let ((result (specializer-from-type spec)))
2260              (if (specializerp result)
2261                  result
2262                  (if (symbolp spec)
2263                      (error "~S was used as a specializer,~%~
2264                              but is not the name of a class."
2265                             spec)
2266                      (error "~S is not a legal specializer." spec))))))
2267     (mapcar #'parse specializers)))
2268
2269 (defun unparse-specializers (specializers-or-method)
2270   (if (listp specializers-or-method)
2271       (flet ((unparse (spec)
2272                (if (specializerp spec)
2273                    (let ((type (specializer-type spec)))
2274                      (if (and (consp type)
2275                               (eq (car type) 'class))
2276                          (let* ((class (cadr type))
2277                                 (class-name (class-name class)))
2278                            (if (eq class (find-class class-name nil))
2279                                class-name
2280                                type))
2281                          type))
2282                    (error "~S is not a legal specializer." spec))))
2283         (mapcar #'unparse specializers-or-method))
2284       (unparse-specializers (method-specializers specializers-or-method))))
2285
2286 (defun parse-method-or-spec (spec &optional (errorp t))
2287   (let (gf method name temp)
2288     (if (method-p spec) 
2289         (setq method spec
2290               gf (method-generic-function method)
2291               temp (and gf (generic-function-name gf))
2292               name (if temp
2293                        (make-method-spec temp
2294                                          (method-qualifiers method)
2295                                          (unparse-specializers
2296                                           (method-specializers method)))
2297                        (make-symbol (format nil "~S" method))))
2298         (multiple-value-bind (gf-spec quals specls)
2299             (parse-defmethod spec)
2300           (and (setq gf (and (or errorp (gboundp gf-spec))
2301                              (gdefinition gf-spec)))
2302                (let ((nreq (compute-discriminating-function-arglist-info gf)))
2303                  (setq specls (append (parse-specializers specls)
2304                                       (make-list (- nreq (length specls))
2305                                                  :initial-element
2306                                                  *the-class-t*)))
2307                  (and
2308                    (setq method (get-method gf quals specls errorp))
2309                    (setq name
2310                          (make-method-spec
2311                           gf-spec quals (unparse-specializers specls))))))))
2312     (values gf method name)))
2313 \f
2314 (defun extract-parameters (specialized-lambda-list)
2315   (multiple-value-bind (parameters ignore1 ignore2)
2316       (parse-specialized-lambda-list specialized-lambda-list)
2317     (declare (ignore ignore1 ignore2))
2318     parameters))
2319
2320 (defun extract-lambda-list (specialized-lambda-list)
2321   (multiple-value-bind (ignore1 lambda-list ignore2)
2322       (parse-specialized-lambda-list specialized-lambda-list)
2323     (declare (ignore ignore1 ignore2))
2324     lambda-list))
2325
2326 (defun extract-specializer-names (specialized-lambda-list)
2327   (multiple-value-bind (ignore1 ignore2 specializers)
2328       (parse-specialized-lambda-list specialized-lambda-list)
2329     (declare (ignore ignore1 ignore2))
2330     specializers))
2331
2332 (defun extract-required-parameters (specialized-lambda-list)
2333   (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2334       (parse-specialized-lambda-list specialized-lambda-list)
2335     (declare (ignore ignore1 ignore2 ignore3))
2336     required-parameters))
2337
2338 (define-condition specialized-lambda-list-error
2339     (reference-condition simple-program-error)
2340   ()
2341   (:default-initargs :references (list '(:ansi-cl :section (3 4 3)))))
2342
2343 (defun parse-specialized-lambda-list
2344     (arglist
2345      &optional supplied-keywords (allowed-keywords '(&optional &rest &key &aux))
2346      &aux (specialized-lambda-list-keywords
2347            '(&optional &rest &key &allow-other-keys &aux)))
2348   (let ((arg (car arglist)))
2349     (cond ((null arglist) (values nil nil nil nil))
2350           ((eq arg '&aux)
2351            (values nil arglist nil nil))
2352           ((memq arg lambda-list-keywords)
2353            ;; non-standard lambda-list-keywords are errors.
2354            (unless (memq arg specialized-lambda-list-keywords)
2355              (error 'specialized-lambda-list-error
2356                     :format-control "unknown specialized-lambda-list ~
2357                                      keyword ~S~%"
2358                     :format-arguments (list arg)))
2359            ;; no multiple &rest x &rest bla specifying
2360            (when (memq arg supplied-keywords)
2361              (error 'specialized-lambda-list-error
2362                     :format-control "multiple occurrence of ~
2363                                      specialized-lambda-list keyword ~S~%"
2364                     :format-arguments (list arg)))
2365            ;; And no placing &key in front of &optional, either.
2366            (unless (memq arg allowed-keywords)
2367              (error 'specialized-lambda-list-error
2368                     :format-control "misplaced specialized-lambda-list ~
2369                                      keyword ~S~%"
2370                     :format-arguments (list arg)))
2371            ;; When we are at a lambda-list keyword, the parameters
2372            ;; don't include the lambda-list keyword; the lambda-list
2373            ;; does include the lambda-list keyword; and no
2374            ;; specializers are allowed to follow the lambda-list
2375            ;; keywords (at least for now).
2376            (multiple-value-bind (parameters lambda-list)
2377                (parse-specialized-lambda-list (cdr arglist)
2378                                               (cons arg supplied-keywords)
2379                                               (if (eq arg '&key)
2380                                                   (cons '&allow-other-keys
2381                                                         (cdr (member arg allowed-keywords)))
2382                                                 (cdr (member arg allowed-keywords))))
2383              (when (and (eq arg '&rest)
2384                         (or (null lambda-list)
2385                             (memq (car lambda-list)
2386                                   specialized-lambda-list-keywords)
2387                             (not (or (null (cadr lambda-list))
2388                                      (memq (cadr lambda-list)
2389                                            specialized-lambda-list-keywords)))))
2390                (error 'specialized-lambda-list-error
2391                       :format-control
2392                       "in a specialized-lambda-list, excactly one ~
2393                        variable must follow &REST.~%"
2394                       :format-arguments nil))
2395              (values parameters
2396                      (cons arg lambda-list)
2397                      ()
2398                      ())))
2399           (supplied-keywords
2400            ;; After a lambda-list keyword there can be no specializers.
2401            (multiple-value-bind (parameters lambda-list)
2402                (parse-specialized-lambda-list (cdr arglist)
2403                                               supplied-keywords
2404                                               allowed-keywords)
2405              (values (cons (if (listp arg) (car arg) arg) parameters)
2406                      (cons arg lambda-list)
2407                      ()
2408                      ())))
2409           (t
2410            (multiple-value-bind (parameters lambda-list specializers required)
2411                (parse-specialized-lambda-list (cdr arglist))
2412              (values (cons (if (listp arg) (car arg) arg) parameters)
2413                      (cons (if (listp arg) (car arg) arg) lambda-list)
2414                      (cons (if (listp arg) (cadr arg) t) specializers)
2415                      (cons (if (listp arg) (car arg) arg) required)))))))
2416 \f
2417 (setq *boot-state* 'early)
2418 \f
2419 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2420 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2421 ;;; walker stuff was only used for implementing stuff like that; maybe
2422 ;;; it's not needed any more? Hunt down what it was used for and see.
2423
2424 (defmacro with-slots (slots instance &body body)
2425   (let ((in (gensym)))
2426     `(let ((,in ,instance))
2427        (declare (ignorable ,in))
2428        ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2429                              (third instance)
2430                              instance)))
2431            (and (symbolp instance)
2432                 `((declare (%variable-rebinding ,in ,instance)))))
2433        ,in
2434        (symbol-macrolet ,(mapcar (lambda (slot-entry)
2435                                    (let ((var-name
2436                                           (if (symbolp slot-entry)
2437                                               slot-entry
2438                                               (car slot-entry)))
2439                                          (slot-name
2440                                           (if (symbolp slot-entry)
2441                                               slot-entry
2442                                               (cadr slot-entry))))
2443                                      `(,var-name
2444                                        (slot-value ,in ',slot-name))))
2445                                  slots)
2446                         ,@body))))
2447
2448 (defmacro with-accessors (slots instance &body body)
2449   (let ((in (gensym)))
2450     `(let ((,in ,instance))
2451        (declare (ignorable ,in))
2452        ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2453                              (third instance)
2454                              instance)))
2455            (and (symbolp instance)
2456                 `((declare (%variable-rebinding ,in ,instance)))))
2457        ,in
2458        (symbol-macrolet ,(mapcar (lambda (slot-entry)
2459                                    (let ((var-name (car slot-entry))
2460                                          (accessor-name (cadr slot-entry)))
2461                                      `(,var-name (,accessor-name ,in))))
2462                                  slots)
2463           ,@body))))