2003-11-18 12:06:07 +00:00
|
|
|
#include "nixexpr.hh"
|
2003-11-18 11:22:29 +00:00
|
|
|
#include "storeexpr.hh"
|
2003-10-30 16:11:24 +00:00
|
|
|
|
|
|
|
|
2004-10-29 11:22:49 +00:00
|
|
|
#include "nixexpr-ast.hh"
|
|
|
|
#include "nixexpr-ast.cc"
|
2004-10-26 22:54:26 +00:00
|
|
|
|
|
|
|
|
2003-11-03 20:30:40 +00:00
|
|
|
ATermMap::ATermMap(unsigned int initialSize, unsigned int maxLoadPct)
|
|
|
|
{
|
2004-01-21 14:49:32 +00:00
|
|
|
this->maxLoadPct = maxLoadPct;
|
2003-11-03 20:30:40 +00: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 14:49:32 +00:00
|
|
|
maxLoadPct = map.maxLoadPct;
|
|
|
|
table = ATtableCreate(ATgetLength(keys), maxLoadPct);
|
2003-11-03 20:30:40 +00:00
|
|
|
if (!table) throw Error("cannot create ATerm table");
|
|
|
|
|
2004-02-04 16:03:29 +00:00
|
|
|
add(map, keys);
|
2003-11-03 20:30:40 +00: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)
|
|
|
|
{
|
2004-11-03 18:12:03 +00:00
|
|
|
set(toATerm(key), value);
|
2003-11-03 20:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ATerm ATermMap::get(ATerm key) const
|
|
|
|
{
|
|
|
|
return ATtableGet(table, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ATerm ATermMap::get(const string & key) const
|
|
|
|
{
|
2004-11-03 18:12:03 +00:00
|
|
|
return get(toATerm(key));
|
2003-11-03 20:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ATermMap::remove(ATerm key)
|
|
|
|
{
|
|
|
|
ATtableRemove(table, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ATermMap::remove(const string & key)
|
|
|
|
{
|
2004-11-03 18:12:03 +00:00
|
|
|
remove(toATerm(key));
|
2003-11-03 20:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ATermList ATermMap::keys() const
|
|
|
|
{
|
|
|
|
ATermList keys = ATtableKeys(table);
|
|
|
|
if (!keys) throw Error("cannot query aterm map keys");
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-04 16:03:29 +00: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-05 22:27:41 +00:00
|
|
|
string showPos(ATerm pos)
|
|
|
|
{
|
2004-10-26 22:54:26 +00:00
|
|
|
ATerm path;
|
2004-04-05 22:27:41 +00:00
|
|
|
int line, column;
|
2004-10-26 22:54:26 +00:00
|
|
|
if (matchNoPos(pos)) return "undefined position";
|
|
|
|
if (!matchPos(pos, path, line, column))
|
2004-04-05 22:27:41 +00:00
|
|
|
throw badTerm("position expected", pos);
|
2004-10-26 22:54:26 +00:00
|
|
|
return (format("`%1%', line %2%") % aterm2String(path) % line).str();
|
2004-04-05 22:27:41 +00:00
|
|
|
}
|
2003-11-03 20:30:40 +00:00
|
|
|
|
|
|
|
|
2003-10-30 16:11:24 +00:00
|
|
|
ATerm bottomupRewrite(TermFun & f, ATerm e)
|
|
|
|
{
|
2004-01-15 20:23:55 +00:00
|
|
|
checkInterrupt();
|
|
|
|
|
2003-10-30 16:11:24 +00: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 11:59:35 +00:00
|
|
|
e = (ATerm) ATmakeApplList(fun, args);
|
2003-10-30 16:11:24 +00:00
|
|
|
}
|
|
|
|
|
2003-11-03 11:59:35 +00:00
|
|
|
else if (ATgetType(e) == AT_LIST) {
|
2003-10-30 16:11:24 +00:00
|
|
|
ATermList in = (ATermList) e;
|
|
|
|
ATermList out = ATempty;
|
|
|
|
|
2003-11-16 18:31:29 +00:00
|
|
|
for (ATermIterator i(in); i; ++i)
|
|
|
|
out = ATinsert(out, bottomupRewrite(f, *i));
|
2003-10-30 16:11:24 +00:00
|
|
|
|
2003-11-03 11:59:35 +00:00
|
|
|
e = (ATerm) ATreverse(out);
|
2003-10-30 16:11:24 +00:00
|
|
|
}
|
|
|
|
|
2003-11-03 11:59:35 +00:00
|
|
|
return f(e);
|
2003-10-30 16:11:24 +00:00
|
|
|
}
|
2003-10-31 17:09:31 +00:00
|
|
|
|
|
|
|
|
2004-04-05 22:27:41 +00:00
|
|
|
void queryAllAttrs(Expr e, ATermMap & attrs, bool withPos)
|
2003-10-31 17:09:31 +00:00
|
|
|
{
|
|
|
|
ATermList bnds;
|
2004-10-26 22:54:26 +00:00
|
|
|
if (!matchAttrs(e, bnds))
|
2004-04-05 22:27:41 +00:00
|
|
|
throw Error("attribute set expected");
|
2003-10-31 17:09:31 +00:00
|
|
|
|
2003-11-16 18:31:29 +00:00
|
|
|
for (ATermIterator i(bnds); i; ++i) {
|
2004-10-26 22:54:26 +00:00
|
|
|
ATerm name;
|
2003-10-31 17:09:31 +00:00
|
|
|
Expr e;
|
2004-04-05 22:27:41 +00:00
|
|
|
ATerm pos;
|
2004-10-26 22:54:26 +00:00
|
|
|
if (!matchBind(*i, name, e, pos)) abort(); /* can't happen */
|
|
|
|
attrs.set(name, withPos ? makeAttrRHS(e, pos) : e);
|
2003-10-31 17:09:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Expr queryAttr(Expr e, const string & name)
|
|
|
|
{
|
2004-04-05 22:27:41 +00:00
|
|
|
ATerm dummy;
|
|
|
|
return queryAttr(e, name, dummy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Expr queryAttr(Expr e, const string & name, ATerm & pos)
|
|
|
|
{
|
|
|
|
ATermList bnds;
|
2004-10-26 22:54:26 +00:00
|
|
|
if (!matchAttrs(e, bnds))
|
2004-04-05 22:27:41 +00:00
|
|
|
throw Error("attribute set expected");
|
|
|
|
|
|
|
|
for (ATermIterator i(bnds); i; ++i) {
|
2004-10-26 22:54:26 +00:00
|
|
|
ATerm name2, pos2;
|
2004-04-05 22:27:41 +00:00
|
|
|
Expr e;
|
2004-10-26 22:54:26 +00:00
|
|
|
if (!matchBind(*i, name2, e, pos2))
|
2004-04-05 22:27:41 +00:00
|
|
|
abort(); /* can't happen */
|
2004-10-26 22:54:26 +00:00
|
|
|
if (aterm2String(name2) == name) {
|
2004-04-05 22:27:41 +00:00
|
|
|
pos = pos2;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2003-10-31 17:09:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-03 20:30:40 +00:00
|
|
|
Expr makeAttrs(const ATermMap & attrs)
|
2003-10-31 17:09:31 +00:00
|
|
|
{
|
2003-11-16 18:31:29 +00:00
|
|
|
ATermList bnds = ATempty;
|
2004-04-05 22:27:41 +00:00
|
|
|
for (ATermIterator i(attrs.keys()); i; ++i) {
|
|
|
|
Expr e;
|
|
|
|
ATerm pos;
|
2004-10-26 22:54:26 +00:00
|
|
|
if (!matchAttrRHS(attrs.get(*i), e, pos))
|
2004-04-05 22:27:41 +00:00
|
|
|
abort(); /* can't happen */
|
2004-10-26 22:54:26 +00:00
|
|
|
bnds = ATinsert(bnds, makeBind(*i, e, pos));
|
2004-04-05 22:27:41 +00:00
|
|
|
}
|
2004-10-26 22:54:26 +00:00
|
|
|
return makeAttrs(ATreverse(bnds));
|
2003-10-31 17:09:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-03 20:30:40 +00:00
|
|
|
Expr substitute(const ATermMap & subs, Expr e)
|
2003-10-31 17:09:31 +00:00
|
|
|
{
|
2004-01-15 20:23:55 +00:00
|
|
|
checkInterrupt();
|
|
|
|
|
2004-10-26 22:54:26 +00:00
|
|
|
ATerm name, pos, e2;
|
2003-10-31 17:09:31 +00: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 15:05:35 +00:00
|
|
|
/* As an optimisation, don't substitute in subterms known to be
|
|
|
|
closed. */
|
2004-10-26 22:54:26 +00: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 15:05:35 +00:00
|
|
|
|
2004-10-26 22:54:26 +00:00
|
|
|
if (matchVar(e, name)) {
|
2004-02-03 14:45:34 +00:00
|
|
|
Expr sub = subs.get(name);
|
2004-10-26 22:54:26 +00:00
|
|
|
return sub ? makeClosed(sub) : e;
|
2003-10-31 17:09:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* In case of a function, filter out all variables bound by this
|
|
|
|
function. */
|
|
|
|
ATermList formals;
|
2004-10-26 22:54:26 +00:00
|
|
|
ATerm body, def;
|
|
|
|
if (matchFunction(e, formals, body, pos)) {
|
2003-11-03 20:30:40 +00:00
|
|
|
ATermMap subs2(subs);
|
2003-11-16 18:31:29 +00:00
|
|
|
for (ATermIterator i(formals); i; ++i) {
|
2004-10-26 22:54:26 +00:00
|
|
|
if (!matchNoDefFormal(*i, name) &&
|
|
|
|
!matchDefFormal(*i, name, def))
|
2003-11-05 15:34:12 +00:00
|
|
|
abort();
|
2004-02-03 14:45:34 +00:00
|
|
|
subs2.remove(name);
|
2003-10-31 17:09:31 +00:00
|
|
|
}
|
2004-10-26 22:54:26 +00:00
|
|
|
return makeFunction(
|
|
|
|
(ATermList) substitute(subs, (ATerm) formals),
|
2004-04-05 22:27:41 +00:00
|
|
|
substitute(subs2, body), pos);
|
2003-10-31 17:09:31 +00:00
|
|
|
}
|
|
|
|
|
2004-10-26 22:54:26 +00:00
|
|
|
if (matchFunction1(e, name, body, pos)) {
|
2004-03-28 20:34:22 +00:00
|
|
|
ATermMap subs2(subs);
|
|
|
|
subs2.remove(name);
|
2004-10-26 22:54:26 +00:00
|
|
|
return makeFunction1(name, substitute(subs2, body), pos);
|
2004-03-28 20:34:22 +00:00
|
|
|
}
|
|
|
|
|
2003-11-01 19:10:19 +00:00
|
|
|
/* Idem for a mutually recursive attribute set. */
|
2004-02-02 21:39:33 +00:00
|
|
|
ATermList rbnds, nrbnds;
|
2004-10-26 22:54:26 +00:00
|
|
|
if (matchRec(e, rbnds, nrbnds)) {
|
2003-11-03 20:30:40 +00:00
|
|
|
ATermMap subs2(subs);
|
2004-02-03 14:45:34 +00:00
|
|
|
for (ATermIterator i(rbnds); i; ++i)
|
2004-10-26 22:54:26 +00:00
|
|
|
if (matchBind(*i, name, e2, pos)) subs2.remove(name);
|
2004-02-16 09:18:35 +00:00
|
|
|
else abort(); /* can't happen */
|
|
|
|
for (ATermIterator i(nrbnds); i; ++i)
|
2004-10-26 22:54:26 +00:00
|
|
|
if (matchBind(*i, name, e2, pos)) subs2.remove(name);
|
2004-02-16 09:18:35 +00:00
|
|
|
else abort(); /* can't happen */
|
2004-10-26 22:54:26 +00:00
|
|
|
return makeRec(
|
|
|
|
(ATermList) substitute(subs2, (ATerm) rbnds),
|
|
|
|
(ATermList) substitute(subs, (ATerm) nrbnds));
|
2003-11-01 19:10:19 +00:00
|
|
|
}
|
2003-10-31 17:09:31 +00: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 18:31:29 +00:00
|
|
|
for (ATermIterator i((ATermList) e); i; ++i)
|
|
|
|
out = ATinsert(out, substitute(subs, *i));
|
2003-10-31 17:09:31 +00:00
|
|
|
return (ATerm) ATreverse(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
2003-11-05 16:27:40 +00:00
|
|
|
|
|
|
|
|
2004-02-03 14:45:34 +00:00
|
|
|
void checkVarDefs(const ATermMap & defs, Expr e)
|
|
|
|
{
|
2004-10-26 22:54:26 +00:00
|
|
|
ATerm name, pos, value;
|
2004-02-03 14:45:34 +00:00
|
|
|
ATermList formals;
|
2004-10-25 16:54:56 +00:00
|
|
|
ATerm with, body;
|
2004-02-03 14:45:34 +00:00
|
|
|
ATermList rbnds, nrbnds;
|
|
|
|
|
2004-10-26 22:54:26 +00:00
|
|
|
if (matchVar(e, name)) {
|
2004-02-03 14:45:34 +00:00
|
|
|
if (!defs.get(name))
|
|
|
|
throw Error(format("undefined variable `%1%'")
|
|
|
|
% aterm2String(name));
|
|
|
|
}
|
|
|
|
|
2004-10-26 22:54:26 +00:00
|
|
|
else if (matchFunction(e, formals, body, pos)) {
|
2004-02-03 14:45:34 +00:00
|
|
|
ATermMap defs2(defs);
|
|
|
|
for (ATermIterator i(formals); i; ++i) {
|
|
|
|
Expr deflt;
|
2004-10-26 22:54:26 +00:00
|
|
|
if (!matchNoDefFormal(*i, name))
|
|
|
|
if (matchDefFormal(*i, name, deflt))
|
2004-02-03 14:45:34 +00:00
|
|
|
checkVarDefs(defs, deflt);
|
|
|
|
else
|
|
|
|
abort();
|
|
|
|
defs2.set(name, (ATerm) ATempty);
|
|
|
|
}
|
2004-03-28 20:34:22 +00:00
|
|
|
checkVarDefs(defs2, body);
|
|
|
|
}
|
|
|
|
|
2004-10-26 22:54:26 +00:00
|
|
|
else if (matchFunction1(e, name, body, pos)) {
|
2004-03-28 20:34:22 +00:00
|
|
|
ATermMap defs2(defs);
|
|
|
|
defs2.set(name, (ATerm) ATempty);
|
|
|
|
checkVarDefs(defs2, body);
|
2004-02-03 14:45:34 +00:00
|
|
|
}
|
|
|
|
|
2004-10-26 22:54:26 +00:00
|
|
|
else if (matchRec(e, rbnds, nrbnds)) {
|
2004-02-16 09:18:35 +00:00
|
|
|
checkVarDefs(defs, (ATerm) nrbnds);
|
2004-02-03 14:45:34 +00:00
|
|
|
ATermMap defs2(defs);
|
|
|
|
for (ATermIterator i(rbnds); i; ++i) {
|
2004-10-26 22:54:26 +00:00
|
|
|
if (!matchBind(*i, name, value, pos)) abort(); /* can't happen */
|
2004-02-03 14:45:34 +00:00
|
|
|
defs2.set(name, (ATerm) ATempty);
|
|
|
|
}
|
2004-02-16 09:18:35 +00:00
|
|
|
for (ATermIterator i(nrbnds); i; ++i) {
|
2004-10-26 22:54:26 +00:00
|
|
|
if (!matchBind(*i, name, value, pos)) abort(); /* can't happen */
|
2004-02-16 09:18:35 +00:00
|
|
|
defs2.set(name, (ATerm) ATempty);
|
|
|
|
}
|
2004-02-03 14:45:34 +00:00
|
|
|
checkVarDefs(defs2, (ATerm) rbnds);
|
|
|
|
}
|
2004-10-25 16:54:56 +00:00
|
|
|
|
2004-10-26 22:54:26 +00:00
|
|
|
else if (matchWith(e, with, body, pos)) {
|
2004-10-25 16:54:56 +00: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'. */
|
|
|
|
checkVarDefs(defs, with);
|
|
|
|
}
|
2004-02-03 14:45:34 +00: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 16:27:40 +00:00
|
|
|
Expr makeBool(bool b)
|
|
|
|
{
|
2004-10-26 22:54:26 +00:00
|
|
|
return b ? eTrue : eFalse;
|
2004-10-26 17:01:35 +00:00
|
|
|
}
|