Introduce Value type setters and make use of them

This commit is contained in:
Silvan Mosberger 2020-12-12 00:19:05 +01:00
parent fa307875e9
commit 9f056f7afd
No known key found for this signature in database
GPG key ID: E8F1E9EAD284E17D
5 changed files with 43 additions and 24 deletions

View file

@ -25,7 +25,7 @@ void EvalState::mkAttrs(Value & v, size_t capacity)
return;
}
clearValue(v);
v.type = tAttrs;
v.setAttrs();
v.attrs = allocBindings(capacity);
nrAttrsets++;
nrAttrsInAttrsets += capacity;

View file

@ -36,11 +36,11 @@ void EvalState::forceValue(Value & v, const Pos & pos)
Env * env = v.thunk.env;
Expr * expr = v.thunk.expr;
try {
v.type = tBlackhole;
v.setBlackhole();
//checkInterrupt();
expr->eval(*this, *env, v);
} catch (...) {
v.type = tThunk;
v.setThunk();
v.thunk.env = env;
v.thunk.expr = expr;
throw;

View file

@ -431,7 +431,7 @@ EvalState::EvalState(const Strings & _searchPath, ref<Store> store)
}
clearValue(vEmptySet);
vEmptySet.type = tAttrs;
vEmptySet.setAttrs();
vEmptySet.attrs = allocBindings(0);
createBaseEnv();
@ -548,7 +548,7 @@ Value * EvalState::addPrimOp(const string & name,
the primop to a dummy value. */
if (arity == 0) {
auto vPrimOp = allocValue();
vPrimOp->type = tPrimOp;
vPrimOp->setPrimOp();
vPrimOp->primOp = new PrimOp { .fun = primOp, .arity = 1, .name = sym };
Value v;
mkApp(v, *vPrimOp, *vPrimOp);
@ -556,7 +556,7 @@ Value * EvalState::addPrimOp(const string & name,
}
Value * v = allocValue();
v->type = tPrimOp;
v->setPrimOp();
v->primOp = new PrimOp { .fun = primOp, .arity = arity, .name = sym };
staticBaseEnv.vars[symbols.create(name)] = baseEnvDispl;
baseEnv.values[baseEnvDispl++] = v;
@ -572,7 +572,7 @@ Value * EvalState::addPrimOp(PrimOp && primOp)
if (primOp.arity == 0) {
primOp.arity = 1;
auto vPrimOp = allocValue();
vPrimOp->type = tPrimOp;
vPrimOp->setPrimOp();
vPrimOp->primOp = new PrimOp(std::move(primOp));
Value v;
mkApp(v, *vPrimOp, *vPrimOp);
@ -584,7 +584,7 @@ Value * EvalState::addPrimOp(PrimOp && primOp)
primOp.name = symbols.create(std::string(primOp.name, 2));
Value * v = allocValue();
v->type = tPrimOp;
v->setPrimOp();
v->primOp = new PrimOp(std::move(primOp));
staticBaseEnv.vars[envName] = baseEnvDispl;
baseEnv.values[baseEnvDispl++] = v;
@ -714,7 +714,7 @@ void mkString(Value & v, const char * s)
Value & mkString(Value & v, std::string_view s, const PathSet & context)
{
v.type = tString;
v.setString();
v.string.s = dupStringWithLen(s.data(), s.size());
v.string.context = 0;
if (!context.empty()) {
@ -794,11 +794,11 @@ void EvalState::mkList(Value & v, size_t size)
{
clearValue(v);
if (size == 1)
v.type = tList1;
v.setList1();
else if (size == 2)
v.type = tList2;
v.setList2();
else {
v.type = tListN;
v.setListN();
v.bigList.size = size;
v.bigList.elems = size ? (Value * *) allocBytes(size * sizeof(Value *)) : 0;
}
@ -810,7 +810,7 @@ unsigned long nrThunks = 0;
static inline void mkThunk(Value & v, Env & env, Expr * expr)
{
v.type = tThunk;
v.setThunk();
v.thunk.env = &env;
v.thunk.expr = expr;
nrThunks++;
@ -1207,7 +1207,7 @@ void ExprOpHasAttr::eval(EvalState & state, Env & env, Value & v)
void ExprLambda::eval(EvalState & state, Env & env, Value & v)
{
v.type = tLambda;
v.setLambda();
v.lambda.env = &env;
v.lambda.fun = this;
}
@ -1252,7 +1252,7 @@ void EvalState::callPrimOp(Value & fun, Value & arg, Value & v, const Pos & pos)
} else {
Value * fun2 = allocValue();
*fun2 = fun;
v.type = tPrimOpApp;
v.setPrimOpApp();
v.primOpApp.left = fun2;
v.primOpApp.right = &arg;
}

View file

@ -107,6 +107,25 @@ std::ostream & operator << (std::ostream & str, const ExternalValueBase & v);
struct Value
{
ValueType type;
inline void setInt() { type = tInt; };
inline void setBool() { type = tBool; };
inline void setString() { type = tString; };
inline void setPath() { type = tPath; };
inline void setNull() { type = tNull; };
inline void setAttrs() { type = tAttrs; };
inline void setList1() { type = tList1; };
inline void setList2() { type = tList2; };
inline void setListN() { type = tListN; };
inline void setThunk() { type = tThunk; };
inline void setApp() { type = tApp; };
inline void setLambda() { type = tLambda; };
inline void setBlackhole() { type = tBlackhole; };
inline void setPrimOp() { type = tPrimOp; };
inline void setPrimOpApp() { type = tPrimOpApp; };
inline void setExternal() { type = tExternal; };
inline void setFloat() { type = tFloat; };
union
{
NixInt integer;
@ -223,7 +242,7 @@ static inline void clearValue(Value & v)
static inline void mkInt(Value & v, NixInt n)
{
clearValue(v);
v.type = tInt;
v.setInt();
v.integer = n;
}
@ -231,7 +250,7 @@ static inline void mkInt(Value & v, NixInt n)
static inline void mkFloat(Value & v, NixFloat n)
{
clearValue(v);
v.type = tFloat;
v.setFloat();
v.fpoint = n;
}
@ -239,7 +258,7 @@ static inline void mkFloat(Value & v, NixFloat n)
static inline void mkBool(Value & v, bool b)
{
clearValue(v);
v.type = tBool;
v.setBool();
v.boolean = b;
}
@ -247,13 +266,13 @@ static inline void mkBool(Value & v, bool b)
static inline void mkNull(Value & v)
{
clearValue(v);
v.type = tNull;
v.setNull();
}
static inline void mkApp(Value & v, Value & left, Value & right)
{
v.type = tApp;
v.setApp();
v.app.left = &left;
v.app.right = &right;
}
@ -261,7 +280,7 @@ static inline void mkApp(Value & v, Value & left, Value & right)
static inline void mkPrimOpApp(Value & v, Value & left, Value & right)
{
v.type = tPrimOpApp;
v.setPrimOpApp();
v.app.left = &left;
v.app.right = &right;
}
@ -269,7 +288,7 @@ static inline void mkPrimOpApp(Value & v, Value & left, Value & right)
static inline void mkStringNoCopy(Value & v, const char * s)
{
v.type = tString;
v.setString();
v.string.s = s;
v.string.context = 0;
}
@ -287,7 +306,7 @@ void mkString(Value & v, const char * s);
static inline void mkPathNoCopy(Value & v, const char * s)
{
clearValue(v);
v.type = tPath;
v.setPath();
v.path = s;
}

View file

@ -551,7 +551,7 @@ bool NixRepl::processLine(string line)
{
Expr * e = parseString(string(line, p + 1));
Value & v(*state->allocValue());
v.type = tThunk;
v.setThunk();
v.thunk.env = env;
v.thunk.expr = e;
addVarToScope(state->symbols.create(name), v);