Print the faulty object upon invalid-keyword errors.
[bpt/guile.git] / libguile / srcprop.c
index f9b000c..c632bb0 100644 (file)
@@ -1,4 +1,5 @@
-/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002, 2006, 2008, 2009, 2010, 2011 Free Software Foundation
+/* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2006,
+ *   2008, 2009, 2010, 2011, 2012 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
@@ -38,6 +39,8 @@
 
 #include "libguile/validate.h"
 #include "libguile/srcprop.h"
+#include "libguile/private-options.h"
+
 \f
 /* {Source Properties}
  *
@@ -57,8 +60,9 @@ SCM_GLOBAL_SYMBOL (scm_sym_filename, "filename");
 SCM_GLOBAL_SYMBOL (scm_sym_copy, "copy");
 SCM_GLOBAL_SYMBOL (scm_sym_line, "line");
 SCM_GLOBAL_SYMBOL (scm_sym_column, "column");
-SCM scm_source_whash;
 
+static SCM scm_source_whash;
+static scm_i_pthread_mutex_t source_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
 
 
 /*
@@ -90,6 +94,14 @@ static SCM scm_srcprops_to_alist (SCM obj);
 
 scm_t_bits scm_tc16_srcprops;
 
+
+static int
+supports_source_props (SCM obj)
+{
+  return SCM_NIMP (obj) && !scm_is_symbol (obj) && !scm_is_keyword (obj);
+}
+
+
 static int
 srcprops_print (SCM obj, SCM port, scm_print_state *pstate)
 {
@@ -125,23 +137,23 @@ scm_make_srcprops (long line, int col, SCM filename, SCM copy, SCM alist)
        thread safety.
        */
       SCM last_acons = SCM_CDR (scm_last_alist_filename);
-      if (old_alist == SCM_EOL
-         && SCM_CDAR (last_acons) == filename)
+      if (scm_is_null (old_alist)
+         && scm_is_eq (SCM_CDAR (last_acons), filename))
        {
          alist = last_acons;
        }
       else
        {
          alist = scm_acons (scm_sym_filename, filename, alist);
-         if (old_alist == SCM_EOL)
+         if (scm_is_null (old_alist))
            SCM_SETCDR (scm_last_alist_filename, alist);
        }
     }
   
   SCM_RETURN_NEWSMOB3 (scm_tc16_srcprops,
                       SRCPROPMAKPOS (line, col),
-                      copy,
-                      alist);
+                      SCM_UNPACK (copy),
+                      SCM_UNPACK (alist));
 }
 
 
@@ -156,19 +168,37 @@ scm_srcprops_to_alist (SCM obj)
   return alist;
 }
 
+SCM_DEFINE (scm_supports_source_properties_p, "supports-source-properties?", 1, 0, 0,
+            (SCM obj),
+            "Return #t if @var{obj} supports adding source properties,\n"
+            "otherwise return #f.")
+#define FUNC_NAME s_scm_supports_source_properties_p
+{
+  return scm_from_bool (supports_source_props (obj));
+}
+#undef FUNC_NAME
+
 SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0, 
             (SCM obj),
            "Return the source property association list of @var{obj}.")
 #define FUNC_NAME s_scm_source_properties
 {
-  SCM p;
-  SCM_VALIDATE_NIM (1, obj);
-  p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
-  if (SRCPROPSP (p))
-    return scm_srcprops_to_alist (p);
+  if (SCM_IMP (obj))
+    return SCM_EOL;
   else
-    /* list from set-source-properties!, or SCM_EOL for not found */
-    return p;
+    {
+      SCM p;
+
+      scm_i_pthread_mutex_lock (&source_lock);
+      p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
+      scm_i_pthread_mutex_unlock (&source_lock);
+
+      if (SRCPROPSP (p))
+        return scm_srcprops_to_alist (p);
+      else
+        /* list from set-source-properties!, or SCM_EOL for not found */
+        return p;
+    }
 }
 #undef FUNC_NAME
 
@@ -181,36 +211,85 @@ SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0,
 #define FUNC_NAME s_scm_set_source_properties_x
 {
   SCM_VALIDATE_NIM (1, obj);
+
+  scm_i_pthread_mutex_lock (&source_lock);
   scm_hashq_set_x (scm_source_whash, obj, alist);
+  scm_i_pthread_mutex_unlock (&source_lock);
+
   return alist;
 }
 #undef FUNC_NAME
 
