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