about summary refs log tree commit diff
path: root/src/libexpr/nixexpr.cc
blob: 37288804d0eba77ed077c55117f6c7f9890e0c67 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include "nixexpr.hh"
#include "storeexpr.hh"


ATermMap::ATermMap(unsigned int initialSize, unsigned int maxLoadPct)
{
    this->maxLoadPct = maxLoadPct;
    table = ATtableCreate(initialSize, maxLoadPct);
    if (!table) throw Error("cannot create ATerm table");
}


ATermMap::ATermMap(const ATermMap & map)
    : table(0)
{
    ATermList keys = map.keys();

    /* !!! adjust allocation for load pct */
    maxLoadPct = map.maxLoadPct;
    table = ATtableCreate(ATgetLength(keys), maxLoadPct);
    if (!table) throw Error("cannot create ATerm table");

    add(map, keys);
}


ATermMap::~ATermMap()
{
    if (table) ATtableDestroy(table);
}


void ATermMap::set(ATerm key, ATerm value)
{
    return ATtablePut(table, key, value);
}


void ATermMap::set(const string & key, ATerm value)
{
    set(string2ATerm(key), value);
}


ATerm ATermMap::get(ATerm key) const
{
    return ATtableGet(table, key);
}


ATerm ATermMap::get(const string & key) const
{
    return get(string2ATerm(key));
}


void ATermMap::remove(ATerm key)
{
    ATtableRemove(table, key);
}


void ATermMap::remove(const string & key)
{
    remove(string2ATerm(key));
}


ATermList ATermMap::keys() const
{
    ATermList keys = ATtableKeys(table);
    if (!keys) throw Error("cannot query aterm map keys");
    return keys;
}


void ATermMap::add(const ATermMap & map)
{
    ATermList keys = map.keys();
    add(map, keys);
}


void ATermMap::add(const ATermMap & map, ATermList & keys)
{
    for (ATermIterator i(keys); i; ++i)
        set(*i, map.get(*i));
}


void ATermMap::reset()
{
    ATtableReset(table);
}


ATerm string2ATerm(const string & s)
{
    return (ATerm) ATmakeAppl0(ATmakeAFun((char *) s.c_str(), 0, ATtrue));
}


string aterm2String(ATerm t)
{
    return ATgetName(ATgetAFun(t));
}


string showPos(ATerm pos)
{
    ATMatcher m;
    Path path;
    int line, column;
    if (atMatch(m, pos) >> "NoPos")
        return "undefined position";
    if (!(atMatch(m, pos) >> "Pos" >> path >> line >> column))
        throw badTerm("position expected", pos);
    return (format("`%1%', line %2%") % path % line).str();
}
    

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

    if (ATgetType(e) == AT_APPL) {
        AFun fun = ATgetAFun(e);
        int arity = ATgetArity(fun);
        ATermList args = ATempty;

        for (int i = arity - 1; i >= 0; i--)
            args = ATinsert(args, bottomupRewrite(f, ATgetArgument(e, i)));
        
        e = (ATerm) ATmakeApplList(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);
}


void queryAllAttrs(Expr e, ATermMap & attrs, bool withPos)
{
    ATMatcher m;
    ATermList bnds;
    if (!(atMatch(m, e) >> "Attrs" >> bnds))
        throw Error("attribute set expected");

    for (ATermIterator i(bnds); i; ++i) {
        string s;
        Expr e;
        ATerm pos;
        if (!(atMatch(m, *i) >> "Bind" >> s >> e >> pos))
            abort(); /* can't happen */
        attrs.set(s, withPos ? ATmake("(<term>, <term>)", e, pos) : e);
    }
}


Expr queryAttr(Expr e, const string & name)
{
    ATerm dummy;
    return queryAttr(e, name, dummy);
}


Expr queryAttr(Expr e, const string & name, ATerm & pos)
{
    ATMatcher m;
    ATermList bnds;
    if (!(atMatch(m, e) >> "Attrs" >> bnds))
        throw Error("attribute set expected");

    for (ATermIterator i(bnds); i; ++i) {
        string s;
        Expr e;
        ATerm pos2;
        if (!(atMatch(m, *i) >> "Bind" >> s >> e >> pos2))
            abort(); /* can't happen */
        if (s == name) {
            pos = pos2;
            return e;
        }
    }

    return 0;
}


Expr makeAttrs(const ATermMap & attrs)
{
    ATMatcher m;
    ATermList bnds = ATempty;
    for (ATermIterator i(attrs.keys()); i; ++i) {
        Expr e;
        ATerm pos;
        if (!(atMatch(m, attrs.get(*i)) >> "" >> e >> pos))
            abort(); /* can't happen */
        bnds = ATinsert(bnds, 
            ATmake("Bind(<term>, <term>, <term>)", *i, e, pos));
    }
    return ATmake("Attrs(<term>)", ATreverse(bnds));
}


