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