Cosmetic goops refactors.
[bpt/guile.git] / libguile / goops.c
index 070b6bc..f829695 100644 (file)
@@ -69,6 +69,7 @@
 
 SCM_KEYWORD (k_name, "name");
 SCM_KEYWORD (k_setter, "setter");
+SCM_SYMBOL (sym_redefined, "redefined");
 SCM_GLOBAL_SYMBOL (scm_sym_args, "args");
 
 static int goops_loaded_p = 0;
@@ -151,6 +152,8 @@ SCM scm_i_port_class[3 * SCM_I_MAX_PORT_TYPE_COUNT];
 /* SMOB classes.  */
 SCM scm_i_smob_class[SCM_I_MAX_SMOB_TYPE_COUNT];
 
+SCM scm_module_goops;
+
 static SCM scm_make_unbound (void);
 static SCM scm_unbound_p (SCM obj);
 static SCM scm_class_p (SCM obj);
@@ -163,6 +166,62 @@ static SCM scm_sys_goops_early_init (void);
 static SCM scm_sys_goops_loaded (void);
 
 
+\f
+
+SCM_DEFINE (scm_sys_make_root_class, "%make-root-class", 1, 0, 0,
+            (SCM layout),
+           "")
+#define FUNC_NAME s_scm_sys_make_root_class
+{
+  SCM z;
+
+  z = scm_i_make_vtable_vtable (layout);
+  SCM_SET_CLASS_FLAGS (z, (SCM_CLASSF_GOOPS_OR_VALID | SCM_CLASSF_METACLASS));
+
+  return z;
+}
+#undef FUNC_NAME
+
+SCM_DEFINE (scm_sys_bless_applicable_struct_vtables_x, "%bless-applicable-struct-vtables!", 2, 0, 0,
+           (SCM applicable, SCM setter),
+           "")
+#define FUNC_NAME s_scm_sys_bless_applicable_struct_vtables_x
+{
+  SCM_VALIDATE_CLASS (1, applicable);
+  SCM_VALIDATE_CLASS (2, setter);
+  SCM_SET_VTABLE_FLAGS (applicable, SCM_VTABLE_FLAG_APPLICABLE_VTABLE);
+  SCM_SET_VTABLE_FLAGS (setter, SCM_VTABLE_FLAG_SETTER_VTABLE);
+  return SCM_UNSPECIFIED;
+}
+#undef FUNC_NAME
+
+SCM
+scm_make_standard_class (SCM meta, SCM name, SCM dsupers, SCM dslots)
+{
+  return scm_call_4 (scm_variable_ref (var_make_standard_class),
+                     meta, name, dsupers, dslots);
+}
+
+SCM_DEFINE (scm_sys_init_layout_x, "%init-layout!", 2, 0, 0,
+           (SCM class, SCM layout),
+           "")
+#define FUNC_NAME s_scm_sys_init_layout_x
+{
+  SCM_VALIDATE_INSTANCE (1, class);
+  SCM_ASSERT (!scm_is_symbol (SCM_VTABLE_LAYOUT (class)), class, 1, FUNC_NAME);
+  SCM_VALIDATE_STRING (2, layout);
+
+  SCM_SET_VTABLE_LAYOUT (class, scm_make_struct_layout (layout));
+  scm_i_struct_inherit_vtable_magic (SCM_CLASS_OF (class), class);
+  SCM_SET_CLASS_FLAGS (class, SCM_CLASSF_GOOPS_OR_VALID);
+
+  return SCM_UNSPECIFIED;
+}
+#undef FUNC_NAME
+
+
+\f
+
 /* This function is used for efficient type dispatch.  */
 SCM_DEFINE (scm_class_of, "class-of", 1, 0, 0,
            (SCM x),
@@ -254,14 +313,16 @@ SCM_DEFINE (scm_class_of, "class-of", 1, 0, 0,
                                    : SCM_IN_PCLASS_INDEX | SCM_PTOBNUM (x))];
        case scm_tcs_struct:
          if (SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_GOOPS_VALID)
+            /* A GOOPS object with a valid class.  */
            return SCM_CLASS_OF (x);
          else if (SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_GOOPS)
+            /* A GOOPS object whose class might have been redefined.  */
            {
-             /* Goops object */
-             if (! scm_is_false (SCM_OBJ_CLASS_REDEF (x)))
-               scm_change_object_class (x,
-                                        SCM_CLASS_OF (x),         /* old */
-                                        SCM_OBJ_CLASS_REDEF (x)); /* new */
+              SCM class = SCM_CLASS_OF (x);
+              SCM new_class = scm_slot_ref (class, sym_redefined);
+              if (!scm_is_false (new_class))
+               scm_change_object_class (x, class, new_class);
+              /* Re-load class from instance.  */
              return SCM_CLASS_OF (x);
            }
          else
@@ -284,59 +345,8 @@ SCM_DEFINE (scm_class_of, "class-of", 1, 0, 0,
 }
 #undef FUNC_NAME
 
