source: trunk/abcl/src/org/armedbear/lisp/clos.lisp @ 12583

Last change on this file since 12583 was 12583, checked in by astalla, 13 years ago

JAVA-CLASS metaclass reimplemented in Lisp.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 105.4 KB
Line 
1;;; clos.lisp
2;;;
3;;; Copyright (C) 2003-2007 Peter Graves
4;;; $Id: clos.lisp 12583 2010-04-08 19:44:14Z astalla $
5;;;
6;;; This program is free software; you can redistribute it and/or
7;;; modify it under the terms of the GNU General Public License
8;;; as published by the Free Software Foundation; either version 2
9;;; of the License, or (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19;;;
20;;; As a special exception, the copyright holders of this library give you
21;;; permission to link this library with independent modules to produce an
22;;; executable, regardless of the license terms of these independent
23;;; modules, and to copy and distribute the resulting executable under
24;;; terms of your choice, provided that you also meet, for each linked
25;;; independent module, the terms and conditions of the license of that
26;;; module.  An independent module is a module which is not derived from
27;;; or based on this library.  If you modify this library, you may extend
28;;; this exception to your version of the library, but you are not
29;;; obligated to do so.  If you do not wish to do so, delete this
30;;; exception statement from your version.
31
32;;; Originally based on Closette.
33
34;;; Closette Version 1.0 (February 10, 1991)
35;;;
36;;; Copyright (c) 1990, 1991 Xerox Corporation.
37;;; All rights reserved.
38;;;
39;;; Use and copying of this software and preparation of derivative works
40;;; based upon this software are permitted.  Any distribution of this
41;;; software or derivative works must comply with all applicable United
42;;; States export control laws.
43;;;
44;;; This software is made available AS IS, and Xerox Corporation makes no
45;;; warranty about the software, its performance or its conformity to any
46;;; specification.
47;;;
48;;; Closette is an implementation of a subset of CLOS with a metaobject
49;;; protocol as described in "The Art of The Metaobject Protocol",
50;;; MIT Press, 1991.
51
52(in-package #:mop)
53
54(export '(class-precedence-list class-slots))
55
56;; Don't use DEFVAR, because that disallows loading clos.lisp
57;; after compiling it: the binding won't get assigned to T anymore
58(defparameter *clos-booting* t)
59
60(defmacro define-class->%class-forwarder (name)
61  (let* (($name (if (consp name) (cadr name) name))
62         (%name (intern (concatenate 'string
63                                     "%"
64                                     (if (consp name)
65                                         (symbol-name 'set-) "")
66                                     (symbol-name $name))
67                        (symbol-package $name))))
68    `(progn
69       (declaim (notinline ,name))
70       (defun ,name (&rest args)
71         (apply #',%name args)))))
72
73(define-class->%class-forwarder class-name)
74(define-class->%class-forwarder (setf class-name))
75(define-class->%class-forwarder class-slots)
76(define-class->%class-forwarder (setf class-slots))
77(define-class->%class-forwarder class-direct-slots)
78(define-class->%class-forwarder (setf class-direct-slots))
79(define-class->%class-forwarder class-layout)
80(define-class->%class-forwarder (setf class-layout))
81(define-class->%class-forwarder class-direct-superclasses)
82(define-class->%class-forwarder (setf class-direct-superclasses))
83(define-class->%class-forwarder class-direct-subclasses)
84(define-class->%class-forwarder (setf class-direct-subclasses))
85(define-class->%class-forwarder class-direct-methods)
86(define-class->%class-forwarder (setf class-direct-methods))
87(define-class->%class-forwarder class-precedence-list)
88(define-class->%class-forwarder (setf class-precedence-list))
89(define-class->%class-forwarder class-finalized-p)
90(define-class->%class-forwarder (setf class-finalized-p))
91(define-class->%class-forwarder class-default-initargs)
92(define-class->%class-forwarder (setf class-default-initargs))
93(define-class->%class-forwarder class-direct-default-initargs)
94(define-class->%class-forwarder (setf class-direct-default-initargs))
95
96(defun no-applicable-method (generic-function &rest args)
97  (error "There is no applicable method for the generic function ~S when called with arguments ~S."
98         generic-function
99         args))
100
101
102
103(defmacro push-on-end (value location)
104  `(setf ,location (nconc ,location (list ,value))))
105
106;;; (SETF GETF*) is like (SETF GETF) except that it always changes the list,
107;;; which must be non-nil.
108
109(defun (setf getf*) (new-value plist key)
110  (block body
111    (do ((x plist (cddr x)))
112        ((null x))
113      (when (eq (car x) key)
114        (setf (car (cdr x)) new-value)
115        (return-from body new-value)))
116    (push-on-end key plist)
117    (push-on-end new-value plist)
118    new-value))
119
120(defun mapappend (fun &rest args)
121  (if (some #'null args)
122      ()
123      (append (apply fun (mapcar #'car args))
124              (apply #'mapappend fun (mapcar #'cdr args)))))
125
126(defun mapplist (fun x)
127  (if (null x)
128      ()
129      (cons (funcall fun (car x) (cadr x))
130            (mapplist fun (cddr x)))))
131
132(defsetf std-instance-layout %set-std-instance-layout)
133(defsetf standard-instance-access %set-standard-instance-access)
134
135(defun (setf find-class) (new-value symbol &optional errorp environment)
136  (declare (ignore errorp environment))
137  (%set-find-class symbol new-value))
138
139(defun canonicalize-direct-slots (direct-slots)
140  `(list ,@(mapcar #'canonicalize-direct-slot direct-slots)))
141
142(defun canonicalize-direct-slot (spec)
143  (if (symbolp spec)
144      `(list :name ',spec)
145      (let ((name (car spec))
146            (initfunction nil)
147            (initform nil)
148            (initargs ())
149            (type nil)
150            (allocation nil)
151            (documentation nil)
152            (readers ())
153            (writers ())
154            (other-options ()))
155        (do ((olist (cdr spec) (cddr olist)))
156            ((null olist))
157          (case (car olist)
158            (:initform
159             (when initform
160               (error 'program-error
161                      "duplicate slot option :INITFORM for slot named ~S"
162                      name))
163             (setq initfunction
164                   `(function (lambda () ,(cadr olist))))
165             (setq initform `',(cadr olist)))
166            (:initarg
167             (push-on-end (cadr olist) initargs))
168            (:allocation
169             (when allocation
170               (error 'program-error
171                      "duplicate slot option :ALLOCATION for slot named ~S"
172                      name))
173             (setf allocation (cadr olist))
174             (push-on-end (car olist) other-options)
175             (push-on-end (cadr olist) other-options))
176            (:type
177             (when type
178               (error 'program-error
179                      "duplicate slot option :TYPE for slot named ~S"
180                      name))
181             (setf type (cadr olist))) ;; FIXME type is ignored
182            (:documentation
183             (when documentation
184               (error 'program-error
185                      "duplicate slot option :DOCUMENTATION for slot named ~S"
186                      name))
187             (setf documentation (cadr olist))) ;; FIXME documentation is ignored
188            (:reader
189             (maybe-note-name-defined (cadr olist))
190             (push-on-end (cadr olist) readers))
191            (:writer
192             (maybe-note-name-defined (cadr olist))
193             (push-on-end (cadr olist) writers))
194            (:accessor
195             (maybe-note-name-defined (cadr olist))
196             (push-on-end (cadr olist) readers)
197             (push-on-end `(setf ,(cadr olist)) writers))
198            (t
199             (error 'program-error
200                    "invalid initialization argument ~S for slot named ~S"
201                    (car olist) name))))
202        `(list
203          :name ',name
204          ,@(when initfunction
205              `(:initform ,initform
206                          :initfunction ,initfunction))
207          ,@(when initargs `(:initargs ',initargs))
208          ,@(when readers `(:readers ',readers))
209          ,@(when writers `(:writers ',writers))
210          ,@other-options))))
211
212(defun maybe-note-name-defined (name)
213  (when (fboundp 'note-name-defined)
214    (note-name-defined name)))
215
216(defun canonicalize-direct-superclasses (direct-superclasses)
217  (let ((classes '()))
218    (dolist (class-specifier direct-superclasses)
219      (if (classp class-specifier)
220          (push class-specifier classes)
221          (let ((class (find-class class-specifier nil)))
222            (unless class
223              (setf class (make-forward-referenced-class class-specifier)))
224            (push class classes))))
225    (nreverse classes)))
226
227(defun canonicalize-defclass-options (options)
228  (mapappend #'canonicalize-defclass-option options))
229
230(defun canonicalize-defclass-option (option)
231  (case (car option)
232    (:metaclass
233     (list ':metaclass
234           `(find-class ',(cadr option))))
235    (:default-initargs
236     (list
237      ':direct-default-initargs
238      `(list ,@(mapappend
239                #'(lambda (x) x)
240                (mapplist
241                 #'(lambda (key value)
242                    `(',key ,(make-initfunction value)))
243                 (cdr option))))))
244    ((:documentation :report)
245     (list (car option) `',(cadr option)))
246    (t
247     (error 'program-error
248            :format-control "invalid DEFCLASS option ~S"
249            :format-arguments (list (car option))))))
250
251(defun make-initfunction (initform)
252  `(function (lambda () ,initform)))
253
254(defun make-direct-slot-definition (class &key name
255                                          (initargs ())
256                                          (initform nil)
257                                          (initfunction nil)
258                                          (readers ())
259                                          (writers ())
260                                          (allocation :instance)
261                                          &allow-other-keys)
262  (let ((slot (make-slot-definition)))
263    (set-slot-definition-name slot name)
264    (set-slot-definition-initargs slot initargs)
265    (set-slot-definition-initform slot initform)
266    (set-slot-definition-initfunction slot initfunction)
267    (set-slot-definition-readers slot readers)
268    (set-slot-definition-writers slot writers)
269    (set-slot-definition-allocation slot allocation)
270    (set-slot-definition-allocation-class slot class)
271    slot))
272
273(defun make-effective-slot-definition (&key name
274                                            (initargs ())
275                                            (initform nil)
276                                            (initfunction nil)
277                                            (allocation :instance)
278                                            (allocation-class nil)
279                                            &allow-other-keys)
280  (let ((slot (make-slot-definition)))
281    (set-slot-definition-name slot name)
282    (set-slot-definition-initargs slot initargs)
283    (set-slot-definition-initform slot initform)
284    (set-slot-definition-initfunction slot initfunction)
285    (set-slot-definition-allocation slot allocation)
286    (set-slot-definition-allocation-class slot allocation-class)
287    slot))
288
289;;; finalize-inheritance
290
291(defun std-compute-class-default-initargs (class)
292  (mapcan #'(lambda (c)
293              (copy-list
294               (class-direct-default-initargs c)))
295          (class-precedence-list class)))
296
297(defun std-finalize-inheritance (class)
298  (setf (class-precedence-list class)
299   (funcall (if (eq (class-of class) (find-class 'standard-class))
300                #'std-compute-class-precedence-list
301                #'compute-class-precedence-list)
302            class))
303  (dolist (class (class-precedence-list class))
304    (when (typep class 'forward-referenced-class)
305      (return-from std-finalize-inheritance)))
306  (setf (class-slots class)
307                   (funcall (if (eq (class-of class) (find-class 'standard-class))
308                                #'std-compute-slots
309                     #'compute-slots) class))
310  (let ((old-layout (class-layout class))
311        (length 0)
312        (instance-slots '())
313        (shared-slots '()))
314    (dolist (slot (class-slots class))
315      (case (%slot-definition-allocation slot)
316        (:instance
317         (set-slot-definition-location slot length)
318         (incf length)
319         (push (%slot-definition-name slot) instance-slots))
320        (:class
321         (unless (%slot-definition-location slot)
322           (let ((allocation-class (%slot-definition-allocation-class slot)))
323             (set-slot-definition-location slot
324                                           (if (eq allocation-class class)
325                                               (cons (%slot-definition-name slot) +slot-unbound+)
326                                               (slot-location allocation-class (%slot-definition-name slot))))))
327         (push (%slot-definition-location slot) shared-slots))))
328    (when old-layout
329      ;; Redefined class: initialize added shared slots.
330      (dolist (location shared-slots)
331        (let* ((slot-name (car location))
332               (old-location (layout-slot-location old-layout slot-name)))
333          (unless old-location
334            (let* ((slot-definition (find slot-name (class-slots class) :key #'%slot-definition-name))
335                   (initfunction (%slot-definition-initfunction slot-definition)))
336              (when initfunction
337                (setf (cdr location) (funcall initfunction))))))))
338    (setf (class-layout class)
339          (make-layout class (nreverse instance-slots) (nreverse shared-slots))))
340  (setf (class-default-initargs class)
341        (std-compute-class-default-initargs class))
342  (setf (class-finalized-p class) t))
343
344;;; Class precedence lists
345
346(defun std-compute-class-precedence-list (class)
347  (let ((classes-to-order (collect-superclasses* class)))
348    (topological-sort classes-to-order
349                      (remove-duplicates
350                       (mapappend #'local-precedence-ordering
351                                  classes-to-order))
352                      #'std-tie-breaker-rule)))
353
354;;; topological-sort implements the standard algorithm for topologically
355;;; sorting an arbitrary set of elements while honoring the precedence
356;;; constraints given by a set of (X,Y) pairs that indicate that element
357;;; X must precede element Y.  The tie-breaker procedure is called when it
358;;; is necessary to choose from multiple minimal elements; both a list of
359;;; candidates and the ordering so far are provided as arguments.
360
361(defun topological-sort (elements constraints tie-breaker)
362  (let ((remaining-constraints constraints)
363        (remaining-elements elements)
364        (result ()))
365    (loop
366      (let ((minimal-elements
367             (remove-if
368              #'(lambda (class)
369                 (member class remaining-constraints
370                         :key #'cadr))
371              remaining-elements)))
372        (when (null minimal-elements)
373          (if (null remaining-elements)
374              (return-from topological-sort result)
375              (error "Inconsistent precedence graph.")))
376        (let ((choice (if (null (cdr minimal-elements))
377                          (car minimal-elements)
378                          (funcall tie-breaker
379                                   minimal-elements
380                                   result))))
381          (setq result (append result (list choice)))
382          (setq remaining-elements
383                (remove choice remaining-elements))
384          (setq remaining-constraints
385                (remove choice
386                        remaining-constraints
387                        :test #'member)))))))
388
389;;; In the event of a tie while topologically sorting class precedence lists,
390;;; the CLOS Specification says to "select the one that has a direct subclass
391;;; rightmost in the class precedence list computed so far."  The same result
392;;; is obtained by inspecting the partially constructed class precedence list
393;;; from right to left, looking for the first minimal element to show up among
394;;; the direct superclasses of the class precedence list constituent.
395;;; (There's a lemma that shows that this rule yields a unique result.)
396
397(defun std-tie-breaker-rule (minimal-elements cpl-so-far)
398  (dolist (cpl-constituent (reverse cpl-so-far))
399    (let* ((supers (class-direct-superclasses cpl-constituent))
400           (common (intersection minimal-elements supers)))
401      (when (not (null common))
402        (return-from std-tie-breaker-rule (car common))))))
403
404;;; This version of collect-superclasses* isn't bothered by cycles in the class
405;;; hierarchy, which sometimes happen by accident.
406
407(defun collect-superclasses* (class)
408  (labels ((all-superclasses-loop (seen superclasses)
409                                  (let ((to-be-processed
410                                         (set-difference superclasses seen)))
411                                    (if (null to-be-processed)
412                                        superclasses
413                                        (let ((class-to-process
414                                               (car to-be-processed)))
415                                          (all-superclasses-loop
416                                           (cons class-to-process seen)
417                                           (union (class-direct-superclasses
418                                                   class-to-process)
419                                                  superclasses)))))))
420          (all-superclasses-loop () (list class))))
421
422;;; The local precedence ordering of a class C with direct superclasses C_1,
423;;; C_2, ..., C_n is the set ((C C_1) (C_1 C_2) ...(C_n-1 C_n)).
424
425(defun local-precedence-ordering (class)
426  (mapcar #'list
427          (cons class
428                (butlast (class-direct-superclasses class)))
429          (class-direct-superclasses class)))
430
431;;; Slot inheritance
432
433(defun std-compute-slots (class)
434  (let* ((all-slots (mapappend #'class-direct-slots
435                               (class-precedence-list class)))
436         (all-names (remove-duplicates
437                     (mapcar #'%slot-definition-name all-slots))))
438    (mapcar #'(lambda (name)
439               (funcall
440                (if (eq (class-of class) (find-class 'standard-class))
441                    #'std-compute-effective-slot-definition
442                    #'compute-effective-slot-definition)
443                class
444                (remove name all-slots
445                        :key #'%slot-definition-name
446                        :test-not #'eq)))
447            all-names)))
448
449(defun std-compute-effective-slot-definition (class direct-slots)
450  (declare (ignore class))
451  (let ((initer (find-if-not #'null direct-slots
452                             :key #'%slot-definition-initfunction)))
453    (make-effective-slot-definition
454     :name (%slot-definition-name (car direct-slots))
455     :initform (if initer
456                   (%slot-definition-initform initer)
457                   nil)
458     :initfunction (if initer
459                       (%slot-definition-initfunction initer)
460                       nil)
461     :initargs (remove-duplicates
462                (mapappend #'%slot-definition-initargs
463                           direct-slots))
464     :allocation (%slot-definition-allocation (car direct-slots))
465     :allocation-class (%slot-definition-allocation-class (car direct-slots)))))
466
467;;; Standard instance slot access
468
469;;; N.B. The location of the effective-slots slots in the class metaobject for
470;;; standard-class must be determined without making any further slot
471;;; references.
472
473(defun find-slot-definition (class slot-name)
474  (dolist (slot (class-slots class) nil)
475    (when (eq slot-name (%slot-definition-name slot))
476      (return slot))))
477
478(defun slot-location (class slot-name)
479  (let ((slot (find-slot-definition class slot-name)))
480    (if slot
481        (%slot-definition-location slot)
482        nil)))
483
484(defun instance-slot-location (instance slot-name)
485  (let ((layout (std-instance-layout instance)))
486    (and layout (layout-slot-location layout slot-name))))
487
488(defun slot-value (object slot-name)
489  (if (eq (class-of (class-of object)) (find-class 'standard-class))
490      (std-slot-value object slot-name)
491      (slot-value-using-class (class-of object) object slot-name)))
492
493(defsetf std-slot-value set-std-slot-value)
494
495(defun %set-slot-value (object slot-name new-value)
496  (if (eq (class-of (class-of object)) (find-class 'standard-class))
497      (setf (std-slot-value object slot-name) new-value)
498      (set-slot-value-using-class new-value (class-of object)
499                                  object slot-name)))
500
501(defsetf slot-value %set-slot-value)
502
503(defun slot-boundp (object slot-name)
504  (if (eq (class-of (class-of object)) (find-class 'standard-class))
505      (std-slot-boundp object slot-name)
506      (slot-boundp-using-class (class-of object) object slot-name)))
507
508(defun std-slot-makunbound (instance slot-name)
509  (let ((location (instance-slot-location instance slot-name)))
510    (cond ((fixnump location)
511           (setf (standard-instance-access instance location) +slot-unbound+))
512          ((consp location)
513           (setf (cdr location) +slot-unbound+))
514          (t
515           (slot-missing (class-of instance) instance slot-name 'slot-makunbound))))
516  instance)
517
518(defun slot-makunbound (object slot-name)
519  (if (eq (class-of (class-of object)) (find-class 'standard-class))
520      (std-slot-makunbound object slot-name)
521      (slot-makunbound-using-class (class-of object) object slot-name)))
522
523(defun std-slot-exists-p (instance slot-name)
524  (not (null (find slot-name (class-slots (class-of instance))
525                   :key #'%slot-definition-name))))
526
527(defun slot-exists-p (object slot-name)
528  (if (eq (class-of (class-of object)) (find-class 'standard-class))
529      (std-slot-exists-p object slot-name)
530      (slot-exists-p-using-class (class-of object) object slot-name)))
531
532(defun instance-slot-p (slot)
533  (eq (%slot-definition-allocation slot) :instance))
534
535(defun make-instance-standard-class (metaclass
536                                     &key name direct-superclasses direct-slots
537                                     direct-default-initargs
538                                     documentation
539                                     &allow-other-keys)
540  (declare (ignore metaclass))
541  (let ((class (std-allocate-instance (find-class 'standard-class))))
542    (%set-class-name name class)
543    (%set-class-layout nil class)
544    (%set-class-direct-subclasses ()  class)
545    (%set-class-direct-methods ()  class)
546    (%set-class-documentation class documentation)
547    (std-after-initialization-for-classes class
548                                          :direct-superclasses direct-superclasses
549                                          :direct-slots direct-slots
550                                          :direct-default-initargs direct-default-initargs)
551    class))
552
553(defun std-after-initialization-for-classes (class
554                                             &key direct-superclasses direct-slots
555                                             direct-default-initargs
556                                             &allow-other-keys)
557  (let ((supers (or direct-superclasses
558                    (list (find-class 'standard-object)))))
559    (setf (class-direct-superclasses class) supers)
560    (dolist (superclass supers)
561      (pushnew class (class-direct-subclasses superclass))))
562  (let ((slots (mapcar #'(lambda (slot-properties)
563                          (apply #'make-direct-slot-definition class slot-properties))
564                       direct-slots)))
565    (setf (class-direct-slots class) slots)
566    (dolist (direct-slot slots)
567      (dolist (reader (%slot-definition-readers direct-slot))
568        (add-reader-method class reader (%slot-definition-name direct-slot)))
569      (dolist (writer (%slot-definition-writers direct-slot))
570        (add-writer-method class writer (%slot-definition-name direct-slot)))))
571  (setf (class-direct-default-initargs class) direct-default-initargs)
572  (funcall (if (eq (class-of class) (find-class 'standard-class))
573               #'std-finalize-inheritance
574               #'finalize-inheritance)
575           class)
576  (values))
577
578(defun canonical-slot-name (canonical-slot)
579  (getf canonical-slot :name))
580
581(defvar *extensible-built-in-classes* (list (find-class 'sequence) (find-class 'java:java-object)))
582
583(defun ensure-class (name &rest all-keys &key metaclass &allow-other-keys)
584  ;; Check for duplicate slots.
585  (remf all-keys :metaclass)
586  (let ((slots (getf all-keys :direct-slots)))
587    (dolist (s1 slots)
588      (let ((name1 (canonical-slot-name s1)))
589        (dolist (s2 (cdr (memq s1 slots)))
590          (when (eq name1 (canonical-slot-name s2))
591            (error 'program-error "Duplicate slot ~S" name1))))))
592  ;; Check for duplicate argument names in :DEFAULT-INITARGS.
593  (let ((names ()))
594    (do* ((initargs (getf all-keys :direct-default-initargs) (cddr initargs))
595          (name (car initargs) (car initargs)))
596         ((null initargs))
597      (push name names))
598    (do* ((names names (cdr names))
599          (name (car names) (car names)))
600         ((null names))
601      (when (memq name (cdr names))
602        (error 'program-error
603               :format-control "Duplicate initialization argument name ~S in :DEFAULT-INITARGS."
604               :format-arguments (list name)))))
605  (let ((direct-superclasses (getf all-keys :direct-superclasses)))
606    (dolist (class direct-superclasses)
607      (when (and (typep class 'built-in-class)
608     (not (member class *extensible-built-in-classes*)))
609        (error "Attempt to define a subclass of a built-in-class: ~S" class))))
610  (let ((old-class (find-class name nil)))
611    (cond ((and old-class (eq name (class-name old-class)))
612           (cond ((typep old-class 'built-in-class)
613                  (error "The symbol ~S names a built-in class." name))
614                 ((typep old-class 'forward-referenced-class)
615                  (let ((new-class (apply #'make-instance-standard-class
616                                          (find-class 'standard-class)
617                                          :name name all-keys)))
618                    (%set-find-class name new-class)
619                    (dolist (subclass (class-direct-subclasses old-class))
620                      (setf (class-direct-superclasses subclass)
621                            (substitute new-class old-class
622                                        (class-direct-superclasses subclass))))
623                    new-class))
624                 (t
625                  ;; We're redefining the class.
626                  (%make-instances-obsolete old-class)
627                  (apply #'std-after-initialization-for-classes old-class all-keys)
628                  old-class)))
629          (t
630           (let ((class (apply (if metaclass
631                                   #'make-instance
632                                   #'make-instance-standard-class)
633                               (or metaclass
634                                   (find-class 'standard-class))
635                               :name name all-keys)))
636             (%set-find-class name class)
637             class)))))
638
639(defmacro defclass (&whole form name direct-superclasses direct-slots &rest options)
640  (unless (>= (length form) 3)
641    (error 'program-error "Wrong number of arguments for DEFCLASS."))
642  (check-declaration-type name)
643  `(ensure-class ',name
644                 :direct-superclasses
645                 (canonicalize-direct-superclasses ',direct-superclasses)
646                 :direct-slots
647                 ,(canonicalize-direct-slots direct-slots)
648                 ,@(canonicalize-defclass-options options)))
649
650(eval-when (:compile-toplevel :load-toplevel :execute)
651  (defstruct method-combination
652    name
653    operator
654    identity-with-one-argument
655    documentation)
656
657  (defun expand-short-defcombin (whole)
658    (let* ((name (cadr whole))
659           (documentation
660            (getf (cddr whole) :documentation ""))
661           (identity-with-one-arg
662            (getf (cddr whole) :identity-with-one-argument nil))
663           (operator
664            (getf (cddr whole) :operator name)))
665      `(progn
666         (setf (get ',name 'method-combination-object)
667               (make-method-combination :name ',name
668                                        :operator ',operator
669                                        :identity-with-one-argument ',identity-with-one-arg
670                                        :documentation ',documentation))
671         ',name)))
672
673  (defun expand-long-defcombin (whole)
674    (declare (ignore whole))
675    (error "The long form of DEFINE-METHOD-COMBINATION is not implemented.")))
676
677(defmacro define-method-combination (&whole form &rest args)
678  (declare (ignore args))
679  (if (and (cddr form)
680           (listp (caddr form)))
681      (expand-long-defcombin form)
682      (expand-short-defcombin form)))
683
684(define-method-combination +      :identity-with-one-argument t)
685(define-method-combination and    :identity-with-one-argument t)
686(define-method-combination append :identity-with-one-argument nil)
687(define-method-combination list   :identity-with-one-argument nil)
688(define-method-combination max    :identity-with-one-argument t)
689(define-method-combination min    :identity-with-one-argument t)
690(define-method-combination nconc  :identity-with-one-argument t)
691(define-method-combination or     :identity-with-one-argument t)
692(define-method-combination progn  :identity-with-one-argument t)
693
694(defstruct eql-specializer
695  object)
696
697(defparameter *eql-specializer-table* (make-hash-table :test 'eql))
698
699(defun intern-eql-specializer (object)
700  (or (gethash object *eql-specializer-table*)
701      (setf (gethash object *eql-specializer-table*)
702            (make-eql-specializer :object object))))
703
704;; MOP (p. 216) specifies the following reader generic functions:
705;;   generic-function-argument-precedence-order
706;;   generic-function-declarations
707;;   generic-function-lambda-list
708;;   generic-function-method-class
709;;   generic-function-method-combination
710;;   generic-function-methods
711;;   generic-function-name
712
713(defun generic-function-lambda-list (gf)
714  (%generic-function-lambda-list gf))
715(defsetf generic-function-lambda-list %set-generic-function-lambda-list)
716
717(defun (setf generic-function-documentation) (new-value gf)
718  (set-generic-function-documentation gf new-value))
719
720(defun (setf generic-function-initial-methods) (new-value gf)
721  (set-generic-function-initial-methods gf new-value))
722
723(defun (setf generic-function-methods) (new-value gf)
724  (set-generic-function-methods gf new-value))
725
726(defun (setf generic-function-method-class) (new-value gf)
727  (set-generic-function-method-class gf new-value))
728
729(defun (setf generic-function-method-combination) (new-value gf)
730  (set-generic-function-method-combination gf new-value))
731
732(defun (setf generic-function-argument-precedence-order) (new-value gf)
733  (set-generic-function-argument-precedence-order gf new-value))
734
735(declaim (ftype (function * t) classes-to-emf-table))
736(defun classes-to-emf-table (gf)
737  (generic-function-classes-to-emf-table gf))
738
739(defun (setf classes-to-emf-table) (new-value gf)
740  (set-generic-function-classes-to-emf-table gf new-value))
741
742(defvar the-class-standard-method (find-class 'standard-method))
743
744(defun (setf method-lambda-list) (new-value method)
745  (set-method-lambda-list method new-value))
746
747(defun (setf method-qualifiers) (new-value method)
748  (set-method-qualifiers method new-value))
749
750(defun (setf method-documentation) (new-value method)
751  (set-method-documentation method new-value))
752
753;;; defgeneric
754
755(defmacro defgeneric (function-name lambda-list
756                                    &rest options-and-method-descriptions)
757  (let ((options ())
758        (methods ())
759        (documentation nil))
760    (dolist (item options-and-method-descriptions)
761      (case (car item)
762        (declare) ; FIXME
763        (:documentation
764         (when documentation
765           (error 'program-error
766                  :format-control "Documentation option was specified twice for generic function ~S."
767                  :format-arguments (list function-name)))
768         (setf documentation t)
769         (push item options))
770        (:method
771         (push
772          `(push (defmethod ,function-name ,@(cdr item))
773                 (generic-function-initial-methods (fdefinition ',function-name)))
774          methods))
775        (t
776         (push item options))))
777    (setf options (nreverse options)
778          methods (nreverse methods))
779    `(prog1
780       (%defgeneric
781        ',function-name
782        :lambda-list ',lambda-list
783        ,@(canonicalize-defgeneric-options options))
784       ,@methods)))
785
786(defun canonicalize-defgeneric-options (options)
787  (mapappend #'canonicalize-defgeneric-option options))
788
789(defun canonicalize-defgeneric-option (option)
790  (case (car option)
791    (:generic-function-class
792     (list :generic-function-class `(find-class ',(cadr option))))
793    (:method-class
794     (list :method-class `(find-class ',(cadr option))))
795    (:method-combination
796     (list :method-combination `',(cdr option)))
797    (:argument-precedence-order
798     (list :argument-precedence-order `',(cdr option)))
799    (t
800     (list `',(car option) `',(cadr option)))))
801
802;; From OpenMCL.
803(defun canonicalize-argument-precedence-order (apo req)
804  (cond ((equal apo req) nil)
805        ((not (eql (length apo) (length req)))
806         (error 'program-error
807                :format-control "Specified argument precedence order ~S does not match lambda list."
808                :format-arguments (list apo)))
809        (t (let ((res nil))
810             (dolist (arg apo (nreverse res))
811               (let ((index (position arg req)))
812                 (if (or (null index) (memq index res))
813                     (error 'program-error
814                            :format-control "Specified argument precedence order ~S does not match lambda list."
815                            :format-arguments (list apo)))
816                 (push index res)))))))
817
818(defun find-generic-function (name &optional (errorp t))
819  (let ((function (and (fboundp name) (fdefinition name))))
820    (when function
821      (when (typep function 'generic-function)
822        (return-from find-generic-function function))
823      (when (and *traced-names* (find name *traced-names* :test #'equal))
824        (setf function (untraced-function name))
825        (when (typep function 'generic-function)
826          (return-from find-generic-function function)))))
827  (if errorp
828      (error "There is no generic function named ~S." name)
829      nil))
830
831(defun lambda-lists-congruent-p (lambda-list1 lambda-list2)
832  (let* ((plist1 (analyze-lambda-list lambda-list1))
833         (args1 (getf plist1 :required-args))
834         (plist2 (analyze-lambda-list lambda-list2))
835         (args2 (getf plist2 :required-args)))
836    (= (length args1) (length args2))))
837
838(defun %defgeneric (function-name &rest all-keys)
839  (when (fboundp function-name)
840    (let ((gf (fdefinition function-name)))
841      (when (typep gf 'generic-function)
842        ;; Remove methods defined by previous DEFGENERIC forms.
843        (dolist (method (generic-function-initial-methods gf))
844          (%remove-method gf method))
845        (setf (generic-function-initial-methods gf) '()))))
846  (apply 'ensure-generic-function function-name all-keys))
847
848(defun ensure-generic-function (function-name
849                                &rest all-keys
850                                &key
851                                lambda-list
852                                (generic-function-class (find-class 'standard-generic-function))
853                                (method-class the-class-standard-method)
854                                (method-combination 'standard)
855                                (argument-precedence-order nil apo-p)
856                                documentation
857                                &allow-other-keys)
858  (when (autoloadp function-name)
859    (resolve function-name))
860  (let ((gf (find-generic-function function-name nil)))
861    (if gf
862        (progn
863          (unless (or (null (generic-function-methods gf))
864                      (lambda-lists-congruent-p lambda-list (generic-function-lambda-list gf)))
865            (error 'simple-error
866                   :format-control "The lambda list ~S is incompatible with the existing methods of ~S."
867                   :format-arguments (list lambda-list gf)))
868          (setf (generic-function-lambda-list gf) lambda-list)
869          (setf (generic-function-documentation gf) documentation)
870          (let* ((plist (analyze-lambda-list lambda-list))
871                 (required-args (getf plist ':required-args)))
872            (%set-gf-required-args gf required-args)
873            (when apo-p
874              (setf (generic-function-argument-precedence-order gf)
875                    (if argument-precedence-order
876                        (canonicalize-argument-precedence-order argument-precedence-order
877                                                                required-args)
878                        nil)))
879            (finalize-generic-function gf))
880          gf)
881        (progn
882          (when (and (null *clos-booting*)
883                     (fboundp function-name))
884            (error 'program-error
885                   :format-control "~A already names an ordinary function, macro, or special operator."
886                   :format-arguments (list function-name)))
887          (setf gf (apply (if (eq generic-function-class (find-class 'standard-generic-function))
888                              #'make-instance-standard-generic-function
889                              #'make-instance)
890                          generic-function-class
891                          :name function-name
892                          :method-class method-class
893                          :method-combination method-combination
894                          all-keys))
895          gf))))
896
897(defun initial-discriminating-function (gf args)
898  (set-funcallable-instance-function
899   gf
900   (funcall (if (eq (class-of gf) (find-class 'standard-generic-function))
901                #'std-compute-discriminating-function
902                #'compute-discriminating-function)
903            gf))
904  (apply gf args))
905
906(defun collect-eql-specializer-objects (generic-function)
907  (let ((result nil))
908    (dolist (method (generic-function-methods generic-function))
909      (dolist (specializer (%method-specializers method))
910        (when (typep specializer 'eql-specializer)
911          (pushnew (eql-specializer-object specializer)
912                   result
913                   :test 'eql))))
914    result))
915
916(defun finalize-generic-function (gf)
917  (%finalize-generic-function gf)
918  (setf (classes-to-emf-table gf) (make-hash-table :test #'equal))
919  (%init-eql-specializations gf (collect-eql-specializer-objects gf))
920  (set-funcallable-instance-function
921   gf #'(lambda (&rest args)
922          (initial-discriminating-function gf args)))
923  ;; FIXME Do we need to warn on redefinition somewhere else?
924  (let ((*warn-on-redefinition* nil))
925    (setf (fdefinition (%generic-function-name gf)) gf))
926  (values))
927
928(defun make-instance-standard-generic-function (generic-function-class
929                                                &key name lambda-list
930                                                method-class
931                                                method-combination
932                                                argument-precedence-order
933                                                documentation)
934  (declare (ignore generic-function-class))
935  (let ((gf (std-allocate-instance (find-class 'standard-generic-function))))
936    (%set-generic-function-name gf name)
937    (setf (generic-function-lambda-list gf) lambda-list)
938    (setf (generic-function-initial-methods gf) ())
939    (setf (generic-function-methods gf) ())
940    (setf (generic-function-method-class gf) method-class)
941    (setf (generic-function-method-combination gf) method-combination)
942    (setf (generic-function-documentation gf) documentation)
943    (setf (classes-to-emf-table gf) nil)
944    (let* ((plist (analyze-lambda-list (generic-function-lambda-list gf)))
945           (required-args (getf plist ':required-args)))
946      (%set-gf-required-args gf required-args)
947      (setf (generic-function-argument-precedence-order gf)
948            (if argument-precedence-order
949                (canonicalize-argument-precedence-order argument-precedence-order
950                                                        required-args)
951                nil)))
952    (finalize-generic-function gf)
953    gf))
954
955(defun canonicalize-specializers (specializers)
956  (mapcar #'canonicalize-specializer specializers))
957
958(defun canonicalize-specializer (specializer)
959  (cond ((classp specializer)
960         specializer)
961        ((eql-specializer-p specializer)
962         specializer)
963        ((symbolp specializer)
964         (find-class specializer))
965        ((and (consp specializer)
966              (eq (car specializer) 'eql))
967         (let ((object (cadr specializer)))
968           (when (and (consp object)
969                      (eq (car object) 'quote))
970             (setf object (cadr object)))
971           (intern-eql-specializer object)))
972  ((and (consp specializer)
973              (eq (car specializer) 'java:jclass))
974         (let ((jclass (eval specializer)))
975     (java::ensure-java-class jclass)))
976        (t
977         (error "Unknown specializer: ~S" specializer))))
978
979(defun parse-defmethod (args)
980  (let ((function-name (car args))
981        (qualifiers ())
982        (specialized-lambda-list ())
983        (body ())
984        (parse-state :qualifiers))
985    (dolist (arg (cdr args))
986      (ecase parse-state
987        (:qualifiers
988         (if (and (atom arg) (not (null arg)))
989             (push arg qualifiers)
990             (progn
991               (setf specialized-lambda-list arg)
992               (setf parse-state :body))))
993        (:body (push arg body))))
994    (setf qualifiers (nreverse qualifiers)
995          body (nreverse body))
996    (multiple-value-bind (real-body declarations documentation)
997        (parse-body body)
998      (values function-name
999              qualifiers
1000              (extract-lambda-list specialized-lambda-list)
1001              (extract-specializers specialized-lambda-list)
1002              documentation
1003              declarations
1004              (list* 'block
1005                     (fdefinition-block-name function-name)
1006                     real-body)))))
1007
1008(defun required-portion (gf args)
1009  (let ((number-required (length (gf-required-args gf))))
1010    (when (< (length args) number-required)
1011      (error 'program-error
1012             :format-control "Not enough arguments for generic function ~S."
1013             :format-arguments (list (%generic-function-name gf))))
1014    (subseq args 0 number-required)))
1015
1016(defun extract-lambda-list (specialized-lambda-list)
1017  (let* ((plist (analyze-lambda-list specialized-lambda-list))
1018         (requireds (getf plist :required-names))
1019         (rv (getf plist :rest-var))
1020         (ks (getf plist :key-args))
1021         (keysp (getf plist :keysp))
1022         (aok (getf plist :allow-other-keys))
1023         (opts (getf plist :optional-args))
1024         (auxs (getf plist :auxiliary-args)))
1025    `(,@requireds
1026      ,@(if rv `(&rest ,rv) ())
1027      ,@(if (or ks keysp aok) `(&key ,@ks) ())
1028      ,@(if aok '(&allow-other-keys) ())
1029      ,@(if opts `(&optional ,@opts) ())
1030      ,@(if auxs `(&aux ,@auxs) ()))))
1031
1032(defun extract-specializers (specialized-lambda-list)
1033  (let ((plist (analyze-lambda-list specialized-lambda-list)))
1034    (getf plist ':specializers)))
1035
1036(defun get-keyword-from-arg (arg)
1037  (if (listp arg)
1038      (if (listp (car arg))
1039          (caar arg)
1040          (make-keyword (car arg)))
1041      (make-keyword arg)))
1042
1043(defun analyze-lambda-list (lambda-list)
1044  (let ((keys ())           ; Just the keywords
1045        (key-args ())       ; Keywords argument specs
1046        (keysp nil)         ;
1047        (required-names ()) ; Just the variable names
1048        (required-args ())  ; Variable names & specializers
1049        (specializers ())   ; Just the specializers
1050        (rest-var nil)
1051        (optionals ())
1052        (auxs ())
1053        (allow-other-keys nil)
1054        (state :parsing-required))
1055    (dolist (arg lambda-list)
1056      (if (member arg lambda-list-keywords)
1057          (ecase arg
1058            (&optional
1059             (setq state :parsing-optional))
1060            (&rest
1061             (setq state :parsing-rest))
1062            (&key
1063             (setq keysp t)
1064             (setq state :parsing-key))
1065            (&allow-other-keys
1066             (setq allow-other-keys 't))
1067            (&aux
1068             (setq state :parsing-aux)))
1069          (case state
1070            (:parsing-required
1071             (push-on-end arg required-args)
1072             (if (listp arg)
1073                 (progn (push-on-end (car arg) required-names)
1074                   (push-on-end (cadr arg) specializers))
1075                 (progn (push-on-end arg required-names)
1076                   (push-on-end 't specializers))))
1077            (:parsing-optional (push-on-end arg optionals))
1078            (:parsing-rest (setq rest-var arg))
1079            (:parsing-key
1080             (push-on-end (get-keyword-from-arg arg) keys)
1081             (push-on-end arg key-args))
1082            (:parsing-aux (push-on-end arg auxs)))))
1083    (list  :required-names required-names
1084           :required-args required-args
1085           :specializers specializers
1086           :rest-var rest-var
1087           :keywords keys
1088           :key-args key-args
1089           :keysp keysp
1090           :auxiliary-args auxs
1091           :optional-args optionals
1092           :allow-other-keys allow-other-keys)))
1093
1094#+nil
1095(defun check-method-arg-info (gf arg-info method)
1096  (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1097      (analyze-lambda-list (if (consp method)
1098                               (early-method-lambda-list method)
1099                               (method-lambda-list method)))
1100    (flet ((lose (string &rest args)
1101                 (error 'simple-program-error
1102                        :format-control "~@<attempt to add the method~2I~_~S~I~_~
1103                        to the generic function~2I~_~S;~I~_~
1104                        but ~?~:>"
1105                        :format-arguments (list method gf string args)))
1106           (comparison-description (x y)
1107                                   (if (> x y) "more" "fewer")))
1108      (let ((gf-nreq (arg-info-number-required arg-info))
1109            (gf-nopt (arg-info-number-optional arg-info))
1110            (gf-key/rest-p (arg-info-key/rest-p arg-info))
1111            (gf-keywords (arg-info-keys arg-info)))
1112        (unless (= nreq gf-nreq)
1113          (lose
1114           "the method has ~A required arguments than the generic function."
1115           (comparison-description nreq gf-nreq)))
1116        (unless (= nopt gf-nopt)
1117          (lose
1118           "the method has ~A optional arguments than the generic function."
1119           (comparison-description nopt gf-nopt)))
1120        (unless (eq (or keysp restp) gf-key/rest-p)
1121          (lose
1122           "the method and generic function differ in whether they accept~_~
1123            &REST or &KEY arguments."))
1124        (when (consp gf-keywords)
1125          (unless (or (and restp (not keysp))
1126                      allow-other-keys-p
1127                      (every (lambda (k) (memq k keywords)) gf-keywords))
1128            (lose "the method does not accept each of the &KEY arguments~2I~_~
1129            ~S."
1130                  gf-keywords)))))))
1131
1132(defun check-method-lambda-list (method-lambda-list gf-lambda-list)
1133  (let* ((gf-restp (not (null (memq '&rest gf-lambda-list))))
1134         (gf-plist (analyze-lambda-list gf-lambda-list))
1135         (gf-keysp (getf gf-plist :keysp))
1136         (gf-keywords (getf gf-plist :keywords))
1137         (method-plist (analyze-lambda-list method-lambda-list))
1138         (method-restp (not (null (memq '&rest method-lambda-list))))
1139         (method-keysp (getf method-plist :keysp))
1140         (method-keywords (getf method-plist :keywords))
1141         (method-allow-other-keys-p (getf method-plist :allow-other-keys)))
1142    (unless (= (length (getf gf-plist :required-args))
1143               (length (getf method-plist :required-args)))
1144      (error "The method has the wrong number of required arguments for the generic function."))
1145    (unless (= (length (getf gf-plist :optional-args))
1146               (length (getf method-plist :optional-args)))
1147      (error "The method has the wrong number of optional arguments for the generic function."))
1148    (unless (eq (or gf-restp gf-keysp) (or method-restp method-keysp))
1149      (error "The method and the generic function differ in whether they accept &REST or &KEY arguments."))
1150    (when (consp gf-keywords)
1151      (unless (or (and method-restp (not method-keysp))
1152                  method-allow-other-keys-p
1153                  (every (lambda (k) (memq k method-keywords)) gf-keywords))
1154        (error "The method does not accept all of the keyword arguments defined for the generic function.")))))
1155
1156(declaim (ftype (function * method) ensure-method))
1157(defun ensure-method (name &rest all-keys)
1158  (let ((method-lambda-list (getf all-keys :lambda-list))
1159        (gf (find-generic-function name nil)))
1160    (if gf
1161        (check-method-lambda-list method-lambda-list (generic-function-lambda-list gf))
1162        (setf gf (ensure-generic-function name :lambda-list method-lambda-list)))
1163    (let ((method
1164           (if (eq (generic-function-method-class gf) the-class-standard-method)
1165               (apply #'make-instance-standard-method gf all-keys)
1166               (apply #'make-instance (generic-function-method-class gf) all-keys))))
1167      (%add-method gf method)
1168      method)))
1169
1170(defun make-instance-standard-method (gf
1171                                      &key
1172                                      lambda-list
1173                                      qualifiers
1174                                      specializers
1175                                      documentation
1176                                      function
1177                                      fast-function)
1178  (declare (ignore gf))
1179  (let ((method (std-allocate-instance the-class-standard-method)))
1180    (setf (method-lambda-list method) lambda-list)
1181    (setf (method-qualifiers method) qualifiers)
1182    (%set-method-specializers method (canonicalize-specializers specializers))
1183    (setf (method-documentation method) documentation)
1184    (%set-method-generic-function method nil)
1185    (%set-method-function method function)
1186    (%set-method-fast-function method fast-function)
1187    method))
1188
1189(defun %add-method (gf method)
1190  (when (%method-generic-function method)
1191    (error 'simple-error
1192           :format-control "ADD-METHOD: ~S is a method of ~S."
1193           :format-arguments (list method (%method-generic-function method))))
1194  ;; Remove existing method with same qualifiers and specializers (if any).
1195  (let ((old-method (%find-method gf (method-qualifiers method)
1196                                 (%method-specializers method) nil)))
1197    (when old-method
1198      (%remove-method gf old-method)))
1199  (%set-method-generic-function method gf)
1200  (push method (generic-function-methods gf))
1201  (dolist (specializer (%method-specializers method))
1202    (when (typep specializer 'class) ;; FIXME What about EQL specializer objects?
1203      (pushnew method (class-direct-methods specializer))))
1204  (finalize-generic-function gf)
1205  gf)
1206
1207(defun %remove-method (gf method)
1208  (setf (generic-function-methods gf)
1209        (remove method (generic-function-methods gf)))
1210  (%set-method-generic-function method nil)
1211  (dolist (specializer (%method-specializers method))
1212    (when (typep specializer 'class) ;; FIXME What about EQL specializer objects?
1213      (setf (class-direct-methods specializer)
1214            (remove method (class-direct-methods specializer)))))
1215  (finalize-generic-function gf)
1216  gf)
1217
1218(defun %find-method (gf qualifiers specializers &optional (errorp t))
1219  ;; "If the specializers argument does not correspond in length to the number
1220  ;; of required arguments of the generic-function, an an error of type ERROR
1221  ;; is signaled."
1222  (unless (= (length specializers) (length (gf-required-args gf)))
1223    (error "The specializers argument has length ~S, but ~S has ~S required parameters."
1224           (length specializers)
1225           gf
1226           (length (gf-required-args gf))))
1227  (let* ((canonical-specializers (canonicalize-specializers specializers))
1228         (method
1229          (find-if #'(lambda (method)
1230                      (and (equal qualifiers
1231                                  (method-qualifiers method))
1232                           (equal canonical-specializers
1233                                  (%method-specializers method))))
1234                   (generic-function-methods gf))))
1235    (if (and (null method) errorp)
1236        (error "No such method for ~S." (%generic-function-name gf))
1237        method)))
1238
1239(defun fast-callable-p (gf)
1240  (and (eq (generic-function-method-combination gf) 'standard)
1241       (null (intersection (%generic-function-lambda-list gf)
1242                           '(&rest &optional &key &allow-other-keys &aux)))))
1243
1244(declaim (ftype (function * t) slow-method-lookup-1))
1245
1246(declaim (ftype (function (t t t) t) slow-reader-lookup))
1247(defun slow-reader-lookup (gf layout slot-name)
1248  (let ((location (layout-slot-location layout slot-name)))
1249    (cache-slot-location gf layout location)
1250    location))
1251
1252(defun std-compute-discriminating-function (gf)
1253  (let ((code
1254         (cond
1255           ((and (= (length (generic-function-methods gf)) 1)
1256                 (typep (car (generic-function-methods gf)) 'standard-reader-method))
1257            ;;                 (sys::%format t "standard reader function ~S~%" (generic-function-name gf))
1258
1259            (let* ((method (%car (generic-function-methods gf)))
1260                   (class (car (%method-specializers method)))
1261                   (slot-name (reader-method-slot-name method)))
1262              #'(lambda (arg)
1263                  (declare (optimize speed))
1264                  (let* ((layout (std-instance-layout arg))
1265                         (location (get-cached-slot-location gf layout)))
1266                    (unless location
1267                      (unless (simple-typep arg class)
1268                        ;; FIXME no applicable method
1269                        (error 'simple-type-error
1270                               :datum arg
1271                               :expected-type class))
1272                      (setf location (slow-reader-lookup gf layout slot-name)))
1273                    (if (consp location)
1274                        ;; Shared slot.
1275                        (cdr location)
1276                        (standard-instance-access arg location))))))
1277
1278           (t
1279            (let* ((emf-table (classes-to-emf-table gf))
1280                   (number-required (length (gf-required-args gf)))
1281                   (lambda-list (%generic-function-lambda-list gf))
1282                   (exact (null (intersection lambda-list
1283                                              '(&rest &optional &key
1284                                                &allow-other-keys &aux)))))
1285              (if exact
1286                  (cond
1287                    ((= number-required 1)
1288                     (cond
1289                       ((and (eq (generic-function-method-combination gf) 'standard)
1290                             (= (length (generic-function-methods gf)) 1))
1291                        (let* ((method (%car (generic-function-methods gf)))
1292                               (specializer (car (%method-specializers method)))
1293                               (function (or (%method-fast-function method)
1294                                             (%method-function method))))
1295                          (if (eql-specializer-p specializer)
1296                              (let ((specializer-object (eql-specializer-object specializer)))
1297                                #'(lambda (arg)
1298                                    (declare (optimize speed))
1299                                    (if (eql arg specializer-object)
1300                                        (funcall function arg)
1301                                        (no-applicable-method gf (list arg)))))
1302                              #'(lambda (arg)
1303                                  (declare (optimize speed))
1304                                  (unless (simple-typep arg specializer)
1305                                    ;; FIXME no applicable method
1306                                    (error 'simple-type-error
1307                                           :datum arg
1308                                           :expected-type specializer))
1309                                  (funcall function arg)))))
1310                       (t
1311                        #'(lambda (arg)
1312                            (declare (optimize speed))
1313                            (let* ((specialization
1314                                    (%get-arg-specialization gf arg))
1315                                   (emfun (or (gethash1 specialization
1316                                                        emf-table)
1317                                              (slow-method-lookup-1
1318                                               gf arg specialization))))
1319                              (if emfun
1320                                  (funcall emfun (list arg))
1321                                  (apply #'no-applicable-method gf (list arg))))))))
1322                    ((= number-required 2)
1323                     #'(lambda (arg1 arg2)
1324                         (declare (optimize speed))
1325                         (let* ((args (list arg1 arg2))
1326                                (emfun (get-cached-emf gf args)))
1327                           (if emfun
1328                               (funcall emfun args)
1329                               (slow-method-lookup gf args)))))
1330                    ((= number-required 3)
1331                     #'(lambda (arg1 arg2 arg3)
1332                         (declare (optimize speed))
1333                         (let* ((args (list arg1 arg2 arg3))
1334                                (emfun (get-cached-emf gf args)))
1335                           (if emfun
1336                               (funcall emfun args)
1337                               (slow-method-lookup gf args)))))
1338                    (t
1339                     #'(lambda (&rest args)
1340                         (declare (optimize speed))
1341                         (let ((len (length args)))
1342                           (unless (= len number-required)
1343                             (error 'program-error
1344                                    :format-control "Not enough arguments for generic function ~S."
1345                                    :format-arguments (list (%generic-function-name gf)))))
1346                         (let ((emfun (get-cached-emf gf args)))
1347                           (if emfun
1348                               (funcall emfun args)
1349                               (slow-method-lookup gf args))))))
1350                  #'(lambda (&rest args)
1351                      (declare (optimize speed))
1352                      (let ((len (length args)))
1353                        (unless (>= len number-required)
1354                          (error 'program-error
1355                                 :format-control "Not enough arguments for generic function ~S."
1356                                 :format-arguments (list (%generic-function-name gf)))))
1357                      (let ((emfun (get-cached-emf gf args)))
1358                        (if emfun
1359                            (funcall emfun args)
1360                            (slow-method-lookup gf args))))))))))
1361
1362    code))
1363
1364(defun sort-methods (methods gf required-classes)
1365  (if (or (null methods) (null (%cdr methods)))
1366      methods
1367      (sort methods
1368      (if (eq (class-of gf) (find-class 'standard-generic-function))
1369    #'(lambda (m1 m2)
1370        (std-method-more-specific-p m1 m2 required-classes
1371            (generic-function-argument-precedence-order gf)))
1372    #'(lambda (m1 m2)
1373        (method-more-specific-p gf m1 m2 required-classes))))))
1374
1375(defun method-applicable-p (method args)
1376  (do* ((specializers (%method-specializers method) (cdr specializers))
1377        (args args (cdr args)))
1378       ((null specializers) t)
1379    (let ((specializer (car specializers)))
1380      (if (typep specializer 'eql-specializer)
1381          (unless (eql (car args) (eql-specializer-object specializer))
1382            (return nil))
1383          (unless (subclassp (class-of (car args)) specializer)
1384            (return nil))))))
1385
1386(defun %compute-applicable-methods (gf args)
1387  (let ((required-classes (mapcar #'class-of (required-portion gf args)))
1388        (methods '()))
1389    (dolist (method (generic-function-methods gf))
1390      (when (method-applicable-p method args)
1391        (push method methods)))
1392    (sort-methods methods gf required-classes)))
1393
1394;;; METHOD-APPLICABLE-USING-CLASSES-P
1395;;;
1396;;; If the first return value is T, METHOD is definitely applicable to
1397;;; arguments that are instances of CLASSES.  If the first value is
1398;;; NIL and the second value is T, METHOD is definitely not applicable
1399;;; to arguments that are instances of CLASSES; if the second value is
1400;;; NIL the applicability of METHOD cannot be determined by inspecting
1401;;; the classes of its arguments only.
1402;;;
1403(defun method-applicable-using-classes-p (method classes)
1404  (do* ((specializers (%method-specializers method) (cdr specializers))
1405  (classes classes (cdr classes))
1406  (knownp t))
1407       ((null specializers)
1408  (if knownp (values t t) (values nil nil)))
1409    (let ((specializer (car specializers)))
1410      (if (typep specializer 'eql-specializer)
1411    (if (eql (class-of (eql-specializer-object specializer)) 
1412       (car classes))
1413        (setf knownp nil)
1414        (return (values nil t)))
1415    (unless (subclassp (car classes) specializer)
1416      (return (values nil t)))))))
1417
1418(defun slow-method-lookup (gf args)
1419  (let ((applicable-methods (%compute-applicable-methods gf args)))
1420    (if applicable-methods
1421        (let ((emfun (funcall (if (eq (class-of gf) (find-class 'standard-generic-function))
1422                                  #'std-compute-effective-method-function
1423                                  #'compute-effective-method-function)
1424                              gf applicable-methods)))
1425          (cache-emf gf args emfun)
1426          (funcall emfun args))
1427        (apply #'no-applicable-method gf args))))
1428
1429(defun slow-method-lookup-1 (gf arg arg-specialization)
1430  (let ((applicable-methods (%compute-applicable-methods gf (list arg))))
1431    (if applicable-methods
1432        (let ((emfun (funcall (if (eq (class-of gf) (find-class 'standard-generic-function))
1433                                  #'std-compute-effective-method-function
1434                                  #'compute-effective-method-function)
1435                              gf applicable-methods)))
1436          (when emfun
1437            (setf (gethash arg-specialization (classes-to-emf-table gf)) emfun))
1438          emfun))))
1439
1440(defun sub-specializer-p (c1 c2 c-arg)
1441  (find c2 (cdr (memq c1 (%class-precedence-list c-arg)))))
1442
1443(defun std-method-more-specific-p (method1 method2 required-classes argument-precedence-order)
1444  (if argument-precedence-order
1445      (let ((specializers-1 (%method-specializers method1))
1446            (specializers-2 (%method-specializers method2)))
1447        (dolist (index argument-precedence-order)
1448          (let ((spec1 (nth index specializers-1))
1449                (spec2 (nth index specializers-2)))
1450            (unless (eq spec1 spec2)
1451              (cond ((eql-specializer-p spec1)
1452                     (return t))
1453                    ((eql-specializer-p spec2)
1454                     (return nil))
1455                    (t
1456                     (return (sub-specializer-p spec1 spec2
1457                                                (nth index required-classes)))))))))
1458      (do ((specializers-1 (%method-specializers method1) (cdr specializers-1))
1459           (specializers-2 (%method-specializers method2) (cdr specializers-2))
1460           (classes required-classes (cdr classes)))
1461          ((null specializers-1) nil)
1462        (let ((spec1 (car specializers-1))
1463              (spec2 (car specializers-2)))
1464          (unless (eq spec1 spec2)
1465            (cond ((eql-specializer-p spec1)
1466                   (return t))
1467                  ((eql-specializer-p spec2)
1468                   (return nil))
1469                  (t
1470                   (return (sub-specializer-p spec1 spec2 (car classes))))))))))
1471
1472(defun primary-method-p (method)
1473  (null (intersection '(:before :after :around) (method-qualifiers method))))
1474
1475(defun before-method-p (method)
1476  (equal '(:before) (method-qualifiers method)))
1477
1478(defun after-method-p (method)
1479  (equal '(:after) (method-qualifiers method)))
1480
1481(defun around-method-p (method)
1482  (equal '(:around) (method-qualifiers method)))
1483
1484(defun std-compute-effective-method-function (gf methods)
1485  (let* ((mc (generic-function-method-combination gf))
1486         (mc-name (if (atom mc) mc (%car mc)))
1487         (options (if (atom mc) '() (%cdr mc)))
1488         (order (car options))
1489         (primaries '())
1490         (arounds '())
1491         around
1492         emf-form)
1493    (dolist (m methods)
1494      (let ((qualifiers (method-qualifiers m)))
1495        (cond ((null qualifiers)
1496               (if (eq mc-name 'standard)
1497                   (push m primaries)
1498                   (error "Method combination type mismatch.")))
1499              ((cdr qualifiers)
1500               (error "Invalid method qualifiers."))
1501              ((eq (car qualifiers) :around)
1502               (push m arounds))
1503              ((eq (car qualifiers) mc-name)
1504               (push m primaries))
1505              ((memq (car qualifiers) '(:before :after)))
1506              (t
1507               (error "Invalid method qualifiers.")))))
1508    (unless (eq order :most-specific-last)
1509      (setf primaries (nreverse primaries)))
1510    (setf arounds (nreverse arounds))
1511    (setf around (car arounds))
1512    (when (null primaries)
1513      (error "No primary methods for the generic function ~S." gf))
1514    (cond
1515      (around
1516       (let ((next-emfun
1517              (funcall
1518               (if (eq (class-of gf) (find-class 'standard-generic-function))
1519                   #'std-compute-effective-method-function
1520                   #'compute-effective-method-function)
1521               gf (remove around methods))))
1522         (setf emf-form
1523;;;           `(lambda (args)
1524;;;          (funcall ,(%method-function around) args ,next-emfun))
1525               (generate-emf-lambda (%method-function around) next-emfun)
1526               )))
1527      ((eq mc-name 'standard)
1528       (let* ((next-emfun (compute-primary-emfun (cdr primaries)))
1529              (befores (remove-if-not #'before-method-p methods))
1530              (reverse-afters
1531               (reverse (remove-if-not #'after-method-p methods))))
1532         (setf emf-form
1533               (cond
1534                 ((and (null befores) (null reverse-afters))
1535                  (let ((fast-function (%method-fast-function (car primaries))))
1536
1537                    (if fast-function
1538                        (ecase (length (gf-required-args gf))
1539                          (1
1540                           #'(lambda (args)
1541                               (declare (optimize speed))
1542                               (funcall fast-function (car args))))
1543                          (2
1544                           #'(lambda (args)
1545                               (declare (optimize speed))
1546                               (funcall fast-function (car args) (cadr args)))))
1547                        ;;                               `(lambda (args)
1548                        ;;                                  (declare (optimize speed))
1549                        ;;                                  (funcall ,(%method-function (car primaries)) args ,next-emfun))
1550                        (generate-emf-lambda (%method-function (car primaries))
1551                                             next-emfun))))
1552                 (t
1553                  (let ((method-function (%method-function (car primaries))))
1554
1555                    #'(lambda (args)
1556                        (declare (optimize speed))
1557                        (dolist (before befores)
1558                          (funcall (%method-function before) args nil))
1559                        (multiple-value-prog1
1560                            (funcall method-function args next-emfun)
1561                          (dolist (after reverse-afters)
1562                            (funcall (%method-function after) args nil))))))))))
1563          (t
1564           (let ((mc-obj (get mc-name 'method-combination-object)))
1565             (unless mc-obj
1566               (error "Unsupported method combination type ~A." mc-name))
1567             (let* ((operator (method-combination-operator mc-obj))
1568                    (ioa (method-combination-identity-with-one-argument mc-obj)))
1569               (setf emf-form
1570                     (if (and (null (cdr primaries))
1571                              (not (null ioa)))
1572;;                          `(lambda (args)
1573;;                             (funcall ,(%method-function (car primaries)) args nil))
1574                         (generate-emf-lambda (%method-function (car primaries)) nil)
1575                         `(lambda (args)
1576                            (,operator ,@(mapcar
1577                                          (lambda (primary)
1578                                            `(funcall ,(%method-function primary) args nil))
1579                                          primaries)))))))))
1580    (or (ignore-errors (autocompile emf-form))
1581        (coerce-to-function emf-form))))
1582
1583(defun generate-emf-lambda (method-function next-emfun)
1584  #'(lambda (args)
1585      (declare (optimize speed))
1586      (funcall method-function args next-emfun)))
1587
1588;;; compute an effective method function from a list of primary methods:
1589
1590(defun compute-primary-emfun (methods)
1591  (if (null methods)
1592      nil
1593      (let ((next-emfun (compute-primary-emfun (cdr methods))))
1594        #'(lambda (args)
1595           (funcall (%method-function (car methods)) args next-emfun)))))
1596
1597(defvar *call-next-method-p*)
1598(defvar *next-method-p-p*)
1599
1600(defun walk-form (form)
1601  (cond ((atom form)
1602         (cond ((eq form 'call-next-method)
1603                (setf *call-next-method-p* t))
1604               ((eq form 'next-method-p)
1605                (setf *next-method-p-p* t))))
1606        (t
1607         (walk-form (%car form))
1608         (walk-form (%cdr form)))))
1609
1610(defun compute-method-function (lambda-expression)
1611  (let ((lambda-list (allow-other-keys (cadr lambda-expression)))
1612        (body (cddr lambda-expression))
1613        (*call-next-method-p* nil)
1614        (*next-method-p-p* nil))
1615    (multiple-value-bind (body declarations) (parse-body body)
1616      (let ((ignorable-vars '()))
1617        (dolist (var lambda-list)
1618          (if (memq var lambda-list-keywords)
1619              (return)
1620              (push var ignorable-vars)))
1621        (push `(declare (ignorable ,@ignorable-vars)) declarations))
1622      (walk-form body)
1623      (cond ((or *call-next-method-p* *next-method-p-p*)
1624             `(lambda (args next-emfun)
1625                (flet ((call-next-method (&rest cnm-args)
1626                         (if (null next-emfun)
1627                             (error "No next method for generic function.")
1628                             (funcall next-emfun (or cnm-args args))))
1629                       (next-method-p ()
1630                         (not (null next-emfun))))
1631                  (declare (ignorable (function call-next-method)
1632                                      (function next-method-p)))
1633                  (apply #'(lambda ,lambda-list ,@declarations ,@body) args))))
1634            ((null (intersection lambda-list '(&rest &optional &key &allow-other-keys &aux)))
1635             ;; Required parameters only.
1636             (case (length lambda-list)
1637               (1
1638                `(lambda (args next-emfun)
1639                   (declare (ignore next-emfun))
1640                   (let ((,(%car lambda-list) (%car args)))
1641                     (declare (ignorable ,(%car lambda-list)))
1642                     ,@declarations ,@body)))
1643               (2
1644                `(lambda (args next-emfun)
1645                   (declare (ignore next-emfun))
1646                   (let ((,(%car lambda-list) (%car args))
1647                         (,(%cadr lambda-list) (%cadr args)))
1648                     (declare (ignorable ,(%car lambda-list)
1649                                         ,(%cadr lambda-list)))
1650                     ,@declarations ,@body)))
1651               (3
1652                `(lambda (args next-emfun)
1653                   (declare (ignore next-emfun))
1654                   (let ((,(%car lambda-list) (%car args))
1655                         (,(%cadr lambda-list) (%cadr args))
1656                         (,(%caddr lambda-list) (%caddr args)))
1657                     (declare (ignorable ,(%car lambda-list)
1658                                         ,(%cadr lambda-list)
1659                                         ,(%caddr lambda-list)))
1660                     ,@declarations ,@body)))
1661               (t
1662                `(lambda (args next-emfun)
1663                   (declare (ignore next-emfun))
1664                   (apply #'(lambda ,lambda-list ,@declarations ,@body) args)))))
1665            (t
1666             `(lambda (args next-emfun)
1667                (declare (ignore next-emfun))
1668                (apply #'(lambda ,lambda-list ,@declarations ,@body) args)))))))
1669
1670(defun compute-method-fast-function (lambda-expression)
1671  (let ((lambda-list (allow-other-keys (cadr lambda-expression))))
1672    (when (intersection lambda-list '(&rest &optional &key &allow-other-keys &aux))
1673      (return-from compute-method-fast-function nil))
1674    ;; Only required args.
1675    (let ((body (cddr lambda-expression))
1676          (*call-next-method-p* nil)
1677          (*next-method-p-p* nil))
1678      (multiple-value-bind (body declarations) (parse-body body)
1679        (walk-form body)
1680        (when (or *call-next-method-p* *next-method-p-p*)
1681          (return-from compute-method-fast-function nil))
1682        (let ((decls `(declare (ignorable ,@lambda-list))))
1683          (setf lambda-expression
1684                (list* (car lambda-expression)
1685                       (cadr lambda-expression)
1686                       decls
1687                       (cddr lambda-expression))))
1688        (case (length lambda-list)
1689          (1
1690;;            `(lambda (args next-emfun)
1691;;               (let ((,(%car lambda-list) (%car args)))
1692;;                 (declare (ignorable ,(%car lambda-list)))
1693;;                 ,@declarations ,@body)))
1694           lambda-expression)
1695          (2
1696;;            `(lambda (args next-emfun)
1697;;               (let ((,(%car lambda-list) (%car args))
1698;;                     (,(%cadr lambda-list) (%cadr args)))
1699;;                 (declare (ignorable ,(%car lambda-list)
1700;;                                     ,(%cadr lambda-list)))
1701;;                 ,@declarations ,@body)))
1702           lambda-expression)
1703;;           (3
1704;;            `(lambda (args next-emfun)
1705;;               (let ((,(%car lambda-list) (%car args))
1706;;                     (,(%cadr lambda-list) (%cadr args))
1707;;                     (,(%caddr lambda-list) (%caddr args)))
1708;;                 (declare (ignorable ,(%car lambda-list)
1709;;                                     ,(%cadr lambda-list)
1710;;                                     ,(%caddr lambda-list)))
1711;;                 ,@declarations ,@body)))
1712          (t
1713           nil))))))
1714
1715;; From CLHS section 7.6.5:
1716;; "When a generic function or any of its methods mentions &key in a lambda
1717;; list, the specific set of keyword arguments accepted by the generic function
1718;; varies according to the applicable methods. The set of keyword arguments
1719;; accepted by the generic function for a particular call is the union of the
1720;; keyword arguments accepted by all applicable methods and the keyword
1721;; arguments mentioned after &key in the generic function definition, if any."
1722;; Adapted from Sacla.
1723(defun allow-other-keys (lambda-list)
1724  (if (and (member '&key lambda-list)
1725           (not (member '&allow-other-keys lambda-list)))
1726      (let* ((key-end (or (position '&aux lambda-list) (length lambda-list)))
1727             (aux-part (subseq lambda-list key-end)))
1728        `(,@(subseq lambda-list 0 key-end) &allow-other-keys ,@aux-part))
1729      lambda-list))
1730
1731(defmacro defmethod (&rest args)
1732  (multiple-value-bind
1733      (function-name qualifiers lambda-list specializers documentation declarations body)
1734      (parse-defmethod args)
1735    (let* ((specializers-form '())
1736           (lambda-expression `(lambda ,lambda-list ,@declarations ,body))
1737           (method-function (compute-method-function lambda-expression))
1738           (fast-function (compute-method-fast-function lambda-expression))
1739           )
1740      (dolist (specializer specializers)
1741        (cond ((and (consp specializer) (eq (car specializer) 'eql))
1742               (push `(list 'eql ,(cadr specializer)) specializers-form))
1743              (t
1744               (push `',specializer specializers-form))))
1745      (setf specializers-form `(list ,@(nreverse specializers-form)))
1746      `(progn
1747         (ensure-method ',function-name
1748                        :lambda-list ',lambda-list
1749                        :qualifiers ',qualifiers
1750                        :specializers ,specializers-form
1751                        ,@(if documentation `(:documentation ,documentation))
1752                        :function (function ,method-function)
1753                        ,@(if fast-function `(:fast-function (function ,fast-function)))
1754                        )))))
1755
1756;;; Reader and writer methods
1757
1758(defun make-instance-standard-reader-method (gf
1759                                             &key
1760                                             lambda-list
1761                                             qualifiers
1762                                             specializers
1763                                             documentation
1764                                             function
1765                                             fast-function
1766                                             slot-name)
1767  (declare (ignore gf))
1768  (let ((method (std-allocate-instance (find-class 'standard-reader-method))))
1769    (setf (method-lambda-list method) lambda-list)
1770    (setf (method-qualifiers method) qualifiers)
1771    (%set-method-specializers method (canonicalize-specializers specializers))
1772    (setf (method-documentation method) documentation)
1773    (%set-method-generic-function method nil)
1774    (%set-method-function method function)
1775    (%set-method-fast-function method fast-function)
1776    (set-reader-method-slot-name method slot-name)
1777    method))
1778
1779(defun add-reader-method (class function-name slot-name)
1780  (let* ((lambda-expression
1781          (if (eq (class-of class) (find-class 'standard-class))
1782              `(lambda (object) (std-slot-value object ',slot-name))
1783              `(lambda (object) (slot-value object ',slot-name))))
1784         (method-function (compute-method-function lambda-expression))
1785         (fast-function (compute-method-fast-function lambda-expression)))
1786    (let ((method-lambda-list '(object))
1787          (gf (find-generic-function function-name nil)))
1788      (if gf
1789          (check-method-lambda-list method-lambda-list (generic-function-lambda-list gf))
1790          (setf gf (ensure-generic-function function-name :lambda-list method-lambda-list)))
1791      (let ((method
1792             (make-instance-standard-reader-method gf
1793                                                   :lambda-list '(object)
1794                                                   :qualifiers ()
1795                                                   :specializers (list class)
1796                                                   :function (if (autoloadp 'compile)
1797                                                                 method-function
1798                                                                 (autocompile method-function))
1799                                                   :fast-function (if (autoloadp 'compile)
1800                                                                      fast-function
1801                                                                      (autocompile fast-function))
1802                                                   :slot-name slot-name)))
1803        (%add-method gf method)
1804        method))))
1805
1806(defun add-writer-method (class function-name slot-name)
1807  (let* ((lambda-expression
1808          (if (eq (class-of class) (find-class 'standard-class))
1809              `(lambda (new-value object)
1810                 (setf (std-slot-value object ',slot-name) new-value))
1811              `(lambda (new-value object)
1812                 (setf (slot-value object ',slot-name) new-value))))
1813         (method-function (compute-method-function lambda-expression))
1814         (fast-function (compute-method-fast-function lambda-expression))
1815         )
1816    (ensure-method function-name
1817                   :lambda-list '(new-value object)
1818                   :qualifiers ()
1819                   :specializers (list (find-class 't) class)
1820;;                    :function `(function ,method-function)
1821                   :function (if (autoloadp 'compile)
1822                                 method-function
1823                                 (autocompile method-function))
1824                   :fast-function (if (autoloadp 'compile)
1825                                      fast-function
1826                                      (autocompile fast-function))
1827                   )))
1828
1829(defmacro redefine-class-forwarder (name slot &optional alternative-name)
1830  (let* (($name (if (consp name) (cadr name) name))
1831         (%name (intern (concatenate 'string
1832                                     "%"
1833                                     (if (consp name)
1834                                         (symbol-name 'set-) "")
1835                                     (symbol-name $name))
1836                        (find-package "SYS"))))
1837    (unless alternative-name
1838      (setf alternative-name name))
1839    (if (consp name)
1840        `(progn ;; setter
1841           (defgeneric ,alternative-name (new-value class))
1842           (defmethod ,alternative-name (new-value (class built-in-class))
1843             (,%name new-value class))
1844           (defmethod ,alternative-name (new-value (class forward-referenced-class))
1845             (,%name new-value class))
1846           (defmethod ,alternative-name (new-value (class structure-class))
1847             (,%name new-value class))
1848           (defmethod ,alternative-name (new-value (class standard-class))
1849             (setf (slot-value class ',slot) new-value))
1850           ,@(unless (eq name alternative-name)
1851                     `((setf (get ',$name 'SETF-FUNCTION)
1852                             (symbol-function ',alternative-name))))
1853           )
1854        `(progn ;; getter
1855           (defgeneric ,alternative-name (class))
1856           (defmethod ,alternative-name ((class built-in-class))
1857             (,%name class))
1858           (defmethod ,alternative-name ((class forward-referenced-class))
1859             (,%name class))
1860           (defmethod ,alternative-name ((class structure-class))
1861             (,%name class))
1862           (defmethod ,alternative-name ((class standard-class))
1863             (slot-value class ',slot))
1864           ,@(unless (eq name alternative-name)
1865                     `((setf (symbol-function ',$name)
1866                             (symbol-function ',alternative-name))))
1867           ) )))
1868
1869(redefine-class-forwarder class-name name)
1870(redefine-class-forwarder (setf class-name) name)
1871(redefine-class-forwarder class-slots slots)
1872(redefine-class-forwarder (setf class-slots) slots)
1873(redefine-class-forwarder class-direct-slots direct-slots)
1874(redefine-class-forwarder (setf class-direct-slots) direct-slots)
1875(redefine-class-forwarder class-layout layout)
1876(redefine-class-forwarder (setf class-layout) layout)
1877(redefine-class-forwarder class-direct-superclasses direct-superclasses)
1878(redefine-class-forwarder (setf class-direct-superclasses) direct-superclasses)
1879(redefine-class-forwarder class-direct-subclasses direct-subclasses)
1880(redefine-class-forwarder (setf class-direct-subclasses) direct-subclasses)
1881(redefine-class-forwarder class-direct-methods direct-methods !class-direct-methods)
1882(redefine-class-forwarder (setf class-direct-methods) direct-methods !!class-direct-methods)
1883(redefine-class-forwarder class-precedence-list precedence-list)
1884(redefine-class-forwarder (setf class-precedence-list) precedence-list)
1885(redefine-class-forwarder class-finalized-p finalized-p)
1886(redefine-class-forwarder (setf class-finalized-p) finalized-p)
1887(redefine-class-forwarder class-default-initargs default-initargs)
1888(redefine-class-forwarder (setf class-default-initargs) default-initargs)
1889(redefine-class-forwarder class-direct-default-initargs direct-default-initargs)
1890(redefine-class-forwarder (setf class-direct-default-initargs) direct-default-initargs)
1891
1892
1893
1894(fmakunbound 'documentation)
1895(defgeneric documentation (x doc-type))
1896
1897(defgeneric (setf documentation) (new-value x doc-type))
1898
1899(defmethod documentation ((x symbol) doc-type)
1900  (%documentation x doc-type))
1901
1902(defmethod (setf documentation) (new-value (x symbol) doc-type)
1903  (%set-documentation x doc-type new-value))
1904
1905(defmethod documentation ((x function) doc-type)
1906  (%documentation x doc-type))
1907
1908(defmethod (setf documentation) (new-value (x function) doc-type)
1909  (%set-documentation x doc-type new-value))
1910
1911;; FIXME This should be a weak hashtable!
1912(defvar *list-documentation-hashtable* (make-hash-table :test #'equal))
1913
1914(defmethod documentation ((x list) (doc-type (eql 'function)))
1915  (let ((alist (gethash x *list-documentation-hashtable*)))
1916    (and alist (cdr (assoc doc-type alist)))))
1917
1918(defmethod documentation ((x list) (doc-type (eql 'compiler-macro)))
1919  (let ((alist (gethash x *list-documentation-hashtable*)))
1920    (and alist (cdr (assoc doc-type alist)))))
1921
1922(defmethod (setf documentation) (new-value (x list) (doc-type (eql 'function)))
1923  (let* ((alist (gethash x *list-documentation-hashtable*))
1924         (entry (and alist (assoc doc-type alist))))
1925    (cond (entry
1926           (setf (cdr entry) new-value))
1927          (t
1928           (setf (gethash x *list-documentation-hashtable*)
1929                 (push (cons doc-type new-value) alist)))))
1930  new-value)
1931
1932(defmethod (setf documentation) (new-value (x list) (doc-type (eql 'compiler-macro)))
1933  (let* ((alist (gethash x *list-documentation-hashtable*))
1934         (entry (and alist (assoc doc-type alist))))
1935    (cond (entry
1936           (setf (cdr entry) new-value))
1937          (t
1938           (setf (gethash x *list-documentation-hashtable*)
1939                 (push (cons doc-type new-value) alist)))))
1940  new-value)
1941
1942(defmethod documentation ((x standard-class) (doc-type (eql 't)))
1943  (class-documentation x))
1944
1945(defmethod documentation ((x standard-class) (doc-type (eql 'type)))
1946  (class-documentation x))
1947
1948(defmethod (setf documentation) (new-value (x standard-class) (doc-type (eql 't)))
1949  (%set-class-documentation x new-value))
1950
1951(defmethod (setf documentation) (new-value (x standard-class) (doc-type (eql 'type)))
1952  (%set-class-documentation x new-value))
1953
1954(defmethod documentation ((x structure-class) (doc-type (eql 't)))
1955  (%documentation x doc-type))
1956
1957(defmethod documentation ((x structure-class) (doc-type (eql 'type)))
1958  (%documentation x doc-type))
1959
1960(defmethod (setf documentation) (new-value (x structure-class) (doc-type (eql 't)))
1961  (%set-documentation x doc-type new-value))
1962
1963(defmethod (setf documentation) (new-value (x structure-class) (doc-type (eql 'type)))
1964  (%set-documentation x doc-type new-value))
1965
1966(defmethod documentation ((x standard-generic-function) (doc-type (eql 't)))
1967  (generic-function-documentation x))
1968
1969(defmethod (setf documentation) (new-value (x standard-generic-function) (doc-type (eql 't)))
1970  (setf (generic-function-documentation x) new-value))
1971
1972(defmethod documentation ((x standard-generic-function) (doc-type (eql 'function)))
1973  (generic-function-documentation x))
1974
1975(defmethod (setf documentation) (new-value (x standard-generic-function) (doc-type (eql 'function)))
1976  (setf (generic-function-documentation x) new-value))
1977
1978(defmethod documentation ((x standard-method) (doc-type (eql 't)))
1979  (method-documentation x))
1980
1981(defmethod (setf documentation) (new-value (x standard-method) (doc-type (eql 't)))
1982  (setf (method-documentation x) new-value))
1983
1984(defmethod documentation ((x package) (doc-type (eql 't)))
1985  (%documentation x doc-type))
1986
1987(defmethod (setf documentation) (new-value (x package) (doc-type (eql 't)))
1988  (%set-documentation x doc-type new-value))
1989
1990;;; Applicable methods
1991
1992(defgeneric compute-applicable-methods (gf args)
1993  (:method ((gf standard-generic-function) args)
1994    (%compute-applicable-methods gf args)))
1995
1996(defgeneric compute-applicable-methods-using-classes (gf classes)
1997  (:method ((gf standard-generic-function) classes)
1998    (let ((methods '()))
1999      (dolist (method (generic-function-methods gf))
2000  (multiple-value-bind (applicable knownp)
2001      (method-applicable-using-classes-p method classes)
2002    (cond (applicable
2003     (push method methods))
2004    ((not knownp)
2005     (return-from compute-applicable-methods-using-classes
2006       (values nil nil))))))
2007      (values (sort-methods methods gf classes)
2008        t))))
2009
2010(export '(compute-applicable-methods
2011    compute-applicable-methods-using-classes))
2012
2013
2014;;; Slot access
2015
2016(defun set-slot-value-using-class (new-value class instance slot-name)
2017  (declare (ignore class)) ; FIXME
2018  (setf (std-slot-value instance slot-name) new-value))
2019
2020(defgeneric slot-value-using-class (class instance slot-name))
2021
2022(defmethod slot-value-using-class ((class standard-class) instance slot-name)
2023  (std-slot-value instance slot-name))
2024
2025(defgeneric (setf slot-value-using-class) (new-value class instance slot-name))
2026(defmethod (setf slot-value-using-class) (new-value
2027                                          (class standard-class)
2028                                          instance
2029                                          slot-name)
2030  (setf (std-slot-value instance slot-name) new-value))
2031
2032(defgeneric slot-exists-p-using-class (class instance slot-name))
2033
2034(defmethod slot-exists-p-using-class (class instance slot-name)
2035  nil)
2036
2037(defmethod slot-exists-p-using-class ((class standard-class) instance slot-name)
2038  (std-slot-exists-p instance slot-name))
2039
2040(defmethod slot-exists-p-using-class ((class structure-class) instance slot-name)
2041  (dolist (dsd (class-slots class))
2042    (when (eq (sys::dsd-name dsd) slot-name)
2043      (return-from slot-exists-p-using-class t)))
2044  nil)
2045
2046(defgeneric slot-boundp-using-class (class instance slot-name))
2047(defmethod slot-boundp-using-class ((class standard-class) instance slot-name)
2048  (std-slot-boundp instance slot-name))
2049
2050(defgeneric slot-makunbound-using-class (class instance slot-name))
2051(defmethod slot-makunbound-using-class ((class standard-class)
2052                                        instance
2053                                        slot-name)
2054  (std-slot-makunbound instance slot-name))
2055
2056(defgeneric slot-missing (class instance slot-name operation &optional new-value))
2057
2058(defmethod slot-missing ((class t) instance slot-name operation &optional new-value)
2059  (declare (ignore new-value))
2060  (error "The slot ~S is missing from the class ~S." slot-name class))
2061
2062(defgeneric slot-unbound (class instance slot-name))
2063
2064(defmethod slot-unbound ((class t) instance slot-name)
2065  (error 'unbound-slot :instance instance :name slot-name))
2066
2067;;; Instance creation and initialization
2068
2069(defgeneric allocate-instance (class &rest initargs &key &allow-other-keys))
2070
2071(defmethod allocate-instance ((class standard-class) &rest initargs)
2072  (declare (ignore initargs))
2073  (std-allocate-instance class))
2074
2075(defmethod allocate-instance ((class structure-class) &rest initargs)
2076  (declare (ignore initargs))
2077  (%make-structure (class-name class)
2078                   (make-list (length (class-slots class))
2079                              :initial-element +slot-unbound+)))
2080
2081;; "The set of valid initialization arguments for a class is the set of valid
2082;; initialization arguments that either fill slots or supply arguments to
2083;; methods, along with the predefined initialization argument :ALLOW-OTHER-KEYS."
2084;; 7.1.2
2085
2086(defun check-initargs (instance shared-initialize-param initargs)
2087  (when (oddp (length initargs))
2088    (error 'program-error
2089           :format-control "Odd number of keyword arguments."))
2090  (unless (getf initargs :allow-other-keys)
2091    (let ((methods 
2092     (nconc 
2093      (compute-applicable-methods 
2094       #'shared-initialize
2095       (if initargs
2096     `(,instance ,shared-initialize-param ,@initargs)
2097         (list instance shared-initialize-param)))
2098      (compute-applicable-methods 
2099       #'initialize-instance
2100       (if initargs
2101     `(,instance ,@initargs)
2102         (list instance)))))
2103    (slots (class-slots (class-of instance))))
2104      (do* ((tail initargs (cddr tail))
2105            (initarg (car tail) (car tail)))
2106           ((null tail))
2107        (unless (or (valid-initarg-p initarg slots)
2108        (valid-methodarg-p initarg methods)
2109                    (eq initarg :allow-other-keys))
2110          (error 'program-error
2111                 :format-control "Invalid initarg ~S."
2112                 :format-arguments (list initarg)))))))
2113
2114(defun valid-methodarg-p (initarg methods)
2115  (when (symbolp initarg)
2116    (dolist (method methods nil)
2117      (let ((valid-initargs (method-lambda-list method)))
2118  (when (find (symbol-value initarg) valid-initargs 
2119         :test #'(lambda (a b)
2120             (if (listp b)
2121           (string= a (car b))
2122         (or
2123          (string= a b)
2124          (string= b "&ALLOW-OTHER-KEYS")))))
2125
2126    (return t))))))
2127
2128(defun valid-initarg-p (initarg slots)
2129  (dolist (slot slots nil)
2130    (let ((valid-initargs (%slot-definition-initargs slot)))
2131      (when (memq initarg valid-initargs)
2132        (return t)))))
2133
2134(defgeneric make-instance (class &rest initargs &key &allow-other-keys))
2135
2136(defmethod make-instance ((class standard-class) &rest initargs)
2137  (when (oddp (length initargs))
2138    (error 'program-error :format-control "Odd number of keyword arguments."))
2139  (unless (class-finalized-p class)
2140    (std-finalize-inheritance class))
2141  (let ((class-default-initargs (class-default-initargs class)))
2142    (when class-default-initargs
2143      (let ((default-initargs '()))
2144        (do* ((list class-default-initargs (cddr list))
2145              (key (car list) (car list))
2146              (fn (cadr list) (cadr list)))
2147             ((null list))
2148          (when (eq (getf initargs key 'not-found) 'not-found)
2149            (setf default-initargs (append default-initargs (list key (funcall fn))))))
2150        (setf initargs (append initargs default-initargs)))))
2151
2152  (let ((instance (std-allocate-instance class)))
2153    (check-initargs instance t initargs)
2154    (apply #'initialize-instance instance initargs)
2155    instance))
2156
2157(defmethod make-instance ((class symbol) &rest initargs)
2158  (apply #'make-instance (find-class class) initargs))
2159
2160(defgeneric initialize-instance (instance &key))
2161
2162(defmethod initialize-instance ((instance standard-object) &rest initargs)
2163  (apply #'shared-initialize instance t initargs))
2164
2165(defgeneric reinitialize-instance (instance &key))
2166
2167;; "The system-supplied primary method for REINITIALIZE-INSTANCE checks the
2168;; validity of initargs and signals an error if an initarg is supplied that is
2169;; not declared as valid. The method then calls the generic function SHARED-
2170;; INITIALIZE with the following arguments: the instance, nil (which means no
2171;; slots should be initialized according to their initforms), and the initargs
2172;; it received."
2173(defmethod reinitialize-instance ((instance standard-object) &rest initargs)
2174  (apply #'shared-initialize instance () initargs))
2175
2176(defun std-shared-initialize (instance slot-names all-keys)
2177  (when (oddp (length all-keys))
2178    (error 'program-error :format-control "Odd number of keyword arguments."))
2179  (do* ((tail all-keys (cddr tail))
2180  (initarg (car tail) (car tail)))
2181      ((null tail))
2182    (when (and initarg (not (symbolp initarg)))
2183      (error 'program-error
2184       :format-control "Invalid initarg ~S."
2185       :format-arguments (list initarg))))
2186  (dolist (slot (class-slots (class-of instance)))
2187    (let ((slot-name (%slot-definition-name slot)))
2188      (multiple-value-bind (init-key init-value foundp)
2189          (get-properties all-keys (%slot-definition-initargs slot))
2190        (if foundp
2191            (setf (std-slot-value instance slot-name) init-value)
2192            (unless (std-slot-boundp instance slot-name)
2193              (let ((initfunction (%slot-definition-initfunction slot)))
2194                (when (and initfunction (or (eq slot-names t)
2195                                            (memq slot-name slot-names)))
2196                  (setf (std-slot-value instance slot-name)
2197                        (funcall initfunction)))))))))
2198  instance)
2199
2200(defgeneric shared-initialize (instance slot-names &key))
2201
2202(defmethod shared-initialize ((instance standard-object) slot-names &rest initargs)
2203  (std-shared-initialize instance slot-names initargs))
2204
2205;;; change-class
2206
2207(defgeneric change-class (instance new-class &key))
2208
2209(defmethod change-class ((old-instance standard-object) (new-class standard-class)
2210                         &rest initargs)
2211  (let ((old-slots (class-slots (class-of old-instance)))
2212        (new-slots (class-slots new-class))
2213        (new-instance (allocate-instance new-class)))
2214    ;; "The values of local slots specified by both the class CTO and the class
2215    ;; CFROM are retained. If such a local slot was unbound, it remains
2216    ;; unbound."
2217    (dolist (new-slot new-slots)
2218      (when (instance-slot-p new-slot)
2219        (let* ((slot-name (%slot-definition-name new-slot))
2220               (old-slot (find slot-name old-slots :key #'%slot-definition-name)))
2221          ;; "The values of slots specified as shared in the class CFROM and as
2222          ;; local in the class CTO are retained."
2223          (when (and old-slot (slot-boundp old-instance slot-name))
2224            (setf (slot-value new-instance slot-name)
2225                  (slot-value old-instance slot-name))))))
2226    (swap-slots old-instance new-instance)
2227    (rotatef (std-instance-layout new-instance)
2228             (std-instance-layout old-instance))
2229    (apply #'update-instance-for-different-class
2230           new-instance old-instance initargs)
2231    old-instance))
2232
2233(defmethod change-class ((instance standard-object) (new-class symbol) &rest initargs)
2234  (apply #'change-class instance (find-class new-class) initargs))
2235
2236(defgeneric update-instance-for-different-class (old new &key))
2237
2238(defmethod update-instance-for-different-class
2239  ((old standard-object) (new standard-object) &rest initargs)
2240  (let ((added-slots
2241         (remove-if #'(lambda (slot-name)
2242                       (slot-exists-p old slot-name))
2243                    (mapcar #'%slot-definition-name
2244                            (class-slots (class-of new))))))
2245    (check-initargs new added-slots initargs)
2246    (apply #'shared-initialize new added-slots initargs)))
2247
2248;;; make-instances-obsolete
2249
2250(defgeneric make-instances-obsolete (class))
2251
2252(defmethod make-instances-obsolete ((class standard-class))
2253  (%make-instances-obsolete class))
2254
2255(defmethod make-instances-obsolete ((class symbol))
2256  (make-instances-obsolete (find-class class))
2257  class)
2258
2259;;; update-instance-for-redefined-class
2260
2261(defgeneric update-instance-for-redefined-class (instance
2262                                                 added-slots
2263                                                 discarded-slots
2264                                                 property-list
2265                                                 &rest initargs
2266                                                 &key
2267                                                 &allow-other-keys))
2268
2269(defmethod update-instance-for-redefined-class ((instance standard-object)
2270            added-slots
2271            discarded-slots
2272            property-list
2273            &rest initargs)
2274  (check-initargs instance added-slots initargs)
2275  (apply #'shared-initialize instance added-slots initargs))
2276
2277;;;  Methods having to do with class metaobjects.
2278
2279(defmethod initialize-instance :after ((class standard-class) &rest args)
2280  (apply #'std-after-initialization-for-classes class args))
2281
2282;;; Finalize inheritance
2283
2284(defgeneric finalize-inheritance (class))
2285
2286(defmethod finalize-inheritance ((class standard-class))
2287  (std-finalize-inheritance class))
2288
2289;;; Class precedence lists
2290
2291(defgeneric compute-class-precedence-list (class))
2292(defmethod compute-class-precedence-list ((class standard-class))
2293  (std-compute-class-precedence-list class))
2294
2295;;; Slot inheritance
2296
2297(defgeneric compute-slots (class))
2298(defmethod compute-slots ((class standard-class))
2299  (std-compute-slots class))
2300
2301(defgeneric compute-effective-slot-definition (class direct-slots))
2302(defmethod compute-effective-slot-definition
2303  ((class standard-class) direct-slots)
2304  (std-compute-effective-slot-definition class direct-slots))
2305
2306;;; Methods having to do with generic function metaobjects.
2307
2308(defmethod initialize-instance :after ((gf standard-generic-function) &key)
2309  (finalize-generic-function gf))
2310
2311;;; Methods having to do with generic function invocation.
2312
2313(defgeneric compute-discriminating-function (gf))
2314(defmethod compute-discriminating-function ((gf standard-generic-function))
2315  (std-compute-discriminating-function gf))
2316
2317(defgeneric method-more-specific-p (gf method1 method2 required-classes))
2318
2319(defmethod method-more-specific-p ((gf standard-generic-function)
2320                                   method1 method2 required-classes)
2321  (std-method-more-specific-p method1 method2 required-classes
2322                              (generic-function-argument-precedence-order gf)))
2323
2324(defgeneric compute-effective-method-function (gf methods))
2325(defmethod compute-effective-method-function ((gf standard-generic-function) methods)
2326  (std-compute-effective-method-function gf methods))
2327
2328(defgeneric compute-applicable-methods (gf args))
2329(defmethod compute-applicable-methods ((gf standard-generic-function) args)
2330  (%compute-applicable-methods gf args))
2331
2332;;; Slot definition accessors
2333
2334(export '(slot-definition-allocation 
2335    slot-definition-initargs
2336    slot-definition-initform
2337    slot-definition-initfunction
2338    slot-definition-name))
2339
2340(defgeneric slot-definition-allocation (slot-definition)
2341  (:method ((slot-definition slot-definition))
2342    (%slot-definition-allocation slot-definition)))
2343
2344(defgeneric slot-definition-initargs (slot-definition)
2345  (:method ((slot-definition slot-definition))
2346    (%slot-definition-initargs slot-definition)))
2347
2348(defgeneric slot-definition-initform (slot-definition)
2349  (:method ((slot-definition slot-definition))
2350    (%slot-definition-initform slot-definition)))
2351
2352(defgeneric slot-definition-initfunction (slot-definition)
2353  (:method ((slot-definition slot-definition))
2354    (%slot-definition-initfunction slot-definition)))
2355
2356(defgeneric slot-definition-name (slot-definition)
2357  (:method ((slot-definition slot-definition))
2358    (%slot-definition-name slot-definition)))
2359
2360;;; No %slot-definition-type.
2361
2362
2363;;; Conditions.
2364
2365(defmacro define-condition (name (&rest parent-types) (&rest slot-specs) &body options)
2366  (let ((parent-types (or parent-types '(condition)))
2367        (report nil))
2368    (dolist (option options)
2369      (when (eq (car option) :report)
2370        (setf report (cadr option))
2371        (return)))
2372    (typecase report
2373      (null
2374       `(progn
2375          (defclass ,name ,parent-types ,slot-specs ,@options)
2376          ',name))
2377      (string
2378       `(progn
2379          (defclass ,name ,parent-types ,slot-specs ,@options)
2380          (defmethod print-object ((condition ,name) stream)
2381            (if *print-escape*
2382                (call-next-method)
2383                (progn (write-string ,report stream) condition)))
2384          ',name))
2385      (t
2386       `(progn
2387          (defclass ,name ,parent-types ,slot-specs ,@options)
2388          (defmethod print-object ((condition ,name) stream)
2389            (if *print-escape*
2390                (call-next-method)
2391                (funcall #',report condition stream)))
2392          ',name)))))
2393
2394(defun make-condition (type &rest initargs)
2395  (or (%make-condition type initargs)
2396      (let ((class (if (symbolp type) (find-class type) type)))
2397        (apply #'make-instance class initargs))))
2398
2399;; Adapted from SBCL.
2400;; Originally defined in signal.lisp. Redefined here now that we have MAKE-CONDITION.
2401(defun coerce-to-condition (datum arguments default-type fun-name)
2402  (cond ((typep datum 'condition)
2403         (when arguments
2404           (error 'simple-type-error
2405                  :datum arguments
2406                  :expected-type 'null
2407                  :format-control "You may not supply additional arguments when giving ~S to ~S."
2408                  :format-arguments (list datum fun-name)))
2409         datum)
2410        ((symbolp datum)
2411         (apply #'make-condition datum arguments))
2412        ((or (stringp datum) (functionp datum))
2413         (make-condition default-type
2414                         :format-control datum
2415                         :format-arguments arguments))
2416        (t
2417         (error 'simple-type-error
2418                :datum datum
2419                :expected-type '(or symbol string)
2420                :format-control "Bad argument to ~S: ~S."
2421                :format-arguments (list fun-name datum)))))
2422
2423(defgeneric make-load-form (object &optional environment))
2424
2425(defmethod make-load-form ((object t) &optional environment)
2426  (declare (ignore environment))
2427  (apply #'no-applicable-method #'make-load-form (list object)))
2428
2429(defmethod make-load-form ((class class) &optional environment)
2430  (declare (ignore environment))
2431  (let ((name (class-name class)))
2432    (unless (and name (eq (find-class name nil) class))
2433      (error 'simple-type-error
2434             :format-control "Can't use anonymous or undefined class as a constant: ~S."
2435             :format-arguments (list class)))
2436    `(find-class ',name)))
2437
2438(defun invalid-method-error (method format-control &rest args)
2439  (let ((message (apply #'format nil format-control args)))
2440    (error "Invalid method error for ~S:~%    ~A" method message)))
2441
2442(defun method-combination-error (format-control &rest args)
2443  (let ((message (apply #'format nil format-control args)))
2444    (error "Method combination error in CLOS dispatch:~%    ~A" message)))
2445
2446(fmakunbound 'no-applicable-method)
2447(defgeneric no-applicable-method (generic-function &rest args))
2448
2449(defmethod no-applicable-method (generic-function &rest args)
2450  (error "There is no applicable method for the generic function ~S when called with arguments ~S."
2451         generic-function
2452         args))
2453
2454(defgeneric find-method (generic-function
2455                         qualifiers
2456                         specializers
2457                         &optional errorp))
2458
2459(defmethod find-method ((generic-function standard-generic-function)
2460      qualifiers specializers &optional (errorp t))
2461  (%find-method generic-function qualifiers specializers errorp))
2462
2463(defgeneric add-method (generic-function method))
2464
2465(defmethod add-method ((generic-function standard-generic-function) (method method))
2466  (let ((method-lambda-list (method-lambda-list method))
2467        (gf-lambda-list (generic-function-lambda-list generic-function)))
2468    (check-method-lambda-list method-lambda-list gf-lambda-list))
2469  (%add-method generic-function method))
2470
2471(defgeneric remove-method (generic-function method))
2472
2473(defmethod remove-method ((generic-function standard-generic-function) method)
2474  (%remove-method generic-function method))
2475
2476;; See describe.lisp.
2477(defgeneric describe-object (object stream))
2478
2479;; FIXME
2480(defgeneric no-next-method (generic-function method &rest args))
2481
2482;; FIXME
2483(defgeneric function-keywords (method))
2484
2485(setf *clos-booting* nil)
2486
2487(defgeneric class-prototype (class))
2488
2489(defmethod class-prototype :before (class)
2490  (unless (class-finalized-p class)
2491    (error "~@<~S is not finalized.~:@>" class)))
2492
2493(defmethod class-prototype ((class standard-class))
2494  (allocate-instance class))
2495
2496(defmethod class-prototype ((class structure-class))
2497  (allocate-instance class))
2498
2499(provide 'clos)
Note: See TracBrowser for help on using the repository browser.