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