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 true;}
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;
59 Node(CompilationGraph* G) : OwningGraph(G) {}
60 Node(CompilationGraph* G, Tool* T) : OwningGraph(G), ToolPtr(T) {}
62 bool HasChildren() const { return !OutEdges.empty(); }
64 iterator EdgesBegin() { return OutEdges.begin(); }
65 const_iterator EdgesBegin() const { return OutEdges.begin(); }
66 iterator EdgesEnd() { return OutEdges.end(); }
67 const_iterator EdgesEnd() const { return OutEdges.end(); }
69 // Takes ownership of the object.
71 { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
73 // Needed to implement NodeChildIterator/GraphTraits
74 CompilationGraph* OwningGraph;
75 // The corresponding Tool.
76 llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
78 container_type OutEdges;
83 class CompilationGraph {
84 typedef llvm::SmallVector<std::string, 3> tools_vector_type;
85 typedef llvm::StringMap<tools_vector_type> tools_map_type;
86 typedef llvm::StringMap<Node> nodes_map_type;
88 // Map from file extensions to language names.
89 LanguageMap ExtsToLangs;
90 // Map from language names to lists of tool names.
91 tools_map_type ToolsMap;
92 // Map from tool names to Tool objects.
93 nodes_map_type NodesMap;
99 // insertVertex - insert a new node into the graph. Takes
100 // ownership of the object.
101 void insertNode(Tool* T);
103 // insertEdge - Insert a new edge into the graph. Takes ownership
105 void insertEdge(const std::string& A, Edge* E);
107 // Build - Build target(s) from the input file set. Command-line
108 // options are passed implicitly as global variables.
109 int Build(llvm::sys::Path const& tempDir) const;
111 // Return a reference to the node correponding to the given tool
112 // name. Throws std::runtime_error.
113 Node& getNode(const std::string& ToolName);
114 const Node& getNode(const std::string& ToolName) const;
116 // viewGraph - This function is meant for use from the debugger.
117 // You can just say 'call G->viewGraph()' and a ghostview window
118 // should pop up from the program, displaying the compilation
119 // graph. This depends on there being a 'dot' and 'gv' program
123 // Write a CompilationGraph.dot file.
126 // GraphTraits support
127 friend NodesIterator GraphBegin(CompilationGraph*);
128 friend NodesIterator GraphEnd(CompilationGraph*);
129 friend void PopulateCompilationGraph(CompilationGraph&);
134 // Find out which language corresponds to the suffix of this file.
135 const std::string& getLanguage(const llvm::sys::Path& File) const;
137 // Return a reference to the list of tool names corresponding to
138 // the given language name. Throws std::runtime_error.
139 const tools_vector_type& getToolsVector(const std::string& LangName) const;
142 /// GraphTraits support code.
144 // Auxiliary class needed to implement GraphTraits support. Can be
145 // generalised to something like value_iterator for map-like
147 class NodesIterator : public llvm::StringMap<Node>::iterator {
148 typedef llvm::StringMap<Node>::iterator super;
149 typedef NodesIterator ThisType;
150 typedef Node* pointer;
151 typedef Node& reference;
154 NodesIterator(super I) : super(I) {}
156 inline reference operator*() const {
157 return super::operator->()->second;
159 inline pointer operator->() const {
160 return &super::operator->()->second;
164 inline NodesIterator GraphBegin(CompilationGraph* G) {
165 return NodesIterator(G->NodesMap.begin());
168 inline NodesIterator GraphEnd(CompilationGraph* G) {
169 return NodesIterator(G->NodesMap.end());
173 // Another auxiliary class needed by GraphTraits.
174 class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> {
175 typedef NodeChildIterator ThisType;
176 typedef Node::container_type::iterator iterator;
178 CompilationGraph* OwningGraph;
181 typedef Node* pointer;
182 typedef Node& reference;
184 NodeChildIterator(Node* N, iterator I) :
185 OwningGraph(N->OwningGraph), EdgeIter(I) {}
187 const ThisType& operator=(const ThisType& I) {
188 assert(OwningGraph == I.OwningGraph);
189 EdgeIter = I.EdgeIter;
193 inline bool operator==(const ThisType& I) const
194 { return EdgeIter == I.EdgeIter; }
195 inline bool operator!=(const ThisType& I) const
196 { return EdgeIter != I.EdgeIter; }
198 inline pointer operator*() const {
199 return &OwningGraph->getNode((*EdgeIter)->ToolName());
201 inline pointer operator->() const {
202 return &OwningGraph->getNode((*EdgeIter)->ToolName());
205 ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
206 ThisType operator++(int) { // Postincrement
207 ThisType tmp = *this;
212 inline ThisType& operator--() { --EdgeIter; return *this; } // Predecrement
213 inline ThisType operator--(int) { // Postdecrement
214 ThisType tmp = *this;
224 struct GraphTraits<llvmcc::CompilationGraph*> {
225 typedef llvmcc::CompilationGraph GraphType;
226 typedef llvmcc::Node NodeType;
227 typedef llvmcc::NodeChildIterator ChildIteratorType;
229 static NodeType* getEntryNode(GraphType* G) {
230 return &G->getNode("root");
233 static ChildIteratorType child_begin(NodeType* N) {
234 return ChildIteratorType(N, N->OutEdges.begin());
236 static ChildIteratorType child_end(NodeType* N) {
237 return ChildIteratorType(N, N->OutEdges.end());
240 typedef llvmcc::NodesIterator nodes_iterator;
241 static nodes_iterator nodes_begin(GraphType *G) {
242 return GraphBegin(G);
244 static nodes_iterator nodes_end(GraphType *G) {
251 #endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H