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