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"
|
2006-09-05 08:54:48 +00:00
|
|
|
#include "aterm.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
|
|
|
|
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 {
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-16 15:55:34 +00: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-04 21:06:23 +00:00
|
|
|
}
|