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