1 //===--- CompilationGraph.h - The LLVM Compiler Driver ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open
6 // Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Compilation graph - definition.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H
15 #define LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H
17 #include "AutoGenerated.h"
20 #include "llvm/ADT/GraphTraits.h"
21 #include "llvm/ADT/IntrusiveRefCntPtr.h"
22 #include "llvm/ADT/iterator"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringMap.h"
25 #include "llvm/System/Path.h"
31 // An edge in the graph.
32 class Edge : public llvm::RefCountedBaseVPTR<Edge> {
34 Edge(const std::string& T) : ToolName_(T) {}
37 const std::string& ToolName() const { return ToolName_; }
38 virtual bool isEnabled() const = 0;
39 virtual bool isDefault() const = 0;
41 std::string ToolName_;
44 // Edges with no properties are instances of this class.
45 class SimpleEdge : public Edge {
47 SimpleEdge(const std::string& T) : Edge(T) {}
48 bool isEnabled() const { return false;}
49 bool isDefault() const { return true;}
52 // A node in the graph.
54 typedef llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> container_type;
55 typedef container_type::iterator iterator;
56 typedef container_type::const_iterator const_iterator;
58 Node() : OwningGraph(0), InEdges(0) {}
59 Node(CompilationGraph* G) : OwningGraph(G), InEdges(0) {}
60 Node(CompilationGraph* G, Tool* T) :
61 OwningGraph(G), ToolPtr(T), InEdges(0) {}
63 bool HasChildren() const { return !OutEdges.empty(); }
64 const std::string Name() const { return ToolPtr->Name(); }
67 iterator EdgesBegin() { return OutEdges.begin(); }
68 const_iterator EdgesBegin() const { return OutEdges.begin(); }
69 iterator EdgesEnd() { return OutEdges.end(); }
70 const_iterator EdgesEnd() const { return OutEdges.end(); }
72 // Choose one of the outward edges based on command-line options.
73 const Edge* ChooseEdge() const;
75 // Add an outward edge. Takes ownership of the Edge object.
77 { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
79 // Inward edge counter. Used by Build() to implement topological
81 void IncrInEdges() { ++InEdges; }
82 void DecrInEdges() { --InEdges; }
83 bool HasNoInEdges() const { return InEdges == 0; }
85 // Needed to implement NodeChildIterator/GraphTraits
86 CompilationGraph* OwningGraph;
87 // The corresponding Tool.
88 llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
90 container_type OutEdges;
97 class CompilationGraph {
98 typedef llvm::SmallVector<std::string, 3> tools_vector_type;
99 typedef llvm::StringMap<tools_vector_type> tools_map_type;
100 typedef llvm::StringMap<Node> nodes_map_type;
102 // Map from file extensions to language names.
103 LanguageMap ExtsToLangs;
104 // Map from language names to lists of tool names.
105 tools_map_type ToolsMap;
106 // Map from tool names to Tool objects.
107 nodes_map_type NodesMap;
113 // insertVertex - insert a new node into the graph. Takes
114 // ownership of the object.
115 void insertNode(Tool* T);
117 // insertEdge - Insert a new edge into the graph. Takes ownership
118 // of the Edge object.
119 void insertEdge(const std::string& A, Edge* E);
121 // Build - Build target(s) from the input file set. Command-line
122 // options are passed implicitly as global variables.
123 int Build(llvm::sys::Path const& tempDir) const;
125 // Return a reference to the node correponding to the given tool
126 // name. Throws std::runtime_error.
127 Node& getNode(const std::string& ToolName);
128 const Node& getNode(const std::string& ToolName) const;
130 // viewGraph - This function is meant for use from the debugger.
131 // You can just say 'call G->viewGraph()' and a ghostview window
132 // should pop up from the program, displaying the compilation
133 // graph. This depends on there being a 'dot' and 'gv' program
137 // Write a CompilationGraph.dot file.
140 // GraphTraits support
141 friend NodesIterator GraphBegin(CompilationGraph*);
142 friend NodesIterator GraphEnd(CompilationGraph*);
143 friend void PopulateCompilationGraph(CompilationGraph&);
148 // Find out which language corresponds to the suffix of this file.
149 const std::string& getLanguage(const llvm::sys::Path& File) const;
151 // Return a reference to the list of tool names corresponding to
152 // the given language name. Throws std::runtime_error.
153 const tools_vector_type& getToolsVector(const std::string& LangName) const;
155 // Pass the input file through the toolchain.
156 const JoinTool* PassThroughGraph (llvm::sys::Path& In,
157 const llvm::sys::Path& TempDir) const;
161 /// GraphTraits support code.
163 // Auxiliary class needed to implement GraphTraits support. Can be
164 // generalised to something like value_iterator for map-like
166 class NodesIterator : public llvm::StringMap<Node>::iterator {
167 typedef llvm::StringMap<Node>::iterator super;
168 typedef NodesIterator ThisType;
169 typedef Node* pointer;
170 typedef Node& reference;
173 NodesIterator(super I) : super(I) {}
175 inline reference operator*() const {
176 return super::operator->()->second;
178 inline pointer operator->() const {
179 return &super::operator->()->second;
183 inline NodesIterator GraphBegin(CompilationGraph* G) {
184 return NodesIterator(G->NodesMap.begin());
187 inline NodesIterator GraphEnd(CompilationGraph* G) {
188 return NodesIterator(G->NodesMap.end());
192 // Another auxiliary class needed by GraphTraits.
193 class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> {
194 typedef NodeChildIterator ThisType;
195 typedef Node::container_type::iterator iterator;
197 CompilationGraph* OwningGraph;
200 typedef Node* pointer;
201 typedef Node& reference;
203 NodeChildIterator(Node* N, iterator I) :
204 OwningGraph(N->OwningGraph), EdgeIter(I) {}
206 const ThisType& operator=(const ThisType& I) {
207 assert(OwningGraph == I.OwningGraph);
208 EdgeIter = I.EdgeIter;
212 inline bool operator==(const ThisType& I) const
213 { return EdgeIter == I.EdgeIter; }
214 inline bool operator!=(const ThisType& I) const
215 { return EdgeIter != I.EdgeIter; }
217 inline pointer operator*() const {
218 return &OwningGraph->getNode((*EdgeIter)->ToolName());
220 inline pointer operator->() const {
221 return &OwningGraph->getNode((*EdgeIter)->ToolName());
224 ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
225 ThisType operator++(int) { // Postincrement
226 ThisType tmp = *this;
231 inline ThisType& operator--() { --EdgeIter; return *this; } // Predecrement
232 inline ThisType operator--(int) { // Postdecrement
233 ThisType tmp = *this;
243 struct GraphTraits<llvmcc::CompilationGraph*> {
244 typedef llvmcc::CompilationGraph GraphType;
245 typedef llvmcc::Node NodeType;
246 typedef llvmcc::NodeChildIterator ChildIteratorType;
248 static NodeType* getEntryNode(GraphType* G) {
249 return &G->getNode("root");
252 static ChildIteratorType child_begin(NodeType* N) {
253 return ChildIteratorType(N, N->OutEdges.begin());
255 static ChildIteratorType child_end(NodeType* N) {
256 return ChildIteratorType(N, N->OutEdges.end());
259 typedef llvmcc::NodesIterator nodes_iterator;
260 static nodes_iterator nodes_begin(GraphType *G) {
261 return GraphBegin(G);
263 static nodes_iterator nodes_end(GraphType *G) {
270 #endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H