src/class-layout-impl.lisp: Fix class pointers in secondary chains.
[sod] / src / class-layout-impl.lisp
index 452e683..1794e5a 100644 (file)
                 :initializer (find-slot-initializer class slot)
                 :initargs (find-slot-initargs class slot)))
 
+(defmethod find-class-initializer ((slot effective-slot) (class sod-class))
+  (let ((dslot (effective-slot-direct-slot slot)))
+    (or (some (lambda (super)
+               (find dslot (sod-class-class-initializers super)
+                     :key #'sod-initializer-slot))
+             (sod-class-precedence-list class))
+       (effective-slot-initializer slot))))
+
 ;;;--------------------------------------------------------------------------
 ;;; Special-purpose slot objects.
 
@@ -82,7 +90,9 @@
   (default-slot (slot 'prepare-function)
     (get-property pset :prepare-function :func nil)))
 
-(export 'sod-class-effective-slot)
+(export '(sod-class-effective-slot
+         effective-slot-initializer-function
+         effective-slot-prepare-function))
 (defclass sod-class-effective-slot (effective-slot)
   ((initializer-function :initarg :initializer-function
                         :type (or symbol function)
            (sod-class-nickname (method-entry-chain-head entry))
            (method-entry-role entry))))
 
+(defmethod sod-message-applicable-methods
+    ((message sod-message) (class sod-class))
+  (mappend (lambda (super)
+            (remove message
+                    (sod-class-methods super)
+                    :key #'sod-method-message
+                    :test-not #'eql))
+          (sod-class-precedence-list class)))
+
 (defmethod compute-sod-effective-method
     ((message sod-message) (class sod-class))
-  (let ((direct-methods (mappend (lambda (super)
-                                  (remove message
-                                          (sod-class-methods super)
-                                          :key #'sod-method-message
-                                          :test-not #'eql))
-                                (sod-class-precedence-list class))))
+  (let ((direct-methods (sod-message-applicable-methods message class)))
     (make-instance (sod-message-effective-method-class message)
                   :message message
                   :class class
     ((class sod-class) (chain-head sod-class)
      (metaclass sod-class) (meta-chain-head sod-class))
 
-  ;; Slightly tricky.  We don't necessarily want a pointer to the metaclass,
-  ;; but to its most specific subclass on the given chain.  Fortunately, CL
-  ;; is good at this game.
-  (let* ((meta-chains (sod-class-chains metaclass))
-        (meta-chain-tails (mapcar #'car meta-chains))
-        (meta-chain-tail (find meta-chain-head meta-chain-tails
-                               :key #'sod-class-chain-head)))
+  ;; Rather tricky.  This is a class pointer on a vtable for the CHAIN-HEAD
+  ;; chain, pointing into the META-CHAIN-HEAD chain of the metaclass.  We
+  ;; need to produce a pointer to the most specific superclass of the
+  ;; metaclass on the right chain that is a superclass of the metaclass of
+  ;; the most specific class in the superclass chain headed by CHAIN-HEAD.
+  (flet ((chain-tail (class head)
+          (find head (mapcar #'car (sod-class-chains class))
+                :key #'sod-class-chain-head)))
     (make-instance 'class-pointer
-                  :class class
-                  :chain-head chain-head
-                  :metaclass meta-chain-tail
+                  :class class :chain-head chain-head
+                  :metaclass (chain-tail (sod-class-metaclass
+                                          (chain-tail class chain-head))
+                                         meta-chain-head)
                   :meta-chain-head meta-chain-head)))
 
 ;;; base-offset
 
   ;; If this class introduces new metaclass chains, then emit pointers to
   ;; them.
-  (let* ((metasuper (sod-class-metaclass super))
+  (let* ((metaclass (sod-class-metaclass class))
+        (metasuper (sod-class-metaclass super))
         (metasuper-chains (sod-class-chains metasuper))
         (metasuper-chain-heads (mapcar (lambda (chain)
                                          (sod-class-chain-head (car chain)))
       (unless (member metasuper-chain-head *done-metaclass-chains*)
        (funcall emit (make-class-pointer class
                                          chain-head
-                                         metasuper
+                                         metaclass
                                          metasuper-chain-head))
        (push metasuper-chain-head *done-metaclass-chains*))))