CompareExprTree.hpp

Go to the documentation of this file.
00001 
00015 #ifndef ExprTree_H
00016 #define ExprTree_H
00017 #include<set>
00018 #include <map>
00019 
00020 // OpenAnalysis headers
00021 #include <OpenAnalysis/IRInterface/IRHandles.hpp>
00022 #include <OpenAnalysis/Utils/OA_ptr.hpp>
00023 #include <OpenAnalysis/Utils/Tree.hpp>
00024 //#include <OpenAnalysis/ReachConsts/Interface.hpp>
00025 #include <OpenAnalysis/IRInterface/ConstValBasicInterface.hpp>
00026 
00027 
00028 namespace OA {
00029 using namespace std;
00030 
00031 class ExprTree;
00032 
00033 // to avoid circular reference in header files
00034 class ExprTreeVisitor;
00035 
00036 typedef std::set<OA_ptr<ExprTree> > ExprTreeSet;
00037 
00039 OA_ptr<std::set<OA_ptr<ExprTree> > >
00040 intersectExprTreeSets(std::set<OA_ptr<ExprTree> >& set1,
00041                  std::set<OA_ptr<ExprTree> >& set2);
00042 
00044 OA_ptr<std::set<OA_ptr<ExprTree> > >
00045     unionExprTreeSets(
00046         std::set<OA_ptr<ExprTree> >& set1,
00047         std::set<OA_ptr<ExprTree> >& set2);
00048 
00049 
00050 
00051 //--------------------------------------------------------------------
00055   class ExprTree : public Tree {
00056 public:
00057   class Node;
00058   class Edge;
00059 private:
00060 static const int sOrder = -200;
00061 
00062 public:
00063   ExprTree();
00064   virtual ~ExprTree();
00065   
00066   OA_ptr<Node>  getRoot() { 
00067     OA_ptr<Tree::Node> n = Tree::getRoot();
00068     return n.convert<Node>(); 
00069   }
00070 
00071 
00073   void acceptVisitor(ExprTreeVisitor& pVisitor);
00074 
00075   //-------------------------------------
00076   // construction
00077   //-------------------------------------
00078   void connect(OA_ptr<Node> src, OA_ptr<Node> dst) 
00079       { OA_ptr<Edge> e; e = new Edge(src,dst); addEdge(e); }
00080   void disconnect(OA_ptr<Edge> e) { removeEdge(e.convert<Tree::Edge>()); }
00081   void copyAndConnectSubTree(OA_ptr<Node> src, OA_ptr<ExprTree> subtree);
00082   
00084   bool operator<(ExprTree &other);
00085 
00086 
00089   bool operator==(ExprTree &other);
00090   virtual int getOrder() { return sOrder; }
00091 
00092 
00093   //--------------------------------------------------------
00094   class Node : public Tree::Node {
00095   public:
00096     Node()
00097       : Tree::Node() { }
00098     Node(const std::string& x)
00099       : Tree::Node(), mName(x) { }
00100     virtual ~Node() { }
00101     
00102     // A name for this node (useful for debugging)
00103     std::string& getName() { return mName; }
00104 
00106     virtual OA_ptr<Node> copy() = 0;
00107 
00109     virtual bool isaOpNode() { return false; }
00110     virtual bool isaCallNode() { return false; }
00111     virtual bool isaMemRefNode() { return false; }
00112     virtual bool isaConstSymNode() { return false; }
00113     virtual bool isaConstValNode() { return false; }
00114 
00116     // NOTE: could give this a default implementation if wanted to
00117     // have ExprTree visitors have default implementation for nodes
00118     // they don't know about
00119     virtual void acceptVisitor(ExprTreeVisitor&) = 0;
00120     virtual void dump(std::ostream& os) { Tree::Node::dump(os); }
00121     virtual void dump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00122 
00123     //*****************************************************************
00124     // Annotation Interface
00125     //*****************************************************************
00126     virtual void output(IRHandlesIRInterface& ir) { Tree::Node::output(ir); }
00127   private:
00128     std::string mName;
00129     //std::string mAttr;
00130   }; // end of Node
00131 
00132   class OpNode : public Node {
00133   public:
00134     OpNode() : Node("OpNode") { }
00135     OpNode(OpHandle h) : Node("OpNode"), mHandle(h) {}
00136     virtual ~OpNode() { }
00137 
00139     OA_ptr<Node> copy() 
00140       { OA_ptr<Node> ret; ret = new OpNode(mHandle); return ret; }
00141 
00142     bool isaOpNode() { return true; }
00143     OpHandle getHandle() { return mHandle; }
00144     void dump(std::ostream& os) { Node::dump(os); }
00145     void dump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00146 
00147     void acceptVisitor(ExprTreeVisitor& pVisitor);
00148 
00149     //*****************************************************************
00150     // Annotation Interface
00151     //*****************************************************************
00152     void output(IRHandlesIRInterface& ir) ;
00154    bool operator<(Node& other);
00155 
00158    bool operator==(Node& other);
00159    virtual int getOrder() { return sOrder; }
00160 
00161   private:
00162     static const int sOrder = 100;
00163     OpHandle mHandle;
00164 
00165   };
00166 
00167   class CallNode : public Node {
00168   public:
00169     CallNode() : Node("CallNode") { }
00170     CallNode(CallHandle h) : Node("CallNode"), mHandle(h) {}
00171     virtual ~CallNode() { }
00172 
00174     OA_ptr<Node> copy() 
00175       { OA_ptr<Node> ret; ret = new CallNode(mHandle); return ret; }
00176 
00177     bool isaCallNode() { return true; }
00178     CallHandle getHandle() { return mHandle; }
00179     void dump(std::ostream& os) { Node::dump(os); }
00180     void dump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00181 
00182     void acceptVisitor(ExprTreeVisitor& pVisitor);
00183 
00184     //*****************************************************************
00185     // Annotation Interface
00186     //*****************************************************************
00187     void output(IRHandlesIRInterface& ir) ;
00189     bool operator<(Node& other);
00190 
00193     bool operator==(Node& other);
00194     virtual int getOrder() { return sOrder; }
00195 
00196   private:
00197     //Changed from ExprHandle to CallHandle by LMR. 6.8.06
00198      static const int sOrder = 500;
00199     CallHandle mHandle;
00200   };
00201 
00202   class MemRefNode : public Node {
00203   public:
00204     MemRefNode() : Node("MemRefNode") { }
00205     MemRefNode(MemRefHandle h) : Node("MemRefNode"), mHandle(h) {}
00206     virtual ~MemRefNode() { }
00207 
00209     OA_ptr<Node> copy() 
00210       { OA_ptr<Node> ret; ret = new MemRefNode(mHandle); return ret; }
00211 
00212     bool isaMemRefNode() { return true; }
00213     MemRefHandle getHandle() { return mHandle; }
00214     void dump(std::ostream& os) { Node::dump(os); }
00215     void dump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00216 
00217     void acceptVisitor(ExprTreeVisitor& pVisitor);
00218 
00219     //*****************************************************************
00220     // Annotation Interface
00221     //*****************************************************************
00222     void output(IRHandlesIRInterface& ir) ;
00224    bool operator<(Node& other);
00225 
00228    bool operator==(Node& other);
00229    virtual int getOrder() { return sOrder; }
00230 
00231   private:
00232    static const int sOrder = 200;
00233     MemRefHandle mHandle;
00234   };
00235 
00236   class ConstSymNode : public Node {
00237   public:
00238     ConstSymNode() : Node("ConstSymNode") { }
00239     ConstSymNode(ConstSymHandle h) : Node("ConstSymNode"), mHandle(h) {}
00240     virtual ~ConstSymNode() { }
00241 
00243     OA_ptr<Node> copy() 
00244       { OA_ptr<Node> ret; ret = new ConstSymNode(mHandle); return ret; }
00245 
00246     bool isaConstSymNode() { return true; }
00247     ConstSymHandle getHandle() { return mHandle; }
00248     void dump(std::ostream& os) { Node::dump(os); }
00249     void dump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00250 
00251     void acceptVisitor(ExprTreeVisitor& pVisitor);
00252 
00253     //*****************************************************************
00254     // Annotation Interface
00255     //*****************************************************************
00256     void output(IRHandlesIRInterface& ir) ;
00258     bool operator<(Node& other);
00259 
00262     bool operator==(Node& other);
00263     virtual int getOrder() { return sOrder; }
00264 
00265   private:
00266  static const int sOrder = 300;
00267     ConstSymHandle mHandle;
00268   };
00269 
00270   class ConstValNode : public Node {
00271   public:
00272     ConstValNode() : Node("ConstValNode") { }
00273     ConstValNode(ConstValHandle h) : Node("ConstValNode"), mHandle(h) {}
00274     virtual ~ConstValNode() { }
00275 
00277     OA_ptr<Node> copy() 
00278       { OA_ptr<Node> ret; ret = new ConstValNode(mHandle); return ret; }
00279 
00280     bool isaConstValNode() { return true; }
00281     ConstValHandle getHandle() { return mHandle; }
00282     void dump(std::ostream& os) { Node::dump(os); }
00283     void dump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00284 
00285     void acceptVisitor(ExprTreeVisitor& pVisitor);
00286 
00287     //*****************************************************************
00288     // Annotation Interface
00289     //*****************************************************************
00290     void output(IRHandlesIRInterface& ir) ;
00292    bool operator<(Node& other);
00293 
00296    bool operator==(Node& other);
00297    virtual int getOrder() { return sOrder; }
00298 
00299   private:
00300  static const int sOrder = 400;
00301     ConstValHandle mHandle;
00302   };
00303   
00304   //--------------------------------------------------------
00305   class Edge : public Tree::Edge {
00306   public:
00307     Edge(OA_ptr<Node> n1, OA_ptr<Node> n2)
00308       : Tree::Edge(n1, n2) { }
00309     virtual ~Edge() { }
00310     
00311     // included here to return subclass node type
00312     OA_ptr<Node>  parent () const { 
00313       OA_ptr<Tree::Node> n = Tree::Edge::parent();
00314       return n.convert<Node>();
00315     }
00316     OA_ptr<Node>  source () const { return parent(); }
00317     OA_ptr<Node>  tail () const { return parent(); }
00318     OA_ptr<Node>  child () const { 
00319       OA_ptr<Tree::Node> n = Tree::Edge::child();
00320       return n.convert<Node>(); 
00321     }
00322     OA_ptr<Node>  sink () const { return child(); }
00323     OA_ptr<Node>  head () const { return child(); }
00324 
00325     void dump(std::ostream& os);
00326     void dump() { dump(std::cout); }
00327 
00328   private:
00329   };  
00330   //--------------------------------------------------------
00331   
00332   //------------------------------------------------------------------
00339   class NodesIterator : public Tree::NodesIterator {
00340   public:
00341     NodesIterator (Tree& t) : Tree::NodesIterator(t) {}
00342     virtual ~NodesIterator () {}
00343     OA_ptr<Node>  current() const { 
00344       OA_ptr<Tree::Node> n = Tree::NodesIterator::current();
00345       return n.convert<Node>(); 
00346     }
00347   };
00348   
00349   //------------------------------------------------------------------
00356   class EdgesIterator : public Tree::EdgesIterator {
00357   public:
00358     EdgesIterator (Tree& t) : Tree::EdgesIterator(t) {}
00359     virtual ~EdgesIterator () {}
00360     OA_ptr<Edge>  current() const { 
00361       OA_ptr<Tree::Edge> e = Tree::EdgesIterator::current();
00362       return e.convert<Edge>(); 
00363     }
00364   };
00365 
00366   //------------------------------------------------------------------
00370   class OutEdgesIterator : public Tree::OutEdgesIterator {
00371   public:
00372     OutEdgesIterator (Node&  n) : Tree::OutEdgesIterator(n) {}
00373     virtual ~OutEdgesIterator () {}
00374     OA_ptr<Edge>  current() const { 
00375       OA_ptr<Tree::Edge> e = Tree::OutEdgesIterator::current();
00376       return e.convert<Edge>(); 
00377     }
00378   };
00379   
00380   //------------------------------------------------------------------
00385   class ChildNodesIterator : public Tree::ChildNodesIterator {
00386   public:
00387     ChildNodesIterator (Node&  n) : Tree::ChildNodesIterator(n) {}
00388     ~ChildNodesIterator () {}
00389     OA_ptr<Node>  current() const { 
00390       OA_ptr<Tree::Node> n = Tree::ChildNodesIterator::current();
00391       return n.convert<Node>();
00392     }
00393   };
00394  
00395   void str(std::ostream& os);
00396   
00397   void dump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00398   //void dump() { dump(std::cout); }
00399 
00400   //*****************************************************************
00401   // Annotation Interface
00402   //*****************************************************************
00403   void output(IRHandlesIRInterface& ir) ;
00404 
00405   //------------------------------------------------------------------
00409   class PreOrderIterator : public Tree::PreOrderIterator {
00410   public:
00411     PreOrderIterator (Tree& t) : Tree::PreOrderIterator(t) {}
00412     virtual ~PreOrderIterator () {}
00413     OA_ptr<Node>  current() const { 
00414       OA_ptr<Tree::Node> n = PreOrderIterator::current();
00415       return n.convert<Node>();
00416     }
00417   };
00418   //------------------------------------------------------------------
00422   class PostOrderIterator : public Tree::PostOrderIterator {
00423   public:
00424     PostOrderIterator (Tree& t) : Tree::PostOrderIterator(t) {}
00425     virtual ~PostOrderIterator () {}
00426     OA_ptr<Node>  current() const { 
00427       OA_ptr<Tree::Node> n = PostOrderIterator::current();
00428       return n.convert<Node>();
00429     }
00430   };
00431   //------------------------------------------------------------------
00435   class ReversePostOrderIterator : public Tree::ReversePostOrderIterator {
00436   public:
00437     ReversePostOrderIterator (Tree& t) : Tree::ReversePostOrderIterator(t) {}
00438     virtual ~ReversePostOrderIterator () {}
00439     OA_ptr<Node>  current() const { 
00440       OA_ptr<Tree::Node> n = Tree::ReversePostOrderIterator::current();
00441       return n.convert<Node>(); 
00442     }
00443   };
00444  //--------------------------------------------------------------------
00445 
00446 };
00447 
00448 
00449 } // end of OA namespace
00450 
00451 #endif