00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 #include "statement.h"
00022 #include "exp.h"
00023 #include <sstream>
00024 
00025 
00026 
00027 class ConstraintMap {
00028     std::map<Exp*, Exp*, lessExpStar> cmap;
00029 public:
00030 typedef std::map<Exp*, Exp*, lessExpStar>::iterator iterator;
00031 
00032     
00033     bool isFound(Exp* e)  {return cmap.find(e) != cmap.end();}
00034     
00035     iterator find(Exp* e) {return cmap.find(e);}
00036     
00037     Exp*& operator[](Exp* e) {return cmap[e];}
00038     
00039     int   size() {return cmap.size();}
00040     
00041     void  clear() {cmap.clear();}
00042     
00043     iterator begin() {return cmap.begin();}
00044     iterator end()   {return cmap.end();}
00045     
00046     void constrain(Exp* loc1, Exp* loc2) {
00047         cmap[new Unary(opTypeOf, loc1)] = new Unary(opTypeOf, loc2);}
00048     
00049     
00050     void insert(Exp* term);
00051     
00052     void insert(Exp* lhs, Exp* rhs) {cmap[lhs] = rhs;}
00053     
00054     void constrain(Exp* loc, Type* t) {
00055         cmap[new Unary(opTypeOf, loc)] = new TypeVal(t);}
00056     
00057     void constrain(Type* t1, Type* t2) { 
00058         cmap[new TypeVal(t1)] = new TypeVal(t2);}
00059     
00060     void makeUnion(ConstraintMap& o);
00061     
00062     void print(std::ostream& os);
00063     
00064     char* prints();
00065     
00066     void    substitute(ConstraintMap& other);
00067     
00068     
00069     
00070     
00071     void    substAlpha();
00072 };  
00073 
00074 
00075 
00076 
00077 
00078 class EquateMap {
00079     std::map<Exp*, LocationSet, lessExpStar> emap;
00080 public:
00081 typedef std::map<Exp*, LocationSet, lessExpStar>::iterator iterator;
00082     iterator begin() {return emap.begin();}
00083     iterator end()   {return emap.end();}
00084     void erase(iterator it) {emap.erase(it);}
00085     int  size() {return emap.size();}
00086     
00087     void addEquate(Exp* a, Exp* b) {
00088         emap[a].insert(b); emap[b].insert(a);}
00089     iterator find(Exp* e) {return emap.find(e);}
00090     void print(std::ostream& os);
00091     char* prints();
00092 };  
00093 
00094 class Constraints {
00095     LocationSet     conSet;
00096     std::list<Exp*> disjunctions;
00097     
00098     
00099     ConstraintMap fixed;
00100     
00101     EquateMap equates;
00102 
00103 public:
00104     Constraints() {}
00105     ~Constraints();
00106 
00107     void    print(std::ostream& os);
00108     char*   prints();
00109 
00110     LocationSet& getConstraints() {return conSet;}
00111     void    addConstraints(LocationSet& con) {conSet.makeUnion(con);}
00112     
00113     void    substIntoDisjuncts(ConstraintMap& in);
00114     
00115     void    substIntoEquates(ConstraintMap& in);
00116     
00117     
00118     
00119     void    alphaSubst();
00120 
00121     
00122     
00123     
00124     bool    solve(std::list<ConstraintMap>& solns);
00125 private:
00126     bool    doSolve(std::list<Exp*>::iterator it, ConstraintMap& extra,
00127               std::list<ConstraintMap>& solns);
00128     
00129     
00130     
00131     bool    unify(Exp* x, Exp* y, ConstraintMap& extra);
00132 };