about summary refs log tree commit diff
path: root/src/libexpr
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2006-05-04T12·21+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2006-05-04T12·21+0000
commit0832956089516d32371060c98df4f8d0cbff2b0f (patch)
treeff054ab5727324f0de5bdc56460aeafded11f5f5 /src/libexpr
parent9840368cad6088e4221fb323202861c97d70bb37 (diff)
* Use the new ATermMap.
Diffstat (limited to 'src/libexpr')
-rw-r--r--src/libexpr/eval.cc44
-rw-r--r--src/libexpr/get-drvs.cc42
-rw-r--r--src/libexpr/nixexpr.cc130
-rw-r--r--src/libexpr/nixexpr.hh39
-rw-r--r--src/libexpr/parser.cc6
-rw-r--r--src/libexpr/primops.cc33
6 files changed, 71 insertions, 223 deletions
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index 6d2b20e539..38f198fb86 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -4,7 +4,7 @@
 
 
 EvalState::EvalState()
-    : normalForms(32768, 50)
+    : normalForms(32768), primOps(128)
 {
     nrEvaluated = nrCached = 0;
 
@@ -17,7 +17,7 @@ EvalState::EvalState()
 void EvalState::addPrimOp(const string & name,
     unsigned int arity, PrimOp primOp)
 {
-    primOps.set(name, makePrimOpDef(arity, ATmakeBlob(0, (void *) primOp)));
+    primOps.set(toATerm(name), makePrimOpDef(arity, ATmakeBlob(0, (void *) primOp)));
 }
 
 
@@ -25,7 +25,6 @@ void EvalState::addPrimOp(const string & name,
 static Expr substArgs(Expr body, ATermList formals, Expr arg)
 {
     ATermMap subs(ATgetLength(formals) * 2);
-    Expr undefined = makeUndefined();
 
     /* ({x ? E1; y ? E2, z}: E3) {x = E4; z = E5;}
 
@@ -44,7 +43,7 @@ static Expr substArgs(Expr body, ATermList formals, Expr arg)
     for (ATermIterator i(formals); i; ++i) {
         Expr name, def;
         if (matchNoDefFormal(*i, name))
-            subs.set(name, undefined);
+            subs.set(name, makeUndefined());
         else if (matchDefFormal(*i, name, def))
             subs.set(name, def);
         else abort(); /* can't happen */
@@ -52,22 +51,21 @@ static Expr substArgs(Expr body, ATermList formals, Expr arg)
 
     /* Get the actual arguments, and check that they match with the
        formals. */
-    ATermMap args;
+    ATermMap args(128); /* !!! fix */
     queryAllAttrs(arg, args);
-    for (ATermIterator i(args.keys()); i; ++i) {
-        Expr key = *i;
-        Expr cur = subs.get(key);
-        if (!cur)
+    for (ATermMap::const_iterator i = args.begin(); i != args.end(); ++i) {
+        Expr cur = subs.get(i->key);
+        if (!subs.get(i->key))
             throw Error(format("unexpected function argument `%1%'")
-                % aterm2String(key));
-        subs.set(key, args.get(key));
+                % aterm2String(i->key));
+        subs.set(i->key, i->value);
     }
 
     /* Check that all arguments are defined. */
-    for (ATermIterator i(subs.keys()); i; ++i)
-        if (subs.get(*i) == undefined)
+    for (ATermMap::const_iterator i = subs.begin(); i != subs.end(); ++i)
+        if (i->value == makeUndefined())
             throw Error(format("required function argument `%1%' missing")
-                % aterm2String(*i));
+                % aterm2String(i->key));
     
     return substitute(Substitution(0, &subs), body);
 }
@@ -85,7 +83,7 @@ ATerm expandRec(ATerm e, ATermList rbnds, ATermList nrbnds)
     Pos pos;
 
     /* Create the substitution list. */
-    ATermMap subs;
+    ATermMap subs(ATgetLength(rbnds) + ATgetLength(nrbnds));
     for (ATermIterator i(rbnds); i; ++i) {
         if (!matchBind(*i, name, e2, pos)) abort(); /* can't happen */
         subs.set(name, makeSelect(e, name));
@@ -98,7 +96,7 @@ ATerm expandRec(ATerm e, ATermList rbnds, ATermList nrbnds)
     Substitution subs_(0, &subs);
 
     /* Create the non-recursive set. */
-    ATermMap as;
+    ATermMap as(ATgetLength(rbnds) + ATgetLength(nrbnds));
     for (ATermIterator i(rbnds); i; ++i) {
         if (!matchBind(*i, name, e2, pos)) abort(); /* can't happen */
         as.set(name, makeAttrRHS(substitute(subs_, e2), pos));
@@ -118,7 +116,7 @@ static Expr updateAttrs(Expr e1, Expr e2)
 {
     /* Note: e1 and e2 should be in normal form. */
 
-    ATermMap attrs;
+    ATermMap attrs(128); /* !!! */
     queryAllAttrs(e1, attrs, true);
     queryAllAttrs(e2, attrs, true);
 
@@ -217,12 +215,12 @@ string coerceToStringWithContext(EvalState & state,
     }
 
     if (matchAttrs(e, es)) {
-        ATermMap attrs;
+        ATermMap attrs(128); /* !!! */
         queryAllAttrs(e, attrs, false);
 
-        Expr a = attrs.get("type");
+        Expr a = attrs.get(toATerm("type"));
         if (a && evalString(state, a) == "derivation") {
-            a = attrs.get("outPath");
+            a = attrs.get(toATerm("outPath"));
             if (!a) throw Error("output path missing from derivation");
             context = ATinsert(context, e);
             return evalPath(state, a);
@@ -342,7 +340,7 @@ Expr evalExpr2(EvalState & state, Expr e)
         
         else if (matchFunction1(e1, name, e4, pos)) {
             try {
-                ATermMap subs;
+                ATermMap subs(1);
                 subs.set(name, e2);
                 return evalExpr(state, substitute(Substitution(0, &subs), e4));
             } catch (Error & e) {
@@ -392,7 +390,7 @@ Expr evalExpr2(EvalState & state, Expr e)
 
     /* Withs. */
     if (matchWith(e, e1, e2, pos)) {
-        ATermMap attrs;
+        ATermMap attrs(128); /* !!! */
         try {
             e1 = evalExpr(state, e1);
             queryAllAttrs(e1, attrs);
@@ -442,7 +440,7 @@ Expr evalExpr2(EvalState & state, Expr e)
 
     /* Attribute existence test (?). */
     if (matchOpHasAttr(e, e1, name)) {
-        ATermMap attrs;
+        ATermMap attrs(128); /* !!! */
         queryAllAttrs(evalExpr(state, e1), attrs);
         return makeBool(attrs.get(name) != 0);
     }
diff --git a/src/libexpr/get-drvs.cc b/src/libexpr/get-drvs.cc
index 78ccab7f32..19778c6abe 100644
--- a/src/libexpr/get-drvs.cc
+++ b/src/libexpr/get-drvs.cc
@@ -5,7 +5,7 @@
 string DrvInfo::queryDrvPath(EvalState & state) const
 {
     if (drvPath == "") {
-        Expr a = attrs->get("drvPath");
+        Expr a = attrs->get(toATerm("drvPath"));
         (string &) drvPath = a ? evalPath(state, a) : "";
     }
     return drvPath;
@@ -15,7 +15,7 @@ string DrvInfo::queryDrvPath(EvalState & state) const
 string DrvInfo::queryOutPath(EvalState & state) const
 {
     if (outPath == "") {
-        Expr a = attrs->get("outPath");
+        Expr a = attrs->get(toATerm("outPath"));
         if (!a) throw Error("output path missing");
         (string &) outPath = evalPath(state, a);
     }
@@ -27,16 +27,16 @@ MetaInfo DrvInfo::queryMetaInfo(EvalState & state) const
 {
     MetaInfo meta;
     
-    Expr a = attrs->get("meta");
+    Expr a = attrs->get(toATerm("meta"));
     if (!a) return meta; /* fine, empty meta information */
 
-    ATermMap attrs2;
+    ATermMap attrs2(16); /* !!! */
     queryAllAttrs(evalExpr(state, a), attrs2);
 
-    for (ATermIterator i(attrs2.keys()); i; ++i) {
-        ATerm s = coerceToString(evalExpr(state, attrs2.get(*i)));
+    for (ATermMap::const_iterator i = attrs2.begin(); i != attrs2.end(); ++i) {
+        ATerm s = coerceToString(evalExpr(state, i->value));
         if (s)
-            meta[aterm2String(*i)] = aterm2String(s);
+            meta[aterm2String(i->key)] = aterm2String(s);
         /* For future compatibility, ignore attribute values that are
            not strings. */
     }
@@ -66,10 +66,10 @@ static bool getDerivation(EvalState & state, Expr e,
         e = evalExpr(state, e);
         if (!matchAttrs(e, es)) return true;
 
-        shared_ptr<ATermMap> attrs(new ATermMap());
+        shared_ptr<ATermMap> attrs(new ATermMap(32)); /* !!! */
         queryAllAttrs(e, *attrs, false);
     
-        Expr a = attrs->get("type");
+        Expr a = attrs->get(toATerm("type"));
         if (!a || evalString(state, a) != "derivation") return true;
 
         /* Remove spurious duplicates (e.g., an attribute set like
@@ -79,11 +79,11 @@ static bool getDerivation(EvalState & state, Expr e,
 
         DrvInfo drv;
     
-        a = attrs->get("name");
+        a = attrs->get(toATerm("name"));
         if (!a) throw badTerm("derivation name missing", e);
         drv.name = evalString(state, a);
 
-        a = attrs->get("system");
+        a = attrs->get(toATerm("system"));
         if (!a)
             drv.system = "unknown";
         else
@@ -128,7 +128,7 @@ static void getDerivations(EvalState & state, Expr e,
                 abort(); /* can't happen */
         }
         getDerivations(state,
-            makeCall(e, makeAttrs(ATermMap())),
+            makeCall(e, makeAttrs(ATermMap(0))),
             drvs, doneExprs, attrPath);
         return;
     }
@@ -169,30 +169,30 @@ static void getDerivations(EvalState & state, Expr e,
 
     if (matchAttrs(e, es)) {
         if (apType != apNone && apType != apAttr) throw attrError;
-        ATermMap drvMap;
+        ATermMap drvMap(ATgetLength(es));
         queryAllAttrs(e, drvMap);
         if (apType == apNone) {
-            for (ATermIterator i(drvMap.keys()); i; ++i) {
+            for (ATermMap::const_iterator i = drvMap.begin(); i != drvMap.end(); ++i) {
                 startNest(nest, lvlDebug,
-                    format("evaluating attribute `%1%'") % aterm2String(*i));
-                if (getDerivation(state, drvMap.get(*i), drvs, doneExprs)) {
+                    format("evaluating attribute `%1%'") % aterm2String(i->key));
+                if (getDerivation(state, i->value, drvs, doneExprs)) {
                     /* If the value of this attribute is itself an
                        attribute set, should we recurse into it?
                        => Only if it has a `recurseForDerivations = true'
                        attribute. */
                     ATermList es;
-                    Expr e = evalExpr(state, drvMap.get(*i));
+                    Expr e = evalExpr(state, i->value);
                     if (matchAttrs(e, es)) {
-                        ATermMap attrs;
+                        ATermMap attrs(ATgetLength(es));
                         queryAllAttrs(e, attrs, false);
-                        if (attrs.get("recurseForDerivations") &&
-                            evalBool(state, attrs.get("recurseForDerivations")))
+                        Expr e2 = attrs.get(toATerm("recurseForDerivations"));
+                        if (e2 && evalBool(state, e2))
                             getDerivations(state, e, drvs, doneExprs, attrPathRest);
                     }
                 }
             }
         } else {
-            Expr e2 = drvMap.get(attr);
+            Expr e2 = drvMap.get(toATerm(attr));
             if (!e2) throw Error(format("attribute `%1%' in selection path not found") % attr);
             startNest(nest, lvlDebug,
                 format("evaluating attribute `%1%'") % attr);
diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc
index bfa7a9ed76..ebdef96d9c 100644
--- a/src/libexpr/nixexpr.cc
+++ b/src/libexpr/nixexpr.cc
@@ -6,124 +6,6 @@
 #include "nixexpr-ast.cc"
 
 
-ATermMap::ATermMap(unsigned int initialSize, unsigned int maxLoadPct)
-    : table(0)
-{
-    this->maxLoadPct = maxLoadPct;
-    table = ATtableCreate(initialSize, maxLoadPct);
-    if (!table) throw Error("cannot create ATerm table");
-}
-
-
-ATermMap::ATermMap(const ATermMap & map)
-    : table(0)
-{
-    copy(map);
-}
-
-
-ATermMap::~ATermMap()
-{
-    free();
-}
-
-
-ATermMap & ATermMap::operator = (const ATermMap & map)
-{
-    if (this == &map) return *this;
-    free();
-    copy(map);
-    return *this;
-}
-
-
-void ATermMap::free()
-{
-    if (table) {
-        ATtableDestroy(table);
-        table = 0;
-    }
-}
-
-
-void ATermMap::copy(const ATermMap & map)
-{
-    ATermList keys = map.keys();
-
-    /* !!! We adjust for the maximum load pct by allocating twice as
-       much.  Probably a bit too much. */
-    maxLoadPct = map.maxLoadPct;
-    table = ATtableCreate(ATgetLength(keys) * 2, maxLoadPct);
-    if (!table) throw Error("cannot create ATerm table");
-
-    add(map, keys);
-}
-
-
-void ATermMap::set(ATerm key, ATerm value)
-{
-    return ATtablePut(table, key, value);
-}
-
-
-void ATermMap::set(const string & key, ATerm value)
-{
-    set(toATerm(key), value);
-}
-
-
-ATerm ATermMap::get(ATerm key) const
-{
-    return ATtableGet(table, key);
-}
-
-
-ATerm ATermMap::get(const string & key) const
-{
-    return get(toATerm(key));
-}
-
-
-void ATermMap::remove(ATerm key)
-{
-    ATtableRemove(table, key);
-}
-
-
-void ATermMap::remove(const string & key)
-{
-    remove(toATerm(key));
-}
-
-
-ATermList ATermMap::keys() const
-{
-    ATermList keys = ATtableKeys(table);
-    if (!keys) throw Error("cannot query aterm map keys");
-    return keys;
-}
-
-
-void ATermMap::add(const ATermMap & map)
-{
-    ATermList keys = map.keys();
-    add(map, keys);
-}
-
-
-void ATermMap::add(const ATermMap & map, ATermList & keys)
-{
-    for (ATermIterator i(keys); i; ++i)
-        set(*i, map.get(*i));
-}
-
-
-void ATermMap::reset()
-{
-    ATtableReset(table);
-}
-
-
 string showPos(ATerm pos)
 {
     ATerm path;
@@ -211,12 +93,12 @@ Expr queryAttr(Expr e, const string & name, ATerm & pos)
 Expr makeAttrs(const ATermMap & attrs)
 {
     ATermList bnds = ATempty;
-    for (ATermIterator i(attrs.keys()); i; ++i) {
+    for (ATermMap::const_iterator i = attrs.begin(); i != attrs.end(); ++i) {
         Expr e;
         ATerm pos;
-        if (!matchAttrRHS(attrs.get(*i), e, pos))
+        if (!matchAttrRHS(i->value, e, pos))
             abort(); /* can't happen */
-        bnds = ATinsert(bnds, makeBind(*i, e, pos));
+        bnds = ATinsert(bnds, makeBind(i->key, e, pos));
     }
     return makeAttrs(bnds);
 }
@@ -250,7 +132,7 @@ Expr substitute(const Substitution & subs, Expr e)
     ATermList formals;
     ATerm body, def;
     if (matchFunction(e, formals, body, pos)) {
-        ATermMap map;
+        ATermMap map(ATgetLength(formals));
         for (ATermIterator i(formals); i; ++i) {
             if (!matchNoDefFormal(*i, name) &&
                 !matchDefFormal(*i, name, def))
@@ -264,7 +146,7 @@ Expr substitute(const Substitution & subs, Expr e)
     }
 
     if (matchFunction1(e, name, body, pos)) {
-        ATermMap map;
+        ATermMap map(1);
         map.set(name, makeRemoved());
         return makeFunction1(name, substitute(Substitution(&subs, &map), body), pos);
     }
@@ -272,7 +154,7 @@ Expr substitute(const Substitution & subs, Expr e)
     /* Idem for a mutually recursive attribute set. */
     ATermList rbnds, nrbnds;
     if (matchRec(e, rbnds, nrbnds)) {
-        ATermMap map;
+        ATermMap map(ATgetLength(rbnds) + ATgetLength(nrbnds));
         for (ATermIterator i(rbnds); i; ++i)
             if (matchBind(*i, name, e2, pos)) map.set(name, makeRemoved());
             else abort(); /* can't happen */
diff --git a/src/libexpr/nixexpr.hh b/src/libexpr/nixexpr.hh
index 298a9f0b94..e3c7ee6074 100644
--- a/src/libexpr/nixexpr.hh
+++ b/src/libexpr/nixexpr.hh
@@ -5,6 +5,7 @@
 
 #include <aterm2.h>
 
+#include "aterm-map.hh"
 #include "util.hh"
 
 
@@ -16,44 +17,6 @@ typedef ATerm Expr;
 typedef ATerm Pos;
 
 
-/* Mappings from ATerms to ATerms.  This is just a wrapper around
-   ATerm tables. */
-class ATermMap
-{
-private:
-    unsigned int maxLoadPct;
-    ATermTable table;
-    
-public:
-    ATermMap(unsigned int initialSize = 64, unsigned int maxLoadPct = 75);
-    ATermMap(const ATermMap & map);
-    ~ATermMap();
-
-    ATermMap & operator = (const ATermMap & map);
-        
-    void set(ATerm key, ATerm value);
-    void set(const string & key, ATerm value);
-
-    ATerm get(ATerm key) const;
-    ATerm get(const string & key) const;
-
-    void remove(ATerm key);
-    void remove(const string & key);
-
-    ATermList keys() const;
-
-    void add(const ATermMap & map);
-    
-    void reset();
-
-private:
-    void add(const ATermMap & map, ATermList & keys);
-
-    void free();
-    void copy(const ATermMap & map);
-};
-
-
 /* A STL vector of ATerms.  Should be used with great care since it's
    stored on the heap, and the elements are therefore not roots to the
    ATerm garbage collector. */
diff --git a/src/libexpr/parser.cc b/src/libexpr/parser.cc
index 2ca1cab4af..241a437345 100644
--- a/src/libexpr/parser.cc
+++ b/src/libexpr/parser.cc
@@ -115,7 +115,7 @@ static void checkAttrSets(ATerm e)
     ATermList formals;
     ATerm body, pos;
     if (matchFunction(e, formals, body, pos)) {
-        ATermMap names;
+        ATermMap names(ATgetLength(formals));
         for (ATermIterator i(formals); i; ++i) {
             ATerm name;
             Expr deflt;
@@ -131,13 +131,13 @@ static void checkAttrSets(ATerm e)
 
     ATermList bnds;
     if (matchAttrs(e, bnds)) {
-        ATermMap names;
+        ATermMap names(ATgetLength(bnds));
         checkAttrs(names, bnds);
     }
     
     ATermList rbnds, nrbnds;
     if (matchRec(e, rbnds, nrbnds)) {
-        ATermMap names;
+        ATermMap names(ATgetLength(rbnds) + ATgetLength(nrbnds));
         checkAttrs(names, rbnds);
         checkAttrs(names, nrbnds);
     }
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index 8935b147e5..262e3bec54 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -234,11 +234,11 @@ static Expr primDerivationStrict(EvalState & state, const ATermVector & args)
 {
     startNest(nest, lvlVomit, "evaluating derivation");
 
-    ATermMap attrs;
+    ATermMap attrs(128); /* !!! */
     queryAllAttrs(evalExpr(state, args[0]), attrs, true);
 
     /* Figure out the name already (for stack backtraces). */
-    Expr eDrvName = attrs.get("name");
+    Expr eDrvName = attrs.get(toATerm("name"));
     if (!eDrvName)
         throw Error("required attribute `name' missing");
     ATerm posDrvName;
@@ -252,11 +252,11 @@ static Expr primDerivationStrict(EvalState & state, const ATermVector & args)
     string outputHashAlgo;
     bool outputHashRecursive = false;
 
-    for (ATermIterator i(attrs.keys()); i; ++i) {
-        string key = aterm2String(*i);
+    for (ATermMap::const_iterator i = attrs.begin(); i != attrs.end(); ++i) {
+        string key = aterm2String(i->key);
         ATerm value;
         Expr pos;
-        ATerm rhs = attrs.get(key);
+        ATerm rhs = i->value;
         if (!matchAttrRHS(rhs, value, pos)) abort();
         startNest(nest, lvlVomit, format("processing attribute `%1%'") % key);
 
@@ -363,9 +363,11 @@ static Expr primDerivationStrict(EvalState & state, const ATermVector & args)
     state.drvHashes[drvPath] = hashDerivationModulo(state, drv);
 
     /* !!! assumes a single output */
-    ATermMap outAttrs;
-    outAttrs.set("outPath", makeAttrRHS(makePath(toATerm(outPath)), makeNoPos()));
-    outAttrs.set("drvPath", makeAttrRHS(makePath(toATerm(drvPath)), makeNoPos()));
+    ATermMap outAttrs(2);
+    outAttrs.set(toATerm("outPath"),
+        makeAttrRHS(makePath(toATerm(outPath)), makeNoPos()));
+    outAttrs.set(toATerm("drvPath"),
+        makeAttrRHS(makePath(toATerm(drvPath)), makeNoPos()));
 
     return makeAttrs(outAttrs);
 }
@@ -374,15 +376,18 @@ static Expr primDerivationStrict(EvalState & state, const ATermVector & args)
 static Expr primDerivationLazy(EvalState & state, const ATermVector & args)
 {
     Expr eAttrs = evalExpr(state, args[0]);
-    ATermMap attrs;
+    ATermMap attrs(128); /* !!! */
     queryAllAttrs(eAttrs, attrs, true);
 
-    attrs.set("type", makeAttrRHS(makeStr(toATerm("derivation")), makeNoPos()));
+    attrs.set(toATerm("type"),
+        makeAttrRHS(makeStr(toATerm("derivation")), makeNoPos()));
 
     Expr drvStrict = makeCall(makeVar(toATerm("derivation!")), eAttrs);
 
-    attrs.set("outPath", makeAttrRHS(makeSelect(drvStrict, toATerm("outPath")), makeNoPos()));
-    attrs.set("drvPath", makeAttrRHS(makeSelect(drvStrict, toATerm("drvPath")), makeNoPos()));
+    attrs.set(toATerm("outPath"),
+        makeAttrRHS(makeSelect(drvStrict, toATerm("outPath")), makeNoPos()));
+    attrs.set(toATerm("drvPath"),
+        makeAttrRHS(makeSelect(drvStrict, toATerm("drvPath")), makeNoPos()));
     
     return makeAttrs(attrs);
 }
@@ -627,14 +632,14 @@ static Expr primCurrentTime(EvalState & state, const ATermVector & args)
 
 static Expr primRemoveAttrs(EvalState & state, const ATermVector & args)
 {
-    ATermMap attrs;
+    ATermMap attrs(128); /* !!! */
     queryAllAttrs(evalExpr(state, args[0]), attrs, true);
     
     ATermList list = evalList(state, args[1]);
 
     for (ATermIterator i(list); i; ++i)
         /* It's not an error for *i not to exist. */
-        attrs.remove(evalString(state, *i));
+        attrs.remove(toATerm(evalString(state, *i)));
 
     return makeAttrs(attrs);
 }