1 //===-- llvm/ADT/GraphTraits.h - Graph traits template ----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the little GraphTraits<X> template class that should be
11 // specialized by classes that want to be iteratable by generic graph iterators.
12 //
13 // This file also defines the marker class Inverse that is used to iterate over
14 // graphs in a graph defined, inverse ordering...
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef LLVM_ADT_GRAPHTRAITS_H
19 #define LLVM_ADT_GRAPHTRAITS_H
20 
21 namespace llvm {
22 
23 // GraphTraits - This class should be specialized by different graph types...
24 // which is why the default version is empty.
25 //
26 template<class GraphType>
27 struct GraphTraits {
28   // Elements to provide:
29 
30   // typedef NodeType          - Type of Node in the graph
31   // typedef ChildIteratorType - Type used to iterate over children in graph
32 
33   // static NodeType *getEntryNode(const GraphType &)
34   //    Return the entry node of the graph
35 
36   // static ChildIteratorType child_begin(NodeType *)
37   // static ChildIteratorType child_end  (NodeType *)
38   //    Return iterators that point to the beginning and ending of the child
39   //    node list for the specified node.
40   //
41 
42 
43   // typedef  ...iterator nodes_iterator;
44   // static nodes_iterator nodes_begin(GraphType *G)
45   // static nodes_iterator nodes_end  (GraphType *G)
46   //    nodes_iterator/begin/end - Allow iteration over all nodes in the graph
47 
48   // static unsigned       size       (GraphType *G)
49   //    Return total number of nodes in the graph
50   //
51 
52 
53   // If anyone tries to use this class without having an appropriate
54   // specialization, make an error.  If you get this error, it's because you
55   // need to include the appropriate specialization of GraphTraits<> for your
56   // graph, or you need to define it for a new graph type. Either that or
57   // your argument to XXX_begin(...) is unknown or needs to have the proper .h
58   // file #include'd.
59   //
60   typedef typename GraphType::UnknownGraphTypeError NodeType;
61 };
62 
63 
64 // Inverse - This class is used as a little marker class to tell the graph
65 // iterator to iterate over the graph in a graph defined "Inverse" ordering.
66 // Not all graphs define an inverse ordering, and if they do, it depends on
67 // the graph exactly what that is.  Here's an example of usage with the
68 // df_iterator:
69 //
70 // idf_iterator<Method*> I = idf_begin(M), E = idf_end(M);
71 // for (; I != E; ++I) { ... }
72 //
73 // Which is equivalent to:
74 // df_iterator<Inverse<Method*> > I = idf_begin(M), E = idf_end(M);
75 // for (; I != E; ++I) { ... }
76 //
77 template <class GraphType>
78 struct Inverse {
79   const GraphType &Graph;
80 
InverseInverse81   inline Inverse(const GraphType &G) : Graph(G) {}
82 };
83 
84 // Provide a partial specialization of GraphTraits so that the inverse of an
85 // inverse falls back to the original graph.
86 template<class T>
87 struct GraphTraits<Inverse<Inverse<T> > > {
88   typedef typename GraphTraits<T>::NodeType NodeType;
89   typedef typename GraphTraits<T>::ChildIteratorType ChildIteratorType;
90 
91   static NodeType *getEntryNode(Inverse<Inverse<T> > *G) {
92     return GraphTraits<T>::getEntryNode(G->Graph.Graph);
93   }
94 
95   static ChildIteratorType child_begin(NodeType* N) {
96     return GraphTraits<T>::child_begin(N);
97   }
98 
99   static ChildIteratorType child_end(NodeType* N) {
100     return GraphTraits<T>::child_end(N);
101   }
102 };
103 
104 } // End llvm namespace
105 
106 #endif
107