about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorShea Levy <shea@shealevy.com>2013-07-15T21·10-0400
committerEelco Dolstra <eelco.dolstra@logicblox.com>2013-08-26T09·31+0200
commitafc6c1bad63e27d68adf49e673f8aafd36495a8a (patch)
tree9cfe21245d123b5d7e525aa2722b1e155d29e736 /src
parent6cd6ce56083d0077485896a761520812d039bf10 (diff)
Simplify inherited attribute handling
This reduces the difference between inherited and non-inherited
attribute handling to the choice of which env to use (in recs and lets)
by setting the AttrDef::e to a new ExprVar in the parser rather than
carrying a separate AttrDef::v VarRef member.

As an added bonus, this allows inherited attributes that inherit from a
with to delay forcing evaluation of the with's attributes.

Signed-off-by: Shea Levy <shea@shealevy.com>
Diffstat (limited to 'src')
-rw-r--r--src/libexpr/eval.cc40
-rw-r--r--src/libexpr/eval.hh2
-rw-r--r--src/libexpr/nixexpr.cc9
-rw-r--r--src/libexpr/nixexpr.hh6
-rw-r--r--src/libexpr/parser.y2
5 files changed, 21 insertions, 38 deletions
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index 4fecd2a70c63..82287f6271e7 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -521,23 +521,17 @@ void ExprAttrs::eval(EvalState & state, Env & env, Value & v)
            environment, while the inherited attributes are evaluated
            in the original environment. */
         unsigned int displ = 0;
-        foreach (AttrDefs::iterator, i, attrs)
-            if (i->second.inherited) {
-                /* !!! handle overrides? */
-                Value * vAttr = state.lookupVar(&env, i->second.var);
-                env2.values[displ++] = vAttr;
-                v.attrs->push_back(Attr(i->first, vAttr, &i->second.pos));
-            } else {
-                Value * vAttr;
-                if (hasOverrides) {
-                    vAttr = state.allocValue();
-                    mkThunk(*vAttr, env2, i->second.e);
-                } else
-                    vAttr = i->second.e->maybeThunk(state, env2);
-                env2.values[displ++] = vAttr;
-                v.attrs->push_back(Attr(i->first, vAttr, &i->second.pos));
-            }
-        
+        foreach (AttrDefs::iterator, i, attrs) {
+            Value * vAttr;
+            if (hasOverrides && !i->second.inherited) {
+                vAttr = state.allocValue();
+                mkThunk(*vAttr, env2, i->second.e);
+            } else
+                vAttr = i->second.e->maybeThunk(state, i->second.inherited ? env : env2);
+            env2.values[displ++] = vAttr;
+            v.attrs->push_back(Attr(i->first, vAttr, &i->second.pos));
+        }
+
         /* If the rec contains an attribute called `__overrides', then
            evaluate it, and add the attributes in that set to the rec.
            This allows overriding of recursive attributes, which is
@@ -563,10 +557,7 @@ void ExprAttrs::eval(EvalState & state, Env & env, Value & v)
 
     else {
         foreach (AttrDefs::iterator, i, attrs)
-            if (i->second.inherited)
-                v.attrs->push_back(Attr(i->first, state.lookupVar(&env, i->second.var), &i->second.pos));
-            else
-                v.attrs->push_back(Attr(i->first, i->second.e->maybeThunk(state, env), &i->second.pos));
+            v.attrs->push_back(Attr(i->first, i->second.e->maybeThunk(state, env), &i->second.pos));
     }
 }
 
@@ -583,10 +574,7 @@ void ExprLet::eval(EvalState & state, Env & env, Value & v)
        environment. */
     unsigned int displ = 0;
     foreach (ExprAttrs::AttrDefs::iterator, i, attrs->attrs)
-        if (i->second.inherited)
-            env2.values[displ++] = state.lookupVar(&env, i->second.var);
-        else
-            env2.values[displ++] = i->second.e->maybeThunk(state, env2);
+        env2.values[displ++] = i->second.e->maybeThunk(state, i->second.inherited ? env : env2);
 
     body->eval(state, env2, v);
 }
