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 #include "llvm/ADT/iterator_range.h" 22 23 namespace llvm { 24 25 // GraphTraits - This class should be specialized by different graph types... 26 // which is why the default version is empty. 27 // 28 template<class GraphType> 29 struct GraphTraits { 30 // Elements to provide: 31 32 // typedef NodeRef - Type of Node token in the graph, which should 33 // be cheap to copy. 34 // typedef ChildIteratorType - Type used to iterate over children in graph, 35 // dereference to a NodeRef. 36 37 // static NodeRef getEntryNode(const GraphType &) 38 // Return the entry node of the graph 39 40 // static ChildIteratorType child_begin(NodeRef) 41 // static ChildIteratorType child_end (NodeRef) 42 // Return iterators that point to the beginning and ending of the child 43 // node list for the specified node. 44 // 45 46 // typedef ...iterator nodes_iterator; - dereference to a NodeRef 47 // static nodes_iterator nodes_begin(GraphType *G) 48 // static nodes_iterator nodes_end (GraphType *G) 49 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph 50 51 // static unsigned size (GraphType *G) 52 // Return total number of nodes in the graph 53 // 54 55 56 // If anyone tries to use this class without having an appropriate 57 // specialization, make an error. If you get this error, it's because you 58 // need to include the appropriate specialization of GraphTraits<> for your 59 // graph, or you need to define it for a new graph type. Either that or 60 // your argument to XXX_begin(...) is unknown or needs to have the proper .h 61 // file #include'd. 62 // 63 typedef typename GraphType::UnknownGraphTypeError NodeRef; 64 }; 65 66 67 // Inverse - This class is used as a little marker class to tell the graph 68 // iterator to iterate over the graph in a graph defined "Inverse" ordering. 69 // Not all graphs define an inverse ordering, and if they do, it depends on 70 // the graph exactly what that is. Here's an example of usage with the 71 // df_iterator: 72 // 73 // idf_iterator<Method*> I = idf_begin(M), E = idf_end(M); 74 // for (; I != E; ++I) { ... } 75 // 76 // Which is equivalent to: 77 // df_iterator<Inverse<Method*> > I = idf_begin(M), E = idf_end(M); 78 // for (; I != E; ++I) { ... } 79 // 80 template <class GraphType> 81 struct Inverse { 82 const GraphType &Graph; 83 84 inline Inverse(const GraphType &G) : Graph(G) {} 85 }; 86 87 // Provide a partial specialization of GraphTraits so that the inverse of an 88 // inverse falls back to the original graph. 89 template <class T> struct GraphTraits<Inverse<Inverse<T>>> : GraphTraits<T> {}; 90 91 // Provide iterator ranges for the graph traits nodes and children 92 template <class GraphType> 93 iterator_range<typename GraphTraits<GraphType>::nodes_iterator> 94 nodes(const GraphType &G) { 95 return make_range(GraphTraits<GraphType>::nodes_begin(G), 96 GraphTraits<GraphType>::nodes_end(G)); 97 } 98 template <class GraphType> 99 iterator_range<typename GraphTraits<Inverse<GraphType>>::nodes_iterator> 100 inverse_nodes(const GraphType &G) { 101 return make_range(GraphTraits<Inverse<GraphType>>::nodes_begin(G), 102 GraphTraits<Inverse<GraphType>>::nodes_end(G)); 103 } 104 105 template <class GraphType> 106 iterator_range<typename GraphTraits<GraphType>::ChildIteratorType> 107 children(const typename GraphTraits<GraphType>::NodeRef &G) { 108 return make_range(GraphTraits<GraphType>::child_begin(G), 109 GraphTraits<GraphType>::child_end(G)); 110 } 111 112 template <class GraphType> 113 iterator_range<typename GraphTraits<Inverse<GraphType>>::ChildIteratorType> 114 inverse_children(const typename GraphTraits<GraphType>::NodeRef &G) { 115 return make_range(GraphTraits<Inverse<GraphType>>::child_begin(G), 116 GraphTraits<Inverse<GraphType>>::child_end(G)); 117 } 118 } // End llvm namespace 119 120 #endif 121