about summary refs log tree commit diff
path: root/src/libexpr/nixexpr.cc
blob: 0abc2a457304d5f97980dc569c58c6519ec34c55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "nixexpr.hh"
#include "derivations.hh"
#include "util.hh"

#include <cstdlib>


namespace nix {


std::ostream & operator << (std::ostream & str, Expr & e)
{
    e.show(str);
    return str;
}


void Expr::show(std::ostream & str)
{
    abort();
}

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 ExprOpHasAttr::show(std::ostream & str)
{
    str << "(" << *e << ") ? " << name;
}

void ExprAttrs::show(std::ostream & str)
{
    if (recursive) str << "rec ";
    str << "{ ";
    foreach (list<Symbol>::iterator, i, inherited)
        str << "inherit " << *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.empty()) str << " @ ";
    }
    if (!arg.empty()) str << arg;
    str << ": " << *body << ")";
}

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;
}

void ExprWith::show(std::ostream & str)
{
    str << "with " << *attrs << "; " << *body;
}

void ExprIf::show(std::ostream & str)
{
    str << "if " << *cond << " then " << *then << " else " << *else_;
}

void ExprAssert::show(std::ostream & str)
{
    str << "assert " << *cond << "; " << *body;
}

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)
{
    if (!pos.line)
        str << "undefined position";
    else
        str << (format("`%1%:%2%:%3%'") % pos.file % pos.line % pos.column).str();
    return str;
}
    

#if 0
ATerm bottomupRewrite(TermFun & f, ATerm e)
{
    checkInterrupt();

    if (ATgetType(e) == AT_APPL) {
        AFun fun = ATgetAFun(e);
        int arity = ATgetArity(fun);
        ATerm args[arity];

        for (int i = 0; i < arity; ++i)
            args[i] = bottomupRewrite(f, ATgetArgument(e, i));
        
        e = (ATerm) ATmakeApplArray(fun, args);
    }

    else if (ATgetType(e) == AT_LIST) {
        ATermList in = (ATermList) e;
        ATermList out = ATempty;

        for (ATermIterator i(in); i; ++i)
            out = ATinsert(out, bottomupRewrite(f, *i));

        e = (ATerm) ATreverse(out);
    }

    return f(e);
}


static void varsBoundByPattern(ATermMap & map, Pattern pat)
{
    ATerm name;
    ATermList formals;
    ATermBool ellipsis;
    /* Use makeRemoved() so that it can be used directly in
       substitute(). */
    if (matchVarPat(pat, name))
        map.set(name, makeRemoved());
    else if (matchAttrsPat(pat, formals, ellipsis, name)) {
        if (name != sNoAlias) map.set(name, makeRemoved());
        for (ATermIterator i(formals); i; ++i) {
            ATerm d1;
            if (!matchFormal(*i, name, d1)) abort();
            map.set(name, makeRemoved());
        }
    }
    else abort();
}


/* 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)
{
    if (done.find(e) != done.end()) return;
    done.insert(e);
    
    ATerm name, pos, value;
    ATerm with, body;
    ATermList rbnds, nrbnds;
    Pattern pat;

    /* 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)) {
        if (!defs.get(name))
            throw EvalError(format("undefined variable `%1%'")
                % aterm2String(name));
    }

    else if (matchFunction(e, pat, body, pos)) {
        ATermMap defs2(defs);
        varsBoundByPattern(defs2, pat);
        set<Expr> done2;
        checkVarDefs2(done2, defs2, pat);
        checkVarDefs2(done2, defs2, body);
    }
        
    else if (matchRec(e, rbnds, nrbnds)) {
        checkVarDefs2(done, defs, (ATerm) nrbnds);
        ATermMap defs2(defs);
        for (ATermIterator i(rbnds); i; ++i) {
            if (!matchBind(*i, name, value, pos)) abort(); /* can't happen */
            defs2.set(name, (ATerm) ATempty);
        }
        for (ATermIterator i(nrbnds); i; ++i) {
            if (!matchBind(*i, name, value, pos)) abort(); /* can't happen */
            defs2.set(name, (ATerm) ATempty);
        }
        set<Expr> done2;
        checkVarDefs2(done2, defs2, (ATerm) rbnds);
    }

    else if (matchWith(e, with, body, pos)) {
        /* 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'. */
        checkVarDefs2(done, defs, with);
    }
    
    else if (ATgetType(e) == AT_APPL) {
        int arity = ATgetArity(ATgetAFun(e));
        for (int i = 0; i < arity; ++i)
            checkVarDefs2(done, defs, ATgetArgument(e, i));
    }

    else if (ATgetType(e) == AT_LIST)
        for (ATermIterator i((ATermList) e); i; ++i)
            checkVarDefs2(done, defs, *i);
}


void checkVarDefs(const ATermMap & defs, Expr e)
{
    set<Expr> done;
    checkVarDefs2(done, defs, e);
}
#endif


}