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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-12 22:03:27 +00:00
|
|
|
void Expr::show(std::ostream & str)
|
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
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 21:21:24 +00:00
|
|
|
void ExprOpHasAttr::show(std::ostream & str)
|
|
|
|
{
|
|
|
|
str << "(" << *e << ") ? " << name;
|
|
|
|
}
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
void ExprAttrs::show(std::ostream & str)
|
|
|
|
{
|
|
|
|
if (recursive) str << "rec ";
|
|
|
|
str << "{ ";
|
2010-04-13 12:25:42 +00:00
|
|
|
foreach (list<Symbol>::iterator, i, inherited)
|
2010-04-12 23:33:23 +00:00
|
|
|
str << "inherit " << *i << "; ";
|
2010-04-12 18:30:11 +00:00
|
|
|
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 << " }";
|
2010-04-13 12:25:42 +00:00
|
|
|
if (!arg.empty()) str << " @ ";
|
2010-04-12 18:30:11 +00:00
|
|
|
}
|
2010-04-13 12:25:42 +00:00
|
|
|
if (!arg.empty()) str << arg;
|
2010-04-12 18:30:11 +00:00
|
|
|
str << ": " << *body << ")";
|
|
|
|
}
|
|
|
|
|
2010-04-13 13:42:25 +00:00
|
|
|
void ExprLet::show(std::ostream & str)
|
|
|
|
{
|
|
|
|
str << "let ";
|
|
|
|
foreach (list<Symbol>::iterator, i, attrs->inherited)
|
|
|
|
str << "inherit " << *i << "; ";
|
|
|
|
foreach (ExprAttrs::Attrs::iterator, i, attrs->attrs)
|
|
|
|
str << i->first << " = " << *i->second << "; ";
|
|
|
|
str << "in " << *body;
|
|
|
|
}
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
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 21:21:24 +00:00
|
|
|
void ExprAssert::show(std::ostream & str)
|
|
|
|
{
|
|
|
|
str << "assert " << *cond << "; " << *body;
|
|
|
|
}
|
2010-04-12 18:30:11 +00:00
|
|
|
|
2010-04-12 21:21:24 +00: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-05 22:27:41 +00:00
|
|
|
{
|
2010-04-12 21:21:24 +00:00
|
|
|
if (!pos.line)
|
|
|
|
str << "undefined position";
|
|
|
|
else
|
|
|
|
str << (format("`%1%:%2%:%3%'") % pos.file % pos.line % pos.column).str();
|
|
|
|
return str;
|
2004-04-05 22:27:41 +00:00
|
|
|
}
|
2003-11-03 20:30:40 +00:00
|
|
|
|
|
|
|
|
2010-04-12 21:21:24 +00:00
|
|
|
#if 0
|
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
|
|
|
}
|