d953aeea9003cf891eabd865676dab16a92afd45
[oota-llvm.git] / tools / llvmc2 / CompilationGraph.h
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
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"
26
27 #include <string>
28
29 namespace llvmcc {
30
31   // An edge in the graph.
32   class Edge : public llvm::RefCountedBaseVPTR<Edge> {
33   public:
34     Edge(const std::string& T) : ToolName_(T) {}
35     virtual ~Edge() {};
36
37     const std::string& ToolName() const { return ToolName_; }
38     virtual bool isEnabled() const = 0;
39     virtual bool isDefault() const = 0;
40   private:
41     std::string ToolName_;
42   };
43
44   // Edges with no properties are instances of this class.
45   class SimpleEdge : public Edge {
46   public:
47     SimpleEdge(const std::string& T) : Edge(T) {}
48     bool isEnabled() const { return true;}
49     bool isDefault() const { return true;}
50   };
51
52   // A node in the graph.
53   struct Node {
54     typedef llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> container_type;
55     typedef container_type::iterator iterator;
56     typedef container_type::const_iterator const_iterator;
57
58     Node() {}
59     Node(CompilationGraph* G) : OwningGraph(G) {}
60     Node(CompilationGraph* G, Tool* T) : OwningGraph(G), ToolPtr(T) {}
61
62     bool HasChildren() const { return !OutEdges.empty(); }
63
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(); }
68
69     // Takes ownership of the object.
70     void AddEdge(Edge* E)
71     { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
72
73     // Needed to implement NodeChildIterator/GraphTraits
74     CompilationGraph* OwningGraph;
75     // The corresponding Tool.
76     llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
77     // Links to children.
78     container_type OutEdges;
79   };
80
81   class NodesIterator;
82
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;
87
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;
94
95   public:
96
97     CompilationGraph();
98
99     // insertVertex - insert a new node into the graph. Takes
100     // ownership of the object.
101     void insertNode(Tool* T);
102
103     // insertEdge - Insert a new edge into the graph. Takes ownership
104     // of the object.
105     void insertEdge(const std::string& A, Edge* E);
106
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;
110
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;
115
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
120     // in your path.
121     void viewGraph();
122
123     // Write a CompilationGraph.dot file.
124     void writeGraph();
125
126     // GraphTraits support
127     friend NodesIterator GraphBegin(CompilationGraph*);
128     friend NodesIterator GraphEnd(CompilationGraph*);
129     friend void PopulateCompilationGraph(CompilationGraph&);
130
131   private:
132     // Helper functions.
133
134     // Find out which language corresponds to the suffix of this file.
135     const std::string& getLanguage(const llvm::sys::Path& File) const;
136
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;
140   };
141
142   /// GraphTraits support code.
143
144   // Auxiliary class needed to implement GraphTraits support.  Can be
145   // generalised to something like value_iterator for map-like
146   // containers.
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;
152
153   public:
154     NodesIterator(super I) : super(I) {}
155
156     inline reference operator*() const {
157       return super::operator->()->second;
158     }
159     inline pointer operator->() const {
160       return &super::operator->()->second;
161     }
162   };
163
164   inline NodesIterator GraphBegin(CompilationGraph* G) {
165     return NodesIterator(G->NodesMap.begin());
166   }
167
168   inline NodesIterator GraphEnd(CompilationGraph* G) {
169     return NodesIterator(G->NodesMap.end());
170   }
171
172
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;
177
178     CompilationGraph* OwningGraph;
179     iterator EdgeIter;
180   public:
181     typedef Node* pointer;
182     typedef Node& reference;
183
184     NodeChildIterator(Node* N, iterator I) :
185       OwningGraph(N->OwningGraph), EdgeIter(I) {}
186
187     const ThisType& operator=(const ThisType& I) {
188       assert(OwningGraph == I.OwningGraph);
189       EdgeIter = I.EdgeIter;
190       return *this;
191     }
192
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; }
197
198     inline pointer operator*() const {
199       return &OwningGraph->getNode((*EdgeIter)->ToolName());
200     }
201     inline pointer operator->() const {
202       return &OwningGraph->getNode((*EdgeIter)->ToolName());
203     }
204
205     ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
206     ThisType operator++(int) { // Postincrement
207       ThisType tmp = *this;
208       ++*this;
209       return tmp;
210     }
211
212     inline ThisType& operator--() { --EdgeIter; return *this; }  // Predecrement
213     inline ThisType operator--(int) { // Postdecrement
214       ThisType tmp = *this;
215       --*this;
216       return tmp;
217     }
218
219   };
220 }
221
222 namespace llvm {
223   template <>
224   struct GraphTraits<llvmcc::CompilationGraph*> {
225     typedef llvmcc::CompilationGraph GraphType;
226     typedef llvmcc::Node NodeType;
227     typedef llvmcc::NodeChildIterator ChildIteratorType;
228
229     static NodeType* getEntryNode(GraphType* G) {
230       return &G->getNode("root");
231     }
232
233     static ChildIteratorType child_begin(NodeType* N) {
234       return ChildIteratorType(N, N->OutEdges.begin());
235     }
236     static ChildIteratorType child_end(NodeType* N) {
237       return ChildIteratorType(N, N->OutEdges.end());
238     }
239
240     typedef llvmcc::NodesIterator nodes_iterator;
241     static nodes_iterator nodes_begin(GraphType *G) {
242       return GraphBegin(G);
243     }
244     static nodes_iterator nodes_end(GraphType *G) {
245       return GraphEnd(G);
246     }
247   };
248
249 }
250
251 #endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H