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