about summary refs log blame commit diff
path: root/src/libexpr/nixexpr.hh
blob: 298a9f0b949bd76272f39d9b41def8b502fcbf80 (plain) (tree)
1
2
3
4
5
6
7
8
9
10

                   
 

              




                   
                                                                  



                                                                  

                  
 








                                                                 
                                                                          


                                   
                                                 
        









                                              






                                                     


                                    


  





                                                                      
























                                                                   


                          


                                                                      



                                            

                                            

                                                            
                                                                   



                                                                    
                                                         

                                                             
                                       

                                                      
                                                   
 



                                                                   


                                                  
 
                         
#ifndef __NIXEXPR_H
#define __NIXEXPR_H

#include <map>

#include <aterm2.h>

#include "util.hh"


/* Nix expressions are represented as ATerms.  The maximal sharing
   property of the ATerm library allows us to implement caching of
   normals forms efficiently. */
typedef ATerm Expr;

typedef ATerm Pos;


/* Mappings from ATerms to ATerms.  This is just a wrapper around
   ATerm tables. */
class ATermMap
{
private:
    unsigned int maxLoadPct;
    ATermTable table;
    
public:
    ATermMap(unsigned int initialSize = 64, unsigned int maxLoadPct = 75);
    ATermMap(const ATermMap & map);
    ~ATermMap();

    ATermMap & operator = (const ATermMap & map);
        
    void set(ATerm key, ATerm value);
    void set(const string & key, ATerm value);

    ATerm get(ATerm key) const;
    ATerm get(const string & key) const;

    void remove(ATerm key);
    void remove(const string & key);

    ATermList keys() const;

    void add(const ATermMap & map);
    
    void reset();

private:
    void add(const ATermMap & map, ATermList & keys);

    void free();
    void copy(const ATermMap & map);
};


/* A STL vector of ATerms.  Should be used with great care since it's
   stored on the heap, and the elements are therefore not roots to the
   ATerm garbage collector. */
typedef vector<ATerm> ATermVector;


/* A substitution is a linked list of ATermMaps that map names to
   identifiers.  We use a list of ATermMaps rather than a single to
   make it easy to grow or shrink a substitution when entering a
   scope. */
struct Substitution
{
    ATermMap * map;
    const Substitution * prev;

    Substitution(const Substitution * prev, ATermMap * map)
    {
        this->prev = prev;
        this->map = map;
    }
    
    Expr lookup(Expr name) const
    {
        Expr x;
        for (const Substitution * s(this); s; s = s->prev)
            if (x = s->map->get(name)) return x;
        return 0;
    }
};


/* Show a position. */
string showPos(ATerm pos);

/* Generic bottomup traversal over ATerms.  The traversal first
   recursively descends into subterms, and then applies the given term
   function to the resulting term. */
struct TermFun
{
    virtual ATerm operator () (ATerm e) = 0;
};
ATerm bottomupRewrite(TermFun & f, ATerm e);

/* Query all attributes in an attribute set expression.  The
   expression must be in normal form. */
void queryAllAttrs(Expr e, ATermMap & attrs, bool withPos = false);

/* Query a specific attribute from an attribute set expression.  The
   expression must be in normal form. */
Expr queryAttr(Expr e, const string & name);
Expr queryAttr(Expr e, const string & name, ATerm & pos);

/* Create an attribute set expression from an Attrs value. */
Expr makeAttrs(const ATermMap & attrs);

/* Perform a set of substitutions on an expression. */
Expr substitute(const Substitution & subs, Expr e);

/* Check whether all variables are defined in the given expression.
   Throw an exception if this isn't the case. */
void checkVarDefs(const ATermMap & def, Expr e);

/* Create an expression representing a boolean. */
Expr makeBool(bool b);


#endif /* !__NIXEXPR_H */