You will notice a curious thing. This method for allocate-instance is used to allocate storage for instances of user-defined classes, but because standard-class is an instance of itself, it is also used to allocate storage for classes. In practical terms, this is not that weird; it just means that storgate for class objects is laid out exactly the same way as storage for ordinary instances. However, an inherent circularity occurs here: an instance cannot be made until its class exists, but the class of standard-class is itself. Luckily, only the implementors of CLOS need to worry about this, because it is really a problem only when a CLOS system is being bootstrapped -- kind of like picking yourself up by your belt and flying.
More interesting than allocate-instance is initialize-instance. When we discussed the initialization protocol, we saw what the purpose of the initialization protocol was and how initialize-instancefits into it. Listing 5 shows an after method for initialize-instance on standard-class that initializes the class object allocated by allocate-std-instance. It is an after method to follow the CLOS convention for methods on initialize-instance. Reading the code, we see it performs the following steps. First, it determines the superclass objects, which are either passed in or defaulted to standard-object. Next, it installs the newly defined class as a subclass of the superclasses. Then it takes each slot property list and turns it into a slot definition metaobject. The slot accessor methods are then defined, and finally inheritance-related actions are performed.
(defmethod initialize-instance :after ((class standard-class) key direct-superclasses direct-slots allow-other-keys) (let ((supers (or direct-superclasses (list (find-class 'standard-object))))) (setf (slot-value class 'direct-superclasses) supers) (dolist (superclass supers) (push class (slot-value superclass 'direct-subclasses)))) (let ((slots (mapcar #'(lambda (slot-properties) (apply #'make-direct-slot-definition slot-properties)) direct-slots))) (setf (slot-value class 'direct-slots) slots) (dolist (direct-slot slots) (dolist (reader (slot-definition-readers direct-slot)) (add-reader-method class reader (slot-definition-name direct-slot))) (dolist (writer (slot-definition-writers direct-slot)) (add-writer-method class writer (slot-definition-name direct-slot))))) (finalize-inheritance class) (values))
The details of slot-definition meta-objects are not important. Let 's look at finalize-inheritance. In a real CLOS implementation, finalize-inheritance could not be called from within the initialization p rotocol because a class's superclasses might not be defined at that point. The definition of finalize-inheritance is:
(defmethod finalize-inheritance ((class standard-class)) (setf (class-precedence-list class) (compute-class-precedence-list class)) (setf (class-slots class) (compute-slots class)) (values))
finalize-inheritance first computes the class precedence list and stores it in a slot in the class object. Next, it computes effective slot-definition metaobjects, which describe each slot whether direct or inherited. This computation considers the slot inheritancles of CLOS. Listing 6 shows the method for compute-slots, which makes a lis t of the effective slot definitions. Each effective slot definition is computed by gathering all the direct slot definitions for each slot name into a list in the most to least specific order (according to the class precedence list) and passing that list along with the class to compute-effective-slot-definition. compute-effective-slot-definition simply makes an object that represents a slot definition with en tries representing its name, initform (and initfunction), initargs, and allocation type. mapappend is like mapcar except that the results are appended together.
(defmethod compute-slots ((class standard-class)) (let* ((all-slots (mapappend #'class-direct-slots (class-precedence-list class))) (all-names (remove-duplicates (mapcar #'slot-definition-name all-slots)))) (mapcar #'(lambda (name) (compute-effective-slot-definition class (remove name all-slots :key #'slot-definition-name :test-not #'eq))) all-names))) (defmethod compute-effective-slot-definition ((class standard-class) direct-slots) (let ((initer (find-if-not #'null direct-slots :key #'slot-definition-initfunction))) (make-effective-slot-definition :name (slot-definition-name (car direct-slots)) :initform (if initer (slot-definition-initform initer) nil) :initfunction (if initer (slot-definition-initfunction initer) nil) :initargs (remove-duplicates (mapappend #'slot-definition-initargs direct-slots)) :allocation (slot-definition-allocation (car direct-slots)))))
The important thing to see is that each slot has an entry in a list ; the relative position of an effective slot-definition entry in that list will determine where in the vector the slot is located. Note that we have not yet seen how slots are accessed. What we have seen is how the class object stores information that is used to access slots, and we've also seen how defclass eventually makes an instance of standard-class, which is a metaobject.