2003-11-18 14:06:07 +02:00
|
|
|
#include "nixexpr.hh"
|
2005-01-19 18:39:47 +02:00
|
|
|
#include "derivations.hh"
|
2006-09-05 00:06:23 +03:00
|
|
|
#include "util.hh"
|
2006-09-05 11:54:48 +03:00
|
|
|
#include "aterm.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
|
|
|
|
2008-05-21 14:17:31 +03:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2004-10-27 01:54:26 +03:00
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
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);
|
2009-05-15 15:35:23 +03:00
|
|
|
return (format("`%1%:%2%:%3%'") % aterm2String(path) % line % column).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
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-14 13:04:22 +03:00
|
|
|
static void varsBoundByPattern(ATermMap & map, Pattern pat)
|
|
|
|
{
|
|
|
|
ATerm name;
|
|
|
|
ATermList formals;
|
2008-08-14 17:00:44 +03:00
|
|
|
ATermBool ellipsis;
|
2008-08-14 13:04:22 +03:00
|
|
|
/* Use makeRemoved() so that it can be used directly in
|
|
|
|
substitute(). */
|
|
|
|
if (matchVarPat(pat, name))
|
|
|
|
map.set(name, makeRemoved());
|
2010-03-25 14:19:41 +02:00
|
|
|
else if (matchAttrsPat(pat, formals, ellipsis, name)) {
|
|
|
|
if (name != sNoAlias) map.set(name, makeRemoved());
|
2008-08-14 13:04:22 +03:00
|
|
|
for (ATermIterator i(formals); i; ++i) {
|
|
|
|
ATerm d1;
|
|
|
|
if (!matchFormal(*i, name, d1)) abort();
|
|
|
|
map.set(name, makeRemoved());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-10-25 19:54:56 +03:00
|
|
|
ATerm with, body;
|
2004-02-03 16:45:34 +02:00
|
|
|
ATermList rbnds, nrbnds;
|
2008-08-14 13:04:22 +03:00
|
|
|
Pattern pat;
|
2004-02-03 16:45:34 +02:00
|
|
|
|
2008-02-21 14:01:24 +02: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 16:45:34 +02:00
|
|
|
if (!defs.get(name))
|
2006-07-19 18:36:15 +03:00
|
|
|
throw EvalError(format("undefined variable `%1%'")
|
2004-02-03 16:45:34 +02:00
|
|
|
% aterm2String(name));
|
|
|
|
}
|
|
|
|
|
2008-08-14 13:04:22 +03:00
|
|
|
else if (matchFunction(e, pat, body, pos)) {
|
2004-03-28 23:34:22 +03:00
|
|
|
ATermMap defs2(defs);
|
2008-08-14 13:04:22 +03:00
|
|
|
varsBoundByPattern(defs2, pat);
|
2006-07-11 13:29:52 +03:00
|
|
|
set<Expr> done2;
|
2008-08-14 13:04:22 +03:00
|
|
|
checkVarDefs2(done2, defs2, pat);
|
2006-07-11 13:29:52 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-16 18:55:34 +03:00
|
|
|
bool matchStr(Expr e, string & s, PathSet & context)
|
|
|
|
{
|
|
|
|
ATermList l;
|
|
|
|
ATerm s_;
|
|
|
|
|
|
|
|
if (!matchStr(e, s_, l)) return false;
|
|
|
|
|
|
|
|
s = aterm2String(s_);
|
|
|
|
|
|
|
|
for (ATermIterator i(l); i; ++i)
|
|
|
|
context.insert(aterm2String(*i));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Expr makeStr(const string & s, const PathSet & context)
|
|
|
|
{
|
|
|
|
return makeStr(toATerm(s), toATermList(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
}
|