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