Split the Build function into two parts.
[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 false;}
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     const std::string Name() const { return ToolPtr->Name(); }
64
65     iterator EdgesBegin() { return OutEdges.begin(); }
66     const_iterator EdgesBegin() const { return OutEdges.begin(); }
67     iterator EdgesEnd() { return OutEdges.end(); }
68     const_iterator EdgesEnd() const { return OutEdges.end(); }
69
70     // Choose one of the edges based on command-line options.
71     const Edge* ChooseEdge() const;
72
73     // Takes ownership of the object.
74     void AddEdge(Edge* E)
75     { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
76
77     // Needed to implement NodeChildIterator/GraphTraits
78     CompilationGraph* OwningGraph;
79     // The corresponding Tool.
80     llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
81     // Links to children.
82     container_type OutEdges;
83   };
84
85   class NodesIterator;
86
87   class CompilationGraph {
88     typedef llvm::SmallVector<std::string, 3> tools_vector_type;
89     typedef llvm::StringMap<tools_vector_type> tools_map_type;
90     typedef llvm::StringMap<Node> nodes_map_type;
91
92     // Map from file extensions to language names.
93     LanguageMap ExtsToLangs;
94     // Map from language names to lists of tool names.
95     tools_map_type ToolsMap;
96     // Map from tool names to Tool objects.
97     nodes_map_type NodesMap;
98
99   public:
100
101     CompilationGraph();
102
103     // insertVertex - insert a new node into the graph. Takes
104     // ownership of the object.
105     void insertNode(Tool* T);
106
107     // insertEdge - Insert a new edge into the graph. Takes ownership
108     // of the object.
109     void insertEdge(const std::string& A, Edge* E);
110
111     // Build - Build target(s) from the input file set. Command-line
112     // options are passed implicitly as global variables.
113     int Build(llvm::sys::Path const& tempDir) const;
114
115     // Return a reference to the node correponding to the given tool
116     // name. Throws std::runtime_error.
117     Node& getNode(const std::string& ToolName);
118     const Node& getNode(const std::string& ToolName) const;
119
120     // viewGraph - This function is meant for use from the debugger.
121     // You can just say 'call G->viewGraph()' and a ghostview window
122     // should pop up from the program, displaying the compilation
123     // graph. This depends on there being a 'dot' and 'gv' program
124     // in your path.
125     void viewGraph();
126
127     // Write a CompilationGraph.dot file.
128     void writeGraph();
129
130     // GraphTraits support
131     friend NodesIterator GraphBegin(CompilationGraph*);
132     friend NodesIterator GraphEnd(CompilationGraph*);
133     friend void PopulateCompilationGraph(CompilationGraph&);
134
135   private:
136     // Helper functions.
137
138     // Find out which language corresponds to the suffix of this file.
139     const std::string& getLanguage(const llvm::sys::Path& File) const;
140
141     // Return a reference to the list of tool names corresponding to
142     // the given language name. Throws std::runtime_error.
143     const tools_vector_type& getToolsVector(const std::string& LangName) const;
144
145     // Pass the input file through the toolchain.
146     const Tool* PassThroughGraph (llvm::sys::Path& In, llvm::sys::Path Out,
147                                   const llvm::sys::Path& TempDir,
148                                   PathVector& JoinList) const;
149
150   };
151
152   /// GraphTraits support code.
153
154   // Auxiliary class needed to implement GraphTraits support.  Can be
155   // generalised to something like value_iterator for map-like
156   // containers.
157   class NodesIterator : public llvm::StringMap<Node>::iterator {
158     typedef llvm::StringMap<Node>::iterator super;
159     typedef NodesIterator ThisType;
160     typedef Node* pointer;
161     typedef Node& reference;
162
163   public:
164     NodesIterator(super I) : super(I) {}
165
166     inline reference operator*() const {
167       return super::operator->()->second;
168     }
169     inline pointer operator->() const {
170       return &super::operator->()->second;
171     }
172   };
173
174   inline NodesIterator GraphBegin(CompilationGraph* G) {
175     return NodesIterator(G->NodesMap.begin());
176   }
177
178   inline NodesIterator GraphEnd(CompilationGraph* G) {
179     return NodesIterator(G->NodesMap.end());
180   }
181
182
183   // Another auxiliary class needed by GraphTraits.
184   class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> {
185     typedef NodeChildIterator ThisType;
186     typedef Node::container_type::iterator iterator;
187
188     CompilationGraph* OwningGraph;
189     iterator EdgeIter;
190   public:
191     typedef Node* pointer;
192     typedef Node& reference;
193
194     NodeChildIterator(Node* N, iterator I) :
195       OwningGraph(N->OwningGraph), EdgeIter(I) {}
196
197     const ThisType& operator=(const ThisType& I) {
198       assert(OwningGraph == I.OwningGraph);
199       EdgeIter = I.EdgeIter;
200       return *this;
201     }
202
203     inline bool operator==(const ThisType& I) const
204     { return EdgeIter == I.EdgeIter; }
205     inline bool operator!=(const ThisType& I) const
206     { return EdgeIter != I.EdgeIter; }
207
208     inline pointer operator*() const {
209       return &OwningGraph->getNode((*EdgeIter)->ToolName());
210     }
211     inline pointer operator->() const {
212       return &OwningGraph->getNode((*EdgeIter)->ToolName());
213     }
214
215     ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
216     ThisType operator++(int) { // Postincrement
217       ThisType tmp = *this;
218       ++*this;
219       return tmp;
220     }
221
222     inline ThisType& operator--() { --EdgeIter; return *this; }  // Predecrement
223     inline ThisType operator--(int) { // Postdecrement
224       ThisType tmp = *this;
225       --*this;
226       return tmp;
227     }
228
229   };
230 }
231
232 namespace llvm {
233   template <>
234   struct GraphTraits<llvmcc::CompilationGraph*> {
235     typedef llvmcc::CompilationGraph GraphType;
236     typedef llvmcc::Node NodeType;
237     typedef llvmcc::NodeChildIterator ChildIteratorType;
238
239     static NodeType* getEntryNode(GraphType* G) {
240       return &G->getNode("root");
241     }
242
243     static ChildIteratorType child_begin(NodeType* N) {
244       return ChildIteratorType(N, N->OutEdges.begin());
245     }
246     static ChildIteratorType child_end(NodeType* N) {
247       return ChildIteratorType(N, N->OutEdges.end());
248     }
249
250     typedef llvmcc::NodesIterator nodes_iterator;
251     static nodes_iterator nodes_begin(GraphType *G) {
252       return GraphBegin(G);
253     }
254     static nodes_iterator nodes_end(GraphType *G) {
255       return GraphEnd(G);
256     }
257   };
258
259 }
260
261 #endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H