about summary refs log tree commit diff
path: root/src/fix-ng/fix-expr.cc
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2003-11-03T20·30+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2003-11-03T20·30+0000
commit0690c1c9c01dd5889dbfccf2da6cb99f5c4e151b (patch)
treec388986ad9539ecf0a238fabfec07873e553021f /src/fix-ng/fix-expr.cc
parentff3132427839888933c3779844bf35ca9e189cb9 (diff)
* Work around problems with the ATerm library.
  The ATerm library doesn't search the heap for pointers to ATerms
  when garbage collecting.  As a result, C++ containers such as
  `map<ATerm, ATerm>' will cause pointer to be hidden from the garbage
  collector, causing crashes.  Instead, we now use ATermTables.

Diffstat (limited to 'src/fix-ng/fix-expr.cc')
-rw-r--r--src/fix-ng/fix-expr.cc122
1 files changed, 103 insertions, 19 deletions
diff --git a/src/fix-ng/fix-expr.cc b/src/fix-ng/fix-expr.cc
index 6333595c63..814e186b41 100644
--- a/src/fix-ng/fix-expr.cc
+++ b/src/fix-ng/fix-expr.cc
@@ -2,6 +2,91 @@
 #include "expr.hh"
 
 
+ATermMap::ATermMap(unsigned int initialSize, unsigned int maxLoadPct)
+{
+    table = ATtableCreate(initialSize, maxLoadPct);
+    if (!table) throw Error("cannot create ATerm table");
+}
+
+
+ATermMap::ATermMap(const ATermMap & map)
+    : table(0)
+{
+    ATermList keys = map.keys();
+
+    /* !!! adjust allocation for load pct */
+    table = ATtableCreate(ATgetLength(keys), map.maxLoadPct);
+    if (!table) throw Error("cannot create ATerm table");
+
+    for (; !ATisEmpty(keys); keys = ATgetNext(keys)) {
+        ATerm key = ATgetFirst(keys);
+        set(key, map.get(key));
+    }
+}
+
+
+ATermMap::~ATermMap()
+{
+    if (table) ATtableDestroy(table);
+}
+
+
+void ATermMap::set(ATerm key, ATerm value)
+{
+    return ATtablePut(table, key, value);
+}
+
+
+void ATermMap::set(const string & key, ATerm value)
+{
+    set(string2ATerm(key), value);
+}
+
+
+ATerm ATermMap::get(ATerm key) const
+{
+    return ATtableGet(table, key);
+}
+
+
+ATerm ATermMap::get(const string & key) const
+{
+    return get(string2ATerm(key));
+}
+
+
+void ATermMap::remove(ATerm key)
+{
+    ATtableRemove(table, key);
+}
+
+
+void ATermMap::remove(const string & key)
+{
+    remove(string2ATerm(key));
+}
+
+
+ATermList ATermMap::keys() const
+{
+    ATermList keys = ATtableKeys(table);
+    if (!keys) throw Error("cannot query aterm map keys");
+    return keys;
+}
+
+
+ATerm string2ATerm(const string & s)
+{
+    return (ATerm) ATmakeAppl0(ATmakeAFun((char *) s.c_str(), 0, ATtrue));
+}
+
+
+string aterm2String(ATerm t)
+{
+    return ATgetName(ATgetAFun(t));
+}
+    
+
 ATerm bottomupRewrite(TermFun & f, ATerm e)
 {
     if (ATgetType(e) == AT_APPL) {
@@ -31,7 +116,7 @@ ATerm bottomupRewrite(TermFun & f, ATerm e)
 }
 
 
-void queryAllAttrs(Expr e, Attrs & attrs)
+void queryAllAttrs(Expr e, ATermMap & attrs)
 {
     ATermList bnds;
     if (!ATmatch(e, "Attrs([<list>])", &bnds))
@@ -42,7 +127,7 @@ void queryAllAttrs(Expr e, Attrs & attrs)
         Expr e;
         if (!ATmatch(ATgetFirst(bnds), "Bind(<str>, <term>)", &s, &e))
             abort(); /* can't happen */
-        attrs[s] = e;
+        attrs.set(s, e);
         bnds = ATgetNext(bnds);
     }
 }
@@ -50,33 +135,32 @@ void queryAllAttrs(Expr e, Attrs & attrs)
 
 Expr queryAttr(Expr e, const string & name)
 {
-    Attrs attrs;
+    ATermMap attrs;
     queryAllAttrs(e, attrs);
-    Attrs::iterator i = attrs.find(name);
-    return i == attrs.end() ? 0 : i->second;
+    return attrs.get(name);
 }
 
 
-Expr makeAttrs(const Attrs & attrs)
+Expr makeAttrs(const ATermMap & attrs)
 {
-    ATermList bnds = ATempty;
-    for (Attrs::const_iterator i = attrs.begin(); i != attrs.end(); i++)
+    ATermList bnds = ATempty, keys = attrs.keys();
+    while (!ATisEmpty(keys)) {
+        Expr key = ATgetFirst(keys);
         bnds = ATinsert(bnds, 
-            ATmake("Bind(<str>, <term>)", i->first.c_str(), i->second));
+            ATmake("Bind(<term>, <term>)", key, attrs.get(key)));
+        keys = ATgetNext(keys);
+    }
     return ATmake("Attrs(<term>)", ATreverse(bnds));
 }
 
 
-ATerm substitute(Subs & subs, ATerm e)
+Expr substitute(const ATermMap & subs, Expr e)
 {
     char * s;
 
     if (ATmatch(e, "Var(<str>)", &s)) {
-        Subs::iterator i = subs.find(s);
-        if (i == subs.end())
-            return e;
-        else
-            return i->second;
+        Expr sub = subs.get(s);
+        return sub ? sub : e;
     }
 
     /* In case of a function, filter out all variables bound by this
@@ -84,11 +168,11 @@ ATerm substitute(Subs & subs, ATerm e)
     ATermList formals;
     ATerm body;
     if (ATmatch(e, "Function([<list>], <term>)", &formals, &body)) {
-        Subs subs2(subs);
+        ATermMap subs2(subs);
         ATermList fs = formals;
         while (!ATisEmpty(fs)) {
             if (!ATmatch(ATgetFirst(fs), "<str>", &s)) abort();
-            subs2.erase(s);
+            subs2.remove(s);
             fs = ATgetNext(fs);
         }
         return ATmake("Function(<term>, <term>)", formals,
@@ -98,13 +182,13 @@ ATerm substitute(Subs & subs, ATerm e)
     /* Idem for a mutually recursive attribute set. */
     ATermList bindings;
     if (ATmatch(e, "Rec([<list>])", &bindings)) {
-        Subs subs2(subs);
+        ATermMap subs2(subs);
         ATermList bnds = bindings;
         while (!ATisEmpty(bnds)) {
             Expr e;
             if (!ATmatch(ATgetFirst(bnds), "Bind(<str>, <term>)", &s, &e))
                 abort(); /* can't happen */
-            subs2.erase(s);
+            subs2.remove(s);
             bnds = ATgetNext(bnds);
         }
         return ATmake("Rec(<term>)", substitute(subs2, (ATerm) bindings));