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