Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / libguile / goops.c
index dfe26c3..5e846ee 100644 (file)
@@ -53,7 +53,6 @@
 #include "libguile/strings.h"
 #include "libguile/strports.h"
 #include "libguile/vectors.h"
-#include "libguile/weaks.h"
 #include "libguile/vm.h"
 
 #include "libguile/validate.h"
@@ -85,13 +84,6 @@ SCM_SYMBOL (sym_change_class, "change-class");
 SCM_VARIABLE (scm_var_make_extended_generic, "make-extended-generic");
 
 
-/* FIXME, exports should come from the scm file only */
-#define DEFVAR(v, val)                                          \
-  { scm_module_define (scm_module_goops, (v), (val));           \
-    scm_module_export (scm_module_goops, scm_list_1 ((v)));     \
-  }
-
-
 /* Class redefinition protocol:
 
    A class is represented by a heap header h1 which points to a
@@ -168,9 +160,10 @@ static SCM class_vm;
 static SCM class_vm_cont;
 static SCM class_bytevector;
 static SCM class_uvec;
+static SCM class_array;
+static SCM class_bitvector;
 
 static SCM vtable_class_map = SCM_BOOL_F;
-static scm_i_pthread_mutex_t vtable_class_map_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
 
 /* Port classes.  Allocate 3 times the maximum number of port types so that
    input ports, output ports, and in/out ports can be stored at different
@@ -198,17 +191,15 @@ scm_i_define_class_for_vtable (SCM vtable)
 {
   SCM class;
 
-  scm_i_pthread_mutex_lock (&vtable_class_map_lock);
-
+  scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
   if (scm_is_false (vtable_class_map))
-    vtable_class_map = scm_make_weak_key_hash_table (SCM_UNDEFINED);
+    vtable_class_map = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
+  scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
   
   if (scm_is_false (scm_struct_vtable_p (vtable)))
     abort ();
 
-  class = scm_hashq_ref (vtable_class_map, vtable, SCM_BOOL_F);
-  
-  scm_i_pthread_mutex_unlock (&vtable_class_map_lock);
+  class = scm_weak_table_refq (vtable_class_map, vtable, SCM_BOOL_F);
 
   if (scm_is_false (class))
     {
@@ -227,9 +218,7 @@ scm_i_define_class_for_vtable (SCM vtable)
 
       /* Don't worry about races.  This only happens when creating a
          vtable, which happens by definition in one thread.  */
-      scm_i_pthread_mutex_lock (&vtable_class_map_lock);
-      scm_hashq_set_x (vtable_class_map, vtable, class);
-      scm_i_pthread_mutex_unlock (&vtable_class_map_lock);
+      scm_weak_table_putq_x (vtable_class_map, vtable, class);
     }
 
   return class;