@@ -602,7 +590,7 @@ void ExprList::eval(EvalState & state, Env & env, Value & v)
 
 void ExprVar::eval(EvalState & state, Env & env, Value & v)
 {
-    Value * v2 = state.lookupVar(&env, info);
+    Value * v2 = state.lookupVar(&env, info, false);
     state.forceValue(*v2);
     v = *v2;
 }
diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh
index 9717fb66d95d..b7b527bc4e6f 100644
--- a/src/libexpr/eval.hh
+++ b/src/libexpr/eval.hh
@@ -206,7 +206,7 @@ private:
     void addPrimOp(const string & name,
         unsigned int arity, PrimOpFun primOp);
 
-    inline Value * lookupVar(Env * env, const VarRef & var, bool noEval = false);
+    inline Value * lookupVar(Env * env, const VarRef & var, bool noEval);
     
     friend class ExprVar;
     friend class ExprAttrs;
diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc
index 9b75bb644844..72bc0a14bed6 100644
--- a/src/libexpr/nixexpr.cc
+++ b/src/libexpr/nixexpr.cc
@@ -230,14 +230,12 @@ void ExprAttrs::bindVars(const StaticEnv & env)
             newEnv.vars[i->first] = i->second.displ = displ++;
         
         foreach (AttrDefs::iterator, i, attrs)
-            if (i->second.inherited) i->second.var.bind(env);
-            else i->second.e->bindVars(newEnv);
+            i->second.e->bindVars(i->second.inherited ? env : newEnv);
     }
 
     else
         foreach (AttrDefs::iterator, i, attrs)
-            if (i->second.inherited) i->second.var.bind(env);
-            else i->second.e->bindVars(env);
+            i->second.e->bindVars(env);
 }
 
 void ExprList::bindVars(const StaticEnv & env)
@@ -274,8 +272,7 @@ void ExprLet::bindVars(const StaticEnv & env)
         newEnv.vars[i->first] = i->second.displ = displ++;
     
     foreach (ExprAttrs::AttrDefs::iterator, i, attrs->attrs)
-        if (i->second.inherited) i->second.var.bind(env);
-        else i->second.e->bindVars(newEnv);
+        i->second.e->bindVars(i->second.inherited ? env : newEnv);
     
     body->bindVars(newEnv);
 }
diff --git a/src/libexpr/nixexpr.hh b/src/libexpr/nixexpr.hh
index f6dcb3935752..69372a77b64c 100644
--- a/src/libexpr/nixexpr.hh
+++ b/src/libexpr/nixexpr.hh
@@ -159,12 +159,10 @@ struct ExprAttrs : Expr
     bool recursive;
     struct AttrDef {
         bool inherited;
-        Expr * e; // if not inherited
-        VarRef var; // if inherited
+        Expr * e;
         Pos pos;
         unsigned int displ; // displacement
-        AttrDef(Expr * e, const Pos & pos) : inherited(false), e(e), pos(pos) { };
-        AttrDef(const Symbol & name, const Pos & pos) : inherited(true), var(name), pos(pos) { };
+        AttrDef(Expr * e, const Pos & pos, bool inherited=false) : inherited(inherited), e(e), pos(pos) { };
         AttrDef() { };
     };
     typedef std::map<Symbol, AttrDef> AttrDefs;
diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y
index 7690271a86d3..0e3086004271 100644
--- a/src/libexpr/parser.y
+++ b/src/libexpr/parser.y
@@ -413,7 +413,7 @@ binds
           if ($$->attrs.find(*i) != $$->attrs.end())
               dupAttr(*i, makeCurPos(@3, data), $$->attrs[*i].pos);
           Pos pos = makeCurPos(@3, data);
-          $$->attrs[*i] = ExprAttrs::AttrDef(*i, pos);
+          $$->attrs[*i] = ExprAttrs::AttrDef(new ExprVar(*i), pos, true);
       }
     }
   | binds INHERIT '(' expr ')' attrs ';'