-SCM_DEFINE (scm_sys_init_layout_x, "%init-layout!", 2, 0, 0,
-           (SCM class, SCM layout),
-           "")
-#define FUNC_NAME s_scm_sys_init_layout_x
-{
-  SCM_VALIDATE_INSTANCE (1, class);
-  SCM_ASSERT (!scm_is_symbol (SCM_VTABLE_LAYOUT (class)), class, 1, FUNC_NAME);
-  SCM_VALIDATE_STRING (2, layout);
-
-  SCM_SET_VTABLE_LAYOUT (class, scm_make_struct_layout (layout));
-  return SCM_UNSPECIFIED;
-}
-#undef FUNC_NAME
-
-SCM_DEFINE (scm_sys_inherit_magic_x, "%inherit-magic!", 2, 0, 0,
-           (SCM class, SCM dsupers),
-           "")
-#define FUNC_NAME s_scm_sys_inherit_magic_x
-{
-  SCM_VALIDATE_INSTANCE (1, class);
-  scm_i_struct_inherit_vtable_magic (SCM_CLASS_OF (class), class);
-  SCM_SET_CLASS_FLAGS (class, SCM_CLASSF_GOOPS_OR_VALID);
-
-  return SCM_UNSPECIFIED;
-}
-#undef FUNC_NAME
-
-/******************************************************************************/
-
-SCM
-scm_make_standard_class (SCM meta, SCM name, SCM dsupers, SCM dslots)
-{
-  return scm_call_4 (scm_variable_ref (var_make_standard_class),
-                     meta, name, dsupers, dslots);
-}
-
-/******************************************************************************/
-
-SCM_DEFINE (scm_sys_make_root_class, "%make-root-class", 1, 0, 0,
-            (SCM layout),
-           "")
-#define FUNC_NAME s_scm_sys_make_root_class
-{
-  SCM z;
-
-  z = scm_i_make_vtable_vtable (layout);
-  SCM_SET_CLASS_FLAGS (z, (SCM_CLASSF_GOOPS_OR_VALID | SCM_CLASSF_METACLASS));
-
-  return z;
-}
-#undef FUNC_NAME
 
-/******************************************************************************/
+\f
 
 SCM_DEFINE (scm_instance_p, "instance?", 1, 0, 0,
            (SCM obj),
@@ -368,11 +378,8 @@ scm_is_method (SCM x)
   return SCM_INSTANCEP (x) && SCM_SUBCLASSP (SCM_CLASS_OF (x), class_method);
 }
 
-/******************************************************************************
- *
- * Meta object accessors
- *
- ******************************************************************************/
+
+\f
 
 SCM
 scm_class_name (SCM obj)
@@ -416,6 +423,9 @@ scm_class_slots (SCM obj)
   return scm_call_1 (scm_variable_ref (var_class_slots), obj);
 }
 
+
+\f
+
 SCM_DEFINE (scm_generic_function_name, "generic-function-name", 1, 0, 0,
             (SCM obj),
            "Return the name of the generic function @var{obj}.")
@@ -450,11 +460,8 @@ scm_method_procedure (SCM obj)
   return scm_call_1 (scm_variable_ref (var_method_procedure), obj);
 }
 
-/******************************************************************************
- *
- * S l o t   a c c e s s
- *
- ******************************************************************************/
+
+\f
 
 SCM_DEFINE (scm_make_unbound, "make-unbound", 0, 0, 0,
            (),
@@ -529,6 +536,9 @@ scm_slot_exists_p (SCM obj, SCM slot_name)
   return scm_call_2 (scm_variable_ref (var_slot_exists_p), obj, slot_name);
 }
 
+
+\f
+
 SCM_DEFINE (scm_sys_clear_fields_x, "%clear-fields!", 1, 0, 0,
            (SCM obj),
             "")
@@ -552,15 +562,12 @@ SCM_DEFINE (scm_sys_clear_fields_x, "%clear-fields!", 1, 0, 0,
 }
 #undef FUNC_NAME
 
-/******************************************************************************
- *
- * %modify-instance (used by change-class to modify in place)
- *
- ******************************************************************************/
+
+\f
 
 SCM_DEFINE (scm_sys_modify_instance, "%modify-instance", 2, 0, 0,
            (SCM old, SCM new),
-           "")
+           "Used by change-class to modify objects in place.")
 #define FUNC_NAME s_scm_sys_modify_instance
 {
   SCM_VALIDATE_INSTANCE (1, old);
@@ -689,21 +696,11 @@ scm_change_object_class (SCM obj, SCM old_class SCM_UNUSED, SCM new_class)
     }
 }
 
