2003-11-18 12:06:07 +00:00
|
|
|
#include "nixexpr.hh"
|
2005-01-19 16:39:47 +00:00
|
|
|
#include "derivations.hh"
|
2006-09-04 21:06:23 +00:00
|
|
|
#include "util.hh"
|
2004-10-26 22:54:26 +00:00
|
|
|
|
2008-05-21 11:17:31 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2004-10-26 22:54:26 +00:00
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
namespace nix {
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
|
|
|
|
std::ostream & operator << (std::ostream & str, Expr & e)
|
|
|
|
{
|
|
|
|
e.show(str);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ExprInt::show(std::ostream & str)
|
|
|
|
{
|
|
|
|
str << n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExprString::show(std::ostream & str)
|
|
|
|
{
|
|
|
|
str << "\"" << s << "\""; // !!! escaping
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExprPath::show(std::ostream & str)
|
|
|
|
{
|
|
|
|
str << s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExprVar::show(std::ostream & str)
|
|
|
|
{
|
|
|
|
str << name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExprSelect::show(std::ostream & str)
|
|
|
|
{
|
|
|
|
str << "(" << *e << ")." << name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExprAttrs::show(std::ostream & str)
|
|
|
|
{
|
|
|
|
if (recursive) str << "rec ";
|
|
|
|
str << "{ ";
|
|
|
|
foreach (list<string>::iterator, i, inherited)
|
|
|
|
str << "inherited " << *i << "; ";
|
|
|
|
foreach (Attrs::iterator, i, attrs)
|
|
|
|
str << i->first << " = " << *i->second << "; ";
|
|
|
|
str << "}";
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExprList::show(std::ostream & str)
|
|
|
|
{
|
|
|
|
str << "[ ";
|
|
|
|
foreach (vector<Expr *>::iterator, i, elems)
|
|
|
|
str << "(" << **i << ") ";
|
|
|
|
str << "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExprLambda::show(std::ostream & str)
|
|
|
|
{
|
|
|
|
str << "(";
|
|
|
|
if (matchAttrs) {
|
|
|
|
str << "{ ";
|
|
|
|
bool first = true;
|
|
|
|
foreach (Formals::Formals_::iterator, i, formals->formals) {
|
|
|
|
if (first) first = false; else str << ", ";
|
|
|
|
str << i->name;
|
|
|
|
if (i->def) str << " ? " << *i->def;
|
|
|
|
}
|
|
|
|
str << " }";
|
|
|
|
if (arg != "") str << " @ ";
|
|
|
|
}
|
|
|
|
if (arg != "") str << arg;
|
|
|
|
str << ": " << *body << ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExprWith::show(std::ostream & str)
|
|
|
|
{
|
|
|
|
str << "with " << *attrs << "; " << *body;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExprIf::show(std::ostream & str)
|
|
|
|
{
|
|
|
|
str << "if " << *cond << " then " << *then << " else " << *else_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
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);
|
2009-05-15 12:35:23 +00:00
|
|
|
return (format("`%1%:%2%:%3%'") % aterm2String(path) % line % column).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);
|
2006-05-02 14:07:28 +00:00
|
|
|
ATerm args[arity];
|
2003-10-30 16:11:24 +00:00
|
|
|
|
2006-05-02 14:07:28 +00:00
|
|
|
for (int i = 0; i < arity; ++i)
|
|
|
|
args[i] = bottomupRewrite(f, ATgetArgument(e, i));
|
2003-10-30 16:11:24 +00:00
|
|
|
|
2006-05-02 14:07:28 +00:00
|
|
|
e = (ATerm) ATmakeApplArray(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
|
|
|
|
|
|
|
|
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;
|
2006-05-04 12:21:08 +00:00
|
|
|
for (ATermMap::const_iterator i = attrs.begin(); i != attrs.end(); ++i) {
|
2004-04-05 22:27:41 +00:00
|
|
|
Expr e;
|
|
|
|
ATerm pos;
|
2006-05-04 12:21:08 +00:00
|
|
|
if (!matchAttrRHS(i->value, e, pos))
|
2004-04-05 22:27:41 +00:00
|
|
|
abort(); /* can't happen */
|
2006-05-04 12:21:08 +00:00
|
|
|
bnds = ATinsert(bnds, makeBind(i->key, e, pos));
|
2004-04-05 22:27:41 +00:00
|
|
|
}
|
2006-05-02 17:51:50 +00:00
|
|
|
return makeAttrs(bnds);
|
2003-10-31 17:09:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-14 10:04:22 +00:00
|
|
|
static void varsBoundByPattern(ATermMap & map, Pattern pat)
|
|
|
|
{
|
|
|
|
ATerm name;
|
|
|
|
ATermList formals;
|
2008-08-14 14:00:44 +00:00
|
|
|
ATermBool ellipsis;
|
2008-08-14 10:04:22 +00:00
|
|
|
/* Use makeRemoved() so that it can be used directly in
|
|
|
|
substitute(). */
|
|
|
|
if (matchVarPat(pat, name))
|
|
|
|
map.set(name, makeRemoved());
|
2010-03-25 12:19:41 +00:00
|
|
|
else if (matchAttrsPat(pat, formals, ellipsis, name)) {
|
|
|
|
if (name != sNoAlias) map.set(name, makeRemoved());
|
2008-08-14 10:04:22 +00:00
|
|
|
for (ATermIterator i(formals); i; ++i) {
|
|
|
|
ATerm d1;
|
|
|
|
if (!matchFormal(*i, name, d1)) abort();
|
|
|
|
map.set(name, makeRemoved());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-04 15:17:05 +00: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 14:45:34 +00:00
|
|
|
{
|
2005-11-04 15:17:05 +00:00
|
|
|
if (done.find(e) != done.end()) return;
|
|
|
|
done.insert(e);
|
|
|
|
|
2004-10-26 22:54:26 +00:00
|
|
|
ATerm name, pos, value;
|
2004-10-25 16:54:56 +00:00
|
|
|
ATerm with, body;
|
2004-02-03 14:45:34 +00:00
|
|
|
ATermList rbnds, nrbnds;
|
2008-08-14 10:04:22 +00:00
|
|
|
Pattern pat;
|
2004-02-03 14:45:34 +00:00
|
|
|
|
2008-02-21 12:01:24 +00:00
|
|
|
/* Closed terms don't have free variables, so we don't have to
|
|
|
|
check by definition. */
|
|
|
|
if (matchClosed(e, value)) return;
|
|
|
|
|
|
|
|
else if (matchVar(e, name)) {
|
2004-02-03 14:45:34 +00:00
|
|
|
if (!defs.get(name))
|
2006-07-19 15:36:15 +00:00
|
|
|
throw EvalError(format("undefined variable `%1%'")
|
2004-02-03 14:45:34 +00:00
|
|
|
% aterm2String(name));
|
|
|
|
}
|
|
|
|
|
2008-08-14 10:04:22 +00:00
|
|
|
else if (matchFunction(e, pat, body, pos)) {
|
2004-03-28 20:34:22 +00:00
|
|
|
ATermMap defs2(defs);
|
2008-08-14 10:04:22 +00:00
|
|
|
varsBoundByPattern(defs2, pat);
|
2006-07-11 10:29:52 +00:00
|
|
|
set<Expr> done2;
|
2008-08-14 10:04:22 +00:00
|
|
|
checkVarDefs2(done2, defs2, pat);
|
2006-07-11 10:29:52 +00:00
|
|
|
checkVarDefs2(done2, defs2, body);
|
2004-02-03 14:45:34 +00:00
|
|
|
}
|
|
|
|
|
2004-10-26 22:54:26 +00:00
|
|
|
else if (matchRec(e, rbnds, nrbnds)) {
|
2005-11-04 15:17:05 +00:00
|
|
|
checkVarDefs2(done, 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);
|
|
|
|
}
|
2006-07-11 10:29:52 +00:00
|
|
|
set<Expr> done2;
|
|
|
|
checkVarDefs2(done2, defs2, (ATerm) rbnds);
|
2004-02-03 14:45:34 +00:00
|
|
|
}
|
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'. */
|
2005-11-04 15:17:05 +00:00
|
|
|
checkVarDefs2(done, defs, with);
|
2004-10-25 16:54:56 +00:00
|
|
|
}
|
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)
|
2005-11-04 15:17:05 +00:00
|
|
|
checkVarDefs2(done, defs, ATgetArgument(e, i));
|
2004-02-03 14:45:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (ATgetType(e) == AT_LIST)
|
|
|
|
for (ATermIterator i((ATermList) e); i; ++i)
|
2005-11-04 15:17:05 +00:00
|
|
|
checkVarDefs2(done, defs, *i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void checkVarDefs(const ATermMap & defs, Expr e)
|
|
|
|
{
|
|
|
|
set<Expr> done;
|
|
|
|
checkVarDefs2(done, defs, e);
|
2004-02-03 14:45:34 +00:00
|
|
|
}
|
2010-04-12 18:30:11 +00:00
|
|
|
#endif
|
2006-10-16 15:55:34 +00:00
|
|
|
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|