Deprecate C interfaces scm_compute_applicable_methods, scm_find_method
[bpt/guile.git] / libguile / goops.c
index 4a2e24d..4028456 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998,1999,2000,2001,2002,2003,2004,2008,2009,2010,2011,2012
+/* Copyright (C) 1998,1999,2000,2001,2002,2003,2004,2008,2009,2010,2011,2012,2013,2014,2015
  * Free Software Foundation, Inc.
  *
  * This library is free software; you can redistribute it and/or
@@ -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
@@ -163,8 +155,6 @@ static SCM class_hashtable;
 static SCM class_fluid;
 static SCM class_dynamic_state;
 static SCM class_frame;
-static SCM class_objcode;
-static SCM class_vm;
 static SCM class_vm_cont;
 static SCM class_bytevector;
 static SCM class_uvec;
@@ -172,7 +162,6 @@ 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
@@ -200,17 +189,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))
     {
@@ -229,9 +216,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;
@@ -279,10 +264,8 @@ SCM_DEFINE (scm_class_of, "class-of", 1, 0, 0,
          return class_dynamic_state;
         case scm_tc7_frame:
          return class_frame;
-        case scm_tc7_objcode:
-         return class_objcode;
-        case scm_tc7_vm:
-         return class_vm;
+        case scm_tc7_keyword:
+         return scm_class_keyword;
         case scm_tc7_vm_cont:
          return class_vm_cont;
        case scm_tc7_bytevector:
@@ -659,7 +642,7 @@ SCM_DEFINE (scm_sys_initialize_object, "%initialize-object", 2, 0, 0,
        get_n_set = SCM_CDR (get_n_set), slots = SCM_CDR (slots))
     {
       SCM slot_name  = SCM_CAR (slots);
-      SCM slot_value = SCM_PACK (0);
+      SCM slot_value = SCM_GOOPS_UNBOUND;
 
       if (!scm_is_null (SCM_CDR (slot_name)))
        {
@@ -683,12 +666,12 @@ SCM_DEFINE (scm_sys_initialize_object, "%initialize-object", 2, 0, 0,
              slot_value = scm_i_get_keyword (tmp,
                                              initargs,
                                              n_initargs,
-                                             SCM_PACK (0),
+                                             SCM_GOOPS_UNBOUND,
                                              FUNC_NAME);
            }
        }
 
-      if (SCM_UNPACK (slot_value))
+      if (!SCM_GOOPS_UNBOUNDP (slot_value))
        /* set slot to provided value */
        set_slot_value (class, obj, SCM_CAR (get_n_set), slot_value);
       else
@@ -696,14 +679,10 @@ SCM_DEFINE (scm_sys_initialize_object, "%initialize-object", 2, 0, 0,
          /* set slot to its :init-form if it exists */
          tmp = SCM_CADAR (get_n_set);
          if (scm_is_true (tmp))
-           {
-             slot_value = get_slot_value (class, obj, SCM_CAR (get_n_set));
-             if (SCM_GOOPS_UNBOUNDP (slot_value))
-                set_slot_value (class,
-                                obj,
-                                SCM_CAR (get_n_set),
-                                scm_call_0 (tmp));
-           }
+            set_slot_value (class,
+                            obj,
+                            SCM_CAR (get_n_set),
+                            scm_call_0 (tmp));
        }
     }
 
@@ -934,7 +913,6 @@ 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");
 
 
@@ -969,7 +947,6 @@ build_class_class_slots (void)
     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);
 }
@@ -1000,21 +977,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));
@@ -1732,36 +1709,7 @@ 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 delayed_compile_var;
 
@@ -1913,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
@@ -1930,13 +1919,6 @@ setup_extended_primitive_generics ()
  *
  ******************************************************************************/
 
-static int
-applicablep (SCM actual, SCM formal)
-{
-  /* We already know that the cpl is well formed. */
-  return scm_is_true (scm_c_memq (formal, SCM_SLOT (actual, scm_si_cpl)));
-}
-
 static int
 more_specificp (SCM m1, SCM m2, SCM const *targs)
 {
@@ -1976,179 +1958,6 @@ more_specificp (SCM m1, SCM m2, SCM const *targs)
   return 0; /* should not occur! */
 }
 
