00337250a792612c949f3e914ad8005f41a1e111
[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 (cadr 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                  ,(locally
1072                    ;; As above, this declaration supresses code
1073                    ;; deletion notes.
1074                    (declare (optimize (inhibit-warnings 3)))
1075                    (if (and (null ',rest-arg)
1076                             (consp cnm-args)
1077                             (eq (car cnm-args) 'list))
1078                        `(call-no-next-method ',method-name-declaration
1079                                              ,@(cdr cnm-args))
1080                        `(call-no-next-method ',method-name-declaration
1081                                              ,@',args
1082                                              ,@',(when rest-arg
1083                                                        `(,rest-arg)))))))
1084               (next-method-p-body ()
1085                `(not (null ,',next-method-call))))
1086     ,@body))
1087
1088 (defmacro bind-lexical-method-functions
1089     ((&key call-next-method-p next-method-p-p
1090            closurep applyp method-name-declaration)
1091      &body body)
1092   (cond ((and (null call-next-method-p) (null next-method-p-p)
1093               (null closurep)
1094               (null applyp))
1095          `(let () ,@body))
1096         ((and (null closurep)
1097               (null applyp))
1098          ;; OK to use MACROLET, and all args are mandatory
1099          ;; (else APPLYP would be true).
1100          `(call-next-method-bind
1101             (macrolet ((call-next-method (&rest cnm-args)
1102                          `(call-next-method-body ,',method-name-declaration
1103                                                  ,(when cnm-args
1104                                                     `(list ,@cnm-args))))
1105                        (next-method-p ()
1106                          `(next-method-p-body)))
1107                ,@body)))
1108         (t
1109          `(call-next-method-bind
1110             (flet (,@(and call-next-method-p
1111                           `((call-next-method (&rest cnm-args)
1112                              (call-next-method-body
1113                               ,method-name-declaration
1114                               cnm-args))))
1115                    ,@(and next-method-p-p
1116                           '((next-method-p ()
1117                               (next-method-p-body)))))
1118               ,@body)))))
1119
1120 (defmacro bind-args ((lambda-list args) &body body)
1121   (let ((args-tail '.args-tail.)
1122         (key '.key.)
1123         (state 'required))
1124     (flet ((process-var (var)
1125              (if (memq var lambda-list-keywords)
1126                  (progn
1127                    (case var
1128                      (&optional       (setq state 'optional))
1129                      (&key            (setq state 'key))
1130                      (&allow-other-keys)
1131                      (&rest           (setq state 'rest))
1132                      (&aux            (setq state 'aux))
1133                      (otherwise
1134                       (error
1135                        "encountered the non-standard lambda list keyword ~S"
1136                        var)))
1137                    nil)
1138                  (case state
1139                    (required `((,var (pop ,args-tail))))
1140                    (optional (cond ((not (consp var))
1141                                     `((,var (when ,args-tail
1142                                               (pop ,args-tail)))))
1143                                    ((null (cddr var))
1144                                     `((,(car var) (if ,args-tail
1145                                                       (pop ,args-tail)
1146                                                       ,(cadr var)))))
1147                                    (t
1148                                     `((,(caddr var) ,args-tail)
1149                                       (,(car var) (if ,args-tail
1150                                                       (pop ,args-tail)
1151                                                       ,(cadr var)))))))
1152                    (rest `((,var ,args-tail)))
1153                    (key (cond ((not (consp var))
1154                                `((,var (car
1155                                         (get-key-arg-tail ,(keywordicate var)
1156                                                           ,args-tail)))))
1157                               ((null (cddr var))
1158                                (multiple-value-bind (keyword variable)
1159                                    (if (consp (car var))
1160                                        (values (caar var)
1161                                                (cadar var))
1162                                        (values (keywordicate (car var))
1163                                                (car var)))
1164                                  `((,key (get-key-arg-tail ',keyword
1165                                                            ,args-tail))
1166                                    (,variable (if ,key
1167                                                   (car ,key)
1168                                                   ,(cadr var))))))
1169                               (t
1170                                (multiple-value-bind (keyword variable)
1171                                    (if (consp (car var))
1172                                        (values (caar var)
1173                                                (cadar var))
1174                                        (values (keywordicate (car var))
1175                                                (car var)))
1176                                  `((,key (get-key-arg-tail ',keyword
1177                                                            ,args-tail))
1178                                    (,(caddr var) ,key)
1179                                    (,variable (if ,key
1180                                                   (car ,key)
1181                                                   ,(cadr var))))))))
1182                    (aux `(,var))))))
1183       (let ((bindings (mapcan #'process-var lambda-list)))
1184         `(let* ((,args-tail ,args)
1185                 ,@bindings)
1186            (declare (ignorable ,args-tail))
1187            ,@body)))))
1188
1189 (defun get-key-arg-tail (keyword list)
1190   (loop for (key . tail) on list by #'cddr
1191         when (null tail) do
1192           ;; FIXME: Do we want to export this symbol? Or maybe use an
1193           ;; (ERROR 'SIMPLE-PROGRAM-ERROR) form?
1194           (sb-c::%odd-key-args-error)
1195         when (eq key keyword)
1196           return tail))
1197
1198 (defun walk-method-lambda (method-lambda required-parameters env slots calls)
1199   (let ((call-next-method-p nil)   ; flag indicating that CALL-NEXT-METHOD
1200                                    ; should be in the method definition
1201         (closurep nil)             ; flag indicating that #'CALL-NEXT-METHOD
1202                                    ; was seen in the body of a method
1203         (next-method-p-p nil))     ; flag indicating that NEXT-METHOD-P
1204                                    ; should be in the method definition
1205     (flet ((walk-function (form context env)
1206              (cond ((not (eq context :eval)) form)
1207                    ;; FIXME: Jumping to a conclusion from the way it's used
1208                    ;; above, perhaps CONTEXT should be called SITUATION
1209                    ;; (after the term used in the ANSI specification of
1210                    ;; EVAL-WHEN) and given modern ANSI keyword values
1211                    ;; like :LOAD-TOPLEVEL.
1212                    ((not (listp form)) form)
1213                    ((eq (car form) 'call-next-method)
1214                     (setq call-next-method-p t)
1215                     form)
1216                    ((eq (car form) 'next-method-p)
1217                     (setq next-method-p-p t)
1218                     form)
1219                    ((and (eq (car form) 'function)
1220                          (cond ((eq (cadr form) 'call-next-method)
1221                                 (setq call-next-method-p t)
1222                                 (setq closurep t)
1223                                 form)
1224                                ((eq (cadr form) 'next-method-p)
1225                                 (setq next-method-p-p t)
1226                                 (setq closurep t)
1227                                 form)
1228                                (t nil))))
1229                    ((and (memq (car form)
1230                                '(slot-value set-slot-value slot-boundp))
1231                          (constantp (caddr form)))
1232                      (let ((parameter (can-optimize-access form
1233                                                            required-parameters
1234                                                            env)))
1235                       (let ((fun (ecase (car form)
1236                                    (slot-value #'optimize-slot-value)
1237                                    (set-slot-value #'optimize-set-slot-value)
1238                                    (slot-boundp #'optimize-slot-boundp))))
1239                         (funcall fun slots parameter form))))
1240                    ((and (eq (car form) 'apply)
1241                          (consp (cadr form))
1242                          (eq (car (cadr form)) 'function)
1243                          (generic-function-name-p (cadr (cadr form))))
1244                     (optimize-generic-function-call
1245                      form required-parameters env slots calls))
1246                    ((generic-function-name-p (car form))
1247                     (optimize-generic-function-call
1248                      form required-parameters env slots calls))
1249                    ((and (eq (car form) 'asv-funcall)
1250                          *optimize-asv-funcall-p*)
1251                     (case (fourth form)
1252                       (reader (push (third form) *asv-readers*))
1253                       (writer (push (third form) *asv-writers*))
1254                       (boundp (push (third form) *asv-boundps*)))
1255                     `(,(second form) ,@(cddddr form)))
1256                    (t form))))
1257
1258       (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1259         (values walked-lambda
1260                 call-next-method-p
1261                 closurep
1262                 next-method-p-p)))))
1263
1264 (defun generic-function-name-p (name)
1265   (and (legal-fun-name-p name)
1266        (gboundp name)
1267        (if (eq *boot-state* 'complete)
1268            (standard-generic-function-p (gdefinition name))
1269            (funcallable-instance-p (gdefinition name)))))
1270 \f
1271 (defvar *method-function-plist* (make-hash-table :test 'eq))
1272 (defvar *mf1* nil)
1273 (defvar *mf1p* nil)
1274 (defvar *mf1cp* nil)
1275 (defvar *mf2* nil)
1276 (defvar *mf2p* nil)
1277 (defvar *mf2cp* nil)
1278
1279 (defun method-function-plist (method-function)
1280   (unless (eq method-function *mf1*)
1281     (rotatef *mf1* *mf2*)
1282     (rotatef *mf1p* *mf2p*)
1283     (rotatef *mf1cp* *mf2cp*))
1284   (unless (or (eq method-function *mf1*) (null *mf1cp*))
1285     (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1286   (unless (eq method-function *mf1*)
1287     (setf *mf1* method-function
1288           *mf1cp* nil
1289           *mf1p* (gethash method-function *method-function-plist*)))
1290   *mf1p*)
1291
1292 (defun (setf method-function-plist)
1293     (val method-function)
1294   (unless (eq method-function *mf1*)
1295     (rotatef *mf1* *mf2*)
1296     (rotatef *mf1cp* *mf2cp*)
1297     (rotatef *mf1p* *mf2p*))
1298   (unless (or (eq method-function *mf1*) (null *mf1cp*))
1299     (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1300   (setf *mf1* method-function
1301         *mf1cp* t
1302         *mf1p* val))
1303
1304 (defun method-function-get (method-function key &optional default)
1305   (getf (method-function-plist method-function) key default))
1306
1307 (defun (setf method-function-get)
1308     (val method-function key)
1309   (setf (getf (method-function-plist method-function) key) val))
1310
1311 (defun method-function-pv-table (method-function)
1312   (method-function-get method-function :pv-table))
1313
1314 (defun method-function-method (method-function)
1315   (method-function-get method-function :method))
1316
1317 (defun method-function-needs-next-methods-p (method-function)
1318   (method-function-get method-function :needs-next-methods-p t))
1319 \f
1320 (defmacro method-function-closure-generator (method-function)
1321   `(method-function-get ,method-function 'closure-generator))
1322
1323 (defun load-defmethod
1324     (class name quals specls ll initargs &optional pv-table-symbol)
1325   (setq initargs (copy-tree initargs))
1326   (let ((method-spec (or (getf initargs :method-spec)
1327                          (make-method-spec name quals specls))))
1328     (setf (getf initargs :method-spec) method-spec)
1329     (load-defmethod-internal class name quals specls
1330                              ll initargs pv-table-symbol)))
1331
1332 (defun load-defmethod-internal
1333     (method-class gf-spec qualifiers specializers lambda-list
1334                   initargs pv-table-symbol)
1335   (when pv-table-symbol
1336     (setf (getf (getf initargs :plist) :pv-table-symbol)
1337           pv-table-symbol))
1338   (when (and (eq *boot-state* 'complete)
1339              (fboundp gf-spec))
1340     (let* ((gf (fdefinition gf-spec))
1341            (method (and (generic-function-p gf)
1342                         (find-method gf
1343                                      qualifiers
1344                                      (parse-specializers specializers)
1345                                      nil))))
1346       (when method
1347         (sb-kernel::style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1348                                gf-spec qualifiers specializers))))
1349   (let ((method (apply #'add-named-method
1350                        gf-spec qualifiers specializers lambda-list
1351                        :definition-source `((defmethod ,gf-spec
1352                                                 ,@qualifiers
1353                                               ,specializers)
1354                                             ,*load-pathname*)
1355                        initargs)))
1356     (unless (or (eq method-class 'standard-method)
1357                 (eq (find-class method-class nil) (class-of method)))
1358       ;; FIXME: should be STYLE-WARNING?
1359       (format *error-output*
1360               "~&At the time the method with qualifiers ~:S and~%~
1361                specializers ~:S on the generic function ~S~%~
1362                was compiled, the method-class for that generic function was~%~
1363                ~S. But, the method class is now ~S, this~%~
1364                may mean that this method was compiled improperly.~%"
1365               qualifiers specializers gf-spec
1366               method-class (class-name (class-of method))))
1367     method))
1368
1369 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1370   `(method ,gf-spec ,@qualifiers ,unparsed-specializers))
1371
1372 (defun initialize-method-function (initargs &optional return-function-p method)
1373   (let* ((mf (getf initargs :function))
1374          (method-spec (getf initargs :method-spec))
1375          (plist (getf initargs :plist))
1376          (pv-table-symbol (getf plist :pv-table-symbol))
1377          (pv-table nil)
1378          (mff (getf initargs :fast-function)))
1379     (flet ((set-mf-property (p v)
1380              (when mf
1381                (setf (method-function-get mf p) v))
1382              (when mff
1383                (setf (method-function-get mff p) v))))
1384       (when method-spec
1385         (when mf
1386           (setq mf (set-fun-name mf method-spec)))
1387         (when mff
1388           (let ((name `(,(or (get (car method-spec) 'fast-sym)
1389                              (setf (get (car method-spec) 'fast-sym)
1390                                    ;; KLUDGE: If we're going to be
1391                                    ;; interning private symbols in our
1392                                    ;; a this way, it would be cleanest
1393                                    ;; to use a separate package
1394                                    ;; %PCL-PRIVATE or something, and
1395                                    ;; failing that, to use a special
1396                                    ;; symbol prefix denoting privateness.
1397                                    ;; -- WHN 19991201
1398                                    (intern (format nil "FAST-~A"
1399                                                    (car method-spec))
1400                                            *pcl-package*)))
1401                          ,@(cdr method-spec))))
1402             (set-fun-name mff name)
1403             (unless mf
1404               (set-mf-property :name name)))))
1405       (when plist
1406         (let ((snl (getf plist :slot-name-lists))
1407               (cl (getf plist :call-list)))
1408           (when (or snl cl)
1409             (setq pv-table (intern-pv-table :slot-name-lists snl
1410                                             :call-list cl))
1411             (when pv-table (set pv-table-symbol pv-table))
1412             (set-mf-property :pv-table pv-table)))
1413         (loop (when (null plist) (return nil))
1414               (set-mf-property (pop plist) (pop plist)))
1415         (when method
1416           (set-mf-property :method method))
1417         (when return-function-p
1418           (or mf (method-function-from-fast-function mff)))))))
1419 \f
1420 (defun analyze-lambda-list (lambda-list)
1421   (flet (;; FIXME: Is this redundant with SB-C::MAKE-KEYWORD-FOR-ARG?
1422          (parse-key-arg (arg)
1423            (if (listp arg)
1424                (if (listp (car arg))
1425                    (caar arg)
1426                    (keywordicate (car arg)))
1427                (keywordicate arg))))
1428     (let ((nrequired 0)
1429           (noptional 0)
1430           (keysp nil)
1431           (restp nil)
1432           (nrest 0)
1433           (allow-other-keys-p nil)
1434           (keywords ())
1435           (keyword-parameters ())
1436           (state 'required))
1437       (dolist (x lambda-list)
1438         (if (memq x lambda-list-keywords)
1439             (case x
1440               (&optional         (setq state 'optional))
1441               (&key              (setq keysp t
1442                                        state 'key))
1443               (&allow-other-keys (setq allow-other-keys-p t))
1444               (&rest             (setq restp t
1445                                        state 'rest))
1446               (&aux           (return t))
1447               (otherwise
1448                 (error "encountered the non-standard lambda list keyword ~S"
1449                        x)))
1450             (ecase state
1451               (required  (incf nrequired))
1452               (optional  (incf noptional))
1453               (key       (push (parse-key-arg x) keywords)
1454                          (push x keyword-parameters))
1455               (rest      (incf nrest)))))
1456       (when (and restp (zerop nrest))
1457         (error "Error in lambda-list:~%~
1458                 After &REST, a DEFGENERIC lambda-list ~
1459                 must be followed by at least one variable."))
1460       (values nrequired noptional keysp restp allow-other-keys-p
1461               (reverse keywords)
1462               (reverse keyword-parameters)))))
1463
1464 (defun keyword-spec-name (x)
1465   (let ((key (if (atom x) x (car x))))
1466     (if (atom key)
1467         (keywordicate key)
1468         (car key))))
1469
1470 (defun ftype-declaration-from-lambda-list (lambda-list name)
1471   (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1472                                   keywords keyword-parameters)
1473       (analyze-lambda-list lambda-list)
1474     (declare (ignore keyword-parameters))
1475     (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1476            (old-ftype (if (sb-kernel:fun-type-p old) old nil))
1477            (old-restp (and old-ftype (sb-kernel:fun-type-rest old-ftype)))
1478            (old-keys (and old-ftype
1479                           (mapcar #'sb-kernel:key-info-name
1480                                   (sb-kernel:fun-type-keywords
1481                                    old-ftype))))
1482            (old-keysp (and old-ftype (sb-kernel:fun-type-keyp old-ftype)))
1483            (old-allowp (and old-ftype
1484                             (sb-kernel:fun-type-allowp old-ftype)))
1485            (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1486       `(function ,(append (make-list nrequired :initial-element t)
1487                           (when (plusp noptional)
1488                             (append '(&optional)
1489                                     (make-list noptional :initial-element t)))
1490                           (when (or restp old-restp)
1491                             '(&rest t))
1492                           (when (or keysp old-keysp)
1493                             (append '(&key)
1494                                     (mapcar (lambda (key)
1495                                               `(,key t))
1496                                             keywords)
1497                                     (when (or allow-other-keys-p old-allowp)
1498                                       '(&allow-other-keys)))))
1499                  *))))
1500
1501 (defun defgeneric-declaration (spec lambda-list)
1502   (when (consp spec)
1503     (setq spec (get-setf-fun-name (cadr spec))))
1504   `(ftype ,(ftype-declaration-from-lambda-list lambda-list spec) ,spec))
1505 \f
1506 ;;;; early generic function support
1507
1508 (defvar *!early-generic-functions* ())
1509
1510 (defun ensure-generic-function (fun-name
1511                                 &rest all-keys
1512                                 &key environment
1513                                 &allow-other-keys)
1514   (declare (ignore environment))
1515   (let ((existing (and (gboundp fun-name)
1516                        (gdefinition fun-name))))
1517     (if (and existing
1518              (eq *boot-state* 'complete)
1519              (null (generic-function-p existing)))
1520         (generic-clobbers-function fun-name)
1521         (apply #'ensure-generic-function-using-class
1522                existing fun-name all-keys))))
1523
1524 (defun generic-clobbers-function (fun-name)
1525   (error 'simple-program-error
1526          :format-control "~S already names an ordinary function or a macro."
1527          :format-arguments (list fun-name)))
1528
1529 (defvar *sgf-wrapper*
1530   (boot-make-wrapper (early-class-size 'standard-generic-function)
1531                      'standard-generic-function))
1532
1533 (defvar *sgf-slots-init*
1534   (mapcar (lambda (canonical-slot)
1535             (if (memq (getf canonical-slot :name) '(arg-info source))
1536                 +slot-unbound+
1537                 (let ((initfunction (getf canonical-slot :initfunction)))
1538                   (if initfunction
1539                       (funcall initfunction)
1540                       +slot-unbound+))))
1541           (early-collect-inheritance 'standard-generic-function)))
1542
1543 (defvar *sgf-method-class-index*
1544   (!bootstrap-slot-index 'standard-generic-function 'method-class))
1545
1546 (defun early-gf-p (x)
1547   (and (fsc-instance-p x)
1548        (eq (clos-slots-ref (get-slots x) *sgf-method-class-index*)
1549            +slot-unbound+)))
1550
1551 (defvar *sgf-methods-index*
1552   (!bootstrap-slot-index 'standard-generic-function 'methods))
1553
1554 (defmacro early-gf-methods (gf)
1555   `(clos-slots-ref (get-slots ,gf) *sgf-methods-index*))
1556
1557 (defvar *sgf-arg-info-index*
1558   (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1559
1560 (defmacro early-gf-arg-info (gf)
1561   `(clos-slots-ref (get-slots ,gf) *sgf-arg-info-index*))
1562
1563 (defvar *sgf-dfun-state-index*
1564   (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1565
1566 (defstruct (arg-info
1567             (:conc-name nil)
1568             (:constructor make-arg-info ())
1569             (:copier nil))
1570   (arg-info-lambda-list :no-lambda-list)
1571   arg-info-precedence
1572   arg-info-metatypes
1573   arg-info-number-optional
1574   arg-info-key/rest-p
1575   arg-info-keys   ;nil        no &KEY or &REST allowed
1576                   ;(k1 k2 ..) Each method must accept these &KEY arguments.
1577                   ;T          must have &KEY or &REST
1578
1579   gf-info-simple-accessor-type ; nil, reader, writer, boundp
1580   (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1581
1582   gf-info-static-c-a-m-emf
1583   (gf-info-c-a-m-emf-std-p t)
1584   gf-info-fast-mf-p)
1585
1586 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1587
1588 (defun arg-info-valid-p (arg-info)
1589   (not (null (arg-info-number-optional arg-info))))
1590
1591 (defun arg-info-applyp (arg-info)
1592   (or (plusp (arg-info-number-optional arg-info))
1593       (arg-info-key/rest-p arg-info)))
1594
1595 (defun arg-info-number-required (arg-info)
1596   (length (arg-info-metatypes arg-info)))
1597
1598 (defun arg-info-nkeys (arg-info)
1599   (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1600
1601 ;;; Keep pages clean by not setting if the value is already the same.
1602 (defmacro esetf (pos val)
1603   (let ((valsym (gensym "value")))
1604     `(let ((,valsym ,val))
1605        (unless (equal ,pos ,valsym)
1606          (setf ,pos ,valsym)))))
1607
1608 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1609                         argument-precedence-order)
1610   (let* ((arg-info (if (eq *boot-state* 'complete)
1611                        (gf-arg-info gf)
1612                        (early-gf-arg-info gf)))
1613          (methods (if (eq *boot-state* 'complete)
1614                       (generic-function-methods gf)
1615                       (early-gf-methods gf)))
1616          (was-valid-p (integerp (arg-info-number-optional arg-info)))
1617          (first-p (and new-method (null (cdr methods)))))
1618     (when (and (not lambda-list-p) methods)
1619       (setq lambda-list (gf-lambda-list gf)))
1620     (when (or lambda-list-p
1621               (and first-p
1622                    (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1623       (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1624           (analyze-lambda-list lambda-list)
1625         (when (and methods (not first-p))
1626           (let ((gf-nreq (arg-info-number-required arg-info))
1627                 (gf-nopt (arg-info-number-optional arg-info))
1628                 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1629             (unless (and (= nreq gf-nreq)
1630                          (= nopt gf-nopt)
1631                          (eq (or keysp restp) gf-key/rest-p))
1632               (error "The lambda-list ~S is incompatible with ~
1633                      existing methods of ~S."
1634                      lambda-list gf))))
1635         (when lambda-list-p
1636           (esetf (arg-info-lambda-list arg-info) lambda-list))
1637         (when (or lambda-list-p argument-precedence-order
1638                   (null (arg-info-precedence arg-info)))
1639           (esetf (arg-info-precedence arg-info)
1640                  (compute-precedence lambda-list nreq
1641                                      argument-precedence-order)))
1642         (esetf (arg-info-metatypes arg-info) (make-list nreq))
1643         (esetf (arg-info-number-optional arg-info) nopt)
1644         (esetf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1645         (esetf (arg-info-keys arg-info)
1646                (if lambda-list-p
1647                    (if allow-other-keys-p t keywords)
1648                    (arg-info-key/rest-p arg-info)))))
1649     (when new-method
1650       (check-method-arg-info gf arg-info new-method))
1651     (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1652     arg-info))
1653
1654 (defun check-method-arg-info (gf arg-info method)
1655   (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1656       (analyze-lambda-list (if (consp method)
1657                                (early-method-lambda-list method)
1658                                (method-lambda-list method)))
1659     (flet ((lose (string &rest args)
1660              (error 'simple-program-error
1661                     :format-control "attempt to add the method ~S ~
1662                                      to the generic function ~S.~%~
1663                                      But ~A"
1664                     :format-arguments (list method gf
1665                                             (apply #'format nil string args))))
1666            (comparison-description (x y)
1667              (if (> x y) "more" "fewer")))
1668       (let ((gf-nreq (arg-info-number-required arg-info))
1669             (gf-nopt (arg-info-number-optional arg-info))
1670             (gf-key/rest-p (arg-info-key/rest-p arg-info))
1671             (gf-keywords (arg-info-keys arg-info)))
1672         (unless (= nreq gf-nreq)
1673           (lose
1674            "the method has ~A required arguments than the generic function."
1675            (comparison-description nreq gf-nreq)))
1676         (unless (= nopt gf-nopt)
1677           (lose
1678            "the method has ~A optional arguments than the generic function."
1679            (comparison-description nopt gf-nopt)))
1680         (unless (eq (or keysp restp) gf-key/rest-p)
1681           (lose
1682            "the method and generic function differ in whether they accept~%~
1683             &REST or &KEY arguments."))
1684         (when (consp gf-keywords)
1685           (unless (or (and restp (not keysp))
1686                       allow-other-keys-p
1687                       (every (lambda (k) (memq k keywords)) gf-keywords))
1688             (lose "the method does not accept each of the &KEY arguments~%~
1689                    ~S."
1690                   gf-keywords)))))))
1691
1692 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1693   (let* ((existing-p (and methods (cdr methods) new-method))
1694          (nreq (length (arg-info-metatypes arg-info)))
1695          (metatypes (if existing-p
1696                         (arg-info-metatypes arg-info)
1697                         (make-list nreq)))
1698          (type (if existing-p
1699                    (gf-info-simple-accessor-type arg-info)
1700                    nil)))
1701     (when (arg-info-valid-p arg-info)
1702       (dolist (method (if new-method (list new-method) methods))
1703         (let* ((specializers (if (or (eq *boot-state* 'complete)
1704                                      (not (consp method)))
1705                                  (method-specializers method)
1706                                  (early-method-specializers method t)))
1707                (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1708                           (class-of method)
1709                           (early-method-class method)))
1710                (new-type (when (and class
1711                                     (or (not (eq *boot-state* 'complete))
1712                                         (eq (generic-function-method-combination gf)
1713                                             *standard-method-combination*)))
1714                            (cond ((eq class *the-class-standard-reader-method*)
1715                                   'reader)
1716                                  ((eq class *the-class-standard-writer-method*)
1717                                   'writer)
1718                                  ((eq class *the-class-standard-boundp-method*)
1719                                   'boundp)))))
1720           (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1721           (setq type (cond ((null type) new-type)
1722                            ((eq type new-type) type)
1723                            (t nil)))))
1724       (esetf (arg-info-metatypes arg-info) metatypes)
1725       (esetf (gf-info-simple-accessor-type arg-info) type)))
1726   (when (or (not was-valid-p) first-p)
1727     (multiple-value-bind (c-a-m-emf std-p)
1728         (if (early-gf-p gf)
1729             (values t t)
1730             (compute-applicable-methods-emf gf))
1731       (esetf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1732       (esetf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1733       (unless (gf-info-c-a-m-emf-std-p arg-info)
1734         (esetf (gf-info-simple-accessor-type arg-info) t))))
1735   (unless was-valid-p
1736     (let ((name (if (eq *boot-state* 'complete)
1737                     (generic-function-name gf)
1738                     (!early-gf-name gf))))
1739       (esetf (gf-precompute-dfun-and-emf-p arg-info)
1740              (let* ((sym (if (atom name) name (cadr name)))
1741                     (pkg-list (cons *pcl-package*
1742                                     (package-use-list *pcl-package*))))
1743                (and sym (symbolp sym)
1744                     (not (null (memq (symbol-package sym) pkg-list)))
1745                     (not (find #\space (symbol-name sym))))))))
1746   (esetf (gf-info-fast-mf-p arg-info)
1747          (or (not (eq *boot-state* 'complete))
1748              (let* ((method-class (generic-function-method-class gf))
1749                     (methods (compute-applicable-methods
1750                               #'make-method-lambda
1751                               (list gf (class-prototype method-class)
1752                                     '(lambda) nil))))
1753                (and methods (null (cdr methods))
1754                     (let ((specls (method-specializers (car methods))))
1755                       (and (classp (car specls))
1756                            (eq 'standard-generic-function
1757                                (class-name (car specls)))
1758                            (classp (cadr specls))
1759                            (eq 'standard-method
1760                                (class-name (cadr specls)))))))))
1761   arg-info)
1762
1763 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
1764 ;;;
1765 ;;; The STATIC-SLOTS field of the funcallable instances used as early
1766 ;;; generic functions is used to store the early methods and early
1767 ;;; discriminator code for the early generic function. The static
1768 ;;; slots field of the fins contains a list whose:
1769 ;;;    CAR    -   a list of the early methods on this early gf
1770 ;;;    CADR   -   the early discriminator code for this method
1771 (defun ensure-generic-function-using-class (existing spec &rest keys
1772                                             &key (lambda-list nil
1773                                                               lambda-list-p)
1774                                             argument-precedence-order
1775                                             &allow-other-keys)
1776   (declare (ignore keys))
1777   (cond ((and existing (early-gf-p existing))
1778          existing)
1779         ((assoc spec *!generic-function-fixups* :test #'equal)
1780          (if existing
1781              (make-early-gf spec lambda-list lambda-list-p existing
1782                             argument-precedence-order)
1783              (error "The function ~S is not already defined." spec)))
1784         (existing
1785          (error "~S should be on the list ~S."
1786                 spec
1787                 '*!generic-function-fixups*))
1788         (t
1789          (pushnew spec *!early-generic-functions* :test #'equal)
1790          (make-early-gf spec lambda-list lambda-list-p nil
1791                         argument-precedence-order))))
1792
1793 (defun make-early-gf (spec &optional lambda-list lambda-list-p
1794                       function argument-precedence-order)
1795   (let ((fin (allocate-funcallable-instance *sgf-wrapper* *sgf-slots-init*)))
1796     (set-funcallable-instance-fun
1797      fin
1798      (or function
1799          (if (eq spec 'print-object)
1800              #'(sb-kernel:instance-lambda (instance stream)
1801                  (print-unreadable-object (instance stream :identity t)
1802                    (format stream "std-instance")))
1803              #'(sb-kernel:instance-lambda (&rest args)
1804                  (declare (ignore args))
1805                  (error "The function of the funcallable-instance ~S~
1806                          has not been set." fin)))))
1807     (setf (gdefinition spec) fin)
1808     (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
1809     (!bootstrap-set-slot 'standard-generic-function
1810                          fin
1811                          'source
1812                          *load-pathname*)
1813     (set-fun-name fin spec)
1814     (let ((arg-info (make-arg-info)))
1815       (setf (early-gf-arg-info fin) arg-info)
1816       (when lambda-list-p
1817         (proclaim (defgeneric-declaration spec lambda-list))
1818         (if argument-precedence-order
1819             (set-arg-info fin
1820                           :lambda-list lambda-list
1821                           :argument-precedence-order argument-precedence-order)
1822             (set-arg-info fin :lambda-list lambda-list))))
1823     fin))
1824
1825 (defun set-dfun (gf &optional dfun cache info)
1826   (when cache
1827     (setf (cache-owner cache) gf))
1828   (let ((new-state (if (and dfun (or cache info))
1829                        (list* dfun cache info)
1830                        dfun)))
1831     (if (eq *boot-state* 'complete)
1832         (setf (gf-dfun-state gf) new-state)
1833         (setf (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*)
1834               new-state)))
1835   dfun)
1836
1837 (defun gf-dfun-cache (gf)
1838   (let ((state (if (eq *boot-state* 'complete)
1839                    (gf-dfun-state gf)
1840                    (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1841     (typecase state
1842       (function nil)
1843       (cons (cadr state)))))
1844
1845 (defun gf-dfun-info (gf)
1846   (let ((state (if (eq *boot-state* 'complete)
1847                    (gf-dfun-state gf)
1848                    (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1849     (typecase state
1850       (function nil)
1851       (cons (cddr state)))))
1852
1853 (defvar *sgf-name-index*
1854   (!bootstrap-slot-index 'standard-generic-function 'name))
1855
1856 (defun !early-gf-name (gf)
1857   (clos-slots-ref (get-slots gf) *sgf-name-index*))
1858
1859 (defun gf-lambda-list (gf)
1860   (let ((arg-info (if (eq *boot-state* 'complete)
1861                       (gf-arg-info gf)
1862                       (early-gf-arg-info gf))))
1863     (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
1864         (let ((methods (if (eq *boot-state* 'complete)
1865                            (generic-function-methods gf)
1866                            (early-gf-methods gf))))
1867           (if (null methods)
1868               (progn
1869                 (warn "no way to determine the lambda list for ~S" gf)
1870                 nil)
1871               (let* ((method (car (last methods)))
1872                      (ll (if (consp method)
1873                              (early-method-lambda-list method)
1874                              (method-lambda-list method)))
1875                      (k (member '&key ll)))
1876                 (if k
1877                     (append (ldiff ll (cdr k)) '(&allow-other-keys))
1878                     ll))))
1879         (arg-info-lambda-list arg-info))))
1880
1881 (defmacro real-ensure-gf-internal (gf-class all-keys env)
1882   `(progn
1883      (cond ((symbolp ,gf-class)
1884             (setq ,gf-class (find-class ,gf-class t ,env)))
1885            ((classp ,gf-class))
1886            (t
1887             (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
1888                     class nor a symbol that names a class."
1889                    ,gf-class)))
1890      (remf ,all-keys :generic-function-class)
1891      (remf ,all-keys :environment)
1892      (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
1893        (unless (eq combin '.shes-not-there.)
1894          (setf (getf ,all-keys :method-combination)
1895                (find-method-combination (class-prototype ,gf-class)
1896                                         (car combin)
1897                                         (cdr combin)))))
1898     (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
1899       (unless (eq method-class '.shes-not-there.)
1900         (setf (getf ,all-keys :method-class)
1901                 (find-class method-class t ,env))))))
1902
1903 (defun real-ensure-gf-using-class--generic-function
1904        (existing
1905         fun-name
1906         &rest all-keys
1907         &key environment (lambda-list nil lambda-list-p)
1908              (generic-function-class 'standard-generic-function gf-class-p)
1909         &allow-other-keys)
1910   (real-ensure-gf-internal generic-function-class all-keys environment)
1911   (unless (or (null gf-class-p)
1912               (eq (class-of existing) generic-function-class))
1913     (change-class existing generic-function-class))
1914   (prog1
1915       (apply #'reinitialize-instance existing all-keys)
1916     (when lambda-list-p
1917       (proclaim (defgeneric-declaration fun-name lambda-list)))))
1918
1919 (defun real-ensure-gf-using-class--null
1920        (existing
1921         fun-name
1922         &rest all-keys
1923         &key environment (lambda-list nil lambda-list-p)
1924              (generic-function-class 'standard-generic-function)
1925         &allow-other-keys)
1926   (declare (ignore existing))
1927   (real-ensure-gf-internal generic-function-class all-keys environment)
1928   (prog1
1929       (setf (gdefinition fun-name)
1930             (apply #'make-instance generic-function-class
1931                    :name fun-name all-keys))
1932     (when lambda-list-p
1933       (proclaim (defgeneric-declaration fun-name lambda-list)))))
1934 \f
1935 (defun get-generic-fun-info (gf)
1936   ;; values   nreq applyp metatypes nkeys arg-info
1937   (multiple-value-bind (applyp metatypes arg-info)
1938       (let* ((arg-info (if (early-gf-p gf)
1939                            (early-gf-arg-info gf)
1940                            (gf-arg-info gf)))
1941              (metatypes (arg-info-metatypes arg-info)))
1942         (values (arg-info-applyp arg-info)
1943                 metatypes
1944                 arg-info))
1945     (values (length metatypes) applyp metatypes
1946             (count-if (lambda (x) (neq x t)) metatypes)
1947             arg-info)))
1948
1949 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
1950                             &optional slot-name)
1951   (initialize-method-function initargs)
1952   (let ((parsed ())
1953         (unparsed ()))
1954     ;; Figure out whether we got class objects or class names as the
1955     ;; specializers and set parsed and unparsed appropriately. If we
1956     ;; got class objects, then we can compute unparsed, but if we got
1957     ;; class names we don't try to compute parsed.
1958     ;;
1959     ;; Note that the use of not symbolp in this call to every should be
1960     ;; read as 'classp' we can't use classp itself because it doesn't
1961     ;; exist yet.
1962     (if (every (lambda (s) (not (symbolp s))) specializers)
1963         (setq parsed specializers
1964               unparsed (mapcar (lambda (s)
1965                                  (if (eq s t) t (class-name s)))
1966                                specializers))
1967         (setq unparsed specializers
1968               parsed ()))
1969     (list :early-method           ;This is an early method dammit!
1970
1971           (getf initargs :function)
1972           (getf initargs :fast-function)
1973
1974           parsed                  ;The parsed specializers. This is used
1975                                   ;by early-method-specializers to cache
1976                                   ;the parse. Note that this only comes
1977                                   ;into play when there is more than one
1978                                   ;early method on an early gf.
1979
1980           (list class        ;A list to which real-make-a-method
1981                 qualifiers      ;can be applied to make a real method
1982                 arglist    ;corresponding to this early one.
1983                 unparsed
1984                 initargs
1985                 doc
1986                 slot-name))))
1987
1988 (defun real-make-a-method
1989        (class qualifiers lambda-list specializers initargs doc
1990         &optional slot-name)
1991   (setq specializers (parse-specializers specializers))
1992   (apply #'make-instance class
1993          :qualifiers qualifiers
1994          :lambda-list lambda-list
1995          :specializers specializers
1996          :documentation doc
1997          :slot-name slot-name
1998          :allow-other-keys t
1999          initargs))
2000
2001 (defun early-method-function (early-method)
2002   (values (cadr early-method) (caddr early-method)))
2003
2004 (defun early-method-class (early-method)
2005   (find-class (car (fifth early-method))))
2006
2007 (defun early-method-standard-accessor-p (early-method)
2008   (let ((class (first (fifth early-method))))
2009     (or (eq class 'standard-reader-method)
2010         (eq class 'standard-writer-method)
2011         (eq class 'standard-boundp-method))))
2012
2013 (defun early-method-standard-accessor-slot-name (early-method)
2014   (seventh (fifth early-method)))
2015
2016 ;;; Fetch the specializers of an early method. This is basically just
2017 ;;; a simple accessor except that when the second argument is t, this
2018 ;;; converts the specializers from symbols into class objects. The
2019 ;;; class objects are cached in the early method, this makes
2020 ;;; bootstrapping faster because the class objects only have to be
2021 ;;; computed once.
2022 ;;;
2023 ;;; NOTE:
2024 ;;;  The second argument should only be passed as T by
2025 ;;;  early-lookup-method. This is to implement the rule that only when
2026 ;;;  there is more than one early method on a generic function is the
2027 ;;;  conversion from class names to class objects done. This
2028 ;;;  corresponds to the fact that we are only allowed to have one
2029 ;;;  method on any generic function up until the time classes exist.
2030 (defun early-method-specializers (early-method &optional objectsp)
2031   (if (and (listp early-method)
2032            (eq (car early-method) :early-method))
2033       (cond ((eq objectsp t)
2034              (or (fourth early-method)
2035                  (setf (fourth early-method)
2036                        (mapcar #'find-class (cadddr (fifth early-method))))))
2037             (t
2038              (cadddr (fifth early-method))))
2039       (error "~S is not an early-method." early-method)))
2040
2041 (defun early-method-qualifiers (early-method)
2042   (cadr (fifth early-method)))
2043
2044 (defun early-method-lambda-list (early-method)
2045   (caddr (fifth early-method)))
2046
2047 (defun early-add-named-method (generic-function-name
2048                                qualifiers
2049                                specializers
2050                                arglist
2051                                &rest initargs)
2052   (let* ((gf (ensure-generic-function generic-function-name))
2053          (existing
2054            (dolist (m (early-gf-methods gf))
2055              (when (and (equal (early-method-specializers m) specializers)
2056                         (equal (early-method-qualifiers m) qualifiers))
2057                (return m))))
2058          (new (make-a-method 'standard-method
2059                              qualifiers
2060                              arglist
2061                              specializers
2062                              initargs
2063                              ())))
2064     (when existing (remove-method gf existing))
2065     (add-method gf new)))
2066
2067 ;;; This is the early version of ADD-METHOD. Later this will become a
2068 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2069 ;;; special knowledge about ADD-METHOD.
2070 (defun add-method (generic-function method)
2071   (when (not (fsc-instance-p generic-function))
2072     (error "Early ADD-METHOD didn't get a funcallable instance."))
2073   (when (not (and (listp method) (eq (car method) :early-method)))
2074     (error "Early ADD-METHOD didn't get an early method."))
2075   (push method (early-gf-methods generic-function))
2076   (set-arg-info generic-function :new-method method)
2077   (unless (assoc (!early-gf-name generic-function)
2078                  *!generic-function-fixups*
2079                  :test #'equal)
2080     (update-dfun generic-function)))
2081
2082 ;;; This is the early version of REMOVE-METHOD. See comments on
2083 ;;; the early version of ADD-METHOD.
2084 (defun remove-method (generic-function method)
2085   (when (not (fsc-instance-p generic-function))
2086     (error "An early remove-method didn't get a funcallable instance."))
2087   (when (not (and (listp method) (eq (car method) :early-method)))
2088     (error "An early remove-method didn't get an early method."))
2089   (setf (early-gf-methods generic-function)
2090         (remove method (early-gf-methods generic-function)))
2091   (set-arg-info generic-function)
2092   (unless (assoc (!early-gf-name generic-function)
2093                  *!generic-function-fixups*
2094                  :test #'equal)
2095     (update-dfun generic-function)))
2096
2097 ;;; This is the early version of GET-METHOD. See comments on the early
2098 ;;; version of ADD-METHOD.
2099 (defun get-method (generic-function qualifiers specializers
2100                                     &optional (errorp t))
2101   (if (early-gf-p generic-function)
2102       (or (dolist (m (early-gf-methods generic-function))
2103             (when (and (or (equal (early-method-specializers m nil)
2104                                   specializers)
2105                            (equal (early-method-specializers m t)
2106                                   specializers))
2107                        (equal (early-method-qualifiers m) qualifiers))
2108               (return m)))
2109           (if errorp
2110               (error "can't get early method")
2111               nil))
2112       (real-get-method generic-function qualifiers specializers errorp)))
2113
2114 (defun !fix-early-generic-functions ()
2115   (let ((accessors nil))
2116     ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2117     ;; FIX-EARLY-GENERIC-FUNCTIONS.
2118     (dolist (early-gf-spec *!early-generic-functions*)
2119       (when (every #'early-method-standard-accessor-p
2120                    (early-gf-methods (gdefinition early-gf-spec)))
2121         (push early-gf-spec accessors)))
2122     (dolist (spec (nconc accessors
2123                          '(accessor-method-slot-name
2124                            generic-function-methods
2125                            method-specializers
2126                            specializerp
2127                            specializer-type
2128                            specializer-class
2129                            slot-definition-location
2130                            slot-definition-name
2131                            class-slots
2132                            gf-arg-info
2133                            class-precedence-list
2134                            slot-boundp-using-class
2135                            (setf slot-value-using-class)
2136                            slot-value-using-class
2137                            structure-class-p
2138                            standard-class-p
2139                            funcallable-standard-class-p
2140                            specializerp)))
2141       (/show spec)
2142       (setq *!early-generic-functions*
2143             (cons spec
2144                   (delete spec *!early-generic-functions* :test #'equal))))
2145
2146     (dolist (early-gf-spec *!early-generic-functions*)
2147       (/show early-gf-spec)
2148       (let* ((gf (gdefinition early-gf-spec))
2149              (methods (mapcar (lambda (early-method)
2150                                 (let ((args (copy-list (fifth
2151                                                         early-method))))
2152                                   (setf (fourth args)
2153                                         (early-method-specializers
2154                                          early-method t))
2155                                   (apply #'real-make-a-method args)))
2156                               (early-gf-methods gf))))
2157         (setf (generic-function-method-class gf) *the-class-standard-method*)
2158         (setf (generic-function-method-combination gf)
2159               *standard-method-combination*)
2160         (set-methods gf methods)))
2161
2162     (dolist (fn *!early-functions*)
2163       (/show fn)
2164       (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2165
2166     (dolist (fixup *!generic-function-fixups*)
2167       (/show fixup)
2168       (let* ((fspec (car fixup))
2169              (gf (gdefinition fspec))
2170              (methods (mapcar (lambda (method)
2171                                 (let* ((lambda-list (first method))
2172                                        (specializers (second method))
2173                                        (method-fn-name (third method))
2174                                        (fn-name (or method-fn-name fspec))
2175                                        (fn (fdefinition fn-name))
2176                                        (initargs
2177                                         (list :function
2178                                               (set-fun-name
2179                                                (lambda (args next-methods)
2180                                                  (declare (ignore
2181                                                            next-methods))
2182                                                  (apply fn args))
2183                                                `(call ,fn-name)))))
2184                                   (declare (type function fn))
2185                                   (make-a-method 'standard-method
2186                                                  ()
2187                                                  lambda-list
2188                                                  specializers
2189                                                  initargs
2190                                                  nil)))
2191                               (cdr fixup))))
2192         (setf (generic-function-method-class gf) *the-class-standard-method*)
2193         (setf (generic-function-method-combination gf)
2194               *standard-method-combination*)
2195         (set-methods gf methods))))
2196   (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2197 \f
2198 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2199 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2200 ;;; is really implemented.
2201 (defun parse-defmethod (cdr-of-form)
2202   (declare (list cdr-of-form))
2203   (let ((name (pop cdr-of-form))
2204         (qualifiers ())
2205         (spec-ll ()))
2206     (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2207               (push (pop cdr-of-form) qualifiers)
2208               (return (setq qualifiers (nreverse qualifiers)))))
2209     (setq spec-ll (pop cdr-of-form))
2210     (values name qualifiers spec-ll cdr-of-form)))
2211
2212 (defun parse-specializers (specializers)
2213   (declare (list specializers))
2214   (flet ((parse (spec)
2215            (let ((result (specializer-from-type spec)))
2216              (if (specializerp result)
2217                  result
2218                  (if (symbolp spec)
2219                      (error "~S was used as a specializer,~%~
2220                              but is not the name of a class."
2221                             spec)
2222                      (error "~S is not a legal specializer." spec))))))
2223     (mapcar #'parse specializers)))
2224
2225 (defun unparse-specializers (specializers-or-method)
2226   (if (listp specializers-or-method)
2227       (flet ((unparse (spec)
2228                (if (specializerp spec)
2229                    (let ((type (specializer-type spec)))
2230                      (if (and (consp type)
2231                               (eq (car type) 'class))
2232                          (let* ((class (cadr type))
2233                                 (class-name (class-name class)))
2234                            (if (eq class (find-class class-name nil))
2235                                class-name
2236                                type))
2237                          type))
2238                    (error "~S is not a legal specializer." spec))))
2239         (mapcar #'unparse specializers-or-method))
2240       (unparse-specializers (method-specializers specializers-or-method))))
2241
2242 (defun parse-method-or-spec (spec &optional (errorp t))
2243   (let (gf method name temp)
2244     (if (method-p spec) 
2245         (setq method spec
2246               gf (method-generic-function method)
2247               temp (and gf (generic-function-name gf))
2248               name (if temp
2249                        (intern-fun-name
2250                          (make-method-spec temp
2251                                            (method-qualifiers method)
2252                                            (unparse-specializers
2253                                              (method-specializers method))))
2254                        (make-symbol (format nil "~S" method))))
2255         (multiple-value-bind (gf-spec quals specls)
2256             (parse-defmethod spec)
2257           (and (setq gf (and (or errorp (gboundp gf-spec))
2258                              (gdefinition gf-spec)))
2259                (let ((nreq (compute-discriminating-function-arglist-info gf)))
2260                  (setq specls (append (parse-specializers specls)
2261                                       (make-list (- nreq (length specls))
2262                                                  :initial-element
2263                                                  *the-class-t*)))
2264                  (and
2265                    (setq method (get-method gf quals specls errorp))
2266                    (setq name
2267                          (intern-fun-name (make-method-spec gf-spec
2268                                                             quals
2269                                                             specls))))))))
2270     (values gf method name)))
2271 \f
2272 (defun extract-parameters (specialized-lambda-list)
2273   (multiple-value-bind (parameters ignore1 ignore2)
2274       (parse-specialized-lambda-list specialized-lambda-list)
2275     (declare (ignore ignore1 ignore2))
2276     parameters))
2277
2278 (defun extract-lambda-list (specialized-lambda-list)
2279   (multiple-value-bind (ignore1 lambda-list ignore2)
2280       (parse-specialized-lambda-list specialized-lambda-list)
2281     (declare (ignore ignore1 ignore2))
2282     lambda-list))
2283
2284 (defun extract-specializer-names (specialized-lambda-list)
2285   (multiple-value-bind (ignore1 ignore2 specializers)
2286       (parse-specialized-lambda-list specialized-lambda-list)
2287     (declare (ignore ignore1 ignore2))
2288     specializers))
2289
2290 (defun extract-required-parameters (specialized-lambda-list)
2291   (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2292       (parse-specialized-lambda-list specialized-lambda-list)
2293     (declare (ignore ignore1 ignore2 ignore3))
2294     required-parameters))
2295
2296 (defun parse-specialized-lambda-list (arglist &optional post-keyword)
2297   ;;(declare (values parameters lambda-list specializers required-parameters))
2298   (let ((arg (car arglist)))
2299     (cond ((null arglist) (values nil nil nil nil))
2300           ((eq arg '&aux)
2301            (values nil arglist nil))
2302           ((memq arg lambda-list-keywords)
2303            (unless (memq arg '(&optional &rest &key &allow-other-keys &aux))
2304              ;; Now, since we try to conform to ANSI, non-standard
2305              ;; lambda-list-keywords should be treated as errors.
2306              (error 'simple-program-error
2307                     :format-control "unrecognized lambda-list keyword ~S ~
2308                      in arglist.~%"
2309                     :format-arguments (list arg)))
2310            ;; When we are at a lambda-list keyword, the parameters
2311            ;; don't include the lambda-list keyword; the lambda-list
2312            ;; does include the lambda-list keyword; and no
2313            ;; specializers are allowed to follow the lambda-list
2314            ;; keywords (at least for now).
2315            (multiple-value-bind (parameters lambda-list)
2316                (parse-specialized-lambda-list (cdr arglist) t)
2317              (when (eq arg '&rest)
2318                ;; check, if &rest is followed by a var ...
2319                (when (or (null lambda-list)
2320                          (memq (car lambda-list) lambda-list-keywords))
2321                  (error "Error in lambda-list:~%~
2322                          After &REST, a DEFMETHOD lambda-list ~
2323                          must be followed by at least one variable.")))
2324              (values parameters
2325                      (cons arg lambda-list)
2326                      ()
2327                      ())))
2328           (post-keyword
2329            ;; After a lambda-list keyword there can be no specializers.
2330            (multiple-value-bind (parameters lambda-list)
2331                (parse-specialized-lambda-list (cdr arglist) t)
2332              (values (cons (if (listp arg) (car arg) arg) parameters)
2333                      (cons arg lambda-list)
2334                      ()
2335                      ())))
2336           (t
2337            (multiple-value-bind (parameters lambda-list specializers required)
2338                (parse-specialized-lambda-list (cdr arglist))
2339              (values (cons (if (listp arg) (car arg) arg) parameters)
2340                      (cons (if (listp arg) (car arg) arg) lambda-list)
2341                      (cons (if (listp arg) (cadr arg) t) specializers)
2342                      (cons (if (listp arg) (car arg) arg) required)))))))
2343 \f
2344 (setq *boot-state* 'early)
2345 \f
2346 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2347 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2348 ;;; walker stuff was only used for implementing stuff like that; maybe
2349 ;;; it's not needed any more? Hunt down what it was used for and see.
2350
2351 (defmacro with-slots (slots instance &body body)
2352   (let ((in (gensym)))
2353     `(let ((,in ,instance))
2354        (declare (ignorable ,in))
2355        ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2356                              (third instance)
2357                              instance)))
2358            (and (symbolp instance)
2359                 `((declare (%variable-rebinding ,in ,instance)))))
2360        ,in
2361        (symbol-macrolet ,(mapcar (lambda (slot-entry)
2362                                    (let ((var-name
2363                                           (if (symbolp slot-entry)
2364                                               slot-entry
2365                                               (car slot-entry)))
2366                                          (slot-name
2367                                           (if (symbolp slot-entry)
2368                                               slot-entry
2369                                               (cadr slot-entry))))
2370                                      `(,var-name
2371                                        (slot-value ,in ',slot-name))))
2372                                  slots)
2373                         ,@body))))
2374
2375 (defmacro with-accessors (slots instance &body body)
2376   (let ((in (gensym)))
2377     `(let ((,in ,instance))
2378        (declare (ignorable ,in))
2379        ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2380                              (third instance)
2381                              instance)))
2382            (and (symbolp instance)
2383                 `((declare (%variable-rebinding ,in ,instance)))))
2384        ,in
2385        (symbol-macrolet ,(mapcar (lambda (slot-entry)
2386                                    (let ((var-name (car slot-entry))
2387                                          (accessor-name (cadr slot-entry)))
2388                                      `(,var-name (,accessor-name ,in))))
2389                                  slots)
2390           ,@body))))