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