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