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