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