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