Expr substitute(const ATermMap & subs, Expr e)
{
    checkInterrupt();

    ATMatcher m;
    ATerm name, pos;

    /* As an optimisation, don't substitute in subterms known to be
       closed. */
    if (atMatch(m, e) >> "Closed") return e;

    if (atMatch(m, e) >> "Var" >> name) {
        Expr sub = subs.get(name);
        return sub ? ATmake("Closed(<term>)", sub) : e;
    }

    /* In case of a function, filter out all variables bound by this
       function. */
    ATermList formals;
    ATerm body;
    if (atMatch(m, e) >> "Function" >> formals >> body >> pos) {
        ATermMap subs2(subs);
        for (ATermIterator i(formals); i; ++i) {
            if (!(atMatch(m, *i) >> "NoDefFormal" >> name) &&
                !(atMatch(m, *i) >> "DefFormal" >> name))
                abort();
            subs2.remove(name);
        }
        return ATmake("Function(<term>, <term>, <term>)",
            substitute(subs, (ATerm) formals),
            substitute(subs2, body), pos);
    }

    if (atMatch(m, e) >> "Function1" >> name >> body >> pos) {
        ATermMap subs2(subs);
        subs2.remove(name);
        return ATmake("Function1(<term>, <term>, <term>)", name,
            substitute(subs2, body), pos);
    }
        
    /* Idem for a mutually recursive attribute set. */
    ATermList rbnds, nrbnds;
    if (atMatch(m, e) >> "Rec" >> rbnds >> nrbnds) {
        ATermMap subs2(subs);
        for (ATermIterator i(rbnds); i; ++i)
            if (atMatch(m, *i) >> "Bind" >> name)
                subs2.remove(name);
            else abort(); /* can't happen */
        for (ATermIterator i(nrbnds); i; ++i)
            if (atMatch(m, *i) >> "Bind" >> name)
                subs2.remove(name);
            else abort(); /* can't happen */
        return ATmake("Rec(<term>, <term>)",
            substitute(subs2, (ATerm) rbnds),
            substitute(subs, (ATerm) nrbnds));
    }

    if (ATgetType(e) == AT_APPL) {
        AFun fun = ATgetAFun(e);
        int arity = ATgetArity(fun);
        ATermList args = ATempty;

        for (int i = arity - 1; i >= 0; i--)
            args = ATinsert(args, substitute(subs, ATgetArgument(e, i)));
        
        return (ATerm) ATmakeApplList(fun, args);
    }

    if (ATgetType(e) == AT_LIST) {
        ATermList out = ATempty;
        for (ATermIterator i((ATermList) e); i; ++i)
            out = ATinsert(out, substitute(subs, *i));
        return (ATerm) ATreverse(out);
    }

    return e;
}


void checkVarDefs(const ATermMap & defs, Expr e)
{
    ATMatcher m;
    ATerm name;
    ATermList formals;
    ATerm with, body;
    ATermList rbnds, nrbnds;

    if (atMatch(m, e) >> "Var" >> name) {
        if (!defs.get(name))
            throw Error(format("undefined variable `%1%'")
                % aterm2String(name));
    }

    else if (atMatch(m, e) >> "Function" >> formals >> body) {
        ATermMap defs2(defs);
        for (ATermIterator i(formals); i; ++i) {
            Expr deflt;
            if (!(atMatch(m, *i) >> "NoDefFormal" >> name))
                if (atMatch(m, *i) >> "DefFormal" >> name >> deflt)
                    checkVarDefs(defs, deflt);
                else
                    abort();
            defs2.set(name, (ATerm) ATempty);
        }
        checkVarDefs(defs2, body);
    }
        
    else if (atMatch(m, e) >> "Function1" >> name >> body) {
        ATermMap defs2(defs);
        defs2.set(name, (ATerm) ATempty);
        checkVarDefs(defs2, body);
    }
        
    else if (atMatch(m, e) >> "Rec" >> rbnds >> nrbnds) {
        checkVarDefs(defs, (ATerm) nrbnds);
        ATermMap defs2(defs);
        for (ATermIterator i(rbnds); i; ++i) {
            if (!(atMatch(m, *i) >> "Bind" >> name))
                abort(); /* can't happen */
            defs2.set(name, (ATerm) ATempty);
        }
        for (ATermIterator i(nrbnds); i; ++i) {
            if (!(atMatch(m, *i) >> "Bind" >> name))
                abort(); /* can't happen */
            defs2.set(name, (ATerm) ATempty);
        }
        checkVarDefs(defs2, (ATerm) rbnds);
    }

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

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


Expr makeBool(bool b)
{
    return b ? ATmake("Bool(True)") : ATmake("Bool(False)");
}


Expr makeString(const string & s)
{
    return ATmake("Str(<str>)", s.c_str());
}


Expr makePath(const Path & path)
{
    return ATmake("Path(<str>)", path.c_str());
}