+int
+scm_i_has_source_properties (SCM obj)
+#define FUNC_NAME "%set-source-properties"
+{
+  if (SCM_IMP (obj))
+    return 0;
+  else
+    {
+      int ret;
+
+      scm_i_pthread_mutex_lock (&source_lock);
+      ret = scm_is_true (scm_hashq_ref (scm_source_whash, obj, SCM_BOOL_F));
+      scm_i_pthread_mutex_unlock (&source_lock);
+
+      return ret;
+    }
+}
+#undef FUNC_NAME
+  
+
+void
+scm_i_set_source_properties_x (SCM obj, long line, int col, SCM fname)
+#define FUNC_NAME "%set-source-properties"
+{
+  SCM_VALIDATE_NIM (1, obj);
+
+  scm_i_pthread_mutex_lock (&source_lock);
+  scm_hashq_set_x (scm_source_whash, obj,
+                   scm_make_srcprops (line, col, fname,
+                                      SCM_COPY_SOURCE_P
+                                      ? scm_copy_tree (obj)
+                                      : SCM_UNDEFINED,
+                                      SCM_EOL));
+  scm_i_pthread_mutex_unlock (&source_lock);
+}
+#undef FUNC_NAME
+
 SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0,
             (SCM obj, SCM key),
            "Return the source property specified by @var{key} from\n"
            "@var{obj}'s source property list.")
 #define FUNC_NAME s_scm_source_property
 {
-  SCM p;
-  SCM_VALIDATE_NIM (1, obj);
-  p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
-  if (!SRCPROPSP (p))
-    goto alist;
-  if (scm_is_eq (scm_sym_line, key))
-    p = scm_from_int (SRCPROPLINE (p));
-  else if (scm_is_eq (scm_sym_column, key))
-    p = scm_from_int (SRCPROPCOL (p));
-  else if (scm_is_eq (scm_sym_copy, key))
-    p = SRCPROPCOPY (p);
+  if (SCM_IMP (obj))
+    return SCM_BOOL_F;
   else
     {
-      p = SRCPROPALIST (p);
-    alist:
-      p = scm_assoc (key, p);
-      return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F);
+      SCM p;
+
+      scm_i_pthread_mutex_lock (&source_lock);
+      p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
+      scm_i_pthread_mutex_unlock (&source_lock);
+
+      if (!SRCPROPSP (p))
+        goto alist;
+      if (scm_is_eq (scm_sym_line, key))
+        p = scm_from_int (SRCPROPLINE (p));
+      else if (scm_is_eq (scm_sym_column, key))
+        p = scm_from_int (SRCPROPCOL (p));
+      else if (scm_is_eq (scm_sym_copy, key))
+        p = SRCPROPCOPY (p);
+      else
+        {
+          p = SRCPROPALIST (p);
+        alist:
+          p = scm_assoc (key, p);
+          return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F);
+        }
+      return SCM_UNBNDP (p) ? SCM_BOOL_F : p;
     }
-  return SCM_UNBNDP (p) ? SCM_BOOL_F : p;
 }
 #undef FUNC_NAME
 
@@ -222,6 +301,8 @@ SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
 {
   SCM p;
   SCM_VALIDATE_NIM (1, obj);
+
+  scm_i_pthread_mutex_lock (&source_lock);
   p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
 
   if (scm_is_eq (scm_sym_line, key))
@@ -258,6 +339,8 @@ SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
        scm_hashq_set_x (scm_source_whash, obj,
                          scm_acons (key, datum, p));
     }
+  scm_i_pthread_mutex_unlock (&source_lock);
+
   return SCM_UNSPECIFIED;
 }
 #undef FUNC_NAME
@@ -272,10 +355,12 @@ SCM_DEFINE (scm_cons_source, "cons-source", 3, 0, 0,
 {
   SCM p, z;
   z = scm_cons (x, y);
+  scm_i_pthread_mutex_lock (&source_lock);
   /* Copy source properties possibly associated with xorig. */
   p = scm_hashq_ref (scm_source_whash, xorig, SCM_BOOL_F);
   if (scm_is_true (p))
     scm_hashq_set_x (scm_source_whash, z, p);
+  scm_i_pthread_mutex_unlock (&source_lock);
   return z;
 }
 #undef FUNC_NAME