ExprTree.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); }
00128   virtual bool operator<(Node& other);
00129 
00132   virtual bool operator==(Node& other);
00133 
00134   virtual int getOrder() { return sOrder; }
00135 
00136 
00137   private:
00138     static const int sOrder = -100;
00139     std::string mName;
00140     //std::string mAttr;
00141   }; // end of Node
00142 
00143   class OpNode : public Node {
00144   public:
00145     OpNode() : Node("OpNode") { }
00146     OpNode(OpHandle h) : Node("OpNode"), mHandle(h) {}
00147     virtual ~OpNode() { }
00148 
00150     OA_ptr<Node> copy() 
00151       { OA_ptr<Node> ret; ret = new OpNode(mHandle); return ret; }
00152 
00153     bool isaOpNode() { return true; }
00154     OpHandle getHandle() { return mHandle; }
00155     void dump(std::ostream& os) { Node::dump(os); }
00156     void dump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00157 
00158     void acceptVisitor(ExprTreeVisitor& pVisitor);
00159 
00160     //*****************************************************************
00161     // Annotation Interface
00162     //*****************************************************************
00163     void output(IRHandlesIRInterface& ir) ;
00165    bool operator<(Node& other);
00166 
00169    bool operator==(Node& other);
00170    virtual int getOrder() { return sOrder; }
00171 
00172   private:
00173     static const int sOrder = 100;
00174     OpHandle mHandle;
00175 
00176   };
00177 
00178   class CallNode : public Node {
00179   public:
00180     CallNode() : Node("CallNode") { }
00181     CallNode(CallHandle h) : Node("CallNode"), mHandle(h) {}
00182     virtual ~CallNode() { }
00183 
00185     OA_ptr<Node> copy() 
00186       { OA_ptr<Node> ret; ret = new CallNode(mHandle); return ret; }
00187 
00188     bool isaCallNode() { return true; }
00189     CallHandle getHandle() { return mHandle; }
00190     void dump(std::ostream& os) { Node::dump(os); }
00191     void dump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00192 
00193     void acceptVisitor(ExprTreeVisitor& pVisitor);
00194 
00195     //*****************************************************************
00196     // Annotation Interface
00197     //*****************************************************************
00198     void output(IRHandlesIRInterface& ir) ;
00200     bool operator<(Node& other);
00201 
00204     bool operator==(Node& other);
00205     virtual int getOrder() { return sOrder; }
00206 
00207   private:
00208     //Changed from ExprHandle to CallHandle by LMR. 6.8.06
00209      static const int sOrder = 500;
00210     CallHandle mHandle;
00211   };
00212 
00213   class MemRefNode : public Node {
00214   public:
00215     MemRefNode() : Node("MemRefNode") { }
00216     MemRefNode(MemRefHandle h) : Node("MemRefNode"), mHandle(h) {}
00217     virtual ~MemRefNode() { }
00218 
00220     OA_ptr<Node> copy() 
00221       { OA_ptr<Node> ret; ret = new MemRefNode(mHandle); return ret; }
00222 
00223     bool isaMemRefNode() { return true; }
00224     MemRefHandle getHandle() { return mHandle; }
00225     void dump(std::ostream& os) { Node::dump(os); }
00226     void dump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00227 
00228     void acceptVisitor(ExprTreeVisitor& pVisitor);
00229 
00230     //*****************************************************************
00231     // Annotation Interface
00232     //*****************************************************************
00233     void output(IRHandlesIRInterface& ir) ;
00235    bool operator<(Node& other);
00236 
00239    bool operator==(Node& other);
00240    virtual int getOrder() { return sOrder; }
00241 
00242   private:
00243    static const int sOrder = 200;
00244     MemRefHandle mHandle;
00245   };
00246 
00247   class ConstSymNode : public Node {
00248   public:
00249     ConstSymNode() : Node("ConstSymNode") { }
00250     ConstSymNode(ConstSymHandle h) : Node("ConstSymNode"), mHandle(h) {}
00251     virtual ~ConstSymNode() { }
00252 
00254     OA_ptr<Node> copy() 
00255       { OA_ptr<Node> ret; ret = new ConstSymNode(mHandle); return ret; }
00256 
00257     bool isaConstSymNode() { return true; }
00258     ConstSymHandle getHandle() { return mHandle; }
00259     void dump(std::ostream& os) { Node::dump(os); }
00260     void dump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00261 
00262     void acceptVisitor(ExprTreeVisitor& pVisitor);
00263 
00264     //*****************************************************************
00265     // Annotation Interface
00266     //*****************************************************************
00267     void output(IRHandlesIRInterface& ir) ;
00269     bool operator<(Node& other);
00270 
00273     bool operator==(Node& other);
00274     virtual int getOrder() { return sOrder; }
00275 
00276   private:
00277  static const int sOrder = 300;
00278     ConstSymHandle mHandle;
00279   };
00280 
00281   class ConstValNode : public Node {
00282   public:
00283     ConstValNode() : Node("ConstValNode") { }
00284     ConstValNode(ConstValHandle h) : Node("ConstValNode"), mHandle(h) {}
00285     virtual ~ConstValNode() { }
00286 
00288     OA_ptr<Node> copy() 
00289       { OA_ptr<Node> ret; ret = new ConstValNode(mHandle); return ret; }
00290 
00291     bool isaConstValNode() { return true; }
00292     ConstValHandle getHandle() { return mHandle; }
00293     void dump(std::ostream& os) { Node::dump(os); }
00294     void dump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00295 
00296     void acceptVisitor(ExprTreeVisitor& pVisitor);
00297 
00298     //*****************************************************************
00299     // Annotation Interface
00300     //*****************************************************************
00301     void output(IRHandlesIRInterface& ir) ;
00303    bool operator<(Node& other);
00304 
00307    bool operator==(Node& other);
00308    virtual int getOrder() { return sOrder; }
00309 
00310   private:
00311  static const int sOrder = 400;
00312     ConstValHandle mHandle;
00313   };
00314   
00315   //--------------------------------------------------------
00316   class Edge : public Tree::Edge {
00317   public:
00318     Edge(OA_ptr<Node> n1, OA_ptr<Node> n2)
00319       : Tree::Edge(n1, n2) { }
00320     virtual ~Edge() { }
00321     
00322     // included here to return subclass node type
00323     OA_ptr<Node>  parent () const { 
00324       OA_ptr<Tree::Node> n = Tree::Edge::parent();
00325       return n.convert<Node>();
00326     }
00327     OA_ptr<Node>  source () const { return parent(); }
00328     OA_ptr<Node>  tail () const { return parent(); }
00329     OA_ptr<Node>  child () const { 
00330       OA_ptr<Tree::Node> n = Tree::Edge::child();
00331       return n.convert<Node>(); 
00332     }
00333     OA_ptr<Node>  sink () const { return child(); }
00334     OA_ptr<Node>  head () const { return child(); }
00335 
00336     void dump(std::ostream& os);
00337     void dump() { dump(std::cout); }
00338 
00339   private:
00340   };  
00341   //--------------------------------------------------------
00342   
00343   //------------------------------------------------------------------
00350   class NodesIterator : public Tree::NodesIterator {
00351   public:
00352     NodesIterator (Tree& t) : Tree::NodesIterator(t) {}
00353     virtual ~NodesIterator () {}
00354     OA_ptr<Node>  current() const { 
00355       OA_ptr<Tree::Node> n = Tree::NodesIterator::current();
00356       return n.convert<Node>(); 
00357     }
00358   };
00359   
00360   //------------------------------------------------------------------
00367   class EdgesIterator : public Tree::EdgesIterator {
00368   public:
00369     EdgesIterator (Tree& t) : Tree::EdgesIterator(t) {}
00370     virtual ~EdgesIterator () {}
00371     OA_ptr<Edge>  current() const { 
00372       OA_ptr<Tree::Edge> e = Tree::EdgesIterator::current();
00373       return e.convert<Edge>(); 
00374     }
00375   };
00376 
00377   //------------------------------------------------------------------
00381   class OutEdgesIterator : public Tree::OutEdgesIterator {
00382   public:
00383     OutEdgesIterator (Node&  n) : Tree::OutEdgesIterator(n) {}
00384     virtual ~OutEdgesIterator () {}
00385     OA_ptr<Edge>  current() const { 
00386       OA_ptr<Tree::Edge> e = Tree::OutEdgesIterator::current();
00387       return e.convert<Edge>(); 
00388     }
00389   };
00390   
00391   //------------------------------------------------------------------
00396   class ChildNodesIterator : public Tree::ChildNodesIterator {
00397   public:
00398     ChildNodesIterator (Node&  n) : Tree::ChildNodesIterator(n) {}
00399     ~ChildNodesIterator () {}
00400     OA_ptr<Node>  current() const { 
00401       OA_ptr<Tree::Node> n = Tree::ChildNodesIterator::current();
00402       return n.convert<Node>();
00403     }
00404   };
00405  
00406   void str(std::ostream& os);
00407   
00408   void dump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00409   //void dump() { dump(std::cout); }
00410 
00411   //*****************************************************************
00412   // Annotation Interface
00413   //*****************************************************************
00414   void output(IRHandlesIRInterface& ir) ;
00415 
00416   //------------------------------------------------------------------
00420   class PreOrderIterator : public Tree::PreOrderIterator {
00421   public:
00422     PreOrderIterator (Tree& t) : Tree::PreOrderIterator(t) {}
00423     virtual ~PreOrderIterator () {}
00424     OA_ptr<Node>  current() const { 
00425       OA_ptr<Tree::Node> n = PreOrderIterator::current();
00426       return n.convert<Node>();
00427     }
00428   };
00429   //------------------------------------------------------------------
00433   class PostOrderIterator : public Tree::PostOrderIterator {
00434   public:
00435     PostOrderIterator (Tree& t) : Tree::PostOrderIterator(t) {}
00436     virtual ~PostOrderIterator () {}
00437     OA_ptr<Node>  current() const { 
00438       OA_ptr<Tree::Node> n = PostOrderIterator::current();
00439       return n.convert<Node>();
00440     }
00441   };
00442   //------------------------------------------------------------------
00446   class ReversePostOrderIterator : public Tree::ReversePostOrderIterator {
00447   public:
00448     ReversePostOrderIterator (Tree& t) : Tree::ReversePostOrderIterator(t) {}
00449     virtual ~ReversePostOrderIterator () {}
00450     OA_ptr<Node>  current() const { 
00451       OA_ptr<Tree::Node> n = Tree::ReversePostOrderIterator::current();
00452       return n.convert<Node>(); 
00453     }
00454   };
00455  //--------------------------------------------------------------------
00456 
00457 };
00458 
00459 
00460 } // end of OA namespace
00461 
00462 #endif

Generated on Sat Oct 31 05:21:21 2009 for OpenAnalysis by  doxygen 1.6.1