-/******************************************************************************
- *
- *   GGGG                FFFFF
- *  G                    F
- *  G  GG                FFF
- *  G   G                F
- *   GGG  E N E R I C    F    U N C T I O N S
- *
- * This implementation provides
- *     - generic functions (with class specializers)
- *     - multi-methods
- *     - next-method
- *     - a hard-coded MOP for standard gf, which can be overloaded for non-std gf
- *
- ******************************************************************************/
+
+\f
+
+/* Primitive generics: primitives that can dispatch to generics if their
+   arguments fail to apply.  */
 
 SCM_DEFINE (scm_generic_capability_p, "generic-capability?", 1, 0, 0,
            (SCM proc),
@@ -763,11 +760,6 @@ SCM_DEFINE (scm_primitive_generic_generic, "primitive-generic-generic", 1, 0, 0,
 }
 #undef FUNC_NAME
 
-/* Dirk:FIXME:: In all of these scm_wta_dispatch_* routines it is
- * assumed that 'gf' is zero if uninitialized.  It would be cleaner if
- * some valid SCM value like SCM_BOOL_F or SCM_UNDEFINED were chosen.
- */
-
 SCM
 scm_wta_dispatch_0 (SCM gf, const char *subr)
 {
@@ -804,22 +796,8 @@ scm_wta_dispatch_n (SCM gf, SCM args, int pos, const char *subr)
   return scm_apply_0 (gf, args);
 }
 
-/******************************************************************************
- *
- * Protocol for calling a generic fumction
- * This protocol is roughly equivalent to (parameter are a little bit different
- * for efficiency reasons):
- *
- *     + apply-generic (gf args)
- *             + compute-applicable-methods (gf args ...)
- *                     + sort-applicable-methods (methods args)
- *             + apply-methods (gf methods args)
- *
- * apply-methods calls make-next-method to build the "continuation" of a a
- * method.  Applying a next-method will call apply-next-method which in
- * turn will call  apply again to call effectively the following method.
- *
- ******************************************************************************/
+
+\f
 
 SCM_DEFINE (scm_make, "make",  0, 0, 1,
            (SCM args),
@@ -832,11 +810,9 @@ SCM_DEFINE (scm_make, "make",  0, 0, 1,
 #undef FUNC_NAME
 
 
-/**********************************************************************
- *
- * Smob classes
- *
- **********************************************************************/
+\f
+
+/* SMOB, struct, and port classes.  */
 
 static SCM
 make_class_name (const char *prefix, const char *type_name, const char *suffix)
@@ -1000,11 +976,8 @@ create_struct_classes (void)
                           vtable_class_map);
 }
 
-/**********************************************************************
- *
- * C interface
- *
- **********************************************************************/
+
+\f
 
 void
 scm_load_goops ()
@@ -1034,22 +1007,8 @@ scm_ensure_accessor (SCM name)
   return gf;
 }
 
-/*
- * Initialization
- */
 
-SCM_DEFINE (scm_sys_bless_applicable_struct_vtables_x, "%bless-applicable-struct-vtables!", 2, 0, 0,
-           (SCM applicable, SCM setter),
-           "")
-#define FUNC_NAME s_scm_sys_bless_applicable_struct_vtables_x
-{
-  SCM_VALIDATE_CLASS (1, applicable);
-  SCM_VALIDATE_CLASS (2, setter);
-  SCM_SET_VTABLE_FLAGS (applicable, SCM_VTABLE_FLAG_APPLICABLE_VTABLE);
-  SCM_SET_VTABLE_FLAGS (setter, SCM_VTABLE_FLAG_SETTER_VTABLE);
-  return SCM_UNSPECIFIED;
-}
-#undef FUNC_NAME
+\f
 
 SCM_DEFINE (scm_sys_goops_early_init, "%goops-early-init", 0, 0, 0,
            (),
@@ -1060,6 +1019,9 @@ SCM_DEFINE (scm_sys_goops_early_init, "%goops-early-init", 0, 0, 0,
   var_make = scm_c_lookup ("make");
   var_inherit_applicable = scm_c_lookup ("inherit-applicable!");
 
+  /* For SCM_SUBCLASSP.  */
+  var_class_precedence_list = scm_c_lookup ("class-precedence-list");
+
   var_slot_ref_using_class = scm_c_lookup ("slot-ref-using-class");
   var_slot_set_using_class_x = scm_c_lookup ("slot-set-using-class!");
   var_slot_bound_using_class_p = scm_c_lookup ("slot-bound-using-class?");
@@ -1159,7 +1121,6 @@ SCM_DEFINE (scm_sys_goops_loaded, "%goops-loaded", 0, 0, 0,
   var_class_direct_slots = scm_c_lookup ("class-direct-slots");
   var_class_direct_subclasses = scm_c_lookup ("class-direct-subclasses");
   var_class_direct_methods = scm_c_lookup ("class-direct-methods");
-  var_class_precedence_list = scm_c_lookup ("class-precedence-list");
   var_class_slots = scm_c_lookup ("class-slots");
 
   var_generic_function_methods = scm_c_lookup ("generic-function-methods");
@@ -1177,8 +1138,6 @@ SCM_DEFINE (scm_sys_goops_loaded, "%goops-loaded", 0, 0, 0,
 }
 #undef FUNC_NAME
 
-SCM scm_module_goops;
-
 static void
 scm_init_goops_builtins (void *unused)
 {