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