2003-11-18 14:06:07 +02:00
|
|
|
#include "nixexpr.hh"
|
2003-11-18 13:22:29 +02:00
|
|
|
#include "storeexpr.hh"
|
2003-10-30 18:11:24 +02:00
|
|
|
|
|
|
|
|
2003-11-03 22:30:40 +02:00
|
|
|
ATermMap::ATermMap(unsigned int initialSize, unsigned int maxLoadPct)
|
|
|
|
{
|
2004-01-21 16:49:32 +02:00
|
|
|
this->maxLoadPct = maxLoadPct;
|
2003-11-03 22:30:40 +02:00
|
|
|
table = ATtableCreate(initialSize, maxLoadPct);
|
|
|
|
if (!table) throw Error("cannot create ATerm table");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ATermMap::ATermMap(const ATermMap & map)
|
|
|
|
: table(0)
|
|
|
|
{
|
|
|
|
ATermList keys = map.keys();
|
|
|
|
|
|
|
|
/* !!! adjust allocation for load pct */
|
2004-01-21 16:49:32 +02:00
|
|
|
maxLoadPct = map.maxLoadPct;
|
|
|
|
table = ATtableCreate(ATgetLength(keys), maxLoadPct);
|
2003-11-03 22:30:40 +02:00
|
|
|
if (!table) throw Error("cannot create ATerm table");
|
|
|
|
|
2004-02-04 18:03:29 +02:00
|
|
|
add(map, keys);
|
2003-11-03 22:30:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ATermMap::~ATermMap()
|
|
|
|
{
|
|
|
|
if (table) ATtableDestroy(table);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ATermMap::set(ATerm key, ATerm value)
|
|
|
|
{
|
|
|
|
return ATtablePut(table, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ATermMap::set(const string & key, ATerm value)
|
|
|
|
{
|
|
|
|
set(string2ATerm(key), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ATerm ATermMap::get(ATerm key) const
|
|
|
|
{
|
|
|
|
return ATtableGet(table, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ATerm ATermMap::get(const string & key) const
|
|
|
|
{
|
|
|
|
return get(string2ATerm(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ATermMap::remove(ATerm key)
|
|
|
|
{
|
|
|
|
ATtableRemove(table, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ATermMap::remove(const string & key)
|
|
|
|
{
|
|
|
|
remove(string2ATerm(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ATermList ATermMap::keys() const
|
|
|
|
{
|
|
|
|
ATermList keys = ATtableKeys(table);
|
|
|
|
if (!keys) throw Error("cannot query aterm map keys");
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-04 18:03:29 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-03 22:30:40 +02:00
|
|
|
ATerm string2ATerm(const string & s)
|
|
|
|
{
|
|
|
|
return (ATerm) ATmakeAppl0(ATmakeAFun((char *) s.c_str(), 0, ATtrue));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string aterm2String(ATerm t)
|
|
|
|
{
|
|
|
|
return ATgetName(ATgetAFun(t));
|
|
|
|
}
|
2004-04-06 01:27:41 +03:00
|
|
|
|
|
|
|
|
|
|
|
string showPos(ATerm pos)
|
|
|
|
{
|
|
|
|
ATMatcher m;
|
|
|
|
Path path;
|
|
|
|
int line, column;
|
|
|
|
if (atMatch(m, pos) >> "NoPos")
|
|
|
|
return "undefined position";
|
|
|
|
if (!(atMatch(m, pos) >> "Pos" >> path >> line >> column))
|
|
|
|
throw badTerm("position expected", pos);
|
|
|
|
return (format("`%1%', line %2%") % path % line).str();
|
|
|
|
}
|
2003-11-03 22:30:40 +02:00
|
|
|
|
|
|
|
|
2003-10-30 18:11:24 +02:00
|
|
|
ATerm bottomupRewrite(TermFun & f, ATerm e)
|
|
|
|
{
|
2004-01-15 22:23:55 +02:00
|
|
|
checkInterrupt();
|
|
|
|
|
2003-10-30 18:11:24 +02:00
|
|
|
if (ATgetType(e) == AT_APPL) {
|
|
|
|
AFun fun = ATgetAFun(e);
|
|
|
|
int arity = ATgetArity(fun);
|
|
|
|
ATermList args = ATempty;
|
|
|
|
|
|
|
|
for (int i = arity - 1; i >= 0; i--)
|
|
|
|
args = ATinsert(args, bottomupRewrite(f, ATgetArgument(e, i)));
|
|
|
|
|
2003-11-03 13:59:35 +02:00
|
|
|
e = (ATerm) ATmakeApplList(fun, args);
|
2003-10-30 18:11:24 +02:00
|
|
|
}
|
|
|
|
|
2003-11-03 13:59:35 +02:00
|
|
|
else if (ATgetType(e) == AT_LIST) {
|
2003-10-30 18:11:24 +02:00
|
|
|
ATermList in = (ATermList) e;
|
|
|
|
ATermList out = ATempty;
|
|
|
|
|
2003-11-16 20:31:29 +02:00
|
|
|
for (ATermIterator i(in); i; ++i)
|
|
|
|
out = ATinsert(out, bottomupRewrite(f, *i));
|
2003-10-30 18:11:24 +02:00
|
|
|
|
2003-11-03 13:59:35 +02:00
|
|
|
e = (ATerm) ATreverse(out);
|
2003-10-30 18:11:24 +02:00
|
|
|
}
|
|
|
|
|
2003-11-03 13:59:35 +02:00
|
|
|
return f(e);
|
2003-10-30 18:11:24 +02:00
|
|
|
}
|
2003-10-31 19:09:31 +02:00
|
|
|
|
|
|
|
|
2004-04-06 01:27:41 +03:00
|
|
|
void queryAllAttrs(Expr e, ATermMap & attrs, bool withPos)
|
2003-10-31 19:09:31 +02:00
|
|
|
{
|
2003-11-16 19:46:31 +02:00
|
|
|
ATMatcher m;
|
2003-10-31 19:09:31 +02:00
|
|
|
ATermList bnds;
|
2003-11-16 19:46:31 +02:00
|
|
|
if (!(atMatch(m, e) >> "Attrs" >> bnds))
|
2004-04-06 01:27:41 +03:00
|
|
|
throw Error("attribute set expected");
|
2003-10-31 19:09:31 +02:00
|
|
|
|
2003-11-16 20:31:29 +02:00
|
|
|
for (ATermIterator i(bnds); i; ++i) {
|
2003-11-16 19:46:31 +02:00
|
|
|
string s;
|
2003-10-31 19:09:31 +02:00
|
|
|
Expr e;
|
2004-04-06 01:27:41 +03:00
|
|
|
ATerm pos;
|
|
|
|
if (!(atMatch(m, *i) >> "Bind" >> s >> e >> pos))
|
2003-10-31 19:09:31 +02:00
|
|
|
abort(); /* can't happen */
|
2004-04-06 01:27:41 +03:00
|
|
|
attrs.set(s, withPos ? ATmake("(<term>, <term>)", e, pos) : e);
|
2003-10-31 19:09:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Expr queryAttr(Expr e, const string & name)
|
|
|
|
{
|
2004-04-06 01:27:41 +03:00
|
|
|
ATerm dummy;
|
|
|
|
return queryAttr(e, name, dummy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Expr queryAttr(Expr e, const string & name, ATerm & pos)
|
|
|
|
{
|
|
|
|
ATMatcher m;
|
|
|
|
ATermList bnds;
|
|
|
|
if (!(atMatch(m, e) >> "Attrs" >> bnds))
|
|
|
|
throw Error("attribute set expected");
|
|
|
|
|
|
|
|
for (ATermIterator i(bnds); i; ++i) {
|
|
|
|
string s;
|
|
|
|
Expr e;
|
|
|
|
ATerm pos2;
|
|
|
|
if (!(atMatch(m, *i) >> "Bind" >> s >> e >> pos2))
|
|
|
|
abort(); /* can't happen */
|
|
|
|
if (s == name) {
|
|
|
|
pos = pos2;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2003-10-31 19:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-03 22:30:40 +02:00
|
|
|
Expr makeAttrs(const ATermMap & attrs)
|
2003-10-31 19:09:31 +02:00
|
|
|
{
|
2004-04-06 01:27:41 +03:00
|
|
|
ATMatcher m;
|
2003-11-16 20:31:29 +02:00
|
|
|
ATermList bnds = ATempty;
|
2004-04-06 01:27:41 +03:00
|
|
|
for (ATermIterator i(attrs.keys()); i; ++i) {
|
|
|
|
Expr e;
|
|
|
|
ATerm pos;
|
|
|
|
if (!(atMatch(m, attrs.get(*i)) >> "" >> e >> pos))
|
|
|
|
abort(); /* can't happen */
|
2003-10-31 19:09:31 +02:00
|
|
|
bnds = ATinsert(bnds,
|
2004-04-06 01:27:41 +03:00
|
|
|
ATmake("Bind(<term>, <term>, <term>)", *i, e, pos));
|
|
|
|
}
|
2003-10-31 19:09:31 +02:00
|
|
|
return ATmake("Attrs(<term>)", ATreverse(bnds));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-03 22:30:40 +02:00
|
|
|
Expr substitute(const ATermMap & subs, Expr e)
|
2003-10-31 19:09:31 +02:00
|
|
|
{
|
2004-01-15 22:23:55 +02:00
|
|
|
checkInterrupt();
|
|
|
|
|
2003-11-16 19:46:31 +02:00
|
|
|
ATMatcher m;
|
2004-04-06 01:27:41 +03:00
|
|
|
ATerm name, pos;
|
2003-10-31 19:09:31 +02:00
|
|
|
|
* The recent change in nixpkgs of calling `stdenv.mkDerivation'
instead of `derivation' triggered a huge slowdown in the Nix
expression evaluator. Total execution time of `nix-env -qa' went up
by a factor of 60 or so.
This scalability problem was caused by expressions such as
(x: y: ... x ...) a b
where `a' is a large term (say, the one in
`all-packages-generic.nix'). Then the first beta-reduction would
produce
(y: ... a ...) b
by substituting `a' for `x'. The second beta-reduction would then
substitute `b' for `y' into the body `... a ...', which is a large
term due to `a', and thus causes a large traversal to be performed
by substitute() in the second reduction. This is however entirely
redundant, since `a' cannot contain free variables (since we never
substitute below a weak head normal form).
The solution is to wrap substituted terms into a `Closed'
constructor, i.e.,
subst(subs, Var(x)) = Closed(e) iff subs[x] = e
have substitution not descent into closed terms,
subst(subs, Closed(x)) = Closed(x)
and otherwise ignore them for evaluation,
eval(Closed(x)) = eval(x).
* Fix a typo that caused incorrect substitutions to be performed in
simple lambdas, e.g., `(x: x: x) a' would reduce to `(x: a)'.
2004-03-30 18:05:35 +03:00
|
|
|
/* As an optimisation, don't substitute in subterms known to be
|
|
|
|
closed. */
|
|
|
|
if (atMatch(m, e) >> "Closed") return e;
|
|
|
|
|
2004-02-03 16:45:34 +02:00
|
|
|
if (atMatch(m, e) >> "Var" >> name) {
|
|
|
|
Expr sub = subs.get(name);
|
* The recent change in nixpkgs of calling `stdenv.mkDerivation'
instead of `derivation' triggered a huge slowdown in the Nix
expression evaluator. Total execution time of `nix-env -qa' went up
by a factor of 60 or so.
This scalability problem was caused by expressions such as
(x: y: ... x ...) a b
where `a' is a large term (say, the one in
`all-packages-generic.nix'). Then the first beta-reduction would
produce
(y: ... a ...) b
by substituting `a' for `x'. The second beta-reduction would then
substitute `b' for `y' into the body `... a ...', which is a large
term due to `a', and thus causes a large traversal to be performed
by substitute() in the second reduction. This is however entirely
redundant, since `a' cannot contain free variables (since we never
substitute below a weak head normal form).
The solution is to wrap substituted terms into a `Closed'
constructor, i.e.,
subst(subs, Var(x)) = Closed(e) iff subs[x] = e
have substitution not descent into closed terms,
subst(subs, Closed(x)) = Closed(x)
and otherwise ignore them for evaluation,
eval(Closed(x)) = eval(x).
* Fix a typo that caused incorrect substitutions to be performed in
simple lambdas, e.g., `(x: x: x) a' would reduce to `(x: a)'.
2004-03-30 18:05:35 +03:00
|
|
|
return sub ? ATmake("Closed(<term>)", sub) : e;
|
2003-10-31 19:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* In case of a function, filter out all variables bound by this
|
|
|
|
function. */
|
|
|
|
ATermList formals;
|
|
|
|
ATerm body;
|
2004-04-06 01:27:41 +03:00
|
|
|
if (atMatch(m, e) >> "Function" >> formals >> body >> pos) {
|
2003-11-03 22:30:40 +02:00
|
|
|
ATermMap subs2(subs);
|
2003-11-16 20:31:29 +02:00
|
|
|
for (ATermIterator i(formals); i; ++i) {
|
2004-02-03 16:45:34 +02:00
|
|
|
if (!(atMatch(m, *i) >> "NoDefFormal" >> name) &&
|
|
|
|
!(atMatch(m, *i) >> "DefFormal" >> name))
|
2003-11-05 17:34:12 +02:00
|
|
|
abort();
|
2004-02-03 16:45:34 +02:00
|
|
|
subs2.remove(name);
|
2003-10-31 19:09:31 +02:00
|
|
|
}
|
2004-04-06 01:27:41 +03:00
|
|
|
return ATmake("Function(<term>, <term>, <term>)",
|
2004-03-28 23:34:22 +03:00
|
|
|
substitute(subs, (ATerm) formals),
|
2004-04-06 01:27:41 +03:00
|
|
|
substitute(subs2, body), pos);
|
2003-10-31 19:09:31 +02:00
|
|
|
}
|
|
|
|
|
2004-04-06 01:27:41 +03:00
|
|
|
if (atMatch(m, e) >> "Function1" >> name >> body >> pos) {
|
2004-03-28 23:34:22 +03:00
|
|
|
ATermMap subs2(subs);
|
|
|
|
subs2.remove(name);
|
2004-04-06 01:27:41 +03:00
|
|
|
return ATmake("Function1(<term>, <term>, <term>)", name,
|
|
|
|
substitute(subs2, body), pos);
|
2004-03-28 23:34:22 +03:00
|
|
|
}
|
|
|
|
|
2003-11-01 21:10:19 +02:00
|
|
|
/* Idem for a mutually recursive attribute set. */
|
2004-02-02 23:39:33 +02:00
|
|
|
ATermList rbnds, nrbnds;
|
|
|
|
if (atMatch(m, e) >> "Rec" >> rbnds >> nrbnds) {
|
2003-11-03 22:30:40 +02:00
|
|
|
ATermMap subs2(subs);
|
2004-02-03 16:45:34 +02:00
|
|
|
for (ATermIterator i(rbnds); i; ++i)
|
|
|
|
if (atMatch(m, *i) >> "Bind" >> name)
|
|
|
|
subs2.remove(name);
|
2004-02-16 11:18:35 +02:00
|
|
|
else abort(); /* can't happen */
|
|
|
|
for (ATermIterator i(nrbnds); i; ++i)
|
|
|
|
if (atMatch(m, *i) >> "Bind" >> name)
|
|
|
|
subs2.remove(name);
|
|
|
|
else abort(); /* can't happen */
|
2004-02-02 23:39:33 +02:00
|
|
|
return ATmake("Rec(<term>, <term>)",
|
|
|
|
substitute(subs2, (ATerm) rbnds),
|
|
|
|
substitute(subs, (ATerm) nrbnds));
|
2003-11-01 21:10:19 +02:00
|
|
|
}
|
2003-10-31 19:09:31 +02:00
|
|
|
|
|
|
|
if (ATgetType(e) == AT_APPL) {
|
|
|
|
AFun fun = ATgetAFun(e);
|
|
|
|
int arity = ATgetArity(fun);
|
|
|
|
ATermList args = ATempty;
|
|
|
|
|
|
|
|
for (int i = arity - 1; i >= 0; i--)
|
|
|
|
args = ATinsert(args, substitute(subs, ATgetArgument(e, i)));
|
|
|
|
|
|
|
|
return (ATerm) ATmakeApplList(fun, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ATgetType(e) == AT_LIST) {
|
|
|
|
ATermList out = ATempty;
|
2003-11-16 20:31:29 +02:00
|
|
|
for (ATermIterator i((ATermList) e); i; ++i)
|
|
|
|
out = ATinsert(out, substitute(subs, *i));
|
2003-10-31 19:09:31 +02:00
|
|
|
return (ATerm) ATreverse(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
2003-11-05 18:27:40 +02:00
|
|
|
|
|
|
|
|
2004-02-03 16:45:34 +02:00
|
|
|
void checkVarDefs(const ATermMap & defs, Expr e)
|
|
|
|
{
|
|
|
|
ATMatcher m;
|
|
|
|
ATerm name;
|
|
|
|
ATermList formals;
|
2004-10-25 19:54:56 +03:00
|
|
|
ATerm with, body;
|
2004-02-03 16:45:34 +02:00
|
|
|
ATermList rbnds, nrbnds;
|
|
|
|
|
|
|
|
if (atMatch(m, e) >> "Var" >> name) {
|
|
|
|
if (!defs.get(name))
|
|
|
|
throw Error(format("undefined variable `%1%'")
|
|
|
|
% aterm2String(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (atMatch(m, e) >> "Function" >> formals >> body) {
|
|
|
|
ATermMap defs2(defs);
|
|
|
|
for (ATermIterator i(formals); i; ++i) {
|
|
|
|
Expr deflt;
|
|
|
|
if (!(atMatch(m, *i) >> "NoDefFormal" >> name))
|
|
|
|
if (atMatch(m, *i) >> "DefFormal" >> name >> deflt)
|
|
|
|
checkVarDefs(defs, deflt);
|
|
|
|
else
|
|
|
|
abort();
|
|
|
|
defs2.set(name, (ATerm) ATempty);
|
|
|
|
}
|
2004-03-28 23:34:22 +03:00
|
|
|
checkVarDefs(defs2, body);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (atMatch(m, e) >> "Function1" >> name >> body) {
|
|
|
|
ATermMap defs2(defs);
|
|
|
|
defs2.set(name, (ATerm) ATempty);
|
|
|
|
checkVarDefs(defs2, body);
|
2004-02-03 16:45:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (atMatch(m, e) >> "Rec" >> rbnds >> nrbnds) {
|
2004-02-16 11:18:35 +02:00
|
|
|
checkVarDefs(defs, (ATerm) nrbnds);
|
2004-02-03 16:45:34 +02:00
|
|
|
ATermMap defs2(defs);
|
|
|
|
for (ATermIterator i(rbnds); i; ++i) {
|
|
|
|
if (!(atMatch(m, *i) >> "Bind" >> name))
|
|
|
|
abort(); /* can't happen */
|
|
|
|
defs2.set(name, (ATerm) ATempty);
|
|
|
|
}
|
2004-02-16 11:18:35 +02:00
|
|
|
for (ATermIterator i(nrbnds); i; ++i) {
|
|
|
|
if (!(atMatch(m, *i) >> "Bind" >> name))
|
|
|
|
abort(); /* can't happen */
|
|
|
|
defs2.set(name, (ATerm) ATempty);
|
|
|
|
}
|
2004-02-03 16:45:34 +02:00
|
|
|
checkVarDefs(defs2, (ATerm) rbnds);
|
|
|
|
}
|
2004-10-25 19:54:56 +03:00
|
|
|
|
|
|
|
else if (atMatch(m, e) >> "With" >> with >> body) {
|
|
|
|
/* We can't check the body without evaluating the definitions
|
|
|
|
(which is an arbitrary expression), so we don't do that
|
|
|
|
here but only when actually evaluating the `with'. */
|
|
|
|
checkVarDefs(defs, with);
|
|
|
|
}
|
2004-02-03 16:45:34 +02:00
|
|
|
|
|
|
|
else if (ATgetType(e) == AT_APPL) {
|
|
|
|
int arity = ATgetArity(ATgetAFun(e));
|
|
|
|
for (int i = 0; i < arity; ++i)
|
|
|
|
checkVarDefs(defs, ATgetArgument(e, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (ATgetType(e) == AT_LIST)
|
|
|
|
for (ATermIterator i((ATermList) e); i; ++i)
|
|
|
|
checkVarDefs(defs, *i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-05 18:27:40 +02:00
|
|
|
Expr makeBool(bool b)
|
|
|
|
{
|
|
|
|
return b ? ATmake("Bool(True)") : ATmake("Bool(False)");
|
|
|
|
}
|
2004-10-26 20:01:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
Expr makeString(const string & s)
|
|
|
|
{
|
|
|
|
return ATmake("Str(<str>)", s.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Expr makePath(const Path & path)
|
|
|
|
{
|
|
|
|
return ATmake("Path(<str>)", path.c_str());
|
|
|
|
}
|