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