@@ -288,6 +277,10 @@ SCM_DEFINE (scm_class_of, "class-of", 1, 0, 0,
             return class_bytevector;
           else
             return class_uvec;
+       case scm_tc7_array:
+          return class_array;
+       case scm_tc7_bitvector:
+          return class_bitvector;
        case scm_tc7_string:
          return scm_class_string;
         case scm_tc7_number:
@@ -373,7 +366,7 @@ map (SCM (*proc) (SCM), SCM ls)
       SCM res = scm_cons (proc (SCM_CAR (ls)), SCM_EOL);
       SCM h = res;
       ls = SCM_CDR (ls);
-      while (!scm_is_null (ls))
+      while (scm_is_pair (ls))
        {
          SCM_SETCDR (h, scm_cons (proc (SCM_CAR (ls)), SCM_EOL));
          h = SCM_CDR (h);
@@ -387,7 +380,7 @@ static SCM
 filter_cpl (SCM ls)
 {
   SCM res = SCM_EOL;
-  while (!scm_is_null (ls))
+  while (scm_is_pair (ls))
     {
       SCM el = SCM_CAR (ls);
       if (scm_is_false (scm_c_memq (el, res)))
@@ -422,7 +415,7 @@ remove_duplicate_slots (SCM l, SCM res, SCM slots_already_seen)
 {
   SCM tmp;
 
-  if (scm_is_null (l))
+  if (!scm_is_pair (l))
     return res;
 
   tmp = SCM_CAAR (l);
@@ -437,15 +430,63 @@ remove_duplicate_slots (SCM l, SCM res, SCM slots_already_seen)
   return remove_duplicate_slots (SCM_CDR (l), res, slots_already_seen);
 }
 
+static void 
+check_cpl (SCM slots, SCM bslots)
+{
+  for (; scm_is_pair (bslots); bslots = SCM_CDR (bslots))
+    if (scm_is_true (scm_assq (SCM_CAAR (bslots), slots)))
+      scm_misc_error ("init-object", "a predefined <class> inherited "
+                      "field cannot be redefined", SCM_EOL);  
+}
+
+static SCM 
+build_class_class_slots (void);
+
 static SCM
 build_slots_list (SCM dslots, SCM cpl)
 {
-  register SCM res = dslots;
+  SCM bslots, class_slots;
+  int classp;
+  SCM res = dslots;
+
+  class_slots = SCM_EOL;  
+  classp = scm_is_true (scm_memq (scm_class_class, cpl));
+  
+  if (classp) 
+    {
+      bslots = build_class_class_slots ();
+      check_cpl (res, bslots);
+    }
+  else
+    bslots = SCM_EOL;
+
+  if (scm_is_pair (cpl))
+    {      
+      for (cpl = SCM_CDR (cpl); scm_is_pair (cpl); cpl = SCM_CDR (cpl))
+        {
+          SCM new_slots = SCM_SLOT (SCM_CAR (cpl),
+                                    scm_si_direct_slots);
+          if (classp)
+            {
+              if (!scm_is_eq (SCM_CAR (cpl), scm_class_class))
+                check_cpl (new_slots, bslots);
+              else
+                {
+                  /* Move class slots to the head of the list. */
+                  class_slots = new_slots;
+                  continue;
+                }
+            }   
+          res = scm_append (scm_list_2 (new_slots, res));
+        }
+    }
+  else
+    scm_misc_error ("%compute-slots", "malformed cpl argument in "
+                    "build_slots_list", SCM_EOL);
 
-  for (cpl = SCM_CDR (cpl); !scm_is_null (cpl); cpl = SCM_CDR (cpl))
-    res = scm_append (scm_list_2 (SCM_SLOT (SCM_CAR (cpl),
-                                           scm_si_direct_slots),
-                                 res));
+  /* make sure to add the <class> slots to the head of the list */
+  if (classp)
+    res = scm_append (scm_list_2 (class_slots, res));
 
   /* res contains a list of slots. Remove slots which appears more than once */
   return remove_duplicate_slots (scm_reverse (res), SCM_EOL, SCM_EOL);
@@ -457,8 +498,11 @@ maplist (SCM ls)
   SCM orig = ls;
   while (!scm_is_null (ls))
     {
+      if (!scm_is_pair (ls))
+        scm_misc_error ("%compute-slots", "malformed ls argument in "
+                        "maplist", SCM_EOL);
       if (!scm_is_pair (SCM_CAR (ls)))
-       SCM_SETCAR (ls, scm_cons (SCM_CAR (ls), SCM_EOL));
+        SCM_SETCAR (ls, scm_cons (SCM_CAR (ls), SCM_EOL));
       ls = SCM_CDR (ls);
     }
   return orig;
@@ -877,12 +921,11 @@ SCM_SYMBOL (sym_cpl, "cpl");
 SCM_SYMBOL (sym_default_slot_definition_class, "default-slot-definition-class");
 SCM_SYMBOL (sym_slots, "slots");
 SCM_SYMBOL (sym_getters_n_setters, "getters-n-setters");
-SCM_SYMBOL (sym_keyword_access, "keyword-access");
 SCM_SYMBOL (sym_nfields, "nfields");
 
 
 static SCM
-build_class_class_slots ()
+build_class_class_slots (void)
 {
   /* has to be kept in sync with SCM_VTABLE_BASE_LAYOUT and
      SCM_CLASS_CLASS_LAYOUT */
@@ -912,7 +955,6 @@ build_class_class_slots ()
     scm_list_1 (sym_default_slot_definition_class),
     scm_list_1 (sym_slots),
     scm_list_1 (sym_getters_n_setters),
-    scm_list_1 (sym_keyword_access),
     scm_list_1 (sym_nfields),
     SCM_UNDEFINED);
 }
@@ -925,7 +967,7 @@ create_basic_classes (void)
   /**** <class> ****/
   SCM cs = scm_from_locale_string (SCM_CLASS_CLASS_LAYOUT);
   SCM name = scm_from_latin1_symbol ("<class>");
-  scm_class_class = scm_make_vtable_vtable (cs, SCM_INUM0, SCM_EOL);
+  scm_class_class = scm_i_make_vtable_vtable (cs);
   SCM_SET_CLASS_FLAGS (scm_class_class, (SCM_CLASSF_GOOPS_OR_VALID
                                         | SCM_CLASSF_METACLASS));
 
@@ -943,21 +985,21 @@ create_basic_classes (void)
 
   prep_hashsets (scm_class_class);
 
-  DEFVAR(name, scm_class_class);
+  scm_module_define (scm_module_goops, name, scm_class_class);
 
   /**** <top> ****/
   name = scm_from_latin1_symbol ("<top>");
   scm_class_top = scm_basic_make_class (scm_class_class, name,
                                         SCM_EOL, SCM_EOL);
 
-  DEFVAR(name, scm_class_top);
+  scm_module_define (scm_module_goops, name, scm_class_top);
 
   /**** <object> ****/
   name  = scm_from_latin1_symbol ("<object>");
   scm_class_object = scm_basic_make_class (scm_class_class, name,
                                            scm_list_1 (scm_class_top), SCM_EOL);
 
-  DEFVAR (name, scm_class_object);
+  scm_module_define (scm_module_goops, name, scm_class_object);
 
   /* <top> <object> and <class> were partially initialized. Correct them here */
   SCM_SET_SLOT (scm_class_object, scm_si_direct_subclasses, scm_list_1 (scm_class_class));
@@ -1676,36 +1718,6 @@ SCM_KEYWORD (k_name, "name");
 SCM_GLOBAL_SYMBOL (scm_sym_args, "args");
 
 
-SCM
-scm_apply_generic (SCM gf, SCM args)
-{
-  return scm_apply (SCM_STRUCT_PROCEDURE (gf), args, SCM_EOL);
-}
-
-SCM
-scm_call_generic_0 (SCM gf)
-{
-  return scm_call_0 (SCM_STRUCT_PROCEDURE (gf));
-}
-
-SCM
-scm_call_generic_1 (SCM gf, SCM a1)
-{
-  return scm_call_1 (SCM_STRUCT_PROCEDURE (gf), a1);
-}
-
-SCM
-scm_call_generic_2 (SCM gf, SCM a1, SCM a2)
-{
-  return scm_call_2 (SCM_STRUCT_PROCEDURE (gf), a1, a2);
-}
-
-SCM
-scm_call_generic_3 (SCM gf, SCM a1, SCM a2, SCM a3)
-{
-  return scm_call_3 (SCM_STRUCT_PROCEDURE (gf), a1, a2, a3);
-}
-
 SCM_SYMBOL (sym_delayed_compile, "delayed-compile");
 static SCM
 make_dispatch_procedure (SCM gf)
@@ -1849,6 +1861,47 @@ setup_extended_primitive_generics ()
     }
 }
 
+/* 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)
+{
+  if (!SCM_UNPACK (gf))
+    scm_error_num_args_subr (subr);
+
+  return scm_call_0 (gf);
+}
+
+SCM
+scm_wta_dispatch_1 (SCM gf, SCM a1, int pos, const char *subr)
+{
+  if (!SCM_UNPACK (gf))
+    scm_wrong_type_arg (subr, pos, a1);
+
+  return scm_call_1 (gf, a1);
+}
+
+SCM
+scm_wta_dispatch_2 (SCM gf, SCM a1, SCM a2, int pos, const char *subr)
+{
+  if (!SCM_UNPACK (gf))
+    scm_wrong_type_arg (subr, pos, (pos == SCM_ARG1) ? a1 : a2);
+
+  return scm_call_2 (gf, a1, a2);
+}
+
+SCM
+scm_wta_dispatch_n (SCM gf, SCM args, int pos, const char *subr)
+{
+  if (!SCM_UNPACK (gf))
+    scm_wrong_type_arg (subr, pos, scm_list_ref (args, scm_from_int (pos)));
+
+  return scm_apply_0 (gf, args);
+}
+
 /******************************************************************************
  *
  * Protocol for calling a generic fumction
@@ -2280,15 +2333,21 @@ SCM_DEFINE (scm_sys_method_more_specific_p, "%method-more-specific?", 3, 0, 0,
  *
  ******************************************************************************/
 
+/* Munge the CPL of C in place such that BEFORE appears before AFTER,
+   assuming that currently the reverse is true.  Recalculate slots and
+   associated getters-n-setters.  */
 static void
 fix_cpl (SCM c, SCM before, SCM after)
 {
   SCM cpl = SCM_SLOT (c, scm_si_cpl);
   SCM ls = scm_c_memq (after, cpl);
-  SCM tail = scm_delq1_x (before, SCM_CDR (ls));
+  SCM tail;
+
   if (scm_is_false (ls))
     /* if this condition occurs, fix_cpl should not be applied this way */
     abort ();
+
+  tail = scm_delq1_x (before, SCM_CDR (ls));
   SCM_SETCAR (ls, before);
   SCM_SETCDR (ls, scm_cons (after, tail));
   {
@@ -2304,12 +2363,12 @@ fix_cpl (SCM c, SCM before, SCM after)
 static void
 make_stdcls (SCM *var, char *name, SCM meta, SCM super, SCM slots)
 {
-   SCM tmp = scm_from_locale_symbol (name);
+   SCM tmp = scm_from_utf8_symbol (name);
 
    *var = scm_basic_make_class (meta, tmp,
                                 scm_is_pair (super) ? super : scm_list_1 (super),
                                 slots);
-   DEFVAR(tmp, *var);
+   scm_module_define (scm_module_goops, tmp, *var);
 }
 
 
@@ -2414,8 +2473,8 @@ create_standard_classes (void)
   make_stdcls (&scm_class_extended_generic_with_setter,
               "<extended-generic-with-setter>",
               scm_class_applicable_struct_class,
-              scm_list_2 (scm_class_generic_with_setter,
-                          scm_class_extended_generic),
+              scm_list_2 (scm_class_extended_generic,
+                           scm_class_generic_with_setter),
               SCM_EOL);
   SCM_SET_CLASS_FLAGS (scm_class_extended_generic_with_setter,
                       SCM_CLASSF_PURE_GENERIC);
@@ -2424,8 +2483,9 @@ create_standard_classes (void)
               scm_list_2 (scm_class_accessor,
                           scm_class_extended_generic_with_setter),
               SCM_EOL);
+  /* <extended-generic> is misplaced.  */
   fix_cpl (scm_class_extended_accessor,
-          scm_class_extended_generic, scm_class_generic);
+          scm_class_extended_generic, scm_class_generic_with_setter);
   SCM_SET_CLASS_FLAGS (scm_class_extended_accessor, SCM_CLASSF_PURE_GENERIC);
 
   /* Primitive types classes */
@@ -2465,6 +2525,10 @@ create_standard_classes (void)
               scm_class_class, scm_class_top,             SCM_EOL);
   make_stdcls (&class_uvec,               "<uvec>",
               scm_class_class, class_bytevector,          SCM_EOL);
+  make_stdcls (&class_array,              "<array>",
+              scm_class_class, scm_class_top,             SCM_EOL);
+  make_stdcls (&class_bitvector,           "<bitvector>",
+              scm_class_class, scm_class_top,             SCM_EOL);
   make_stdcls (&scm_class_number,         "<number>",
               scm_class_class, scm_class_top,             SCM_EOL);
   make_stdcls (&scm_class_complex,        "<complex>",
@@ -2504,30 +2568,25 @@ create_standard_classes (void)
 static SCM
 make_class_from_template (char const *template, char const *type_name, SCM supers, int applicablep)
 {
-  SCM class, name;
+  SCM name;
   if (type_name)
     {
       char buffer[100];
       sprintf (buffer, template, type_name);
-      name = scm_from_locale_symbol (buffer);
+      name = scm_from_utf8_symbol (buffer);
     }
   else
     name = SCM_GOOPS_UNBOUND;
 
-  class = scm_basic_make_class (applicablep ? scm_class_procedure_class : scm_class_class,
-                                name, supers, SCM_EOL);
-
-  /* Only define name if doesn't already exist. */
-  if (!SCM_GOOPS_UNBOUNDP (name)
-      && scm_is_false (scm_module_variable (scm_module_goops, name)))
-    DEFVAR (name, class);
-  return class;
+  return scm_basic_make_class (applicablep ? scm_class_procedure_class : scm_class_class,
+                               name, supers, SCM_EOL);
 }
 
 static SCM
 make_class_from_symbol (SCM type_name_sym, SCM supers, int applicablep)
 {
-  SCM class, name;
+  SCM name;
+
   if (scm_is_true (type_name_sym))
     {
       name = scm_string_append (scm_list_3 (scm_from_locale_string ("<"),
@@ -2538,14 +2597,8 @@ make_class_from_symbol (SCM type_name_sym, SCM supers, int applicablep)
   else
     name = SCM_GOOPS_UNBOUND;
 
-  class = scm_basic_make_class (applicablep ? scm_class_procedure_class : scm_class_class,
-                                name, supers, SCM_EOL);
-
-  /* Only define name if doesn't already exist. */
-  if (!SCM_GOOPS_UNBOUNDP (name)
-      && scm_is_false (scm_module_variable (scm_module_goops, name)))
-    DEFVAR (name, class);
-  return class;
+  return scm_basic_make_class (applicablep ? scm_class_procedure_class : scm_class_class,
+                               name, supers, SCM_EOL);
 }
 
 SCM
@@ -2653,7 +2706,7 @@ create_port_classes (void)
 {
   long i;
 
-  for (i = 0; i < scm_numptob; ++i)
+  for (i = scm_c_num_port_types () - 1; i >= 0; i--)
     scm_make_port_classes (i, SCM_PTOBNAME (i));
 }
 
@@ -2775,7 +2828,7 @@ scm_init_goops_builtins (void)
     SCM name = scm_from_latin1_symbol ("no-applicable-method");
     scm_no_applicable_method =
       scm_make (scm_list_3 (scm_class_generic, k_name, name));
-    DEFVAR (name, scm_no_applicable_method);
+    scm_module_define (scm_module_goops, name, scm_no_applicable_method);
   }
 
   return SCM_UNSPECIFIED;