0.7.8.31:
[sbcl.git] / src / pcl / boot.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3
4 ;;;; This software is derived from software originally released by Xerox
5 ;;;; Corporation. Copyright and release statements follow. Later modifications
6 ;;;; to the software are in the public domain and are provided with
7 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
8 ;;;; information.
9
10 ;;;; copyright information from original PCL sources:
11 ;;;;
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
14 ;;;;
15 ;;;; Use and copying of this software and preparation of derivative works based
16 ;;;; upon this software are permitted. Any distribution of this software or
17 ;;;; derivative works must comply with all applicable United States export
18 ;;;; control laws.
19 ;;;;
20 ;;;; This software is made available AS IS, and Xerox Corporation makes no
21 ;;;; warranty about the software, its performance or its conformity to any
22 ;;;; specification.
23
24 (in-package "SB-PCL")
25 \f
26 #|
27
28 The CommonLoops evaluator is meta-circular.
29
30 Most of the code in PCL is methods on generic functions, including
31 most of the code that actually implements generic functions and method
32 lookup.
33
34 So, we have a classic bootstrapping problem. The solution to this is
35 to first get a cheap implementation of generic functions running,
36 these are called early generic functions. These early generic
37 functions and the corresponding early methods and early method lookup
38 are used to get enough of the system running that it is possible to
39 create real generic functions and methods and implement real method
40 lookup. At that point (done in the file FIXUP) the function
41 !FIX-EARLY-GENERIC-FUNCTIONS is called to convert all the early generic
42 functions to real generic functions.
43
44 The cheap generic functions are built using the same
45 FUNCALLABLE-INSTANCE objects that real generic functions are made out of.
46 This means that as PCL is being bootstrapped, the cheap generic
47 function objects which are being created are the same objects which
48 will later be real generic functions. This is good because:
49   - we don't cons garbage structure, and
50   - we can keep pointers to the cheap generic function objects
51     during booting because those pointers will still point to
52     the right object after the generic functions are all fixed up.
53
54 This file defines the DEFMETHOD macro and the mechanism used to expand
55 it. This includes the mechanism for processing the body of a method.
56 DEFMETHOD basically expands into a call to LOAD-DEFMETHOD, which
57 basically calls ADD-METHOD to add the method to the generic function.
58 These expansions can be loaded either during bootstrapping or when PCL
59 is fully up and running.
60
61 An important effect of this arrangement is it means we can compile
62 files with DEFMETHOD forms in them in a completely running PCL, but
63 then load those files back in during bootstrapping. This makes
64 development easier. It also means there is only one set of code for
65 processing DEFMETHOD. Bootstrapping works by being sure to have
66 LOAD-METHOD be careful to call only primitives which work during
67 bootstrapping.
68
69 |#
70
71 ;;; FIXME: As of sbcl-0.6.9.10, PCL still uses this nonstandard type
72 ;;; of declaration internally. It would be good to figure out how to
73 ;;; get rid of it, or failing that, (1) document why it's needed and
74 ;;; (2) use a private symbol with a forbidding name which suggests
75 ;;; it's not to be messed with by the user (e.g. SB-PCL:%CLASS)
76 ;;; instead of the too-inviting CLASS. (I tried just deleting the
77 ;;; declarations in MAKE-METHOD-LAMBDA-INTERNAL ca. sbcl-0.6.9.10, but
78 ;;; then things break.)
79 (declaim (declaration class))
80
81 (declaim (notinline make-a-method
82                     add-named-method
83                     ensure-generic-function-using-class
84                     add-method
85                     remove-method))
86
87 (defvar *!early-functions*
88         '((make-a-method early-make-a-method
89                          real-make-a-method)
90           (add-named-method early-add-named-method
91                             real-add-named-method)
92           ))
93
94 ;;; For each of the early functions, arrange to have it point to its
95 ;;; early definition. Do this in a way that makes sure that if we
96 ;;; redefine one of the early definitions the redefinition will take
97 ;;; effect. This makes development easier.
98 (dolist (fns *!early-functions*)
99   (let ((name (car fns))
100         (early-name (cadr fns)))
101     (setf (gdefinition name)
102             (set-fun-name
103              (lambda (&rest args)
104                (apply (fdefinition early-name) args))
105              name))))
106
107 ;;; *!GENERIC-FUNCTION-FIXUPS* is used by !FIX-EARLY-GENERIC-FUNCTIONS
108 ;;; to convert the few functions in the bootstrap which are supposed
109 ;;; to be generic functions but can't be early on.
110 (defvar *!generic-function-fixups*
111   '((add-method
112      ((generic-function method)  ;lambda-list
113       (standard-generic-function method) ;specializers
114       real-add-method))          ;method-function
115     (remove-method
116      ((generic-function method)
117       (standard-generic-function method)
118       real-remove-method))
119     (get-method
120      ((generic-function qualifiers specializers &optional (errorp t))
121       (standard-generic-function t t)
122       real-get-method))
123     (ensure-generic-function-using-class
124      ((generic-function fun-name
125                         &key generic-function-class environment
126                         &allow-other-keys)
127       (generic-function t)
128       real-ensure-gf-using-class--generic-function)
129      ((generic-function fun-name
130                         &key generic-function-class environment
131                         &allow-other-keys)
132       (null t)
133       real-ensure-gf-using-class--null))
134     (make-method-lambda
135      ((proto-generic-function proto-method lambda-expression environment)
136       (standard-generic-function standard-method t t)
137       real-make-method-lambda))
138     (make-method-initargs-form
139      ((proto-generic-function proto-method
140                               lambda-expression
141                               lambda-list environment)
142       (standard-generic-function standard-method t t t)
143       real-make-method-initargs-form))
144     (compute-effective-method
145      ((generic-function combin applicable-methods)
146       (generic-function standard-method-combination t)
147       standard-compute-effective-method))))
148 \f
149 (defmacro defgeneric (fun-name lambda-list &body options)
150   (declare (type list lambda-list))
151   (unless (legal-fun-name-p fun-name)
152     (error 'simple-program-error
153            :format-control "illegal generic function name ~S"
154            :format-arguments (list fun-name)))
155   (check-gf-lambda-list lambda-list)
156   (let ((initargs ())
157         (methods ()))
158     (flet ((duplicate-option (name)
159              (error 'simple-program-error
160                     :format-control "The option ~S appears more than once."
161                     :format-arguments (list name)))
162            (expand-method-definition (qab) ; QAB = qualifiers, arglist, body
163              (let* ((arglist-pos (position-if #'listp qab))
164                     (arglist (elt qab arglist-pos))
165                     (qualifiers (subseq qab 0 arglist-pos))
166                     (body (nthcdr (1+ arglist-pos) qab)))
167                `(push (defmethod ,fun-name ,@qualifiers ,arglist ,@body)
168                       (generic-function-initial-methods #',fun-name)))))
169       (macrolet ((initarg (key) `(getf initargs ,key)))
170         (dolist (option options)
171           (let ((car-option (car option)))
172             (case car-option
173               (declare
174                (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 (car
1092                                         (get-key-arg-tail ,(keywordicate var)
1093                                                           ,args-tail)))))
1094                               ((null (cddr var))
1095                                (multiple-value-bind (keyword variable)
1096                                    (if (consp (car var))
1097                                        (values (caar var)
1098                                                (cadar var))
1099                                        (values (keywordicate (car var))
1100                                                (car var)))
1101                                  `((,key (get-key-arg-tail ',keyword
1102                                                            ,args-tail))
1103                                    (,variable (if ,key
1104                                                   (car ,key)
1105                                                   ,(cadr var))))))
1106                               (t
1107                                (multiple-value-bind (keyword variable)
1108                                    (if (consp (car var))
1109                                        (values (caar var)
1110                                                (cadar var))
1111                                        (values (keywordicate (car var))
1112                                                (car var)))
1113                                  `((,key (get-key-arg-tail ',keyword
1114                                                            ,args-tail))
1115                                    (,(caddr var) ,key)
1116                                    (,variable (if ,key
1117                                                   (car ,key)
1118                                                   ,(cadr var))))))))
1119                    (aux `(,var))))))
1120       (let ((bindings (mapcan #'process-var lambda-list)))
1121         `(let* ((,args-tail ,args)
1122                 ,@bindings)
1123            (declare (ignorable ,args-tail))
1124            ,@body)))))
1125
1126 (defun get-key-arg-tail (keyword list)
1127   (loop for (key . tail) on list by #'cddr
1128         when (null tail) do
1129           ;; FIXME: Do we want to export this symbol? Or maybe use an
1130           ;; (ERROR 'SIMPLE-PROGRAM-ERROR) form?
1131           (sb-c::%odd-key-args-error)
1132         when (eq key keyword)
1133           return tail))
1134
1135 (defun walk-method-lambda (method-lambda required-parameters env slots calls)
1136   (let ((call-next-method-p nil)   ; flag indicating that CALL-NEXT-METHOD
1137                                    ; should be in the method definition
1138         (closurep nil)             ; flag indicating that #'CALL-NEXT-METHOD
1139                                    ; was seen in the body of a method
1140         (next-method-p-p nil))     ; flag indicating that NEXT-METHOD-P
1141                                    ; should be in the method definition
1142     (flet ((walk-function (form context env)
1143              (cond ((not (eq context :eval)) form)
1144                    ;; FIXME: Jumping to a conclusion from the way it's used
1145                    ;; above, perhaps CONTEXT should be called SITUATION
1146                    ;; (after the term used in the ANSI specification of
1147                    ;; EVAL-WHEN) and given modern ANSI keyword values
1148                    ;; like :LOAD-TOPLEVEL.
1149                    ((not (listp form)) form)
1150                    ((eq (car form) 'call-next-method)
1151                     (setq call-next-method-p t)
1152                     form)
1153                    ((eq (car form) 'next-method-p)
1154                     (setq next-method-p-p t)
1155                     form)
1156                    ((and (eq (car form) 'function)
1157                          (cond ((eq (cadr form) 'call-next-method)
1158                                 (setq call-next-method-p t)
1159                                 (setq closurep t)
1160                                 form)
1161                                ((eq (cadr form) 'next-method-p)
1162                                 (setq next-method-p-p t)
1163                                 (setq closurep t)
1164                                 form)
1165                                (t nil))))
1166                    ((and (memq (car form)
1167                                '(slot-value set-slot-value slot-boundp))
1168                          (constantp (caddr form)))
1169                      (let ((parameter (can-optimize-access form
1170                                                            required-parameters
1171                                                            env)))
1172                       (let ((fun (ecase (car form)
1173                                    (slot-value #'optimize-slot-value)
1174                                    (set-slot-value #'optimize-set-slot-value)
1175                                    (slot-boundp #'optimize-slot-boundp))))
1176                         (funcall fun slots parameter form))))
1177                    ((and (eq (car form) 'apply)
1178                          (consp (cadr form))
1179                          (eq (car (cadr form)) 'function)
1180                          (generic-function-name-p (cadr (cadr form))))
1181                     (optimize-generic-function-call
1182                      form required-parameters env slots calls))
1183                    ((generic-function-name-p (car form))
1184                     (optimize-generic-function-call
1185                      form required-parameters env slots calls))
1186                    ((and (eq (car form) 'asv-funcall)
1187                          *optimize-asv-funcall-p*)
1188                     (case (fourth form)
1189                       (reader (push (third form) *asv-readers*))
1190                       (writer (push (third form) *asv-writers*))
1191                       (boundp (push (third form) *asv-boundps*)))
1192                     `(,(second form) ,@(cddddr form)))
1193                    (t form))))
1194
1195       (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1196         (values walked-lambda
1197                 call-next-method-p
1198                 closurep
1199                 next-method-p-p)))))
1200
1201 (defun generic-function-name-p (name)
1202   (and (legal-fun-name-p name)
1203        (gboundp name)
1204        (if (eq *boot-state* 'complete)
1205            (standard-generic-function-p (gdefinition name))
1206            (funcallable-instance-p (gdefinition name)))))
1207 \f
1208 (defvar *method-function-plist* (make-hash-table :test 'eq))
1209 (defvar *mf1* nil)
1210 (defvar *mf1p* nil)
1211 (defvar *mf1cp* nil)
1212 (defvar *mf2* nil)
1213 (defvar *mf2p* nil)
1214 (defvar *mf2cp* nil)
1215
1216 (defun method-function-plist (method-function)
1217   (unless (eq method-function *mf1*)
1218     (rotatef *mf1* *mf2*)
1219     (rotatef *mf1p* *mf2p*)
1220     (rotatef *mf1cp* *mf2cp*))
1221   (unless (or (eq method-function *mf1*) (null *mf1cp*))
1222     (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1223   (unless (eq method-function *mf1*)
1224     (setf *mf1* method-function
1225           *mf1cp* nil
1226           *mf1p* (gethash method-function *method-function-plist*)))
1227   *mf1p*)
1228
1229 (defun (setf method-function-plist)
1230     (val method-function)
1231   (unless (eq method-function *mf1*)
1232     (rotatef *mf1* *mf2*)
1233     (rotatef *mf1cp* *mf2cp*)
1234     (rotatef *mf1p* *mf2p*))
1235   (unless (or (eq method-function *mf1*) (null *mf1cp*))
1236     (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1237   (setf *mf1* method-function
1238         *mf1cp* t
1239         *mf1p* val))
1240
1241 (defun method-function-get (method-function key &optional default)
1242   (getf (method-function-plist method-function) key default))
1243
1244 (defun (setf method-function-get)
1245     (val method-function key)
1246   (setf (getf (method-function-plist method-function) key) val))
1247
1248 (defun method-function-pv-table (method-function)
1249   (method-function-get method-function :pv-table))
1250
1251 (defun method-function-method (method-function)
1252   (method-function-get method-function :method))
1253
1254 (defun method-function-needs-next-methods-p (method-function)
1255   (method-function-get method-function :needs-next-methods-p t))
1256 \f
1257 (defmacro method-function-closure-generator (method-function)
1258   `(method-function-get ,method-function 'closure-generator))
1259
1260 (defun load-defmethod
1261     (class name quals specls ll initargs &optional pv-table-symbol)
1262   (setq initargs (copy-tree initargs))
1263   (let ((method-spec (or (getf initargs :method-spec)
1264                          (make-method-spec name quals specls))))
1265     (setf (getf initargs :method-spec) method-spec)
1266     (load-defmethod-internal class name quals specls
1267                              ll initargs pv-table-symbol)))
1268
1269 (defun load-defmethod-internal
1270     (method-class gf-spec qualifiers specializers lambda-list
1271                   initargs pv-table-symbol)
1272   (when pv-table-symbol
1273     (setf (getf (getf initargs :plist) :pv-table-symbol)
1274           pv-table-symbol))
1275   (when (and (eq *boot-state* 'complete)
1276              (fboundp gf-spec))
1277     (let* ((gf (fdefinition gf-spec))
1278            (method (and (generic-function-p gf)
1279                         (find-method gf
1280                                      qualifiers
1281                                      (parse-specializers specializers)
1282                                      nil))))
1283       (when method
1284         (sb-kernel::style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1285                                gf-spec qualifiers specializers))))
1286   (let ((method (apply #'add-named-method
1287                        gf-spec qualifiers specializers lambda-list
1288                        :definition-source `((defmethod ,gf-spec
1289                                                 ,@qualifiers
1290                                               ,specializers)
1291                                             ,*load-truename*)
1292                        initargs)))
1293     (unless (or (eq method-class 'standard-method)
1294                 (eq (find-class method-class nil) (class-of method)))
1295       ;; FIXME: should be STYLE-WARNING?
1296       (format *error-output*
1297               "~&At the time the method with qualifiers ~:S and~%~
1298                specializers ~:S on the generic function ~S~%~
1299                was compiled, the method-class for that generic function was~%~
1300                ~S. But, the method class is now ~S, this~%~
1301                may mean that this method was compiled improperly.~%"
1302               qualifiers specializers gf-spec
1303               method-class (class-name (class-of method))))
1304     method))
1305
1306 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1307   `(method ,gf-spec ,@qualifiers ,unparsed-specializers))
1308
1309 (defun initialize-method-function (initargs &optional return-function-p method)
1310   (let* ((mf (getf initargs :function))
1311          (method-spec (getf initargs :method-spec))
1312          (plist (getf initargs :plist))
1313          (pv-table-symbol (getf plist :pv-table-symbol))
1314          (pv-table nil)
1315          (mff (getf initargs :fast-function)))
1316     (flet ((set-mf-property (p v)
1317              (when mf
1318                (setf (method-function-get mf p) v))
1319              (when mff
1320                (setf (method-function-get mff p) v))))
1321       (when method-spec
1322         (when mf
1323           (setq mf (set-fun-name mf method-spec)))
1324         (when mff
1325           (let ((name `(,(or (get (car method-spec) 'fast-sym)
1326                              (setf (get (car method-spec) 'fast-sym)
1327                                    ;; KLUDGE: If we're going to be
1328                                    ;; interning private symbols in our
1329                                    ;; a this way, it would be cleanest
1330                                    ;; to use a separate package
1331                                    ;; %PCL-PRIVATE or something, and
1332                                    ;; failing that, to use a special
1333                                    ;; symbol prefix denoting privateness.
1334                                    ;; -- WHN 19991201
1335                                    (intern (format nil "FAST-~A"
1336                                                    (car method-spec))
1337                                            *pcl-package*)))
1338                          ,@(cdr method-spec))))
1339             (set-fun-name mff name)
1340             (unless mf
1341               (set-mf-property :name name)))))
1342       (when plist
1343         (let ((snl (getf plist :slot-name-lists))
1344               (cl (getf plist :call-list)))
1345           (when (or snl cl)
1346             (setq pv-table (intern-pv-table :slot-name-lists snl
1347                                             :call-list cl))
1348             (when pv-table (set pv-table-symbol pv-table))
1349             (set-mf-property :pv-table pv-table)))
1350         (loop (when (null plist) (return nil))
1351               (set-mf-property (pop plist) (pop plist)))
1352         (when method
1353           (set-mf-property :method method))
1354         (when return-function-p
1355           (or mf (method-function-from-fast-function mff)))))))
1356 \f
1357 (defun analyze-lambda-list (lambda-list)
1358   (flet (;; FIXME: Is this redundant with SB-C::MAKE-KEYWORD-FOR-ARG?
1359          (parse-key-arg (arg)
1360            (if (listp arg)
1361                (if (listp (car arg))
1362                    (caar arg)
1363                    (keywordicate (car arg)))
1364                (keywordicate arg))))
1365     (let ((nrequired 0)
1366           (noptional 0)
1367           (keysp nil)
1368           (restp nil)
1369           (nrest 0)
1370           (allow-other-keys-p nil)
1371           (keywords ())
1372           (keyword-parameters ())
1373           (state 'required))
1374       (dolist (x lambda-list)
1375         (if (memq x lambda-list-keywords)
1376             (case x
1377               (&optional         (setq state 'optional))
1378               (&key              (setq keysp t
1379                                        state 'key))
1380               (&allow-other-keys (setq allow-other-keys-p t))
1381               (&rest             (setq restp t
1382                                        state 'rest))
1383               (&aux           (return t))
1384               (otherwise
1385                 (error "encountered the non-standard lambda list keyword ~S"
1386                        x)))
1387             (ecase state
1388               (required  (incf nrequired))
1389               (optional  (incf noptional))
1390               (key       (push (parse-key-arg x) keywords)
1391                          (push x keyword-parameters))
1392               (rest      (incf nrest)))))
1393       (when (and restp (zerop nrest))
1394         (error "Error in lambda-list:~%~
1395                 After &REST, a DEFGENERIC lambda-list ~
1396                 must be followed by at least one variable."))
1397       (values nrequired noptional keysp restp allow-other-keys-p
1398               (reverse keywords)
1399               (reverse keyword-parameters)))))
1400
1401 (defun keyword-spec-name (x)
1402   (let ((key (if (atom x) x (car x))))
1403     (if (atom key)
1404         (keywordicate key)
1405         (car key))))
1406
1407 (defun ftype-declaration-from-lambda-list (lambda-list name)
1408   (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1409                                   keywords keyword-parameters)
1410       (analyze-lambda-list lambda-list)
1411     (declare (ignore keyword-parameters))
1412     (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1413            (old-ftype (if (sb-kernel:fun-type-p old) old nil))
1414            (old-restp (and old-ftype (sb-kernel:fun-type-rest old-ftype)))
1415            (old-keys (and old-ftype
1416                           (mapcar #'sb-kernel:key-info-name
1417                                   (sb-kernel:fun-type-keywords
1418                                    old-ftype))))
1419            (old-keysp (and old-ftype (sb-kernel:fun-type-keyp old-ftype)))
1420            (old-allowp (and old-ftype
1421                             (sb-kernel:fun-type-allowp old-ftype)))
1422            (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1423       `(function ,(append (make-list nrequired :initial-element t)
1424                           (when (plusp noptional)
1425                             (append '(&optional)
1426                                     (make-list noptional :initial-element t)))
1427                           (when (or restp old-restp)
1428                             '(&rest t))
1429                           (when (or keysp old-keysp)
1430                             (append '(&key)
1431                                     (mapcar (lambda (key)
1432                                               `(,key t))
1433                                             keywords)
1434                                     (when (or allow-other-keys-p old-allowp)
1435                                       '(&allow-other-keys)))))
1436                  *))))
1437
1438 (defun defgeneric-declaration (spec lambda-list)
1439   (when (consp spec)
1440     (setq spec (get-setf-fun-name (cadr spec))))
1441   `(ftype ,(ftype-declaration-from-lambda-list lambda-list spec) ,spec))
1442 \f
1443 ;;;; early generic function support
1444
1445 (defvar *!early-generic-functions* ())
1446
1447 (defun ensure-generic-function (fun-name
1448                                 &rest all-keys
1449                                 &key environment
1450                                 &allow-other-keys)
1451   (declare (ignore environment))
1452   (let ((existing (and (gboundp fun-name)
1453                        (gdefinition fun-name))))
1454     (if (and existing
1455              (eq *boot-state* 'complete)
1456              (null (generic-function-p existing)))
1457         (generic-clobbers-function fun-name)
1458         (apply #'ensure-generic-function-using-class
1459                existing fun-name all-keys))))
1460
1461 (defun generic-clobbers-function (fun-name)
1462   (error 'simple-program-error
1463          :format-control "~S already names an ordinary function or a macro."
1464          :format-arguments (list fun-name)))
1465
1466 (defvar *sgf-wrapper*
1467   (boot-make-wrapper (early-class-size 'standard-generic-function)
1468                      'standard-generic-function))
1469
1470 (defvar *sgf-slots-init*
1471   (mapcar (lambda (canonical-slot)
1472             (if (memq (getf canonical-slot :name) '(arg-info source))
1473                 +slot-unbound+
1474                 (let ((initfunction (getf canonical-slot :initfunction)))
1475                   (if initfunction
1476                       (funcall initfunction)
1477                       +slot-unbound+))))
1478           (early-collect-inheritance 'standard-generic-function)))
1479
1480 (defvar *sgf-method-class-index*
1481   (!bootstrap-slot-index 'standard-generic-function 'method-class))
1482
1483 (defun early-gf-p (x)
1484   (and (fsc-instance-p x)
1485        (eq (clos-slots-ref (get-slots x) *sgf-method-class-index*)
1486            +slot-unbound+)))
1487
1488 (defvar *sgf-methods-index*
1489   (!bootstrap-slot-index 'standard-generic-function 'methods))
1490
1491 (defmacro early-gf-methods (gf)
1492   `(clos-slots-ref (get-slots ,gf) *sgf-methods-index*))
1493
1494 (defvar *sgf-arg-info-index*
1495   (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1496
1497 (defmacro early-gf-arg-info (gf)
1498   `(clos-slots-ref (get-slots ,gf) *sgf-arg-info-index*))
1499
1500 (defvar *sgf-dfun-state-index*
1501   (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1502
1503 (defstruct (arg-info
1504             (:conc-name nil)
1505             (:constructor make-arg-info ())
1506             (:copier nil))
1507   (arg-info-lambda-list :no-lambda-list)
1508   arg-info-precedence
1509   arg-info-metatypes
1510   arg-info-number-optional
1511   arg-info-key/rest-p
1512   arg-info-keys   ;nil        no &KEY or &REST allowed
1513                   ;(k1 k2 ..) Each method must accept these &KEY arguments.
1514                   ;T          must have &KEY or &REST
1515
1516   gf-info-simple-accessor-type ; nil, reader, writer, boundp
1517   (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1518
1519   gf-info-static-c-a-m-emf
1520   (gf-info-c-a-m-emf-std-p t)
1521   gf-info-fast-mf-p)
1522
1523 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1524
1525 (defun arg-info-valid-p (arg-info)
1526   (not (null (arg-info-number-optional arg-info))))
1527
1528 (defun arg-info-applyp (arg-info)
1529   (or (plusp (arg-info-number-optional arg-info))
1530       (arg-info-key/rest-p arg-info)))
1531
1532 (defun arg-info-number-required (arg-info)
1533   (length (arg-info-metatypes arg-info)))
1534
1535 (defun arg-info-nkeys (arg-info)
1536   (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1537
1538 ;;; Keep pages clean by not setting if the value is already the same.
1539 (defmacro esetf (pos val)
1540   (let ((valsym (gensym "value")))
1541     `(let ((,valsym ,val))
1542        (unless (equal ,pos ,valsym)
1543          (setf ,pos ,valsym)))))
1544
1545 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1546                         argument-precedence-order)
1547   (let* ((arg-info (if (eq *boot-state* 'complete)
1548                        (gf-arg-info gf)
1549                        (early-gf-arg-info gf)))
1550          (methods (if (eq *boot-state* 'complete)
1551                       (generic-function-methods gf)
1552                       (early-gf-methods gf)))
1553          (was-valid-p (integerp (arg-info-number-optional arg-info)))
1554          (first-p (and new-method (null (cdr methods)))))
1555     (when (and (not lambda-list-p) methods)
1556       (setq lambda-list (gf-lambda-list gf)))
1557     (when (or lambda-list-p
1558               (and first-p
1559                    (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1560       (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1561           (analyze-lambda-list lambda-list)
1562         (when (and methods (not first-p))
1563           (let ((gf-nreq (arg-info-number-required arg-info))
1564                 (gf-nopt (arg-info-number-optional arg-info))
1565                 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1566             (unless (and (= nreq gf-nreq)
1567                          (= nopt gf-nopt)
1568                          (eq (or keysp restp) gf-key/rest-p))
1569               (error "The lambda-list ~S is incompatible with ~
1570                      existing methods of ~S."
1571                      lambda-list gf))))
1572         (when lambda-list-p
1573           (esetf (arg-info-lambda-list arg-info) lambda-list))
1574         (when (or lambda-list-p argument-precedence-order
1575                   (null (arg-info-precedence arg-info)))
1576           (esetf (arg-info-precedence arg-info)
1577                  (compute-precedence lambda-list nreq
1578                                      argument-precedence-order)))
1579         (esetf (arg-info-metatypes arg-info) (make-list nreq))
1580         (esetf (arg-info-number-optional arg-info) nopt)
1581         (esetf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1582         (esetf (arg-info-keys arg-info)
1583                (if lambda-list-p
1584                    (if allow-other-keys-p t keywords)
1585                    (arg-info-key/rest-p arg-info)))))
1586     (when new-method
1587       (check-method-arg-info gf arg-info new-method))
1588     (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1589     arg-info))
1590
1591 (defun check-method-arg-info (gf arg-info method)
1592   (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1593       (analyze-lambda-list (if (consp method)
1594                                (early-method-lambda-list method)
1595                                (method-lambda-list method)))
1596     (flet ((lose (string &rest args)
1597              (error
1598               "attempt to add the method ~S to the generic function ~S.~%~
1599                But ~A"
1600               method
1601               gf
1602               (apply #'format nil string args)))
1603            (comparison-description (x y)
1604              (if (> x y) "more" "fewer")))
1605       (let ((gf-nreq (arg-info-number-required arg-info))
1606             (gf-nopt (arg-info-number-optional arg-info))
1607             (gf-key/rest-p (arg-info-key/rest-p arg-info))
1608             (gf-keywords (arg-info-keys arg-info)))
1609         (unless (= nreq gf-nreq)
1610           (lose
1611            "the method has ~A required arguments than the generic function."
1612            (comparison-description nreq gf-nreq)))
1613         (unless (= nopt gf-nopt)
1614           (lose
1615            "the method has ~A optional arguments than the generic function."
1616            (comparison-description nopt gf-nopt)))
1617         (unless (eq (or keysp restp) gf-key/rest-p)
1618           (error
1619            "The method and generic function differ in whether they accept~%~
1620             &REST or &KEY arguments."))
1621         (when (consp gf-keywords)
1622           (unless (or (and restp (not keysp))
1623                       allow-other-keys-p
1624                       (every (lambda (k) (memq k keywords)) gf-keywords))
1625             (lose "the method does not accept each of the &KEY arguments~%~
1626                    ~S."
1627                   gf-keywords)))))))
1628
1629 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1630   (let* ((existing-p (and methods (cdr methods) new-method))
1631          (nreq (length (arg-info-metatypes arg-info)))
1632          (metatypes (if existing-p
1633                         (arg-info-metatypes arg-info)
1634                         (make-list nreq)))
1635          (type (if existing-p
1636                    (gf-info-simple-accessor-type arg-info)
1637                    nil)))
1638     (when (arg-info-valid-p arg-info)
1639       (dolist (method (if new-method (list new-method) methods))
1640         (let* ((specializers (if (or (eq *boot-state* 'complete)
1641                                      (not (consp method)))
1642                                  (method-specializers method)
1643                                  (early-method-specializers method t)))
1644                (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1645                           (class-of method)
1646                           (early-method-class method)))
1647                (new-type (when (and class
1648                                     (or (not (eq *boot-state* 'complete))
1649                                         (eq (generic-function-method-combination gf)
1650                                             *standard-method-combination*)))
1651                            (cond ((eq class *the-class-standard-reader-method*)
1652                                   'reader)
1653                                  ((eq class *the-class-standard-writer-method*)
1654                                   'writer)
1655                                  ((eq class *the-class-standard-boundp-method*)
1656                                   'boundp)))))
1657           (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1658           (setq type (cond ((null type) new-type)
1659                            ((eq type new-type) type)
1660                            (t nil)))))
1661       (esetf (arg-info-metatypes arg-info) metatypes)
1662       (esetf (gf-info-simple-accessor-type arg-info) type)))
1663   (when (or (not was-valid-p) first-p)
1664     (multiple-value-bind (c-a-m-emf std-p)
1665         (if (early-gf-p gf)
1666             (values t t)
1667             (compute-applicable-methods-emf gf))
1668       (esetf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1669       (esetf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1670       (unless (gf-info-c-a-m-emf-std-p arg-info)
1671         (esetf (gf-info-simple-accessor-type arg-info) t))))
1672   (unless was-valid-p
1673     (let ((name (if (eq *boot-state* 'complete)
1674                     (generic-function-name gf)
1675                     (!early-gf-name gf))))
1676       (esetf (gf-precompute-dfun-and-emf-p arg-info)
1677              (let* ((sym (if (atom name) name (cadr name)))
1678                     (pkg-list (cons *pcl-package*
1679                                     (package-use-list *pcl-package*))))
1680                (and sym (symbolp sym)
1681                     (not (null (memq (symbol-package sym) pkg-list)))
1682                     (not (find #\space (symbol-name sym))))))))
1683   (esetf (gf-info-fast-mf-p arg-info)
1684          (or (not (eq *boot-state* 'complete))
1685              (let* ((method-class (generic-function-method-class gf))
1686                     (methods (compute-applicable-methods
1687                               #'make-method-lambda
1688                               (list gf (class-prototype method-class)
1689                                     '(lambda) nil))))
1690                (and methods (null (cdr methods))
1691                     (let ((specls (method-specializers (car methods))))
1692                       (and (classp (car specls))
1693                            (eq 'standard-generic-function
1694                                (class-name (car specls)))
1695                            (classp (cadr specls))
1696                            (eq 'standard-method
1697                                (class-name (cadr specls)))))))))
1698   arg-info)
1699
1700 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
1701 ;;;
1702 ;;; The STATIC-SLOTS field of the funcallable instances used as early
1703 ;;; generic functions is used to store the early methods and early
1704 ;;; discriminator code for the early generic function. The static
1705 ;;; slots field of the fins contains a list whose:
1706 ;;;    CAR    -   a list of the early methods on this early gf
1707 ;;;    CADR   -   the early discriminator code for this method
1708 (defun ensure-generic-function-using-class (existing spec &rest keys
1709                                             &key (lambda-list nil
1710                                                               lambda-list-p)
1711                                             &allow-other-keys)
1712   (declare (ignore keys))
1713   (cond ((and existing (early-gf-p existing))
1714          existing)
1715         ((assoc spec *!generic-function-fixups* :test #'equal)
1716          (if existing
1717              (make-early-gf spec lambda-list lambda-list-p existing)
1718              (error "The function ~S is not already defined." spec)))
1719         (existing
1720          (error "~S should be on the list ~S."
1721                 spec
1722                 '*!generic-function-fixups*))
1723         (t
1724          (pushnew spec *!early-generic-functions* :test #'equal)
1725          (make-early-gf spec lambda-list lambda-list-p))))
1726
1727 (defun make-early-gf (spec &optional lambda-list lambda-list-p function)
1728   (let ((fin (allocate-funcallable-instance *sgf-wrapper* *sgf-slots-init*)))
1729     (set-funcallable-instance-fun
1730      fin
1731      (or function
1732          (if (eq spec 'print-object)
1733              #'(sb-kernel:instance-lambda (instance stream)
1734                  (print-unreadable-object (instance stream :identity t)
1735                    (format stream "std-instance")))
1736              #'(sb-kernel:instance-lambda (&rest args)
1737                  (declare (ignore args))
1738                  (error "The function of the funcallable-instance ~S~
1739                          has not been set." fin)))))
1740     (setf (gdefinition spec) fin)
1741     (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
1742     (!bootstrap-set-slot 'standard-generic-function
1743                          fin
1744                          'source
1745                          *load-truename*)
1746     (set-fun-name fin spec)
1747     (let ((arg-info (make-arg-info)))
1748       (setf (early-gf-arg-info fin) arg-info)
1749       (when lambda-list-p
1750         (proclaim (defgeneric-declaration spec lambda-list))
1751         (set-arg-info fin :lambda-list lambda-list)))
1752     fin))
1753
1754 (defun set-dfun (gf &optional dfun cache info)
1755   (when cache
1756     (setf (cache-owner cache) gf))
1757   (let ((new-state (if (and dfun (or cache info))
1758                        (list* dfun cache info)
1759                        dfun)))
1760     (if (eq *boot-state* 'complete)
1761         (setf (gf-dfun-state gf) new-state)
1762         (setf (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*)
1763               new-state)))
1764   dfun)
1765
1766 (defun gf-dfun-cache (gf)
1767   (let ((state (if (eq *boot-state* 'complete)
1768                    (gf-dfun-state gf)
1769                    (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1770     (typecase state
1771       (function nil)
1772       (cons (cadr state)))))
1773
1774 (defun gf-dfun-info (gf)
1775   (let ((state (if (eq *boot-state* 'complete)
1776                    (gf-dfun-state gf)
1777                    (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1778     (typecase state
1779       (function nil)
1780       (cons (cddr state)))))
1781
1782 (defvar *sgf-name-index*
1783   (!bootstrap-slot-index 'standard-generic-function 'name))
1784
1785 (defun !early-gf-name (gf)
1786   (clos-slots-ref (get-slots gf) *sgf-name-index*))
1787
1788 (defun gf-lambda-list (gf)
1789   (let ((arg-info (if (eq *boot-state* 'complete)
1790                       (gf-arg-info gf)
1791                       (early-gf-arg-info gf))))
1792     (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
1793         (let ((methods (if (eq *boot-state* 'complete)
1794                            (generic-function-methods gf)
1795                            (early-gf-methods gf))))
1796           (if (null methods)
1797               (progn
1798                 (warn "no way to determine the lambda list for ~S" gf)
1799                 nil)
1800               (let* ((method (car (last methods)))
1801                      (ll (if (consp method)
1802                              (early-method-lambda-list method)
1803                              (method-lambda-list method)))
1804                      (k (member '&key ll)))
1805                 (if k
1806                     (append (ldiff ll (cdr k)) '(&allow-other-keys))
1807                     ll))))
1808         (arg-info-lambda-list arg-info))))
1809
1810 (defmacro real-ensure-gf-internal (gf-class all-keys env)
1811   `(progn
1812      (cond ((symbolp ,gf-class)
1813             (setq ,gf-class (find-class ,gf-class t ,env)))
1814            ((classp ,gf-class))
1815            (t
1816             (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
1817                     class nor a symbol that names a class."
1818                    ,gf-class)))
1819      (remf ,all-keys :generic-function-class)
1820      (remf ,all-keys :environment)
1821      (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
1822        (unless (eq combin '.shes-not-there.)
1823          (setf (getf ,all-keys :method-combination)
1824                (find-method-combination (class-prototype ,gf-class)
1825                                         (car combin)
1826                                         (cdr combin)))))
1827     (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
1828       (unless (eq method-class '.shes-not-there.)
1829         (setf (getf ,all-keys :method-class)
1830                 (find-class method-class t ,env))))))
1831
1832 (defun real-ensure-gf-using-class--generic-function
1833        (existing
1834         fun-name
1835         &rest all-keys
1836         &key environment (lambda-list nil lambda-list-p)
1837              (generic-function-class 'standard-generic-function gf-class-p)
1838         &allow-other-keys)
1839   (real-ensure-gf-internal generic-function-class all-keys environment)
1840   (unless (or (null gf-class-p)
1841               (eq (class-of existing) generic-function-class))
1842     (change-class existing generic-function-class))
1843   (prog1
1844       (apply #'reinitialize-instance existing all-keys)
1845     (when lambda-list-p
1846       (proclaim (defgeneric-declaration fun-name lambda-list)))))
1847
1848 (defun real-ensure-gf-using-class--null
1849        (existing
1850         fun-name
1851         &rest all-keys
1852         &key environment (lambda-list nil lambda-list-p)
1853              (generic-function-class 'standard-generic-function)
1854         &allow-other-keys)
1855   (declare (ignore existing))
1856   (real-ensure-gf-internal generic-function-class all-keys environment)
1857   (prog1
1858       (setf (gdefinition fun-name)
1859             (apply #'make-instance generic-function-class
1860                    :name fun-name all-keys))
1861     (when lambda-list-p
1862       (proclaim (defgeneric-declaration fun-name lambda-list)))))
1863 \f
1864 (defun get-generic-fun-info (gf)
1865   ;; values   nreq applyp metatypes nkeys arg-info
1866   (multiple-value-bind (applyp metatypes arg-info)
1867       (let* ((arg-info (if (early-gf-p gf)
1868                            (early-gf-arg-info gf)
1869                            (gf-arg-info gf)))
1870              (metatypes (arg-info-metatypes arg-info)))
1871         (values (arg-info-applyp arg-info)
1872                 metatypes
1873                 arg-info))
1874     (values (length metatypes) applyp metatypes
1875             (count-if (lambda (x) (neq x t)) metatypes)
1876             arg-info)))
1877
1878 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
1879                             &optional slot-name)
1880   (initialize-method-function initargs)
1881   (let ((parsed ())
1882         (unparsed ()))
1883     ;; Figure out whether we got class objects or class names as the
1884     ;; specializers and set parsed and unparsed appropriately. If we
1885     ;; got class objects, then we can compute unparsed, but if we got
1886     ;; class names we don't try to compute parsed.
1887     ;;
1888     ;; Note that the use of not symbolp in this call to every should be
1889     ;; read as 'classp' we can't use classp itself because it doesn't
1890     ;; exist yet.
1891     (if (every (lambda (s) (not (symbolp s))) specializers)
1892         (setq parsed specializers
1893               unparsed (mapcar (lambda (s)
1894                                  (if (eq s t) t (class-name s)))
1895                                specializers))
1896         (setq unparsed specializers
1897               parsed ()))
1898     (list :early-method           ;This is an early method dammit!
1899
1900           (getf initargs :function)
1901           (getf initargs :fast-function)
1902
1903           parsed                  ;The parsed specializers. This is used
1904                                   ;by early-method-specializers to cache
1905                                   ;the parse. Note that this only comes
1906                                   ;into play when there is more than one
1907                                   ;early method on an early gf.
1908
1909           (list class        ;A list to which real-make-a-method
1910                 qualifiers      ;can be applied to make a real method
1911                 arglist    ;corresponding to this early one.
1912                 unparsed
1913                 initargs
1914                 doc
1915                 slot-name))))
1916
1917 (defun real-make-a-method
1918        (class qualifiers lambda-list specializers initargs doc
1919         &optional slot-name)
1920   (setq specializers (parse-specializers specializers))
1921   (apply #'make-instance class
1922          :qualifiers qualifiers
1923          :lambda-list lambda-list
1924          :specializers specializers
1925          :documentation doc
1926          :slot-name slot-name
1927          :allow-other-keys t
1928          initargs))
1929
1930 (defun early-method-function (early-method)
1931   (values (cadr early-method) (caddr early-method)))
1932
1933 (defun early-method-class (early-method)
1934   (find-class (car (fifth early-method))))
1935
1936 (defun early-method-standard-accessor-p (early-method)
1937   (let ((class (first (fifth early-method))))
1938     (or (eq class 'standard-reader-method)
1939         (eq class 'standard-writer-method)
1940         (eq class 'standard-boundp-method))))
1941
1942 (defun early-method-standard-accessor-slot-name (early-method)
1943   (seventh (fifth early-method)))
1944
1945 ;;; Fetch the specializers of an early method. This is basically just
1946 ;;; a simple accessor except that when the second argument is t, this
1947 ;;; converts the specializers from symbols into class objects. The
1948 ;;; class objects are cached in the early method, this makes
1949 ;;; bootstrapping faster because the class objects only have to be
1950 ;;; computed once.
1951 ;;;
1952 ;;; NOTE:
1953 ;;;  The second argument should only be passed as T by
1954 ;;;  early-lookup-method. This is to implement the rule that only when
1955 ;;;  there is more than one early method on a generic function is the
1956 ;;;  conversion from class names to class objects done. This
1957 ;;;  corresponds to the fact that we are only allowed to have one
1958 ;;;  method on any generic function up until the time classes exist.
1959 (defun early-method-specializers (early-method &optional objectsp)
1960   (if (and (listp early-method)
1961            (eq (car early-method) :early-method))
1962       (cond ((eq objectsp t)
1963              (or (fourth early-method)
1964                  (setf (fourth early-method)
1965                        (mapcar #'find-class (cadddr (fifth early-method))))))
1966             (t
1967              (cadddr (fifth early-method))))
1968       (error "~S is not an early-method." early-method)))
1969
1970 (defun early-method-qualifiers (early-method)
1971   (cadr (fifth early-method)))
1972
1973 (defun early-method-lambda-list (early-method)
1974   (caddr (fifth early-method)))
1975
1976 (defun early-add-named-method (generic-function-name
1977                                qualifiers
1978                                specializers
1979                                arglist
1980                                &rest initargs)
1981   (let* ((gf (ensure-generic-function generic-function-name))
1982          (existing
1983            (dolist (m (early-gf-methods gf))
1984              (when (and (equal (early-method-specializers m) specializers)
1985                         (equal (early-method-qualifiers m) qualifiers))
1986                (return m))))
1987          (new (make-a-method 'standard-method
1988                              qualifiers
1989                              arglist
1990                              specializers
1991                              initargs
1992                              ())))
1993     (when existing (remove-method gf existing))
1994     (add-method gf new)))
1995
1996 ;;; This is the early version of ADD-METHOD. Later this will become a
1997 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
1998 ;;; special knowledge about ADD-METHOD.
1999 (defun add-method (generic-function method)
2000   (when (not (fsc-instance-p generic-function))
2001     (error "Early ADD-METHOD didn't get a funcallable instance."))
2002   (when (not (and (listp method) (eq (car method) :early-method)))
2003     (error "Early ADD-METHOD didn't get an early method."))
2004   (push method (early-gf-methods generic-function))
2005   (set-arg-info generic-function :new-method method)
2006   (unless (assoc (!early-gf-name generic-function)
2007                  *!generic-function-fixups*
2008                  :test #'equal)
2009     (update-dfun generic-function)))
2010
2011 ;;; This is the early version of REMOVE-METHOD. See comments on
2012 ;;; the early version of ADD-METHOD.
2013 (defun remove-method (generic-function method)
2014   (when (not (fsc-instance-p generic-function))
2015     (error "An early remove-method didn't get a funcallable instance."))
2016   (when (not (and (listp method) (eq (car method) :early-method)))
2017     (error "An early remove-method didn't get an early method."))
2018   (setf (early-gf-methods generic-function)
2019         (remove method (early-gf-methods generic-function)))
2020   (set-arg-info generic-function)
2021   (unless (assoc (!early-gf-name generic-function)
2022                  *!generic-function-fixups*
2023                  :test #'equal)
2024     (update-dfun generic-function)))
2025
2026 ;;; This is the early version of GET-METHOD. See comments on the early
2027 ;;; version of ADD-METHOD.
2028 (defun get-method (generic-function qualifiers specializers
2029                                     &optional (errorp t))
2030   (if (early-gf-p generic-function)
2031       (or (dolist (m (early-gf-methods generic-function))
2032             (when (and (or (equal (early-method-specializers m nil)
2033                                   specializers)
2034                            (equal (early-method-specializers m t)
2035                                   specializers))
2036                        (equal (early-method-qualifiers m) qualifiers))
2037               (return m)))
2038           (if errorp
2039               (error "can't get early method")
2040               nil))
2041       (real-get-method generic-function qualifiers specializers errorp)))
2042
2043 (defun !fix-early-generic-functions ()
2044   (let ((accessors nil))
2045     ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2046     ;; FIX-EARLY-GENERIC-FUNCTIONS.
2047     (dolist (early-gf-spec *!early-generic-functions*)
2048       (when (every #'early-method-standard-accessor-p
2049                    (early-gf-methods (gdefinition early-gf-spec)))
2050         (push early-gf-spec accessors)))
2051     (dolist (spec (nconc accessors
2052                          '(accessor-method-slot-name
2053                            generic-function-methods
2054                            method-specializers
2055                            specializerp
2056                            specializer-type
2057                            specializer-class
2058                            slot-definition-location
2059                            slot-definition-name
2060                            class-slots
2061                            gf-arg-info
2062                            class-precedence-list
2063                            slot-boundp-using-class
2064                            (setf slot-value-using-class)
2065                            slot-value-using-class
2066                            structure-class-p
2067                            standard-class-p
2068                            funcallable-standard-class-p
2069                            specializerp)))
2070       (/show spec)
2071       (setq *!early-generic-functions*
2072             (cons spec
2073                   (delete spec *!early-generic-functions* :test #'equal))))
2074
2075     (dolist (early-gf-spec *!early-generic-functions*)
2076       (/show early-gf-spec)
2077       (let* ((gf (gdefinition early-gf-spec))
2078              (methods (mapcar (lambda (early-method)
2079                                 (let ((args (copy-list (fifth
2080                                                         early-method))))
2081                                   (setf (fourth args)
2082                                         (early-method-specializers
2083                                          early-method t))
2084                                   (apply #'real-make-a-method args)))
2085                               (early-gf-methods gf))))
2086         (setf (generic-function-method-class gf) *the-class-standard-method*)
2087         (setf (generic-function-method-combination gf)
2088               *standard-method-combination*)
2089         (set-methods gf methods)))
2090
2091     (dolist (fn *!early-functions*)
2092       (/show fn)
2093       (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2094
2095     (dolist (fixup *!generic-function-fixups*)
2096       (/show fixup)
2097       (let* ((fspec (car fixup))
2098              (gf (gdefinition fspec))
2099              (methods (mapcar (lambda (method)
2100                                 (let* ((lambda-list (first method))
2101                                        (specializers (second method))
2102                                        (method-fn-name (third method))
2103                                        (fn-name (or method-fn-name fspec))
2104                                        (fn (fdefinition fn-name))
2105                                        (initargs
2106                                         (list :function
2107                                               (set-fun-name
2108                                                (lambda (args next-methods)
2109                                                  (declare (ignore
2110                                                            next-methods))
2111                                                  (apply fn args))
2112                                                `(call ,fn-name)))))
2113                                   (declare (type function fn))
2114                                   (make-a-method 'standard-method
2115                                                  ()
2116                                                  lambda-list
2117                                                  specializers
2118                                                  initargs
2119                                                  nil)))
2120                               (cdr fixup))))
2121         (setf (generic-function-method-class gf) *the-class-standard-method*)
2122         (setf (generic-function-method-combination gf)
2123               *standard-method-combination*)
2124         (set-methods gf methods))))
2125   (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2126 \f
2127 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2128 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2129 ;;; is really implemented.
2130 (defun parse-defmethod (cdr-of-form)
2131   (declare (list cdr-of-form))
2132   (let ((name (pop cdr-of-form))
2133         (qualifiers ())
2134         (spec-ll ()))
2135     (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2136               (push (pop cdr-of-form) qualifiers)
2137               (return (setq qualifiers (nreverse qualifiers)))))
2138     (setq spec-ll (pop cdr-of-form))
2139     (values name qualifiers spec-ll cdr-of-form)))
2140
2141 (defun parse-specializers (specializers)
2142   (declare (list specializers))
2143   (flet ((parse (spec)
2144            (let ((result (specializer-from-type spec)))
2145              (if (specializerp result)
2146                  result
2147                  (if (symbolp spec)
2148                      (error "~S was used as a specializer,~%~
2149                              but is not the name of a class."
2150                             spec)
2151                      (error "~S is not a legal specializer." spec))))))
2152     (mapcar #'parse specializers)))
2153
2154 (defun unparse-specializers (specializers-or-method)
2155   (if (listp specializers-or-method)
2156       (flet ((unparse (spec)
2157                (if (specializerp spec)
2158                    (let ((type (specializer-type spec)))
2159                      (if (and (consp type)
2160                               (eq (car type) 'class))
2161                          (let* ((class (cadr type))
2162                                 (class-name (class-name class)))
2163                            (if (eq class (find-class class-name nil))
2164                                class-name
2165                                type))
2166                          type))
2167                    (error "~S is not a legal specializer." spec))))
2168         (mapcar #'unparse specializers-or-method))
2169       (unparse-specializers (method-specializers specializers-or-method))))
2170
2171 (defun parse-method-or-spec (spec &optional (errorp t))
2172   (let (gf method name temp)
2173     (if (method-p spec) 
2174         (setq method spec
2175               gf (method-generic-function method)
2176               temp (and gf (generic-function-name gf))
2177               name (if temp
2178                        (intern-fun-name
2179                          (make-method-spec temp
2180                                            (method-qualifiers method)
2181                                            (unparse-specializers
2182                                              (method-specializers method))))
2183                        (make-symbol (format nil "~S" method))))
2184         (multiple-value-bind (gf-spec quals specls)
2185             (parse-defmethod spec)
2186           (and (setq gf (and (or errorp (gboundp gf-spec))
2187                              (gdefinition gf-spec)))
2188                (let ((nreq (compute-discriminating-function-arglist-info gf)))
2189                  (setq specls (append (parse-specializers specls)
2190                                       (make-list (- nreq (length specls))
2191                                                  :initial-element
2192                                                  *the-class-t*)))
2193                  (and
2194                    (setq method (get-method gf quals specls errorp))
2195                    (setq name
2196                          (intern-fun-name (make-method-spec gf-spec
2197                                                             quals
2198                                                             specls))))))))
2199     (values gf method name)))
2200 \f
2201 (defun extract-parameters (specialized-lambda-list)
2202   (multiple-value-bind (parameters ignore1 ignore2)
2203       (parse-specialized-lambda-list specialized-lambda-list)
2204     (declare (ignore ignore1 ignore2))
2205     parameters))
2206
2207 (defun extract-lambda-list (specialized-lambda-list)
2208   (multiple-value-bind (ignore1 lambda-list ignore2)
2209       (parse-specialized-lambda-list specialized-lambda-list)
2210     (declare (ignore ignore1 ignore2))
2211     lambda-list))
2212
2213 (defun extract-specializer-names (specialized-lambda-list)
2214   (multiple-value-bind (ignore1 ignore2 specializers)
2215       (parse-specialized-lambda-list specialized-lambda-list)
2216     (declare (ignore ignore1 ignore2))
2217     specializers))
2218
2219 (defun extract-required-parameters (specialized-lambda-list)
2220   (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2221       (parse-specialized-lambda-list specialized-lambda-list)
2222     (declare (ignore ignore1 ignore2 ignore3))
2223     required-parameters))
2224
2225 (defun parse-specialized-lambda-list (arglist &optional post-keyword)
2226   ;;(declare (values parameters lambda-list specializers required-parameters))
2227   (let ((arg (car arglist)))
2228     (cond ((null arglist) (values nil nil nil nil))
2229           ((eq arg '&aux)
2230            (values nil arglist nil))
2231           ((memq arg lambda-list-keywords)
2232            (unless (memq arg '(&optional &rest &key &allow-other-keys &aux))
2233              ;; Now, since we try to conform to ANSI, non-standard
2234              ;; lambda-list-keywords should be treated as errors.
2235              (error 'simple-program-error
2236                     :format-control "unrecognized lambda-list keyword ~S ~
2237                      in arglist.~%"
2238                     :format-arguments (list arg)))
2239            ;; When we are at a lambda-list keyword, the parameters
2240            ;; don't include the lambda-list keyword; the lambda-list
2241            ;; does include the lambda-list keyword; and no
2242            ;; specializers are allowed to follow the lambda-list
2243            ;; keywords (at least for now).
2244            (multiple-value-bind (parameters lambda-list)
2245                (parse-specialized-lambda-list (cdr arglist) t)
2246              (when (eq arg '&rest)
2247                ;; check, if &rest is followed by a var ...
2248                (when (or (null lambda-list)
2249                          (memq (car lambda-list) lambda-list-keywords))
2250                  (error "Error in lambda-list:~%~
2251                          After &REST, a DEFMETHOD lambda-list ~
2252                          must be followed by at least one variable.")))
2253              (values parameters
2254                      (cons arg lambda-list)
2255                      ()
2256                      ())))
2257           (post-keyword
2258            ;; After a lambda-list keyword there can be no specializers.
2259            (multiple-value-bind (parameters lambda-list)
2260                (parse-specialized-lambda-list (cdr arglist) t)
2261              (values (cons (if (listp arg) (car arg) arg) parameters)
2262                      (cons arg lambda-list)
2263                      ()
2264                      ())))
2265           (t
2266            (multiple-value-bind (parameters lambda-list specializers required)
2267                (parse-specialized-lambda-list (cdr arglist))
2268              (values (cons (if (listp arg) (car arg) arg) parameters)
2269                      (cons (if (listp arg) (car arg) arg) lambda-list)
2270                      (cons (if (listp arg) (cadr arg) t) specializers)
2271                      (cons (if (listp arg) (car arg) arg) required)))))))
2272 \f
2273 (setq *boot-state* 'early)
2274 \f
2275 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2276 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2277 ;;; walker stuff was only used for implementing stuff like that; maybe
2278 ;;; it's not needed any more? Hunt down what it was used for and see.
2279
2280 (defmacro with-slots (slots instance &body body)
2281   (let ((in (gensym)))
2282     `(let ((,in ,instance))
2283        (declare (ignorable ,in))
2284        ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2285                              (third instance)
2286                              instance)))
2287            (and (symbolp instance)
2288                 `((declare (%variable-rebinding ,in ,instance)))))
2289        ,in
2290        (symbol-macrolet ,(mapcar (lambda (slot-entry)
2291                                    (let ((var-name
2292                                           (if (symbolp slot-entry)
2293                                               slot-entry
2294                                               (car slot-entry)))
2295                                          (slot-name
2296                                           (if (symbolp slot-entry)
2297                                               slot-entry
2298                                               (cadr slot-entry))))
2299                                      `(,var-name
2300                                        (slot-value ,in ',slot-name))))
2301                                  slots)
2302                         ,@body))))
2303
2304 (defmacro with-accessors (slots instance &body body)
2305   (let ((in (gensym)))
2306     `(let ((,in ,instance))
2307        (declare (ignorable ,in))
2308        ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2309                              (third instance)
2310                              instance)))
2311            (and (symbolp instance)
2312                 `((declare (%variable-rebinding ,in ,instance)))))
2313        ,in
2314        (symbol-macrolet ,(mapcar (lambda (slot-entry)
2315                                    (let ((var-name (car slot-entry))
2316                                          (accessor-name (cadr slot-entry)))
2317                                      `(,var-name (,accessor-name ,in))))
2318                                  slots)
2319           ,@body))))