Skip to content

Commit

Permalink
Merge pull request #4355 from Infinisil/private-value-type
Browse files Browse the repository at this point in the history
Refactoring for private Value type
  • Loading branch information
edolstra authored Dec 21, 2020
2 parents ec3e202 + b70d22b commit ec4a5c5
Show file tree
Hide file tree
Showing 19 changed files with 464 additions and 377 deletions.
2 changes: 1 addition & 1 deletion src/libexpr/attr-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ std::pair<Value *, Pos> findAlongAttrPath(EvalState & state, const string & attr

if (apType == apAttr) {

if (v->type != tAttrs)
if (v->type() != nAttrs)
throw TypeError(
"the expression selected by the selection path '%1%' should be a set but is %2%",
attrPath,
Expand Down
4 changes: 1 addition & 3 deletions src/libexpr/attr-set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ void EvalState::mkAttrs(Value & v, size_t capacity)
v = vEmptySet;
return;
}
clearValue(v);
v.type = tAttrs;
v.attrs = allocBindings(capacity);
v.mkAttrs(allocBindings(capacity));
nrAttrsets++;
nrAttrsInAttrsets += capacity;
}
Expand Down
26 changes: 13 additions & 13 deletions src/libexpr/eval-cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -390,14 +390,14 @@ Value & AttrCursor::forceValue()
}

if (root->db && (!cachedValue || std::get_if<placeholder_t>(&cachedValue->second))) {
if (v.type == tString)
if (v.type() == nString)
cachedValue = {root->db->setString(getKey(), v.string.s, v.string.context),
string_t{v.string.s, {}}};
else if (v.type == tPath)
else if (v.type() == nPath)
cachedValue = {root->db->setString(getKey(), v.path), v.path};
else if (v.type == tBool)
else if (v.type() == nBool)
cachedValue = {root->db->setBool(getKey(), v.boolean), v.boolean};
else if (v.type == tAttrs)
else if (v.type() == nAttrs)
; // FIXME: do something?
else
cachedValue = {root->db->setMisc(getKey()), misc_t()};
Expand Down Expand Up @@ -442,7 +442,7 @@ std::shared_ptr<AttrCursor> AttrCursor::maybeGetAttr(Symbol name, bool forceErro

auto & v = forceValue();

if (v.type != tAttrs)
if (v.type() != nAttrs)
return nullptr;
//throw TypeError("'%s' is not an attribute set", getAttrPathStr());

Expand Down Expand Up @@ -512,10 +512,10 @@ std::string AttrCursor::getString()

auto & v = forceValue();

if (v.type != tString && v.type != tPath)
throw TypeError("'%s' is not a string but %s", getAttrPathStr(), showType(v.type));
if (v.type() != nString && v.type() != nPath)
throw TypeError("'%s' is not a string but %s", getAttrPathStr(), showType(v.type()));

return v.type == tString ? v.string.s : v.path;
return v.type() == nString ? v.string.s : v.path;
}

string_t AttrCursor::getStringWithContext()
Expand Down Expand Up @@ -543,12 +543,12 @@ string_t AttrCursor::getStringWithContext()

auto & v = forceValue();

if (v.type == tString)
if (v.type() == nString)
return {v.string.s, v.getContext()};
else if (v.type == tPath)
else if (v.type() == nPath)
return {v.path, {}};
else
throw TypeError("'%s' is not a string but %s", getAttrPathStr(), showType(v.type));
throw TypeError("'%s' is not a string but %s", getAttrPathStr(), showType(v.type()));
}

bool AttrCursor::getBool()
Expand All @@ -567,7 +567,7 @@ bool AttrCursor::getBool()

auto & v = forceValue();

if (v.type != tBool)
if (v.type() != nBool)
throw TypeError("'%s' is not a Boolean", getAttrPathStr());

return v.boolean;
Expand All @@ -589,7 +589,7 @@ std::vector<Symbol> AttrCursor::getAttrs()

auto & v = forceValue();

if (v.type != tAttrs)
if (v.type() != nAttrs)
throw TypeError("'%s' is not an attribute set", getAttrPathStr());

std::vector<Symbol> attrs;
Expand Down
16 changes: 7 additions & 9 deletions src/libexpr/eval-inline.hh
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,37 @@ LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const char * s, const

void EvalState::forceValue(Value & v, const Pos & pos)
{
if (v.type == tThunk) {
if (v.isThunk()) {
Env * env = v.thunk.env;
Expr * expr = v.thunk.expr;
try {
v.type = tBlackhole;
v.mkBlackhole();
//checkInterrupt();
expr->eval(*this, *env, v);
} catch (...) {
v.type = tThunk;
v.thunk.env = env;
v.thunk.expr = expr;
v.mkThunk(env, expr);
throw;
}
}
else if (v.type == tApp)
else if (v.isApp())
callFunction(*v.app.left, *v.app.right, v, noPos);
else if (v.type == tBlackhole)
else if (v.isBlackhole())
throwEvalError(pos, "infinite recursion encountered");
}


inline void EvalState::forceAttrs(Value & v)
{
forceValue(v);
if (v.type != tAttrs)
if (v.type() != nAttrs)
throwTypeError("value is %1% while a set was expected", v);
}


inline void EvalState::forceAttrs(Value & v, const Pos & pos)
{
forceValue(v, pos);
if (v.type != tAttrs)
if (v.type() != nAttrs)
throwTypeError(pos, "value is %1% while a set was expected", v);
}

Expand Down
Loading

0 comments on commit ec4a5c5

Please sign in to comment.