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