90da7877c7ad26f995083e11c6bfbf8f1557d3d6
[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                                  `((,key (get-key-arg1 ,keyword ,args-tail))
1020                                    (,variable (if (consp ,key)
1021                                                   (car ,key)
1022                                                   ,(cadr var))))))
1023                               (t
1024                                (multiple-value-bind (keyword variable)
1025                                    (if (consp (car var))
1026                                        (values (caar var)
1027                                                (cadar var))
1028                                        (values (make-keyword (car var))
1029                                                (car var)))
1030                                  `((,key (get-key-arg1 ,keyword ,args-tail))
1031                                    (,(caddr var) ,key)
1032                                    (,variable (if (consp ,key)
1033                                                   (car ,key)
1034                                                   ,(cadr var))))))))
1035                    (aux `(,var))))))
1036       (let ((bindings (mapcan #'process-var lambda-list)))
1037         `(let* ((,args-tail ,args)
1038                 ,@bindings)
1039            (declare (ignorable ,args-tail))
1040            ,@body)))))
1041
1042 (defun get-key-arg (keyword list)
1043   (loop (when (atom list) (return nil))
1044         (when (eq (car list) keyword) (return (cadr list)))
1045         (setq list (cddr list))))
1046
1047 (defun get-key-arg1 (keyword list)
1048   (loop (when (atom list) (return nil))
1049         (when (eq (car list) keyword) (return (cdr list)))
1050         (setq list (cddr list))))
1051
1052 (defun walk-method-lambda (method-lambda required-parameters env slots calls)
1053   (let ((call-next-method-p nil)   ; flag indicating that CALL-NEXT-METHOD
1054                                    ; should be in the method definition
1055         (closurep nil)             ; flag indicating that #'CALL-NEXT-METHOD
1056                                    ; was seen in the body of a method
1057         (next-method-p-p nil))     ; flag indicating that NEXT-METHOD-P
1058                                    ; should be in the method definition
1059     (flet ((walk-function (form context env)
1060              (cond ((not (eq context ':eval)) form)
1061                    ;; FIXME: Jumping to a conclusion from the way it's used
1062                    ;; above, perhaps CONTEXT should be called SITUATION
1063                    ;; (after the term used in the ANSI specification of
1064                    ;; EVAL-WHEN) and given modern ANSI keyword values
1065                    ;; like :LOAD-TOPLEVEL.
1066                    ((not (listp form)) form)
1067                    ((eq (car form) 'call-next-method)
1068                     (setq call-next-method-p 't)
1069                     form)
1070                    ((eq (car form) 'next-method-p)
1071                     (setq next-method-p-p 't)
1072                     form)
1073                    ((and (eq (car form) 'function)
1074                          (cond ((eq (cadr form) 'call-next-method)
1075                                 (setq call-next-method-p 't)
1076                                 (setq closurep t)
1077                                 form)
1078                                ((eq (cadr form) 'next-method-p)
1079                                 (setq next-method-p-p 't)
1080                                 (setq closurep t)
1081                                 form)
1082                                (t nil))))
1083                    (;; FIXME: should be MEMQ or FIND :TEST #'EQ
1084                     (and (or (eq (car form) 'slot-value)
1085                              (eq (car form) 'set-slot-value)
1086                              (eq (car form) 'slot-boundp))
1087                          (constantp (caddr form)))
1088                     (let ((parameter (can-optimize-access form
1089                                                           required-parameters
1090                                                           env)))
1091                       ;; FIXME: could be
1092                       ;;   (LET ((FUN (ECASE (CAR FORM) ..)))
1093                       ;;     (FUNCALL FUN SLOTS PARAMETER FORM))
1094                       (ecase (car form)
1095                         (slot-value
1096                          (optimize-slot-value     slots parameter form))
1097                         (set-slot-value
1098                          (optimize-set-slot-value slots parameter form))
1099                         (slot-boundp
1100                          (optimize-slot-boundp    slots parameter form)))))
1101                    ((and (eq (car form) 'apply)
1102                          (consp (cadr form))
1103                          (eq (car (cadr form)) 'function)
1104                          (generic-function-name-p (cadr (cadr form))))
1105                     (optimize-generic-function-call
1106                      form required-parameters env slots calls))
1107                    ((generic-function-name-p (car form))
1108                     (optimize-generic-function-call
1109                      form required-parameters env slots calls))
1110                    ((and (eq (car form) 'asv-funcall)
1111                          *optimize-asv-funcall-p*)
1112                     (case (fourth form)
1113                       (reader (push (third form) *asv-readers*))
1114                       (writer (push (third form) *asv-writers*))
1115                       (boundp (push (third form) *asv-boundps*)))
1116                     `(,(second form) ,@(cddddr form)))
1117                    (t form))))
1118
1119       (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1120         (values walked-lambda
1121                 call-next-method-p
1122                 closurep
1123                 next-method-p-p)))))
1124
1125 (defun generic-function-name-p (name)
1126   (and (sb-int:legal-function-name-p name)
1127        (gboundp name)
1128        (if (eq *boot-state* 'complete)
1129            (standard-generic-function-p (gdefinition name))
1130            (funcallable-instance-p (gdefinition name)))))
1131
1132 (defun make-parameter-references (specialized-lambda-list
1133                                   required-parameters
1134                                   declarations
1135                                   method-name
1136                                   specializers)
1137   (flet ((ignoredp (symbol)
1138            (dolist (decl (cdar declarations))
1139              (when (and (eq (car decl) 'ignore)
1140                         (memq symbol (cdr decl)))
1141                (return t)))))
1142     (gathering ((references (collecting)))
1143       (iterate ((s (list-elements specialized-lambda-list))
1144                 (p (list-elements required-parameters)))
1145         (progn p)
1146         (cond ((not (listp s)))
1147               ((ignoredp (car s))
1148                (warn "In DEFMETHOD ~S, there is a~%~
1149                       redundant IGNORE declaration for the parameter ~S."
1150                      method-name
1151                      specializers
1152                      (car s)))
1153               (t
1154                (gather (car s) references)))))))
1155 \f
1156 (defvar *method-function-plist* (make-hash-table :test 'eq))
1157 (defvar *mf1* nil)
1158 (defvar *mf1p* nil)
1159 (defvar *mf1cp* nil)
1160 (defvar *mf2* nil)
1161 (defvar *mf2p* nil)
1162 (defvar *mf2cp* nil)
1163
1164 (defun method-function-plist (method-function)
1165   (unless (eq method-function *mf1*)
1166     (rotatef *mf1* *mf2*)
1167     (rotatef *mf1p* *mf2p*)
1168     (rotatef *mf1cp* *mf2cp*))
1169   (unless (or (eq method-function *mf1*) (null *mf1cp*))
1170     (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1171   (unless (eq method-function *mf1*)
1172     (setf *mf1* method-function
1173           *mf1cp* nil
1174           *mf1p* (gethash method-function *method-function-plist*)))
1175   *mf1p*)
1176
1177 (defun #-setf SETF\ SB-PCL\ METHOD-FUNCTION-PLIST
1178        #+setf (setf method-function-plist)
1179     (val method-function)
1180   (unless (eq method-function *mf1*)
1181     (rotatef *mf1* *mf2*)
1182     (rotatef *mf1cp* *mf2cp*)
1183     (rotatef *mf1p* *mf2p*))
1184   (unless (or (eq method-function *mf1*) (null *mf1cp*))
1185     (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1186   (setf *mf1* method-function
1187         *mf1cp* t
1188         *mf1p* val))
1189
1190 (defun method-function-get (method-function key &optional default)
1191   (getf (method-function-plist method-function) key default))
1192
1193 (defun #-setf SETF\ SB-PCL\ METHOD-FUNCTION-GET
1194        #+setf (setf method-function-get)
1195     (val method-function key)
1196   (setf (getf (method-function-plist method-function) key) val))
1197
1198 (defun method-function-pv-table (method-function)
1199   (method-function-get method-function :pv-table))
1200
1201 (defun method-function-method (method-function)
1202   (method-function-get method-function :method))
1203
1204 (defun method-function-needs-next-methods-p (method-function)
1205   (method-function-get method-function :needs-next-methods-p t))
1206 \f
1207 (defmacro method-function-closure-generator (method-function)
1208   `(method-function-get ,method-function 'closure-generator))
1209
1210 (defun load-defmethod
1211     (class name quals specls ll initargs &optional pv-table-symbol)
1212   (when (listp name) (do-standard-defsetf-1 (cadr name)))
1213   (setq initargs (copy-tree initargs))
1214   (let ((method-spec (or (getf initargs ':method-spec)
1215                          (make-method-spec name quals specls))))
1216     (setf (getf initargs ':method-spec) method-spec)
1217     (record-definition 'method method-spec)
1218     (load-defmethod-internal class name quals specls
1219                              ll initargs pv-table-symbol)))
1220
1221 (defun load-defmethod-internal
1222     (method-class gf-spec qualifiers specializers lambda-list
1223                   initargs pv-table-symbol)
1224   (when (listp gf-spec) (do-standard-defsetf-1 (cadr gf-spec)))
1225   (when pv-table-symbol
1226     (setf (getf (getf initargs ':plist) :pv-table-symbol)
1227           pv-table-symbol))
1228   ;; FIXME: It seems as though I should be able to get this to work.
1229   ;; But it keeps on screwing up PCL bootstrapping.
1230   #+nil
1231   (when (and (eq *boot-state* 'complete)
1232              (fboundp gf-spec))
1233     (let* ((gf (symbol-function gf-spec))
1234            (method (and (generic-function-p gf)
1235                         (find-method gf
1236                                      qualifiers
1237                                      (mapcar #'find-class specializers)
1238                                      nil))))
1239       (when method
1240         (sb-kernel::style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1241                                gf-spec qualifiers specializers))))
1242   (let ((method (apply #'add-named-method
1243                        gf-spec qualifiers specializers lambda-list
1244                        :definition-source `((defmethod ,gf-spec
1245                                                 ,@qualifiers
1246                                               ,specializers)
1247                                             ,*load-truename*)
1248                        initargs)))
1249     (unless (or (eq method-class 'standard-method)
1250                 (eq (find-class method-class nil) (class-of method)))
1251       ;; FIXME: should be STYLE-WARNING?
1252       (format *error-output*
1253               "~&At the time the method with qualifiers ~:S and~%~
1254                specializers ~:S on the generic function ~S~%~
1255                was compiled, the method-class for that generic function was~%~
1256                ~S. But, the method class is now ~S, this~%~
1257                may mean that this method was compiled improperly.~%"
1258               qualifiers specializers gf-spec
1259               method-class (class-name (class-of method))))
1260     method))
1261
1262 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1263   `(method ,gf-spec ,@qualifiers ,unparsed-specializers))
1264
1265 (defun initialize-method-function (initargs &optional return-function-p method)
1266   (let* ((mf (getf initargs ':function))
1267          (method-spec (getf initargs ':method-spec))
1268          (plist (getf initargs ':plist))
1269          (pv-table-symbol (getf plist ':pv-table-symbol))
1270          (pv-table nil)
1271          (mff (getf initargs ':fast-function)))
1272     (flet ((set-mf-property (p v)
1273              (when mf
1274                (setf (method-function-get mf p) v))
1275              (when mff
1276                (setf (method-function-get mff p) v))))
1277       (when method-spec
1278         (when mf
1279           (setq mf (set-function-name mf method-spec)))
1280         (when mff
1281           (let ((name `(,(or (get (car method-spec) 'fast-sym)
1282                              (setf (get (car method-spec) 'fast-sym)
1283                                    ;; KLUDGE: If we're going to be
1284                                    ;; interning private symbols in our
1285                                    ;; a this way, it would be cleanest
1286                                    ;; to use a separate package
1287                                    ;; %PCL-PRIVATE or something, and
1288                                    ;; failing that, to use a special
1289                                    ;; symbol prefix denoting privateness.
1290                                    ;; -- WHN 19991201
1291                                    (intern (format nil "FAST-~A"
1292                                                    (car method-spec))
1293                                            *pcl-package*)))
1294                          ,@(cdr method-spec))))
1295             (set-function-name mff name)
1296             (unless mf
1297               (set-mf-property :name name)))))
1298       (when plist
1299         (let ((snl (getf plist :slot-name-lists))
1300               (cl (getf plist :call-list)))
1301           (when (or snl cl)
1302             (setq pv-table (intern-pv-table :slot-name-lists snl
1303                                             :call-list cl))
1304             (when pv-table (set pv-table-symbol pv-table))
1305             (set-mf-property :pv-table pv-table)))
1306         (loop (when (null plist) (return nil))
1307               (set-mf-property (pop plist) (pop plist)))
1308         (when method
1309           (set-mf-property :method method))
1310         (when return-function-p
1311           (or mf (method-function-from-fast-function mff)))))))
1312 \f
1313 (defun analyze-lambda-list (lambda-list)
1314   ;;(declare (values nrequired noptional keysp restp allow-other-keys-p
1315   ;;             keywords keyword-parameters))
1316   (flet ((parse-keyword-argument (arg)
1317            (if (listp arg)
1318                (if (listp (car arg))
1319                    (caar arg)
1320                    (make-keyword (car arg)))
1321                (make-keyword arg))))
1322     (let ((nrequired 0)
1323           (noptional 0)
1324           (keysp nil)
1325           (restp nil)
1326           (allow-other-keys-p nil)
1327           (keywords ())
1328           (keyword-parameters ())
1329           (state 'required))
1330       (dolist (x lambda-list)
1331         (if (memq x lambda-list-keywords)
1332             (case x
1333               (&optional         (setq state 'optional))
1334               (&key           (setq keysp 't
1335                                        state 'key))
1336               (&allow-other-keys (setq allow-other-keys-p 't))
1337               (&rest         (setq restp 't
1338                                        state 'rest))
1339               (&aux           (return t))
1340               (otherwise
1341                 (error "encountered the non-standard lambda list keyword ~S" x)))
1342             (ecase state
1343               (required  (incf nrequired))
1344               (optional  (incf noptional))
1345               (key       (push (parse-keyword-argument x) keywords)
1346                          (push x keyword-parameters))
1347               (rest      ()))))
1348       (values nrequired noptional keysp restp allow-other-keys-p
1349               (reverse keywords)
1350               (reverse keyword-parameters)))))
1351
1352 (defun keyword-spec-name (x)
1353   (let ((key (if (atom x) x (car x))))
1354     (if (atom key)
1355         (intern (symbol-name key) *keyword-package*)
1356         (car key))))
1357
1358 (defun ftype-declaration-from-lambda-list (lambda-list name)
1359   (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1360                                   keywords keyword-parameters)
1361       (analyze-lambda-list lambda-list)
1362     (declare (ignore keyword-parameters))
1363     (let* ((old (sb-c::info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1364            (old-ftype (if (sb-c::function-type-p old) old nil))
1365            (old-restp (and old-ftype (sb-c::function-type-rest old-ftype)))
1366            (old-keys (and old-ftype
1367                           (mapcar #'sb-c::key-info-name
1368                                   (sb-c::function-type-keywords old-ftype))))
1369            (old-keysp (and old-ftype (sb-c::function-type-keyp old-ftype)))
1370            (old-allowp (and old-ftype (sb-c::function-type-allowp old-ftype)))
1371            (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1372       `(function ,(append (make-list nrequired :initial-element 't)
1373                           (when (plusp noptional)
1374                             (append '(&optional)
1375                                     (make-list noptional :initial-element 't)))
1376                           (when (or restp old-restp)
1377                             '(&rest t))
1378                           (when (or keysp old-keysp)
1379                             (append '(&key)
1380                                     (mapcar #'(lambda (key)
1381                                                 `(,key t))
1382                                             keywords)
1383                                     (when (or allow-other-keys-p old-allowp)
1384                                       '(&allow-other-keys)))))
1385                  *))))
1386
1387 (defun defgeneric-declaration (spec lambda-list)
1388   (when (consp spec)
1389     (setq spec (get-setf-function-name (cadr spec))))
1390   `(ftype ,(ftype-declaration-from-lambda-list lambda-list spec) ,spec))
1391 \f
1392 ;;;; early generic function support
1393
1394 (defvar *early-generic-functions* ())
1395
1396 (defun ensure-generic-function (function-name
1397                                 &rest all-keys
1398                                 &key environment
1399                                 &allow-other-keys)
1400   (declare (ignore environment))
1401   (let ((existing (and (gboundp function-name)
1402                        (gdefinition function-name))))
1403     (if (and existing
1404              (eq *boot-state* 'complete)
1405              (null (generic-function-p existing)))
1406         (generic-clobbers-function function-name)
1407         (apply #'ensure-generic-function-using-class
1408                existing function-name all-keys))))
1409
1410 (defun generic-clobbers-function (function-name)
1411   (error 'sb-kernel:simple-program-error
1412          :format-control
1413          "~S already names an ordinary function or a macro."
1414          :format-arguments (list function-name)))
1415
1416 (defvar *sgf-wrapper*
1417   (boot-make-wrapper (early-class-size 'standard-generic-function)
1418                      'standard-generic-function))
1419
1420 (defvar *sgf-slots-init*
1421   (mapcar #'(lambda (canonical-slot)
1422               (if (memq (getf canonical-slot :name) '(arg-info source))
1423                   *slot-unbound*
1424                   (let ((initfunction (getf canonical-slot :initfunction)))
1425                     (if initfunction
1426                         (funcall initfunction)
1427                         *slot-unbound*))))
1428           (early-collect-inheritance 'standard-generic-function)))
1429
1430 (defvar *sgf-method-class-index*
1431   (bootstrap-slot-index 'standard-generic-function 'method-class))
1432
1433 (defun early-gf-p (x)
1434   (and (fsc-instance-p x)
1435        (eq (instance-ref (get-slots x) *sgf-method-class-index*)
1436            *slot-unbound*)))
1437
1438 (defvar *sgf-methods-index*
1439   (bootstrap-slot-index 'standard-generic-function 'methods))
1440
1441 (defmacro early-gf-methods (gf)
1442   `(instance-ref (get-slots ,gf) *sgf-methods-index*))
1443
1444 (defvar *sgf-arg-info-index*
1445   (bootstrap-slot-index 'standard-generic-function 'arg-info))
1446
1447 (defmacro early-gf-arg-info (gf)
1448   `(instance-ref (get-slots ,gf) *sgf-arg-info-index*))
1449
1450 (defvar *sgf-dfun-state-index*
1451   (bootstrap-slot-index 'standard-generic-function 'dfun-state))
1452
1453 (defstruct (arg-info
1454              (:conc-name nil)
1455              (:constructor make-arg-info ()))
1456   (arg-info-lambda-list :no-lambda-list)
1457   arg-info-precedence
1458   arg-info-metatypes
1459   arg-info-number-optional
1460   arg-info-key/rest-p
1461   arg-info-keywords ;nil        no keyword or rest allowed
1462                     ;(k1 k2 ..) each method must accept these keyword arguments
1463                     ;T    must have &key or &rest
1464
1465   gf-info-simple-accessor-type ; nil, reader, writer, boundp
1466   (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1467
1468   gf-info-static-c-a-m-emf
1469   (gf-info-c-a-m-emf-std-p t)
1470   gf-info-fast-mf-p)
1471
1472 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1473
1474 (defun arg-info-valid-p (arg-info)
1475   (not (null (arg-info-number-optional arg-info))))
1476
1477 (defun arg-info-applyp (arg-info)
1478   (or (plusp (arg-info-number-optional arg-info))
1479       (arg-info-key/rest-p arg-info)))
1480
1481 (defun arg-info-number-required (arg-info)
1482   (length (arg-info-metatypes arg-info)))
1483
1484 (defun arg-info-nkeys (arg-info)
1485   (count-if #'(lambda (x) (neq x 't)) (arg-info-metatypes arg-info)))
1486
1487 ;;; Keep pages clean by not setting if the value is already the same.
1488 (defmacro esetf (pos val)
1489   (let ((valsym (gensym "value")))
1490     `(let ((,valsym ,val))
1491        (unless (equal ,pos ,valsym)
1492          (setf ,pos ,valsym)))))
1493
1494 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1495                         argument-precedence-order)
1496   (let* ((arg-info (if (eq *boot-state* 'complete)
1497                        (gf-arg-info gf)
1498                        (early-gf-arg-info gf)))
1499          (methods (if (eq *boot-state* 'complete)
1500                       (generic-function-methods gf)
1501                       (early-gf-methods gf)))
1502          (was-valid-p (integerp (arg-info-number-optional arg-info)))
1503          (first-p (and new-method (null (cdr methods)))))
1504     (when (and (not lambda-list-p) methods)
1505       (setq lambda-list (gf-lambda-list gf)))
1506     (when (or lambda-list-p
1507               (and first-p
1508                    (eq (arg-info-lambda-list arg-info) ':no-lambda-list)))
1509       (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1510           (analyze-lambda-list lambda-list)
1511         (when (and methods (not first-p))
1512           (let ((gf-nreq (arg-info-number-required arg-info))
1513                 (gf-nopt (arg-info-number-optional arg-info))
1514                 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1515             (unless (and (= nreq gf-nreq)
1516                          (= nopt gf-nopt)
1517                          (eq (or keysp restp) gf-key/rest-p))
1518               (error "The lambda-list ~S is incompatible with ~
1519                      existing methods of ~S."
1520                      lambda-list gf))))
1521         (when lambda-list-p
1522           (esetf (arg-info-lambda-list arg-info) lambda-list))
1523         (when (or lambda-list-p argument-precedence-order
1524                   (null (arg-info-precedence arg-info)))
1525           (esetf (arg-info-precedence arg-info)
1526                  (compute-precedence lambda-list nreq
1527                                      argument-precedence-order)))
1528         (esetf (arg-info-metatypes arg-info) (make-list nreq))
1529         (esetf (arg-info-number-optional arg-info) nopt)
1530         (esetf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1531         (esetf (arg-info-keywords arg-info)
1532                (if lambda-list-p
1533                    (if allow-other-keys-p t keywords)
1534                    (arg-info-key/rest-p arg-info)))))
1535     (when new-method
1536       (check-method-arg-info gf arg-info new-method))
1537     (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1538     arg-info))
1539
1540 (defun check-method-arg-info (gf arg-info method)
1541   (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1542       (analyze-lambda-list (if (consp method)
1543                                (early-method-lambda-list method)
1544                                (method-lambda-list method)))
1545     (flet ((lose (string &rest args)
1546              (error
1547               "attempt to add the method ~S to the generic function ~S.~%~
1548                But ~A"
1549               method
1550               gf
1551               (apply #'format nil string args)))
1552            (compare (x y)
1553              (if (> x y) "more" "fewer")))
1554       (let ((gf-nreq (arg-info-number-required arg-info))
1555             (gf-nopt (arg-info-number-optional arg-info))
1556             (gf-key/rest-p (arg-info-key/rest-p arg-info))
1557             (gf-keywords (arg-info-keywords arg-info)))
1558         (unless (= nreq gf-nreq)
1559           (lose
1560            "the method has ~A required arguments than the generic function."
1561            (compare nreq gf-nreq)))
1562         (unless (= nopt gf-nopt)
1563           (lose
1564            "the method has ~S optional arguments than the generic function."
1565            (compare nopt gf-nopt)))
1566         (unless (eq (or keysp restp) gf-key/rest-p)
1567           (error
1568            "The method and generic function differ in whether they accept~%~
1569             &REST or &KEY arguments."))
1570         (when (consp gf-keywords)
1571           (unless (or (and restp (not keysp))
1572                       allow-other-keys-p
1573                       (every #'(lambda (k) (memq k keywords)) gf-keywords))
1574             (lose "the method does not accept each of the keyword arguments~%~
1575                    ~S."
1576                   gf-keywords)))))))
1577
1578 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1579   (let* ((existing-p (and methods (cdr methods) new-method))
1580          (nreq (length (arg-info-metatypes arg-info)))
1581          (metatypes (if existing-p
1582                         (arg-info-metatypes arg-info)
1583                         (make-list nreq)))
1584          (type (if existing-p
1585                    (gf-info-simple-accessor-type arg-info)
1586                    nil)))
1587     (when (arg-info-valid-p arg-info)
1588       (dolist (method (if new-method (list new-method) methods))
1589         (let* ((specializers (if (or (eq *boot-state* 'complete)
1590                                      (not (consp method)))
1591                                  (method-specializers method)
1592                                  (early-method-specializers method t)))
1593                (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1594                           (class-of method)
1595                           (early-method-class method)))
1596                (new-type (when (and class
1597                                     (or (not (eq *boot-state* 'complete))
1598                                         (eq (generic-function-method-combination gf)
1599                                             *standard-method-combination*)))
1600                            (cond ((eq class *the-class-standard-reader-method*)
1601                                   'reader)
1602                                  ((eq class *the-class-standard-writer-method*)
1603                                   'writer)
1604                                  ((eq class *the-class-standard-boundp-method*)
1605                                   'boundp)))))
1606           (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1607           (setq type (cond ((null type) new-type)
1608                            ((eq type new-type) type)
1609                            (t nil)))))
1610       (esetf (arg-info-metatypes arg-info) metatypes)
1611       (esetf (gf-info-simple-accessor-type arg-info) type)))
1612   (when (or (not was-valid-p) first-p)
1613     (multiple-value-bind (c-a-m-emf std-p)
1614         (if (early-gf-p gf)
1615             (values t t)
1616             (compute-applicable-methods-emf gf))
1617       (esetf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1618       (esetf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1619       (unless (gf-info-c-a-m-emf-std-p arg-info)
1620         (esetf (gf-info-simple-accessor-type arg-info) t))))
1621   (unless was-valid-p
1622     (let ((name (if (eq *boot-state* 'complete)
1623                     (generic-function-name gf)
1624                     (early-gf-name gf))))
1625       (esetf (gf-precompute-dfun-and-emf-p arg-info)
1626              (let* ((sym (if (atom name) name (cadr name)))
1627                     (pkg-list (cons *pcl-package*
1628                                     (package-use-list *pcl-package*))))
1629                (and sym (symbolp sym)
1630                     (not (null (memq (symbol-package sym) pkg-list)))
1631                     (not (find #\space (symbol-name sym))))))))
1632   (esetf (gf-info-fast-mf-p arg-info)
1633          (or (not (eq *boot-state* 'complete))
1634              (let* ((method-class (generic-function-method-class gf))
1635                     (methods (compute-applicable-methods
1636                               #'make-method-lambda
1637                               (list gf (class-prototype method-class)
1638                                     '(lambda) nil))))
1639                (and methods (null (cdr methods))
1640                     (let ((specls (method-specializers (car methods))))
1641                       (and (classp (car specls))
1642                            (eq 'standard-generic-function
1643                                (class-name (car specls)))
1644                            (classp (cadr specls))
1645                            (eq 'standard-method
1646                                (class-name (cadr specls)))))))))
1647   arg-info)
1648
1649 ;;; This is the early definition of ensure-generic-function-using-class.
1650 ;;;
1651 ;;; The static-slots field of the funcallable instances used as early generic
1652 ;;; functions is used to store the early methods and early discriminator code
1653 ;;; for the early generic function. The static slots field of the fins
1654 ;;; contains a list whose:
1655 ;;;    CAR    -   a list of the early methods on this early gf
1656 ;;;    CADR   -   the early discriminator code for this method
1657 (defun ensure-generic-function-using-class (existing spec &rest keys
1658                                             &key (lambda-list nil lambda-list-p)
1659                                             &allow-other-keys)
1660   (declare (ignore keys))
1661   (cond ((and existing (early-gf-p existing))
1662          existing)
1663         ((assoc spec *generic-function-fixups* :test #'equal)
1664          (if existing
1665              (make-early-gf spec lambda-list lambda-list-p existing)
1666              (error "The function ~S is not already defined." spec)))
1667         (existing
1668          (error "~S should be on the list ~S."
1669                 spec
1670                 '*generic-function-fixups*))
1671         (t
1672          (pushnew spec *early-generic-functions* :test #'equal)
1673          (make-early-gf spec lambda-list lambda-list-p))))
1674
1675 (defun make-early-gf (spec &optional lambda-list lambda-list-p function)
1676   (let ((fin (allocate-funcallable-instance *sgf-wrapper* *sgf-slots-init*)))
1677     (set-funcallable-instance-function
1678      fin
1679      (or function
1680          (if (eq spec 'print-object)
1681              #'(sb-kernel:instance-lambda (instance stream)
1682                  (print-unreadable-object (instance stream :identity t)
1683                    (format stream "std-instance")))
1684              #'(sb-kernel:instance-lambda (&rest args)
1685                  (declare (ignore args))
1686                  (error "The function of the funcallable-instance ~S~
1687                          has not been set." fin)))))
1688     (setf (gdefinition spec) fin)
1689     (bootstrap-set-slot 'standard-generic-function fin 'name spec)
1690     (bootstrap-set-slot 'standard-generic-function fin 'source *load-truename*)
1691     (set-function-name fin spec)
1692     (let ((arg-info (make-arg-info)))
1693       (setf (early-gf-arg-info fin) arg-info)
1694       (when lambda-list-p
1695         (proclaim (defgeneric-declaration spec lambda-list))
1696         (set-arg-info fin :lambda-list lambda-list)))
1697     fin))
1698
1699 (defun set-dfun (gf &optional dfun cache info)
1700   (when cache
1701     (setf (cache-owner cache) gf))
1702   (let ((new-state (if (and dfun (or cache info))
1703                        (list* dfun cache info)
1704                        dfun)))
1705     (if (eq *boot-state* 'complete)
1706         (setf (gf-dfun-state gf) new-state)
1707         (setf (instance-ref (get-slots gf) *sgf-dfun-state-index*) new-state)))
1708   dfun)
1709
1710 (defun gf-dfun-cache (gf)
1711   (let ((state (if (eq *boot-state* 'complete)
1712                    (gf-dfun-state gf)
1713                    (instance-ref (get-slots gf) *sgf-dfun-state-index*))))
1714     (typecase state
1715       (function nil)
1716       (cons (cadr state)))))
1717
1718 (defun gf-dfun-info (gf)
1719   (let ((state (if (eq *boot-state* 'complete)
1720                    (gf-dfun-state gf)
1721                    (instance-ref (get-slots gf) *sgf-dfun-state-index*))))
1722     (typecase state
1723       (function nil)
1724       (cons (cddr state)))))
1725
1726 (defvar *sgf-name-index*
1727   (bootstrap-slot-index 'standard-generic-function 'name))
1728
1729 (defun early-gf-name (gf)
1730   (instance-ref (get-slots gf) *sgf-name-index*))
1731
1732 (defun gf-lambda-list (gf)
1733   (let ((arg-info (if (eq *boot-state* 'complete)
1734                       (gf-arg-info gf)
1735                       (early-gf-arg-info gf))))
1736     (if (eq ':no-lambda-list (arg-info-lambda-list arg-info))
1737         (let ((methods (if (eq *boot-state* 'complete)
1738                            (generic-function-methods gf)
1739                            (early-gf-methods gf))))
1740           (if (null methods)
1741               (progn
1742                 (warn "no way to determine the lambda list for ~S" gf)
1743                 nil)
1744               (let* ((method (car (last methods)))
1745                      (ll (if (consp method)
1746                              (early-method-lambda-list method)
1747                              (method-lambda-list method)))
1748                      (k (member '&key ll)))
1749                 (if k
1750                     (append (ldiff ll (cdr k)) '(&allow-other-keys))
1751                     ll))))
1752         (arg-info-lambda-list arg-info))))
1753
1754 (defmacro real-ensure-gf-internal (gf-class all-keys env)
1755   `(progn
1756      (cond ((symbolp ,gf-class)
1757             (setq ,gf-class (find-class ,gf-class t ,env)))
1758            ((classp ,gf-class))
1759            (t
1760             (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
1761                     class nor a symbol that names a class."
1762                    ,gf-class)))
1763      (remf ,all-keys :generic-function-class)
1764      (remf ,all-keys :environment)
1765      (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
1766        (unless (eq combin '.shes-not-there.)
1767          (setf (getf ,all-keys :method-combination)
1768                (find-method-combination (class-prototype ,gf-class)
1769                                         (car combin)
1770                                         (cdr combin)))))))
1771
1772 (defun real-ensure-gf-using-class--generic-function
1773        (existing
1774         function-name
1775         &rest all-keys
1776         &key environment (lambda-list nil lambda-list-p)
1777              (generic-function-class 'standard-generic-function gf-class-p)
1778         &allow-other-keys)
1779   (real-ensure-gf-internal generic-function-class all-keys environment)
1780   (unless (or (null gf-class-p)
1781               (eq (class-of existing) generic-function-class))
1782     (change-class existing generic-function-class))
1783   (prog1
1784       (apply #'reinitialize-instance existing all-keys)
1785     (when lambda-list-p
1786       (proclaim (defgeneric-declaration function-name lambda-list)))))
1787
1788 (defun real-ensure-gf-using-class--null
1789        (existing
1790         function-name
1791         &rest all-keys
1792         &key environment (lambda-list nil lambda-list-p)
1793              (generic-function-class 'standard-generic-function)
1794         &allow-other-keys)
1795   (declare (ignore existing))
1796   (real-ensure-gf-internal generic-function-class all-keys environment)
1797   (prog1
1798       (setf (gdefinition function-name)
1799             (apply #'make-instance generic-function-class
1800                    :name function-name all-keys))
1801     (when lambda-list-p
1802       (proclaim (defgeneric-declaration function-name lambda-list)))))
1803 \f
1804 (defun get-generic-function-info (gf)
1805   ;; values   nreq applyp metatypes nkeys arg-info
1806   (multiple-value-bind (applyp metatypes arg-info)
1807       (let* ((arg-info (if (early-gf-p gf)
1808                            (early-gf-arg-info gf)
1809                            (gf-arg-info gf)))
1810              (metatypes (arg-info-metatypes arg-info)))
1811         (values (arg-info-applyp arg-info)
1812                 metatypes
1813                 arg-info))
1814     (values (length metatypes) applyp metatypes
1815             (count-if #'(lambda (x) (neq x 't)) metatypes)
1816             arg-info)))
1817
1818 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
1819                             &optional slot-name)
1820   (initialize-method-function initargs)
1821   (let ((parsed ())
1822         (unparsed ()))
1823     ;; Figure out whether we got class objects or class names as the
1824     ;; specializers and set parsed and unparsed appropriately. If we
1825     ;; got class objects, then we can compute unparsed, but if we got
1826     ;; class names we don't try to compute parsed.
1827     ;;
1828     ;; Note that the use of not symbolp in this call to every should be
1829     ;; read as 'classp' we can't use classp itself because it doesn't
1830     ;; exist yet.
1831     (if (every #'(lambda (s) (not (symbolp s))) specializers)
1832         (setq parsed specializers
1833               unparsed (mapcar #'(lambda (s)
1834                                    (if (eq s 't) 't (class-name s)))
1835                                specializers))
1836         (setq unparsed specializers
1837               parsed ()))
1838     (list :early-method           ;This is an early method dammit!
1839
1840           (getf initargs ':function)
1841           (getf initargs ':fast-function)
1842
1843           parsed                  ;The parsed specializers. This is used
1844                                   ;by early-method-specializers to cache
1845                                   ;the parse. Note that this only comes
1846                                   ;into play when there is more than one
1847                                   ;early method on an early gf.
1848
1849           (list class        ;A list to which real-make-a-method
1850                 qualifiers      ;can be applied to make a real method
1851                 arglist    ;corresponding to this early one.
1852                 unparsed
1853                 initargs
1854                 doc
1855                 slot-name))))
1856
1857 (defun real-make-a-method
1858        (class qualifiers lambda-list specializers initargs doc
1859         &optional slot-name)
1860   (setq specializers (parse-specializers specializers))
1861   (apply #'make-instance class
1862          :qualifiers qualifiers
1863          :lambda-list lambda-list
1864          :specializers specializers
1865          :documentation doc
1866          :slot-name slot-name
1867          :allow-other-keys t
1868          initargs))
1869
1870 (defun early-method-function (early-method)
1871   (values (cadr early-method) (caddr early-method)))
1872
1873 (defun early-method-class (early-method)
1874   (find-class (car (fifth early-method))))
1875
1876 (defun early-method-standard-accessor-p (early-method)
1877   (let ((class (first (fifth early-method))))
1878     (or (eq class 'standard-reader-method)
1879         (eq class 'standard-writer-method)
1880         (eq class 'standard-boundp-method))))
1881
1882 (defun early-method-standard-accessor-slot-name (early-method)
1883   (seventh (fifth early-method)))
1884
1885 ;;; Fetch the specializers of an early method. This is basically just a
1886 ;;; simple accessor except that when the second argument is t, this converts
1887 ;;; the specializers from symbols into class objects. The class objects
1888 ;;; are cached in the early method, this makes bootstrapping faster because
1889 ;;; the class objects only have to be computed once.
1890 ;;; NOTE:
1891 ;;;  the second argument should only be passed as T by early-lookup-method.
1892 ;;;  this is to implement the rule that only when there is more than one
1893 ;;;  early method on a generic function is the conversion from class names
1894 ;;;  to class objects done.
1895 ;;;  the corresponds to the fact that we are only allowed to have one method
1896 ;;;  on any generic function up until the time classes exist.
1897 (defun early-method-specializers (early-method &optional objectsp)
1898   (if (and (listp early-method)
1899            (eq (car early-method) :early-method))
1900       (cond ((eq objectsp 't)
1901              (or (fourth early-method)
1902                  (setf (fourth early-method)
1903                        (mapcar #'find-class (cadddr (fifth early-method))))))
1904             (t
1905              (cadddr (fifth early-method))))
1906       (error "~S is not an early-method." early-method)))
1907
1908 (defun early-method-qualifiers (early-method)
1909   (cadr (fifth early-method)))
1910
1911 (defun early-method-lambda-list (early-method)
1912   (caddr (fifth early-method)))
1913
1914 (defun early-add-named-method (generic-function-name
1915                                qualifiers
1916                                specializers
1917                                arglist
1918                                &rest initargs)
1919   (let* ((gf (ensure-generic-function generic-function-name))
1920          (existing
1921            (dolist (m (early-gf-methods gf))
1922              (when (and (equal (early-method-specializers m) specializers)
1923                         (equal (early-method-qualifiers m) qualifiers))
1924                (return m))))
1925          (new (make-a-method 'standard-method
1926                              qualifiers
1927                              arglist
1928                              specializers
1929                              initargs
1930                              ())))
1931     (when existing (remove-method gf existing))
1932     (add-method gf new)))
1933
1934 ;;; This is the early version of add-method. Later this will become a
1935 ;;; generic function. See fix-early-generic-functions which has special
1936 ;;; knowledge about add-method.
1937 (defun add-method (generic-function method)
1938   (when (not (fsc-instance-p generic-function))
1939     (error "Early add-method didn't get a funcallable instance."))
1940   (when (not (and (listp method) (eq (car method) :early-method)))
1941     (error "Early add-method didn't get an early method."))
1942   (push method (early-gf-methods generic-function))
1943   (set-arg-info generic-function :new-method method)
1944   (unless (assoc (early-gf-name generic-function) *generic-function-fixups*
1945                  :test #'equal)
1946     (update-dfun generic-function)))
1947
1948 ;;; This is the early version of REMOVE-METHOD..
1949 (defun remove-method (generic-function method)
1950   (when (not (fsc-instance-p generic-function))
1951     (error "An early remove-method didn't get a funcallable instance."))
1952   (when (not (and (listp method) (eq (car method) :early-method)))
1953     (error "An early remove-method didn't get an early method."))
1954   (setf (early-gf-methods generic-function)
1955         (remove method (early-gf-methods generic-function)))
1956   (set-arg-info generic-function)
1957   (unless (assoc (early-gf-name generic-function) *generic-function-fixups*
1958                  :test #'equal)
1959     (update-dfun generic-function)))
1960
1961 ;;; ..and the early version of GET-METHOD.
1962 (defun get-method (generic-function qualifiers specializers
1963                                     &optional (errorp t))
1964   (if (early-gf-p generic-function)
1965       (or (dolist (m (early-gf-methods generic-function))
1966             (when (and (or (equal (early-method-specializers m nil)
1967                                   specializers)
1968                            (equal (early-method-specializers m 't)
1969                                   specializers))
1970                        (equal (early-method-qualifiers m) qualifiers))
1971               (return m)))
1972           (if errorp
1973               (error "can't get early method")
1974               nil))
1975       (real-get-method generic-function qualifiers specializers errorp)))
1976
1977 (defvar *fegf-debug-p* nil)
1978
1979 (defun fix-early-generic-functions (&optional (noisyp *fegf-debug-p*))
1980   (let ((accessors nil))
1981     ;; Rearrange *EARLY-GENERIC-FUNCTIONS* to speed up
1982     ;; FIX-EARLY-GENERIC-FUNCTIONS.
1983     (dolist (early-gf-spec *early-generic-functions*)
1984       (when (every #'early-method-standard-accessor-p
1985                    (early-gf-methods (gdefinition early-gf-spec)))
1986         (push early-gf-spec accessors)))
1987     (dolist (spec (nconc accessors
1988                          '(accessor-method-slot-name
1989                            generic-function-methods
1990                            method-specializers
1991                            specializerp
1992                            specializer-type
1993                            specializer-class
1994                            slot-definition-location
1995                            slot-definition-name
1996                            class-slots
1997                            gf-arg-info
1998                            class-precedence-list
1999                            slot-boundp-using-class
2000                            (setf slot-value-using-class)
2001                            slot-value-using-class
2002                            structure-class-p
2003                            standard-class-p
2004                            funcallable-standard-class-p
2005                            specializerp)))
2006       (setq *early-generic-functions*
2007             (cons spec (delete spec *early-generic-functions* :test #'equal))))
2008
2009     (dolist (early-gf-spec *early-generic-functions*)
2010       (when noisyp (format t "~&~S..." early-gf-spec))
2011       (let* ((gf (gdefinition early-gf-spec))
2012              (methods (mapcar #'(lambda (early-method)
2013                                   (let ((args (copy-list (fifth
2014                                                           early-method))))
2015                                     (setf (fourth args)
2016                                           (early-method-specializers
2017                                            early-method t))
2018                                     (apply #'real-make-a-method args)))
2019                               (early-gf-methods gf))))
2020         (setf (generic-function-method-class gf) *the-class-standard-method*)
2021         (setf (generic-function-method-combination gf)
2022               *standard-method-combination*)
2023         (set-methods gf methods)))
2024
2025     (dolist (fns *early-functions*)
2026       (setf (gdefinition (car fns)) (symbol-function (caddr fns))))
2027
2028     (dolist (fixup *generic-function-fixups*)
2029       (let* ((fspec (car fixup))
2030              (gf (gdefinition fspec))
2031              (methods (mapcar #'(lambda (method)
2032                                   (let* ((lambda-list (first method))
2033                                          (specializers (second method))
2034                                          (method-fn-name (third method))
2035                                          (fn-name (or method-fn-name fspec))
2036                                          (fn (symbol-function fn-name))
2037                                          (initargs
2038                                           (list :function
2039                                                 (set-function-name
2040                                                  #'(lambda (args next-methods)
2041                                                      (declare (ignore
2042                                                                next-methods))
2043                                                      (apply fn args))
2044                                                  `(call ,fn-name)))))
2045                                     (declare (type function fn))
2046                                     (make-a-method 'standard-method
2047                                                    ()
2048                                                    lambda-list
2049                                                    specializers
2050                                                    initargs
2051                                                    nil)))
2052                               (cdr fixup))))
2053         (setf (generic-function-method-class gf) *the-class-standard-method*)
2054         (setf (generic-function-method-combination gf)
2055               *standard-method-combination*)
2056         (set-methods gf methods)))))
2057 \f
2058 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument into
2059 ;;; the 'real' arguments. This is where the syntax of DEFMETHOD is really
2060 ;;; implemented.
2061 (defun parse-defmethod (cdr-of-form)
2062   ;;(declare (values name qualifiers specialized-lambda-list body))
2063   (let ((name (pop cdr-of-form))
2064         (qualifiers ())
2065         (spec-ll ()))
2066     (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2067               (push (pop cdr-of-form) qualifiers)
2068               (return (setq qualifiers (nreverse qualifiers)))))
2069     (setq spec-ll (pop cdr-of-form))
2070     (values name qualifiers spec-ll cdr-of-form)))
2071
2072 (defun parse-specializers (specializers)
2073   (flet ((parse (spec)
2074            (let ((result (specializer-from-type spec)))
2075              (if (specializerp result)
2076                  result
2077                  (if (symbolp spec)
2078                      (error "~S was used as a specializer,~%~
2079                              but is not the name of a class."
2080                             spec)
2081                      (error "~S is not a legal specializer." spec))))))
2082     (mapcar #'parse specializers)))
2083
2084 (defun unparse-specializers (specializers-or-method)
2085   (if (listp specializers-or-method)
2086       (flet ((unparse (spec)
2087                (if (specializerp spec)
2088                    (let ((type (specializer-type spec)))
2089                      (if (and (consp type)
2090                               (eq (car type) 'class))
2091                          (let* ((class (cadr type))
2092                                 (class-name (class-name class)))
2093                            (if (eq class (find-class class-name nil))
2094                                class-name
2095                                type))
2096                          type))
2097                    (error "~S is not a legal specializer." spec))))
2098         (mapcar #'unparse specializers-or-method))
2099       (unparse-specializers (method-specializers specializers-or-method))))
2100
2101 (defun parse-method-or-spec (spec &optional (errorp t))
2102   ;;(declare (values generic-function method method-name))
2103   (let (gf method name temp)
2104     (if (method-p spec) 
2105         (setq method spec
2106               gf (method-generic-function method)
2107               temp (and gf (generic-function-name gf))
2108               name (if temp
2109                        (intern-function-name
2110                          (make-method-spec temp
2111                                            (method-qualifiers method)
2112                                            (unparse-specializers
2113                                              (method-specializers method))))
2114                        (make-symbol (format nil "~S" method))))
2115         (multiple-value-bind (gf-spec quals specls)
2116             (parse-defmethod spec)
2117           (and (setq gf (and (or errorp (gboundp gf-spec))
2118                              (gdefinition gf-spec)))
2119                (let ((nreq (compute-discriminating-function-arglist-info gf)))
2120                  (setq specls (append (parse-specializers specls)
2121                                       (make-list (- nreq (length specls))
2122                                                  :initial-element
2123                                                  *the-class-t*)))
2124                  (and
2125                    (setq method (get-method gf quals specls errorp))
2126                    (setq name
2127                          (intern-function-name (make-method-spec gf-spec
2128                                                                  quals
2129                                                                  specls))))))))
2130     (values gf method name)))
2131 \f
2132 (defun extract-parameters (specialized-lambda-list)
2133   (multiple-value-bind (parameters ignore1 ignore2)
2134       (parse-specialized-lambda-list specialized-lambda-list)
2135     (declare (ignore ignore1 ignore2))
2136     parameters))
2137
2138 (defun extract-lambda-list (specialized-lambda-list)
2139   (multiple-value-bind (ignore1 lambda-list ignore2)
2140       (parse-specialized-lambda-list specialized-lambda-list)
2141     (declare (ignore ignore1 ignore2))
2142     lambda-list))
2143
2144 (defun extract-specializer-names (specialized-lambda-list)
2145   (multiple-value-bind (ignore1 ignore2 specializers)
2146       (parse-specialized-lambda-list specialized-lambda-list)
2147     (declare (ignore ignore1 ignore2))
2148     specializers))
2149
2150 (defun extract-required-parameters (specialized-lambda-list)
2151   (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2152       (parse-specialized-lambda-list specialized-lambda-list)
2153     (declare (ignore ignore1 ignore2 ignore3))
2154     required-parameters))
2155
2156 (defun parse-specialized-lambda-list (arglist &optional post-keyword)
2157   ;;(declare (values parameters lambda-list specializers required-parameters))
2158   (let ((arg (car arglist)))
2159     (cond ((null arglist) (values nil nil nil nil))
2160           ((eq arg '&aux)
2161            (values nil arglist nil))
2162           ((memq arg lambda-list-keywords)
2163            (unless (memq arg '(&optional &rest &key &allow-other-keys &aux))
2164              ;; Warn about non-standard lambda-list-keywords, but then
2165              ;; go on to treat them like a standard lambda-list-keyword
2166              ;; what with the warning its probably ok.
2167              ;;
2168              ;; FIXME: This shouldn't happen now that this is maintained
2169              ;; as part of SBCL, should it? Perhaps this is now
2170              ;; "internal error: unrecognized lambda-list keyword ~S"?
2171              (warn "Unrecognized lambda-list keyword ~S in arglist.~%~
2172                     Assuming that the symbols following it are parameters,~%~
2173                     and not allowing any parameter specializers to follow~%~
2174                     to follow it."
2175                    arg))
2176            ;; When we are at a lambda-list keyword, the parameters don't
2177            ;; include the lambda-list keyword; the lambda-list does include
2178            ;; the lambda-list keyword; and no specializers are allowed to
2179            ;; follow the lambda-list keywords (at least for now).
2180            (multiple-value-bind (parameters lambda-list)
2181                (parse-specialized-lambda-list (cdr arglist) t)
2182              (values parameters
2183                      (cons arg lambda-list)
2184                      ()
2185                      ())))
2186           (post-keyword
2187            ;; After a lambda-list keyword there can be no specializers.
2188            (multiple-value-bind (parameters lambda-list)
2189                (parse-specialized-lambda-list (cdr arglist) t)
2190              (values (cons (if (listp arg) (car arg) arg) parameters)
2191                      (cons arg lambda-list)
2192                      ()
2193                      ())))
2194           (t
2195            (multiple-value-bind (parameters lambda-list specializers required)
2196                (parse-specialized-lambda-list (cdr arglist))
2197              (values (cons (if (listp arg) (car arg) arg) parameters)
2198                      (cons (if (listp arg) (car arg) arg) lambda-list)
2199                      (cons (if (listp arg) (cadr arg) 't) specializers)
2200                      (cons (if (listp arg) (car arg) arg) required)))))))
2201 \f
2202 (eval-when (:load-toplevel :execute)
2203   (setq *boot-state* 'early))
2204 \f
2205 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET which used
2206 ;;; %WALKER stuff. That suggests to me that maybe the code walker stuff was
2207 ;;; only used for implementing stuff like that; maybe it's not needed any more?
2208 ;;; Hunt down what it was used for and see.
2209
2210 (defmacro with-slots (slots instance &body body)
2211   (let ((in (gensym)))
2212     `(let ((,in ,instance))
2213        (declare (ignorable ,in))
2214        ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2215                              (third instance)
2216                              instance)))
2217            (and (symbolp instance)
2218                 `((declare (variable-rebinding ,in ,instance)))))
2219        ,in
2220        (symbol-macrolet ,(mapcar #'(lambda (slot-entry)
2221                                      (let ((variable-name
2222                                             (if (symbolp slot-entry)
2223                                                 slot-entry
2224                                                 (car slot-entry)))
2225                                            (slot-name
2226                                             (if (symbolp slot-entry)
2227                                                 slot-entry
2228                                                 (cadr slot-entry))))
2229                                        `(,variable-name
2230                                           (slot-value ,in ',slot-name))))
2231                                  slots)
2232                         ,@body))))
2233
2234 (defmacro with-accessors (slots instance &body body)
2235   (let ((in (gensym)))
2236     `(let ((,in ,instance))
2237        (declare (ignorable ,in))
2238        ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2239                              (third instance)
2240                              instance)))
2241            (and (symbolp instance)
2242                 `((declare (variable-rebinding ,in ,instance)))))
2243        ,in
2244        (symbol-macrolet ,(mapcar #'(lambda (slot-entry)
2245                                    (let ((variable-name (car slot-entry))
2246                                          (accessor-name (cadr slot-entry)))
2247                                      `(,variable-name
2248                                         (,accessor-name ,in))))
2249                                slots)
2250           ,@body))))