2003-11-18 14:06:07 +02:00
|
|
|
#include "nixexpr.hh"
|
2005-01-19 18:39:47 +02:00
|
|
|
#include "derivations.hh"
|
2003-10-30 18:11:24 +02:00
|
|
|
|
|
|
|
|
2004-10-29 14:22:49 +03:00
|
|
|
#include "nixexpr-ast.hh"
|
|
|
|
#include "nixexpr-ast.cc"
|
2004-10-27 01:54:26 +03:00
|
|
|
|
|
|
|
|
2004-04-06 01:27:41 +03:00
|
|
|
string showPos(ATerm pos)
|
|
|
|
{
|
2004-10-27 01:54:26 +03:00
|
|
|
ATerm path;
|
2004-04-06 01:27:41 +03:00
|
|
|
int line, column;
|
2004-10-27 01:54:26 +03:00
|
|
|
if (matchNoPos(pos)) return "undefined position";
|
|
|
|
if (!matchPos(pos, path, line, column))
|
2004-04-06 01:27:41 +03:00
|
|
|
throw badTerm("position expected", pos);
|
2004-10-27 01:54:26 +03:00
|
|
|
return (format("`%1%', line %2%") % aterm2String(path) % line).str();
|
2004-04-06 01:27:41 +03:00
|
|
|
}
|
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);
|
2006-05-02 17:07:28 +03:00
|
|
|
ATerm args[arity];
|
2003-10-30 18:11:24 +02:00
|
|
|
|
2006-05-02 17:07:28 +03:00
|
|
|
for (int i = 0; i < arity; ++i)
|
|
|
|
args[i] = bottomupRewrite(f, ATgetArgument(e, i));
|
2003-10-30 18:11:24 +02:00
|
|
|
|
2006-05-02 17:07:28 +03:00
|
|
|
e = (ATerm) ATmakeApplArray(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
|
|
|
{
|
|
|
|
ATermList bnds;
|
2004-10-27 01:54:26 +03:00
|
|
|
if (!matchAttrs(e, 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) {
|
2004-10-27 01:54:26 +03:00
|
|
|
ATerm name;
|
2003-10-31 19:09:31 +02:00
|
|
|
Expr e;
|
2004-04-06 01:27:41 +03:00
|
|
|
ATerm pos;
|
2004-10-27 01:54:26 +03:00
|
|
|
if (!matchBind(*i, name, e, pos)) abort(); /* can't happen */
|
|
|
|
attrs.set(name, withPos ? makeAttrRHS(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)
|
|
|
|
{
|
|
|
|
ATermList bnds;
|
2004-10-27 01:54:26 +03:00
|
|
|
if (!matchAttrs(e, bnds))
|
2004-04-06 01:27:41 +03:00
|
|
|
throw Error("attribute set expected");
|
|
|
|
|
|
|
|
for (ATermIterator i(bnds); i; ++i) {
|
2004-10-27 01:54:26 +03:00
|
|
|
ATerm name2, pos2;
|
2004-04-06 01:27:41 +03:00
|
|
|
Expr e;
|
2004-10-27 01:54:26 +03:00
|
|
|
if (!matchBind(*i, name2, e, pos2))
|
2004-04-06 01:27:41 +03:00
|
|
|
abort(); /* can't happen */
|
2004-10-27 01:54:26 +03:00
|
|
|
if (aterm2String(name2) == name) {
|
2004-04-06 01:27:41 +03:00
|
|
|
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
|
|
|
{
|
2003-11-16 20:31:29 +02:00
|
|
|
ATermList bnds = ATempty;
|
2006-05-04 15:21:08 +03:00
|
|
|
for (ATermMap::const_iterator i = attrs.begin(); i != attrs.end(); ++i) {
|
2004-04-06 01:27:41 +03:00
|
|
|
Expr e;
|
|
|
|
ATerm pos;
|
2006-05-04 15:21:08 +03:00
|
|
|
if (!matchAttrRHS(i->value, e, pos))
|
2004-04-06 01:27:41 +03:00
|
|
|
abort(); /* can't happen */
|
2006-05-04 15:21:08 +03:00
|
|
|
bnds = ATinsert(bnds, makeBind(i->key, e, pos));
|
2004-04-06 01:27:41 +03:00
|
|
|
}
|
2006-05-02 20:51:50 +03:00
|
|
|
return makeAttrs(bnds);
|
2003-10-31 19:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-03 00:39:02 +03:00
|
|
|
Expr substitute(const Substitution & subs, Expr e)
|
2003-10-31 19:09:31 +02:00
|
|
|
{
|
2004-01-15 22:23:55 +02:00
|
|
|
checkInterrupt();
|
|
|
|
|
2006-05-02 16:39:55 +03:00
|
|
|
//if (subs.size() == 0) return e;
|
|
|
|
|
2004-10-27 01:54:26 +03:00
|
|
|
ATerm name, pos, e2;
|
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. */
|
2004-10-27 01:54:26 +03:00
|
|
|
if (matchClosed(e, e2)) return e;
|
* 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
|
|
|
|
2004-10-27 01:54:26 +03:00
|
|
|
if (matchVar(e, name)) {
|
2006-05-03 00:39:02 +03:00
|
|
|
Expr sub = subs.lookup(name);
|
|
|
|
if (sub == makeRemoved()) sub = 0;
|
2005-07-19 14:48:05 +03:00
|
|
|
Expr wrapped;
|
|
|
|
/* Add a "closed" wrapper around terms that aren't already
|
|
|
|
closed. The check is necessary to prevent repeated
|
|
|
|
wrapping, e.g., closed(closed(closed(...))), which kills
|
|
|
|
caching. */
|
|
|
|
return sub ? (matchClosed(sub, wrapped) ? sub : makeClosed(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;
|
2004-10-27 01:54:26 +03:00
|
|
|
ATerm body, def;
|
|
|
|
if (matchFunction(e, formals, body, pos)) {
|
2006-05-04 15:21:08 +03:00
|
|
|
ATermMap map(ATgetLength(formals));
|
2003-11-16 20:31:29 +02:00
|
|
|
for (ATermIterator i(formals); i; ++i) {
|
2004-10-27 01:54:26 +03:00
|
|
|
if (!matchNoDefFormal(*i, name) &&
|
|
|
|
!matchDefFormal(*i, name, def))
|
2003-11-05 17:34:12 +02:00
|
|
|
abort();
|
2006-05-03 00:39:02 +03:00
|
|
|
map.set(name, makeRemoved());
|
2003-10-31 19:09:31 +02:00
|
|
|
}
|
2006-05-03 00:39:02 +03:00
|
|
|
Substitution subs2(&subs, &map);
|
2004-10-27 01:54:26 +03:00
|
|
|
return makeFunction(
|
2006-05-02 16:39:55 +03:00
|
|
|
(ATermList) substitute(subs2, (ATerm) formals),
|
2004-04-06 01:27:41 +03:00
|
|
|
substitute(subs2, body), pos);
|
2003-10-31 19:09:31 +02:00
|
|
|
}
|
|
|
|
|
2004-10-27 01:54:26 +03:00
|
|
|
if (matchFunction1(e, name, body, pos)) {
|
2006-05-04 15:21:08 +03:00
|
|
|
ATermMap map(1);
|
2006-05-03 00:39:02 +03:00
|
|
|
map.set(name, makeRemoved());
|
|
|
|
return makeFunction1(name, substitute(Substitution(&subs, &map), 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;
|
2004-10-27 01:54:26 +03:00
|
|
|
if (matchRec(e, rbnds, nrbnds)) {
|
2006-05-04 15:21:08 +03:00
|
|
|
ATermMap map(ATgetLength(rbnds) + ATgetLength(nrbnds));
|
2004-02-03 16:45:34 +02:00
|
|
|
for (ATermIterator i(rbnds); i; ++i)
|
2006-05-03 00:39:02 +03:00
|
|
|
if (matchBind(*i, name, e2, pos)) map.set(name, makeRemoved());
|
2004-02-16 11:18:35 +02:00
|
|
|
else abort(); /* can't happen */
|
|
|
|
for (ATermIterator i(nrbnds); i; ++i)
|
2006-05-03 00:39:02 +03:00
|
|
|
if (matchBind(*i, name, e2, pos)) map.set(name, makeRemoved());
|
2004-02-16 11:18:35 +02:00
|
|
|
else abort(); /* can't happen */
|
2004-10-27 01:54:26 +03:00
|
|
|
return makeRec(
|
2006-05-03 00:39:02 +03:00
|
|
|
(ATermList) substitute(Substitution(&subs, &map), (ATerm) rbnds),
|
2004-10-27 01:54:26 +03:00
|
|
|
(ATermList) 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);
|
2006-05-02 17:07:28 +03:00
|
|
|
ATerm args[arity];
|
2006-05-03 00:39:02 +03:00
|
|
|
bool changed = false;
|
2003-10-31 19:09:31 +02:00
|
|
|
|
2006-05-03 00:39:02 +03:00
|
|
|
for (int i = 0; i < arity; ++i) {
|
|
|
|
ATerm arg = ATgetArgument(e, i);
|
|
|
|
args[i] = substitute(subs, arg);
|
|
|
|
if (args[i] != arg) changed = true;
|
|
|
|
}
|
2003-10-31 19:09:31 +02:00
|
|
|
|
2006-05-03 00:39:02 +03:00
|
|
|
return changed ? (ATerm) ATmakeApplArray(fun, args) : e;
|
2003-10-31 19:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ATgetType(e) == AT_LIST) {
|
2006-05-02 17:07:28 +03:00
|
|
|
unsigned int len = ATgetLength((ATermList) e);
|
|
|
|
ATerm es[len];
|
|
|
|
ATermIterator i((ATermList) e);
|
|
|
|
for (unsigned int j = 0; i; ++i, ++j)
|
|
|
|
es[j] = substitute(subs, *i);
|
2003-10-31 19:09:31 +02:00
|
|
|
ATermList out = ATempty;
|
2006-05-02 17:07:28 +03:00
|
|
|
for (unsigned int j = len; j; --j)
|
|
|
|
out = ATinsert(out, es[j - 1]);
|
|
|
|
return (ATerm) out;
|
2003-10-31 19:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
2003-11-05 18:27:40 +02:00
|
|
|
|
|
|
|
|
2005-11-04 17:17:05 +02:00
|
|
|
/* We use memoisation to prevent exponential complexity on heavily
|
|
|
|
shared ATerms (remember, an ATerm is a graph, not a tree!). Note
|
|
|
|
that using an STL set is fine here wrt to ATerm garbage collection
|
|
|
|
since all the ATerms in the set are already reachable from
|
|
|
|
somewhere else. */
|
|
|
|
static void checkVarDefs2(set<Expr> & done, const ATermMap & defs, Expr e)
|
2004-02-03 16:45:34 +02:00
|
|
|
{
|
2005-11-04 17:17:05 +02:00
|
|
|
if (done.find(e) != done.end()) return;
|
|
|
|
done.insert(e);
|
|
|
|
|
2004-10-27 01:54:26 +03:00
|
|
|
ATerm name, pos, value;
|
2004-02-03 16:45:34 +02:00
|
|
|
ATermList formals;
|
2004-10-25 19:54:56 +03:00
|
|
|
ATerm with, body;
|
2004-02-03 16:45:34 +02:00
|
|
|
ATermList rbnds, nrbnds;
|
|
|
|
|
2004-10-27 01:54:26 +03:00
|
|
|
if (matchVar(e, name)) {
|
2004-02-03 16:45:34 +02:00
|
|
|
if (!defs.get(name))
|
|
|
|
throw Error(format("undefined variable `%1%'")
|
|
|
|
% aterm2String(name));
|
|
|
|
}
|
|
|
|
|
2004-10-27 01:54:26 +03:00
|
|
|
else if (matchFunction(e, formals, body, pos)) {
|
2004-02-03 16:45:34 +02:00
|
|
|
ATermMap defs2(defs);
|
|
|
|
for (ATermIterator i(formals); i; ++i) {
|
|
|
|
Expr deflt;
|
2006-05-02 16:39:55 +03:00
|
|
|
if (!matchNoDefFormal(*i, name) &&
|
|
|
|
!matchDefFormal(*i, name, deflt))
|
|
|
|
abort();
|
2004-02-03 16:45:34 +02:00
|
|
|
defs2.set(name, (ATerm) ATempty);
|
|
|
|
}
|
2006-05-02 16:39:55 +03:00
|
|
|
for (ATermIterator i(formals); i; ++i) {
|
|
|
|
Expr deflt;
|
2006-07-11 13:29:52 +03:00
|
|
|
set<Expr> done2;
|
2006-05-02 16:39:55 +03:00
|
|
|
if (matchDefFormal(*i, name, deflt))
|
2006-07-11 13:29:52 +03:00
|
|
|
checkVarDefs2(done2, defs2, deflt);
|
2006-05-02 16:39:55 +03:00
|
|
|
}
|
2006-07-11 13:29:52 +03:00
|
|
|
set<Expr> done2;
|
|
|
|
checkVarDefs2(done2, defs2, body);
|
2004-03-28 23:34:22 +03:00
|
|
|
}
|
|
|
|
|
2004-10-27 01:54:26 +03:00
|
|
|
else if (matchFunction1(e, name, body, pos)) {
|
2004-03-28 23:34:22 +03:00
|
|
|
ATermMap defs2(defs);
|
|
|
|
defs2.set(name, (ATerm) ATempty);
|
2006-07-11 13:29:52 +03:00
|
|
|
set<Expr> done2;
|
|
|
|
checkVarDefs2(done2, defs2, body);
|
2004-02-03 16:45:34 +02:00
|
|
|
}
|
|
|
|
|
2004-10-27 01:54:26 +03:00
|
|
|
else if (matchRec(e, rbnds, nrbnds)) {
|
2005-11-04 17:17:05 +02:00
|
|
|
checkVarDefs2(done, defs, (ATerm) nrbnds);
|
2004-02-03 16:45:34 +02:00
|
|
|
ATermMap defs2(defs);
|
|
|
|
for (ATermIterator i(rbnds); i; ++i) {
|
2004-10-27 01:54:26 +03:00
|
|
|
if (!matchBind(*i, name, value, pos)) abort(); /* can't happen */
|
2004-02-03 16:45:34 +02:00
|
|
|
defs2.set(name, (ATerm) ATempty);
|
|
|
|
}
|
2004-02-16 11:18:35 +02:00
|
|
|
for (ATermIterator i(nrbnds); i; ++i) {
|
2004-10-27 01:54:26 +03:00
|
|
|
if (!matchBind(*i, name, value, pos)) abort(); /* can't happen */
|
2004-02-16 11:18:35 +02:00
|
|
|
defs2.set(name, (ATerm) ATempty);
|
|
|
|
}
|
2006-07-11 13:29:52 +03:00
|
|
|
set<Expr> done2;
|
|
|
|
checkVarDefs2(done2, defs2, (ATerm) rbnds);
|
2004-02-03 16:45:34 +02:00
|
|
|
}
|
2004-10-25 19:54:56 +03:00
|
|
|
|
2004-10-27 01:54:26 +03:00
|
|
|
else if (matchWith(e, with, body, pos)) {
|
2004-10-25 19:54:56 +03:00
|
|
|
/* 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'. */
|
2005-11-04 17:17:05 +02:00
|
|
|
checkVarDefs2(done, defs, with);
|
2004-10-25 19:54:56 +03:00
|
|
|
}
|
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)
|
2005-11-04 17:17:05 +02:00
|
|
|
checkVarDefs2(done, defs, ATgetArgument(e, i));
|
2004-02-03 16:45:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (ATgetType(e) == AT_LIST)
|
|
|
|
for (ATermIterator i((ATermList) e); i; ++i)
|
2005-11-04 17:17:05 +02:00
|
|
|
checkVarDefs2(done, defs, *i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void checkVarDefs(const ATermMap & defs, Expr e)
|
|
|
|
{
|
|
|
|
set<Expr> done;
|
|
|
|
checkVarDefs2(done, defs, e);
|
2004-02-03 16:45:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-05 18:27:40 +02:00
|
|
|
Expr makeBool(bool b)
|
|
|
|
{
|
2004-10-27 01:54:26 +03:00
|
|
|
return b ? eTrue : eFalse;
|
2004-10-26 20:01:35 +03:00
|
|
|
}
|