module-local-variable optimization
[bpt/guile.git] / libguile / modules.c
index 689510c..6c3d0e8 100644 (file)
@@ -1,18 +1,19 @@
-/* Copyright (C) 1998,2000,2001,2002,2003,2004,2006,2007,2008 Free Software Foundation, Inc.
+/* Copyright (C) 1998,2000,2001,2002,2003,2004,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
  *
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3 of
+ * the License, or (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
  */
 
 
@@ -41,12 +42,29 @@ int scm_module_system_booted_p = 0;
 
 scm_t_bits scm_module_tag;
 
+/* The current module, a fluid. */
 static SCM the_module;
 
+/* Most of the module system is implemented in Scheme. These bindings from
+   boot-9 are needed to provide the Scheme interface. */
 static SCM the_root_module_var;
+static SCM module_make_local_var_x_var;
+static SCM process_define_module_var;
+static SCM process_use_modules_var;
+static SCM resolve_module_var;
+static SCM module_public_interface_var;
+static SCM module_export_x_var;
+static SCM default_duplicate_binding_procedures_var;
 
-static SCM
-the_root_module ()
+
+static SCM unbound_variable (const char *func, SCM sym)
+{
+  scm_error (scm_from_locale_symbol ("unbound-variable"), func,
+             "Unbound variable: ~S", scm_list_1 (sym), SCM_BOOL_F);
+}
+
+SCM
+scm_the_root_module (void)
 {
   if (scm_module_system_booted_p)
     return SCM_VARIABLE_REF (the_root_module_var);
@@ -61,7 +79,7 @@ SCM_DEFINE (scm_current_module, "current-module", 0, 0, 0,
 {
   SCM curr = scm_fluid_ref (the_module);
 
-  return scm_is_true (curr) ? curr : the_root_module ();
+  return scm_is_true (curr) ? curr : scm_the_root_module ();
 }
 #undef FUNC_NAME
 
@@ -142,10 +160,6 @@ convert_module_name (const char *name)
   return list;
 }
 
-static SCM process_define_module_var;
-static SCM process_use_modules_var;
-static SCM resolve_module_var;
-
 SCM
 scm_c_resolve_module (const char *name)
 {
@@ -176,8 +190,6 @@ scm_c_use_module (const char *name)
              scm_list_1 (scm_list_1 (convert_module_name (name))));
 }
 
