2003-11-18 13:06:07 +01:00
|
|
|
#include "nixexpr.hh"
|
2005-01-19 17:39:47 +01:00
|
|
|
#include "derivations.hh"
|
2006-09-04 23:06:23 +02:00
|
|
|
#include "util.hh"
|
2004-10-27 00:54:26 +02:00
|
|
|
|
2008-05-21 13:17:31 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2004-10-27 00:54:26 +02:00
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
namespace nix {
|
|
|
|
|
2010-04-12 20:30:11 +02: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;
|
|
|
|
}
|
|
|
|
|
2010-04-12 23:21:24 +02:00
|
|
|
void ExprOpHasAttr::show(std::ostream & str)
|
|
|
|
{
|
|
|
|
str << "(" << *e << ") ? " << name;
|
|
|
|
}
|
|
|
|
|
2010-04-12 20:30:11 +02:00
|
|
|
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_;
|
|
|
|
}
|
|
|
|
|
2010-04-12 23:21:24 +02:00
|
|
|
void ExprAssert::show(std::ostream & str)
|
|
|
|
{
|
|
|
|
str << "assert " << *cond << "; " << *body;
|
|
|
|
}
|
2010-04-12 20:30:11 +02:00
|
|
|
|
2010-04-12 23:21:24 +02:00
|
|
|
void ExprOpNot::show(std::ostream & str)
|
|
|
|
{
|
|
|
|
str << "! " << *e;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExprConcatStrings::show(std::ostream & str)
|
|
|
|
{
|
|
|
|
bool first = true;
|
|
|
|
foreach (vector<Expr *>::iterator, i, *es) {
|
|
|
|
if (first) first = false; else str << " + ";
|
|
|
|
str << **i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::ostream & operator << (std::ostream & str, const Pos & pos)
|
2004-04-06 00:27:41 +02:00
|
|
|
{
|
2010-04-12 23:21:24 +02:00
|
|
|
if (!pos.line)
|
|
|
|
str << "undefined position";
|
|
|
|
else
|
|
|
|
str << (format("`%1%:%2%:%3%'") % pos.file % pos.line % pos.column).str();
|
|
|
|
return str;
|
2004-04-06 00:27:41 +02:00
|
|
|
}
|
2003-11-03 21:30:40 +01:00
|
|
|
|
|
|
|
|
2010-04-12 23:21:24 +02:00
|
|
|
#if 0
|
2003-10-30 17:11:24 +01:00
|
|
|
ATerm bottomupRewrite(TermFun & f, ATerm e)
|
|
|
|
{
|
2004-01-15 21:23:55 +01:00
|
|
|
checkInterrupt();
|
|
|
|
|
2003-10-30 17:11:24 +01:00
|
|
|
if (ATgetType(e) == AT_APPL) {
|
|
|
|
AFun fun = ATgetAFun(e);
|
|
|
|
int arity = ATgetArity(fun);
|
2006-05-02 16:07:28 +02:00
|
|
|
ATerm args[arity];
|
2003-10-30 17:11:24 +01:00
|
|
|
|
2006-05-02 16:07:28 +02:00
|
|
|
for (int i = 0; i < arity; ++i)
|
|
|
|
args[i] = bottomupRewrite(f, ATgetArgument(e, i));
|
2003-10-30 17:11:24 +01:00
|
|
|
|
2006-05-02 16:07:28 +02:00
|
|
|
e = (ATerm) ATmakeApplArray(fun, args);
|
2003-10-30 17:11:24 +01:00
|
|
|
}
|
|
|
|
|
2003-11-03 12:59:35 +01:00
|
|
|
else if (ATgetType(e) == AT_LIST) {
|
2003-10-30 17:11:24 +01:00
|
|
|
ATermList in = (ATermList) e;
|
|
|
|
ATermList out = ATempty;
|
|
|
|
|
2003-11-16 19:31:29 +01:00
|
|
|
for (ATermIterator i(in); i; ++i)
|
|
|
|
out = ATinsert(out, bottomupRewrite(f, *i));
|
2003-10-30 17:11:24 +01:00
|
|
|
|
2003-11-03 12:59:35 +01:00
|
|
|
e = (ATerm) ATreverse(out);
|
2003-10-30 17:11:24 +01:00
|
|
|
}
|
|
|
|
|
2003-11-03 12:59:35 +01:00
|
|
|
return f(e);
|
2003-10-30 17:11:24 +01:00
|
|
|
}
|
2003-10-31 18:09:31 +01:00
|
|
|
|
|
|
|
|
2003-11-03 21:30:40 +01:00
|
|
|
Expr makeAttrs(const ATermMap & attrs)
|
2003-10-31 18:09:31 +01:00
|
|
|
{
|
2003-11-16 19:31:29 +01:00
|
|
|
ATermList bnds = ATempty;
|
2006-05-04 14:21:08 +02:00
|
|
|
for (ATermMap::const_iterator i = attrs.begin(); i != attrs.end(); ++i) {
|
2004-04-06 00:27:41 +02:00
|
|
|
Expr e;
|
|
|
|
ATerm pos;
|
2006-05-04 14:21:08 +02:00
|
|
|
if (!matchAttrRHS(i->value, e, pos))
|
2004-04-06 00:27:41 +02:00
|
|
|
abort(); /* can't happen */
|
2006-05-04 14:21:08 +02:00
|
|
|
bnds = ATinsert(bnds, makeBind(i->key, e, pos));
|
2004-04-06 00:27:41 +02:00
|
|
|
}
|
2006-05-02 19:51:50 +02:00
|
|
|
return makeAttrs(bnds);
|
2003-10-31 18:09:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-14 12:04:22 +02:00
|
|
|
static void varsBoundByPattern(ATermMap & map, Pattern pat)
|
|
|
|
{
|
|
|
|
ATerm name;
|
|
|
|
ATermList formals;
|
2008-08-14 16:00:44 +02:00
|
|
|
ATermBool ellipsis;
|
2008-08-14 12:04:22 +02:00
|
|
|
/* Use makeRemoved() so that it can be used directly in
|
|
|
|
substitute(). */
|
|
|
|
if (matchVarPat(pat, name))
|
|
|
|
map.set(name, makeRemoved());
|
2010-03-25 13:19:41 +01:00
|
|
|
else if (matchAttrsPat(pat, formals, ellipsis, name)) {
|
|
|
|
if (name != sNoAlias) map.set(name, makeRemoved());
|
2008-08-14 12:04:22 +02:00
|
|
|
for (ATermIterator i(formals); i; ++i) {
|
|
|
|
ATerm d1;
|
|
|
|
if (!matchFormal(*i, name, d1)) abort();
|
|
|
|
map.set(name, makeRemoved());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-04 16:17:05 +01: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 15:45:34 +01:00
|
|
|
{
|
2005-11-04 16:17:05 +01:00
|
|
|
if (done.find(e) != done.end()) return;
|
|
|
|
done.insert(e);
|
|
|
|
|
2004-10-27 00:54:26 +02:00
|
|
|
ATerm name, pos, value;
|
2004-10-25 18:54:56 +02:00
|
|
|
ATerm with, body;
|
2004-02-03 15:45:34 +01:00
|
|
|
ATermList rbnds, nrbnds;
|
2008-08-14 12:04:22 +02:00
|
|
|
Pattern pat;
|
2004-02-03 15:45:34 +01:00
|
|
|
|
2008-02-21 13:01:24 +01: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 15:45:34 +01:00
|
|
|
if (!defs.get(name))
|
2006-07-19 17:36:15 +02:00
|
|
|
throw EvalError(format("undefined variable `%1%'")
|
2004-02-03 15:45:34 +01:00
|
|
|
% aterm2String(name));
|
|
|
|
}
|
|
|
|
|
2008-08-14 12:04:22 +02:00
|
|
|
else if (matchFunction(e, pat, body, pos)) {
|
2004-03-28 22:34:22 +02:00
|
|
|
ATermMap defs2(defs);
|
2008-08-14 12:04:22 +02:00
|
|
|
varsBoundByPattern(defs2, pat);
|
2006-07-11 12:29:52 +02:00
|
|
|
set<Expr> done2;
|
2008-08-14 12:04:22 +02:00
|
|
|
checkVarDefs2(done2, defs2, pat);
|
2006-07-11 12:29:52 +02:00
|
|
|
checkVarDefs2(done2, defs2, body);
|
2004-02-03 15:45:34 +01:00
|
|
|
}
|
|
|
|
|
2004-10-27 00:54:26 +02:00
|
|
|
else if (matchRec(e, rbnds, nrbnds)) {
|
2005-11-04 16:17:05 +01:00
|
|
|
checkVarDefs2(done, defs, (ATerm) nrbnds);
|
2004-02-03 15:45:34 +01:00
|
|
|
ATermMap defs2(defs);
|
|
|
|
for (ATermIterator i(rbnds); i; ++i) {
|
2004-10-27 00:54:26 +02:00
|
|
|
if (!matchBind(*i, name, value, pos)) abort(); /* can't happen */
|
2004-02-03 15:45:34 +01:00
|
|
|
defs2.set(name, (ATerm) ATempty);
|
|
|
|
}
|
2004-02-16 10:18:35 +01:00
|
|
|
for (ATermIterator i(nrbnds); i; ++i) {
|
2004-10-27 00:54:26 +02:00
|
|
|
if (!matchBind(*i, name, value, pos)) abort(); /* can't happen */
|
2004-02-16 10:18:35 +01:00
|
|
|
defs2.set(name, (ATerm) ATempty);
|
|
|
|
}
|
2006-07-11 12:29:52 +02:00
|
|
|
set<Expr> done2;
|
|
|
|
checkVarDefs2(done2, defs2, (ATerm) rbnds);
|
2004-02-03 15:45:34 +01:00
|
|
|
}
|
2004-10-25 18:54:56 +02:00
|
|
|
|
2004-10-27 00:54:26 +02:00
|
|
|
else if (matchWith(e, with, body, pos)) {
|
2004-10-25 18:54:56 +02: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 16:17:05 +01:00
|
|
|
checkVarDefs2(done, defs, with);
|
2004-10-25 18:54:56 +02:00
|
|
|
}
|
2004-02-03 15:45:34 +01:00
|
|
|
|
|
|
|
else if (ATgetType(e) == AT_APPL) {
|
|
|
|
int arity = ATgetArity(ATgetAFun(e));
|
|
|
|
for (int i = 0; i < arity; ++i)
|
2005-11-04 16:17:05 +01:00
|
|
|
checkVarDefs2(done, defs, ATgetArgument(e, i));
|
2004-02-03 15:45:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (ATgetType(e) == AT_LIST)
|
|
|
|
for (ATermIterator i((ATermList) e); i; ++i)
|
2005-11-04 16:17:05 +01:00
|
|
|
checkVarDefs2(done, defs, *i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void checkVarDefs(const ATermMap & defs, Expr e)
|
|
|
|
{
|
|
|
|
set<Expr> done;
|
|
|
|
checkVarDefs2(done, defs, e);
|
2004-02-03 15:45:34 +01:00
|
|
|
}
|
2010-04-12 20:30:11 +02:00
|
|
|
#endif
|
2006-10-16 17:55:34 +02:00
|
|
|
|
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
}
|