1 : #ifndef __NIXEXPR_H
2 : #define __NIXEXPR_H
3 :
4 : #include <map>
5 :
6 : #include "aterm-map.hh"
7 : #include "types.hh"
8 :
9 :
10 : namespace nix {
11 :
12 :
13 36 : MakeError(EvalError, Error)
14 38 : MakeError(AssertionError, EvalError)
15 2 : MakeError(Abort, EvalError)
16 6 : MakeError(TypeError, EvalError)
17 :
18 :
19 : /* Nix expressions are represented as ATerms. The maximal sharing
20 : property of the ATerm library allows us to implement caching of
21 : normals forms efficiently. */
22 : typedef ATerm Expr;
23 :
24 : typedef ATerm DefaultValue;
25 : typedef ATerm ValidValues;
26 :
27 : typedef ATerm Pos;
28 :
29 :
30 : /* A STL vector of ATerms. Should be used with great care since it's
31 : stored on the heap, and the elements are therefore not roots to the
32 : ATerm garbage collector. */
33 : typedef vector<ATerm> ATermVector;
34 :
35 :
36 : /* A substitution is a linked list of ATermMaps that map names to
37 : identifiers. We use a list of ATermMaps rather than a single to
38 : make it easy to grow or shrink a substitution when entering a
39 : scope. */
40 : struct Substitution
41 : {
42 : ATermMap * map;
43 : const Substitution * prev;
44 :
45 : Substitution(const Substitution * prev, ATermMap * map)
46 1037 : {
47 1037 : this->prev = prev;
48 1037 : this->map = map;
49 : }
50 :
51 : Expr lookup(Expr name) const
52 2545 : {
53 2545 : Expr x;
54 4341 : for (const Substitution * s(this); s; s = s->prev)
55 3563 : if ((x = s->map->get(name))) return x;
56 778 : return 0;
57 : }
58 : };
59 :
60 :
61 : /* Show a position. */
62 : string showPos(ATerm pos);
63 :
64 : /* Generic bottomup traversal over ATerms. The traversal first
65 : recursively descends into subterms, and then applies the given term
66 : function to the resulting term. */
67 : struct TermFun
68 : {
69 72 : virtual ~TermFun() { }
70 : virtual ATerm operator () (ATerm e) = 0;
71 : };
72 : ATerm bottomupRewrite(TermFun & f, ATerm e);
73 :
74 : /* Query all attributes in an attribute set expression. The
75 : expression must be in normal form. */
76 : void queryAllAttrs(Expr e, ATermMap & attrs, bool withPos = false);
77 :
78 : /* Query a specific attribute from an attribute set expression. The
79 : expression must be in normal form. */
80 : Expr queryAttr(Expr e, const string & name);
81 : Expr queryAttr(Expr e, const string & name, ATerm & pos);
82 :
83 : /* Create an attribute set expression from an Attrs value. */
84 : Expr makeAttrs(const ATermMap & attrs);
85 :
86 : /* Perform a set of substitutions on an expression. */
87 : Expr substitute(const Substitution & subs, Expr e);
88 :
89 : /* Check whether all variables are defined in the given expression.
90 : Throw an exception if this isn't the case. */
91 : void checkVarDefs(const ATermMap & def, Expr e);
92 :
93 : /* Create an expression representing a boolean. */
94 : Expr makeBool(bool b);
95 :
96 : string showType(Expr e);
97 :
98 : string showValue(Expr e);
99 :
100 :
101 : }
102 :
103 :
104 : #endif /* !__NIXEXPR_H */
|