-static SCM module_export_x_var;
-
 SCM
 scm_module_export (SCM module, SCM namelist)
 {
@@ -222,55 +234,36 @@ scm_c_export (const char *name, ...)
 
 /* Environments */
 
-SCM
-scm_top_level_env (SCM thunk)
-{
-  if (SCM_IMP (thunk))
-    return SCM_EOL;
-  else
-    return scm_cons (thunk, SCM_EOL);
-}
-
-SCM
-scm_env_top_level (SCM env)
-{
-  while (scm_is_pair (env))
-    {
-      SCM car_env = SCM_CAR (env);
-      if (!scm_is_pair (car_env) && scm_is_true (scm_procedure_p (car_env)))
-       return car_env;
-      env = SCM_CDR (env);
-    }
-  return SCM_BOOL_F;
-}
-
 SCM_SYMBOL (sym_module, "module");
 
 SCM
 scm_lookup_closure_module (SCM proc)
 {
   if (scm_is_false (proc))
-    return the_root_module ();
+    return scm_the_root_module ();
   else if (SCM_EVAL_CLOSURE_P (proc))
     return SCM_PACK (SCM_SMOB_DATA (proc));
   else
     {
-      SCM mod = scm_procedure_property (proc, sym_module);
+      SCM mod;
+
+      /* FIXME: The `module' property is no longer set on eval closures, as it
+        introduced a circular reference that precludes garbage collection of
+        modules with the current weak hash table semantics (see
+        http://lists.gnu.org/archive/html/guile-devel/2009-01/msg00102.html and
+        http://thread.gmane.org/gmane.comp.programming.garbage-collection.boehmgc/2465
+        for details). Since it doesn't appear to be used (only in this
+        function, which has 1 caller), we no longer extend
+        `set-module-eval-closure!' to set the `module' property. */
+      abort ();
+
+      mod = scm_procedure_property (proc, sym_module);
       if (scm_is_false (mod))
-       mod = the_root_module ();
+       mod = scm_the_root_module ();
       return mod;
     }
 }
 
-SCM_DEFINE (scm_env_module, "env-module", 1, 0, 0,
-           (SCM env),
-           "Return the module of @var{ENV}, a lexical environment.")
-#define FUNC_NAME s_scm_env_module
-{
-  return scm_lookup_closure_module (scm_env_top_level (env));
-}
-#undef FUNC_NAME
-
 /*
  * C level implementation of the standard eval closure
  *
@@ -279,12 +272,6 @@ SCM_DEFINE (scm_env_module, "env-module", 1, 0, 0,
  * release.
  */
 
-/* The `module-make-local-var!' variable.  */
-static SCM module_make_local_var_x_var = SCM_UNSPECIFIED;
-
-/* The `default-duplicate-binding-procedures' variable.  */
-static SCM default_duplicate_binding_procedures_var = SCM_UNSPECIFIED;
-
 /* Return the list of default duplicate binding handlers (procedures).  */
 static inline SCM
 default_duplicate_binding_handlers (void)
@@ -425,19 +412,34 @@ SCM_DEFINE (scm_module_local_variable, "module-local-variable", 2, 0, 0,
   if (SCM_BOUND_THING_P (b))
     return b;
 
-  /* 2. Search imported bindings.  In order to be consistent with
-     `module-variable', the binder gets called only when no imported binding
-     matches SYM.  */
-  b = module_imported_variable (module, sym);
-  if (SCM_BOUND_THING_P (b))
-    return SCM_BOOL_F;
+  /* At this point we should just be able to return #f, but there is the
+     possibility that a custom binder establishes a mapping for this
+     variable.
+
+     However a custom binder should be called only if there is no
+     imported binding with the name SYM. So here instead of the order:
+
+       2. Search imported bindings.  In order to be consistent with
+          `module-variable', the binder gets called only when no
+          imported binding matches SYM.
+  
+       3. Query the custom binder.
+
+     we first check if there is a binder at all, and if not, just return
+     #f directly.
+  */
 
   {
-    /* 3. Query the custom binder.  */
     SCM binder = SCM_MODULE_BINDER (module);
 
     if (scm_is_true (binder))
       {
+        /* 2. */
+        b = module_imported_variable (module, sym);
+        if (SCM_BOUND_THING_P (b))
+          return SCM_BOOL_F;
+
+        /* 3. */
        b = scm_call_3 (binder, module, sym, SCM_BOOL_F);
        if (SCM_BOUND_THING_P (b))
          return b;
@@ -501,9 +503,9 @@ SCM_DEFINE (scm_module_variable, "module-variable", 2, 0, 0,
 
 scm_t_bits scm_tc16_eval_closure;
 
-#define SCM_F_EVAL_CLOSURE_INTERFACE (1<<16)
+#define SCM_F_EVAL_CLOSURE_INTERFACE (1<<0)
 #define SCM_EVAL_CLOSURE_INTERFACE_P(e) \
-  (SCM_CELL_WORD_0 (e) & SCM_F_EVAL_CLOSURE_INTERFACE)
+  (SCM_SMOB_FLAGS (e) & SCM_F_EVAL_CLOSURE_INTERFACE)
 
 /* NOTE: This function may be called by a smob application
    or from another C function directly. */
@@ -539,7 +541,7 @@ SCM_DEFINE (scm_standard_interface_eval_closure,
            "Such a closure does not allow new bindings to be added.")
 #define FUNC_NAME s_scm_standard_interface_eval_closure
 {
-  SCM_RETURN_NEWSMOB (scm_tc16_eval_closure | SCM_F_EVAL_CLOSURE_INTERFACE,
+  SCM_RETURN_NEWSMOB (scm_tc16_eval_closure | (SCM_F_EVAL_CLOSURE_INTERFACE<<16),
                      SCM_UNPACK (module));
 }
 #undef FUNC_NAME
@@ -577,23 +579,29 @@ scm_current_module_lookup_closure ()
     return SCM_BOOL_F;
 }
 
-SCM_SYMBOL (sym_sys_pre_modules_transformer, "%pre-modules-transformer");
+SCM_SYMBOL (sym_macroexpand, "macroexpand");
 
-SCM
-scm_module_transformer (SCM module)
+SCM_DEFINE (scm_module_transformer, "module-transformer", 1, 0, 0,
+           (SCM module),
+           "Returns the syntax expander for the given module.")
+#define FUNC_NAME s_scm_module_transformer
 {
   if (SCM_UNLIKELY (scm_is_false (module)))
-    { SCM v = scm_hashq_ref (scm_pre_modules_obarray,
-                             sym_sys_pre_modules_transformer,
+    {
+      SCM v = scm_hashq_ref (scm_pre_modules_obarray,
+                             sym_macroexpand,
                              SCM_BOOL_F);
       if (scm_is_false (v))
-        return SCM_BOOL_F;
-      else
-        return SCM_VARIABLE_REF (v);
+        SCM_MISC_ERROR ("no module, and `macroexpand' unbound", SCM_EOL);
+      return SCM_VARIABLE_REF (v);
     }
   else
-    return SCM_MODULE_TRANSFORMER (module);
+    {
+      SCM_VALIDATE_MODULE (SCM_ARG1, module);
+      return SCM_MODULE_TRANSFORMER (module);
+    }
 }
+#undef FUNC_NAME
 
 SCM
 scm_current_module_transformer ()
@@ -644,24 +652,11 @@ SCM_DEFINE (scm_module_import_interface, "module-import-interface", 2, 0, 0,
 }
 #undef FUNC_NAME
 
-SCM_SYMBOL (sym_sys_module_public_interface, "%module-public-interface");
-
-SCM_DEFINE (scm_module_public_interface, "module-public-interface", 1, 0, 0,
-           (SCM module),
-           "Return the public interface of @var{module}.\n\n"
-            "If @var{module} has no public interface, @code{#f} is returned.")
-#define FUNC_NAME s_scm_module_public_interface
+SCM
+scm_module_public_interface (SCM module)
 {
-  SCM var;
-
-  SCM_VALIDATE_MODULE (1, module);
-  var = scm_module_local_variable (module, sym_sys_module_public_interface);
-  if (scm_is_true (var))
-    return SCM_VARIABLE_REF (var);
-  else
-    return SCM_BOOL_F;
+  return scm_call_1 (SCM_VARIABLE_REF (module_public_interface_var), module);
 }
-#undef FUNC_NAME
 
 /* scm_sym2var
  *
@@ -733,7 +728,7 @@ scm_module_lookup (SCM module, SCM sym)
 
   var = scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F);
   if (scm_is_false (var))
-    SCM_MISC_ERROR ("unbound variable: ~S", scm_list_1 (sym));
+    unbound_variable (FUNC_NAME, sym);
   return var;
 }
 #undef FUNC_NAME
@@ -750,7 +745,7 @@ scm_lookup (SCM sym)
   SCM var = 
     scm_sym2var (sym, scm_current_module_lookup_closure (), SCM_BOOL_F);
   if (scm_is_false (var))
-    scm_misc_error ("scm_lookup", "unbound variable: ~S", scm_list_1 (sym));
+    unbound_variable (NULL, sym);
   return var;
 }
 
@@ -779,14 +774,20 @@ scm_c_define (const char *name, SCM value)
   return scm_define (scm_from_locale_symbol (name), value);
 }
 
-SCM
-scm_define (SCM sym, SCM value)
+SCM_DEFINE (scm_define, "define!", 2, 0, 0,
+           (SCM sym, SCM value),
+           "Define @var{sym} to be @var{value} in the current module."
+            "Returns the variable itself. Note that this is a procedure, "
+            "not a macro.")
+#define FUNC_NAME s_scm_define
 {
-  SCM var =
-    scm_sym2var (sym, scm_current_module_lookup_closure (), SCM_BOOL_T);
+  SCM var;
+  SCM_VALIDATE_SYMBOL (SCM_ARG1, sym);
+  var = scm_sym2var (sym, scm_current_module_lookup_closure (), SCM_BOOL_T);
   SCM_VARIABLE_SET (var, value);
   return var;
 }
+#undef FUNC_NAME
 
 SCM_DEFINE (scm_module_reverse_lookup, "module-reverse-lookup", 2, 0, 0,
            (SCM module, SCM variable),
@@ -807,6 +808,8 @@ SCM_DEFINE (scm_module_reverse_lookup, "module-reverse-lookup", 2, 0, 0,
       obarray = SCM_MODULE_OBARRAY (module);
     }
 
+  SCM_VALIDATE_VARIABLE (SCM_ARG2, variable);
+
   if (!SCM_HASHTABLE_P (obarray))
       return SCM_BOOL_F;
 
@@ -820,23 +823,34 @@ SCM_DEFINE (scm_module_reverse_lookup, "module-reverse-lookup", 2, 0, 0,
       while (!scm_is_null (ls))
        {
          handle = SCM_CAR (ls);
-         if (SCM_CDR (handle) == variable)
-           return SCM_CAR (handle);
+
+         if (SCM_CAR (handle) == SCM_PACK (NULL))
+           {
+             /* FIXME: We hit a weak pair whose car has become unreachable.
+                We should remove the pair in question or something.  */
+           }
+         else
+           {
+             if (SCM_CDR (handle) == variable)
+               return SCM_CAR (handle);
+           }
+
          ls = SCM_CDR (ls);
        }
     }
 
-  /* Try the `uses' list.  */
-  {
-    SCM uses = SCM_MODULE_USES (module);
-    while (scm_is_pair (uses))
-      {
-       SCM sym = scm_module_reverse_lookup (SCM_CAR (uses), variable);
-       if (scm_is_true (sym))
-         return sym;
-       uses = SCM_CDR (uses);
-      }
-  }
+  if (!scm_is_false (module))
+    {
+      /* Try the `uses' list.  */
+      SCM uses = SCM_MODULE_USES (module);
+      while (scm_is_pair (uses))
+       {
+         SCM sym = scm_module_reverse_lookup (SCM_CAR (uses), variable);
+         if (scm_is_true (sym))
+           return sym;
+         uses = SCM_CDR (uses);
+       }
+    }
 
   return SCM_BOOL_F;
 }
@@ -855,23 +869,10 @@ SCM_DEFINE (scm_get_pre_modules_obarray, "%get-pre-modules-obarray", 0, 0, 0,
 
 SCM_SYMBOL (scm_sym_system_module, "system-module");
 
-SCM
-scm_system_module_env_p (SCM env)
-{
-  SCM proc = scm_env_top_level (env);
-  if (scm_is_false (proc))
-    return SCM_BOOL_T;
-  return ((scm_is_true (scm_procedure_property (proc,
-                                               scm_sym_system_module)))
-         ? SCM_BOOL_T
-         : SCM_BOOL_F);
-}
-
 void
 scm_modules_prehistory ()
 {
-  scm_pre_modules_obarray 
-    = scm_permanent_object (scm_c_make_hash_table (1533));
+  scm_pre_modules_obarray = scm_c_make_hash_table (1533);
 }
 
 void
@@ -881,27 +882,25 @@ scm_init_modules ()
   module_make_local_var_x_var = scm_c_define ("module-make-local-var!",
                                            SCM_UNDEFINED);
   scm_tc16_eval_closure = scm_make_smob_type ("eval-closure", 0);
-  scm_set_smob_mark (scm_tc16_eval_closure, scm_markcdr);
   scm_set_smob_apply (scm_tc16_eval_closure, scm_eval_closure_lookup, 2, 0, 0);
 
-  the_module = scm_permanent_object (scm_make_fluid ());
+  the_module = scm_make_fluid ();
 }
 
 static void
 scm_post_boot_init_modules ()
 {
-#define PERM(x) scm_permanent_object(x)
-
   SCM module_type = SCM_VARIABLE_REF (scm_c_lookup ("module-type"));
   scm_module_tag = (SCM_CELL_WORD_1 (module_type) + scm_tc3_struct);
 
-  resolve_module_var = PERM (scm_c_lookup ("resolve-module"));
-  process_define_module_var = PERM (scm_c_lookup ("process-define-module"));
-  process_use_modules_var = PERM (scm_c_lookup ("process-use-modules"));
-  module_export_x_var = PERM (scm_c_lookup ("module-export!"));
-  the_root_module_var = PERM (scm_c_lookup ("the-root-module"));
-  default_duplicate_binding_procedures_var =
-    PERM (scm_c_lookup ("default-duplicate-binding-procedures"));
+  resolve_module_var = scm_c_lookup ("resolve-module");
+  process_define_module_var = scm_c_lookup ("process-define-module");
+  process_use_modules_var = scm_c_lookup ("process-use-modules");
+  module_export_x_var = scm_c_lookup ("module-export!");
+  the_root_module_var = scm_c_lookup ("the-root-module");
+  default_duplicate_binding_procedures_var = 
+    scm_c_lookup ("default-duplicate-binding-procedures");
+  module_public_interface_var = scm_c_lookup ("module-public-interface");
 
   scm_module_system_booted_p = 1;
 }