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