-#define BUFFSIZE 32            /* big enough for most uses */
-
-static SCM
-scm_i_vector2list (SCM l, long len)
-{
-  long j;
-  SCM z = scm_c_make_vector (len, SCM_UNDEFINED);
-
-  for (j = 0; j < len; j++, l = SCM_CDR (l)) {
-    SCM_SIMPLE_VECTOR_SET (z, j, SCM_CAR (l));
-  }
-  return z;
-}
-
-static SCM
-sort_applicable_methods (SCM method_list, long size, SCM const *targs)
-{
-  long i, j, incr;
-  SCM *v, vector = SCM_EOL;
-  SCM buffer[BUFFSIZE];
-  SCM save = method_list;
-  scm_t_array_handle handle;
-
-  /* For reasonably sized method_lists we can try to avoid all the
-   * consing and reorder the list in place...
-   * This idea is due to David McClain <Dave_McClain@msn.com>
-   */
-  if (size <= BUFFSIZE)
-    {
-      for (i = 0;  i < size; i++)
-       {
-         buffer[i]   = SCM_CAR (method_list);
-         method_list = SCM_CDR (method_list);
-       }
-      v = buffer;
-    }
-  else
-    {
-      /* Too many elements in method_list to keep everything locally */
-      vector = scm_i_vector2list (save, size);
-      v = scm_vector_writable_elements (vector, &handle, NULL, NULL);
-    }
-
-  /* Use a simple shell sort since it is generally faster than qsort on
-   * small vectors (which is probably mostly the case when we have to
-   * sort a list of applicable methods).
-   */
-  for (incr = size / 2; incr; incr /= 2)
-    {
-      for (i = incr; i < size; i++)
-       {
-         for (j = i - incr; j >= 0; j -= incr)
-           {
-             if (more_specificp (v[j], v[j+incr], targs))
-               break;
-             else
-               {
-                 SCM tmp = v[j + incr];
-                 v[j + incr] = v[j];
-                 v[j] = tmp;
-               }
-           }
-       }
-    }
-
-  if (size <= BUFFSIZE)
-    {
-      /* We did it in locally, so restore the original list (reordered) in-place */
-      for (i = 0, method_list = save; i < size; i++, v++)
-       {
-         SCM_SETCAR (method_list, *v);
-         method_list = SCM_CDR (method_list);
-       }
-      return save;
-    }
-
-  /* If we are here, that's that we did it the hard way... */
-  scm_array_handle_release (&handle);
-  return scm_vector_to_list (vector);
-}
-
-SCM
-scm_compute_applicable_methods (SCM gf, SCM args, long len, int find_method_p)
-{
-  register long i;
-  long count = 0;
-  SCM l, fl, applicable = SCM_EOL;
-  SCM save = args;
-  SCM buffer[BUFFSIZE];
-  SCM const *types;
-  SCM *p;
-  SCM tmp = SCM_EOL;
-  scm_t_array_handle handle;
-
-  /* Build the list of arguments types */
-  if (len >= BUFFSIZE) 
-    {
-      tmp = scm_c_make_vector (len, SCM_UNDEFINED);
-      types = p = scm_vector_writable_elements (tmp, &handle, NULL, NULL);
-
-    /*
-      note that we don't have to work to reset the generation
-      count. TMP is a new vector anyway, and it is found
-      conservatively.
-    */
-    }
-  else
-    types = p = buffer;
-
-  for (  ; !scm_is_null (args); args = SCM_CDR (args))
-    *p++ = scm_class_of (SCM_CAR (args));
-  
-  /* Build a list of all applicable methods */
-  for (l = scm_generic_function_methods (gf); !scm_is_null (l); l = SCM_CDR (l))
-    {
-      fl = SPEC_OF (SCM_CAR (l));
-      for (i = 0; ; i++, fl = SCM_CDR (fl))
-       {
-         if (SCM_INSTANCEP (fl)
-             /* We have a dotted argument list */
-             || (i >= len && scm_is_null (fl)))
-           {   /* both list exhausted */
-             applicable = scm_cons (SCM_CAR (l), applicable);
-             count     += 1;
-             break;
-           }
-         if (i >= len
-             || scm_is_null (fl)
-             || !applicablep (types[i], SCM_CAR (fl)))
-           break;
-       }
-    }
-
-  if (len >= BUFFSIZE)
-      scm_array_handle_release (&handle);
-
-  if (count == 0)
-    {
-      if (find_method_p)
-       return SCM_BOOL_F;
-      scm_call_2 (SCM_VARIABLE_REF (var_no_applicable_method), gf, save);
-      /* if we are here, it's because no-applicable-method hasn't signaled an error */
-      return SCM_BOOL_F;
-    }
-
-  return (count == 1
-         ? applicable
-         : sort_applicable_methods (applicable, count, types));
-}
-
-#if 0
-SCM_PROC (s_sys_compute_applicable_methods, "%compute-applicable-methods", 2, 0, 0, scm_sys_compute_applicable_methods);
-#endif
-
-static const char s_sys_compute_applicable_methods[] = "%compute-applicable-methods";
-
-SCM
-scm_sys_compute_applicable_methods (SCM gf, SCM args)
-#define FUNC_NAME s_sys_compute_applicable_methods
-{
-  long n;
-  SCM_VALIDATE_GENERIC (1, gf);
-  n = scm_ilength (args);
-  SCM_ASSERT (n >= 0, args, SCM_ARG2, FUNC_NAME);
-  return scm_compute_applicable_methods (gf, args, n, 1);
-}
-#undef FUNC_NAME
-
-SCM_SYMBOL (sym_compute_applicable_methods, "compute-applicable-methods");
-SCM_VARIABLE_INIT (var_compute_applicable_methods, "compute-applicable-methods",
-                   scm_c_define_gsubr (s_sys_compute_applicable_methods, 2, 0, 0,
-                                       scm_sys_compute_applicable_methods));
-
 /******************************************************************************
  *
  * A simple make (which will be redefined later in Scheme)
@@ -2275,26 +2084,6 @@ SCM_DEFINE (scm_make, "make",  0, 0, 1,
 }
 #undef FUNC_NAME
 
-SCM_DEFINE (scm_find_method, "find-method", 0, 0, 1,
-           (SCM l),
-           "")
-#define FUNC_NAME s_scm_find_method
-{
-  SCM gf;
-  long len = scm_ilength (l);
-
-  if (len == 0)
-    SCM_WRONG_NUM_ARGS ();
-
-  gf = SCM_CAR(l); l = SCM_CDR(l);
-  SCM_VALIDATE_GENERIC (1, gf);
-  if (scm_is_null (SCM_SLOT (gf, scm_si_methods)))
-    SCM_MISC_ERROR ("no methods for generic ~S", scm_list_1 (gf));
-
-  return scm_compute_applicable_methods (gf, l, len - 1, 1);
-}
-#undef FUNC_NAME
-
 SCM_DEFINE (scm_sys_method_more_specific_p, "%method-more-specific?", 3, 0, 0,
            (SCM m1, SCM m2, SCM targs),
            "Return true if method @var{m1} is more specific than @var{m2} "
@@ -2374,12 +2163,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);
 }
 
 
@@ -2526,10 +2315,6 @@ create_standard_classes (void)
               scm_class_class, scm_class_top,             SCM_EOL);
   make_stdcls (&class_frame,              "<frame>",
               scm_class_class, scm_class_top,             SCM_EOL);
-  make_stdcls (&class_objcode,            "<objcode>",
-              scm_class_class, scm_class_top,             SCM_EOL);
-  make_stdcls (&class_vm,                 "<vm>",
-              scm_class_class, scm_class_top,             SCM_EOL);
   make_stdcls (&class_vm_cont,            "<vm-continuation>",
               scm_class_class, scm_class_top,             SCM_EOL);
   make_stdcls (&class_bytevector,         "<bytevector>",
@@ -2579,30 +2364,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 ("<"),
@@ -2613,14 +2393,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
@@ -2687,8 +2461,6 @@ create_smob_classes (void)
   for (i = 0; i < SCM_I_MAX_SMOB_TYPE_COUNT; ++i)
     scm_smob_class[i] = SCM_BOOL_F;
 
-  scm_smob_class[SCM_TC2SMOBNUM (scm_tc16_keyword)] = scm_class_keyword;
-
   for (i = 0; i < scm_numsmob; ++i)
     if (scm_is_false (scm_smob_class[i]))
       scm_smob_class[i] = scm_make_extended_class (SCM_SMOBNAME (i),
@@ -2728,7 +2500,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));
 }
 
@@ -2817,8 +2589,6 @@ SCM_DEFINE (scm_sys_goops_loaded, "%goops-loaded", 0, 0, 0,
 #define FUNC_NAME s_scm_sys_goops_loaded
 {
   goops_loaded_p = 1;
-  var_compute_applicable_methods =
-    scm_module_variable (scm_module_goops, sym_compute_applicable_methods);
   var_slot_unbound =
     scm_module_variable (scm_module_goops, sym_slot_unbound);
   var_slot_missing =
@@ -2858,7 +2628,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;