Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 1 | //===--- CompilationGraph.h - The LLVM Compiler Driver ----------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open |
| 6 | // Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Compilation graph - definition. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H |
| 15 | #define LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H |
| 16 | |
| 17 | #include "AutoGenerated.h" |
| 18 | #include "Tool.h" |
| 19 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/GraphTraits.h" |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/iterator" |
| 23 | #include "llvm/ADT/SmallVector.h" |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringMap.h" |
| 25 | #include "llvm/System/Path.h" |
| 26 | |
Mikhail Glushenkov | e0ff9ae | 2008-05-06 18:18:58 +0000 | [diff] [blame] | 27 | #include <cassert> |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 28 | #include <string> |
| 29 | |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 30 | namespace llvmc { |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 31 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 32 | /// StringSet - A wrapper for StringMap that provides set-like |
| 33 | /// functionality. Only insert() and count() methods are used by my |
| 34 | /// code. |
Mikhail Glushenkov | e0ff9ae | 2008-05-06 18:18:58 +0000 | [diff] [blame] | 35 | template <class AllocatorTy = llvm::MallocAllocator> |
| 36 | class StringSet : public llvm::StringMap<char, AllocatorTy> { |
| 37 | typedef llvm::StringMap<char, AllocatorTy> base; |
| 38 | public: |
| 39 | void insert (const std::string& InLang) { |
| 40 | assert(!InLang.empty()); |
| 41 | const char* KeyStart = &InLang[0]; |
| 42 | const char* KeyEnd = KeyStart + InLang.size(); |
| 43 | base::insert(llvm::StringMapEntry<char>:: |
| 44 | Create(KeyStart, KeyEnd, base::getAllocator(), '+')); |
| 45 | } |
| 46 | }; |
| 47 | typedef StringSet<> InputLanguagesSet; |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame] | 48 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 49 | /// Edge - Represents an edge of the compilation graph. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 50 | class Edge : public llvm::RefCountedBaseVPTR<Edge> { |
| 51 | public: |
| 52 | Edge(const std::string& T) : ToolName_(T) {} |
| 53 | virtual ~Edge() {}; |
| 54 | |
| 55 | const std::string& ToolName() const { return ToolName_; } |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame] | 56 | virtual unsigned Weight(const InputLanguagesSet& InLangs) const = 0; |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 57 | private: |
| 58 | std::string ToolName_; |
| 59 | }; |
| 60 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 61 | /// SimpleEdge - An edge that has no properties. |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 62 | class SimpleEdge : public Edge { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 63 | public: |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 64 | SimpleEdge(const std::string& T) : Edge(T) {} |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame] | 65 | unsigned Weight(const InputLanguagesSet&) const { return 1; } |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 66 | }; |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 67 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 68 | /// Node - A node (vertex) of the compilation graph. |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 69 | struct Node { |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 70 | // A Node holds a list of the outward edges. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 71 | typedef llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> container_type; |
| 72 | typedef container_type::iterator iterator; |
| 73 | typedef container_type::const_iterator const_iterator; |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 74 | |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 75 | Node() : OwningGraph(0), InEdges(0) {} |
| 76 | Node(CompilationGraph* G) : OwningGraph(G), InEdges(0) {} |
| 77 | Node(CompilationGraph* G, Tool* T) : |
| 78 | OwningGraph(G), ToolPtr(T), InEdges(0) {} |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 79 | |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 80 | bool HasChildren() const { return !OutEdges.empty(); } |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 81 | const std::string Name() const |
| 82 | { return ToolPtr ? ToolPtr->Name() : "root"; } |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 83 | |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 84 | // Iteration. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 85 | iterator EdgesBegin() { return OutEdges.begin(); } |
| 86 | const_iterator EdgesBegin() const { return OutEdges.begin(); } |
| 87 | iterator EdgesEnd() { return OutEdges.end(); } |
| 88 | const_iterator EdgesEnd() const { return OutEdges.end(); } |
| 89 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 90 | /// AddEdge - Add an outward edge. Takes ownership of the provided |
| 91 | /// Edge object. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 92 | void AddEdge(Edge* E) |
| 93 | { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); } |
| 94 | |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 95 | // Inward edge counter. Used to implement topological sort. |
| 96 | // TOTHINK: Move the mutable counter back into Tool classes? Makes |
| 97 | // us more const-correct. |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 98 | void IncrInEdges() { ++InEdges; } |
| 99 | void DecrInEdges() { --InEdges; } |
| 100 | bool HasNoInEdges() const { return InEdges == 0; } |
| 101 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 102 | // Needed to implement NodeChildIterator/GraphTraits |
| 103 | CompilationGraph* OwningGraph; |
| 104 | // The corresponding Tool. |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 105 | // WARNING: ToolPtr can be NULL (for the root node). |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 106 | llvm::IntrusiveRefCntPtr<Tool> ToolPtr; |
| 107 | // Links to children. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 108 | container_type OutEdges; |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 109 | // Inward edge counter. Updated in |
| 110 | // CompilationGraph::insertEdge(). Used for topological sorting. |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 111 | unsigned InEdges; |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 112 | }; |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 113 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 114 | class NodesIterator; |
| 115 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 116 | /// CompilationGraph - The compilation graph itself. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 117 | class CompilationGraph { |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 118 | /// nodes_map_type - The main data structure. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 119 | typedef llvm::StringMap<Node> nodes_map_type; |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 120 | /// tools_vector_type, tools_map_type - Data structures used to |
| 121 | /// map from language names to tools. (We can have several tools |
| 122 | /// associated with each language name, hence the need for a |
| 123 | /// vector.) |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 124 | typedef |
| 125 | llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> tools_vector_type; |
| 126 | typedef llvm::StringMap<tools_vector_type> tools_map_type; |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 127 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 128 | /// ExtsToLangs - Map from file extensions to language names. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 129 | LanguageMap ExtsToLangs; |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 130 | /// ToolsMap - Map from language names to lists of tool names. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 131 | tools_map_type ToolsMap; |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 132 | /// NodesMap - Map from tool names to Tool objects. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 133 | nodes_map_type NodesMap; |
| 134 | |
| 135 | public: |
| 136 | |
| 137 | CompilationGraph(); |
| 138 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 139 | /// insertNode - Insert a new node into the graph. Takes |
| 140 | /// ownership of the object. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 141 | void insertNode(Tool* T); |
| 142 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 143 | /// insertEdge - Insert a new edge into the graph. Takes ownership |
| 144 | /// of the Edge object. |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 145 | void insertEdge(const std::string& A, Edge* E); |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 146 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 147 | /// Build - Build target(s) from the input file set. Command-line |
| 148 | /// options are passed implicitly as global variables. |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 149 | int Build(llvm::sys::Path const& tempDir); |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 150 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 151 | /// getNode -Return a reference to the node correponding to the |
| 152 | /// given tool name. Throws std::runtime_error. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 153 | Node& getNode(const std::string& ToolName); |
| 154 | const Node& getNode(const std::string& ToolName) const; |
| 155 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 156 | /// viewGraph - This function is meant for use from the debugger. |
| 157 | /// You can just say 'call G->viewGraph()' and a ghostview window |
| 158 | /// should pop up from the program, displaying the compilation |
| 159 | /// graph. This depends on there being a 'dot' and 'gv' program |
| 160 | /// in your path. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 161 | void viewGraph(); |
| 162 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 163 | /// writeGraph - Write a compilation-graph.dot file. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 164 | void writeGraph(); |
| 165 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 166 | // GraphTraits support. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 167 | friend NodesIterator GraphBegin(CompilationGraph*); |
| 168 | friend NodesIterator GraphEnd(CompilationGraph*); |
| 169 | friend void PopulateCompilationGraph(CompilationGraph&); |
| 170 | |
| 171 | private: |
| 172 | // Helper functions. |
| 173 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 174 | /// getLanguage - Find out which language corresponds to the |
| 175 | /// suffix of this file. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 176 | const std::string& getLanguage(const llvm::sys::Path& File) const; |
| 177 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 178 | /// getToolsVector - Return a reference to the list of tool names |
| 179 | /// corresponding to the given language name. Throws |
| 180 | /// std::runtime_error. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 181 | const tools_vector_type& getToolsVector(const std::string& LangName) const; |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 182 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 183 | /// PassThroughGraph - Pass the input file through the toolchain |
| 184 | /// starting at StartNode. |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 185 | void PassThroughGraph (const llvm::sys::Path& In, const Node* StartNode, |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame] | 186 | const InputLanguagesSet& InLangs, |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 187 | const llvm::sys::Path& TempDir) const; |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 188 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 189 | /// FindToolChain - Find head of the toolchain corresponding to the given file. |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 190 | const Node* FindToolChain(const llvm::sys::Path& In, |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame] | 191 | const std::string* forceLanguage, |
| 192 | InputLanguagesSet& InLangs) const; |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 193 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 194 | /// BuildInitial - Traverse the initial parts of the toolchains. |
Mikhail Glushenkov | 4c11a62 | 2008-05-06 18:15:35 +0000 | [diff] [blame] | 195 | void BuildInitial(InputLanguagesSet& InLangs, |
| 196 | const llvm::sys::Path& TempDir); |
| 197 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 198 | /// TopologicalSort - Sort the nodes in topological order. |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 199 | void TopologicalSort(std::vector<const Node*>& Out); |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 200 | /// TopologicalSortFilterJoinNodes - Call TopologicalSort and |
| 201 | /// filter the resulting list to include only Join nodes. |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 202 | void TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out); |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 203 | }; |
| 204 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 205 | // GraphTraits support code. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 206 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 207 | /// NodesIterator - Auxiliary class needed to implement GraphTraits |
| 208 | /// support. Can be generalised to something like value_iterator |
| 209 | /// for map-like containers. |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 210 | class NodesIterator : public llvm::StringMap<Node>::iterator { |
| 211 | typedef llvm::StringMap<Node>::iterator super; |
| 212 | typedef NodesIterator ThisType; |
| 213 | typedef Node* pointer; |
| 214 | typedef Node& reference; |
| 215 | |
| 216 | public: |
| 217 | NodesIterator(super I) : super(I) {} |
| 218 | |
| 219 | inline reference operator*() const { |
| 220 | return super::operator->()->second; |
| 221 | } |
| 222 | inline pointer operator->() const { |
| 223 | return &super::operator->()->second; |
| 224 | } |
| 225 | }; |
| 226 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 227 | inline NodesIterator GraphBegin(CompilationGraph* G) { |
| 228 | return NodesIterator(G->NodesMap.begin()); |
| 229 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 230 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 231 | inline NodesIterator GraphEnd(CompilationGraph* G) { |
| 232 | return NodesIterator(G->NodesMap.end()); |
| 233 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 234 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 235 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame^] | 236 | /// NodeChildIterator - Another auxiliary class needed by GraphTraits. |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 237 | class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> { |
| 238 | typedef NodeChildIterator ThisType; |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 239 | typedef Node::container_type::iterator iterator; |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 240 | |
| 241 | CompilationGraph* OwningGraph; |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 242 | iterator EdgeIter; |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 243 | public: |
| 244 | typedef Node* pointer; |
| 245 | typedef Node& reference; |
| 246 | |
| 247 | NodeChildIterator(Node* N, iterator I) : |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 248 | OwningGraph(N->OwningGraph), EdgeIter(I) {} |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 249 | |
| 250 | const ThisType& operator=(const ThisType& I) { |
| 251 | assert(OwningGraph == I.OwningGraph); |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 252 | EdgeIter = I.EdgeIter; |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 253 | return *this; |
| 254 | } |
| 255 | |
| 256 | inline bool operator==(const ThisType& I) const |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 257 | { return EdgeIter == I.EdgeIter; } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 258 | inline bool operator!=(const ThisType& I) const |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 259 | { return EdgeIter != I.EdgeIter; } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 260 | |
| 261 | inline pointer operator*() const { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 262 | return &OwningGraph->getNode((*EdgeIter)->ToolName()); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 263 | } |
| 264 | inline pointer operator->() const { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 265 | return &OwningGraph->getNode((*EdgeIter)->ToolName()); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 268 | ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 269 | ThisType operator++(int) { // Postincrement |
| 270 | ThisType tmp = *this; |
| 271 | ++*this; |
| 272 | return tmp; |
| 273 | } |
| 274 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 275 | inline ThisType& operator--() { --EdgeIter; return *this; } // Predecrement |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 276 | inline ThisType operator--(int) { // Postdecrement |
| 277 | ThisType tmp = *this; |
| 278 | --*this; |
| 279 | return tmp; |
| 280 | } |
| 281 | |
| 282 | }; |
| 283 | } |
| 284 | |
| 285 | namespace llvm { |
| 286 | template <> |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 287 | struct GraphTraits<llvmc::CompilationGraph*> { |
| 288 | typedef llvmc::CompilationGraph GraphType; |
| 289 | typedef llvmc::Node NodeType; |
| 290 | typedef llvmc::NodeChildIterator ChildIteratorType; |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 291 | |
| 292 | static NodeType* getEntryNode(GraphType* G) { |
| 293 | return &G->getNode("root"); |
| 294 | } |
| 295 | |
| 296 | static ChildIteratorType child_begin(NodeType* N) { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 297 | return ChildIteratorType(N, N->OutEdges.begin()); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 298 | } |
| 299 | static ChildIteratorType child_end(NodeType* N) { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 300 | return ChildIteratorType(N, N->OutEdges.end()); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 303 | typedef llvmc::NodesIterator nodes_iterator; |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 304 | static nodes_iterator nodes_begin(GraphType *G) { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 305 | return GraphBegin(G); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 306 | } |
| 307 | static nodes_iterator nodes_end(GraphType *G) { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 308 | return GraphEnd(G); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 309 | } |
| 310 | }; |
| 311 | |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | #endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H |