chiark / gitweb /
src/class-{finalize,layout}-*.lisp: Relocate layout interface code.
[sod] / src / class-layout-impl.lisp
index 4bff54d0f7e1998a1aad1fc44e9509575ad8c77c..452e683c74eee759eacd30a9379ec1dc47ef92f5 100644 (file)
@@ -7,7 +7,7 @@
 
 ;;;----- Licensing notice ---------------------------------------------------
 ;;;
-;;; This file is part of the Sensble Object Design, an object system for C.
+;;; This file is part of the Sensible Object Design, an object system for C.
 ;;;
 ;;; SOD is free software; you can redistribute it and/or modify
 ;;; it under the terms of the GNU General Public License as published by
@@ -41,16 +41,26 @@ (defmethod find-slot-initializer ((class sod-class) (slot sod-slot))
                :key #'sod-initializer-slot))
        (sod-class-precedence-list class)))
 
+(defmethod find-slot-initargs ((class sod-class) (slot sod-slot))
+  (mappend (lambda (super)
+            (remove-if-not (lambda (initarg)
+                             (and (typep initarg 'sod-slot-initarg)
+                                  (eq (sod-initarg-slot initarg) slot)))
+                           (sod-class-initargs super)))
+          (sod-class-precedence-list class)))
+
 (defmethod compute-effective-slot ((class sod-class) (slot sod-slot))
   (make-instance 'effective-slot
                 :slot slot
                 :class class
-                :initializer (find-slot-initializer class slot)))
+                :initializer (find-slot-initializer class slot)
+                :initargs (find-slot-initargs class slot)))
 
 ;;;--------------------------------------------------------------------------
 ;;; Special-purpose slot objects.
 
-(export 'sod-class-slot)
+(export '(sod-class-slot
+         sod-slot-initializer-function sod-slot-prepare-function))
 (defclass sod-class-slot (sod-slot)
   ((initializer-function :initarg :initializer-function
                         :type (or symbol function)
@@ -58,7 +68,7 @@ (defclass sod-class-slot (sod-slot)
    (prepare-function :initarg :prepare-function :type (or symbol function)
                     :reader sod-slot-prepare-function))
   (:documentation
-   "Special class for slots defined on SodClass.
+   "Special class for slots defined on `SodClass'.
 
    These slots need class-specific initialization.  It's easier to keep all
    of the information (name, type, and how to initialize them) about these
@@ -68,9 +78,9 @@ (defmethod shared-initialize :after
     ((slot sod-class-slot) slot-names &key pset)
   (declare (ignore slot-names))
   (default-slot (slot 'initializer-function)
-    (get-property pset :initializer-function t nil))
+    (get-property pset :initializer-function :func nil))
   (default-slot (slot 'prepare-function)
-    (get-property pset :prepare-function t nil)))
+    (get-property pset :prepare-function :func nil)))
 
 (export 'sod-class-effective-slot)
 (defclass sod-class-effective-slot (effective-slot)
@@ -80,7 +90,7 @@ (defclass sod-class-effective-slot (effective-slot)
    (prepare-function :initarg :prepare-function :type (or symbol function)
                     :reader effective-slot-prepare-function))
   (:documentation
-   "Special class for slots defined on SodClass.
+   "Special class for slots defined on `SodClass'.
 
    This class ignores any explicit initializers and computes initializer
    values using the slot's INIT-FUNC slot and a magical protocol during
@@ -104,9 +114,10 @@ (defmethod print-object ((method effective-method) stream)
 
 (defmethod print-object ((entry method-entry) stream)
   (maybe-print-unreadable-object (entry stream :type t)
-    (format stream "~A:~A"
+    (format stream "~A:~A~@[ ~S~]"
            (method-entry-effective-method entry)
-           (sod-class-nickname (method-entry-chain-head entry)))))
+           (sod-class-nickname (method-entry-chain-head entry))
+           (method-entry-role entry))))
 
 (defmethod compute-sod-effective-method
     ((message sod-message) (class sod-class))
@@ -115,8 +126,8 @@ (defmethod compute-sod-effective-method
                                           (sod-class-methods super)
                                           :key #'sod-method-message
                                           :test-not #'eql))
-                               (sod-class-precedence-list class))))
-    (make-instance (message-effective-method-class message)
+                                (sod-class-precedence-list class))))
+    (make-instance (sod-message-effective-method-class message)
                   :message message
                   :class class
                   :direct-methods direct-methods)))
@@ -128,11 +139,6 @@ (defmethod compute-effective-methods ((class sod-class))
                    (sod-class-messages super)))
          (sod-class-precedence-list class)))
 
-(defmethod slot-unbound
-    (clos-class (class sod-class) (slot-name (eql 'effective-methods)))
-  (setf (slot-value class 'effective-methods)
-       (compute-effective-methods class)))
-
 ;;;--------------------------------------------------------------------------
 ;;; Instance layout.
 
@@ -205,11 +211,6 @@ (defmethod compute-ilayout ((class sod-class))
                                                    (reverse chain)))
                                  (sod-class-chains class))))
 
-(defmethod slot-unbound
-    (clos-class (class sod-class) (slot-name (eql 'ilayout)))
-  (setf (slot-value class 'ilayout)
-       (compute-ilayout class)))
-
 ;;;--------------------------------------------------------------------------
 ;;; Vtable layout.
 
@@ -227,17 +228,17 @@ (defmethod compute-vtmsgs
      (subclass sod-class)
      (chain-head sod-class)
      (chain-tail sod-class))
-  (flet ((make-entry (message)
+  (flet ((make-entries (message)
           (let ((method (find message
                               (sod-class-effective-methods subclass)
                               :key #'effective-method-message)))
-            (make-method-entry method chain-head chain-tail))))
+            (make-method-entries method chain-head chain-tail))))
     (make-instance 'vtmsgs
                   :class class
                   :subclass subclass
                   :chain-head chain-head
                   :chain-tail chain-tail
-                  :entries (mapcar #'make-entry
+                  :entries (mapcan #'make-entries
                                    (sod-class-messages class)))))
 
 ;;; class-pointer
@@ -387,9 +388,22 @@ (defmethod compute-vtables ((class sod-class))
            (compute-vtable class (reverse chain)))
          (sod-class-chains class)))
 
-(defmethod slot-unbound
-    (clos-class (class sod-class) (slot-name (eql 'vtables)))
-  (setf (slot-value class 'vtables)
-       (compute-vtables class)))
+;;;--------------------------------------------------------------------------
+;;; Layout interface.
+
+;; Just arrange to populate the necessary slots on demand.
+(flet ((check-class-is-finalized (class)
+        (unless (eq (sod-class-state class) :finalized)
+          (error "Class ~S is not finalized" class))))
+    (macrolet ((define-layout-slot (slot (class) &body body)
+              `(define-on-demand-slot sod-class ,slot (,class)
+                 (check-class-is-finalized ,class)
+                 ,@body)))
+    (define-layout-slot %ilayout (class)
+      (compute-ilayout class))
+    (define-layout-slot effective-methods (class)
+      (compute-effective-methods class))
+    (define-layout-slot vtables (class)
+      (compute-vtables class))))
 
 ;;;----- That's all, folks --------------------------------------------------