1 //===- llvm/ADT/GraphTraits.h - Graph traits template -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 // This file also defines the marker class Inverse that is used to iterate over
14 // graphs in a graph defined, inverse ordering...
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_ADT_GRAPHTRAITS_H
19 #define LLVM_ADT_GRAPHTRAITS_H
21 #include "llvm/ADT/iterator_range.h"
25 // GraphTraits - This class should be specialized by different graph types...
26 // which is why the default version is empty.
28 template<class GraphType>
30 // Elements to provide:
32 // typedef NodeRef - Type of Node token in the graph, which should
34 // typedef ChildIteratorType - Type used to iterate over children in graph,
35 // dereference to a NodeRef.
37 // static NodeRef getEntryNode(const GraphType &)
38 // Return the entry node of the graph
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.
45 // typedef ...iterator nodes_iterator; - dereference to a NodeRef
46 // static nodes_iterator nodes_begin(GraphType *G)
47 // static nodes_iterator nodes_end (GraphType *G)
48 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
50 // typedef EdgeRef - Type of Edge token in the graph, which should
52 // typedef ChildEdgeIteratorType - Type used to iterate over children edges in
53 // graph, dereference to a EdgeRef.
55 // static ChildEdgeIteratorType child_edge_begin(NodeRef)
56 // static ChildEdgeIteratorType child_edge_end(NodeRef)
57 // Return iterators that point to the beginning and ending of the
58 // edge list for the given callgraph node.
60 // static NodeRef edge_dest(EdgeRef)
61 // Return the destination node of an edge.
63 // static unsigned size (GraphType *G)
64 // Return total number of nodes in the graph
66 // If anyone tries to use this class without having an appropriate
67 // specialization, make an error. If you get this error, it's because you
68 // need to include the appropriate specialization of GraphTraits<> for your
69 // graph, or you need to define it for a new graph type. Either that or
70 // your argument to XXX_begin(...) is unknown or needs to have the proper .h
72 using NodeRef = typename GraphType::UnknownGraphTypeError;
75 // Inverse - This class is used as a little marker class to tell the graph
76 // iterator to iterate over the graph in a graph defined "Inverse" ordering.
77 // Not all graphs define an inverse ordering, and if they do, it depends on
78 // the graph exactly what that is. Here's an example of usage with the
81 // idf_iterator<Method*> I = idf_begin(M), E = idf_end(M);
82 // for (; I != E; ++I) { ... }
84 // Which is equivalent to:
85 // df_iterator<Inverse<Method*>> I = idf_begin(M), E = idf_end(M);
86 // for (; I != E; ++I) { ... }
88 template <class GraphType>
90 const GraphType &Graph;
92 inline Inverse(const GraphType &G) : Graph(G) {}
95 // Provide a partial specialization of GraphTraits so that the inverse of an
96 // inverse falls back to the original graph.
97 template <class T> struct GraphTraits<Inverse<Inverse<T>>> : GraphTraits<T> {};
99 // Provide iterator ranges for the graph traits nodes and children
100 template <class GraphType>
101 iterator_range<typename GraphTraits<GraphType>::nodes_iterator>
102 nodes(const GraphType &G) {
103 return make_range(GraphTraits<GraphType>::nodes_begin(G),
104 GraphTraits<GraphType>::nodes_end(G));
106 template <class GraphType>
107 iterator_range<typename GraphTraits<Inverse<GraphType>>::nodes_iterator>
108 inverse_nodes(const GraphType &G) {
109 return make_range(GraphTraits<Inverse<GraphType>>::nodes_begin(G),
110 GraphTraits<Inverse<GraphType>>::nodes_end(G));
113 template <class GraphType>
114 iterator_range<typename GraphTraits<GraphType>::ChildIteratorType>
115 children(const typename GraphTraits<GraphType>::NodeRef &G) {
116 return make_range(GraphTraits<GraphType>::child_begin(G),
117 GraphTraits<GraphType>::child_end(G));
120 template <class GraphType>
121 iterator_range<typename GraphTraits<Inverse<GraphType>>::ChildIteratorType>
122 inverse_children(const typename GraphTraits<GraphType>::NodeRef &G) {
123 return make_range(GraphTraits<Inverse<GraphType>>::child_begin(G),
124 GraphTraits<Inverse<GraphType>>::child_end(G));
127 template <class GraphType>
128 iterator_range<typename GraphTraits<GraphType>::ChildEdgeIteratorType>
129 children_edges(const typename GraphTraits<GraphType>::NodeRef &G) {
130 return make_range(GraphTraits<GraphType>::child_edge_begin(G),
131 GraphTraits<GraphType>::child_edge_end(G));
134 } // end namespace llvm
136 #endif // LLVM_ADT_GRAPHTRAITS_H