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 40 : MakeError(EvalError, Error)
14 2 : MakeError(AssertionError, EvalError)
15 0 : MakeError(ThrownError, AssertionError)
16 2 : MakeError(Abort, EvalError)
17 4 : MakeError(TypeError, EvalError)
18 :
19 :
20 : /* Nix expressions are represented as ATerms. The maximal sharing
21 : property of the ATerm library allows us to implement caching of
22 : normals forms efficiently. */
23 : typedef ATerm Expr;
24 : typedef ATerm DefaultValue;
25 : typedef ATerm Pos;
26 : typedef ATerm Pattern;
27 : typedef ATerm ATermBool;
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 4592 : Substitution(const Substitution * prev, ATermMap * map)
46 : {
47 4592 : this->prev = prev;
48 4592 : this->map = map;
49 4592 : }
50 :
51 14311 : Expr lookup(Expr name) const
52 : {
53 : Expr x;
54 22424 : for (const Substitution * s(this); s; s = s->prev)
55 20821 : if ((x = s->map->get(name))) return x;
56 1603 : 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 195 : {
69 195 : virtual ~TermFun() { }
70 : virtual ATerm operator () (ATerm e) = 0;
71 : };
72 : ATerm bottomupRewrite(TermFun & f, ATerm e);
73 :
74 :
75 : /* Query all attributes in an attribute set expression. The
76 : expression must be in normal form. */
77 : void queryAllAttrs(Expr e, ATermMap & attrs, bool withPos = false);
78 :
79 : /* Query a specific attribute from an attribute set expression. The
80 : expression must be in normal form. */
81 : Expr queryAttr(Expr e, const string & name);
82 : Expr queryAttr(Expr e, const string & name, ATerm & pos);
83 :
84 : /* Create an attribute set expression from an Attrs value. */
85 : Expr makeAttrs(const ATermMap & attrs);
86 :
87 :
88 : /* Perform a set of substitutions on an expression. */
89 : Expr substitute(const Substitution & subs, Expr e);
90 :
91 :
92 : /* Check whether all variables are defined in the given expression.
93 : Throw an exception if this isn't the case. */
94 : void checkVarDefs(const ATermMap & def, Expr e);
95 :
96 :
97 : /* Canonicalise a Nix expression by sorting attributes and removing
98 : location information. */
99 : Expr canonicaliseExpr(Expr e);
100 :
101 :
102 : /* Create an expression representing a boolean. */
103 : Expr makeBool(bool b);
104 :
105 :
106 : /* Manipulation of Str() nodes. Note: matchStr() does not clear
107 : context! */
108 : bool matchStr(Expr e, string & s, PathSet & context);
109 :
110 : Expr makeStr(const string & s, const PathSet & context = PathSet());
111 :
112 :
113 : /* Showing types, values. */
114 : string showType(Expr e);
115 :
116 : string showValue(Expr e);
117 :
118 :
119 : }
120 :
121 :
122 : #endif /* !__NIXEXPR_H */
|