aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Burdette <bburdette@protonmail.com>2022-05-19 10:48:10 -0600
committerBen Burdette <bburdette@protonmail.com>2022-05-19 10:48:10 -0600
commit357fb84dbaad0b056704915c6a43764cda63ee7f (patch)
treeb6dbcfeb43b8090d46b8f33621c0ae89fd929590
parent86ba0a702c63b4a8ff79a07f9303318feb330642 (diff)
use an expr->StaticEnv table in evalState
-rw-r--r--src/libcmd/command.cc5
-rw-r--r--src/libcmd/repl.cc9
-rw-r--r--src/libexpr/eval.cc8
-rw-r--r--src/libexpr/eval.hh12
-rw-r--r--src/libexpr/nixexpr.cc77
-rw-r--r--src/libexpr/nixexpr.hh7
6 files changed, 65 insertions, 53 deletions
diff --git a/src/libcmd/command.cc b/src/libcmd/command.cc
index bf97a3de8..b8c3b0846 100644
--- a/src/libcmd/command.cc
+++ b/src/libcmd/command.cc
@@ -136,8 +136,9 @@ ref<EvalState> EvalCommand::getEvalState()
if (error)
printError("%s\n\n" ANSI_BOLD "Starting REPL to allow you to inspect the current state of the evaluator.\n" ANSI_NORMAL, error->what());
- if (expr.staticEnv) {
- auto vm = mapStaticEnvBindings(evalState->symbols, *expr.staticEnv.get(), env);
+ auto se = evalState->getStaticEnv(expr);
+ if (se) {
+ auto vm = mapStaticEnvBindings(evalState->symbols, *se.get(), env);
runRepl(evalState, *vm);
}
};
diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc
index deac3d408..43c2ce65e 100644
--- a/src/libcmd/repl.cc
+++ b/src/libcmd/repl.cc
@@ -441,8 +441,9 @@ void NixRepl::loadDebugTraceEnv(DebugTrace & dt)
{
initEnv();
- if (dt.expr.staticEnv) {
- auto vm = mapStaticEnvBindings(state->symbols, *dt.expr.staticEnv.get(), dt.env);
+ auto se = state->getStaticEnv(dt.expr);
+ if (se) {
+ auto vm = mapStaticEnvBindings(state->symbols, *se.get(), dt.env);
// add staticenv vars.
for (auto & [name, value] : *(vm.get()))
@@ -516,7 +517,7 @@ bool NixRepl::processLine(std::string line)
else if (debuggerHook && (command == ":env")) {
for (const auto & [idx, i] : enumerate(state->debugTraces)) {
if (idx == debugTraceIndex) {
- printEnvBindings(state->symbols, i.expr, i.env);
+ printEnvBindings(*state, i.expr, i.env);
break;
}
}
@@ -533,7 +534,7 @@ bool NixRepl::processLine(std::string line)
std::cout << "\n" << ANSI_BLUE << idx << ANSI_NORMAL << ": ";
showDebugTrace(std::cout, state->positions, i);
std::cout << std::endl;
- printEnvBindings(state->symbols, i.expr, i.env);
+ printEnvBindings(*state, i.expr, i.env);
loadDebugTraceEnv(i);
break;
}
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index d9ea92cc0..c9e4a05b7 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -772,12 +772,12 @@ void printEnvBindings(const SymbolTable & st, const StaticEnv & se, const Env &
}
}
-// TODO: add accompanying env for With stuff.
-void printEnvBindings(const SymbolTable & st, const Expr & expr, const Env & env)
+void printEnvBindings(const EvalState &es, const Expr & expr, const Env & env)
{
// just print the names for now
- if (expr.staticEnv)
- printEnvBindings(st, *expr.staticEnv.get(), env, 0);
+ auto se = es.getStaticEnv(expr);
+ if (se)
+ printEnvBindings(es.symbols, *se, env, 0);
}
void mapStaticEnvBindings(const SymbolTable & st, const StaticEnv & se, const Env & env, ValMap & vm)
diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh
index f274278be..0543cb35c 100644
--- a/src/libexpr/eval.hh
+++ b/src/libexpr/eval.hh
@@ -25,7 +25,7 @@ enum RepairFlag : bool;
typedef void (* PrimOpFun) (EvalState & state, const PosIdx pos, Value * * args, Value & v);
-void printEnvBindings(const SymbolTable & st, const Expr & expr, const Env & env);
+void printEnvBindings(const EvalState &es, const Expr & expr, const Env & env);
void printEnvBindings(const SymbolTable & st, const StaticEnv & se, const Env & env, int lvl = 0);
struct PrimOp
@@ -130,6 +130,16 @@ public:
bool debugStop;
bool debugQuit;
std::list<DebugTrace> debugTraces;
+ std::map<const Expr*, const std::shared_ptr<const StaticEnv> > exprEnvs;
+ const std::shared_ptr<const StaticEnv> getStaticEnv(const Expr &expr) const
+ {
+ auto i = exprEnvs.find(&expr);
+ if (i != exprEnvs.end())
+ return i->second;
+ else
+ return std::shared_ptr<const StaticEnv>();;
+ }
+
template<class E>
[[gnu::noinline, gnu::noreturn]]
diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc
index 213cf93fa..4fb43707e 100644
--- a/src/libexpr/nixexpr.cc
+++ b/src/libexpr/nixexpr.cc
@@ -296,38 +296,39 @@ std::string showAttrPath(const SymbolTable & symbols, const AttrPath & attrPath)
/* Computing levels/displacements for variables. */
-void Expr::bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env)
+void Expr::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{
abort();
}
-void ExprInt::bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env)
+void ExprInt::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{
if (debuggerHook)
- staticEnv = env;}
+ es.exprEnvs.insert(std::make_pair(this, env));
+}
-void ExprFloat::bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env)
+void ExprFloat::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{
if (debuggerHook)
- staticEnv = env;
+ es.exprEnvs.insert(std::make_pair(this, env));
}
-void ExprString::bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env)
+void ExprString::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{
if (debuggerHook)
- staticEnv = env;
+ es.exprEnvs.insert(std::make_pair(this, env));
}
-void ExprPath::bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env)
+void ExprPath::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{
if (debuggerHook)
- staticEnv = env;
+ es.exprEnvs.insert(std::make_pair(this, env));
}
-void ExprVar::bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env)
+void ExprVar::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{
if (debuggerHook)
- staticEnv = env;
+ es.exprEnvs.insert(std::make_pair(this, env));
/* Check whether the variable appears in the environment. If so,
set its level and displacement. */
@@ -360,10 +361,10 @@ void ExprVar::bindVars(const EvalState & es, const std::shared_ptr<const StaticE
this->level = withLevel;
}
-void ExprSelect::bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env)
+void ExprSelect::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{
if (debuggerHook)
- staticEnv = env;
+ es.exprEnvs.insert(std::make_pair(this, env));
e->bindVars(es, env);
if (def) def->bindVars(es, env);
@@ -372,10 +373,10 @@ void ExprSelect::bindVars(const EvalState & es, const std::shared_ptr<const Stat
i.expr->bindVars(es, env);
}
-void ExprOpHasAttr::bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env)
+void ExprOpHasAttr::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{
if (debuggerHook)
- staticEnv = env;
+ es.exprEnvs.insert(std::make_pair(this, env));
e->bindVars(es, env);
for (auto & i : attrPath)
@@ -383,10 +384,10 @@ void ExprOpHasAttr::bindVars(const EvalState & es, const std::shared_ptr<const S
i.expr->bindVars(es, env);
}
-void ExprAttrs::bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env)
+void ExprAttrs::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{
if (debuggerHook)
- staticEnv = env;
+ es.exprEnvs.insert(std::make_pair(this, env));
if (recursive) {
auto newEnv = std::make_shared<StaticEnv>(false, env.get(), recursive ? attrs.size() : 0);
@@ -416,19 +417,19 @@ void ExprAttrs::bindVars(const EvalState & es, const std::shared_ptr<const Stati
}
}
-void ExprList::bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env)
+void ExprList::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{
if (debuggerHook)
- staticEnv = env;
+ es.exprEnvs.insert(std::make_pair(this, env));
for (auto & i : elems)
i->bindVars(es, env);
}
-void ExprLambda::bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env)
+void ExprLambda::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{
if (debuggerHook)
- staticEnv = env;
+ es.exprEnvs.insert(std::make_pair(this, env));
auto newEnv = std::make_shared<StaticEnv>(
false, env.get(),
@@ -452,20 +453,20 @@ void ExprLambda::bindVars(const EvalState & es, const std::shared_ptr<const Stat
body->bindVars(es, newEnv);
}
-void ExprCall::bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env)
+void ExprCall::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{
if (debuggerHook)
- staticEnv = env;
+ es.exprEnvs.insert(std::make_pair(this, env));
fun->bindVars(es, env);
for (auto e : args)
e->bindVars(es, env);
}
-void ExprLet::bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env)
+void ExprLet::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{
if (debuggerHook)
- staticEnv = env;
+ es.exprEnvs.insert(std::make_pair(this, env));
auto newEnv = std::make_shared<StaticEnv>(false, env.get(), attrs->attrs.size());
@@ -481,10 +482,10 @@ void ExprLet::bindVars(const EvalState & es, const std::shared_ptr<const StaticE
body->bindVars(es, newEnv);
}
-void ExprWith::bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env)
+void ExprWith::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{
if (debuggerHook)
- staticEnv = env;
+ es.exprEnvs.insert(std::make_pair(this, env));
/* Does this `with' have an enclosing `with'? If so, record its
level so that `lookupVar' can look up variables in the previous
@@ -499,53 +500,53 @@ void ExprWith::bindVars(const EvalState & es, const std::shared_ptr<const Static
}
if (debuggerHook)
- staticEnv = env;
+ es.exprEnvs.insert(std::make_pair(this, env));
attrs->bindVars(es, env);
auto newEnv = std::make_shared<StaticEnv>(true, env.get());
body->bindVars(es, newEnv);
}
-void ExprIf::bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env)
+void ExprIf::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{
if (debuggerHook)
- staticEnv = env;
+ es.exprEnvs.insert(std::make_pair(this, env));
cond->bindVars(es, env);
then->bindVars(es, env);
else_->bindVars(es, env);
}
-void ExprAssert::bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env)
+void ExprAssert::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{
if (debuggerHook)
- staticEnv = env;
+ es.exprEnvs.insert(std::make_pair(this, env));
cond->bindVars(es, env);
body->bindVars(es, env);
}
-void ExprOpNot::bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env)
+void ExprOpNot::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{
if (debuggerHook)
- staticEnv = env;
+ es.exprEnvs.insert(std::make_pair(this, env));
e->bindVars(es, env);
}
-void ExprConcatStrings::bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env)
+void ExprConcatStrings::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{
if (debuggerHook)
- staticEnv = env;
+ es.exprEnvs.insert(std::make_pair(this, env));
for (auto & i : *this->es)
i.second->bindVars(es, env);
}
-void ExprPos::bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env)
+void ExprPos::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{
if (debuggerHook)
- staticEnv = env;
+ es.exprEnvs.insert(std::make_pair(this, env));
}
diff --git a/src/libexpr/nixexpr.hh b/src/libexpr/nixexpr.hh
index c4a509f31..e923f36a4 100644
--- a/src/libexpr/nixexpr.hh
+++ b/src/libexpr/nixexpr.hh
@@ -144,18 +144,17 @@ struct Expr
{
virtual ~Expr() { };
virtual void show(const SymbolTable & symbols, std::ostream & str) const;
- virtual void bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env);
+ virtual void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env);
virtual void eval(EvalState & state, Env & env, Value & v);
virtual Value * maybeThunk(EvalState & state, Env & env);
virtual void setName(Symbol name);
- std::shared_ptr<const StaticEnv> staticEnv;
virtual PosIdx getPos() const { return noPos; }
};
#define COMMON_METHODS \
void show(const SymbolTable & symbols, std::ostream & str) const; \
void eval(EvalState & state, Env & env, Value & v); \
- void bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env);
+ void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env);
struct ExprInt : Expr
{
@@ -402,7 +401,7 @@ struct ExprOpNot : Expr
{ \
str << "("; e1->show(symbols, str); str << " " s " "; e2->show(symbols, str); str << ")"; \
} \
- void bindVars(const EvalState & es, const std::shared_ptr<const StaticEnv> & env) \
+ void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env) \
{ \
e1->bindVars(es, env); e2->bindVars(es, env); \
} \